xref: /aosp_15_r20/external/XNNPACK/src/f16-f32-vcvt/gen/vcvt-wasmsimd-int16-x32.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f16-f32-vcvt/wasmsimd-int16.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 
xnn_f16_f32_vcvt_ukernel__wasmsimd_int16_x32(size_t n,const void * input,float * output,const union xnn_f16_f32_cvt_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_f16_f32_vcvt_ukernel__wasmsimd_int16_x32(
19     size_t n,
20     const void* input,
21     float* output,
22     const union xnn_f16_f32_cvt_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
23 {
24   assert(n != 0);
25   assert(n % sizeof(uint16_t) == 0);
26   assert(input != NULL);
27   assert(output != NULL);
28 
29   const v128_t vsign_mask = wasm_v128_load64_splat(params->wasmsimd_int16.sign_mask);
30   const v128_t vexp_offset = wasm_v128_load64_splat(params->wasmsimd_int16.exp_offset);
31   const v128_t vexp_scale = wasm_v128_load64_splat(params->wasmsimd_int16.exp_scale);
32   const v128_t vmagic_mask = wasm_v128_load64_splat(params->wasmsimd_int16.magic_mask);
33   const v128_t vmagic_bias = wasm_v128_load64_splat(params->wasmsimd_int16.magic_bias);
34   const v128_t vdenorm_cutoff = wasm_v128_load64_splat(params->wasmsimd_int16.denorm_cutoff);
35 
36   const uint16_t* i = (const uint16_t*) input;
37   for (; n >= 32 * sizeof(uint16_t); n -= 32 * sizeof(uint16_t)) {
38     const v128_t vh0 = wasm_v128_load(i);
39     const v128_t vh1 = wasm_v128_load(i + 8);
40     const v128_t vh2 = wasm_v128_load(i + 16);
41     const v128_t vh3 = wasm_v128_load(i + 24);
42     i += 32;
43 
44     const v128_t vsign0 = wasm_v128_and(vh0, vsign_mask);
45     const v128_t vsign1 = wasm_v128_and(vh1, vsign_mask);
46     const v128_t vsign2 = wasm_v128_and(vh2, vsign_mask);
47     const v128_t vsign3 = wasm_v128_and(vh3, vsign_mask);
48 
49     const v128_t vnonsign0 = wasm_v128_xor(vh0, vsign0);
50     const v128_t vnonsign1 = wasm_v128_xor(vh1, vsign1);
51     const v128_t vnonsign2 = wasm_v128_xor(vh2, vsign2);
52     const v128_t vnonsign3 = wasm_v128_xor(vh3, vsign3);
53 
54     const v128_t vprenorm0 = wasm_i16x8_shl(vnonsign0, 13);
55     const v128_t vprenorm1 = wasm_i16x8_add(wasm_u16x8_shr(vnonsign0, 3), vexp_offset);
56     const v128_t vprenorm2 = wasm_i16x8_shl(vnonsign1, 13);
57     const v128_t vprenorm3 = wasm_i16x8_add(wasm_u16x8_shr(vnonsign1, 3), vexp_offset);
58     const v128_t vprenorm4 = wasm_i16x8_shl(vnonsign2, 13);
59     const v128_t vprenorm5 = wasm_i16x8_add(wasm_u16x8_shr(vnonsign2, 3), vexp_offset);
60     const v128_t vprenorm6 = wasm_i16x8_shl(vnonsign3, 13);
61     const v128_t vprenorm7 = wasm_i16x8_add(wasm_u16x8_shr(vnonsign3, 3), vexp_offset);
62 
63     const v128_t vnorm0 = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm0, vprenorm1, 0,  8, 1,  9, 2, 10, 3, 11), vexp_scale);
64     const v128_t vnorm1 = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm0, vprenorm1, 4, 12, 5, 13, 6, 14, 7, 15), vexp_scale);
65     const v128_t vnorm2 = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm2, vprenorm3, 0,  8, 1,  9, 2, 10, 3, 11), vexp_scale);
66     const v128_t vnorm3 = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm2, vprenorm3, 4, 12, 5, 13, 6, 14, 7, 15), vexp_scale);
67     const v128_t vnorm4 = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm4, vprenorm5, 0,  8, 1,  9, 2, 10, 3, 11), vexp_scale);
68     const v128_t vnorm5 = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm4, vprenorm5, 4, 12, 5, 13, 6, 14, 7, 15), vexp_scale);
69     const v128_t vnorm6 = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm6, vprenorm7, 0,  8, 1,  9, 2, 10, 3, 11), vexp_scale);
70     const v128_t vnorm7 = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm6, vprenorm7, 4, 12, 5, 13, 6, 14, 7, 15), vexp_scale);
71 
72     const v128_t vdenorm0 = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign0, vmagic_mask, 0,  8, 1,  9, 2, 10, 3, 11), vmagic_bias);
73     const v128_t vdenorm1 = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign0, vmagic_mask, 4, 12, 5, 13, 6, 14, 7, 15), vmagic_bias);
74     const v128_t vdenorm2 = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign1, vmagic_mask, 0,  8, 1,  9, 2, 10, 3, 11), vmagic_bias);
75     const v128_t vdenorm3 = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign1, vmagic_mask, 4, 12, 5, 13, 6, 14, 7, 15), vmagic_bias);
76     const v128_t vdenorm4 = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign2, vmagic_mask, 0,  8, 1,  9, 2, 10, 3, 11), vmagic_bias);
77     const v128_t vdenorm5 = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign2, vmagic_mask, 4, 12, 5, 13, 6, 14, 7, 15), vmagic_bias);
78     const v128_t vdenorm6 = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign3, vmagic_mask, 0,  8, 1,  9, 2, 10, 3, 11), vmagic_bias);
79     const v128_t vdenorm7 = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign3, vmagic_mask, 4, 12, 5, 13, 6, 14, 7, 15), vmagic_bias);
80 
81     const v128_t vmask0 = wasm_i16x8_gt(vnonsign0, vdenorm_cutoff);
82     const v128_t vmask1 = wasm_i16x8_gt(vnonsign1, vdenorm_cutoff);
83     const v128_t vmask2 = wasm_i16x8_gt(vnonsign2, vdenorm_cutoff);
84     const v128_t vmask3 = wasm_i16x8_gt(vnonsign3, vdenorm_cutoff);
85     const v128_t vzero = wasm_i16x8_const_splat(0);
86 
87     const v128_t vxmask0 = wasm_i32x4_extend_low_i16x8(vmask0);
88     const v128_t vxmask1 = wasm_i32x4_extend_high_i16x8(vmask0);
89     const v128_t vxmask2 = wasm_i32x4_extend_low_i16x8(vmask1);
90     const v128_t vxmask3 = wasm_i32x4_extend_high_i16x8(vmask1);
91     const v128_t vxmask4 = wasm_i32x4_extend_low_i16x8(vmask2);
92     const v128_t vxmask5 = wasm_i32x4_extend_high_i16x8(vmask2);
93     const v128_t vxmask6 = wasm_i32x4_extend_low_i16x8(vmask3);
94     const v128_t vxmask7 = wasm_i32x4_extend_high_i16x8(vmask3);
95 
96     const v128_t vf0 = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign0, 0,  8, 1,  9, 2, 10, 3, 11),
97       wasm_v128_bitselect(vnorm0, vdenorm0, vxmask0));
98     const v128_t vf1 = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign0, 4, 12, 5, 13, 6, 14, 7, 15),
99       wasm_v128_bitselect(vnorm1, vdenorm1, vxmask1));
100     const v128_t vf2 = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign1, 0,  8, 1,  9, 2, 10, 3, 11),
101       wasm_v128_bitselect(vnorm2, vdenorm2, vxmask2));
102     const v128_t vf3 = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign1, 4, 12, 5, 13, 6, 14, 7, 15),
103       wasm_v128_bitselect(vnorm3, vdenorm3, vxmask3));
104     const v128_t vf4 = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign2, 0,  8, 1,  9, 2, 10, 3, 11),
105       wasm_v128_bitselect(vnorm4, vdenorm4, vxmask4));
106     const v128_t vf5 = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign2, 4, 12, 5, 13, 6, 14, 7, 15),
107       wasm_v128_bitselect(vnorm5, vdenorm5, vxmask5));
108     const v128_t vf6 = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign3, 0,  8, 1,  9, 2, 10, 3, 11),
109       wasm_v128_bitselect(vnorm6, vdenorm6, vxmask6));
110     const v128_t vf7 = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign3, 4, 12, 5, 13, 6, 14, 7, 15),
111       wasm_v128_bitselect(vnorm7, vdenorm7, vxmask7));
112 
113     wasm_v128_store(output, vf0);
114     wasm_v128_store(output + 4, vf1);
115     wasm_v128_store(output + 8, vf2);
116     wasm_v128_store(output + 12, vf3);
117     wasm_v128_store(output + 16, vf4);
118     wasm_v128_store(output + 20, vf5);
119     wasm_v128_store(output + 24, vf6);
120     wasm_v128_store(output + 28, vf7);
121     output += 32;
122   }
123   for (; n >= 8 * sizeof(uint16_t); n -= 8 * sizeof(uint16_t)) {
124     const v128_t vh = wasm_v128_load(i);
125     i += 8;
126 
127     const v128_t vsign = wasm_v128_and(vh, vsign_mask);
128 
129     const v128_t vnonsign = wasm_v128_xor(vh, vsign);
130 
131     const v128_t vprenorm_lo = wasm_i16x8_shl(vnonsign, 13);
132     const v128_t vprenorm_hi = wasm_i16x8_add(wasm_u16x8_shr(vnonsign, 3), vexp_offset);
133 
134     const v128_t vnorm_lo = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm_lo, vprenorm_hi, 0,  8, 1,  9, 2, 10, 3, 11), vexp_scale);
135     const v128_t vnorm_hi = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm_lo, vprenorm_hi, 4, 12, 5, 13, 6, 14, 7, 15), vexp_scale);
136 
137     const v128_t vdenorm_lo = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign, vmagic_mask, 0,  8, 1,  9, 2, 10, 3, 11), vmagic_bias);
138     const v128_t vdenorm_hi = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign, vmagic_mask, 4, 12, 5, 13, 6, 14, 7, 15), vmagic_bias);
139 
140     const v128_t vmask = wasm_i16x8_gt(vnonsign, vdenorm_cutoff);
141     const v128_t vzero = wasm_i16x8_const_splat(0);
142 
143     const v128_t vxmask_lo = wasm_i32x4_extend_low_i16x8(vmask);
144     const v128_t vf_lo = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign, 0,  8, 1,  9, 2, 10, 3, 11),
145       wasm_v128_bitselect(vnorm_lo, vdenorm_lo, vxmask_lo));
146 
147     const v128_t vxmask_hi = wasm_i32x4_extend_high_i16x8(vmask);
148     const v128_t vf_hi = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign, 4, 12, 5, 13, 6, 14, 7, 15),
149       wasm_v128_bitselect(vnorm_hi, vdenorm_hi, vxmask_hi));
150 
151     wasm_v128_store(output, vf_lo);
152     wasm_v128_store(output + 4, vf_hi);
153     output += 8;
154   }
155   if XNN_UNLIKELY(n != 0) {
156     assert(n >= 1 * sizeof(uint16_t));
157     assert(n <= 7 * sizeof(uint16_t));
158     const v128_t vh = wasm_v128_load(i);
159 
160     const v128_t vsign = wasm_v128_and(vh, vsign_mask);
161 
162     const v128_t vnonsign = wasm_v128_xor(vh, vsign);
163 
164     const v128_t vprenorm_lo = wasm_i16x8_shl(vnonsign, 13);
165     const v128_t vprenorm_hi = wasm_i16x8_add(wasm_u16x8_shr(vnonsign, 3), vexp_offset);
166 
167     const v128_t vnorm_lo = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm_lo, vprenorm_hi, 0,  8, 1,  9, 2, 10, 3, 11), vexp_scale);
168     const v128_t vnorm_hi = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm_lo, vprenorm_hi, 4, 12, 5, 13, 6, 14, 7, 15), vexp_scale);
169 
170     const v128_t vdenorm_lo = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign, vmagic_mask, 0,  8, 1,  9, 2, 10, 3, 11), vmagic_bias);
171     const v128_t vdenorm_hi = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign, vmagic_mask, 4, 12, 5, 13, 6, 14, 7, 15), vmagic_bias);
172 
173     const v128_t vmask = wasm_i16x8_gt(vnonsign, vdenorm_cutoff);
174     const v128_t vzero = wasm_i16x8_const_splat(0);
175 
176     const v128_t vxmask_lo = wasm_i32x4_extend_low_i16x8(vmask);
177     v128_t vf = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign, 0, 8, 1, 9, 2, 10, 3, 11),
178       wasm_v128_bitselect(vnorm_lo, vdenorm_lo, vxmask_lo));
179 
180     if (n & (4 * sizeof(uint16_t))) {
181       wasm_v128_store(output, vf);
182       output += 4;
183 
184       const v128_t vxmask_hi = wasm_i32x4_extend_high_i16x8(vmask);
185       vf = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign, 4, 12, 5, 13, 6, 14, 7, 15),
186         wasm_v128_bitselect(vnorm_hi, vdenorm_hi, vxmask_hi));
187     }
188     if (n & (2 * sizeof(uint16_t))) {
189       *((double*) output) = wasm_f64x2_extract_lane(vf, 0);
190       output += 2;
191 
192       vf = wasm_v64x2_shuffle(vf, vf, 1, 1);
193     }
194     if (n & (1 * sizeof(uint16_t))) {
195       *((float*) output) = wasm_f32x4_extract_lane(vf, 0);
196     }
197   }
198 }
199