Debian Codegraph @

src/f32-f16-vcvt/gen/f32-f16-vcvt-sse41-u32.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/f32-f16-vcvt/sse.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 <smmintrin.h>
13
14 #include "xnnpack/common.h"
15 #include "xnnpack/unaligned.h"
16 #include "xnnpack/vcvt.h"
17
18
19 void xnn_f32_f16_vcvt_ukernel__sse41_u32(
20 size_t batch,
21 const float* input,
22 xnn_float16* output,
23 const void* params) XNN_OOB_READS
24 {
25 assert(batch != 0);
26 assert(batch % sizeof(float) == 0);
27 assert(input != NULL);
28 assert(output != NULL);
29
30 const __m128 vnonsign_mask = _mm_castsi128_ps(_mm_set1_epi32(UINT32_C(0x7FFFFFFF)));
31 const __m128i vexp_bias = _mm_set1_epi32(UINT32_C(0x07800000));
32 const __m128 vscale_to_inf = _mm_set1_ps(0x1.0p+112f);
33 const __m128i vexpw_max = _mm_set1_epi32(UINT32_C(0x7F800000));
34 const __m128 vscale_to_zero = _mm_set1_ps(0x1.0p-110f);
35 const __m128i vbias_min = _mm_set1_epi32(UINT32_C(0x40008000)); // 0x8000, 0x4000, 0x8000, 0x4000, ...
36 const __m128i vmanth_mask = _mm_set1_epi32(UINT32_C(0x00000FFF));
37 const __m128i vexph_mask = _mm_set1_epi32(UINT32_C(0x00007C00));
38 const __m128i vnanh = _mm_set1_epi16(UINT16_C(0x7E00));
39
40 XNN_FORCE_REALIZATION(vnonsign_mask);
41 XNN_FORCE_REALIZATION(vexp_bias);
42 XNN_FORCE_REALIZATION(vscale_to_inf);
43 XNN_FORCE_REALIZATION(vexpw_max);
44 XNN_FORCE_REALIZATION(vscale_to_zero);
45 XNN_FORCE_REALIZATION(vbias_min);
46 XNN_FORCE_REALIZATION(vmanth_mask);
47 XNN_FORCE_REALIZATION(vexph_mask);
48 XNN_FORCE_REALIZATION(vnanh);
49
50 uint16_t* o = (uint16_t*) output;
51 for (; batch >= 32 * sizeof(float); batch -= 32 * sizeof(float)) {
52 const __m128 vx0 = _mm_loadu_ps(input);
53 const __m128 vx1 = _mm_loadu_ps(input + 4);
54 const __m128 vx2 = _mm_loadu_ps(input + 8);
55 const __m128 vx3 = _mm_loadu_ps(input + 12);
56 const __m128 vx4 = _mm_loadu_ps(input + 16);
57 const __m128 vx5 = _mm_loadu_ps(input + 20);
58 const __m128 vx6 = _mm_loadu_ps(input + 24);
59 const __m128 vx7 = _mm_loadu_ps(input + 28);
60 input += 32;
61
62 const __m128 vabsx0 = _mm_and_ps(vx0, vnonsign_mask);
63 const __m128 vabsx1 = _mm_and_ps(vx1, vnonsign_mask);
64 const __m128 vabsx2 = _mm_and_ps(vx2, vnonsign_mask);
65 const __m128 vabsx3 = _mm_and_ps(vx3, vnonsign_mask);
66 const __m128 vabsx4 = _mm_and_ps(vx4, vnonsign_mask);
67 const __m128 vabsx5 = _mm_and_ps(vx5, vnonsign_mask);
68 const __m128 vabsx6 = _mm_and_ps(vx6, vnonsign_mask);
69 const __m128 vabsx7 = _mm_and_ps(vx7, vnonsign_mask);
70
71 const __m128 vsignx0 = _mm_xor_ps(vx0, vabsx0);
72 const __m128 vsignx1 = _mm_xor_ps(vx1, vabsx1);
73 const __m128 vsignx2 = _mm_xor_ps(vx2, vabsx2);
74 const __m128 vsignx3 = _mm_xor_ps(vx3, vabsx3);
75 const __m128 vsignx4 = _mm_xor_ps(vx4, vabsx4);
76 const __m128 vsignx5 = _mm_xor_ps(vx5, vabsx5);
77 const __m128 vsignx6 = _mm_xor_ps(vx6, vabsx6);
78 const __m128 vsignx7 = _mm_xor_ps(vx7, vabsx7);
79
80 __m128i vbias0 = _mm_add_epi32(_mm_castps_si128(vabsx0), vexp_bias);
81 __m128i vbias1 = _mm_add_epi32(_mm_castps_si128(vabsx1), vexp_bias);
82 __m128i vbias2 = _mm_add_epi32(_mm_castps_si128(vabsx2), vexp_bias);
83 __m128i vbias3 = _mm_add_epi32(_mm_castps_si128(vabsx3), vexp_bias);
84 __m128i vbias4 = _mm_add_epi32(_mm_castps_si128(vabsx4), vexp_bias);
85 __m128i vbias5 = _mm_add_epi32(_mm_castps_si128(vabsx5), vexp_bias);
86 __m128i vbias6 = _mm_add_epi32(_mm_castps_si128(vabsx6), vexp_bias);
87 __m128i vbias7 = _mm_add_epi32(_mm_castps_si128(vabsx7), vexp_bias);
88
89 __m128 vf0 = _mm_mul_ps(vabsx0, vscale_to_inf);
90 __m128 vf1 = _mm_mul_ps(vabsx1, vscale_to_inf);
91 __m128 vf2 = _mm_mul_ps(vabsx2, vscale_to_inf);
92 __m128 vf3 = _mm_mul_ps(vabsx3, vscale_to_inf);
93 __m128 vf4 = _mm_mul_ps(vabsx4, vscale_to_inf);
94 __m128 vf5 = _mm_mul_ps(vabsx5, vscale_to_inf);
95 __m128 vf6 = _mm_mul_ps(vabsx6, vscale_to_inf);
96 __m128 vf7 = _mm_mul_ps(vabsx7, vscale_to_inf);
97
98 const __m128i vnanmaskw0 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx0), vexpw_max);
99 const __m128i vnanmaskw1 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx1), vexpw_max);
100 const __m128i vnanmaskw2 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx2), vexpw_max);
101 const __m128i vnanmaskw3 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx3), vexpw_max);
102 const __m128i vnanmaskw4 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx4), vexpw_max);
103 const __m128i vnanmaskw5 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx5), vexpw_max);
104 const __m128i vnanmaskw6 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx6), vexpw_max);
105 const __m128i vnanmaskw7 = _mm_cmpgt_epi32(_mm_castps_si128(vabsx7), vexpw_max);
106
107 vbias0 = _mm_and_si128(vbias0, vexpw_max);
108 vbias1 = _mm_and_si128(vbias1, vexpw_max);
109 vbias2 = _mm_and_si128(vbias2, vexpw_max);
110 vbias3 = _mm_and_si128(vbias3, vexpw_max);
111 vbias4 = _mm_and_si128(vbias4, vexpw_max);
112 vbias5 = _mm_and_si128(vbias5, vexpw_max);
113 vbias6 = _mm_and_si128(vbias6, vexpw_max);
114 vbias7 = _mm_and_si128(vbias7, vexpw_max);
115
116 vf0 = _mm_mul_ps(vf0, vscale_to_zero);
117 vf1 = _mm_mul_ps(vf1, vscale_to_zero);
118 vf2 = _mm_mul_ps(vf2, vscale_to_zero);
119 vf3 = _mm_mul_ps(vf3, vscale_to_zero);
120 vf4 = _mm_mul_ps(vf4, vscale_to_zero);
121 vf5 = _mm_mul_ps(vf5, vscale_to_zero);
122 vf6 = _mm_mul_ps(vf6, vscale_to_zero);
123 vf7 = _mm_mul_ps(vf7, vscale_to_zero);
124
125 const __m128i vnanmaskh0 = _mm_packs_epi32(vnanmaskw0, vnanmaskw1);
126 const __m128i vnanmaskh1 = _mm_packs_epi32(vnanmaskw2, vnanmaskw3);
127 const __m128i vnanmaskh2 = _mm_packs_epi32(vnanmaskw4, vnanmaskw5);
128 const __m128i vnanmaskh3 = _mm_packs_epi32(vnanmaskw6, vnanmaskw7);
129
130 const __m128i vsignh0 = _mm_packs_epi32(_mm_castps_si128(vsignx0), _mm_castps_si128(vsignx1));
131 const __m128i vsignh1 = _mm_packs_epi32(_mm_castps_si128(vsignx2), _mm_castps_si128(vsignx3));
132 const __m128i vsignh2 = _mm_packs_epi32(_mm_castps_si128(vsignx4), _mm_castps_si128(vsignx5));
133 const __m128i vsignh3 = _mm_packs_epi32(_mm_castps_si128(vsignx6), _mm_castps_si128(vsignx7));
134
135 vbias0 = _mm_max_epi16(vbias0, vbias_min);
136 vbias1 = _mm_max_epi16(vbias1, vbias_min);
137 vbias2 = _mm_max_epi16(vbias2, vbias_min);
138 vbias3 = _mm_max_epi16(vbias3, vbias_min);
139 vbias4 = _mm_max_epi16(vbias4, vbias_min);
140 vbias5 = _mm_max_epi16(vbias5, vbias_min);
141 vbias6 = _mm_max_epi16(vbias6, vbias_min);
142 vbias7 = _mm_max_epi16(vbias7, vbias_min);
143
144
145 vf0 = _mm_add_ps(vf0, _mm_castsi128_ps(vbias0));
146 vf1 = _mm_add_ps(vf1, _mm_castsi128_ps(vbias1));
147 vf2 = _mm_add_ps(vf2, _mm_castsi128_ps(vbias2));
148 vf3 = _mm_add_ps(vf3, _mm_castsi128_ps(vbias3));
149 vf4 = _mm_add_ps(vf4, _mm_castsi128_ps(vbias4));
150 vf5 = _mm_add_ps(vf5, _mm_castsi128_ps(vbias5));
151 vf6 = _mm_add_ps(vf6, _mm_castsi128_ps(vbias6));
152 vf7 = _mm_add_ps(vf7, _mm_castsi128_ps(vbias7));
153
154
155 __m128i vexpw0 = _mm_srli_epi32(_mm_castps_si128(vf0), 13);
156 __m128i vexpw1 = _mm_srli_epi32(_mm_castps_si128(vf1), 13);
157 __m128i vexpw2 = _mm_srli_epi32(_mm_castps_si128(vf2), 13);
158 __m128i vexpw3 = _mm_srli_epi32(_mm_castps_si128(vf3), 13);
159 __m128i vexpw4 = _mm_srli_epi32(_mm_castps_si128(vf4), 13);
160 __m128i vexpw5 = _mm_srli_epi32(_mm_castps_si128(vf5), 13);
161 __m128i vexpw6 = _mm_srli_epi32(_mm_castps_si128(vf6), 13);
162 __m128i vexpw7 = _mm_srli_epi32(_mm_castps_si128(vf7), 13);
163
164 const __m128i vmantw0 = _mm_and_si128(_mm_castps_si128(vf0), vmanth_mask);
165 const __m128i vmantw1 = _mm_and_si128(_mm_castps_si128(vf1), vmanth_mask);
166 const __m128i vmantw2 = _mm_and_si128(_mm_castps_si128(vf2), vmanth_mask);
167 const __m128i vmantw3 = _mm_and_si128(_mm_castps_si128(vf3), vmanth_mask);
168 const __m128i vmantw4 = _mm_and_si128(_mm_castps_si128(vf4), vmanth_mask);
169 const __m128i vmantw5 = _mm_and_si128(_mm_castps_si128(vf5), vmanth_mask);
170 const __m128i vmantw6 = _mm_and_si128(_mm_castps_si128(vf6), vmanth_mask);
171 const __m128i vmantw7 = _mm_and_si128(_mm_castps_si128(vf7), vmanth_mask);
172
173 vexpw0 = _mm_and_si128(vexpw0, vexph_mask);
174 vexpw1 = _mm_and_si128(vexpw1, vexph_mask);
175 vexpw2 = _mm_and_si128(vexpw2, vexph_mask);
176 vexpw3 = _mm_and_si128(vexpw3, vexph_mask);
177 vexpw4 = _mm_and_si128(vexpw4, vexph_mask);
178 vexpw5 = _mm_and_si128(vexpw5, vexph_mask);
179 vexpw6 = _mm_and_si128(vexpw6, vexph_mask);
180 vexpw7 = _mm_and_si128(vexpw7, vexph_mask);
181
182 const __m128i vnonsignw0 = _mm_add_epi32(vmantw0, vexpw0);
183 const __m128i vnonsignw1 = _mm_add_epi32(vmantw1, vexpw1);
184 const __m128i vnonsignw2 = _mm_add_epi32(vmantw2, vexpw2);
185 const __m128i vnonsignw3 = _mm_add_epi32(vmantw3, vexpw3);
186 const __m128i vnonsignw4 = _mm_add_epi32(vmantw4, vexpw4);
187 const __m128i vnonsignw5 = _mm_add_epi32(vmantw5, vexpw5);
188 const __m128i vnonsignw6 = _mm_add_epi32(vmantw6, vexpw6);
189 const __m128i vnonsignw7 = _mm_add_epi32(vmantw7, vexpw7);
190
191 const __m128i vnonsignh0 = _mm_packs_epi32(vnonsignw0, vnonsignw1);
192 const __m128i vnonsignh1 = _mm_packs_epi32(vnonsignw2, vnonsignw3);
193 const __m128i vnonsignh2 = _mm_packs_epi32(vnonsignw4, vnonsignw5);
194 const __m128i vnonsignh3 = _mm_packs_epi32(vnonsignw6, vnonsignw7);
195
196 const __m128i vabsh0 = _mm_blendv_epi8(vnonsignh0, vnanh, vnanmaskh0);
197 const __m128i vabsh1 = _mm_blendv_epi8(vnonsignh1, vnanh, vnanmaskh1);
198 const __m128i vabsh2 = _mm_blendv_epi8(vnonsignh2, vnanh, vnanmaskh2);
199 const __m128i vabsh3 = _mm_blendv_epi8(vnonsignh3, vnanh, vnanmaskh3);
200
201 const __m128i vh0 = _mm_or_si128(vabsh0, vsignh0);
202 const __m128i vh1 = _mm_or_si128(vabsh1, vsignh1);
203 const __m128i vh2 = _mm_or_si128(vabsh2, vsignh2);
204 const __m128i vh3 = _mm_or_si128(vabsh3, vsignh3);
205
206 _mm_storeu_si128((__m128i*) o, vh0);
207 _mm_storeu_si128((__m128i*) (o + 8), vh1);
208 _mm_storeu_si128((__m128i*) (o + 16), vh2);
209 _mm_storeu_si128((__m128i*) (o + 24), vh3);
210 o += 32;
211 }
212 for (; batch >= 8 * sizeof(float); batch -= 8 * sizeof(float)) {
213 const __m128 vx_lo = _mm_loadu_ps(input);
214 const __m128 vx_hi = _mm_loadu_ps(input + 4);
215 input += 8;
216
217 const __m128 vabsx_lo = _mm_and_ps(vx_lo, vnonsign_mask);
218 const __m128 vabsx_hi = _mm_and_ps(vx_hi, vnonsign_mask);
219
220 const __m128 vsignx_lo = _mm_xor_ps(vx_lo, vabsx_lo);
221 const __m128 vsignx_hi = _mm_xor_ps(vx_hi, vabsx_hi);
222 __m128i vbias_lo = _mm_add_epi32(_mm_castps_si128(vabsx_lo), vexp_bias);
223 __m128i vbias_hi = _mm_add_epi32(_mm_castps_si128(vabsx_hi), vexp_bias);
224 __m128 vf_lo = _mm_mul_ps(vabsx_lo, vscale_to_inf);
225 __m128 vf_hi = _mm_mul_ps(vabsx_hi, vscale_to_inf);
226 const __m128i vnanmaskw_lo = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_lo), vexpw_max);
227 const __m128i vnanmaskw_hi = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_hi), vexpw_max);
228
229 vbias_lo = _mm_and_si128(vbias_lo, vexpw_max);
230 vbias_hi = _mm_and_si128(vbias_hi, vexpw_max);
231 vf_lo = _mm_mul_ps(vf_lo, vscale_to_zero);
232 vf_hi = _mm_mul_ps(vf_hi, vscale_to_zero);
233 const __m128i vnanmaskh = _mm_packs_epi32(vnanmaskw_lo, vnanmaskw_hi);
234 const __m128i vsignh = _mm_packs_epi32(_mm_castps_si128(vsignx_lo), _mm_castps_si128(vsignx_hi));
235
236 vbias_lo = _mm_max_epi16(vbias_lo, vbias_min);
237 vbias_hi = _mm_max_epi16(vbias_hi, vbias_min);
238
239 vf_lo = _mm_add_ps(vf_lo, _mm_castsi128_ps(vbias_lo));
240 vf_hi = _mm_add_ps(vf_hi, _mm_castsi128_ps(vbias_hi));
241
242 __m128i vexpw_lo = _mm_srli_epi32(_mm_castps_si128(vf_lo), 13);
243 __m128i vexpw_hi = _mm_srli_epi32(_mm_castps_si128(vf_hi), 13);
244 const __m128i vmantw_lo = _mm_and_si128(_mm_castps_si128(vf_lo), vmanth_mask);
245 const __m128i vmantw_hi = _mm_and_si128(_mm_castps_si128(vf_hi), vmanth_mask);
246
247 vexpw_lo = _mm_and_si128(vexpw_lo, vexph_mask);
248 vexpw_hi = _mm_and_si128(vexpw_hi, vexph_mask);
249
250 const __m128i vnonsignw_lo = _mm_add_epi32(vmantw_lo, vexpw_lo);
251 const __m128i vnonsignw_hi = _mm_add_epi32(vmantw_hi, vexpw_hi);
252
253 const __m128i vnonsignh = _mm_packs_epi32(vnonsignw_lo, vnonsignw_hi);
254
255 const __m128i vabsh = _mm_blendv_epi8(vnonsignh, vnanh, vnanmaskh);
256
257 const __m128i vh = _mm_or_si128(vabsh, vsignh);
258
259 _mm_storeu_si128((__m128i*) o, vh);
260 o += 8;
261 }
262 if XNN_UNPREDICTABLE(batch != 0) {
263 const __m128 vx_lo = _mm_loadu_ps(input);
264 const float* input_hi = (const float*) ((uintptr_t) input + (batch & (4 * sizeof(float))));
265 const __m128 vx_hi = _mm_loadu_ps(input_hi);
266
267 const __m128 vabsx_lo = _mm_and_ps(vx_lo, vnonsign_mask);
268 const __m128 vabsx_hi = _mm_and_ps(vx_hi, vnonsign_mask);
269
270 const __m128 vsignx_lo = _mm_xor_ps(vx_lo, vabsx_lo);
271 const __m128 vsignx_hi = _mm_xor_ps(vx_hi, vabsx_hi);
272 __m128i vbias_lo = _mm_add_epi32(_mm_castps_si128(vabsx_lo), vexp_bias);
273 __m128i vbias_hi = _mm_add_epi32(_mm_castps_si128(vabsx_hi), vexp_bias);
274 __m128 vf_lo = _mm_mul_ps(vabsx_lo, vscale_to_inf);
275 __m128 vf_hi = _mm_mul_ps(vabsx_hi, vscale_to_inf);
276 const __m128i vnanmaskw_lo = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_lo), vexpw_max);
277 const __m128i vnanmaskw_hi = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_hi), vexpw_max);
278
279 vbias_lo = _mm_and_si128(vbias_lo, vexpw_max);
280 vbias_hi = _mm_and_si128(vbias_hi, vexpw_max);
281 vf_lo = _mm_mul_ps(vf_lo, vscale_to_zero);
282 vf_hi = _mm_mul_ps(vf_hi, vscale_to_zero);
283 const __m128i vnanmaskh = _mm_packs_epi32(vnanmaskw_lo, vnanmaskw_hi);
284 const __m128i vsignh = _mm_packs_epi32(_mm_castps_si128(vsignx_lo), _mm_castps_si128(vsignx_hi));
285
286 vbias_lo = _mm_max_epi16(vbias_lo, vbias_min);
287 vbias_hi = _mm_max_epi16(vbias_hi, vbias_min);
288
289 vf_lo = _mm_add_ps(vf_lo, _mm_castsi128_ps(vbias_lo));
290 vf_hi = _mm_add_ps(vf_hi, _mm_castsi128_ps(vbias_hi));
291
292 __m128i vexpw_lo = _mm_srli_epi32(_mm_castps_si128(vf_lo), 13);
293 __m128i vexpw_hi = _mm_srli_epi32(_mm_castps_si128(vf_hi), 13);
294 const __m128i vmantw_lo = _mm_and_si128(_mm_castps_si128(vf_lo), vmanth_mask);
295 const __m128i vmantw_hi = _mm_and_si128(_mm_castps_si128(vf_hi), vmanth_mask);
296
297 vexpw_lo = _mm_and_si128(vexpw_lo, vexph_mask);
298 vexpw_hi = _mm_and_si128(vexpw_hi, vexph_mask);
299
300 const __m128i vnonsignw_lo = _mm_add_epi32(vmantw_lo, vexpw_lo);
301 const __m128i vnonsignw_hi = _mm_add_epi32(vmantw_hi, vexpw_hi);
302
303 const __m128i vnonsignh = _mm_packs_epi32(vnonsignw_lo, vnonsignw_hi);
304
305 const __m128i vabsh = _mm_blendv_epi8(vnonsignh, vnanh, vnanmaskh);
306
307 __m128i vh = _mm_or_si128(vabsh, vsignh);
308
309 if (batch & (4 * sizeof(float))) {
310 _mm_storel_epi64((__m128i*) o, vh);
311 vh = _mm_unpackhi_epi64(vh, vh);
312 o += 4;
313 }
314 if (batch & (2 * sizeof(float))) {
315 unaligned_store_u32(o, (uint32_t) _mm_cvtsi128_si32(vh));
316 vh = _mm_srli_epi64(vh, 32);
317 o += 2;
318 }
319 if (batch & (1 * sizeof(float))) {
320 *o = (uint16_t) _mm_extract_epi16(vh, 0);
321 }
322 }
323 }