-
Notifications
You must be signed in to change notification settings - Fork 148
/
vectorfp16.h
2672 lines (2273 loc) · 91.5 KB
/
vectorfp16.h
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
/**************************** vectorfp16.h *******************************
* Author: Agner Fog
* Date created: 2022-05-03
* Last modified: 2023-11-07
* Version: 2.02.02
* Project: vector class library
* Description:
* Header file defining half precision floating point vector classes
* Instruction sets AVX512_FP16 and AVX512VL required
*
* Instructions: see vcl_manual.pdf
*
* The following vector classes are defined here:
* Vec8h Vector of 8 half precision floating point numbers in 128 bit vector
* Vec16h Vector of 16 half precision floating point numbers in 256 bit vector
* Vec32h Vector of 32 half precision floating point numbers in 512 bit vector
*
* This header file defines operators and functions for these vectors.
*
* You need a compiler supporting the AVX512_FP16 instruction to compile for this.
* This code works with the following compilers:
* clang++ version 14.0
* g++ version 12.1 with binutils version 2.34
* Intel c++ compiler version 2022.0
*
* (c) Copyright 2012-2023 Agner Fog.
* Apache License version 2.0 or later.
*****************************************************************************/
#ifndef VECTORFP16_H
#define VECTORFP16_H
#ifndef VECTORCLASS_H
#include "vectorclass.h"
#endif
#if VECTORCLASS_H < 20200
#error Incompatible versions of vector class library mixed
#endif
#if INSTRSET < 10 || !defined(__AVX512FP16__)
// half precision instructions not supported. Use emulation
#include "vectorfp16e.h"
#else
#ifdef VCL_NAMESPACE
namespace VCL_NAMESPACE {
#endif
// type Float16 emulates _Float16 in vectorfp16e.h if _Float16 not defined
#ifdef __STDCPP_FLOAT16_T__
typedef std::float16_t Float16;
#else
typedef _Float16 Float16;
#endif
/*****************************************************************************
*
* Vec8hb: Vector of 8 Booleans for use with Vec8h
*
*****************************************************************************/
typedef Vec8b Vec8hb; // compact boolean vector
/*****************************************************************************
*
* Vec8h: Vector of 8 half precision floating point values
*
*****************************************************************************/
class Vec8h {
protected:
__m128h xmm; // Float vector
public:
// Default constructor:
Vec8h() = default;
// Constructor to broadcast the same value into all elements:
Vec8h(_Float16 f) {
xmm = _mm_set1_ph (f);
}
// Constructor to build from all elements:
Vec8h(_Float16 f0, _Float16 f1, _Float16 f2, _Float16 f3, _Float16 f4, _Float16 f5, _Float16 f6, _Float16 f7) {
xmm = _mm_setr_ph (f0, f1, f2, f3, f4, f5, f6, f7);
}
// Constructor to convert from type __m128 used in intrinsics:
Vec8h(__m128h const x) {
xmm = x;
}
// Assignment operator to convert from type __m128 used in intrinsics:
Vec8h & operator = (__m128h const x) {
xmm = x;
return *this;
}
// Type cast operator to convert to __m128 used in intrinsics
operator __m128h() const {
return xmm;
}
// Member function to load from array (unaligned)
Vec8h & load(void const * p) {
xmm = _mm_loadu_ph (p);
return *this;
}
// Member function to load from array, aligned by 16
// You may use load_a instead of load if you are certain that p points to an address
// divisible by 16. In most cases there is no difference in speed between load and load_a
Vec8h & load_a(void const * p) {
xmm = _mm_load_ph (p);
return *this;
}
// Member function to store into array (unaligned)
void store(void * p) const {
_mm_storeu_ph (p, xmm);
}
// Member function storing into array, aligned by 16
// You may use store_a instead of store if you are certain that p points to an address
// divisible by 16.
void store_a(void * p) const {
_mm_store_ph (p, xmm);
}
// Member function storing to aligned uncached memory (non-temporal store).
// This may be more efficient than store_a when storing large blocks of memory if it
// is unlikely that the data will stay in the cache until it is read again.
// Note: Will generate runtime error if p is not aligned by 16
void store_nt(void * p) const {
_mm_stream_ps((float*)p, _mm_castph_ps(xmm));
}
// Partial load. Load n elements and set the rest to 0
Vec8h & load_partial(int n, void const * p) {
xmm = _mm_castsi128_ph(_mm_maskz_loadu_epi16(__mmask8((1u << n) - 1), p));
return *this;
}
// Partial store. Store n elements
void store_partial(int n, void * p) const {
_mm_mask_storeu_epi16(p, __mmask8((1u << n) - 1), _mm_castph_si128(xmm));
}
// cut off vector to n elements. The last 8-n elements are set to zero
Vec8h & cutoff(int n) {
xmm = _mm_castsi128_ph(_mm_maskz_mov_epi16(__mmask8((1u << n) - 1), _mm_castph_si128(xmm)));
return *this;
}
// Member function to change a single element in vector
Vec8h const insert(int index, _Float16 a) {
__m128h aa = _mm_set1_ph (a);
xmm = _mm_castsi128_ph(_mm_mask_mov_epi16(_mm_castph_si128(xmm), __mmask8(1u << index), _mm_castph_si128(aa)));
return *this;
}
// Member function extract a single element from vector
_Float16 extract(int index) const {
#if INSTRSET >= 10 && defined (__AVX512VBMI2__)
__m128i x = _mm_maskz_compress_epi16(__mmask8(1u << index), _mm_castph_si128(xmm));
return _mm_cvtsh_h(_mm_castsi128_ph(x));
#elif 0
union {
__m128h v;
_Float16 f[8];
} y;
y.v = xmm;
return y.f[index & 7];
#else
Vec4ui x = _mm_maskz_compress_epi32(__mmask8(1u << (index >> 1)), _mm_castph_si128(xmm)); // extract int32_t
x >>= (index & 1) << 4; // get upper 16 bits if index odd
return _mm_cvtsh_h(_mm_castsi128_ph(x));
#endif
}
// Extract a single element. Use store function if extracting more than one element.
// Operator [] can only read an element, not write.
_Float16 operator [] (int index) const {
return extract(index);
}
static constexpr int size() {
return 8;
}
static constexpr int elementtype() {
return 15;
}
typedef __m128h registertype;
};
/*****************************************************************************
*
* Operators for Vec8h
*
*****************************************************************************/
// vector operator + : add element by element
static inline Vec8h operator + (Vec8h const a, Vec8h const b) {
return _mm_add_ph(a, b);
}
// vector operator + : add vector and scalar
static inline Vec8h operator + (Vec8h const a, _Float16 b) {
return a + Vec8h(b);
}
static inline Vec8h operator + (_Float16 a, Vec8h const b) {
return Vec8h(a) + b;
}
// vector operator += : add
static inline Vec8h & operator += (Vec8h & a, Vec8h const b) {
a = a + b;
return a;
}
// postfix operator ++
static inline Vec8h operator ++ (Vec8h & a, int) {
Vec8h a0 = a;
a = a + _Float16(1); // 1.0f16 not supported by g++ version 12.1
return a0;
}
// prefix operator ++
static inline Vec8h & operator ++ (Vec8h & a) {
a = a + _Float16(1);
return a;
}
// vector operator - : subtract element by element
static inline Vec8h operator - (Vec8h const a, Vec8h const b) {
return _mm_sub_ph(a, b);
}
// vector operator - : subtract vector and scalar
static inline Vec8h operator - (Vec8h const a, _Float16 b) {
return a - Vec8h(b);
}
static inline Vec8h operator - (_Float16 a, Vec8h const b) {
return Vec8h(a) - b;
}
// vector operator - : unary minus
// Change sign bit, even for 0, INF and NAN
static inline Vec8h operator - (Vec8h const a) {
return _mm_castps_ph(_mm_xor_ps(_mm_castph_ps(a), _mm_castsi128_ps(_mm_set1_epi32(0x80008000))));
}
// vector operator -= : subtract
static inline Vec8h & operator -= (Vec8h & a, Vec8h const b) {
a = a - b;
return a;
}
// postfix operator --
static inline Vec8h operator -- (Vec8h & a, int) {
Vec8h a0 = a;
a = a - _Float16(1);
return a0;
}
// prefix operator --
static inline Vec8h & operator -- (Vec8h & a) {
a = a - _Float16(1);
return a;
}
// vector operator * : multiply element by element
static inline Vec8h operator * (Vec8h const a, Vec8h const b) {
return _mm_mul_ph(a, b);
}
// vector operator * : multiply vector and scalar
static inline Vec8h operator * (Vec8h const a, _Float16 b) {
return a * Vec8h(b);
}
static inline Vec8h operator * (_Float16 a, Vec8h const b) {
return Vec8h(a) * b;
}
// vector operator *= : multiply
static inline Vec8h & operator *= (Vec8h & a, Vec8h const b) {
a = a * b;
return a;
}
// vector operator / : divide all elements by same integer
static inline Vec8h operator / (Vec8h const a, Vec8h const b) {
return _mm_div_ph(a, b);
}
// vector operator / : divide vector and scalar
static inline Vec8h operator / (Vec8h const a, _Float16 b) {
return a / Vec8h(b);
}
static inline Vec8h operator / (_Float16 a, Vec8h const b) {
return Vec8h(a) / b;
}
// vector operator /= : divide
static inline Vec8h & operator /= (Vec8h & a, Vec8h const b) {
a = a / b;
return a;
}
// vector operator == : returns true for elements for which a == b
static inline Vec8hb operator == (Vec8h const a, Vec8h const b) {
return _mm_cmp_ph_mask(a, b, 0);
}
// vector operator != : returns true for elements for which a != b
static inline Vec8hb operator != (Vec8h const a, Vec8h const b) {
return _mm_cmp_ph_mask(a, b, 4);
}
// vector operator < : returns true for elements for which a < b
static inline Vec8hb operator < (Vec8h const a, Vec8h const b) {
return _mm_cmp_ph_mask(a, b, 1);
}
// vector operator <= : returns true for elements for which a <= b
static inline Vec8hb operator <= (Vec8h const a, Vec8h const b) {
return _mm_cmp_ph_mask(a, b, 2);
}
// vector operator > : returns true for elements for which a > b
static inline Vec8hb operator > (Vec8h const a, Vec8h const b) {
return _mm_cmp_ph_mask(a, b, 6+8);
}
// vector operator >= : returns true for elements for which a >= b
static inline Vec8hb operator >= (Vec8h const a, Vec8h const b) {
return _mm_cmp_ph_mask(a, b, 5+8);
}
// Bitwise logical operators
// vector operator & : bitwise and
static inline Vec8h operator & (Vec8h const a, Vec8h const b) {
return _mm_castps_ph(_mm_and_ps(_mm_castph_ps(a), _mm_castph_ps(b)));
}
// vector operator &= : bitwise and
static inline Vec8h & operator &= (Vec8h & a, Vec8h const b) {
a = a & b;
return a;
}
// vector operator & : bitwise and of Vec8h and Vec8hb
static inline Vec8h operator & (Vec8h const a, Vec8hb const b) {
return _mm_castsi128_ph(_mm_maskz_mov_epi16(b, _mm_castph_si128(a)));
}
static inline Vec8h operator & (Vec8hb const a, Vec8h const b) {
return b & a;
}
// vector operator | : bitwise or
static inline Vec8h operator | (Vec8h const a, Vec8h const b) {
return _mm_castps_ph(_mm_or_ps(_mm_castph_ps(a), _mm_castph_ps(b)));
}
// vector operator |= : bitwise or
static inline Vec8h & operator |= (Vec8h & a, Vec8h const b) {
a = a | b;
return a;
}
// vector operator ^ : bitwise xor
static inline Vec8h operator ^ (Vec8h const a, Vec8h const b) {
return _mm_castps_ph(_mm_xor_ps(_mm_castph_ps(a), _mm_castph_ps(b)));
}
// vector operator ^= : bitwise xor
static inline Vec8h & operator ^= (Vec8h & a, Vec8h const b) {
a = a ^ b;
return a;
}
// vector operator ! : logical not. Returns Boolean vector
static inline Vec8hb operator ! (Vec8h const a) {
return a == Vec8h(0.0);
}
/*****************************************************************************
*
* Functions for Vec8h
*
*****************************************************************************/
// Select between two operands. Corresponds to this pseudocode:
// for (int i = 0; i < 4; i++) result[i] = s[i] ? a[i] : b[i];
static inline Vec8h select(Vec8hb const s, Vec8h const a, Vec8h const b) {
return _mm_castsi128_ph(_mm_mask_mov_epi16(_mm_castph_si128(b), s, _mm_castph_si128(a)));
}
// Conditional add: For all vector elements i: result[i] = f[i] ? (a[i] + b[i]) : a[i]
static inline Vec8h if_add(Vec8hb const f, Vec8h const a, Vec8h const b) {
return _mm_mask_add_ph (a, f, a, b);
}
// Conditional subtract: For all vector elements i: result[i] = f[i] ? (a[i] - b[i]) : a[i]
static inline Vec8h if_sub(Vec8hb const f, Vec8h const a, Vec8h const b) {
return _mm_mask_sub_ph (a, f, a, b);
}
// Conditional multiply: For all vector elements i: result[i] = f[i] ? (a[i] * b[i]) : a[i]
static inline Vec8h if_mul(Vec8hb const f, Vec8h const a, Vec8h const b) {
return _mm_mask_mul_ph (a, f, a, b);
}
// Conditional divide: For all vector elements i: result[i] = f[i] ? (a[i] / b[i]) : a[i]
static inline Vec8h if_div(Vec8hb const f, Vec8h const a, Vec8h const b) {
return _mm_mask_div_ph (a, f, a, b);
}
// Sign functions
// Function sign_bit: gives true for elements that have the sign bit set
// even for -0.0f, -INF and -NAN
// Note that sign_bit(Vec8h(-0.0f16)) gives true, while Vec8h(-0.0f16) < Vec8h(0.0f16) gives false
// (the underscore in the name avoids a conflict with a macro in Intel's mathimf.h)
static inline Vec8hb sign_bit(Vec8h const a) {
Vec8s t1 = _mm_castph_si128(a); // reinterpret as 16-bit integer
Vec8s t2 = t1 >> 15; // extend sign bit
return t2 != 0;
}
// Function sign_combine: changes the sign of a when b has the sign bit set
// same as select(sign_bit(b), -a, a)
static inline Vec8h sign_combine(Vec8h const a, Vec8h const b) {
return a ^ (b & Vec8h(_Float16(-0.0)));
}
// Categorization functions
// Function is_finite: gives true for elements that are normal, subnormal or zero,
// false for INF and NAN
// (the underscore in the name avoids a conflict with a macro in Intel's mathimf.h)
static inline Vec8hb is_finite(Vec8h const a) {
return __mmask8(_mm_fpclass_ph_mask(a, 0x99) ^ 0xFF);
}
// Function is_inf: gives true for elements that are +INF or -INF
// false for finite numbers and NAN
// (the underscore in the name avoids a conflict with a macro in Intel's mathimf.h)
static inline Vec8hb is_inf(Vec8h const a) {
return __mmask8(_mm_fpclass_ph_mask(a, 0x18));
}
// Function is_nan: gives true for elements that are +NAN or -NAN
// false for finite numbers and +/-INF
// (the underscore in the name avoids a conflict with a macro in Intel's mathimf.h)
static inline Vec8hb is_nan(Vec8h const a) {
// assume that compiler does not optimize this away with -ffinite-math-only:
return Vec4fb(_mm_fpclass_ph_mask(a, 0x81));
}
// Function is_subnormal: gives true for elements that are subnormal
// false for finite numbers, zero, NAN and INF
static inline Vec8hb is_subnormal(Vec8h const a) {
return Vec8hb(_mm_fpclass_ph_mask(a, 0x20));
}
// Function is_zero_or_subnormal: gives true for elements that are zero or subnormal
// false for finite numbers, NAN and INF
static inline Vec8hb is_zero_or_subnormal(Vec8h const a) {
return Vec8hb(_mm_fpclass_ph_mask(a, 0x26));
}
// Function infinite8h: returns a vector where all elements are +INF
static inline Vec8h infinite8h() {
return _mm_castsi128_ph(_mm_set1_epi16(0x7C00));
}
// template for producing quiet NAN
template <>
Vec8h nan_vec<Vec8h>(uint32_t payload) {
if constexpr (Vec8h::elementtype() == 15) { // _Float16
union {
uint16_t i;
_Float16 f;
} uf;
uf.i = 0x7E00 | (payload & 0x01FF);
return Vec8h(uf.f);
}
}
// Function nan8h: returns a vector where all elements are NAN (quiet)
static inline Vec8h nan8h(uint32_t n = 0x10) {
return nan_vec<Vec8h>(n);
}
// This function returns the code hidden in a NAN. The sign bit is ignored
static inline Vec8us nan_code(Vec8h const x) {
Vec8us a = Vec8us(_mm_castph_si128(x));
Vec8us const n = 0x3FF;
return select(is_nan(x), a & n, Vec8us(0));
}
// General arithmetic functions, etc.
// Horizontal add: Calculates the sum of all vector elements.
static inline _Float16 horizontal_add(Vec8h const a) {
//return _mm_reduce_add_ph(a);
__m128h b = _mm_castps_ph(_mm_movehl_ps(_mm_castph_ps(a), _mm_castph_ps(a)));
__m128h c = _mm_add_ph(a, b);
__m128h d = _mm_castps_ph(_mm_movehdup_ps( _mm_castph_ps(c)));
__m128h e = _mm_add_ph(c, d);
__m128h f = _mm_castsi128_ph(_mm_shufflelo_epi16(_mm_castph_si128(e), 1));
__m128h g = _mm_add_sh(e, f);
return _mm_cvtsh_h(g);
}
#if MAX_VECTOR_SIZE >= 256
// same, with high precision
static inline float horizontal_add_x(Vec8h const a) {
//Vec8f b = _mm256_cvtph_ps(a); // declaration of _mm256_cvtph_ps has __m128i parameter because it was defined before __m128h was defined
Vec8f b = _mm256_cvtph_ps(_mm_castph_si128(a));
return horizontal_add(b);
}
#endif
// function max: a > b ? a : b
static inline Vec8h max(Vec8h const a, Vec8h const b) {
return _mm_max_ph(a, b);
}
// function min: a < b ? a : b
static inline Vec8h min(Vec8h const a, Vec8h const b) {
return _mm_min_ph(a, b);
}
// NAN-safe versions of maximum and minimum are in vector_convert.h
// function abs: absolute value
static inline Vec8h abs(Vec8h const a) {
return _mm_abs_ph(a);
}
// function sqrt: square root
static inline Vec8h sqrt(Vec8h const a) {
return _mm_sqrt_ph(a);
}
// function square: a * a
static inline Vec8h square(Vec8h const a) {
return a * a;
}
// The purpose of this template is to prevent implicit conversion of a float
// exponent to int when calling pow(vector, float) and vectormath_exp.h is not included
template <typename TT> static Vec8h pow(Vec8h const a, TT const n); // = delete
// Raise floating point numbers to integer power n
template <>
inline Vec8h pow<int>(Vec8h const x0, int const n) {
return pow_template_i<Vec8h>(x0, n);
}
// allow conversion from unsigned int
template <>
inline Vec8h pow<uint32_t>(Vec8h const x0, uint32_t const n) {
return pow_template_i<Vec8h>(x0, (int)n);
}
// Raise floating point numbers to integer power n, where n is a compile-time constant:
// Template in vectorf28.h is used
//template <typename V, int n>
//static inline V pow_n(V const a);
// implement as function pow(vector, const_int)
template <int n>
static inline Vec8h pow(Vec8h const a, Const_int_t<n>) {
return pow_n<Vec8h, n>(a);
}
static inline Vec8h round(Vec8h const a) {
return _mm_roundscale_ph (a, 8);
}
// function truncate: round towards zero. (result as float vector)
static inline Vec8h truncate(Vec8h const a) {
return _mm_roundscale_ph(a, 3 + 8);
}
// function floor: round towards minus infinity. (result as float vector)
static inline Vec8h floor(Vec8h const a) {
return _mm_roundscale_ph(a, 1 + 8);
}
// function ceil: round towards plus infinity. (result as float vector)
static inline Vec8h ceil(Vec8h const a) {
return _mm_roundscale_ph(a, 2 + 8);
}
// function roundi: round to nearest integer (even). (result as integer vector)
static inline Vec8s roundi(Vec8h const a) {
// Note: assume MXCSR control register is set to rounding
return _mm_cvtph_epi16(a);
}
// function truncatei: round towards zero. (result as integer vector)
static inline Vec8s truncatei(Vec8h const a) {
return _mm_cvttph_epi16(a);
}
// function to_float: convert integer vector to float vector
static inline Vec8h to_float16(Vec8s const a) {
return _mm_cvtepi16_ph(a);
}
// function to_float: convert unsigned integer vector to float vector
static inline Vec8h to_float16(Vec8us const a) {
return _mm_cvtepu16_ph(a);
}
// Approximate math functions
// reciprocal (almost exact)
static inline Vec8h approx_recipr(Vec8h const a) {
return _mm_rcp_ph (a);
}
// reciprocal squareroot (almost exact)
static inline Vec8h approx_rsqrt(Vec8h const a) {
return _mm_rsqrt_ph(a);
}
// Fused multiply and add functions
// Multiply and add. a*b+c
static inline Vec8h mul_add(Vec8h const a, Vec8h const b, Vec8h const c) {
return _mm_fmadd_ph(a, b, c);
}
// Multiply and subtract. a*b-c
static inline Vec8h mul_sub(Vec8h const a, Vec8h const b, Vec8h const c) {
return _mm_fmsub_ph(a, b, c);
}
// Multiply and inverse subtract
static inline Vec8h nmul_add(Vec8h const a, Vec8h const b, Vec8h const c) {
return _mm_fnmadd_ph(a, b, c);
}
// Math functions using fast bit manipulation
// Extract the exponent as an integer
// exponent(a) = floor(log2(abs(a)));
// exponent(1.0f) = 0, exponent(0.0f) = -127, exponent(INF) = +128, exponent(NAN) = +128
static inline Vec8s exponent(Vec8h const a) {
Vec8us t1 = _mm_castph_si128(a); // reinterpret as 16-bit integer
Vec8us t2 = t1 << 1; // shift out sign bit
Vec8us t3 = t2 >> 11; // shift down logical to position 0
Vec8s t4 = Vec8s(t3) - 0x0F; // subtract bias from exponent
return t4;
}
// Extract the fraction part of a floating point number
// a = 2^exponent(a) * fraction(a), except for a = 0
// fraction(1.0f) = 1.0f, fraction(5.0f) = 1.25f
// NOTE: The name fraction clashes with an ENUM in MAC XCode CarbonCore script.h !
static inline Vec8h fraction(Vec8h const a) {
return _mm_getmant_ph(a, _MM_MANT_NORM_1_2, _MM_MANT_SIGN_zero);
}
// Fast calculation of pow(2,n) with n integer
// n = 0 gives 1.0f
// n >= 16 gives +INF
// n <= -15 gives 0.0f
// This function will never produce subnormals, and never raise exceptions
static inline Vec8h exp2(Vec8s const n) {
Vec8s t1 = max(n, -15); // limit to allowed range
Vec8s t2 = min(t1, 16);
Vec8s t3 = t2 + 15; // add bias
Vec8s t4 = t3 << 10; // put exponent into position 10
return _mm_castsi128_ph(t4); // reinterpret as float
}
//static Vec8h exp2(Vec8h const x); // defined in vectormath_exp.h ??
// change signs on vectors Vec8h
// Each index i0 - i7 is 1 for changing sign on the corresponding element, 0 for no change
template <int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7>
static inline Vec8h change_sign(Vec8h const a) {
if constexpr ((i0 | i1 | i2 | i3 | i4 | i5 | i6 | i7) == 0) return a;
__m128i mask = constant4ui<
(i0 ? 0x8000 : 0) | (i1 ? 0x80000000 : 0),
(i2 ? 0x8000 : 0) | (i3 ? 0x80000000 : 0),
(i4 ? 0x8000 : 0) | (i5 ? 0x80000000 : 0),
(i6 ? 0x8000 : 0) | (i7 ? 0x80000000 : 0) >();
return _mm_castps_ph(_mm_xor_ps(_mm_castph_ps(a), _mm_castsi128_ps(mask))); // flip sign bits
}
/*****************************************************************************
*
* conversion of precision
*
*****************************************************************************/
// conversions Vec8h <-> Vec4f
// extend precision: Vec8h -> Vec4f. upper half ignored
static inline Vec4f convert8h_4f (Vec8h h) {
return _mm_cvtph_ps(_mm_castph_si128(h));
}
// reduce precision: Vec4f -> Vec8h. upper half zero
static inline Vec8h convert4f_8h (Vec4f f) {
return _mm_castsi128_ph(_mm_cvtps_ph(f, 0));
}
#if MAX_VECTOR_SIZE >= 256
// conversions Vec8h <-> Vec8f
// extend precision: Vec8h -> Vec8f
static inline Vec8f to_float (Vec8h h) {
return _mm256_cvtph_ps(_mm_castph_si128(h));
}
// reduce precision: Vec8f -> Vec8h
static inline Vec8h to_float16 (Vec8f f) {
return _mm_castsi128_ph(_mm256_cvtps_ph(f, 0));
}
#endif
/*****************************************************************************
*
* Functions for reinterpretation between vector types
*
*****************************************************************************/
static inline __m128i reinterpret_i(__m128h const x) {
return _mm_castph_si128(x);
}
static inline __m128h reinterpret_h(__m128i const x) {
return _mm_castsi128_ph(x);
}
static inline __m128 reinterpret_f(__m128h const x) {
return _mm_castph_ps(x);
}
static inline __m128d reinterpret_d(__m128h const x) {
return _mm_castph_pd(x);
}
/*****************************************************************************
*
* Vector permute and blend functions
*
******************************************************************************
*
* The permute function can reorder the elements of a vector and optionally
* set some elements to zero.
*
* See vectori128.h for details
*
*****************************************************************************/
// permute vector Vec8h
template <int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7>
static inline Vec8h permute8(Vec8h const a) {
return _mm_castsi128_ph (permute8<i0, i1, i2, i3, i4, i5, i6, i7>(Vec8s(_mm_castph_si128(a))));
}
/*****************************************************************************
*
* Vector blend functions
*
*****************************************************************************/
// permute and blend Vec8h
template <int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7>
static inline Vec8h blend8(Vec8h const a, Vec8h const b) {
return _mm_castsi128_ph (blend8<i0, i1, i2, i3, i4, i5, i6, i7>(Vec8s(_mm_castph_si128(a)), Vec8s(_mm_castph_si128(b))));
}
/*****************************************************************************
*
* Vector lookup functions
*
******************************************************************************
*
* These functions use vector elements as indexes into a table.
* The table is given as one or more vectors or as an array.
*
*****************************************************************************/
static inline Vec8h lookup8 (Vec8s const index, Vec8h const table) {
return _mm_castsi128_ph(lookup8(index, Vec8s(_mm_castph_si128(table))));
}
static inline Vec8h lookup16(Vec8s const index, Vec8h const table0, Vec8h const table1) {
return _mm_castsi128_ph(lookup16(index, Vec8s(_mm_castph_si128(table0)), Vec8s(_mm_castph_si128(table1))));
}
template <int n>
static inline Vec8h lookup(Vec8s const index, void const * table) {
return _mm_castsi128_ph(lookup<n>(index, (void const *)(table)));
}
/*****************************************************************************
*
* 256 bit vectors
*
*****************************************************************************/
#if MAX_VECTOR_SIZE >= 256
/*****************************************************************************
*
* Vec16hb: Vector of 16 Booleans for use with Vec16h
*
*****************************************************************************/
typedef Vec16b Vec16hb; // compact boolean vector
/*****************************************************************************
*
* Vec16h: Vector of 16 half precision floating point values
*
*****************************************************************************/
class Vec16h {
protected:
__m256h ymm; // Float vector
public:
// Default constructor:
Vec16h() = default;
// Constructor to broadcast the same value into all elements:
Vec16h(_Float16 f) {
ymm = _mm256_set1_ph (f);
}
// Constructor to build from all elements:
Vec16h(_Float16 f0, _Float16 f1, _Float16 f2, _Float16 f3, _Float16 f4, _Float16 f5, _Float16 f6, _Float16 f7,
_Float16 f8, _Float16 f9, _Float16 f10, _Float16 f11, _Float16 f12, _Float16 f13, _Float16 f14, _Float16 f15) {
ymm = _mm256_setr_ph (f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15);
}
// Constructor to build from two Vec8h:
Vec16h(Vec8h const a0, Vec8h const a1) {
ymm = _mm256_castps_ph(_mm256_insertf128_ps(_mm256_castps128_ps256(_mm_castph_ps(a0)),_mm_castph_ps(a1),1));
}
// Constructor to convert from type __m256h used in intrinsics:
Vec16h(__m256h const x) {
ymm = x;
}
// Assignment operator to convert from type __m256h used in intrinsics:
Vec16h & operator = (__m256h const x) {
ymm = x;
return *this;
}
// Type cast operator to convert to __m256h used in intrinsics
operator __m256h() const {
return ymm;
}
// Member function to load from array (unaligned)
Vec16h & load(void const * p) {
ymm = _mm256_loadu_ph (p);
return *this;
}
// Member function to load from array, aligned by 32
// You may use load_a instead of load if you are certain that p points to an address
// divisible by 32. In most cases there is no difference in speed between load and load_a
Vec16h & load_a(void const * p) {
ymm = _mm256_load_ph (p);
return *this;
}
// Member function to store into array (unaligned)
void store(void * p) const {
_mm256_storeu_ph (p, ymm);
}
// Member function storing into array, aligned by 32
// You may use store_a instead of store if you are certain that p points to an address
// divisible by 32.
void store_a(void * p) const {
_mm256_store_ph (p, ymm);
}
// Member function storing to aligned uncached memory (non-temporal store).
// This may be more efficient than store_a when storing large blocks of memory if it
// is unlikely that the data will stay in the cache until it is read again.
// Note: Will generate runtime error if p is not aligned by 32
void store_nt(void * p) const {
_mm256_stream_ps((float*)p, _mm256_castph_ps(ymm));
}
// Partial load. Load n elements and set the rest to 0
Vec16h & load_partial(int n, void const * p) {
ymm = _mm256_castsi256_ph(_mm256_maskz_loadu_epi16(__mmask16((1u << n) - 1), p));
return *this;
}
// Partial store. Store n elements
void store_partial(int n, void * p) const {
_mm256_mask_storeu_epi16(p, __mmask16((1u << n) - 1), _mm256_castph_si256(ymm));
}
// cut off vector to n elements. The last 8-n elements are set to zero
Vec16h & cutoff(int n) {
ymm = _mm256_castsi256_ph(_mm256_maskz_mov_epi16(__mmask16((1u << n) - 1), _mm256_castph_si256(ymm)));
return *this;
}
// Member function to change a single element in vector
Vec16h const insert(int index, _Float16 a) {
__m256h aa = _mm256_set1_ph (a);
ymm = _mm256_castsi256_ph(_mm256_mask_mov_epi16(_mm256_castph_si256(ymm), __mmask16(1u << index), _mm256_castph_si256(aa)));
return *this;
}
// Member function extract a single element from vector
_Float16 extract(int index) const {
#if INSTRSET >= 10 && defined (__AVX512VBMI2__)
__m256i x = _mm256_maskz_compress_epi16(__mmask16(1u << index), _mm256_castph_si256(ymm));
return _mm256_cvtsh_h(_mm256_castsi256_ph(x));
#elif 0
union {
__m256h v;
_Float16 f[16];
} y;
y.v = ymm;
return y.f[index & 15];
#else
Vec8ui x = _mm256_maskz_compress_epi32(__mmask16(1u << (index >> 1)), _mm256_castph_si256(ymm)); // extract int32_t
x >>= uint32_t((index & 1) << 4); // get upper 16 bits if index odd
return _mm256_cvtsh_h(_mm256_castsi256_ph(x));
#endif
}
// Extract a single element. Use store function if extracting more than one element.
// Operator [] can only read an element, not write.
_Float16 operator [] (int index) const {
return extract(index);
}
Vec8h get_low() const {
return _mm256_castph256_ph128(ymm);
}
Vec8h get_high() const {
return _mm_castps_ph(_mm256_extractf128_ps(_mm256_castph_ps(ymm),1));
}
static constexpr int size() {
return 16;
}
static constexpr int elementtype() {
return 15;
}
typedef __m256h registertype;
};
/*****************************************************************************
*
* Operators for Vec16h
*
*****************************************************************************/
// vector operator + : add element by element
static inline Vec16h operator + (Vec16h const a, Vec16h const b) {
return _mm256_add_ph(a, b);
}
// vector operator + : add vector and scalar
static inline Vec16h operator + (Vec16h const a, _Float16 b) {
return a + Vec16h(b);
}
static inline Vec16h operator + (_Float16 a, Vec16h const b) {
return Vec16h(a) + b;
}
// vector operator += : add
static inline Vec16h & operator += (Vec16h & a, Vec16h const b) {
a = a + b;
return a;
}
// postfix operator ++
static inline Vec16h operator ++ (Vec16h & a, int) {
Vec16h a0 = a;
a = a + _Float16(1);
return a0;
}
// prefix operator ++
static inline Vec16h & operator ++ (Vec16h & a) {
a = a + _Float16(1);
return a;
}
// vector operator - : subtract element by element
static inline Vec16h operator - (Vec16h const a, Vec16h const b) {
return _mm256_sub_ph(a, b);
}
// vector operator - : subtract vector and scalar
static inline Vec16h operator - (Vec16h const a, float b) {
return a - Vec16h(b);
}
static inline Vec16h operator - (float a, Vec16h const b) {
return Vec16h(a) - b;
}
// vector operator - : unary minus
// Change sign bit, even for 0, INF and NAN
static inline Vec16h operator - (Vec16h const a) {
return _mm256_castps_ph(_mm256_xor_ps(_mm256_castph_ps(a), _mm256_castsi256_ps(_mm256_set1_epi32(0x80008000))));
}
// vector operator -= : subtract
static inline Vec16h & operator -= (Vec16h & a, Vec16h const b) {