File: test_avx.cpp

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 121,860 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,366; asm: 2,415; lisp: 1,869
file content (410 lines) | stat: -rw-r--r-- 12,484 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * Copyright 2020 The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */
// This file uses AVX by calling different functions with different interesting
// inputs and prints the results. Use a diff tool to compare the results between
// platforms.

// immintrin.h must be included before test_sse.h
// clang-format off
#include <immintrin.h>
#include "test_sse.h"
// clang-format on

bool testNaNBits = true;

float* interesting_floats = get_interesting_floats();
int numInterestingFloats =
  sizeof(interesting_floats_) / sizeof(interesting_floats_[0]);
uint32_t* interesting_ints = get_interesting_ints();
int numInterestingInts =
  sizeof(interesting_ints_) / sizeof(interesting_ints_[0]);
double* interesting_doubles = get_interesting_doubles();
int numInterestingDoubles =
  sizeof(interesting_doubles_) / sizeof(interesting_doubles_[0]);

void test_arithmetic(void) {
  testNaNBits = false;
  Ret_M256d_M256d(__m256d, _mm256_add_pd);
  Ret_M256_M256(__m256, _mm256_add_ps);

  testNaNBits = true;
  Ret_M256d_M256d(__m256d, _mm256_sub_pd);
  Ret_M256_M256(__m256, _mm256_sub_ps);

  testNaNBits = false;
  Ret_M256d_M256d(__m256d, _mm256_addsub_pd);
  Ret_M256_M256(__m256, _mm256_addsub_ps);

  testNaNBits = true;
  Ret_M256d_M256d(__m256d, _mm256_div_pd);
  Ret_M256_M256(__m256, _mm256_div_ps);

  testNaNBits = false;
  Ret_M256d_M256d(__m256d, _mm256_mul_pd);
  Ret_M256_M256(__m256, _mm256_mul_ps);

  Ret_M256d_M256d(__m256d, _mm256_hadd_pd);
  Ret_M256_M256(__m256, _mm256_hadd_ps);

  testNaNBits = true;
  Ret_M256d_M256d(__m256d, _mm256_hsub_pd);
  Ret_M256_M256(__m256, _mm256_hsub_ps);

  testNaNBits = false;
  Ret_M256_M256_Tint(__m256, _mm256_dp_ps);
}

void test_special_math(void) {
  Ret_M256d_M256d(__m256d, _mm256_max_pd);
  Ret_M256_M256(__m256, _mm256_max_ps);

  Ret_M256d_M256d(__m256d, _mm256_min_pd);
  Ret_M256_M256(__m256, _mm256_min_ps);

  Ret_M256d_Tint(__m256d, _mm256_round_pd);
  Ret_M256_Tint(__m256, _mm256_round_ps);

  Ret_M256d(__m256d, _mm256_ceil_pd);
  Ret_M256(__m256, _mm256_ceil_ps);

  Ret_M256d(__m256d, _mm256_floor_pd);
  Ret_M256(__m256, _mm256_floor_ps);
}

void test_elementary_math(void) {
  Ret_M256d(__m256d, _mm256_sqrt_pd);
  Ret_M256approx(__m256, _mm256_sqrt_ps);
  Ret_M256approx(__m256, _mm256_rsqrt_ps);
  Ret_M256approx(__m256, _mm256_rcp_ps);
}

void test_logical(void) {
  Ret_M128d_M128d(__m128d, _mm_and_pd);
  Ret_M128_M128(__m128, _mm_and_ps);

  Ret_M128d_M128d(__m128d, _mm_andnot_pd);
  Ret_M128_M128(__m128, _mm_andnot_ps);

  Ret_M128d_M128d(__m128d, _mm_or_pd);
  Ret_M128_M128(__m128, _mm_or_ps);

  Ret_M128d_M128d(__m128d, _mm_xor_pd);
  Ret_M128_M128(__m128, _mm_xor_ps);

  Ret_M128d_M128d(int, _mm_testz_pd);
  Ret_M128d_M128d(int, _mm_testc_pd);
  Ret_M128d_M128d(int, _mm_testnzc_pd);

  Ret_M128_M128(int, _mm_testz_ps);
  Ret_M128_M128(int, _mm_testc_ps);
  Ret_M128_M128(int, _mm_testnzc_ps);

  Ret_M256d_M256d(int, _mm256_testz_pd);
  Ret_M256d_M256d(int, _mm256_testc_pd);
  Ret_M256d_M256d(int, _mm256_testnzc_pd);

  Ret_M256_M256(int, _mm256_testz_ps);
  Ret_M256_M256(int, _mm256_testc_ps);
  Ret_M256_M256(int, _mm256_testnzc_ps);

  Ret_M256i_M256i(int, _mm256_testz_si256);
  Ret_M256i_M256i(int, _mm256_testc_si256);
  Ret_M256i_M256i(int, _mm256_testnzc_si256);
}

