-
Notifications
You must be signed in to change notification settings - Fork 7
/
testbench1.cpp
1974 lines (1752 loc) · 62.3 KB
/
testbench1.cpp
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************** testbench1.cpp *******************************
* Author: Agner Fog
* Date created: 2019-04-09
* Last modified: 2023-12-02
* Version: 2.02.02
* Project: Testbench for vector class library
* Description:
* Compile and run this program to test operators and functions in VCL
* This file contains test cases for general operators and functions.
* Each function or operator is tested with many different combinations
* of input data.
*
* Instructions:
* The following parameters must be defined on the command line or added
* in the top of this file:
*
* vtype: Vector type to test
* rtype: Vector type for result, if different from vtype
* (If result is a scalar, specify a corresponding vector type)
* testcase: A number defining a function or operator to test.
* See the cases in this file.
* seed: Seed for random number generator. May be any integer
* INSTRSET: Desired instruction set. Needs to be specified for MS compiler,
* but determined automatically for other compilers. Values:
* 2: SSE2
* 3: SSE3
* 4: SSSE3
* 5: SSE4.1
* 6: SSE4.2
* 7: AVX
* 8: AVX2
* 9: AVX512F
* 10: AVX512BW/DQ/VL
*
* Compile with any compiler supported by VCL.
* Specify the desired instruction set and optimization options as parameters
* to the compiler.
*
* (c) Copyright 2019 - 2022 Agner Fog.
* Gnu general public license 3.0 https://www.gnu.org/licenses/gpl.html
******************************************************************************
Test cases:
1: operator +
2: operator -
3: operator *
4: operator / (floating point types)
5: operator / (integer types. divide by scalar)
6: operator / (signed integer types. divide by compile-time constant)
7: operator / (unsigned integer types. divide by compile-time constant)
8: unary -
9: max
10: min
11: maximum (floating point types)
12: minimum (floating point types)
13: abs
14: if_add
15: if_sub
16: if_mul
17: if_div (floating point types)
20: store_a
21: store_nt
100: operator << (integer types)
101: operator >> (integer types)
102: rotate_left (signed integers only)
103: add_saturated (integer types)
104: sub_saturated (integer types)
105: abs_saturated (integer types)
106: operator &
107: operator |
108: operator ^
109: operator ~
110: andnot (bool vectors only)
200: sign_combine (float types)
202: square (float types)
//added. not in scripts yet:
210: is_finite
211: is_inf
212: is_nan
213: is_subnormal
214: is_zero_or_subnormal
220: infinite4f
221: nan_vec (nan_code tested in testbench3)
300: operator <
301: operator <=
302: operator ==
303: operator !=
304: operator >=
305: operator >
306: sign_bit (float types)
400: horizontal_add
401: horizontal_add_x (integer types)
402: horizontal_and (bool vectors)
403: horizontal_or (bool vectors)
404: horizontal_min
405: horizontal_max
410: horizontal_find_first (bool vectors)
411: horizontal_count (bool vectors)
412: to_bits (bool vectors)
413: load_bits (bool vectors)
500: conversion between integer vectors with same total number of bits
501: conversion between boolean vectors with same number of elements
502: reinterpret_i
503: reinterpret_f
504: reinterpret_d
505: roundi
506: truncatei
507: round_to_int32
508: truncate_to_int32
509: round_to_int32 with two parameters
510: truncate_to_int32 with two parameters
511: to_float (integer to float)
512: to_double (integer to double)
513: to_float (double to float)
514: to_double (float to double)
//added. not in scripts yet:
542: pow
543: exp2
544: fremainder
545: fmodulo
600: concatenate vectors (constructor with two parameters)
601: concatenate boolean vectors (constructor with two parameters)
602: get_low (int or float vector)
603: get_high (int or float vector)
604: get_low (boolean vector)
605: get_high (boolean vector)
606: extend (integer. vector size doubled)
607: extend_low
608: extend_high
609: compress (one integer parameter, vector size reduced)
610: compress_saturated (one integer parameter, vector size reduced)
611: compress (two integer parameters, integer size reduced)
612: compress_saturated (two integer parameters, integer size reduced)
620: insert(index, value)
621: extract(index)
622: cutoff(index)
623: load_partial(index, p)
624: store_partial(index, p)
650: constructor with all elements
*****************************************************************************/
#include <stdio.h>
#include <cmath>
#if defined (__linux__) && !defined(__LP64__)
#include <fpu_control.h> // set floating point control word
#endif
// maximum vector size
#define MAX_VECTOR_SIZE 512
#ifndef INSTRSET
#define INSTRSET 10 // desired instruction set
#endif
//#define VCL_NAMESPACE MYNAMESPACE
#include <vectorclass.h> // vector class library
#ifdef VCL_NAMESPACE
using namespace VCL_NAMESPACE;
#endif
#ifndef testcase
// ----------------------------------------------------------------------------
// Specify input parameters here if running from an IDE:
// ----------------------------------------------------------------------------
#define testcase 1
#define vtype Vec16i
#define rtype vtype
//#define rtype Vec32uc
#define seed 1
#else
// ----------------------------------------------------------------------------
// Default input parameters when compiling from a script
// ----------------------------------------------------------------------------
// input or index vector type to be tested
#ifndef vtype
#define vtype Vec2d
#endif
// return type or data vector type to be tested
#ifndef rtype
#define rtype vtype
#endif
// random number seed
#ifndef seed
#define seed 1
#endif
#endif // testcase
// ----------------------------------------------------------------------------
// Declarations
// ----------------------------------------------------------------------------
// dummy vectors used for getting element type
vtype dummy;
rtype dummyr;
typedef decltype(dummy[0]) ST; // scalar type input vectors
typedef decltype(dummyr[0]) RT; // scalar type for return vector
const int maxvectorsize = 64; // max number of elements in a vector
uint64_t bitfield; // integer for load_bits function
/************************************************************************
*
* Test cases
*
************************************************************************/
#if testcase == 1 // +
inline rtype testFunction(vtype const& a, vtype const& b) { return a + b; }
RT referenceFunction(ST a, ST b) { return a + b; }
#elif testcase == 2 // -
inline rtype testFunction(vtype const& a, vtype const& b) { return a - b; }
RT referenceFunction(ST a, ST b) { return a - b; }
#elif testcase == 3 // *
inline rtype testFunction(vtype const& a, vtype const& b) { return a * b; }
RT referenceFunction(ST a, ST b) { return a * b; }
#elif testcase == 4 // / (float types only)
inline rtype testFunction(vtype const& a, vtype const& b) { return a / b; }
RT referenceFunction(ST a, ST b) { return a / b; }
#elif testcase == 5 // / int types: divide by scalar
#define WHOLE_VECTOR
ST b0;
inline rtype testFunction(vtype const& a, vtype const& b) {
int i = 0;
while (b[i] == 0) i++;
b0 = b[i];
return a / b0;
}
rtype referenceFunction(vtype a, vtype b) {
rtype r(0);
for (int i = 0; i < r.size(); i++) {
r.insert(i, a[i] / b0);
}
return r; }
#elif testcase == 6 // / int types: divide by compile-time constant
#if defined(indexes) && (indexes + 0 != 0) // if indexes is not blank, use it as divisor
const ST divisor = indexes ;
#else
const ST divisor = 13;
#endif
inline rtype testFunction(vtype const& a, vtype const& b) {
//return a / (divisor);
return a / const_int(divisor);
}
RT referenceFunction(ST a, ST b) {
return a / divisor; }
#elif testcase == 7 // / int types: divide by compile-time unsigned constant
#if defined(indexes) && (indexes + 0 != 0) // if indexes is not blank, use it as divisor
const ST divisor = indexes ;
#else
const ST divisor = 27;
#endif
inline rtype testFunction(vtype const& a, vtype const& b) {
return a / const_uint(divisor);
}
RT referenceFunction(ST a, ST b) {
return a / divisor; }
#elif testcase == 8 // unary -
inline rtype testFunction(vtype const& a, vtype const& b) { return -b; }
RT referenceFunction(ST a, ST b) { return -b; }
#elif testcase == 9 // max
inline rtype testFunction(vtype const& a, vtype const& b) { return max(a, b); }
RT referenceFunction(ST a, ST b) { return a > b ? a : b; }
#define TESTNAN
#elif testcase == 10 // min
inline rtype testFunction(vtype const& a, vtype const& b) { return min(a, b); }
RT referenceFunction(ST a, ST b) { return a < b ? a : b; }
#define TESTNAN
#elif testcase == 11 // maximum
inline rtype testFunction(vtype const& a, vtype const& b) { return maximum(a, b); }
RT referenceFunction(ST a, ST b) {
if (a!=a) return a;
if (b!=b) return b;
return a > b ? a : b;
}
#define TESTNAN
#elif testcase == 12 // minimum
inline rtype testFunction(vtype const& a, vtype const& b) { return minimum(a, b); }
RT referenceFunction(ST a, ST b) {
if (a!=a) return a;
if (b!=b) return b;
return a < b ? a : b;
}
#define TESTNAN
#elif testcase == 13 // abs
inline rtype testFunction(vtype const& a, vtype const& b) { return abs(b); }
RT referenceFunction(ST a, ST b) { return b > 0 ? b : -b; }
#elif testcase == 14 // if_add
inline rtype testFunction(vtype const& f, vtype const& a, vtype const& b) {
return if_add(f != vtype(0), a, b);
}
RT referenceFunction(ST f, ST a, ST b) { return f != 0 ? a+b : a; }
#define USE_FLAG
#elif testcase == 15 // if_sub
inline rtype testFunction(vtype const& f, vtype const& a, vtype const& b) {
return if_sub(f != vtype(0), a, b);
}
RT referenceFunction(ST f, ST a, ST b) { return f != 0 ? a-b : a; }
#define USE_FLAG
#elif testcase == 16 // if_mul
inline rtype testFunction(vtype const& f, vtype const& a, vtype const& b) {
return if_mul(f != vtype(0), a, b);
}
RT referenceFunction(ST f, ST a, ST b) { return f != 0 ? a*b : a; }
#define USE_FLAG
#elif testcase == 17 // if_div
inline rtype testFunction(vtype const& f, vtype const& a, vtype const& b) {
return if_div(f != vtype(0), a, b);
}
RT referenceFunction(ST f, ST a, ST b) { return f != 0 ? a/b : a; }
#define USE_FLAG
#elif testcase == 20 // store_a
inline rtype testFunction(vtype const& a, vtype const& b) {
union { // make aligned array
ST y[vtype::size()];
vtype dummy;
} aligned = {{0}};
a.store_a(aligned.y);
return vtype().load_a(aligned.y);
}
RT referenceFunction(ST a, ST b) { return a; }
#elif testcase == 21 // store_nt
inline rtype testFunction(vtype const& a, vtype const& b) {
union { // make aligned array
ST y[vtype::size()];
vtype dummy;
} aligned = {{0}};
a.store_nt(aligned.y);
return vtype().load_a(aligned.y);
}
RT referenceFunction(ST a, ST b) { return a; }
// ----------------------------------------------------------------------------
// integer only cases:
// ----------------------------------------------------------------------------
#elif testcase == 100 // <<
ST b0;
inline rtype testFunction(vtype const& a, vtype const& b) {
b0 = b[0];
int c = b0 & (sizeof(ST) * 8 - 1);
return a << c;
}
RT referenceFunction(ST a, ST b) {
int c = b0 & (sizeof(ST) * 8 - 1);
return a << c;
}
#elif testcase == 101 // >>
ST b0;
inline rtype testFunction(vtype const& a, vtype const& b) {
b0 = b[0];
int c = b0 & (sizeof(ST) * 8 - 1);
return a >> c;
}
RT referenceFunction(ST a, ST b) {
int c = b0 & (sizeof(ST) * 8 - 1);
return a >> c;
}
#elif testcase == 102 // rotate_left (signed integers only)
#define WHOLE_VECTOR
ST b0;
inline rtype testFunction(vtype const& a, vtype const& b) {
b0 = b[0];
int s = sizeof(ST) * 8; // size in bits
int c = b0 & (s - 1);
vtype r = rotate_left(a, c);
return r;
}
// overloaded functions needed for converting all signed types to unsigned
uint8_t rotleft(uint8_t a, uint8_t b) { return a << b | a >> (8 - b); }
uint16_t rotleft(uint16_t a, uint16_t b) { return a << b | a >> (16 - b); }
uint32_t rotleft(uint32_t a, uint32_t b) { return a << b | a >> (32 - b); }
uint64_t rotleft(uint64_t a, uint64_t b) { return a << b | a >> (64 - b); }
int8_t rotleft(int8_t a, int8_t b) { return rotleft(uint8_t(a), uint8_t(b)); }
int16_t rotleft(int16_t a, int16_t b) { return rotleft(uint16_t(a), uint16_t(b)); }
int32_t rotleft(int32_t a, int32_t b) { return rotleft(uint32_t(a), uint32_t(b)); }
int64_t rotleft(int64_t a, int64_t b) { return rotleft(uint64_t(a), uint64_t(b)); }
rtype referenceFunction(vtype const a, vtype const b) {
b0 = b[0];
int s = sizeof(ST) * 8; // size in bits
int c = b0 & (s - 1);
ST res[64];
for (int i = 0; i < a.size(); i++) {
res[i] = rotleft(a[i], (ST)c);
}
return vtype().load(res);
}
#elif testcase == 103 // add_saturated
inline rtype testFunction(vtype const& a, vtype const& b) {
return add_saturated(a, b);
}
RT referenceFunction(ST a, ST b) {
ST sum = a + b;
if (ST(-1) < 0) { // signed type
if (!((a < 0) ^ (b < 0)) && ((a < 0) ^ (sum < 0))) { // overflow
sum = (1ull << (sizeof(ST) * 8 - 1)) - 1 + (sum >= 0);
}
}
else { // unsigned type
if (sum < a) { // overflow
sum = ST(-1);
}
}
return sum;
}
#elif testcase == 104 // sub_saturated
inline rtype testFunction(vtype const& a, vtype const& b) { return sub_saturated(a, b); }
RT referenceFunction(ST a, ST b) {
ST dif = a - b;
if (ST(-1) < 0) { // signed type
if (((a < 0) ^ (b < 0)) && ((a < 0) ^ (dif < 0))) { // overflow
dif = ((uint64_t)1 << (sizeof(ST) * 8 - 1)) - 1 + (dif >= 0);
}
}
else { // unsigned type
if (b > a) { // underflow
dif = ST(0);
}
}
return dif;
}
#elif testcase == 105 // abs_saturated
inline rtype testFunction(vtype const& a, vtype const& b) { return abs_saturated(b); }
RT referenceFunction(ST a, ST b) {
volatile ST r = abs(b); // volatile to prevent the compiler from optimizing away overflow check
if (r < 0) return b - 1;
return r;
}
#elif testcase == 106 // &
inline rtype testFunction(vtype const& a, vtype const& b) { return a & b; }
RT referenceFunction(ST a, ST b) { return a & b; }
#elif testcase == 107 // |
inline rtype testFunction(vtype const& a, vtype const& b) { return a | b;}
RT referenceFunction(ST a, ST b) { return a | b; }
#elif testcase == 108 // ^
inline rtype testFunction(vtype const& a, vtype const& b) { return a ^ b; }
RT referenceFunction(ST a, ST b) { return a ^ b; }
#elif testcase == 109 // ~
inline rtype testFunction(vtype const& a, vtype const& b) { return a & ~b; }
RT referenceFunction(ST a, ST b) { return a & ~b; }
#elif testcase == 110 // andnot (bool vectors only)
inline rtype testFunction(vtype const& a, vtype const& b) { return andnot(a, b); }
RT referenceFunction(ST a, ST b) { return a & ~b; }
// ----------------------------------------------------------------------------
// floating point only cases
// (round, sqrt, pow, etc. are in the math testbench)
// ----------------------------------------------------------------------------
#elif testcase == 200 // sign_combine
inline rtype testFunction(vtype const& a, vtype const& b) { return sign_combine(a, b); }
// define signbit function because Visual studio is missing it
bool signbit_(float x) {
union { float f; uint32_t i; } u;
u.f = x; return u.i >> 31 != 0;
}
bool signbit_(double x) {
union { double f; uint64_t i; } u;
u.f = x; return u.i >> 63 != 0;
}
RT referenceFunction(ST a, ST b) {
return signbit_(b) ? -a : a;
}
#elif testcase == 202 // square
inline rtype testFunction(vtype const& a, vtype const& b) { return square(b); }
RT referenceFunction(ST a, ST b) {
return b * b;
}
#elif testcase == 210 // is_finite
inline rtype testFunction(vtype const& a, vtype const& b) { return is_finite(a); }
RT referenceFunction(ST a, ST b) {
union {
ST f;
uint32_t i;
uint64_t q;
} ua;
ua.f = a;
if (vtype::elementtype() == 16) {
return (ua.i & 0x7F800000) != 0x7F800000;
}
if (vtype::elementtype() == 17) {
return (ua.i & 0x7FF0000000000000) != 0x7FF0000000000000;
}
return 0; // wrong type
}
#elif testcase == 211 // is_inf
inline rtype testFunction(vtype const& a, vtype const& b) { return is_inf(a); }
RT referenceFunction(ST a, ST b) {
union {
ST f;
uint32_t i;
uint64_t q;
} ua;
ua.f = a;
if (vtype::elementtype() == 16) {
return (ua.i & 0x7FFFFFFF) == 0x7F800000;
}
if (vtype::elementtype() == 17) {
return (ua.i & 0x7FFFFFFFFFFFFFFF) == 0x7FF0000000000000;
}
return 0; // wrong type
}
#elif testcase == 212 // is_nan
inline rtype testFunction(vtype const& a, vtype const& b) { return is_nan(a); }
RT referenceFunction(ST a, ST b) {
union {
ST f;
uint32_t i;
uint64_t q;
} ua;
ua.f = a;
if (vtype::elementtype() == 16) {
return (ua.i & 0x7FFFFFFF) > 0x7F800000;
}
if (vtype::elementtype() == 17) {
return (ua.i & 0x7FFFFFFFFFFFFFFF) > 0x7FF0000000000000;
}
return 0; // wrong type
}
#elif testcase == 213 // is_subnormal
inline rtype testFunction(vtype const& a, vtype const& b) { return is_subnormal(a); }
RT referenceFunction(ST a, ST b) {
union {
ST f;
uint32_t i;
uint64_t q;
} ua;
ua.f = a;
if (vtype::elementtype() == 16) {
return (ua.i & 0x7F800000) == 0 && (ua.i & 0x007FFFFF) != 0;
}
if (vtype::elementtype() == 17) {
return (ua.i & 0x7FF0000000000000) == 0 && (ua.i & 0x000FFFFFFFFFFFFF) != 0;
}
return 0; // wrong type
}
#elif testcase == 214 // is_zero_or_subnormal
inline rtype testFunction(vtype const& a, vtype const& b) { return is_zero_or_subnormal(a); }
RT referenceFunction(ST a, ST b) {
union {
ST f;
uint32_t i;
uint64_t q;
} ua;
ua.f = a;
if (vtype::elementtype() == 16) {
return (ua.i & 0x7F800000) == 0;
}
if (vtype::elementtype() == 17) {
return (ua.i & 0x7FF0000000000000) == 0;
}
return 0; // wrong type
}
#elif testcase == 220 // infinite4f
inline rtype testFunction(vtype const& a, vtype const& b) { return infinite4f(); }
RT referenceFunction(ST a, ST b) {
union {
ST f;
uint32_t i;
uint64_t q;
} ua;
ua.q = 0;
if (vtype::elementtype() == 16) ua.i = 0x7F800000;
if (vtype::elementtype() == 17) ua.q = 0x7FF0000000000000;
return ua.f;
}
#elif testcase == 221 // nan_vec
inline rtype testFunction(vtype const& a, vtype const& b) { return nan_vec<vtype>(5); }
RT referenceFunction(ST a, ST b) {
union {
ST f;
uint32_t i;
uint64_t q;
} ua;
ua.q = 0;
if (vtype::elementtype() == 16) ua.i = 0x7FC00005;
if (vtype::elementtype() == 17) ua.q = 0x7FF8000000000005;
return ua.f;
}
// ----------------------------------------------------------------------------
// boolean result cases
// ----------------------------------------------------------------------------
#elif testcase == 300 // <
inline rtype testFunction(vtype const& a, vtype const& b) { return a < b; }
RT referenceFunction(ST a, ST b) { return a < b; }
#elif testcase == 301 // <=
inline rtype testFunction(vtype const& a, vtype const& b) { return a <= b; }
RT referenceFunction(ST a, ST b) { return a <= b; }
#elif testcase == 302 // ==
inline rtype testFunction(vtype const& a, vtype const& b) { return a == b; }
RT referenceFunction(ST a, ST b) { return a == b; }
#elif testcase == 303 // !=
inline rtype testFunction(vtype const& a, vtype const& b) { return a != b; }
RT referenceFunction(ST a, ST b) { return a != b; }
#elif testcase == 304 // >=
inline rtype testFunction(vtype const& a, vtype const& b) { return a >= b; }
RT referenceFunction(ST a, ST b) { return a >= b; }
#elif testcase == 305 // >
inline rtype testFunction(vtype const& a, vtype const& b) { return a > b; }
RT referenceFunction(ST a, ST b) { return a > b; }
#elif testcase == 306 // sign_bit (float types only)
inline rtype testFunction(vtype const& a, vtype const& b) { return sign_bit(b); }
// define signbit function because Visual studio is missing it
bool signbit_(float x) {
union { float f; uint32_t i; } u;
u.f = x; return u.i >> 31 != 0;
}
bool signbit_(double x) {
union { double f; uint64_t i; } u;
u.f = x; return u.i >> 63 != 0;
}
RT referenceFunction(ST a, ST b) {
return signbit_(b);
}
// ----------------------------------------------------------------------------
// scalar result cases
// ----------------------------------------------------------------------------
#elif testcase == 400 // horizontal_add
#define SCALAR_RESULT
#define FACCURACY 4 // accept accumulating rounding errors
inline rtype testFunction(vtype const& a, vtype const& b) {
return rtype(horizontal_add(b));
}
RT referenceFunction(vtype const& a, vtype const& b) {
ST sum = 0;
for (int i = 0; i < b.size(); i++) sum += b[i];
return sum;
}
#elif testcase == 401 // horizontal_add_x
#define SCALAR_RESULT
#define FACCURACY 4 // accept accumulating rounding errors
inline rtype testFunction(vtype const& a, vtype const& b) { return horizontal_add_x(b); }
RT referenceFunction(vtype const& a, vtype const& b) {
RT sum = 0;
for (int i = 0; i < b.size(); i++) sum += (RT)b[i];
return sum;
}
#elif testcase == 402 // horizontal_and
#define SCALAR_RESULT
inline rtype testFunction(vtype const& a, vtype const& b) { return rtype(horizontal_and(b)); }
RT referenceFunction(vtype const& a, vtype const& b) {
RT sum = true;
for (int i = 0; i < b.size(); i++) sum = sum && b[i];
return sum;
}
#elif testcase == 403 // horizontal_or
#define SCALAR_RESULT
inline rtype testFunction(vtype const& a, vtype const& b) { return rtype(horizontal_or(b)); }
RT referenceFunction(vtype const& a, vtype const& b) {
RT sum = false;
for (int i = 0; i < b.size(); i++) sum = sum || b[i];
return sum;
}
#elif testcase == 404 // horizontal_min
#define SCALAR_RESULT
inline rtype testFunction(vtype const& a, vtype const& b) { return rtype(horizontal_min(b)); }
RT referenceFunction(vtype const& a, vtype const& b) {
ST m = b[0];
for (int i = 0; i < b.size(); i++) {
if (b[i] != b[i]) return b[i]; // NAN
if (b[i] < m) m = b[i];
}
return m;
}
#elif testcase == 405 // horizontal_max
#define SCALAR_RESULT
inline rtype testFunction(vtype const& a, vtype const& b) { return rtype(horizontal_max(b)); }
RT referenceFunction(vtype const& a, vtype const& b) {
ST m = b[0];
for (int i = 0; i < b.size(); i++) {
if (b[i] != b[i]) return b[i]; // NAN
if (b[i] > m) m = b[i];
}
return m;
}
#elif testcase == 410 // horizontal_find_first
#define SCALAR_RESULT // integer result
inline rtype testFunction(vtype const& a, vtype const& b) {return rtype(horizontal_find_first(vtype(b))); }
RT referenceFunction(vtype const& a, vtype const& b) {
RT sum = -1;
for (int i = 0; i < b.size(); i++) {
if (b[i] && sum < 0) sum = i;
}
return sum;
}
#elif testcase == 411 // horizontal_count
#define SCALAR_RESULT
inline rtype testFunction(vtype const& a, vtype const& b) { return horizontal_count(b); }
RT referenceFunction(vtype const& a, vtype const& b) {
RT sum = 0;
for (int i = 0; i < b.size(); i++) {
if (b[i]) sum++;
}
return sum;
}
#elif testcase == 412 // to_bits
#define SCALAR_RESULT
inline rtype testFunction(vtype const& a, vtype const& b) { return to_bits(b); }
RT referenceFunction(vtype const& a, vtype const& b) {
RT r = 0;
for (int i = 0; i < b.size(); i++) {
r |= RT(b[i]) << i;
}
return r;
}
#elif testcase == 413 // load_bits. (vtype is the type to test, rtype is the same)
inline rtype testFunction(vtype const& f, vtype const& a, vtype const& b) {
rtype r;
if (vtype::size() <= 8) {
r.load_bits((uint8_t)bitfield);
}
else if (vtype::size() <= 16) {
r.load_bits((uint16_t)bitfield);
}
else if (vtype::size() <= 32) {
r.load_bits((uint32_t)bitfield);
}
else {
r.load_bits(bitfield); // ignore warnings here
}
return r;
}
rtype referenceFunction(vtype const& a, vtype const& b) {
rtype r(false);
for (int i=0; i < r.size(); i++) {
r.insert(i, (bitfield >> i) & 1);
}
return r;
}
#define WHOLE_VECTOR // test whole vector, not individual elements
#define USE_FLAG // use bitfield
// ----------------------------------------------------------------------------
// type conversion functions
// ----------------------------------------------------------------------------
#elif testcase == 500 // direct conversion between integer vectors with same total number of bits
inline rtype testFunction(vtype const& a, vtype const& b) { return rtype(b); }
rtype referenceFunction(vtype const& a, vtype const& b) {
int8_t temp[maxvectorsize];
rtype r;
b.store(temp);
r.load(temp);
return r;
}
#define WHOLE_VECTOR // test whole vector, not individual elements
#elif testcase == 501 // direct conversion between boolean vectors with same number of elements
// possible for compact boolean vectors. not always possible for big boolean vectors
inline rtype testFunction(vtype const& a, vtype const& b) { return rtype(b); }
rtype referenceFunction(vtype const& a, vtype const& b) {
uint64_t temp = to_bits(b);
rtype r;
if (vtype::size() <= 8) {
r.load_bits((uint8_t)temp);
}
else if (vtype::size() <= 16) {
r.load_bits((uint16_t)temp);
}
else if (vtype::size() <= 32) {
r.load_bits((uint32_t)temp);
}
else {
r.load_bits(temp); // ignore warnings here
}
return r;
}
#define WHOLE_VECTOR // test whole vector, not individual elements
#elif testcase == 502 // reinterpret_i
// float or double to int
inline rtype testFunction(vtype const& a, vtype const& b) { return reinterpret_i(b); }
rtype referenceFunction(vtype a, vtype b) {
int8_t temp[maxvectorsize];
rtype r;
b.store((ST*)temp);
r.load(temp);
return r;
}
#define WHOLE_VECTOR // test whole vector, not individual elements
#elif testcase == 503 // reinterpret_f
// int to float
inline rtype testFunction(vtype const& a, vtype const& b) { return reinterpret_f(b); }
rtype referenceFunction(vtype a, vtype b) {
int8_t temp[maxvectorsize];
rtype r;
b.store((ST*)temp);
r.load((RT*)temp);
return r;
}
#define WHOLE_VECTOR // test whole vector, not individual elements
#elif testcase == 504 // reinterpret_d
// int to double
inline rtype testFunction(vtype const& a, vtype const& b) { return reinterpret_d(b); }
rtype referenceFunction(vtype a, vtype b) {
int8_t temp[maxvectorsize];
rtype r;
b.store((ST*)temp);
r.load((RT*)temp);
return r;
}
#define WHOLE_VECTOR // test whole vector, not individual elements
#elif testcase == 505 // roundi
inline rtype testFunction(vtype const& a, vtype const& b) { return roundi(b); }
RT referenceFunction(ST a, ST b) {
RT r;
if (b >= 0) {
r = (RT)(b + 0.5); // 0.5 rounds up
if ((r & 1) && r - b == 0.5) r--; // round down if result is odd
}
else {
r = (RT)(b - 0.5); // 0.5 rounds up
if ((r & 1) && b - r == 0.5) r++; // round up if result is odd
}
return r;
}
#elif testcase == 506 // truncatei
inline rtype testFunction(vtype const& a, vtype const& b) { return truncatei(b); }
RT referenceFunction(ST a, ST b) {
RT r;
if (b >= 0) {
r = (RT)(b); // truncate towards zero
}
else {
r = -(RT)(-b);
}
return r;
}
#elif testcase == 507 // round_to_int32
inline rtype testFunction(vtype const& a, vtype const& b) { return round_to_int32(b); }
rtype referenceFunction(vtype a, vtype b) {
ST x; RT r; rtype y(0);
int vsize = vtype::size(); // size of input vector
int rsize = rtype::size(); // size of output vector
int i;
for (i = 0; i < vsize && i < rsize; i++) {
x = b[i];
if (x >= 0) {
r = (RT)(x + 0.5); // 0.5 rounds up
if ((r & 1) && r - x == 0.5) r--; // round down if result is odd
}
else {
r = (RT)(x - 0.5); // 0.5 rounds up
if ((r & 1) && x - r == 0.5) r++; // round up if result is odd
}
y.insert(i, r);
}
return y;
}
#define WHOLE_VECTOR // test whole vector, not individual elements
#elif testcase == 508 // truncate_to_int32
inline rtype testFunction(vtype const& a, vtype const& b) { return truncate_to_int32(b); }
rtype referenceFunction(vtype a, vtype b) {
ST x; RT r; rtype y(0);
int vsize = vtype::size(); // size of input vector
int rsize = rtype::size(); // size of output vector
int i;
for (i = 0; i < vsize && i < rsize; i++) {
x = b[i];
if (x >= 0) {
r = (RT)(x); // truncate towards zero
}
else {
r = -(RT)(-x);
}
y.insert(i, r);
}
return y;
}
#define WHOLE_VECTOR // test whole vector, not individual elements
#elif testcase == 509 // round_to_int32 with two parameters
inline rtype testFunction(vtype const& a, vtype const& b) { return round_to_int32(a,b); }
rtype referenceFunction(vtype a, vtype b) {
ST x; RT r; rtype y(0);
int vsize = vtype::size(); // size of input vector
int rsize = rtype::size(); // size of output vector
int i;
for (i = 0; i < rsize; i++) {
if (i < vsize) {
x = a[i];
}
else {
x = b[i-vsize];
}
if (x >= 0) {
r = (RT)(x + 0.5); // 0.5 rounds up
if ((r & 1) && r - x == 0.5) r--; // round down if result is odd
}
else {
r = (RT)(x - 0.5); // 0.5 rounds up
if ((r & 1) && x - r == 0.5) r++; // round up if result is odd
}
y.insert(i, r);
}
return y;
}
#define WHOLE_VECTOR // test whole vector, not individual elements
#elif testcase == 510 // truncate_to_int32 with two parameters
inline rtype testFunction(vtype const& a, vtype const& b) { return truncate_to_int32(a,b); }
rtype referenceFunction(vtype a, vtype b) {
ST x; RT r; rtype y(0);
int vsize = vtype::size(); // size of input vector