-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtestbench4.cpp
2210 lines (1943 loc) · 71.4 KB
/
testbench4.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
/**************************** testbench4.cpp *******************************
* Author: Agner Fog
* Date created: 2019-04-09
* Last modified: 2022-07-20
* Version: 2.02.00
* 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
* on half-precision floating point vectors.
* 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 // fails in emulator for Vec16h unless -ffp-model=strict
10: min
11: maximum (floating point types)
12: minimum (floating point types)
13: abs
14: if_add // clang compilation fails if -ffp-model=strict
15: if_sub
16: if_mul
17: if_div (floating point types)
18: select
20: store_a
106: operator &
107: operator |
108: operator ^
109: operator !
200: sign_combine (float types)
201: change_sign
202: square (float types)
210: is_finite
211: is_inf
212: is_nan
213: is_subnormal
214: is_zero_or_subnormal
220: infinite8h
221: nan_vec
222: nan_code
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 and half precision)
404: horizontal_min
405: horizontal_max
501: reinterpret_h
502: reinterpret_i
503: reinterpret_f
504: reinterpret_d
505: roundi
506: truncatei
507: round
508: truncate
509: floor
510: ceil
519: sqrt
520: approx_recipr
521: approx_rsqrt
530: mul_add
531: mul_sub
532: nmul_add
540: exponent
541: fraction
542: pow_n
543: exp2
550: exp
551: exp2
552: exp10
553: expm1
560: sin
561: cos
562: tan
563: sincos
570: sinpi
571: cospi
572: tanpi
573: sincospi
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 (two integer parameters)
611: compress_saturated (two integer parameters, integer types)
612: insert(index, value)
613: extract(index)
614: cutoff(index)
615: load_partial(index, p)
616: store_partial(index, p)
620: to_float16 (int16_t to float16)
621: to_float16 (uint16_t to float16)
622: Vec8h -> Vec8f
623: Vec8f -> Vec8h
624: Vec8h -> Vec8f -> Vec8h
650: constructor with all elements
900: test supported instruction sets
*****************************************************************************/
#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
#if INSTRSET >= 9 && !defined(__F16C__)
#define __F16C__
#endif
#ifndef __AVX512FP16__
//#define __AVX512FP16__
#endif
//#define __AVX512VBMI2__
#include <vectorclass.h> // vector class library
#include <vectorfp16.h> // half precision vectors
#ifndef testcase
// ----------------------------------------------------------------------------
// Specify input parameters here if running from an IDE:
// ----------------------------------------------------------------------------
#define testcase 1
#define vtype Vec16h
#define rtype vtype
//#define rtype Vec16s
#define seed 1
#define EXHAUSTIVE_TEST
#define TESTNAN
#else
// ----------------------------------------------------------------------------
// Default input parameters when compiling from a script
// ----------------------------------------------------------------------------
// input or index vector type to be tested
#ifndef vtype
#define vtype Vec8h
#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
#if testcase == 900
#include "instrset_detect.cpp"
#endif
// ----------------------------------------------------------------------------
// 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
// ----------------------------------------------------------------------------
// Overhead functions
// ----------------------------------------------------------------------------
const int maxerrors = 10; // maximum errors to report
int numerr = 0; // count errors
// type-specific load function
template <typename T, typename E>
inline void loadData(T & x, E const* p) {
x.load(p);
}
template <typename T>
inline void loadData(T & x, bool const* p) {
x = false;
for (int i = 0; i < x.size(); i++) {
x.insert(i, p[i]); // bool vectors have no load function
}
}
// ----------------------------------------------------------------------------
// conversions Float16 <-> float
// ----------------------------------------------------------------------------
// type Float16 emulates _Float16 if _Float16 not defined (vectorfp16e.h)
// convert float to user-defined type Float16
#ifdef __AVX512FP16__
uint16_t to_float16(float x, bool supportSubnormal = true) {
return _mm_cvtsi128_si32(_mm_castph_si128(_mm_cvtss_sh(_mm_setzero_ph(), _mm_load_ss(&x))));
}
float to_float(uint32_t half, bool supportSubnormal = true) {
return _mm_cvtss_f32(_mm_cvtsh_ss (_mm_setzero_ps(),_mm_castsi128_ph(_mm_cvtsi32_si128(half))));
}
// Float16 is defined. Define bit-casting between uint16_t <-> Float16
static inline uint16_t castfp162s(Float16 x) {
union {
Float16 f;
uint16_t i;
} u;
u.f = x;
return u.i;
}
static inline Float16 casts2fp16(uint16_t x) {
union {
uint16_t i;
Float16 f;
} u;
u.i = x;
return u.f;
}
#else
// half to float conversion functions
// Convert half precision floating point number to single precision
// Optional support for subnormals
// NAN payload is right-justified for ForwardCom
float to_float(uint32_t half, bool supportSubnormal = true) {
union {
uint32_t hhh;
float fff;
struct {
uint32_t mant: 23;
uint32_t expo: 8;
uint32_t sign: 1;
};
} u;
u.hhh = (half & 0x7fff) << 13; // Exponent and mantissa
u.hhh += 0x38000000; // Adjust exponent bias
if ((half & 0x7C00) == 0) {// Subnormal
if (supportSubnormal) {
u.hhh = 0x3F800000 - (24 << 23); // 2^-24
u.fff *= int(half & 0x3FF); // subnormal value = mantissa * 2^-24
}
else {
u.hhh = 0; // make zero
}
}
if ((half & 0x7C00) == 0x7C00) { // infinity or nan
u.expo = 0xFF;
if (half & 0x3FF) { // nan
u.mant = 1 << 22 | (half & 0x1FF); // NAN payload is right-justified only in ForwardCom
}
}
u.hhh |= (half & 0x8000) << 16; // sign bit
return u.fff;
}
// Convert floating point number to half precision.
// Round to nearest or even.
// Optional support for subnormals
// NAN payload is right-justified
uint16_t to_float16(float x, bool supportSubnormal = true) {
union { // single precision float
float f;
struct {
uint32_t mant: 23;
uint32_t expo: 8;
uint32_t sign: 1;
};
} u;
union { // half precision float
uint16_t h;
struct {
uint16_t mant: 10;
uint16_t expo: 5;
uint16_t sign: 1;
};
} v;
u.f = x;
v.expo = u.expo - 0x70; // adjust exponent bias
v.mant = u.mant >> 13; // get upper part of mantissa
if (u.mant & (1 << 12)) { // round to nearest or even
if ((u.mant & ((1 << 12) - 1)) || (v.mant & 1)) { // round up if odd or remaining bits are nonzero
v.h++; // overflow will carry into exponent and sign
}
}
v.sign = u.sign;
if (u.expo == 0xFF) { // infinity or nan
v.expo = 0x1F;
if (u.mant != 0) { // Nan
v.mant = (u.mant & 0x1FF) | 0x200; // NAN payload is right-justified only in ForwardCom
}
}
else if (u.expo > 0x8E) {
v.expo = 0x1F; v.mant = 0; // overflow -> inf
}
else if (u.expo < 0x71) {
v.expo = 0;
if (supportSubnormal) {
u.expo += 24;
u.sign = 0;
uint16_t mants = _mm_cvt_ss2si(_mm_load_ss(&u.f));
v.mant = mants & 0x3FF; // proper rounding of subnormal
if (mants == 0x400) v.expo = 1; // round up to normal
}
else {
v.mant = 0; // underflow -> 0
}
}
return v.h;
}
#endif
// convert float to user-defined type Float16
Float16 float2fp16(float x) {
return casts2fp16(to_float16(x));
}
// convert uint16_t to Float16 by bit casting
Float16 half2fp16 (uint16_t x) {
return casts2fp16(x);
}
// convert uint16_t to Float16 by bit casting
uint16_t fp162half (Float16 x) {
return castfp162s(x);
}
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;
}
bool signbit_(Float16 x) {
return (fp162half(x) & 0x8000) != 0;
}
/************************************************************************
*
* 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;
/*
float p = float(a) * float(b);
uint32_t p1 = to_float16(p);
union {
uint16_t h;
Float16 f;
} u;
u.h = p1;
return u.f;
*/
}
#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 == 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 > ST(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(ST(0)), a, b);
}
RT referenceFunction(ST f, ST a, ST b) { return f > ST(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(ST(1)), a, b);
}
RT referenceFunction(ST f, ST a, ST b) { return f < ST(1) ? 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(ST(0)), a, b);
}
RT referenceFunction(ST f, ST a, ST b) {
return f > ST(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(ST(1)), a, b);
}
RT referenceFunction(ST f, ST a, ST b) { return f < ST(1) ? a/b : a; }
#define USE_FLAG
#elif testcase == 18 // select
inline rtype testFunction(vtype const& f, vtype const& a, vtype const& b) {
return select(f < vtype(ST(1)), a, b);
}
RT referenceFunction(ST f, ST a, ST b) { return f < ST(1) ? a : b; }
#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; }
#elif testcase == 106 // operator &
inline rtype testFunction(vtype const& a, vtype const& b) { return a & b; }
RT referenceFunction(ST a, ST b) {
return casts2fp16(castfp162s(a) & castfp162s(b));
}
#elif testcase == 107 // operator |
inline rtype testFunction(vtype const& a, vtype const& b) { return a | b; }
RT referenceFunction(ST a, ST b) {
return casts2fp16(castfp162s(a) | castfp162s(b));
}
#elif testcase == 108 // operator ^
inline rtype testFunction(vtype const& a, vtype const& b) { return a ^ b; }
RT referenceFunction(ST a, ST b) {
return casts2fp16(castfp162s(a) ^ castfp162s(b));
}
#elif testcase == 109 // operator !
inline rtype testFunction(vtype const& a, vtype const& b) { return !b; }
RT referenceFunction(ST a, ST b) {
return b == ST(0.);
}
// ----------------------------------------------------------------------------
// 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
RT referenceFunction(ST a, ST b) {
return signbit_(b) ? -a : a;
}
#elif testcase == 201 // change_sign
static int aindex = 0; // index to vector element
/* works with clang, not with g++:
template <int n> rtype testFunction1(vtype const& a) {
aindex = 0;
rtype y = 0;
if constexpr (n == 8) y = change_sign<1,0,0,0,0,1,0,0>(a);
if constexpr (n == 16) y = change_sign<1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1>(a);
if constexpr (n == 32) y = change_sign<1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0>(a);
return y;
} */
inline Vec8h testFunction(Vec8h const& a, Vec8h const& b) {
aindex = 0;
return change_sign<1,0,0,0,0,1,0,0>(a);
}
inline Vec16h testFunction(Vec16h const& a, Vec16h const& b) {
aindex = 0;
return change_sign<1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1>(a);
}
inline Vec32h testFunction(Vec32h const& a, Vec32h const& b) {
aindex = 0;
return change_sign<1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0>(a);
}
RT referenceFunction(ST a, ST b) {
if (aindex++ % 5 == 0) return -a; else return 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) {
uint16_t ai = fp162half(a);
if ((ai & 0x7C00) != 0x7C00) return 1; else return 0;
}
#elif testcase == 211 // is_inf
inline rtype testFunction(vtype const& a, vtype const& b) { return is_inf(a); }
RT referenceFunction(ST a, ST b) {
uint16_t ai = fp162half(a);
if ((ai & 0x7C00) == 0x7C00 && (ai & 0x03FF) == 0) return 1; else return 0;
}
#elif testcase == 212 // is_nan
inline rtype testFunction(vtype const& a, vtype const& b) { return is_nan(a); }
RT referenceFunction(ST a, ST b) {
uint16_t ai = fp162half(a);
if ((ai & 0x7C00) == 0x7C00 && (ai & 0x03FF) != 0) return 1; else return 0;
}
#elif testcase == 213 // is_subnormal
inline rtype testFunction(vtype const& a, vtype const& b) { return is_subnormal(a); }
RT referenceFunction(ST a, ST b) {
uint16_t ai = fp162half(a);
if ((ai & 0x7C00) == 0 && (ai & 0x03FF) != 0) return 1; else return 0;
}
#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) {
uint16_t ai = fp162half(a);
if ((ai & 0x7C00) == 0) return 1; else return 0;
}
#elif testcase == 220 // infinite8h
template <int n> auto testFunction1(vtype const& a) {
if constexpr (n == 8) return infinite8h();
if constexpr (n == 16) return infinite16h();
if constexpr (n == 32) return infinite32h();
}
inline rtype testFunction(vtype const& a, vtype const& b) {
return testFunction1<vtype::size()>(a);
}
RT referenceFunction(ST a, ST b) {
return half2fp16(0x7C00);
}
#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) {
return half2fp16(0x7E05);
}
#elif testcase == 222 // nan_code
inline rtype testFunction(vtype const& a, vtype const& b) {
return nan_code(a);
}
RT referenceFunction(ST a, ST b) {
uint16_t k = fp162half(a) ;
if ((k & 0x7C00) != 0x7C00) return 0; // not NAN
return k & 0x3FF;
}
// ----------------------------------------------------------------------------
// 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 float(a) < float(b); }
#elif testcase == 301 // <=
inline rtype testFunction(vtype const& a, vtype const& b) { return a <= b; }
RT referenceFunction(ST a, ST b) { return float(a) <= float(b); }
#elif testcase == 302 // ==
inline rtype testFunction(vtype const& a, vtype const& b) { return a == b; }
RT referenceFunction(ST a, ST b) { return float(a) == float(b); }
#elif testcase == 303 // !=
inline rtype testFunction(vtype const& a, vtype const& b) { return a != b; }
RT referenceFunction(ST a, ST b) { return float(a) != float(b); }
#elif testcase == 304 // >=
inline rtype testFunction(vtype const& a, vtype const& b) { return a >= b; }
RT referenceFunction(ST a, ST b) { return float(a) >= float(b); }
#elif testcase == 305 // >
inline rtype testFunction(vtype const& a, vtype const& b) { return a > b; }
RT referenceFunction(ST a, ST b) { return float(a) > float(b); }
#elif testcase == 306 // sign_bit (float types only)
inline rtype testFunction(vtype const& a, vtype const& b) { return sign_bit(b); }
RT referenceFunction(ST a, ST b) {
return signbit_(b);
}
// ----------------------------------------------------------------------------
// scalar result cases
// ----------------------------------------------------------------------------
#elif testcase == 400 // horizontal_add
#define SCALAR_RESULT
#define FACCURACY 8 // accept accumulating rounding errors
inline rtype testFunction(vtype const& a, vtype const& b) {
return rtype(horizontal_add(a));
}
RT referenceFunction(vtype const& a, vtype const& b) {
float sum = 0;
for (int i = 0; i < a.size(); i++) sum += float(a[i]);
return sum;
/*
RT sss[vtype::size()];
int i;
for (i = 0; i < vtype::size(); i++) sss[i] = a[i];
if constexpr (vtype::size() >= 32) {
for (i = 0; i < 16; i++) sss[i] = sss[i] + sss[i+16];
}
if constexpr (vtype::size() >= 16) {
for (i = 0; i < 8; i++) sss[i] = sss[i] + sss[i+8];
}
for (i = 0; i < 4; i++) sss[i] = sss[i] + sss[i+4];
for (i = 0; i < 2; i++) sss[i] = sss[i] + sss[i+2];
sss[0] = sss[0] + sss[1];
return sss[0];*/
}
#elif testcase == 401 // horizontal_add_x
#define SCALAR_RESULT
#define FACCURACY 30 // 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) {
double sum = 0;
for (int i = 0; i < b.size(); i++) sum += double(float(b[i]));
return RT(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;
}
// ----------------------------------------------------------------------------
// type conversion functions
// ----------------------------------------------------------------------------
#elif testcase == 501 // reinterpret_h
// int to float16
inline rtype testFunction(vtype const& a, vtype const& b) { return reinterpret_h(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 == 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 & a, vtype const& b) {
rtype r = roundi(a);
// work around problem with VCVTPH2W(INF) gives 0x8000
rtype r2 = select(r == rtype(-0x8000) && a > vtype(ST(0)), rtype(0x7FFF), r);
return r2;
}
RT referenceFunction(ST a, ST b) {
RT r;
uint16_t ai = fp162half(a);
float af = a;
if (af >= float( 0x7FFF)) return 0x7FFF; // overflow saturates
if (af < float(-0x8000)) return 0x8000; // negative overflow saturates
if ((ai & 0x7C00) == 0x7C00) { // infinity or nan
return 0x8000; // NAN
}
if (af >= 0) {
r = (RT)(af + 0.5); // 0.5 rounds up
if ((r & 1) && r - af == 0.5) r--; // round down if result is odd
}
else {
r = (RT)(af - 0.5); // 0.5 rounds up
if ((r & 1) && af - 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) {
rtype r = truncatei(a);
// work around problem with vcvttps2dq VCVTPH2W(INF) gives 0x8000
rtype r2 = select(r == rtype(-0x8000) && a > vtype(ST(0)), rtype(0x7FFF), r);
return r2;
}
RT referenceFunction(ST a, ST b) {
RT r;
uint16_t ai = fp162half(a);
float af = a;
if (af >= float( 0x7FFF)) return 0x7FFF; // overflow saturates
if (af < float(-0x8000)) return 0x8000; // negative overflow saturates
if ((ai & 0x7C00) == 0x7C00) { // infinity or nan
return 0x8000; // NAN
}
if (af >= 0) {
r = (RT)(af); // truncate towards zero
}
else {
r = -(RT)(-af);
}
return r;
}
#elif testcase == 507 // round
inline rtype testFunction(vtype const& a, vtype const& b) { return round(a); }
RT referenceFunction(ST a, ST b) {
int r;
uint16_t ai = fp162half(a);
float af = float(a);
if ((ai & 0x7C00) == 0x7C00) { // infinity or nan
return a;
}
if (ai == 0x8000) return a; // -0
if (af >= 0) {
r = int(af + 0.5f); // 0.5 rounds up
if ((r & 1) && r - af == 0.5f) r--; // round down if result is odd
}
else {
r = int(af - 0.5f); // 0.5 rounds up
if ((r & 1) && af - r == 0.5) r++; // round up if result is odd
}
return RT(float(r));
}
#elif testcase == 508 // truncate
inline rtype testFunction(vtype const& a, vtype const& b) { return truncate(a); }
RT referenceFunction(ST a, ST b) {
int r;
uint16_t ai = fp162half(a);
float af = a;
if ((ai & 0x7C00) == 0x7C00) { // infinity or nan
return a;
}
if (ai == 0x8000) return a; // -0
r = int(af); // truncate towards zero
return (RT)float(r);
}
#elif testcase == 509 // floor
inline rtype testFunction(vtype const& a, vtype const& b) { return floor(a); }
RT referenceFunction(ST a, ST b) {
int r;
uint16_t ai = fp162half(a);
float af = a;
if ((ai & 0x7C00) == 0x7C00) { // infinity or nan
return a;
}
if (ai == 0x8000) return a; // -0
r = int(af); // truncate towards zero
if (r > af) r--; // round down
return (RT)float(r);
}
#elif testcase == 510 // ceil
inline rtype testFunction(vtype const& a, vtype const& b) { return ceil(a); }
RT referenceFunction(ST a, ST b) {
int r;
uint16_t ai = fp162half(a);
float af = a;
if ((ai & 0x7C00) == 0x7C00) { // infinity or nan
return a;
}
if (ai == 0x8000) return a; // -0
r = int(af); // truncate towards zero
if (r < af) r++; // round up
return (RT)float(r);
}
#elif testcase == 519 // sqrt
inline rtype testFunction(vtype const& a, vtype const& b) { return sqrt(a); }
RT referenceFunction(ST a, ST b) {
float c = sqrtf(float(a));
return RT(c);
}