// split test_swizzle into multiple functions to avoid too many locals error
void test_swizzle_128bit() {
  Ret_M128d_M128i(__m128d, _mm_permutevar_pd);
  Ret_M128_M128i(__m128, _mm_permutevar_ps);
  Ret_M128d_Tint(__m128d, _mm_permute_pd);
  Ret_M128_Tint(__m128, _mm_permute_ps);
}

void test_swizzle_permute2f128() {
  Ret_M256d_M256i(__m256d, _mm256_permutevar_pd);
  Ret_M256_M256i(__m256, _mm256_permutevar_ps);
  Ret_M256d_Tint(__m256d, _mm256_permute_pd);
  Ret_M256_Tint(__m256, _mm256_permute_ps);

  Ret_M256d_M256d_Tint(__m256d, _mm256_permute2f128_pd);
  Ret_M256_M256_Tint(__m256, _mm256_permute2f128_ps);
  Ret_M256i_M256i_Tint(__m256i, _mm256_permute2f128_si256);
}

void test_swizzle_blend() {
  Ret_M256d_M256d_Tint(__m256d, _mm256_blend_pd);
  Ret_M256_M256_Tint(__m256, _mm256_blend_ps);
  Ret_M256d_M256d_M256d(__m256d, _mm256_blendv_pd);
  Ret_M256_M256_M256(__m256, _mm256_blendv_ps);
}

void test_swizzle_shuffle() {
  Ret_M256d_M256d_Tint(__m256d, _mm256_shuffle_pd);
  Ret_M256_M256_Tint(__m256, _mm256_shuffle_ps);
}

void test_swizzle_extract_int() {
  Ret_M256i_Tint(int, _mm256_extract_epi32);
  Ret_M256i_Tint(int, _mm256_extract_epi16);
  Ret_M256i_Tint(int, _mm256_extract_epi8);
  Ret_M256i_Tint(int64_t, _mm256_extract_epi64);
}

void test_swizzle_insert_int() {
  Ret_M256i_int_Tint(__m256i, _mm256_insert_epi32);
  Ret_M256i_int_Tint(__m256i, _mm256_insert_epi16);
  Ret_M256i_int_Tint(__m256i, _mm256_insert_epi8);
  Ret_M256i_int_Tint(__m256i, _mm256_insert_epi64);
}

void test_swizzle_unpack() {
  Ret_M256d_M256d(__m256d, _mm256_unpackhi_pd);
  Ret_M256d_M256d(__m256d, _mm256_unpacklo_pd);
  Ret_M256_M256(__m256, _mm256_unpackhi_ps);
  Ret_M256_M256(__m256, _mm256_unpacklo_ps);
}

void test_swizzle_insertf128() {
  Ret_M256d_M128d_Tint(__m256d, _mm256_insertf128_pd);
  Ret_M256_M128_Tint(__m256, _mm256_insertf128_ps);
  Ret_M256i_M128i_Tint(__m256i, _mm256_insertf128_si256);
}

void test_swizzle_extractf128() {
  Ret_M256d_Tint(__m128d, _mm256_extractf128_pd);
  Ret_M256_Tint(__m128, _mm256_extractf128_ps);
  Ret_M256i_Tint(__m128i, _mm256_extractf128_si256);
}

void test_swizzle(void) {
  test_swizzle_128bit();
  test_swizzle_permute2f128();
  test_swizzle_blend();
  test_swizzle_shuffle();
  test_swizzle_extract_int();
  test_swizzle_insert_int();
  test_swizzle_unpack();
  test_swizzle_insertf128();
  test_swizzle_extractf128();
}

void test_convert(void) {
  Ret_M128i(__m256d, _mm256_cvtepi32_pd);
  Ret_M256i(__m256, _mm256_cvtepi32_ps);

  Ret_M256d(__m128, _mm256_cvtpd_ps);

  Ret_M256(__m256i, _mm256_cvtps_epi32);
  Ret_M128(__m256d, _mm256_cvtps_pd);

  Ret_M256d(__m128i, _mm256_cvttpd_epi32);

  Ret_M256d(__m128i, _mm256_cvtpd_epi32);

  Ret_M256(__m256i, _mm256_cvttps_epi32);

  Ret_M256d(double, _mm256_cvtsd_f64);

  Ret_M256i(int, _mm256_cvtsi256_si32);

  Ret_M256(float, _mm256_cvtss_f32);
}

void test_move(void) {
  Ret_M256(__m256, _mm256_movehdup_ps);
  Ret_M256(__m256, _mm256_moveldup_ps);
  Ret_M256d(__m256d, _mm256_movedup_pd);
}

