xref: /aosp_15_r20/external/XNNPACK/src/f16-f32-vcvt/gen/vcvt-wasmsimd-int16-x24.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_x24(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_x24(
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 >= 24 * sizeof(uint16_t); n -= 24 * 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     i += 24;
42 
43     const v128_t vsign0 = wasm_v128_and(vh0, vsign_mask);
44     const v128_t vsign1 = wasm_v128_and(vh1, vsign_mask);
45     const v128_t vsign2 = wasm_v128_and(vh2, vsign_mask);
46 
47     const v128_t vnonsign0 = wasm_v128_xor(vh0, vsign0);
48     const v128_t vnonsign1 = wasm_v128_xor(vh1, vsign1);
49     const v128_t vnonsign2 = wasm_v128_xor(vh2, vsign2);
50 
51     const v128_t vprenorm0 = wasm_i16x8_shl(vnonsign0, 13);
52     const v128_t vprenorm1 = wasm_i16x8_add(wasm_u16x8_shr(vnonsign0, 3), vexp_offset);
53     const v128_t vprenorm2 = wasm_i16x8_shl(vnonsign1, 13);
54     const v128_t vprenorm3 = wasm_i16x8_add(wasm_u16x8_shr(vnonsign1, 3), vexp_offset);
55     const v128_t vprenorm4 = wasm_i16x8_shl(vnonsign2, 13);
56     const v128_t vprenorm5 = wasm_i16x8_add(wasm_u16x8_shr(vnonsign2, 3), vexp_offset);
57 
58     const v128_t vnorm0 = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm0, vprenorm1, 0,  8, 1,  9, 2, 10, 3, 11), vexp_scale);
59     const v128_t vnorm1 = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm0, vprenorm1, 4, 12, 5, 13, 6, 14, 7, 15), vexp_scale);
60     const v128_t vnorm2 = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm2, vprenorm3, 0,  8, 1,  9, 2, 10, 3, 11), vexp_scale);
61     const v128_t vnorm3 = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm2, vprenorm3, 4, 12, 5, 13, 6, 14, 7, 15), vexp_scale);
62     const v128_t vnorm4 = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm4, vprenorm5, 0,  8, 1,  9, 2, 10, 3, 11), vexp_scale);
63     const v128_t vnorm5 = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm4, vprenorm5, 4, 12, 5, 13, 6, 14, 7, 15), vexp_scale);
64 
65     const v128_t vdenorm0 = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign0, vmagic_mask, 0,  8, 1,  9, 2, 10, 3, 11), vmagic_bias);
66     const v128_t vdenorm1 = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign0, vmagic_mask, 4, 12, 5, 13, 6, 14, 7, 15), vmagic_bias);
67     const v128_t vdenorm2 = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign1, vmagic_mask, 0,  8, 1,  9, 2, 10, 3, 11), vmagic_bias);
68     const v128_t vdenorm3 = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign1, vmagic_mask, 4, 12, 5, 13, 6, 14, 7, 15), vmagic_bias);
69     const v128_t vdenorm4 = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign2, vmagic_mask, 0,  8, 1,  9, 2, 10, 3, 11), vmagic_bias);
70     const v128_t vdenorm5 = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign2, vmagic_mask, 4, 12, 5, 13, 6, 14, 7, 15), vmagic_bias);
71 
72     const v128_t vmask0 = wasm_i16x8_gt(vnonsign0, vdenorm_cutoff);
73     const v128_t vmask1 = wasm_i16x8_gt(vnonsign1, vdenorm_cutoff);
74     const v128_t vmask2 = wasm_i16x8_gt(vnonsign2, vdenorm_cutoff);
75     const v128_t vzero = wasm_i16x8_const_splat(0);
76 
77     const v128_t vxmask0 = wasm_i32x4_extend_low_i16x8(vmask0);
78     const v128_t vxmask1 = wasm_i32x4_extend_high_i16x8(vmask0);
79     const v128_t vxmask2 = wasm_i32x4_extend_low_i16x8(vmask1);
80     const v128_t vxmask3 = wasm_i32x4_extend_high_i16x8(vmask1);
81     const v128_t vxmask4 = wasm_i32x4_extend_low_i16x8(vmask2);
82     const v128_t vxmask5 = wasm_i32x4_extend_high_i16x8(vmask2);
83 
84     const v128_t vf0 = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign0, 0,  8, 1,  9, 2, 10, 3, 11),
85       wasm_v128_bitselect(vnorm0, vdenorm0, vxmask0));
86     const v128_t vf1 = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign0, 4, 12, 5, 13, 6, 14, 7, 15),
87       wasm_v128_bitselect(vnorm1, vdenorm1, vxmask1));
88     const v128_t vf2 = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign1, 0,  8, 1,  9, 2, 10, 3, 11),
89       wasm_v128_bitselect(vnorm2, vdenorm2, vxmask2));
90     const v128_t vf3 = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign1, 4, 12, 5, 13, 6, 14, 7, 15),
91       wasm_v128_bitselect(vnorm3, vdenorm3, vxmask3));
92     const v128_t vf4 = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign2, 0,  8, 1,  9, 2, 10, 3, 11),
93       wasm_v128_bitselect(vnorm4, vdenorm4, vxmask4));
94     const v128_t vf5 = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign2, 4, 12, 5, 13, 6, 14, 7, 15),
95       wasm_v128_bitselect(vnorm5, vdenorm5, vxmask5));
96 
97     wasm_v128_store(output, vf0);
98     wasm_v128_store(output + 4, vf1);
99     wasm_v128_store(output + 8, vf2);
100     wasm_v128_store(output + 12, vf3);
101     wasm_v128_store(output + 16, vf4);
102     wasm_v128_store(output + 20, vf5);
103     output += 24;
104   }
105   for (; n >= 8 * sizeof(uint16_t); n -= 8 * sizeof(uint16_t)) {
106     const v128_t vh = wasm_v128_load(i);
107     i += 8;
108 
109     const v128_t vsign = wasm_v128_and(vh, vsign_mask);
110 
111     const v128_t vnonsign = wasm_v128_xor(vh, vsign);
112 
113     const v128_t vprenorm_lo = wasm_i16x8_shl(vnonsign, 13);
114     const v128_t vprenorm_hi = wasm_i16x8_add(wasm_u16x8_shr(vnonsign, 3), vexp_offset);
115 
116     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);
117     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);
118 
119     const v128_t vdenorm_lo = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign, vmagic_mask, 0,  8, 1,  9, 2, 10, 3, 11), vmagic_bias);
120     const v128_t vdenorm_hi = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign, vmagic_mask, 4, 12, 5, 13, 6, 14, 7, 15), vmagic_bias);
121 
122     const v128_t vmask = wasm_i16x8_gt(vnonsign, vdenorm_cutoff);
123     const v128_t vzero = wasm_i16x8_const_splat(0);
124 
125     const v128_t vxmask_lo = wasm_i32x4_extend_low_i16x8(vmask);
126     const v128_t vf_lo = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign, 0,  8, 1,  9, 2, 10, 3, 11),
127       wasm_v128_bitselect(vnorm_lo, vdenorm_lo, vxmask_lo));
128 
129     const v128_t vxmask_hi = wasm_i32x4_extend_high_i16x8(vmask);
130     const v128_t vf_hi = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign, 4, 12, 5, 13, 6, 14, 7, 15),
131       wasm_v128_bitselect(vnorm_hi, vdenorm_hi, vxmask_hi));
132 
133     wasm_v128_store(output, vf_lo);
134     wasm_v128_store(output + 4, vf_hi);
135     output += 8;
136   }
137   if XNN_UNLIKELY(n != 0) {
138     assert(n >= 1 * sizeof(uint16_t));
139     assert(n <= 7 * sizeof(uint16_t));
140     const v128_t vh = wasm_v128_load(i);
141 
142     const v128_t vsign = wasm_v128_and(vh, vsign_mask);
143 
144     const v128_t vnonsign = wasm_v128_xor(vh, vsign);
145 
146     const v128_t vprenorm_lo = wasm_i16x8_shl(vnonsign, 13);
147     const v128_t vprenorm_hi = wasm_i16x8_add(wasm_u16x8_shr(vnonsign, 3), vexp_offset);
148 
149     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);
150     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);
151 
152     const v128_t vdenorm_lo = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign, vmagic_mask, 0,  8, 1,  9, 2, 10, 3, 11), vmagic_bias);
153     const v128_t vdenorm_hi = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign, vmagic_mask, 4, 12, 5, 13, 6, 14, 7, 15), vmagic_bias);
154 
155     const v128_t vmask = wasm_i16x8_gt(vnonsign, vdenorm_cutoff);
156     const v128_t vzero = wasm_i16x8_const_splat(0);
157 
158     const v128_t vxmask_lo = wasm_i32x4_extend_low_i16x8(vmask);
159     v128_t vf = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign, 0, 8, 1, 9, 2, 10, 3, 11),
160       wasm_v128_bitselect(vnorm_lo, vdenorm_lo, vxmask_lo));
161 
162     if (n & (4 * sizeof(uint16_t))) {
163       wasm_v128_store(output, vf);
164       output += 4;
165 
166       const v128_t vxmask_hi = wasm_i32x4_extend_high_i16x8(vmask);
167       vf = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign, 4, 12, 5, 13, 6, 14, 7, 15),
168         wasm_v128_bitselect(vnorm_hi, vdenorm_hi, vxmask_hi));
169     }
170     if (n & (2 * sizeof(uint16_t))) {
171       *((double*) output) = wasm_f64x2_extract_lane(vf, 0);
172       output += 2;
173 
174       vf = wasm_v64x2_shuffle(vf, vf, 1, 1);
175     }
176     if (n & (1 * sizeof(uint16_t))) {
177       *((float*) output) = wasm_f32x4_extract_lane(vf, 0);
178     }
179   }
180 }
181