Debian Codegraph @

src/f16-f32-vcvt/gen/f16-f32-vcvt-wasmsimd-int32-u16.c

raw
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
18 void xnn_f16_f32_vcvt_ukernel__wasmsimd_int32_u16(
19 size_t batch,
20 const xnn_float16* input,
21 float* output,
22 const void* params) XNN_OOB_READS
23 {
24 assert(batch != 0);
25 assert(batch % sizeof(uint16_t) == 0);
26 assert(input != NULL);
27 assert(output != NULL);
28
29 const v128_t vsign_mask = wasm_i32x4_const_splat(UINT32_C(0x80000000));
30 const v128_t vexp_offset = wasm_i32x4_const_splat(UINT32_C(0x70000000));
31 const v128_t vexp_scale = wasm_f32x4_const_splat(0x1.0p-112f);
32 const v128_t vmagic_bias = wasm_f32x4_const_splat(0.5f);
33 const v128_t vdenorm_cutoff = wasm_i32x4_const_splat(UINT32_C(0x08000000));
34
35 XNN_FORCE_REALIZATION(vsign_mask);
36 XNN_FORCE_REALIZATION(vexp_offset);
37 XNN_FORCE_REALIZATION(vexp_scale);
38 XNN_FORCE_REALIZATION(vmagic_bias);
39 XNN_FORCE_REALIZATION(vdenorm_cutoff);
40
41 const uint16_t* i = (const uint16_t*) input;
42 for (; batch >= 16 * sizeof(uint16_t); batch -= 16 * sizeof(uint16_t)) {
43 const v128_t vh0 = wasm_v128_load(i);
44 const v128_t vh1 = wasm_v128_load(i + 8);
45 i += 16;
46
47 const v128_t vzero = wasm_i16x8_const_splat(0);
48 const v128_t vw0 = wasm_v16x8_shuffle(vzero, vh0, 0, 8, 1, 9, 2, 10, 3, 11);
49 const v128_t vw1 = wasm_v16x8_shuffle(vzero, vh0, 4, 12, 5, 13, 6, 14, 7, 15);
50 const v128_t vw2 = wasm_v16x8_shuffle(vzero, vh1, 0, 8, 1, 9, 2, 10, 3, 11);
51 const v128_t vw3 = wasm_v16x8_shuffle(vzero, vh1, 4, 12, 5, 13, 6, 14, 7, 15);
52
53 const v128_t vsign0 = wasm_v128_and(vw0, vsign_mask);
54 const v128_t vsign1 = wasm_v128_and(vw1, vsign_mask);
55 const v128_t vsign2 = wasm_v128_and(vw2, vsign_mask);
56 const v128_t vsign3 = wasm_v128_and(vw3, vsign_mask);
57
58 const v128_t vnonsign0 = wasm_v128_xor(vw0, vsign0);
59 const v128_t vnonsign1 = wasm_v128_xor(vw1, vsign1);
60 const v128_t vnonsign2 = wasm_v128_xor(vw2, vsign2);
61 const v128_t vnonsign3 = wasm_v128_xor(vw3, vsign3);
62
63 const v128_t vnorm0 = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign0, 3), vexp_offset), vexp_scale);
64 const v128_t vnorm1 = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign1, 3), vexp_offset), vexp_scale);
65 const v128_t vnorm2 = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign2, 3), vexp_offset), vexp_scale);
66 const v128_t vnorm3 = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign3, 3), vexp_offset), vexp_scale);
67
68 const v128_t vdenorm0 = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign0, 16), vmagic_bias), vmagic_bias);
69 const v128_t vdenorm1 = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign1, 16), vmagic_bias), vmagic_bias);
70 const v128_t vdenorm2 = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign2, 16), vmagic_bias), vmagic_bias);
71 const v128_t vdenorm3 = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign3, 16), vmagic_bias), vmagic_bias);
72
73 const v128_t vxmask0 = wasm_i32x4_gt(vnonsign0, vdenorm_cutoff);
74 const v128_t vxmask1 = wasm_i32x4_gt(vnonsign1, vdenorm_cutoff);
75 const v128_t vxmask2 = wasm_i32x4_gt(vnonsign2, vdenorm_cutoff);
76 const v128_t vxmask3 = wasm_i32x4_gt(vnonsign3, vdenorm_cutoff);
77
78 const v128_t vf0 = wasm_v128_or(vsign0, wasm_v128_bitselect(vnorm0, vdenorm0, vxmask0));
79 const v128_t vf1 = wasm_v128_or(vsign1, wasm_v128_bitselect(vnorm1, vdenorm1, vxmask1));
80 const v128_t vf2 = wasm_v128_or(vsign2, wasm_v128_bitselect(vnorm2, vdenorm2, vxmask2));
81 const v128_t vf3 = wasm_v128_or(vsign3, wasm_v128_bitselect(vnorm3, vdenorm3, vxmask3));
82
83 wasm_v128_store(output, vf0);
84 wasm_v128_store(output + 4, vf1);
85 wasm_v128_store(output + 8, vf2);
86 wasm_v128_store(output + 12, vf3);
87 output += 16;
88 }
89 for (; batch >= 8 * sizeof(uint16_t); batch -= 8 * sizeof(uint16_t)) {
90 const v128_t vh = wasm_v128_load(i);
91 i += 8;
92
93 const v128_t vzero = wasm_i16x8_const_splat(0);
94 const v128_t vw_lo = wasm_v16x8_shuffle(vzero, vh, 0, 8, 1, 9, 2, 10, 3, 11);
95 const v128_t vw_hi = wasm_v16x8_shuffle(vzero, vh, 4, 12, 5, 13, 6, 14, 7, 15);
96
97 const v128_t vsign_lo = wasm_v128_and(vw_lo, vsign_mask);
98 const v128_t vsign_hi = wasm_v128_and(vw_hi, vsign_mask);
99
100 const v128_t vnonsign_lo = wasm_v128_xor(vw_lo, vsign_lo);
101 const v128_t vnonsign_hi = wasm_v128_xor(vw_hi, vsign_hi);
102
103 const v128_t vnorm_lo = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign_lo, 3), vexp_offset), vexp_scale);
104 const v128_t vnorm_hi = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign_hi, 3), vexp_offset), vexp_scale);
105
106 const v128_t vdenorm_lo = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign_lo, 16), vmagic_bias), vmagic_bias);
107 const v128_t vdenorm_hi = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign_hi, 16), vmagic_bias), vmagic_bias);
108
109 const v128_t vxmask_lo = wasm_i32x4_gt(vnonsign_lo, vdenorm_cutoff);
110 const v128_t vxmask_hi = wasm_i32x4_gt(vnonsign_hi, vdenorm_cutoff);
111
112 const v128_t vf_lo = wasm_v128_or(vsign_lo, wasm_v128_bitselect(vnorm_lo, vdenorm_lo, vxmask_lo));
113 const v128_t vf_hi = wasm_v128_or(vsign_hi, 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(batch != 0) {
120 assert(batch >= 1 * sizeof(uint16_t));
121 assert(batch <= 7 * sizeof(uint16_t));
122 const v128_t vh = wasm_v128_load(i);
123
124 const v128_t vzero = wasm_i16x8_const_splat(0);
125 const v128_t vw_lo = wasm_v16x8_shuffle(vzero, vh, 0, 8, 1, 9, 2, 10, 3, 11);
126 const v128_t vw_hi = wasm_v16x8_shuffle(vzero, vh, 4, 12, 5, 13, 6, 14, 7, 15);
127
128 const v128_t vsign_lo = wasm_v128_and(vw_lo, vsign_mask);
129 const v128_t vsign_hi = wasm_v128_and(vw_hi, vsign_mask);
130
131 const v128_t vnonsign_lo = wasm_v128_xor(vw_lo, vsign_lo);
132 const v128_t vnonsign_hi = wasm_v128_xor(vw_hi, vsign_hi);
133
134 const v128_t vnorm_lo = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign_lo, 3), vexp_offset), vexp_scale);
135 const v128_t vnorm_hi = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign_hi, 3), vexp_offset), vexp_scale);
136
137 const v128_t vdenorm_lo = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign_lo, 16), vmagic_bias), vmagic_bias);
138 const v128_t vdenorm_hi = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign_hi, 16), vmagic_bias), vmagic_bias);
139
140 const v128_t vxmask_lo = wasm_i32x4_gt(vnonsign_lo, vdenorm_cutoff);
141 v128_t vf = wasm_v128_or(vsign_lo, wasm_v128_bitselect(vnorm_lo, vdenorm_lo, vxmask_lo));
142
143 if (batch & (4 * sizeof(uint16_t))) {
144 wasm_v128_store(output, vf);
145 output += 4;
146
147 const v128_t vxmask_hi = wasm_i32x4_gt(vnonsign_hi, vdenorm_cutoff);
148 vf = wasm_v128_or(vsign_hi, wasm_v128_bitselect(vnorm_hi, vdenorm_hi, vxmask_hi));
149 }
150 if (batch & (2 * sizeof(uint16_t))) {
151 wasm_v128_store64_lane(output, vf, 0);
152 vf = wasm_v64x2_shuffle(vf, vf, 1, 1);
153 output += 2;
154 }
155 if (batch & (1 * sizeof(uint16_t))) {
156 wasm_v128_store32_lane(output, vf, 0);
157 }
158 }
159 }