// split test_compare into multiple functions to avoid too many locals error
void test_compare_128bit() {
  Ret_M128d_M128d_Tint_5bits(__m128d, _mm_cmp_pd);
  Ret_M128_M128_Tint_5bits(__m128, _mm_cmp_ps);
  Ret_M128d_M128d_Tint_5bits(__m128d, _mm_cmp_sd);
  Ret_M128_M128_Tint_5bits(__m128, _mm_cmp_ss);
}

void test_mm256_cmp_pd_tint_0_to_15() {
  Ret_M256d_M256d_Tint_5bits_0_to_15(__m256d, _mm256_cmp_pd);
}

void test_mm256_cmp_pd_tint_16_to_31() {
  Ret_M256d_M256d_Tint_5bits_16_to_31(__m256d, _mm256_cmp_pd);
}

void test_mm256_cmp_pd() {
  test_mm256_cmp_pd_tint_0_to_15();
  test_mm256_cmp_pd_tint_16_to_31();
}

void test_mm256_cmp_ps_tint_0_to_15() {
  Ret_M256_M256_Tint_5bits_0_to_15(__m256, _mm256_cmp_ps);
}

void test_mm256_cmp_ps_tint_16_to_31() {
  Ret_M256_M256_Tint_5bits_16_to_31(__m256, _mm256_cmp_ps);
}

void test_mm256_cmp_ps() {
  test_mm256_cmp_ps_tint_0_to_15();
  test_mm256_cmp_ps_tint_16_to_31();
}

void test_compare(void) {
  test_compare_128bit();
  test_mm256_cmp_pd();
  test_mm256_cmp_ps();
}

void test_misc(void) {
  Ret_M256d(int, _mm256_movemask_pd);
  Ret_M256(int, _mm256_movemask_ps);
}

void test_load(void) {
  Ret_FloatPtr(__m128, _mm_broadcast_ss, 1, 1);
  Ret_DoublePtr(__m256d, _mm256_broadcast_sd, 1, 1);
  Ret_FloatPtr(__m256, _mm256_broadcast_ss, 1, 1);

  // TODO reuse Ret_DoublePtr?
  Ret_M128dPtr(__m256d, _mm256_broadcast_pd);
  Ret_M128Ptr(__m256, _mm256_broadcast_ps); // must aligned? not sure

  Ret_DoublePtr(__m256d, _mm256_load_pd, 4, 4); // error, input not aligned
  Ret_FloatPtr(__m256, _mm256_load_ps, 8, 8);   // error, align
  Ret_DoublePtr(__m256d, _mm256_loadu_pd, 4, 1);
  Ret_FloatPtr(__m256, _mm256_loadu_ps, 8, 1);

  Ret_IntPtr(__m256i, _mm256_load_si256, __m256i*, 8, 8); // error, align
  Ret_IntPtr(__m256i, _mm256_loadu_si256, __m256i_u*, 8, 1);
  Ret_IntPtr(__m256i, _mm256_lddqu_si256, __m256i_u*, 8, 1);

  Ret_DoublePtr_M128i(__m128d, _mm_maskload_pd, 2, 2);
  Ret_DoublePtr_M256i(__m256d, _mm256_maskload_pd, 4, 4);
  Ret_FloatPtr_M128i(__m128, _mm_maskload_ps, 4, 4);
  Ret_FloatPtr_M256i(__m256, _mm256_maskload_ps, 8, 8);

  Ret_DoublePtr_DoublePtr(__m256d, _mm256_loadu2_m128d, 2, 2);
  Ret_FloatPtr_FloatPtr(__m256, _mm256_loadu2_m128, 4, 4);
  Ret_IntPtr_IntPtr(__m256i, _mm256_loadu2_m128i, __m128i_u*, 4, 4);
}

void test_store(void) {

  void_OutDoublePtr_M256d(_mm256_store_pd, double*, 32, 32);
  void_OutFloatPtr_M256(_mm256_store_ps, float*, 32, 32);
  void_OutDoublePtr_M256d(_mm256_storeu_pd, double*, 32, 1);
  void_OutFloatPtr_M256(_mm256_storeu_ps, float*, 32, 1);
  void_OutIntPtr_M256i(_mm256_store_si256, __m256i*, 32, 32);
  void_OutIntPtr_M256i(_mm256_storeu_si256, __m256i_u*, 32, 1);

  void_OutDoublePtr_M128i_M128d(_mm_maskstore_pd, double*, 16, 8);
  void_OutDoublePtr_M256i_M256d(_mm256_maskstore_pd, double*, 32, 8);
  void_OutFloatPtr_M128i_M128(_mm_maskstore_ps, float*, 16, 4);
  void_OutFloatPtr_M256i_M256(_mm256_maskstore_ps, float*, 32, 4);

  void_OutIntPtr_M256i(_mm256_stream_si256, __m256i*, 32, 32);
  void_OutDoublePtr_M256d(_mm256_stream_pd, double*, 32, 32);
  void_OutFloatPtr_M256(_mm256_stream_ps, float*, 32, 32);

  void_OutFloatPtr_OutFloatPtr_M256(_mm256_storeu2_m128, float*, 32, 1);
  void_OutDoublePtr_OutDoublePtr_M256d(_mm256_storeu2_m128d, double*, 32, 1);
  void_OutIntPtr_OutIntPtr_M256i(_mm256_storeu2_m128i, __m128i_u*, 32, 1)
}

