Debian Codegraph @

tools/tests/x86_emulator/simd.c

raw
1 #include "simd.h"
2
3 ENTRY(simd_test);
4
5 #if defined(__AVX512F__)
6 # define ALL_TRUE (~0ULL >> (64 - ELEM_COUNT))
7 # if VEC_SIZE == 4
8 # define eq(x, y) ({ \
9 float x_ = (x)[0]; \
10 float __attribute__((vector_size(16))) y_ = { (y)[0] }; \
11 unsigned short r_; \
12 asm ( "vcmpss $0, %1, %2, %0" : "=k" (r_) : "m" (x_), "v" (y_) ); \
13 r_ == 1; \
14 })
15 # elif VEC_SIZE == 8
16 # define eq(x, y) ({ \
17 double x_ = (x)[0]; \
18 double __attribute__((vector_size(16))) y_ = { (y)[0] }; \
19 unsigned short r_; \
20 asm ( "vcmpsd $0, %1, %2, %0" : "=k" (r_) : "m" (x_), "v" (y_) ); \
21 r_ == 1; \
22 })
23 # elif VEC_SIZE == 2
24 # define eq(x, y) ({ \
25 _Float16 x_ = (x)[0]; \
26 _Float16 __attribute__((vector_size(16))) y_ = { (y)[0] }; \
27 unsigned int r_; \
28 asm ( "vcmpsh $0, %1, %2, %0" : "=k" (r_) : "m" (x_), "v" (y_) ); \
29 r_ == 1; \
30 })
31 # elif FLOAT_SIZE == 4
32 /*
33 * gcc's (up to at least 8.2) __builtin_ia32_cmpps256_mask() has an anomaly in
34 * that its return type is QI rather than UQI, and hence the value would get
35 * sign-extended before comapring to ALL_TRUE. The same oddity does not matter
36 * for __builtin_ia32_cmppd256_mask(), as there only 4 bits are significant.
37 * Hence the extra " & ALL_TRUE".
38 */
39 # define eq(x, y) ((BR(cmpps, _mask, x, y, 0, -1) & ALL_TRUE) == ALL_TRUE)
40 # elif FLOAT_SIZE == 8
41 # define eq(x, y) (BR(cmppd, _mask, x, y, 0, -1) == ALL_TRUE)
42 # elif FLOAT_SIZE == 2
43 # define eq(x, y) (B(cmpph, _mask, x, y, 0, -1) == ALL_TRUE)
44 # elif (INT_SIZE == 1 || UINT_SIZE == 1) && defined(__AVX512BW__)
45 # define eq(x, y) (B(pcmpeqb, _mask, (vqi_t)(x), (vqi_t)(y), -1) == ALL_TRUE)
46 # elif (INT_SIZE == 2 || UINT_SIZE == 2) && defined(__AVX512BW__)
47 # define eq(x, y) (B(pcmpeqw, _mask, (vhi_t)(x), (vhi_t)(y), -1) == ALL_TRUE)
48 # elif INT_SIZE == 4 || UINT_SIZE == 4
49 # define eq(x, y) (B(pcmpeqd, _mask, (vsi_t)(x), (vsi_t)(y), -1) == ALL_TRUE)
50 # elif INT_SIZE == 8 || UINT_SIZE == 8
51 # define eq(x, y) (B(pcmpeqq, _mask, (vdi_t)(x), (vdi_t)(y), -1) == ALL_TRUE)
52 # endif
53 #elif VEC_SIZE == 8 && defined(__SSE__)
54 # define to_bool(cmp) (__builtin_ia32_pmovmskb(cmp) == 0xff)
55 #elif VEC_SIZE == 16
56 # if defined(__AVX__) && defined(FLOAT_SIZE)
57 # if ELEM_SIZE == 4
58 # define to_bool(cmp) __builtin_ia32_vtestcps(cmp, (vec_t){} == 0)
59 # elif ELEM_SIZE == 8
60 # define to_bool(cmp) __builtin_ia32_vtestcpd(cmp, (vec_t){} == 0)
61 # endif
62 # elif defined(__SSE4_1__)
63 # define to_bool(cmp) __builtin_ia32_ptestc128(cmp, (vdi_t){} == 0)
64 # elif defined(__SSE__) && ELEM_SIZE == 4
65 # define to_bool(cmp) (__builtin_ia32_movmskps(cmp) == 0xf)
66 # elif defined(__SSE2__)
67 # if ELEM_SIZE == 8
68 # define to_bool(cmp) (__builtin_ia32_movmskpd(cmp) == 3)
69 # else
70 # define to_bool(cmp) (__builtin_ia32_pmovmskb128(cmp) == 0xffff)
71 # endif
72 # endif
73 #elif VEC_SIZE == 32
74 # if defined(__AVX2__)
75 # define to_bool(cmp) __builtin_ia32_ptestc256(cmp, (vdi_t){} == 0)
76 # elif defined(__AVX__) && ELEM_SIZE == 4
77 # define to_bool(cmp) (__builtin_ia32_movmskps256(cmp) == 0xff)
78 # elif defined(__AVX__) && ELEM_SIZE == 8
79 # define to_bool(cmp) (__builtin_ia32_movmskpd256(cmp) == 0xf)
80 # endif
81 #endif
82
83 #ifndef to_bool
84 static inline bool _to_bool(byte_vec_t bv)
85 {
86 unsigned int i;
87
88 for ( i = 0; i < VEC_SIZE; ++i )
89 if ( bv[i] != 0xff )
90 return false;
91
92 return true;
93 }
94 # define to_bool(cmp) _to_bool((byte_vec_t)(cmp))
95 #endif
96
97 #ifndef eq
98 # define eq(x, y) to_bool((x) == (y))
99 #endif
100
101 #if VEC_SIZE == FLOAT_SIZE
102 # define to_int(x) ({ int i_ = (x)[0]; touch(i_); ((vec_t){ i_ }); })
103 # ifdef __x86_64__
104 # define to_wint(x) ({ long l_ = (x)[0]; touch(l_); ((vec_t){ l_ }); })
105 # endif
106 # ifdef __AVX512F__
107 /*
108 * Sadly even gcc 9.x, at the time of writing, does not carry out at least
109 * uint -> FP conversions using VCVTUSI2S{S,D}, so we need to use builtins
110 * or inline assembly here. The full-vector parameter types of the builtins
111 * aren't very helpful for our purposes, so use inline assembly.
112 */
113 # if FLOAT_SIZE == 4
114 # define to_u_int(type, x) ({ \
115 unsigned type u_; \
116 float __attribute__((vector_size(16))) t_; \
117 asm ( "vcvtss2usi %1, %0" : "=r" (u_) : "m" ((x)[0]) ); \
118 asm ( "vcvtusi2ss%z1 %1, %0, %0" : "=v" (t_) : "m" (u_) ); \
119 (vec_t){ t_[0] }; \
120 })
121 # elif FLOAT_SIZE == 8
122 # define to_u_int(type, x) ({ \
123 unsigned type u_; \
124 double __attribute__((vector_size(16))) t_; \
125 asm ( "vcvtsd2usi %1, %0" : "=r" (u_) : "m" ((x)[0]) ); \
126 asm ( "vcvtusi2sd%z1 %1, %0, %0" : "=v" (t_) : "m" (u_) ); \
127 (vec_t){ t_[0] }; \
128 })
129 # elif FLOAT_SIZE == 2
130 # define to_u_int(type, x) ({ \
131 unsigned type u_; \
132 _Float16 __attribute__((vector_size(16))) t_; \
133 asm ( "vcvtsh2usi %1, %0" : "=r" (u_) : "m" ((x)[0]) ); \
134 asm ( "vcvtusi2sh%z1 %1, %0, %0" : "=v" (t_) : "m" (u_) ); \
135 (vec_t){ t_[0] }; \
136 })
137 # endif
138 # define to_uint(x) to_u_int(int, x)
139 # ifdef __x86_64__
140 # define to_uwint(x) to_u_int(long, x)
141 # endif
142 # endif
143 #elif VEC_SIZE == 8 && FLOAT_SIZE == 4 && defined(__3dNOW__)
144 # define to_int(x) __builtin_ia32_pi2fd(__builtin_ia32_pf2id(x))
145 #elif defined(FLOAT_SIZE) && VEC_SIZE > FLOAT_SIZE && defined(__AVX512F__) && \
146 (VEC_SIZE == 64 || defined(__AVX512VL__))
147 # if FLOAT_SIZE == 4
148 # define to_int(x) BR(cvtdq2ps, _mask, BR(cvtps2dq, _mask, x, (vsi_t)undef(), ~0), undef(), ~0)
149 # define to_uint(x) BR(cvtudq2ps, _mask, BR(cvtps2udq, _mask, x, (vsi_t)undef(), ~0), undef(), ~0)
150 # ifdef __AVX512DQ__
151 # define to_w_int(x, s) ({ \
152 vsf_half_t t_ = low_half(x); \
153 vdi_t lo_, hi_; \
154 touch(t_); \
155 lo_ = BR(cvtps2 ## s ## qq, _mask, t_, (vdi_t)undef(), ~0); \
156 t_ = high_half(x); \
157 touch(t_); \
158 hi_ = BR(cvtps2 ## s ## qq, _mask, t_, (vdi_t)undef(), ~0); \
159 touch(lo_); touch(hi_); \
160 insert_half(insert_half(undef(), \
161 BR(cvt ## s ## qq2ps, _mask, lo_, (vsf_half_t){}, ~0), 0), \
162 BR(cvt ## s ## qq2ps, _mask, hi_, (vsf_half_t){}, ~0), 1); \
163 })
164 # define to_wint(x) to_w_int(x, )
165 # define to_uwint(x) to_w_int(x, u)
166 # endif
167 # elif FLOAT_SIZE == 8
168 # define to_int(x) B(cvtdq2pd, _mask, BR(cvtpd2dq, _mask, x, (vsi_half_t){}, ~0), undef(), ~0)
169 # define to_uint(x) B(cvtudq2pd, _mask, BR(cvtpd2udq, _mask, x, (vsi_half_t){}, ~0), undef(), ~0)
170 # ifdef __AVX512DQ__
171 # define to_wint(x) BR(cvtqq2pd, _mask, BR(cvtpd2qq, _mask, x, (vdi_t)undef(), ~0), undef(), ~0)
172 # define to_uwint(x) BR(cvtuqq2pd, _mask, BR(cvtpd2uqq, _mask, x, (vdi_t)undef(), ~0), undef(), ~0)
173 # endif
174 # elif FLOAT_SIZE == 2
175 # define to_int(x) BR2(vcvtw2ph, _mask, BR2(vcvtph2w, _mask, x, (vhi_t)undef(), ~0), undef(), ~0)
176 # define to_uint(x) BR2(vcvtuw2ph, _mask, BR2(vcvtph2uw, _mask, x, (vhi_t)undef(), ~0), undef(), ~0)
177 # if VEC_SIZE == 16
178 # define low_half(x) (x)
179 # define high_half(x) ((vec_t)B_(movhlps, , (vsf_t)undef(), (vsf_t)(x)))
180 # define insert_half(x, y, p) ((vec_t)((p) ? B_(movlhps, , (vsf_t)(x), (vsf_t)(y)) \
181 : B_(shufps, , (vsf_t)(y), (vsf_t)(x), 0b11100100)))
182 # elif VEC_SIZE == 32
183 # define _half(x, lh) ((vhf_half_t)B(extracti32x4_, _mask, (vsi_t)(x), lh, (vsi_half_t){}, ~0))
184 # define low_half(x) _half(x, 0)
185 # define high_half(x) _half(x, 1)
186 # define insert_half(x, y, p) \
187 ((vec_t)B(inserti32x4_, _mask, (vsi_t)(x), (vsi_half_t)(y), p, (vsi_t)undef(), ~0))
188 # elif VEC_SIZE == 64
189 # define _half(x, lh) \
190 ((vhf_half_t)__builtin_ia32_extracti64x4_mask((vdi_t)(x), lh, (vdi_half_t){}, ~0))
191 # define low_half(x) _half(x, 0)
192 # define high_half(x) _half(x, 1)
193 # define insert_half(x, y, p) \
194 ((vec_t)__builtin_ia32_inserti64x4_mask((vdi_t)(x), (vdi_half_t)(y), p, (vdi_t)undef(), ~0))
195 # endif
196 # define to_w_int(x, s) ({ \
197 vhf_half_t t_ = low_half(x); \
198 vsi_t lo_, hi_; \
199 touch(t_); \
200 lo_ = BR2(vcvtph2 ## s ## dq, _mask, t_, (vsi_t)undef(), ~0); \
201 t_ = high_half(x); \
202 touch(t_); \
203 hi_ = BR2(vcvtph2 ## s ## dq, _mask, t_, (vsi_t)undef(), ~0); \
204 touch(lo_); touch(hi_); \
205 insert_half(insert_half(undef(), \
206 BR2(vcvt ## s ## dq2ph, _mask, lo_, (vhf_half_t){}, ~0), 0), \
207 BR2(vcvt ## s ## dq2ph, _mask, hi_, (vhf_half_t){}, ~0), 1); \
208 })
209 # define to_wint(x) to_w_int(x, )
210 # define to_uwint(x) to_w_int(x, u)
211 # endif
212 #elif VEC_SIZE == 16 && defined(__SSE2__)
213 # if FLOAT_SIZE == 4
214 # define to_int(x) __builtin_ia32_cvtdq2ps(__builtin_ia32_cvtps2dq(x))
215 # elif FLOAT_SIZE == 8
216 # define to_int(x) __builtin_ia32_cvtdq2pd(__builtin_ia32_cvtpd2dq(x))
217 # endif
218 #elif VEC_SIZE == 32 && defined(__AVX__)
219 # if FLOAT_SIZE == 4
220 # define to_int(x) __builtin_ia32_cvtdq2ps256(__builtin_ia32_cvtps2dq256(x))
221 # elif FLOAT_SIZE == 8
222 # define to_int(x) __builtin_ia32_cvtdq2pd256(__builtin_ia32_cvtpd2dq256(x))
223 # endif
224 #endif
225
226 #if VEC_SIZE == FLOAT_SIZE
227 # define scalar_1op(x, op) ({ \
228 typeof((x)[0]) __attribute__((vector_size(16))) r_; \
229 asm ( op : [out] "=&x" (r_) : [in] "m" (x) ); \
230 (vec_t){ r_[0] }; \
231 })
232 # define scalar_2op(x, y, op) ({ \
233 typeof((x)[0]) __attribute__((vector_size(16))) r_ = { x[0] }; \
234 asm ( op : [out] "=&x" (r_) : [in1] "[out]" (r_), [in2] "m" (y) ); \
235 (vec_t){ r_[0] }; \
236 })
237 #endif
238
239 #if VEC_SIZE == 16 && FLOAT_SIZE == 4 && defined(__SSE__)
240 # define low_half(x) (x)
241 # define high_half(x) B_(movhlps, , undef(), x)
242 /*
243 * GCC 7 (and perhaps earlier) report a bogus type mismatch for the conditional
244 * expression below. All works well with this no-op wrapper.
245 */
246 static inline vec_t movlhps(vec_t x, vec_t y) {
247 return __builtin_ia32_movlhps(x, y);
248 }
249 # define insert_pair(x, y, p) \
250 ((p) ? movlhps(x, y) \
251 : ({ vec_t t_ = (x); t_[0] = (y)[0]; t_[1] = (y)[1]; t_; }))
252 #endif
253
254 #if VEC_SIZE == 8 && FLOAT_SIZE == 4 && defined(__3dNOW_A__)
255 # define max __builtin_ia32_pfmax
256 # define min __builtin_ia32_pfmin
257 # define recip(x) ({ \
258 vec_t t_ = __builtin_ia32_pfrcp(x); \
259 touch(x); \
260 t_[1] = __builtin_ia32_pfrcp(__builtin_ia32_pswapdsf(x))[0]; \
261 touch(x); \
262 __builtin_ia32_pfrcpit2(__builtin_ia32_pfrcpit1(t_, x), t_); \
263 })
264 # define rsqrt(x) ({ \
265 vec_t t_ = __builtin_ia32_pfrsqrt(x); \
266 touch(x); \
267 t_[1] = __builtin_ia32_pfrsqrt(__builtin_ia32_pswapdsf(x))[0]; \
268 touch(x); \
269 __builtin_ia32_pfrcpit2(__builtin_ia32_pfrsqit1(__builtin_ia32_pfmul(t_, t_), x), t_); \
270 })
271 #elif defined(FLOAT_SIZE) && VEC_SIZE == FLOAT_SIZE && defined(__AVX512F__)
272 # if FLOAT_SIZE == 4
273 # define getexp(x) scalar_1op(x, "vgetexpss %[in], %[out], %[out]")
274 # define getmant(x) scalar_1op(x, "vgetmantss $0, %[in], %[out], %[out]")
275 # ifdef __AVX512ER__
276 # define recip(x) scalar_1op(x, "vrcp28ss %[in], %[out], %[out]")
277 # define rsqrt(x) scalar_1op(x, "vrsqrt28ss %[in], %[out], %[out]")
278 # else
279 # define recip(x) scalar_1op(x, "vrcp14ss %[in], %[out], %[out]")
280 # define rsqrt(x) scalar_1op(x, "vrsqrt14ss %[in], %[out], %[out]")
281 # endif
282 # define scale(x, y) scalar_2op(x, y, "vscalefss %[in2], %[in1], %[out]")
283 # define sqrt(x) scalar_1op(x, "vsqrtss %[in], %[out], %[out]")
284 # define trunc(x) scalar_1op(x, "vrndscaless $0b1011, %[in], %[out], %[out]")
285 # elif FLOAT_SIZE == 8
286 # define getexp(x) scalar_1op(x, "vgetexpsd %[in], %[out], %[out]")
287 # define getmant(x) scalar_1op(x, "vgetmantsd $0, %[in], %[out], %[out]")
288 # ifdef __AVX512ER__
289 # define recip(x) scalar_1op(x, "vrcp28sd %[in], %[out], %[out]")
290 # define rsqrt(x) scalar_1op(x, "vrsqrt28sd %[in], %[out], %[out]")
291 # else
292 # define recip(x) scalar_1op(x, "vrcp14sd %[in], %[out], %[out]")
293 # define rsqrt(x) scalar_1op(x, "vrsqrt14sd %[in], %[out], %[out]")
294 # endif
295 # define scale(x, y) scalar_2op(x, y, "vscalefsd %[in2], %[in1], %[out]")
296 # define sqrt(x) scalar_1op(x, "vsqrtsd %[in], %[out], %[out]")
297 # define trunc(x) scalar_1op(x, "vrndscalesd $0b1011, %[in], %[out], %[out]")
298 # elif FLOAT_SIZE == 2
299 # define getexp(x) scalar_1op(x, "vgetexpsh %[in], %[out], %[out]")
300 # define getmant(x) scalar_1op(x, "vgetmantsh $0, %[in], %[out], %[out]")
301 # define recip(x) scalar_1op(x, "vrcpsh %[in], %[out], %[out]")
302 # define rsqrt(x) scalar_1op(x, "vrsqrtsh %[in], %[out], %[out]")
303 # define scale(x, y) scalar_2op(x, y, "vscalefsh %[in2], %[in1], %[out]")
304 # define sqrt(x) scalar_1op(x, "vsqrtsh %[in], %[out], %[out]")
305 # define trunc(x) scalar_1op(x, "vrndscalesh $0b1011, %[in], %[out], %[out]")
306 # endif
307 #elif defined(FLOAT_SIZE) && defined(__AVX512F__) && \
308 (VEC_SIZE == 64 || defined(__AVX512VL__))
309 # if (ELEM_COUNT == 8 && ELEM_SIZE >= 4) /* vextractf{32,64}x4 */ || \
310 (ELEM_COUNT == 16 && ELEM_SIZE == 4 && defined(__AVX512DQ__)) /* vextractf32x8 */ || \
311 (ELEM_COUNT == 4 && ELEM_SIZE == 8 && defined(__AVX512DQ__)) /* vextractf64x2 */
312 # define _half(x, lh) ({ \
313 half_t t_; \
314 asm ( "vextractf%c[w]x%c[n] %[sel], %[s], %[d]" \
315 : [d] "=m" (t_) \
316 : [s] "v" (x), [sel] "i" (lh), \
317 [w] "i" (ELEM_SIZE * 8), [n] "i" (ELEM_COUNT / 2) ); \
318 t_; \
319 })
320 # define low_half(x) _half(x, 0)
321 # define high_half(x) _half(x, 1)
322 # endif
323 # if (ELEM_COUNT == 16 && ELEM_SIZE == 4) /* vextractf32x4 */ || \
324 (ELEM_COUNT == 8 && ELEM_SIZE == 8 && defined(__AVX512DQ__)) /* vextractf64x2 */
325 # define low_quarter(x) ({ \
326 quarter_t t_; \
327 asm ( "vextractf%c[w]x%c[n] $0, %[s], %[d]" \
328 : [d] "=m" (t_) \
329 : [s] "v" (x), [w] "i" (ELEM_SIZE * 8), [n] "i" (ELEM_COUNT / 4) ); \
330 t_; \
331 })
332 # endif
333 # if FLOAT_SIZE == 4
334 # define broadcast(x) ({ \
335 vec_t t_; \
336 asm ( "vbroadcastss %1, %0" \
337 : "=v" (t_) : "m" (*(float[1]){ x }) ); \
338 t_; \
339 })
340 # if VEC_SIZE >= 32 && defined(__AVX512DQ__)
341 # define broadcast_pair(x) ({ \
342 vec_t t_; \
343 asm ( "vbroadcastf32x2 %1, %0" : "=v" (t_) : "m" (x) ); \
344 t_; \
345 })
346 # endif
347 # if VEC_SIZE == 64 && defined(__AVX512DQ__)
348 # define broadcast_octet(x) B(broadcastf32x8_, _mask, x, undef(), ~0)
349 # define insert_octet(x, y, p) B(insertf32x8_, _mask, x, y, p, undef(), ~0)
350 # endif
351 # ifdef __AVX512DQ__
352 # define frac(x) B(reduceps, _mask, x, 0b00001011, undef(), ~0)
353 # endif
354 # define getexp(x) BR(getexpps, _mask, x, undef(), ~0)
355 # define getmant(x) BR(getmantps, _mask, x, 0, undef(), ~0)
356 # ifdef __AVX512DQ__
357 # define max(x, y) BR(rangeps, _mask, x, y, 0b0101, undef(), ~0)
358 # define min(x, y) BR(rangeps, _mask, x, y, 0b0100, undef(), ~0)
359 # else
360 # define max(x, y) BR_(maxps, _mask, x, y, undef(), ~0)
361 # define min(x, y) BR_(minps, _mask, x, y, undef(), ~0)
362 # endif
363 # define mix(x, y) B(blendmps_, _mask, x, y, (0b1010101010101010 & ALL_TRUE))
364 # define scale(x, y) BR(scalefps, _mask, x, y, undef(), ~0)
365 # if VEC_SIZE == 64 && defined(__AVX512ER__)
366 # define recip(x) BR(rcp28ps, _mask, x, undef(), ~0)
367 # define rsqrt(x) BR(rsqrt28ps, _mask, x, undef(), ~0)
368 # else
369 # define recip(x) B(rcp14ps, _mask, x, undef(), ~0)
370 # define rsqrt(x) B(rsqrt14ps, _mask, x, undef(), ~0)
371 # endif
372 # define shrink1(x) BR_(cvtpd2ps, _mask, (vdf_t)(x), (vsf_half_t){}, ~0)
373 # define sqrt(x) BR(sqrtps, _mask, x, undef(), ~0)
374 # define trunc(x) BR(rndscaleps_, _mask, x, 0b1011, undef(), ~0)
375 # define widen1(x) ((vec_t)BR(cvtps2pd, _mask, x, (vdf_t)undef(), ~0))
376 # if VEC_SIZE == 16
377 # define interleave_hi(x, y) B(unpckhps, _mask, x, y, undef(), ~0)
378 # define interleave_lo(x, y) B(unpcklps, _mask, x, y, undef(), ~0)
379 # define swap(x) B(shufps, _mask, x, x, 0b00011011, undef(), ~0)
380 # define swap2(x) B_(vpermilps, _mask, x, 0b00011011, undef(), ~0)
381 # else
382 # define broadcast_quartet(x) B(broadcastf32x4_, _mask, x, undef(), ~0)
383 # define insert_pair(x, y, p) \
384 B(insertf32x4_, _mask, x, \
385 /* Cast needed below to work around gcc 7.x quirk. */ \
386 (p) & 1 ? (typeof(y))__builtin_ia32_shufps(y, y, 0b01000100) : (y), \
387 (p) >> 1, x, 3 << ((p) * 2))
388 # define insert_quartet(x, y, p) B(insertf32x4_, _mask, x, y, p, undef(), ~0)
389 # define interleave_hi(x, y) B(vpermi2varps, _mask, x, interleave_hi, y, ~0)
390 # define interleave_lo(x, y) B(vpermt2varps, _mask, interleave_lo, x, y, ~0)
391 # define swap(x) ({ \
392 vec_t t_ = B(shuf_f32x4_, _mask, x, x, VEC_SIZE == 32 ? 0b01 : 0b00011011, undef(), ~0); \
393 B(shufps, _mask, t_, t_, 0b00011011, undef(), ~0); \
394 })
395 # define swap2(x) B(vpermilps, _mask, \
396 B(shuf_f32x4_, _mask, x, x, \
397 VEC_SIZE == 32 ? 0b01 : 0b00011011, undef(), ~0), \
398 0b00011011, undef(), ~0)
399 # endif
400 # elif FLOAT_SIZE == 8
401 # if VEC_SIZE >= 32
402 # define broadcast(x) ({ \
403 vec_t t_; \
404 asm ( "vbroadcastsd %1, %0" : "=v" (t_) \
405 : "m" (*(double[1]){ x }) ); \
406 t_; \
407 })
408 # else
409 # define broadcast(x) ({ \
410 vec_t t_; \
411 asm ( "vpbroadcastq %1, %0" \
412 : "=v" (t_) : "m" (*(double[1]){ x }) ); \
413 t_; \
414 })
415 # endif
416 # if VEC_SIZE >= 32 && defined(__AVX512DQ__)
417 # define broadcast_pair(x) B(broadcastf64x2_, _mask, x, undef(), ~0)
418 # define insert_pair(x, y, p) B(insertf64x2_, _mask, x, y, p, undef(), ~0)
419 # endif
420 # if VEC_SIZE == 64
421 # define broadcast_quartet(x) B(broadcastf64x4_, , x, undef(), ~0)
422 # define insert_quartet(x, y, p) B(insertf64x4_, _mask, x, y, p, undef(), ~0)
423 # endif
424 # ifdef __AVX512DQ__
425 # define frac(x) B(reducepd, _mask, x, 0b00001011, undef(), ~0)
426 # endif
427 # define getexp(x) BR(getexppd, _mask, x, undef(), ~0)
428 # define getmant(x) BR(getmantpd, _mask, x, 0, undef(), ~0)
429 # ifdef __AVX512DQ__
430 # define max(x, y) BR(rangepd, _mask, x, y, 0b0101, undef(), ~0)
431 # define min(x, y) BR(rangepd, _mask, x, y, 0b0100, undef(), ~0)
432 # else
433 # define max(x, y) BR_(maxpd, _mask, x, y, undef(), ~0)
434 # define min(x, y) BR_(minpd, _mask, x, y, undef(), ~0)
435 # endif
436 # define mix(x, y) B(blendmpd_, _mask, x, y, 0b10101010)
437 # define scale(x, y) BR(scalefpd, _mask, x, y, undef(), ~0)
438 # if VEC_SIZE == 64 && defined(__AVX512ER__)
439 # define recip(x) BR(rcp28pd, _mask, x, undef(), ~0)
440 # define rsqrt(x) BR(rsqrt28pd, _mask, x, undef(), ~0)
441 # else
442 # define recip(x) B(rcp14pd, _mask, x, undef(), ~0)
443 # define rsqrt(x) B(rsqrt14pd, _mask, x, undef(), ~0)
444 # endif
445 # define sqrt(x) BR(sqrtpd, _mask, x, undef(), ~0)
446 # define trunc(x) BR(rndscalepd_, _mask, x, 0b1011, undef(), ~0)
447 # if VEC_SIZE == 16
448 # define interleave_hi(x, y) B(unpckhpd, _mask, x, y, undef(), ~0)
449 # define interleave_lo(x, y) B(unpcklpd, _mask, x, y, undef(), ~0)
450 # define swap(x) B(shufpd, _mask, x, x, 0b01, undef(), ~0)
451 # define swap2(x) B_(vpermilpd, _mask, x, 0b01, undef(), ~0)
452 # else
453 # define interleave_hi(x, y) B(vpermi2varpd, _mask, x, interleave_hi, y, ~0)
454 # define interleave_lo(x, y) B(vpermt2varpd, _mask, interleave_lo, x, y, ~0)
455 # define swap(x) ({ \
456 vec_t t_ = B(shuf_f64x2_, _mask, x, x, VEC_SIZE == 32 ? 0b01 : 0b00011011, undef(), ~0); \
457 B(shufpd, _mask, t_, t_, 0b01010101, undef(), ~0); \
458 })
459 # define swap2(x) B(vpermilpd, _mask, \
460 B(shuf_f64x2_, _mask, x, x, \
461 VEC_SIZE == 32 ? 0b01 : 0b00011011, undef(), ~0), \
462 0b01010101, undef(), ~0)
463 # endif
464 # elif FLOAT_SIZE == 2
465 # define frac(x) BR2(reduceph, _mask, x, 0b00001011, undef(), ~0)
466 # define getexp(x) BR(getexpph, _mask, x, undef(), ~0)
467 # define getmant(x) BR(getmantph, _mask, x, 0, undef(), ~0)
468 # define max(x, y) BR2(maxph, _mask, x, y, undef(), ~0)
469 # define min(x, y) BR2(minph, _mask, x, y, undef(), ~0)
470 # define scale(x, y) BR2(scalefph, _mask, x, y, undef(), ~0)
471 # define recip(x) B(rcpph, _mask, x, undef(), ~0)
472 # define rsqrt(x) B(rsqrtph, _mask, x, undef(), ~0)
473 # define shrink1(x) BR2(vcvtps2phx, _mask, (vsf_t)(x), (vhf_half_t){}, ~0)
474 # define shrink2(x) BR2(vcvtpd2ph, _mask, (vdf_t)(x), (vhf_quarter_t){}, ~0)
475 # define sqrt(x) BR2(sqrtph, _mask, x, undef(), ~0)
476 # define trunc(x) BR2(rndscaleph, _mask, x, 0b1011, undef(), ~0)
477 # define widen1(x) ((vec_t)BR2(vcvtph2psx, _mask, x, (vsf_t)undef(), ~0))
478 # define widen2(x) ((vec_t)BR2(vcvtph2pd, _mask, x, (vdf_t)undef(), ~0))
479 # endif
480 #elif FLOAT_SIZE == 4 && defined(__SSE__)
481 # if VEC_SIZE == 32 && defined(__AVX__)
482 # if defined(__AVX2__)
483 # define broadcast(x) \
484 __builtin_ia32_vbroadcastss_ps256((float __attribute__((vector_size(16)))){ x })
485 # else
486 # define broadcast(x) ({ float t_ = (x); __builtin_ia32_vbroadcastss256(&t_); })
487 # endif
488 # define max(x, y) __builtin_ia32_maxps256(x, y)
489 # define min(x, y) __builtin_ia32_minps256(x, y)
490 # define recip(x) __builtin_ia32_rcpps256(x)
491 # define rsqrt(x) __builtin_ia32_rsqrtps256(x)
492 # define sqrt(x) __builtin_ia32_sqrtps256(x)
493 # define swap(x) ({ \
494 vec_t t_ = __builtin_ia32_vpermilps256(x, 0b00011011); \
495 __builtin_ia32_vperm2f128_ps256(t_, t_, 0b00000001); \
496 })
497 # ifdef __AVX2__
498 # define swap2(x) __builtin_ia32_permvarsf256(x, __builtin_ia32_cvtps2dq256(inv) - 1)
499 # else
500 # define swap2(x) ({ \
501 vec_t t_ = __builtin_ia32_vpermilvarps256(x, __builtin_ia32_cvtps2dq256(inv) - 1); \
502 __builtin_ia32_vperm2f128_ps256(t_, t_, 0b00000001); \
503 })
504 # endif
505 # elif VEC_SIZE == 16
506 # if defined(__AVX2__)
507 # define broadcast(x) __builtin_ia32_vbroadcastss_ps((vec_t){ x })
508 # elif defined(__AVX__)
509 # define broadcast(x) ({ float t_ = (x); __builtin_ia32_vbroadcastss(&t_); })
510 # endif
511 # define interleave_hi(x, y) __builtin_ia32_unpckhps(x, y)
512 # define interleave_lo(x, y) __builtin_ia32_unpcklps(x, y)
513 # define max(x, y) __builtin_ia32_maxps(x, y)
514 # define min(x, y) __builtin_ia32_minps(x, y)
515 # define recip(x) __builtin_ia32_rcpps(x)
516 # define rsqrt(x) __builtin_ia32_rsqrtps(x)
517 # define sqrt(x) __builtin_ia32_sqrtps(x)
518 # define swap(x) __builtin_ia32_shufps(x, x, 0b00011011)
519 # ifdef __AVX__
520 # define swap2(x) __builtin_ia32_vpermilvarps(x, __builtin_ia32_cvtps2dq(inv) - 1)
521 # endif
522 # elif VEC_SIZE == 4
523 # define recip(x) scalar_1op(x, "rcpss %[in], %[out]")
524 # define rsqrt(x) scalar_1op(x, "rsqrtss %[in], %[out]")
525 # define sqrt(x) scalar_1op(x, "sqrtss %[in], %[out]")
526 # endif
527 #elif FLOAT_SIZE == 8 && defined(__SSE2__)
528 # if VEC_SIZE == 32 && defined(__AVX__)
529 # if defined(__AVX2__)
530 # define broadcast(x) \
531 __builtin_ia32_vbroadcastsd_pd256((double __attribute__((vector_size(16)))){ x })
532 # else
533 # define broadcast(x) ({ double t_ = (x); __builtin_ia32_vbroadcastsd256(&t_); })
534 # endif
535 # define max(x, y) __builtin_ia32_maxpd256(x, y)
536 # define min(x, y) __builtin_ia32_minpd256(x, y)
537 # define recip(x) ({ \
538 float __attribute__((vector_size(16))) t_ = __builtin_ia32_cvtpd2ps256(x); \
539 t_ = __builtin_ia32_vextractf128_ps256( \
540 __builtin_ia32_rcpps256( \
541 __builtin_ia32_vbroadcastf128_ps256(&t_)), 0); \
542 __builtin_ia32_cvtps2pd256(t_); \
543 })
544 # define rsqrt(x) ({ \
545 float __attribute__((vector_size(16))) t1_ = __builtin_ia32_cvtpd2ps256(x); \
546 float __attribute__((vector_size(32))) t2_ = __builtin_ia32_vinsertf128_ps256((typeof(t2_)){}, t1_, 0); \
547 t2_ = __builtin_ia32_vinsertf128_ps256(t2_, t1_, 1); \
548 t1_ = __builtin_ia32_vextractf128_ps256(__builtin_ia32_rsqrtps256(t2_), 0); \
549 __builtin_ia32_cvtps2pd256(t1_); \
550 })
551 # define sqrt(x) __builtin_ia32_sqrtpd256(x)
552 # define swap(x) ({ \
553 vec_t t_ = __builtin_ia32_vpermilpd256(x, 0b00000101); \
554 __builtin_ia32_vperm2f128_pd256(t_, t_, 0b00000001); \
555 })
556 # ifdef __AVX2__
557 # define swap2(x) __builtin_ia32_permdf256(x, 0b00011011)
558 # endif
559 # elif VEC_SIZE == 16
560 # define interleave_hi(x, y) __builtin_ia32_unpckhpd(x, y)
561 # define interleave_lo(x, y) __builtin_ia32_unpcklpd(x, y)
562 # define max(x, y) __builtin_ia32_maxpd(x, y)
563 # define min(x, y) __builtin_ia32_minpd(x, y)
564 # define recip(x) __builtin_ia32_cvtps2pd(__builtin_ia32_rcpps(__builtin_ia32_cvtpd2ps(x)))
565 # define rsqrt(x) __builtin_ia32_cvtps2pd(__builtin_ia32_rsqrtps(__builtin_ia32_cvtpd2ps(x)))
566 # define sqrt(x) __builtin_ia32_sqrtpd(x)
567 # define swap(x) __builtin_ia32_shufpd(x, x, 0b01)
568 # ifdef __AVX__
569 # define swap2(x) __builtin_ia32_vpermilvarpd(x, __builtin_ia32_pmovsxdq128( \
570 __builtin_ia32_cvtpd2dq(inv) - 1) << 1)
571 # endif
572 # elif VEC_SIZE == 8
573 # define recip(x) scalar_1op(x, "cvtsd2ss %[in], %[out]; rcpss %[out], %[out]; cvtss2sd %[out], %[out]")
574 # define rsqrt(x) scalar_1op(x, "cvtsd2ss %[in], %[out]; rsqrtss %[out], %[out]; cvtss2sd %[out], %[out]")
575 # define sqrt(x) scalar_1op(x, "sqrtsd %[in], %[out]")
576 # endif
577 #endif
578 #if (INT_SIZE == 4 || UINT_SIZE == 4 || INT_SIZE == 8 || UINT_SIZE == 8) && \
579 defined(__AVX512F__) && (VEC_SIZE == 64 || defined(__AVX512VL__))
580 # if ELEM_COUNT == 8 /* vextracti{32,64}x4 */ || \
581 (ELEM_COUNT == 16 && ELEM_SIZE == 4 && defined(__AVX512DQ__)) /* vextracti32x8 */ || \
582 (ELEM_COUNT == 4 && ELEM_SIZE == 8 && defined(__AVX512DQ__)) /* vextracti64x2 */
583 # define low_half(x) ({ \
584 half_t t_; \
585 asm ( "vextracti%c[w]x%c[n] $0, %[s], %[d]" \
586 : [d] "=m" (t_) \
587 : [s] "v" (x), [w] "i" (ELEM_SIZE * 8), [n] "i" (ELEM_COUNT / 2) ); \
588 t_; \
589 })
590 # endif
591 # if (ELEM_COUNT == 16 && ELEM_SIZE == 4) /* vextracti32x4 */ || \
592 (ELEM_COUNT == 8 && ELEM_SIZE == 8 && defined(__AVX512DQ__)) /* vextracti64x2 */
593 # define low_quarter(x) ({ \
594 quarter_t t_; \
595 asm ( "vextracti%c[w]x%c[n] $0, %[s], %[d]" \
596 : [d] "=m" (t_) \
597 : [s] "v" (x), [w] "i" (ELEM_SIZE * 8), [n] "i" (ELEM_COUNT / 4) ); \
598 t_; \
599 })
600 # endif
601 # if INT_SIZE == 4 || UINT_SIZE == 4
602 # define broadcast(x) ({ \
603 vec_t t_; \
604 asm ( "vpbroadcastd %1, %0" \
605 : "=v" (t_) : "m" (*(int[1]){ x }) ); \
606 t_; \
607 })
608 # define broadcast2(x) ({ \
609 vec_t t_; \
610 asm ( "vpbroadcastd %k1, %0" : "=v" (t_) : "r" (x) ); \
611 t_; \
612 })
613 # ifdef __AVX512DQ__
614 # define broadcast_pair(x) ({ \
615 vec_t t_; \
616 asm ( "vbroadcasti32x2 %1, %0" : "=v" (t_) : "m" (x) ); \
617 t_; \
618 })
619 # endif
620 # if VEC_SIZE == 64 && defined(__AVX512DQ__)
621 # define broadcast_octet(x) ((vec_t)B(broadcasti32x8_, _mask, (vsi_octet_t)(x), (vsi_t)undef(), ~0))
622 # define insert_octet(x, y, p) ((vec_t)B(inserti32x8_, _mask, (vsi_t)(x), (vsi_octet_t)(y), p, (vsi_t)undef(), ~0))
623 # endif
624 # if VEC_SIZE == 16
625 # define interleave_hi(x, y) ((vec_t)B(punpckhdq, _mask, (vsi_t)(x), (vsi_t)(y), (vsi_t)undef(), ~0))
626 # define interleave_lo(x, y) ((vec_t)B(punpckldq, _mask, (vsi_t)(x), (vsi_t)(y), (vsi_t)undef(), ~0))
627 # define swap(x) ((vec_t)B(pshufd, _mask, (vsi_t)(x), 0b00011011, (vsi_t)undef(), ~0))
628 # else
629 # define broadcast_quartet(x) ((vec_t)B(broadcasti32x4_, _mask, (vsi_quartet_t)(x), (vsi_t)undef(), ~0))
630 # define insert_pair(x, y, p) \
631 (vec_t)(B(inserti32x4_, _mask, (vsi_t)(x), \
632 /* First cast needed below to work around gcc 7.x quirk. */ \
633 (p) & 1 ? (vsi_pair_t)__builtin_ia32_pshufd((vsi_pair_t)(y), 0b01000100) \
634 : (vsi_pair_t)(y), \
635 (p) >> 1, (vsi_t)(x), 3 << ((p) * 2)))
636 # define insert_quartet(x, y, p) ((vec_t)B(inserti32x4_, _mask, (vsi_t)(x), (vsi_quartet_t)(y), p, (vsi_t)undef(), ~0))
637 # define interleave_hi(x, y) ((vec_t)B(vpermi2vard, _mask, (vsi_t)(x), interleave_hi, (vsi_t)(y), ~0))
638 # define interleave_lo(x, y) ((vec_t)B(vpermt2vard, _mask, interleave_lo, (vsi_t)(x), (vsi_t)(y), ~0))
639 # define swap(x) ((vec_t)B(pshufd, _mask, \
640 B(shuf_i32x4_, _mask, (vsi_t)(x), (vsi_t)(x), \
641 VEC_SIZE == 32 ? 0b01 : 0b00011011, (vsi_t)undef(), ~0), \
642 0b00011011, (vsi_t)undef(), ~0))
643 # define swap2(x) ((vec_t)B_(permvarsi, _mask, (vsi_t)(x), (vsi_t)(inv - 1), (vsi_t)undef(), ~0))
644 # endif
645 # define mix(x, y) ((vec_t)B(blendmd_, _mask, (vsi_t)(x), (vsi_t)(y), \
646 (0b1010101010101010 & ((1 << ELEM_COUNT) - 1))))
647 # define rotr(x, n) ((vec_t)B(alignd, _mask, (vsi_t)(x), (vsi_t)(x), n, (vsi_t)undef(), ~0))
648 # define shrink1(x) ((half_t)B(pmovqd, _mask, (vdi_t)(x), (vsi_half_t){}, ~0))
649 # elif INT_SIZE == 8 || UINT_SIZE == 8
650 # define broadcast(x) ({ \
651 vec_t t_; \
652 asm ( "vpbroadcastq %1, %0" \
653 : "=v" (t_) : "m" (*(long long[1]){ x }) ); \
654 t_; \
655 })
656 # ifdef __x86_64__
657 # define broadcast2(x) ({ \
658 vec_t t_; \
659 asm ( "vpbroadcastq %1, %0" : "=v" (t_) : "r" ((x) + 0ULL) ); \
660 t_; \
661 })
662 # endif
663 # if VEC_SIZE >= 32 && defined(__AVX512DQ__)
664 # define broadcast_pair(x) ((vec_t)B(broadcasti64x2_, _mask, (vdi_pair_t)(x), (vdi_t)undef(), ~0))
665 # define insert_pair(x, y, p) ((vec_t)B(inserti64x2_, _mask, (vdi_t)(x), (vdi_pair_t)(y), p, (vdi_t)undef(), ~0))
666 # endif
667 # if VEC_SIZE == 64
668 # define broadcast_quartet(x) ((vec_t)B(broadcasti64x4_, , (vdi_quartet_t)(x), (vdi_t)undef(), ~0))
669 # define insert_quartet(x, y, p) ((vec_t)B(inserti64x4_, _mask, (vdi_t)(x), (vdi_quartet_t)(y), p, (vdi_t)undef(), ~0))
670 # endif
671 # if VEC_SIZE == 16
672 # define interleave_hi(x, y) ((vec_t)B(punpckhqdq, _mask, (vdi_t)(x), (vdi_t)(y), (vdi_t)undef(), ~0))
673 # define interleave_lo(x, y) ((vec_t)B(punpcklqdq, _mask, (vdi_t)(x), (vdi_t)(y), (vdi_t)undef(), ~0))
674 # define swap(x) ((vec_t)B(pshufd, _mask, (vsi_t)(x), 0b01001110, (vsi_t)undef(), ~0))
675 # else
676 # define interleave_hi(x, y) ((vec_t)B(vpermi2varq, _mask, (vdi_t)(x), interleave_hi, (vdi_t)(y), ~0))
677 # define interleave_lo(x, y) ((vec_t)B(vpermt2varq, _mask, interleave_lo, (vdi_t)(x), (vdi_t)(y), ~0))
678 # define swap(x) ((vec_t)B(pshufd, _mask, \
679 (vsi_t)B(shuf_i64x2_, _mask, (vdi_t)(x), (vdi_t)(x), \
680 VEC_SIZE == 32 ? 0b01 : 0b00011011, (vdi_t)undef(), ~0), \
681 0b01001110, (vsi_t)undef(), ~0))
682 # define swap2(x) ((vec_t)B(permvardi, _mask, (vdi_t)(x), (vdi_t)(inv - 1), (vdi_t)undef(), ~0))
683 # endif
684 # define mix(x, y) ((vec_t)B(blendmq_, _mask, (vdi_t)(x), (vdi_t)(y), 0b10101010))
685 # define rotr(x, n) ((vec_t)B(alignq, _mask, (vdi_t)(x), (vdi_t)(x), n, (vdi_t)undef(), ~0))
686 # if VEC_SIZE == 32
687 # define swap3(x) ((vec_t)B_(permdi, _mask, (vdi_t)(x), 0b00011011, (vdi_t)undef(), ~0))
688 # elif VEC_SIZE == 64
689 # define swap3(x) ({ \
690 vdi_t t_ = B_(permdi, _mask, (vdi_t)(x), 0b00011011, (vdi_t)undef(), ~0); \
691 B(shuf_i64x2_, _mask, t_, t_, 0b01001110, (vdi_t)undef(), ~0); \
692 })
693 # endif
694 # endif
695 # if INT_SIZE == 4
696 # define abs(x) B(pabsd, _mask, x, undef(), ~0)
697 # define max(x, y) B(pmaxsd, _mask, x, y, undef(), ~0)
698 # define min(x, y) B(pminsd, _mask, x, y, undef(), ~0)
699 # define mul_full(x, y) ((vec_t)B(pmuldq, _mask, x, y, (vdi_t)undef(), ~0))
700 # define widen1(x) ((vec_t)B(pmovsxdq, _mask, x, (vdi_t)undef(), ~0))
701 # elif UINT_SIZE == 4
702 # define max(x, y) ((vec_t)B(pmaxud, _mask, (vsi_t)(x), (vsi_t)(y), (vsi_t)undef(), ~0))
703 # define min(x, y) ((vec_t)B(pminud, _mask, (vsi_t)(x), (vsi_t)(y), (vsi_t)undef(), ~0))
704 # define mul_full(x, y) ((vec_t)B(pmuludq, _mask, (vsi_t)(x), (vsi_t)(y), (vdi_t)undef(), ~0))
705 # define widen1(x) ((vec_t)B(pmovzxdq, _mask, (vsi_half_t)(x), (vdi_t)undef(), ~0))
706 # elif INT_SIZE == 8
707 # define abs(x) ((vec_t)B(pabsq, _mask, (vdi_t)(x), (vdi_t)undef(), ~0))
708 # define max(x, y) ((vec_t)B(pmaxsq, _mask, (vdi_t)(x), (vdi_t)(y), (vdi_t)undef(), ~0))
709 # define min(x, y) ((vec_t)B(pminsq, _mask, (vdi_t)(x), (vdi_t)(y), (vdi_t)undef(), ~0))
710 # elif UINT_SIZE == 8
711 # define max(x, y) ((vec_t)B(pmaxuq, _mask, (vdi_t)(x), (vdi_t)(y), (vdi_t)undef(), ~0))
712 # define min(x, y) ((vec_t)B(pminuq, _mask, (vdi_t)(x), (vdi_t)(y), (vdi_t)undef(), ~0))
713 # endif
714 #elif (INT_SIZE == 1 || UINT_SIZE == 1 || INT_SIZE == 2 || UINT_SIZE == 2) && \
715 defined(__AVX512BW__) && (VEC_SIZE == 64 || defined(__AVX512VL__))
716 # if INT_SIZE == 1 || UINT_SIZE == 1
717 # define broadcast(x) ({ \
718 vec_t t_; \
719 asm ( "vpbroadcastb %1, %0" \
720 : "=v" (t_) : "m" (*(char[1]){ x }) ); \
721 t_; \
722 })
723 # define broadcast2(x) ({ \
724 vec_t t_; \
725 asm ( "vpbroadcastb %k1, %0" : "=v" (t_) : "r" (x) ); \
726 t_; \
727 })
728 # if VEC_SIZE == 16
729 # define interleave_hi(x, y) ((vec_t)B(punpckhbw, _mask, (vqi_t)(x), (vqi_t)(y), (vqi_t)undef(), ~0))
730 # define interleave_lo(x, y) ((vec_t)B(punpcklbw, _mask, (vqi_t)(x), (vqi_t)(y), (vqi_t)undef(), ~0))
731 # define rotr(x, n) ((vec_t)B(palignr, _mask, (vdi_t)(x), (vdi_t)(x), (n) * 8, (vdi_t)undef(), ~0))
732 # define swap(x) ((vec_t)B(pshufb, _mask, (vqi_t)(x), (vqi_t)(inv - 1), (vqi_t)undef(), ~0))
733 # elif defined(__AVX512VBMI__)
734 # define interleave_hi(x, y) ((vec_t)B(vpermi2varqi, _mask, (vqi_t)(x), interleave_hi, (vqi_t)(y), ~0))
735 # define interleave_lo(x, y) ((vec_t)B(vpermt2varqi, _mask, interleave_lo, (vqi_t)(x), (vqi_t)(y), ~0))
736 # endif
737 # define mix(x, y) ((vec_t)B(blendmb_, _mask, (vqi_t)(x), (vqi_t)(y), \
738 (0b1010101010101010101010101010101010101010101010101010101010101010LL & ALL_TRUE)))
739 # define shrink1(x) ((half_t)B(pmovwb, _mask, (vhi_t)(x), (vqi_half_t){}, ~0))
740 # define shrink2(x) ((quarter_t)B(pmovdb, _mask, (vsi_t)(x), (vqi_quarter_t){}, ~0))
741 # define shrink3(x) ((eighth_t)B(pmovqb, _mask, (vdi_t)(x), (vqi_eighth_t){}, ~0))
742 # ifdef __AVX512VBMI__
743 # define swap2(x) ((vec_t)B(permvarqi, _mask, (vqi_t)(x), (vqi_t)(inv - 1), (vqi_t)undef(), ~0))
744 # endif
745 # elif INT_SIZE == 2 || UINT_SIZE == 2
746 # define broadcast(x) ({ \
747 vec_t t_; \
748 asm ( "vpbroadcastw %1, %0" \
749 : "=v" (t_) : "m" (*(short[1]){ x }) ); \
750 t_; \
751 })
752 # define broadcast2(x) ({ \
753 vec_t t_; \
754 asm ( "vpbroadcastw %k1, %0" : "=v" (t_) : "r" (x) ); \
755 t_; \
756 })
757 # if VEC_SIZE == 16
758 # define interleave_hi(x, y) ((vec_t)B(punpckhwd, _mask, (vhi_t)(x), (vhi_t)(y), (vhi_t)undef(), ~0))
759 # define interleave_lo(x, y) ((vec_t)B(punpcklwd, _mask, (vhi_t)(x), (vhi_t)(y), (vhi_t)undef(), ~0))
760 # define rotr(x, n) ((vec_t)B(palignr, _mask, (vdi_t)(x), (vdi_t)(x), (n) * 16, (vdi_t)undef(), ~0))
761 # define swap(x) ((vec_t)B(pshufd, _mask, \
762 (vsi_t)B(pshufhw, _mask, \
763 B(pshuflw, _mask, (vhi_t)(x), 0b00011011, (vhi_t)undef(), ~0), \
764 0b00011011, (vhi_t)undef(), ~0), \
765 0b01001110, (vsi_t)undef(), ~0))
766 # else
767 # define interleave_hi(x, y) ((vec_t)B(vpermi2varhi, _mask, (vhi_t)(x), interleave_hi, (vhi_t)(y), ~0))
768 # define interleave_lo(x, y) ((vec_t)B(vpermt2varhi, _mask, interleave_lo, (vhi_t)(x), (vhi_t)(y), ~0))
769 # endif
770 # define mix(x, y) ((vec_t)B(blendmw_, _mask, (vhi_t)(x), (vhi_t)(y), \
771 (0b10101010101010101010101010101010 & ALL_TRUE)))
772 # define shrink1(x) ((half_t)B(pmovdw, _mask, (vsi_t)(x), (vhi_half_t){}, ~0))
773 # define shrink2(x) ((quarter_t)B(pmovqw, _mask, (vdi_t)(x), (vhi_quarter_t){}, ~0))
774 # define swap2(x) ((vec_t)B(permvarhi, _mask, (vhi_t)(x), (vhi_t)(inv - 1), (vhi_t)undef(), ~0))
775 # endif
776 # if INT_SIZE == 1
777 # define abs(x) ((vec_t)B(pabsb, _mask, (vqi_t)(x), (vqi_t)undef(), ~0))
778 # define max(x, y) ((vec_t)B(pmaxsb, _mask, (vqi_t)(x), (vqi_t)(y), (vqi_t)undef(), ~0))
779 # define min(x, y) ((vec_t)B(pminsb, _mask, (vqi_t)(x), (vqi_t)(y), (vqi_t)undef(), ~0))
780 # define widen1(x) ((vec_t)B(pmovsxbw, _mask, (vqi_half_t)(x), (vhi_t)undef(), ~0))
781 # define widen2(x) ((vec_t)B(pmovsxbd, _mask, (vqi_quarter_t)(x), (vsi_t)undef(), ~0))
782 # define widen3(x) ((vec_t)B(pmovsxbq, _mask, (vqi_eighth_t)(x), (vdi_t)undef(), ~0))
783 # elif UINT_SIZE == 1
784 # define max(x, y) ((vec_t)B(pmaxub, _mask, (vqi_t)(x), (vqi_t)(y), (vqi_t)undef(), ~0))
785 # define min(x, y) ((vec_t)B(pminub, _mask, (vqi_t)(x), (vqi_t)(y), (vqi_t)undef(), ~0))
786 # define widen1(x) ((vec_t)B(pmovzxbw, _mask, (vqi_half_t)(x), (vhi_t)undef(), ~0))
787 # define widen2(x) ((vec_t)B(pmovzxbd, _mask, (vqi_quarter_t)(x), (vsi_t)undef(), ~0))
788 # define widen3(x) ((vec_t)B(pmovzxbq, _mask, (vqi_eighth_t)(x), (vdi_t)undef(), ~0))
789 # elif INT_SIZE == 2
790 # define abs(x) B(pabsw, _mask, x, undef(), ~0)
791 # define max(x, y) B(pmaxsw, _mask, x, y, undef(), ~0)
792 # define min(x, y) B(pminsw, _mask, x, y, undef(), ~0)
793 # define mul_hi(x, y) B(pmulhw, _mask, x, y, undef(), ~0)
794 # define widen1(x) ((vec_t)B(pmovsxwd, _mask, x, (vsi_t)undef(), ~0))
795 # define widen2(x) ((vec_t)B(pmovsxwq, _mask, x, (vdi_t)undef(), ~0))
796 # elif UINT_SIZE == 2
797 # define max(x, y) ((vec_t)B(pmaxuw, _mask, (vhi_t)(x), (vhi_t)(y), (vhi_t)undef(), ~0))
798 # define min(x, y) ((vec_t)B(pminuw, _mask, (vhi_t)(x), (vhi_t)(y), (vhi_t)undef(), ~0))
799 # define mul_hi(x, y) ((vec_t)B(pmulhuw, _mask, (vhi_t)(x), (vhi_t)(y), (vhi_t)undef(), ~0))
800 # define widen1(x) ((vec_t)B(pmovzxwd, _mask, (vhi_half_t)(x), (vsi_t)undef(), ~0))
801 # define widen2(x) ((vec_t)B(pmovzxwq, _mask, (vhi_quarter_t)(x), (vdi_t)undef(), ~0))
802 # endif
803 #elif VEC_SIZE == 16 && defined(__SSE2__)
804 # if INT_SIZE == 1 || UINT_SIZE == 1
805 # define interleave_hi(x, y) ((vec_t)__builtin_ia32_punpckhbw128((vqi_t)(x), (vqi_t)(y)))
806 # define interleave_lo(x, y) ((vec_t)__builtin_ia32_punpcklbw128((vqi_t)(x), (vqi_t)(y)))
807 # elif INT_SIZE == 2 || UINT_SIZE == 2
808 # define interleave_hi(x, y) ((vec_t)__builtin_ia32_punpckhwd128((vhi_t)(x), (vhi_t)(y)))
809 # define interleave_lo(x, y) ((vec_t)__builtin_ia32_punpcklwd128((vhi_t)(x), (vhi_t)(y)))
810 # define swap(x) ((vec_t)__builtin_ia32_pshufd( \
811 (vsi_t)__builtin_ia32_pshufhw( \
812 __builtin_ia32_pshuflw((vhi_t)(x), 0b00011011), 0b00011011), 0b01001110))
813 # elif INT_SIZE == 4 || UINT_SIZE == 4
814 # define interleave_hi(x, y) ((vec_t)__builtin_ia32_punpckhdq128((vsi_t)(x), (vsi_t)(y)))
815 # define interleave_lo(x, y) ((vec_t)__builtin_ia32_punpckldq128((vsi_t)(x), (vsi_t)(y)))
816 # define swap(x) ((vec_t)__builtin_ia32_pshufd((vsi_t)(x), 0b00011011))
817 # elif INT_SIZE == 8 || UINT_SIZE == 8
818 # define interleave_hi(x, y) ((vec_t)__builtin_ia32_punpckhqdq128((vdi_t)(x), (vdi_t)(y)))
819 # define interleave_lo(x, y) ((vec_t)__builtin_ia32_punpcklqdq128((vdi_t)(x), (vdi_t)(y)))
820 # define swap(x) ((vec_t)__builtin_ia32_pshufd((vsi_t)(x), 0b01001110))
821 # endif
822 # if UINT_SIZE == 1
823 # define max(x, y) ((vec_t)__builtin_ia32_pmaxub128((vqi_t)(x), (vqi_t)(y)))
824 # define min(x, y) ((vec_t)__builtin_ia32_pminub128((vqi_t)(x), (vqi_t)(y)))
825 # elif INT_SIZE == 2
826 # define max(x, y) __builtin_ia32_pmaxsw128(x, y)
827 # define min(x, y) __builtin_ia32_pminsw128(x, y)
828 # define mul_hi(x, y) __builtin_ia32_pmulhw128(x, y)
829 # elif UINT_SIZE == 2
830 # define mul_hi(x, y) ((vec_t)__builtin_ia32_pmulhuw128((vhi_t)(x), (vhi_t)(y)))
831 # elif UINT_SIZE == 4
832 # define mul_full(x, y) ((vec_t)__builtin_ia32_pmuludq128((vsi_t)(x), (vsi_t)(y)))
833 # endif
834 # define select(d, x, y, m) ({ \
835 void *d_ = (d); \
836 vqi_t m_ = (vqi_t)(m); \
837 __builtin_ia32_maskmovdqu((vqi_t)(x), m_, d_); \
838 __builtin_ia32_maskmovdqu((vqi_t)(y), ~m_, d_); \
839 })
840 #elif VEC_SIZE == 32 && defined(__AVX2__)
841 # define swap_lanes(x, y, func, type) ({ \
842 long long __attribute__((vector_size(16))) t_ = __builtin_ia32_extract128i256((vdi_t)(y), 0); \
843 type t1_ = (type)__builtin_ia32_insert128i256((vdi_t)(x), t_, 1), t2_; \
844 t_ = __builtin_ia32_extract128i256((vdi_t)(x), 1); \
845 t2_ = (type)__builtin_ia32_insert128i256((vdi_t)(y), t_, 0); \
846 func(t1_, t2_); \
847 })
848 # if INT_SIZE == 1 || UINT_SIZE == 1
849 # define broadcast(x) ({ char s_ = (x); vec_t d_; asm ( "vpbroadcastb %1,%0" : "=x" (d_) : "m" (s_)); d_; })
850 # define copysignz(x, y) ((vec_t)__builtin_ia32_psignb256((vqi_t)(x), (vqi_t)(y)))
851 # define rotr(x, n) ((vec_t)__builtin_ia32_palignr256(__builtin_ia32_permti256((vdi_t)(x), (vdi_t)(x), 0b00000001), \
852 (vdi_t)(x), (n) * 8))
853 # elif INT_SIZE == 2 || UINT_SIZE == 2
854 # define broadcast(x) ({ short s_ = (x); vec_t d_; asm ( "vpbroadcastw %1,%0" : "=x" (d_) : "m" (s_)); d_; })
855 # define copysignz(x, y) ((vec_t)__builtin_ia32_psignw256((vhi_t)(x), (vhi_t)(y)))
856 # define hadd(x, y) ((vec_t)swap_lanes(x, y, __builtin_ia32_phaddw256, vhi_t))
857 # define hsub(x, y) ((vec_t)swap_lanes(x, y, __builtin_ia32_phsubw256, vhi_t))
858 # define mix(x, y) ((vec_t)__builtin_ia32_pblendw256((vhi_t)(x), (vhi_t)(y), 0b10101010))
859 # define rotr(x, n) ((vec_t)__builtin_ia32_palignr256(__builtin_ia32_permti256((vdi_t)(x), (vdi_t)(x), 0b00000001), \
860 (vdi_t)(x), (n) * 16))
861 # elif INT_SIZE == 4 || UINT_SIZE == 4
862 # define broadcast(x) ({ int s_ = (x); vec_t d_; asm ( "vpbroadcastd %1,%0" : "=x" (d_) : "m" (s_)); d_; })
863 # define copysignz(x, y) ((vec_t)__builtin_ia32_psignd256((vsi_t)(x), (vsi_t)(y)))
864 # define hadd(x, y) ((vec_t)swap_lanes(x, y, __builtin_ia32_phaddd256, vsi_t))
865 # define hsub(x, y) ((vec_t)swap_lanes(x, y, __builtin_ia32_phsubd256, vsi_t))
866 # define mix(x, y) ((vec_t)__builtin_ia32_pblendd256((vsi_t)(x), (vsi_t)(y), 0b10101010))
867 # define rotr(x, n) ((vec_t)__builtin_ia32_palignr256(__builtin_ia32_permti256((vdi_t)(x), (vdi_t)(x), 0b00000001), \
868 (vdi_t)(x), (n) * 32))
869 # define select(d, x, y, m) ({ \
870 vsi_t m_ = (vsi_t)(m); \
871 *(d) = (vec_t)__builtin_ia32_maskloadd256((vsi_t *)&(x), m_); \
872 __builtin_ia32_maskstored256((vsi_t *)(d), ~m_, (vsi_t)(y)); \
873 })
874 # define swap(x) ((vec_t)__builtin_ia32_permvarsi256((vsi_t)(x), (vsi_t)inv - 1))
875 # elif INT_SIZE == 8 || UINT_SIZE == 8
876 # define mix(x, y) ((vec_t)__builtin_ia32_pblendd256((vsi_t)(x), (vsi_t)(y), 0b11001100))
877 # define rotr(x, n) ((vec_t)__builtin_ia32_palignr256(__builtin_ia32_permti256((vdi_t)(x), (vdi_t)(x), 0b00000001), \
878 (vdi_t)(x), (n) * 64))
879 # define select(d, x, y, m) ({ \
880 vdi_t m_ = (vdi_t)(m); \
881 *(d) = (vec_t)__builtin_ia32_maskloadq256((vdi_t *)&(x), m_); \
882 __builtin_ia32_maskstoreq256((vdi_t *)(d), ~m_, (vdi_t)(y)); \
883 })
884 # define swap(x) ((vec_t)__builtin_ia32_permdi256((vdi_t)(x), 0b00011011))
885 # define swap2(x) ({ \
886 vdi_t t_ = __builtin_ia32_permdi256((vdi_t)(x), 0b10110001); \
887 (vec_t)__builtin_ia32_permti256(t_, t_, 0b00000001); \
888 })
889 # endif
890 # if INT_SIZE == 1
891 # define abs(x) ((vec_t)__builtin_ia32_pabsb256((vqi_t)(x)))
892 # define max(x, y) ((vec_t)__builtin_ia32_pmaxsb256((vqi_t)(x), (vqi_t)(y)))
893 # define min(x, y) ((vec_t)__builtin_ia32_pminsb256((vqi_t)(x), (vqi_t)(y)))
894 # define widen1(x) ((vec_t)__builtin_ia32_pmovsxbw256((vqi_t)(x)))
895 # define widen2(x) ((vec_t)__builtin_ia32_pmovsxbd256((vqi_t)(x)))
896 # define widen3(x) ((vec_t)__builtin_ia32_pmovsxbq256((vqi_t)(x)))
897 # elif UINT_SIZE == 1
898 # define max(x, y) ((vec_t)__builtin_ia32_pmaxub256((vqi_t)(x), (vqi_t)(y)))
899 # define min(x, y) ((vec_t)__builtin_ia32_pminub256((vqi_t)(x), (vqi_t)(y)))
900 # define widen1(x) ((vec_t)__builtin_ia32_pmovzxbw256((vqi_t)(x)))
901 # define widen2(x) ((vec_t)__builtin_ia32_pmovzxbd256((vqi_t)(x)))
902 # define widen3(x) ((vec_t)__builtin_ia32_pmovzxbq256((vqi_t)(x)))
903 # elif INT_SIZE == 2
904 # define abs(x) __builtin_ia32_pabsw256(x)
905 # define max(x, y) __builtin_ia32_pmaxsw256(x, y)
906 # define min(x, y) __builtin_ia32_pminsw256(x, y)
907 # define mul_hi(x, y) __builtin_ia32_pmulhw256(x, y)
908 # define widen1(x) ((vec_t)__builtin_ia32_pmovsxwd256(x))
909 # define widen2(x) ((vec_t)__builtin_ia32_pmovsxwq256(x))
910 # elif UINT_SIZE == 2
911 # define max(x, y) ((vec_t)__builtin_ia32_pmaxuw256((vhi_t)(x), (vhi_t)(y)))
912 # define min(x, y) ((vec_t)__builtin_ia32_pminuw256((vhi_t)(x), (vhi_t)(y)))
913 # define mul_hi(x, y) ((vec_t)__builtin_ia32_pmulhuw256((vhi_t)(x), (vhi_t)(y)))
914 # define widen1(x) ((vec_t)__builtin_ia32_pmovzxwd256((vhi_t)(x)))
915 # define widen2(x) ((vec_t)__builtin_ia32_pmovzxwq256((vhi_t)(x)))
916 # elif INT_SIZE == 4
917 # define abs(x) __builtin_ia32_pabsd256(x)
918 # define max(x, y) __builtin_ia32_pmaxsd256(x, y)
919 # define min(x, y) __builtin_ia32_pminsd256(x, y)
920 # define widen1(x) ((vec_t)__builtin_ia32_pmovsxdq256(x))
921 # elif UINT_SIZE == 4
922 # define max(x, y) ((vec_t)__builtin_ia32_pmaxud256((vsi_t)(x), (vsi_t)(y)))
923 # define min(x, y) ((vec_t)__builtin_ia32_pminud256((vsi_t)(x), (vsi_t)(y)))
924 # define mul_full(x, y) ((vec_t)__builtin_ia32_pmuludq256((vsi_t)(x), (vsi_t)(y)))
925 # define widen1(x) ((vec_t)__builtin_ia32_pmovzxdq256((vsi_t)(x)))
926 # elif INT_SIZE == 8
927 # define broadcast(x) ({ \
928 long long s_ = (x); \
929 long long __attribute__((vector_size(16))) t_; \
930 vec_t d_; \
931 asm ( "vpbroadcastq %1,%0" : "=x" (t_) : "m" (s_)); \
932 asm ( "vbroadcasti128 %1,%0" : "=x" (d_) : "m" (t_)); \
933 d_; \
934 })
935 # elif UINT_SIZE == 8
936 # define broadcast(x) ({ long long s_ = (x); vec_t d_; asm ( "vpbroadcastq %1,%0" : "=x" (d_) : "m" (s_)); d_; })
937 # endif
938 #endif
939 #if VEC_SIZE == 16 && defined(__SSE3__)
940 # if FLOAT_SIZE == 4
941 # define addsub(x, y) __builtin_ia32_addsubps(x, y)
942 # define dup_hi(x) __builtin_ia32_movshdup(x)
943 # define dup_lo(x) __builtin_ia32_movsldup(x)
944 # define hadd(x, y) __builtin_ia32_haddps(x, y)
945 # define hsub(x, y) __builtin_ia32_hsubps(x, y)
946 # elif FLOAT_SIZE == 8
947 # define addsub(x, y) __builtin_ia32_addsubpd(x, y)
948 # define dup_lo(x) ({ \
949 double __attribute__((vector_size(16))) r_; \
950 asm ( "movddup %1,%0" : "=x" (r_) : "m" ((x)[0]) ); \
951 r_; \
952 })
953 # define hadd(x, y) __builtin_ia32_haddpd(x, y)
954 # define hsub(x, y) __builtin_ia32_hsubpd(x, y)
955 # endif
956 #elif VEC_SIZE == 32 && defined(__AVX__)
957 # if FLOAT_SIZE == 4
958 # define addsub(x, y) __builtin_ia32_addsubps256(x, y)
959 # define dup_hi(x) __builtin_ia32_movshdup256(x)
960 # define dup_lo(x) __builtin_ia32_movsldup256(x)
961 # ifdef __AVX2__
962 # define hadd(x, y) __builtin_ia32_permvarsf256(__builtin_ia32_haddps256(x, y), \
963 (vsi_t){0, 1, 4, 5, 2, 3, 6, 7})
964 # define hsub(x, y) __builtin_ia32_permvarsf256(__builtin_ia32_hsubps256(x, y), \
965 (vsi_t){0, 1, 4, 5, 2, 3, 6, 7})
966 # else
967 # define hadd(x, y) ({ \
968 vec_t t_ = __builtin_ia32_haddps256(x, y); \
969 (vec_t){t_[0], t_[1], t_[4], t_[5], t_[2], t_[3], t_[6], t_[7]}; \
970 })
971 # define hsub(x, y) ({ \
972 vec_t t_ = __builtin_ia32_hsubps256(x, y); \
973 (vec_t){t_[0], t_[1], t_[4], t_[5], t_[2], t_[3], t_[6], t_[7]}; \
974 })
975 # endif
976 # elif FLOAT_SIZE == 8
977 # define addsub(x, y) __builtin_ia32_addsubpd256(x, y)
978 # define dup_lo(x) __builtin_ia32_movddup256(x)
979 # ifdef __AVX2__
980 # define hadd(x, y) __builtin_ia32_permdf256(__builtin_ia32_haddpd256(x, y), 0b11011000)
981 # define hsub(x, y) __builtin_ia32_permdf256(__builtin_ia32_hsubpd256(x, y), 0b11011000)
982 # else
983 # define hadd(x, y) ({ \
984 vec_t t_ = __builtin_ia32_haddpd256(x, y); \
985 (vec_t){t_[0], t_[2], t_[1], t_[3]}; \
986 })
987 # define hsub(x, y) ({ \
988 vec_t t_ = __builtin_ia32_hsubpd256(x, y); \
989 (vec_t){t_[0], t_[2], t_[1], t_[3]}; \
990 })
991 # endif
992 # endif
993 #elif VEC_SIZE == 64
994 # if FLOAT_SIZE == 4
995 # define dup_hi(x) B(movshdup, _mask, x, undef(), ~0)
996 # define dup_lo(x) B(movsldup, _mask, x, undef(), ~0)
997 # elif FLOAT_SIZE == 8
998 # define dup_lo(x) B(movddup, _mask, x, undef(), ~0)
999 # endif
1000 #endif
1001 #if FLOAT_SIZE == 2 && ELEM_COUNT > 1
1002 # define dup_hi(x) ((vec_t)B(pshufhw, _mask, \
1003 B(pshuflw, _mask, (vhi_t)(x), 0b11110101, \
1004 (vhi_t)undef(), ~0), \
1005 0b11110101, (vhi_t)undef(), ~0))
1006 # define dup_lo(x) ((vec_t)B(pshufhw, _mask, \
1007 B(pshuflw, _mask, (vhi_t)(x), 0b10100000, \
1008 (vhi_t)undef(), ~0), \
1009 0b10100000, (vhi_t)undef(), ~0))
1010 #endif
1011 #if VEC_SIZE == 16 && defined(__SSSE3__) && !defined(__AVX512VL__)
1012 # if INT_SIZE == 1
1013 # define abs(x) ((vec_t)__builtin_ia32_pabsb128((vqi_t)(x)))
1014 # elif INT_SIZE == 2
1015 # define abs(x) __builtin_ia32_pabsw128(x)
1016 # elif INT_SIZE == 4
1017 # define abs(x) __builtin_ia32_pabsd128(x)
1018 # endif
1019 # if INT_SIZE == 1 || UINT_SIZE == 1
1020 # define copysignz(x, y) ((vec_t)__builtin_ia32_psignb128((vqi_t)(x), (vqi_t)(y)))
1021 # define swap(x) ((vec_t)__builtin_ia32_pshufb128((vqi_t)(x), (vqi_t)(inv - 1)))
1022 # define rotr(x, n) ((vec_t)__builtin_ia32_palignr128((vdi_t)(x), (vdi_t)(x), (n) * 8))
1023 # elif INT_SIZE == 2 || UINT_SIZE == 2
1024 # define copysignz(x, y) ((vec_t)__builtin_ia32_psignw128((vhi_t)(x), (vhi_t)(y)))
1025 # define hadd(x, y) ((vec_t)__builtin_ia32_phaddw128((vhi_t)(x), (vhi_t)(y)))
1026 # define hsub(x, y) ((vec_t)__builtin_ia32_phsubw128((vhi_t)(x), (vhi_t)(y)))
1027 # define rotr(x, n) ((vec_t)__builtin_ia32_palignr128((vdi_t)(x), (vdi_t)(x), (n) * 16))
1028 # elif INT_SIZE == 4 || UINT_SIZE == 4
1029 # define copysignz(x, y) ((vec_t)__builtin_ia32_psignd128((vsi_t)(x), (vsi_t)(y)))
1030 # define hadd(x, y) ((vec_t)__builtin_ia32_phaddd128((vsi_t)(x), (vsi_t)(y)))
1031 # define hsub(x, y) ((vec_t)__builtin_ia32_phsubd128((vsi_t)(x), (vsi_t)(y)))
1032 # define rotr(x, n) ((vec_t)__builtin_ia32_palignr128((vdi_t)(x), (vdi_t)(x), (n) * 32))
1033 # elif INT_SIZE == 8 || UINT_SIZE == 8
1034 # define rotr(x, n) ((vec_t)__builtin_ia32_palignr128((vdi_t)(x), (vdi_t)(x), (n) * 64))
1035 # endif
1036 #endif
1037 #if VEC_SIZE == 16 && defined(__SSE4_1__) && !defined(__AVX512VL__)
1038 # if INT_SIZE == 1
1039 # define max(x, y) ((vec_t)__builtin_ia32_pmaxsb128((vqi_t)(x), (vqi_t)(y)))
1040 # define min(x, y) ((vec_t)__builtin_ia32_pminsb128((vqi_t)(x), (vqi_t)(y)))
1041 # define widen1(x) ((vec_t)__builtin_ia32_pmovsxbw128((vqi_t)(x)))
1042 # define widen2(x) ((vec_t)__builtin_ia32_pmovsxbd128((vqi_t)(x)))
1043 # define widen3(x) ((vec_t)__builtin_ia32_pmovsxbq128((vqi_t)(x)))
1044 # elif INT_SIZE == 2
1045 # define widen1(x) ((vec_t)__builtin_ia32_pmovsxwd128(x))
1046 # define widen2(x) ((vec_t)__builtin_ia32_pmovsxwq128(x))
1047 # elif INT_SIZE == 4
1048 # define max(x, y) __builtin_ia32_pmaxsd128(x, y)
1049 # define min(x, y) __builtin_ia32_pminsd128(x, y)
1050 # define mul_full(x, y) ((vec_t)__builtin_ia32_pmuldq128(x, y))
1051 # define widen1(x) ((vec_t)__builtin_ia32_pmovsxdq128(x))
1052 # elif UINT_SIZE == 1
1053 # define widen1(x) ((vec_t)__builtin_ia32_pmovzxbw128((vqi_t)(x)))
1054 # define widen2(x) ((vec_t)__builtin_ia32_pmovzxbd128((vqi_t)(x)))
1055 # define widen3(x) ((vec_t)__builtin_ia32_pmovzxbq128((vqi_t)(x)))
1056 # elif UINT_SIZE == 2
1057 # define max(x, y) ((vec_t)__builtin_ia32_pmaxuw128((vhi_t)(x), (vhi_t)(y)))
1058 # define min(x, y) ((vec_t)__builtin_ia32_pminuw128((vhi_t)(x), (vhi_t)(y)))
1059 # define widen1(x) ((vec_t)__builtin_ia32_pmovzxwd128((vhi_t)(x)))
1060 # define widen2(x) ((vec_t)__builtin_ia32_pmovzxwq128((vhi_t)(x)))
1061 # elif UINT_SIZE == 4
1062 # define max(x, y) ((vec_t)__builtin_ia32_pmaxud128((vsi_t)(x), (vsi_t)(y)))
1063 # define min(x, y) ((vec_t)__builtin_ia32_pminud128((vsi_t)(x), (vsi_t)(y)))
1064 # define widen1(x) ((vec_t)__builtin_ia32_pmovzxdq128((vsi_t)(x)))
1065 # endif
1066 # undef select
1067 # if defined(INT_SIZE) || defined(UINT_SIZE)
1068 # define select(d, x, y, m) \
1069 (*(d) = (vec_t)__builtin_ia32_pblendvb128((vqi_t)(y), (vqi_t)(x), (vqi_t)(m)))
1070 # elif FLOAT_SIZE == 4
1071 # define dot_product(x, y) __builtin_ia32_dpps(x, y, 0b11110001)
1072 # define select(d, x, y, m) (*(d) = __builtin_ia32_blendvps(y, x, m))
1073 # define trunc(x) __builtin_ia32_roundps(x, 0b1011)
1074 # elif FLOAT_SIZE == 8
1075 # define dot_product(x, y) __builtin_ia32_dppd(x, y, 0b00110001)
1076 # define select(d, x, y, m) (*(d) = __builtin_ia32_blendvpd(y, x, m))
1077 # define trunc(x) __builtin_ia32_roundpd(x, 0b1011)
1078 # endif
1079 # if INT_SIZE == 2 || UINT_SIZE == 2
1080 # define mix(x, y) ((vec_t)__builtin_ia32_pblendw128((vhi_t)(x), (vhi_t)(y), 0b10101010))
1081 # elif INT_SIZE == 4 || UINT_SIZE == 4
1082 # define mix(x, y) ((vec_t)__builtin_ia32_pblendw128((vhi_t)(x), (vhi_t)(y), 0b11001100))
1083 # elif INT_SIZE == 8 || UINT_SIZE == 8
1084 # define mix(x, y) ((vec_t)__builtin_ia32_pblendw128((vhi_t)(x), (vhi_t)(y), 0b11110000))
1085 # elif FLOAT_SIZE == 4
1086 # define mix(x, y) __builtin_ia32_blendps(x, y, 0b1010)
1087 # elif FLOAT_SIZE == 8
1088 # define mix(x, y) __builtin_ia32_blendpd(x, y, 0b10)
1089 # endif
1090 #endif
1091 #if VEC_SIZE == 32 && defined(__AVX__) && !defined(__AVX512VL__)
1092 # if FLOAT_SIZE == 4
1093 # define dot_product(x, y) ({ \
1094 vec_t t_ = __builtin_ia32_dpps256(x, y, 0b11110001); \
1095 (vec_t){t_[0] + t_[4]}; \
1096 })
1097 # define mix(x, y) __builtin_ia32_blendps256(x, y, 0b10101010)
1098 # define select(d, x, y, m) (*(d) = __builtin_ia32_blendvps256(y, x, m))
1099 # define select2(d, x, y, m) ({ \
1100 vsi_t m_ = (vsi_t)(m); \
1101 *(d) = __builtin_ia32_maskloadps256(&(x), m_); \
1102 __builtin_ia32_maskstoreps256(d, ~m_, y); \
1103 })
1104 # define trunc(x) __builtin_ia32_roundps256(x, 0b1011)
1105 # elif FLOAT_SIZE == 8
1106 # define mix(x, y) __builtin_ia32_blendpd256(x, y, 0b1010)
1107 # define select(d, x, y, m) (*(d) = __builtin_ia32_blendvpd256(y, x, m))
1108 # define select2(d, x, y, m) ({ \
1109 vdi_t m_ = (vdi_t)(m); \
1110 *(d) = __builtin_ia32_maskloadpd256(&(x), m_); \
1111 __builtin_ia32_maskstorepd256(d, ~m_, y); \
1112 })
1113 # define trunc(x) __builtin_ia32_roundpd256(x, 0b1011)
1114 # endif
1115 #endif
1116 #if VEC_SIZE == FLOAT_SIZE
1117 # define max(x, y) ((vec_t){({ typeof(x[0]) x_ = (x)[0], y_ = (y)[0]; x_ > y_ ? x_ : y_; })})
1118 # define min(x, y) ((vec_t){({ typeof(x[0]) x_ = (x)[0], y_ = (y)[0]; x_ < y_ ? x_ : y_; })})
1119 # if defined(__SSE4_1__) && !defined(__AVX512F__)
1120 # if FLOAT_SIZE == 4
1121 # define trunc(x) scalar_1op(x, "roundss $0b1011, %[in], %[out]")
1122 # elif FLOAT_SIZE == 8
1123 # define trunc(x) scalar_1op(x, "roundsd $0b1011, %[in], %[out]")
1124 # endif
1125 # endif
1126 #endif
1127 #ifdef __XOP__
1128 # undef select
1129 # if VEC_SIZE == 16
1130 # if INT_SIZE == 2 || INT_SIZE == 4
1131 # include "simd-fma.c"
1132 # endif
1133 # define select(d, x, y, m) \
1134 (*(d) = (vec_t)__builtin_ia32_vpcmov((vdi_t)(x), (vdi_t)(y), (vdi_t)(m)))
1135 # if INT_SIZE == 1 || UINT_SIZE == 1
1136 # define swap2(x) ((vec_t)__builtin_ia32_vpperm((vqi_t)(x), (vqi_t)(x), (vqi_t)inv - 1))
1137 # elif INT_SIZE == 2 || UINT_SIZE == 2
1138 # define swap2(x) \
1139 ((vec_t)__builtin_ia32_vpperm((vqi_t)(x), (vqi_t)(x), \
1140 (vqi_t)(__builtin_ia32_vprotwi(2 * (vhi_t)inv - 1, 8) | \
1141 (2 * inv - 2))))
1142 # elif FLOAT_SIZE == 4
1143 # define frac(x) __builtin_ia32_vfrczps(x)
1144 # undef swap2
1145 # define swap2(x) ({ \
1146 /* Buggy in gcc 7.1.0 and earlier. */ \
1147 /* __builtin_ia32_vpermil2ps((vec_t){}, x, __builtin_ia32_cvtps2dq(inv) + 3, 0) */ \
1148 vec_t t_; \
1149 asm ( "vpermil2ps $0, %3, %2, %1, %0" : \
1150 "=x" (t_) : \
1151 "x" ((vec_t){}), "m" (x), "x" (__builtin_ia32_cvtps2dq(inv) + 3) ); \
1152 t_; \
1153 })
1154 # elif FLOAT_SIZE == 8
1155 # define frac(x) __builtin_ia32_vfrczpd(x)
1156 # undef swap2
1157 # define swap2(x) ({ \
1158 /* Buggy in gcc 7.1.0 and earlier. */ \
1159 /* __builtin_ia32_vpermil2pd((vec_t){}, x, */ \
1160 /* __builtin_ia32_pmovsxdq128( */ \
1161 /* __builtin_ia32_cvtpd2dq(inv) + 1) << 1, 0) */ \
1162 vdi_t s_ = __builtin_ia32_pmovsxdq128( \
1163 __builtin_ia32_cvtpd2dq(inv) + 1) << 1; \
1164 vec_t t_; \
1165 asm ( "vpermil2pd $0, %3, %2, %1, %0" : \
1166 "=x" (t_) : "x" ((vec_t){}), "x" (x), "m" (s_) ); \
1167 t_; \
1168 })
1169 # endif
1170 # if INT_SIZE == 1
1171 # define hadd(x, y) ((vec_t)__builtin_ia32_packsswb128(__builtin_ia32_vphaddbw((vqi_t)(x)), \
1172 __builtin_ia32_vphaddbw((vqi_t)(y))))
1173 # define hsub(x, y) ((vec_t)__builtin_ia32_packsswb128(__builtin_ia32_vphsubbw((vqi_t)(x)), \
1174 __builtin_ia32_vphsubbw((vqi_t)(y))))
1175 # elif UINT_SIZE == 1
1176 # define hadd(x, y) ((vec_t)__builtin_ia32_packuswb128(__builtin_ia32_vphaddubw((vqi_t)(x)), \
1177 __builtin_ia32_vphaddubw((vqi_t)(y))))
1178 # elif INT_SIZE == 2
1179 # undef hadd
1180 # define hadd(x, y) __builtin_ia32_packssdw128(__builtin_ia32_vphaddwd(x), \
1181 __builtin_ia32_vphaddwd(y))
1182 # undef hsub
1183 # define hsub(x, y) __builtin_ia32_packssdw128(__builtin_ia32_vphsubwd(x), \
1184 __builtin_ia32_vphsubwd(y))
1185 # elif UINT_SIZE == 2
1186 # undef hadd
1187 # define hadd(x, y) ((vec_t)__builtin_ia32_packusdw128(__builtin_ia32_vphadduwd((vhi_t)(x)), \
1188 __builtin_ia32_vphadduwd((vhi_t)(y))))
1189 # undef hsub
1190 # endif
1191 # elif VEC_SIZE == 32
1192 # define select(d, x, y, m) \
1193 (*(d) = (vec_t)__builtin_ia32_vpcmov256((vdi_t)(x), (vdi_t)(y), (vdi_t)(m)))
1194 # if FLOAT_SIZE == 4
1195 # define frac(x) __builtin_ia32_vfrczps256(x)
1196 # elif FLOAT_SIZE == 8
1197 # define frac(x) __builtin_ia32_vfrczpd256(x)
1198 # endif
1199 # elif VEC_SIZE == FLOAT_SIZE
1200 # if VEC_SIZE == 4
1201 # define frac(x) scalar_1op(x, "vfrczss %[in], %[out]")
1202 # elif VEC_SIZE == 8
1203 # define frac(x) scalar_1op(x, "vfrczsd %[in], %[out]")
1204 # endif
1205 # endif
1206 #endif
1207
1208 #if VEC_SIZE >= 16
1209
1210 # if !defined(low_half) && defined(HALF_SIZE)
1211 static inline half_t low_half(vec_t x)
1212 {
1213 # if HALF_SIZE < VEC_SIZE
1214 half_t y;
1215 unsigned int i;
1216
1217 for ( i = 0; i < ELEM_COUNT / 2; ++i )
1218 y[i] = x[i];
1219
1220 return y;
1221 # else
1222 return x;
1223 # endif
1224 }
1225 # endif
1226
1227 # if !defined(low_quarter) && defined(QUARTER_SIZE)
1228 static inline quarter_t low_quarter(vec_t x)
1229 {
1230 # if QUARTER_SIZE < VEC_SIZE
1231 quarter_t y;
1232 unsigned int i;
1233
1234 for ( i = 0; i < ELEM_COUNT / 4; ++i )
1235 y[i] = x[i];
1236
1237 return y;
1238 # else
1239 return x;
1240 # endif
1241 }
1242 # endif
1243
1244 # if !defined(low_eighth) && defined(EIGHTH_SIZE)
1245 static inline eighth_t low_eighth(vec_t x)
1246 {
1247 # if EIGHTH_SIZE < VEC_SIZE
1248 eighth_t y;
1249 unsigned int i;
1250
1251 for ( i = 0; i < ELEM_COUNT / 8; ++i )
1252 y[i] = x[i];
1253
1254 return y;
1255 # else
1256 return x;
1257 # endif
1258 }
1259 # endif
1260
1261 #endif
1262
1263 #ifdef broadcast_pair
1264 # if ELEM_COUNT == 4
1265 # define broadcast_half broadcast_pair
1266 # elif ELEM_COUNT == 8
1267 # define broadcast_quarter broadcast_pair
1268 # elif ELEM_COUNT == 16
1269 # define broadcast_eighth broadcast_pair
1270 # endif
1271 #endif
1272
1273 #ifdef insert_pair
1274 # if ELEM_COUNT == 4
1275 # define insert_half insert_pair
1276 # elif ELEM_COUNT == 8
1277 # define insert_quarter insert_pair
1278 # elif ELEM_COUNT == 16
1279 # define insert_eighth insert_pair
1280 # endif
1281 #endif
1282
1283 #ifdef broadcast_quartet
1284 # if ELEM_COUNT == 8
1285 # define broadcast_half broadcast_quartet
1286 # elif ELEM_COUNT == 16
1287 # define broadcast_quarter broadcast_quartet
1288 # endif
1289 #endif
1290
1291 #ifdef insert_quartet
1292 # if ELEM_COUNT == 8
1293 # define insert_half insert_quartet
1294 # elif ELEM_COUNT == 16
1295 # define insert_quarter insert_quartet
1296 # endif
1297 #endif
1298
1299 #if defined(broadcast_octet) && ELEM_COUNT == 16
1300 # define broadcast_half broadcast_octet
1301 #endif
1302
1303 #if defined(insert_octet) && ELEM_COUNT == 16
1304 # define insert_half insert_octet
1305 #endif
1306
1307 #if defined(__AVX512F__) && defined(FLOAT_SIZE)
1308 # include "simd-fma.c"
1309 #endif
1310
1311 int simd_test(void)
1312 {
1313 unsigned int i, j;
1314 vec_t x, y, z, src, inv, alt, sh;
1315 vint_t interleave_lo, interleave_hi;
1316
1317 for ( i = 0, j = ELEM_SIZE << 3; i < ELEM_COUNT; ++i )
1318 {
1319 src[i] = i + 1;
1320 inv[i] = ELEM_COUNT - i;
1321 #ifdef UINT_SIZE
1322 alt[i] = -!(i & 1);
1323 #else
1324 alt[i] = i & 1 ? -1 : 1;
1325 #endif
1326 if ( !(i & (i + 1)) )
1327 --j;
1328 sh[i] = j;
1329
1330 interleave_lo[i] = ((i & 1) * ELEM_COUNT) | (i >> 1);
1331 interleave_hi[i] = interleave_lo[i] + (ELEM_COUNT / 2);
1332 }
1333
1334 touch(src);
1335 x = src;
1336 touch(x);
1337 if ( !eq(x, src) ) return __LINE__;
1338
1339 touch(src);
1340 y = x + src;
1341 touch(src);
1342 touch(y);
1343 if ( !eq(y, 2 * src) ) return __LINE__;
1344
1345 touch(src);
1346 z = y -= src;
1347 touch(z);
1348 if ( !eq(x, z) ) return __LINE__;
1349
1350 #if defined(UINT_SIZE)
1351
1352 touch(inv);
1353 x |= inv;
1354 touch(inv);
1355 y &= inv;
1356 touch(inv);
1357 z ^= inv;
1358 touch(inv);
1359 touch(x);
1360 if ( !eq(x & ~y, z) ) return __LINE__;
1361
1362 #elif ELEM_SIZE > 1 || VEC_SIZE <= 8
1363
1364 touch(src);
1365 x *= src;
1366 y = inv * inv;
1367 touch(src);
1368 z = src + inv;
1369 touch(inv);
1370 z *= (src - inv);
1371 if ( !eq(x - y, z) ) return __LINE__;
1372
1373 #endif
1374
1375 #if defined(FLOAT_SIZE)
1376
1377 x = src * alt;
1378 touch(alt);
1379 y = src / alt;
1380 if ( !eq(x, y) ) return __LINE__;
1381 touch(alt);
1382 touch(src);
1383 if ( !eq(x * -alt, -src) ) return __LINE__;
1384
1385 # ifdef to_int
1386
1387 touch(src);
1388 x = to_int(src);
1389 touch(src);
1390 if ( !eq(x, src) ) return __LINE__;
1391
1392 # ifdef recip
1393 touch(src);
1394 x = recip(src);
1395 touch(src);
1396 touch(x);
1397 if ( !eq(to_int(recip(x)), src) ) return __LINE__;
1398
1399 # ifdef rsqrt
1400 x = src * src;
1401 touch(x);
1402 y = rsqrt(x);
1403 touch(y);
1404 if ( !eq(to_int(recip(y)), src) ) return __LINE__;
1405 touch(src);
1406 if ( !eq(to_int(y), to_int(recip(src))) ) return __LINE__;
1407 # endif
1408 # endif
1409
1410 # endif
1411
1412 # ifdef to_wint
1413 touch(src);
1414 x = to_wint(src);
1415 touch(src);
1416 if ( !eq(x, src) ) return __LINE__;
1417 # endif
1418
1419 # ifdef to_uint
1420 touch(src);
1421 x = to_uint(src);
1422 touch(src);
1423 if ( !eq(x, src) ) return __LINE__;
1424 # endif
1425
1426 # ifdef to_uwint
1427 touch(src);
1428 x = to_uwint(src);
1429 touch(src);
1430 if ( !eq(x, src) ) return __LINE__;
1431 # endif
1432
1433 # ifdef sqrt
1434 x = src * src;
1435 touch(x);
1436 if ( !eq(sqrt(x), src) ) return __LINE__;
1437 # endif
1438
1439 # ifdef trunc
1440 x = 1 / src;
1441 y = (vec_t){ 1 };
1442 touch(x);
1443 z = trunc(x);
1444 if ( !eq(y, z) ) return __LINE__;
1445 # endif
1446
1447 # ifdef frac
1448 touch(src);
1449 x = frac(src);
1450 touch(src);
1451 if ( !eq(x, (vec_t){}) ) return __LINE__;
1452
1453 x = 1 / (src + 1);
1454 touch(x);
1455 y = frac(x);
1456 touch(x);
1457 if ( !eq(x, y) ) return __LINE__;
1458 # endif
1459
1460 # if defined(trunc) && defined(frac)
1461 x = src / 4;
1462 touch(x);
1463 y = trunc(x);
1464 touch(x);
1465 z = frac(x);
1466 touch(x);
1467 if ( !eq(x, y + z) ) return __LINE__;
1468 # endif
1469
1470 #else
1471
1472 # if ELEM_SIZE > 1
1473
1474 touch(inv);
1475 x = src * inv;
1476 touch(inv);
1477 y[ELEM_COUNT - 1] = y[0] = j = ELEM_COUNT;
1478 for ( i = 1; i < ELEM_COUNT / 2; ++i )
1479 y[ELEM_COUNT - i - 1] = y[i] = y[i - 1] + (j -= 2);
1480 if ( !eq(x, y) ) return __LINE__;
1481
1482 # ifdef mul_hi
1483 touch(alt);
1484 x = mul_hi(src, alt);
1485 touch(alt);
1486 # ifdef INT_SIZE
1487 if ( !eq(x, alt < 0) ) return __LINE__;
1488 # else
1489 if ( !eq(x, (src & alt) + alt) ) return __LINE__;
1490 # endif
1491 # endif
1492
1493 # ifdef mul_full
1494 x = src ^ alt;
1495 touch(inv);
1496 y = mul_full(x, inv);
1497 touch(inv);
1498 for ( i = 0; i < ELEM_COUNT; i += 2 )
1499 {
1500 unsigned long long res = x[i] * 1ULL * inv[i];
1501
1502 z[i] = res;
1503 z[i + 1] = res >> (ELEM_SIZE << 3);
1504 }
1505 if ( !eq(y, z) ) return __LINE__;
1506 # endif
1507
1508 z = src;
1509 # ifdef INT_SIZE
1510 z *= alt;
1511 # endif
1512 touch(z);
1513 x = z << 3;
1514 touch(z);
1515 y = z << 2;
1516 touch(z);
1517 if ( !eq(x, y + y) ) return __LINE__;
1518
1519 touch(x);
1520 z = x >> 2;
1521 touch(x);
1522 if ( !eq(y, z + z) ) return __LINE__;
1523
1524 z = src;
1525 # ifdef INT_SIZE
1526 z *= alt;
1527 # endif
1528 /*
1529 * Note that despite the touch()-es here there doesn't appear to be a way
1530 * to make the compiler use a memory operand for the shift instruction (at
1531 * least without resorting to built-ins).
1532 */
1533 j = 3;
1534 touch(j);
1535 x = z << j;
1536 touch(j);
1537 j = 2;
1538 touch(j);
1539 y = z << j;
1540 touch(j);
1541 if ( !eq(x, y + y) ) return __LINE__;
1542
1543 z = x >> j;
1544 touch(j);
1545 if ( !eq(y, z + z) ) return __LINE__;
1546
1547 # endif
1548
1549 # if ELEM_SIZE == 2 || defined(__SSE4_1__)
1550 /*
1551 * Even when there are no instructions with varying shift counts per
1552 * field, the code turns out to be a nice exercise for pextr/pinsr.
1553 */
1554 z = src;
1555 # ifdef INT_SIZE
1556 z *= alt;
1557 # endif
1558 /*
1559 * Zap elements for which the shift count is zero (and the hence the
1560 * decrement below would yield a negative count.
1561 */
1562 z &= (sh > 0);
1563 touch(sh);
1564 x = z << sh;
1565 touch(sh);
1566 --sh;
1567 touch(sh);
1568 y = z << sh;
1569 if ( !eq(x, y + y) ) return __LINE__;
1570
1571 # if (defined(__AVX2__) && ELEM_SIZE >= 4) || defined(__XOP__)
1572 touch(sh);
1573 x = y >> sh;
1574 if ( !eq(x, z) ) return __LINE__;
1575 # endif
1576
1577 # endif
1578
1579 #endif
1580
1581 #if defined(max) && defined(min)
1582 # ifdef UINT_SIZE
1583 touch(inv);
1584 x = min(src, inv);
1585 touch(inv);
1586 y = max(src, inv);
1587 touch(inv);
1588 if ( !eq(x + y, src + inv) ) return __LINE__;
1589 # else
1590 x = src * alt;
1591 y = inv * alt;
1592 touch(y);
1593 z = max(x, y);
1594 touch(y);
1595 y = min(x, y);
1596 touch(y);
1597 if ( !eq((y + z) * alt, src + inv) ) return __LINE__;
1598 # endif
1599 #endif
1600
1601 #ifdef abs
1602 x = src * alt;
1603 touch(x);
1604 if ( !eq(abs(x), src) ) return __LINE__;
1605 #endif
1606
1607 #ifdef copysignz
1608 touch(alt);
1609 if ( !eq(copysignz((vec_t){} + 1, alt), alt) ) return __LINE__;
1610 #endif
1611
1612 #ifdef swap
1613 touch(src);
1614 if ( !eq(swap(src), inv) ) return __LINE__;
1615 #endif
1616
1617 #ifdef swap2
1618 touch(src);
1619 if ( !eq(swap2(src), inv) ) return __LINE__;
1620 #endif
1621
1622 #ifdef swap3
1623 touch(src);
1624 if ( !eq(swap3(src), inv) ) return __LINE__;
1625 touch(src);
1626 #endif
1627
1628 #ifdef broadcast
1629 if ( !eq(broadcast(ELEM_COUNT + 1), src + inv) ) return __LINE__;
1630 #endif
1631
1632 #ifdef broadcast2
1633 if ( !eq(broadcast2(ELEM_COUNT + 1), src + inv) ) return __LINE__;
1634 #endif
1635
1636 #if defined(broadcast_half) && defined(insert_half)
1637 {
1638 half_t aux = low_half(src);
1639
1640 touch(aux);
1641 x = broadcast_half(aux);
1642 touch(aux);
1643 y = insert_half(src, aux, 1);
1644 if ( !eq(x, y) ) return __LINE__;
1645 }
1646 #endif
1647
1648 #if defined(broadcast_quarter) && defined(insert_quarter)
1649 {
1650 quarter_t aux = low_quarter(src);
1651
1652 touch(aux);
1653 x = broadcast_quarter(aux);
1654 touch(aux);
1655 y = insert_quarter(src, aux, 1);
1656 touch(aux);
1657 y = insert_quarter(y, aux, 2);
1658 touch(aux);
1659 y = insert_quarter(y, aux, 3);
1660 if ( !eq(x, y) ) return __LINE__;
1661 }
1662 #endif
1663
1664 #if defined(broadcast_eighth) && defined(insert_eighth) && \
1665 /* At least gcc 7.3 "optimizes" away all insert_eighth() calls below. */ \
1666 __GNUC__ >= 8
1667 {
1668 eighth_t aux = low_eighth(src);
1669
1670 touch(aux);
1671 x = broadcast_eighth(aux);
1672 touch(aux);
1673 y = insert_eighth(src, aux, 1);
1674 touch(aux);
1675 y = insert_eighth(y, aux, 2);
1676 touch(aux);
1677 y = insert_eighth(y, aux, 3);
1678 touch(aux);
1679 y = insert_eighth(y, aux, 4);
1680 touch(aux);
1681 y = insert_eighth(y, aux, 5);
1682 touch(aux);
1683 y = insert_eighth(y, aux, 6);
1684 touch(aux);
1685 y = insert_eighth(y, aux, 7);
1686 if ( !eq(x, y) ) return __LINE__;
1687 }
1688 #endif
1689
1690 #if defined(interleave_lo) && defined(interleave_hi)
1691 touch(src);
1692 x = interleave_lo(inv, src);
1693 touch(src);
1694 y = interleave_hi(inv, src);
1695 touch(src);
1696 # ifdef UINT_SIZE
1697 z = ((x - y) ^ ~alt) - ~alt;
1698 # else
1699 z = (x - y) * alt;
1700 # endif
1701 # ifdef broadcast
1702 if ( !eq(z, broadcast(ELEM_COUNT / 2)) ) return __LINE__;
1703 # else
1704 if ( !eq(z, ELEM_COUNT / 2) ) return __LINE__;
1705 # endif
1706 #endif
1707
1708 #if defined(INT_SIZE) && defined(widen1) && defined(interleave_lo)
1709
1710 x = src * alt;
1711 y = interleave_lo(x, alt < 0);
1712 touch(x);
1713 z = widen1(low_half(x));
1714 touch(x);
1715 if ( !eq(z, y) ) return __LINE__;
1716
1717 # ifdef widen2
1718 y = interleave_lo(alt < 0, alt < 0);
1719 y = interleave_lo(z, y);
1720 touch(x);
1721 z = widen2(low_quarter(x));
1722 touch(x);
1723 if ( !eq(z, y) ) return __LINE__;
1724
1725 # ifdef widen3
1726 y = interleave_lo(alt < 0, alt < 0);
1727 y = interleave_lo(y, y);
1728 y = interleave_lo(z, y);
1729 touch(x);
1730 z = widen3(low_eighth(x));
1731 touch(x);
1732 if ( !eq(z, y) ) return __LINE__;
1733 # endif
1734 # endif
1735
1736 #endif
1737
1738 #if defined(UINT_SIZE) && defined(interleave_lo)
1739
1740 y = interleave_lo(src, (vec_t){});
1741 z = interleave_lo(y, (vec_t){});
1742
1743 # ifdef widen1
1744 touch(src);
1745 x = widen1(low_half(src));
1746 touch(src);
1747 if ( !eq(x, y) ) return __LINE__;
1748 # endif
1749
1750 # ifdef widen2
1751 touch(src);
1752 x = widen2(low_quarter(src));
1753 touch(src);
1754 if ( !eq(x, z) ) return __LINE__;
1755 # endif
1756
1757 # ifdef widen3
1758 touch(src);
1759 x = widen3(low_eighth(src));
1760 touch(src);
1761 if ( !eq(x, interleave_lo(z, (vec_t){})) ) return __LINE__;
1762 # endif
1763
1764 #endif
1765
1766 #if defined(widen1) && defined(shrink1)
1767 {
1768 half_t aux1 = low_half(src), aux2;
1769
1770 touch(aux1);
1771 x = widen1(aux1);
1772 touch(x);
1773 aux2 = shrink1(x);
1774 touch(aux2);
1775 for ( i = 0; i < ELEM_COUNT / 2; ++i )
1776 if ( aux2[i] != src[i] )
1777 return __LINE__;
1778 }
1779 #endif
1780
1781 #if defined(widen2) && defined(shrink2)
1782 {
1783 quarter_t aux1 = low_quarter(src), aux2;
1784
1785 touch(aux1);
1786 x = widen2(aux1);
1787 touch(x);
1788 aux2 = shrink2(x);
1789 touch(aux2);
1790 for ( i = 0; i < ELEM_COUNT / 4; ++i )
1791 if ( aux2[i] != src[i] )
1792 return __LINE__;
1793 }
1794 #endif
1795
1796 #if defined(widen3) && defined(shrink3)
1797 {
1798 eighth_t aux1 = low_eighth(src), aux2;
1799
1800 touch(aux1);
1801 x = widen3(aux1);
1802 touch(x);
1803 aux2 = shrink3(x);
1804 touch(aux2);
1805 for ( i = 0; i < ELEM_COUNT / 8; ++i )
1806 if ( aux2[i] != src[i] )
1807 return __LINE__;
1808 }
1809 #endif
1810
1811 #ifdef dup_lo
1812 touch(src);
1813 x = dup_lo(src);
1814 touch(src);
1815 if ( !eq(x - src, (alt - 1) / 2) ) return __LINE__;
1816 #endif
1817
1818 #ifdef dup_hi
1819 touch(src);
1820 x = dup_hi(src);
1821 touch(src);
1822 if ( !eq(x - src, (alt + 1) / 2) ) return __LINE__;
1823 #endif
1824
1825 for ( y = src, i = 1; i < ELEM_COUNT; i += 2 )
1826 y[i] = inv[i];
1827
1828 #ifdef select
1829 # ifdef UINT_SIZE
1830 select(&z, src, inv, alt);
1831 # else
1832 select(&z, src, inv, alt > 0);
1833 # endif
1834 if ( !eq(z, y) ) return __LINE__;
1835 #endif
1836
1837 #ifdef select2
1838 # ifdef UINT_SIZE
1839 select2(&z, src, inv, alt);
1840 # else
1841 select2(&z, src, inv, alt > 0);
1842 # endif
1843 if ( !eq(z, y) ) return __LINE__;
1844 #endif
1845
1846 #ifdef mix
1847 touch(src);
1848 touch(inv);
1849 x = mix(src, inv);
1850 if ( !eq(x, y) ) return __LINE__;
1851
1852 # ifdef addsub
1853 touch(src);
1854 touch(inv);
1855 x = addsub(src, inv);
1856 touch(src);
1857 touch(inv);
1858 y = mix(src - inv, src + inv);
1859 if ( !eq(x, y) ) return __LINE__;
1860 # endif
1861 #endif
1862
1863 #ifdef rotr
1864 x = rotr(src, 1);
1865 y = (src & (ELEM_COUNT - 1)) + 1;
1866 if ( !eq(x, y) ) return __LINE__;
1867 #endif
1868
1869 #ifdef dot_product
1870 touch(src);
1871 touch(inv);
1872 x = dot_product(src, inv);
1873 if ( !eq(x, (vec_t){ (ELEM_COUNT * (ELEM_COUNT + 1) *
1874 (ELEM_COUNT + 2)) / 6 }) ) return __LINE__;
1875 #endif
1876
1877 #ifdef hadd
1878 # if (!defined(INT_SIZE) || INT_SIZE > 1 || ELEM_COUNT < 16) && \
1879 (!defined(UINT_SIZE) || UINT_SIZE > 1 || ELEM_COUNT <= 16)
1880 x = src;
1881 for ( i = ELEM_COUNT; i >>= 1; )
1882 {
1883 touch(x);
1884 x = hadd((vec_t){}, x);
1885 }
1886 if ( x[ELEM_COUNT - 1] != (ELEM_COUNT * (ELEM_COUNT + 1)) / 2 ) return __LINE__;
1887 # endif
1888
1889 # ifdef hsub
1890 touch(src);
1891 touch(inv);
1892 x = hsub(src, inv);
1893 for ( i = ELEM_COUNT; i >>= 1; )
1894 x = hadd(x, (vec_t){});
1895 if ( !eq(x, (vec_t){}) ) return __LINE__;
1896 # endif
1897 #endif
1898
1899 #if defined(getexp) && defined(getmant)
1900 touch(src);
1901 x = getmant(src);
1902 touch(src);
1903 y = getexp(src);
1904 touch(src);
1905 for ( j = i = 0; i < ELEM_COUNT; ++i )
1906 {
1907 if ( y[i] != j ) return __LINE__;
1908
1909 if ( !((i + 1) & (i + 2)) )
1910 ++j;
1911
1912 if ( !(i & (i + 1)) && x[i] != 1 ) return __LINE__;
1913 }
1914 # ifdef scale
1915 touch(y);
1916 z = scale(x, y);
1917 if ( !eq(src, z) ) return __LINE__;
1918 # endif
1919 #endif
1920
1921 #if (defined(__XOP__) && VEC_SIZE == 16 && (INT_SIZE == 2 || INT_SIZE == 4)) || \
1922 (defined(__AVX512F__) && defined(FLOAT_SIZE))
1923 return -fma_test();
1924 #endif
1925
1926 return 0;
1927 }