xref: /aosp_15_r20/external/XNNPACK/src/f16-f32-vcvt/gen/vcvt-wasmsimd-int32-x16.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f16-f32-vcvt/wasmsimd-int32.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_int32_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_int32_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_int32.sign_mask);
30   const v128_t vexp_offset = wasm_v128_load64_splat(params->wasmsimd_int32.exp_offset);
31   const v128_t vexp_scale = wasm_v128_load64_splat(params->wasmsimd_int32.exp_scale);
32   const v128_t vmagic_bias = wasm_v128_load64_splat(params->wasmsimd_int32.magic_bias);
33   const v128_t vdenorm_cutoff = wasm_v128_load64_splat(params->wasmsimd_int32.denorm_cutoff);
34 
35   const uint16_t* i = (const uint16_t*) input;
36   for (; n >= 16 * sizeof(uint16_t); n -= 16 * sizeof(uint16_t)) {
37     const v128_t vh0 = wasm_v128_load(i);
38     const v128_t vh1 = wasm_v128_load(i + 8);
39     i += 16;
40 
41     const v128_t vzero = wasm_i16x8_const_splat(0);
42     const v128_t vw0 = wasm_v16x8_shuffle(vzero, vh0, 0,  8, 1,  9, 2, 10, 3, 11);
43     const v128_t vw1 = wasm_v16x8_shuffle(vzero, vh0, 4, 12, 5, 13, 6, 14, 7, 15);
44     const v128_t vw2 = wasm_v16x8_shuffle(vzero, vh1, 0,  8, 1,  9, 2, 10, 3, 11);
45     const v128_t vw3 = wasm_v16x8_shuffle(vzero, vh1, 4, 12, 5, 13, 6, 14, 7, 15);
46 
47     const v128_t vsign0 = wasm_v128_and(vw0, vsign_mask);
48     const v128_t vsign1 = wasm_v128_and(vw1, vsign_mask);
49     const v128_t vsign2 = wasm_v128_and(vw2, vsign_mask);
50     const v128_t vsign3 = wasm_v128_and(vw3, vsign_mask);
51 
52     const v128_t vnonsign0 = wasm_v128_xor(vw0, vsign0);
53     const v128_t vnonsign1 = wasm_v128_xor(vw1, vsign1);
54     const v128_t vnonsign2 = wasm_v128_xor(vw2, vsign2);
55     const v128_t vnonsign3 = wasm_v128_xor(vw3, vsign3);
56 
57     const v128_t vnorm0 = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign0, 3), vexp_offset), vexp_scale);
58     const v128_t vnorm1 = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign1, 3), vexp_offset), vexp_scale);
59     const v128_t vnorm2 = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign2, 3), vexp_offset), vexp_scale);
60     const v128_t vnorm3 = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign3, 3), vexp_offset), vexp_scale);
61 
62     const v128_t vdenorm0 = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign0, 16), vmagic_bias), vmagic_bias);
63     const v128_t vdenorm1 = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign1, 16), vmagic_bias), vmagic_bias);
64     const v128_t vdenorm2 = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign2, 16), vmagic_bias), vmagic_bias);
65     const v128_t vdenorm3 = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign3, 16), vmagic_bias), vmagic_bias);
66 
67     const v128_t vxmask0 = wasm_i32x4_gt(vnonsign0, vdenorm_cutoff);
68     const v128_t vxmask1 = wasm_i32x4_gt(vnonsign1, vdenorm_cutoff);
69     const v128_t vxmask2 = wasm_i32x4_gt(vnonsign2, vdenorm_cutoff);
70     const v128_t vxmask3 = wasm_i32x4_gt(vnonsign3, vdenorm_cutoff);
71 
72     const v128_t vf0 = wasm_v128_or(vsign0, wasm_v128_bitselect(vnorm0, vdenorm0, vxmask0));
73     const v128_t vf1 = wasm_v128_or(vsign1, wasm_v128_bitselect(vnorm1, vdenorm1, vxmask1));
74     const v128_t vf2 = wasm_v128_or(vsign2, wasm_v128_bitselect(vnorm2, vdenorm2, vxmask2));
75     const v128_t vf3 = wasm_v128_or(vsign3, wasm_v128_bitselect(vnorm3, vdenorm3, vxmask3));
76 
77     wasm_v128_store(output, vf0);
78     wasm_v128_store(output + 4, vf1);
79     wasm_v128_store(output + 8, vf2);
80     wasm_v128_store(output + 12, vf3);
81     output += 16;
82   }
83   for (; n >= 8 * sizeof(uint16_t); n -= 8 * sizeof(uint16_t)) {
84     const v128_t vh = wasm_v128_load(i);
85     i += 8;
86 
87     const v128_t vzero = wasm_i16x8_const_splat(0);
88     const v128_t vw_lo = wasm_v16x8_shuffle(vzero, vh, 0,  8, 1,  9, 2, 10, 3, 11);
89     const v128_t vw_hi = wasm_v16x8_shuffle(vzero, vh, 4, 12, 5, 13, 6, 14, 7, 15);
90 
91     const v128_t vsign_lo = wasm_v128_and(vw_lo, vsign_mask);
92     const v128_t vsign_hi = wasm_v128_and(vw_hi, vsign_mask);
93 
94     const v128_t vnonsign_lo = wasm_v128_xor(vw_lo, vsign_lo);
95     const v128_t vnonsign_hi = wasm_v128_xor(vw_hi, vsign_hi);
96 
97     const v128_t vnorm_lo = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign_lo, 3), vexp_offset), vexp_scale);
98     const v128_t vnorm_hi = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign_hi, 3), vexp_offset), vexp_scale);
99 
100     const v128_t vdenorm_lo = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign_lo, 16), vmagic_bias), vmagic_bias);
101     const v128_t vdenorm_hi = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign_hi, 16), vmagic_bias), vmagic_bias);
102 
103     const v128_t vxmask_lo = wasm_i32x4_gt(vnonsign_lo, vdenorm_cutoff);
104     const v128_t vxmask_hi = wasm_i32x4_gt(vnonsign_hi, vdenorm_cutoff);
105 
106     const v128_t vf_lo = wasm_v128_or(vsign_lo, wasm_v128_bitselect(vnorm_lo, vdenorm_lo, vxmask_lo));
107     const v128_t vf_hi = wasm_v128_or(vsign_hi, wasm_v128_bitselect(vnorm_hi, vdenorm_hi, vxmask_hi));
108 
109     wasm_v128_store(output, vf_lo);
110     wasm_v128_store(output + 4, vf_hi);
111     output += 8;
112   }
113   if XNN_UNLIKELY(n != 0) {
114     assert(n >= 1 * sizeof(uint16_t));
115     assert(n <= 7 * sizeof(uint16_t));
116     const v128_t vh = wasm_v128_load(i);
117 
118     const v128_t vzero = wasm_i16x8_const_splat(0);
119     const v128_t vw_lo = wasm_v16x8_shuffle(vzero, vh, 0,  8, 1,  9, 2, 10, 3, 11);
120     const v128_t vw_hi = wasm_v16x8_shuffle(vzero, vh, 4, 12, 5, 13, 6, 14, 7, 15);
121 
122     const v128_t vsign_lo = wasm_v128_and(vw_lo, vsign_mask);
123     const v128_t vsign_hi = wasm_v128_and(vw_hi, vsign_mask);
124 
125     const v128_t vnonsign_lo = wasm_v128_xor(vw_lo, vsign_lo);
126     const v128_t vnonsign_hi = wasm_v128_xor(vw_hi, vsign_hi);
127 
128     const v128_t vnorm_lo = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign_lo, 3), vexp_offset), vexp_scale);
129     const v128_t vnorm_hi = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign_hi, 3), vexp_offset), vexp_scale);
130 
131     const v128_t vdenorm_lo = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign_lo, 16), vmagic_bias), vmagic_bias);
132     const v128_t vdenorm_hi = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign_hi, 16), vmagic_bias), vmagic_bias);
133 
134     const v128_t vxmask_lo = wasm_i32x4_gt(vnonsign_lo, vdenorm_cutoff);
135     v128_t vf = wasm_v128_or(vsign_lo, wasm_v128_bitselect(vnorm_lo, vdenorm_lo, vxmask_lo));
136 
137     if (n & (4 * sizeof(uint16_t))) {
138       wasm_v128_store(output, vf);
139       output += 4;
140 
141       const v128_t vxmask_hi = wasm_i32x4_gt(vnonsign_hi, vdenorm_cutoff);
142       vf = wasm_v128_or(vsign_hi, wasm_v128_bitselect(vnorm_hi, vdenorm_hi, vxmask_hi));
143     }
144     if (n & (2 * sizeof(uint16_t))) {
145       *((double*) output) = wasm_f64x2_extract_lane(vf, 0);
146       output += 2;
147 
148       vf = wasm_v64x2_shuffle(vf, vf, 1, 1);
149     }
150     if (n & (1 * sizeof(uint16_t))) {
151       *((float*) output) = wasm_f32x4_extract_lane(vf, 0);
152     }
153   }
154 }
155