void test_undef(void) {
#ifdef __EMSCRIPTEN__
  _mm256_undefined_pd();
  _mm256_undefined_ps();
  _mm256_undefined_si256();
#endif
}

void test_set(void) {
  Ret_Double4(__m256d, _mm256_set_pd, 1);
  Ret_Float8(__m256, _mm256_set_ps, 1);
  Ret_Int8(__m256i, _mm256_set_epi32, 1);
  Ret_Short16(__m256i, _mm256_set_epi16, 2);
  Ret_Char32(__m256i, _mm256_set_epi8, 4);
  Ret_Longlong4(__m256i, _mm256_set_epi64x, 1);

  Ret_Double4(__m256d, _mm256_setr_pd, 1);
  Ret_Float8(__m256, _mm256_setr_ps, 1);
  Ret_Int8(__m256i, _mm256_setr_epi32, 1);
  Ret_Short16(__m256i, _mm256_setr_epi16, 2);
  Ret_Char32(__m256i, _mm256_setr_epi8, 4);
  Ret_Longlong4(__m256i, _mm256_setr_epi64x, 1);

  Ret_Double(__m256d, _mm256_set1_pd, 1);
  Ret_Float(__m256, _mm256_set1_ps, 1);
  Ret_Int(__m256i, _mm256_set1_epi32, 1);
  Ret_Int(__m256i, _mm256_set1_epi16, 1);
  Ret_Int(__m256i, _mm256_set1_epi8, 1);
  Ret_Int(__m256i, _mm256_set1_epi64x, 1);

  char str[256] = {};
  __m256d zerod = _mm256_setzero_pd();
  tostr(&zerod, str);
  printf("_mm256_setzero_pd() = %s\n", str);

  __m256 zero = _mm256_setzero_ps();
  tostr(&zero, str);
  printf("_mm256_setzero_ps() = %s\n", str);

  __m256i zeroi = _mm256_setzero_si256();
  tostr(&zeroi, str);
  printf("_mm256_setzero_si256() = %s\n", str);

  Ret_M128_M128(__m256, _mm256_set_m128);
  Ret_M128d_M128d(__m256d, _mm256_set_m128d);
  Ret_M128i_M128i(__m256i, _mm256_set_m128i);

  Ret_M128_M128(__m256, _mm256_setr_m128);
  Ret_M128d_M128d(__m256d, _mm256_setr_m128d);
  Ret_M128i_M128i(__m256i, _mm256_setr_m128i);
}

void test_cast(void) {
  Ret_M256d(__m256, _mm256_castpd_ps);
  Ret_M256d(__m256i, _mm256_castpd_si256);
  Ret_M256(__m256d, _mm256_castps_pd);
  Ret_M256(__m256i, _mm256_castps_si256);
  Ret_M256i(__m256d, _mm256_castsi256_pd);
  Ret_M256i(__m256, _mm256_castsi256_ps);

  Ret_M256d(__m128d, _mm256_castpd256_pd128);
  Ret_M256(__m128, _mm256_castps256_ps128);
  Ret_M256i(__m128i, _mm256_castsi256_si128);
  Ret_M128d(__m256d, _mm256_castpd128_pd256);
  Ret_M128(__m256, _mm256_castps128_ps256);
  Ret_M128i(__m256i, _mm256_castsi128_si256);

  Ret_M128d(__m256d, _mm256_zextpd128_pd256);
  Ret_M128(__m256, _mm256_zextps128_ps256);
  Ret_M128i(__m256i, _mm256_zextsi128_si256);
}

int main() {
  assert(numInterestingFloats % 8 == 0);
  assert(numInterestingInts % 8 == 0);
  assert(numInterestingDoubles % 4 == 0);

  test_arithmetic();
  test_special_math();
  test_elementary_math();
  test_logical();
  test_swizzle();
  test_convert();
  test_move();
  test_compare();
  test_misc();
  test_load();
  test_store();
  test_undef();
  test_set();
  test_cast();
}