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