Debian Codegraph @

src/f32-f16-vcvt/gen/f32-f16-vcvt-wasmsimd-u8.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/f32-f16-vcvt/wasmsimd.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2021 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9
10 #include <assert.h>
11
12 #include <wasm_simd128.h>
13
14 #include "xnnpack/common.h"
15 #include "xnnpack/vcvt.h"
16
17
18 void xnn_f32_f16_vcvt_ukernel__wasmsimd_u8(
19 size_t batch,
20 const float* input,
21 xnn_float16* output,
22 const void* params) XNN_OOB_READS
23 {
24 assert(batch != 0);
25 assert(batch % sizeof(float) == 0);
26 assert(input != NULL);
27 assert(output != NULL);
28
29 const v128_t vexp_bias = wasm_u32x4_const_splat(UINT32_C(0x07800000));
30 const v128_t vscale_to_inf = wasm_f32x4_const_splat(0x1.0p+112f);
31 const v128_t vexpw_max = wasm_u32x4_const_splat(UINT32_C(0x7F800000));
32 const v128_t vscale_to_zero = wasm_f32x4_const_splat(0x1.0p-110f);
33 const v128_t vbias_min = wasm_u32x4_const_splat(UINT32_C(0x40008000));
34 const v128_t vmanth_mask = wasm_u32x4_const_splat(UINT32_C(0x00000FFF));
35 const v128_t vexph_mask = wasm_u32x4_const_splat(UINT32_C(0x00007C00));
36 const v128_t vnanh = wasm_u16x8_const_splat(UINT16_C(0x7E00));
37
38 XNN_FORCE_REALIZATION(vexp_bias);
39 XNN_FORCE_REALIZATION(vscale_to_inf);
40 XNN_FORCE_REALIZATION(vexpw_max);
41 XNN_FORCE_REALIZATION(vscale_to_zero);
42 XNN_FORCE_REALIZATION(vbias_min);
43 XNN_FORCE_REALIZATION(vmanth_mask);
44 XNN_FORCE_REALIZATION(vexph_mask);
45 XNN_FORCE_REALIZATION(vnanh);
46
47 uint16_t* o = (uint16_t*) output;
48 for (; batch >= 8 * sizeof(float); batch -= 8 * sizeof(float)) {
49 const v128_t vx_lo = wasm_v128_load(input);
50 const v128_t vx_hi = wasm_v128_load(input + 4);
51 input += 8;
52
53 const v128_t vabsx_lo = wasm_f32x4_abs(vx_lo);
54 const v128_t vabsx_hi = wasm_f32x4_abs(vx_hi);
55
56 const v128_t vsignx_lo = wasm_v128_xor(vx_lo, vabsx_lo);
57 const v128_t vsignx_hi = wasm_v128_xor(vx_hi, vabsx_hi);
58 v128_t vbias_lo = wasm_i32x4_add(vabsx_lo, vexp_bias);
59 v128_t vbias_hi = wasm_i32x4_add(vabsx_hi, vexp_bias);
60 v128_t vf_lo = wasm_f32x4_mul(vabsx_lo, vscale_to_inf);
61 v128_t vf_hi = wasm_f32x4_mul(vabsx_hi, vscale_to_inf);
62 const v128_t vnanmaskw_lo = wasm_i32x4_gt(vabsx_lo, vexpw_max);
63 const v128_t vnanmaskw_hi = wasm_i32x4_gt(vabsx_hi, vexpw_max);
64
65 vbias_lo = wasm_v128_and(vbias_lo, vexpw_max);
66 vbias_hi = wasm_v128_and(vbias_hi, vexpw_max);
67 vf_lo = wasm_f32x4_mul(vf_lo, vscale_to_zero);
68 vf_hi = wasm_f32x4_mul(vf_hi, vscale_to_zero);
69 const v128_t vnanmaskh = wasm_i16x8_narrow_i32x4(vnanmaskw_lo, vnanmaskw_hi);
70 const v128_t vsignh = wasm_i16x8_narrow_i32x4(vsignx_lo, vsignx_hi);
71
72 vbias_lo = wasm_i16x8_max(vbias_lo, vbias_min);
73 vbias_hi = wasm_i16x8_max(vbias_hi, vbias_min);
74
75 vf_lo = wasm_f32x4_add(vf_lo, vbias_lo);
76 vf_hi = wasm_f32x4_add(vf_hi, vbias_hi);
77
78 v128_t vexpw_lo = wasm_i32x4_shr(vf_lo, 13);
79 v128_t vexpw_hi = wasm_i32x4_shr(vf_hi, 13);
80 const v128_t vmantw_lo = wasm_v128_and(vf_lo, vmanth_mask);
81 const v128_t vmantw_hi = wasm_v128_and(vf_hi, vmanth_mask);
82
83 vexpw_lo = wasm_v128_and(vexpw_lo, vexph_mask);
84 vexpw_hi = wasm_v128_and(vexpw_hi, vexph_mask);
85
86 const v128_t vnonsignw_lo = wasm_i32x4_add(vmantw_lo, vexpw_lo);
87 const v128_t vnonsignw_hi = wasm_i32x4_add(vmantw_hi, vexpw_hi);
88
89 const v128_t vnonsignh = wasm_i16x8_narrow_i32x4(vnonsignw_lo, vnonsignw_hi);
90
91 const v128_t vabsh = wasm_v128_bitselect(vnanh, vnonsignh, vnanmaskh);
92
93 const v128_t vh = wasm_v128_or(vabsh, vsignh);
94
95 wasm_v128_store(o, vh);
96 o += 8;
97 }
98 if XNN_UNPREDICTABLE(batch != 0) {
99 const v128_t vx_lo = wasm_v128_load(input);
100 const float* input_hi = (const float*) ((uintptr_t) input + (batch & (4 * sizeof(float))));
101 const v128_t vx_hi = wasm_v128_load(input_hi);
102
103 const v128_t vabsx_lo = wasm_f32x4_abs(vx_lo);
104 const v128_t vabsx_hi = wasm_f32x4_abs(vx_hi);
105
106 const v128_t vsignx_lo = wasm_v128_xor(vx_lo, vabsx_lo);
107 const v128_t vsignx_hi = wasm_v128_xor(vx_hi, vabsx_hi);
108 v128_t vbias_lo = wasm_i32x4_add(vabsx_lo, vexp_bias);
109 v128_t vbias_hi = wasm_i32x4_add(vabsx_hi, vexp_bias);
110 v128_t vf_lo = wasm_f32x4_mul(vabsx_lo, vscale_to_inf);
111 v128_t vf_hi = wasm_f32x4_mul(vabsx_hi, vscale_to_inf);
112 const v128_t vnanmaskw_lo = wasm_i32x4_gt(vabsx_lo, vexpw_max);
113 const v128_t vnanmaskw_hi = wasm_i32x4_gt(vabsx_hi, vexpw_max);
114
115 vbias_lo = wasm_v128_and(vbias_lo, vexpw_max);
116 vbias_hi = wasm_v128_and(vbias_hi, vexpw_max);
117 vf_lo = wasm_f32x4_mul(vf_lo, vscale_to_zero);
118 vf_hi = wasm_f32x4_mul(vf_hi, vscale_to_zero);
119 const v128_t vnanmaskh = wasm_i16x8_narrow_i32x4(vnanmaskw_lo, vnanmaskw_hi);
120 const v128_t vsignh = wasm_i16x8_narrow_i32x4(vsignx_lo, vsignx_hi);
121
122 vbias_lo = wasm_i16x8_max(vbias_lo, vbias_min);
123 vbias_hi = wasm_i16x8_max(vbias_hi, vbias_min);
124
125 vf_lo = wasm_f32x4_add(vf_lo, vbias_lo);
126 vf_hi = wasm_f32x4_add(vf_hi, vbias_hi);
127
128 v128_t vexpw_lo = wasm_i32x4_shr(vf_lo, 13);
129 v128_t vexpw_hi = wasm_i32x4_shr(vf_hi, 13);
130 const v128_t vmantw_lo = wasm_v128_and(vf_lo, vmanth_mask);
131 const v128_t vmantw_hi = wasm_v128_and(vf_hi, vmanth_mask);
132
133 vexpw_lo = wasm_v128_and(vexpw_lo, vexph_mask);
134 vexpw_hi = wasm_v128_and(vexpw_hi, vexph_mask);
135
136 const v128_t vnonsignw_lo = wasm_i32x4_add(vmantw_lo, vexpw_lo);
137 const v128_t vnonsignw_hi = wasm_i32x4_add(vmantw_hi, vexpw_hi);
138
139 const v128_t vnonsignh = wasm_i16x8_narrow_i32x4(vnonsignw_lo, vnonsignw_hi);
140
141 const v128_t vabsh = wasm_v128_bitselect(vnanh, vnonsignh, vnanmaskh);
142
143 v128_t vh = wasm_v128_or(vabsh, vsignh);
144
145 if (batch & (4 * sizeof(float))) {
146 wasm_v128_store64_lane(o, vh, 0);
147 vh = wasm_v64x2_shuffle(vh, vh, 1, 1);
148 o += 4;
149 }
150 if (batch & (2 * sizeof(float))) {
151 wasm_v128_store32_lane(o, vh, 0);
152 vh = wasm_i64x2_shr(vh, 32);
153 o += 2;
154 }
155 if (batch & (1 * sizeof(float))) {
156 wasm_v128_store16_lane(o, vh, 0);
157 }
158 }
159 }