-
Notifications
You must be signed in to change notification settings - Fork 148
/
vectorf256.h
3058 lines (2713 loc) · 107 KB
/
vectorf256.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
/**************************** vectorf256.h *******************************
* Author: Agner Fog
* Date created: 2012-05-30
* Last modified: 2023-07-04
* Version: 2.02.02
* Project: vector class library
* Description:
* Header file defining 256-bit floating point vector classes
*
* Instructions: see vcl_manual.pdf
*
* The following vector classes are defined here:
* Vec8f Vector of 8 single precision floating point numbers
* Vec8fb Vector of 8 Booleans for use with Vec8f
* Vec4d Vector of 4 double precision floating point numbers
* Vec4db Vector of 4 Booleans for use with Vec4d
*
* Each vector object is represented internally in the CPU as a 256-bit register.
* This header file defines operators and functions for these vectors.
*
* (c) Copyright 2012-2023 Agner Fog.
* Apache License version 2.0 or later.
*****************************************************************************/
#ifndef VECTORF256_H
#define VECTORF256_H 1
#ifndef VECTORCLASS_H
#include "vectorclass.h"
#endif
#if VECTORCLASS_H < 20200
#error Incompatible versions of vector class library mixed
#endif
#ifdef VECTORF256E_H
#error Two different versions of vectorf256.h included
#endif
#ifdef VCL_NAMESPACE
namespace VCL_NAMESPACE {
#endif
/*****************************************************************************
*
* Generate compile-time constant vector
*
*****************************************************************************/
// Generate a constant vector of 8 integers stored in memory
template <uint32_t i0, uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4, uint32_t i5, uint32_t i6, uint32_t i7>
inline __m256 constant8f() {
/*
const union {
uint32_t i[8];
__m256 ymm;
} u = {{i0,i1,i2,i3,i4,i5,i6,i7}};
return u.ymm;
*/
return _mm256_castsi256_ps(_mm256_setr_epi32(i0,i1,i2,i3,i4,i5,i6,i7));
}
// Join two 128-bit vectors. Used below
#define set_m128r(lo,hi) _mm256_insertf128_ps(_mm256_castps128_ps256(lo),(hi),1)
// _mm256_set_m128(hi,lo); // not defined in all versions of immintrin.h
/*****************************************************************************
*
* Vec8fb: Vector of 8 Booleans for use with Vec8f
*
*****************************************************************************/
#if INSTRSET < 10 // broad boolean vectors
class Vec8fb {
protected:
__m256 ymm; // Float vector
public:
// Default constructor:
Vec8fb() = default;
// Constructor to build from all elements:
Vec8fb(bool b0, bool b1, bool b2, bool b3, bool b4, bool b5, bool b6, bool b7) {
#if INSTRSET >= 8 // AVX2
ymm = _mm256_castsi256_ps(_mm256_setr_epi32(-(int)b0, -(int)b1, -(int)b2, -(int)b3, -(int)b4, -(int)b5, -(int)b6, -(int)b7));
#else
__m128 blo = _mm_castsi128_ps(_mm_setr_epi32(-(int)b0, -(int)b1, -(int)b2, -(int)b3));
__m128 bhi = _mm_castsi128_ps(_mm_setr_epi32(-(int)b4, -(int)b5, -(int)b6, -(int)b7));
ymm = set_m128r(blo,bhi);
#endif
}
// Constructor to build from two Vec4fb:
Vec8fb(Vec4fb const a0, Vec4fb const a1) {
ymm = set_m128r(a0, a1);
}
// Constructor to convert from type __m256 used in intrinsics:
Vec8fb(__m256 const x) {
ymm = x;
}
// Assignment operator to convert from type __m256 used in intrinsics:
Vec8fb & operator = (__m256 const x) {
ymm = x;
return *this;
}
// Constructor to broadcast the same value into all elements:
Vec8fb(bool b) {
#if INSTRSET >= 8 // AVX2
ymm = _mm256_castsi256_ps(_mm256_set1_epi32(-(int)b));
#else
__m128 b1 = _mm_castsi128_ps(_mm_set1_epi32(-(int)b));
//ymm = _mm256_set_m128(b1,b1);
ymm = set_m128r(b1,b1);
#endif
}
// Assignment operator to broadcast scalar value:
Vec8fb & operator = (bool b) {
*this = Vec8fb(b);
return *this;
}
// Type cast operator to convert to __m256 used in intrinsics
operator __m256() const {
return ymm;
}
#if INSTRSET >= 8 // AVX2
// Constructor to convert from type Vec8ib used as Boolean for integer vectors
Vec8fb(Vec8ib const x) {
ymm = _mm256_castsi256_ps(x);
}
// Assignment operator to convert from type Vec8ib used as Boolean for integer vectors
Vec8fb & operator = (Vec8ib const x) {
ymm = _mm256_castsi256_ps(x);
return *this;
}
// Member function to change a bitfield to a boolean vector
Vec8fb & load_bits(uint8_t a) {
Vec8ib b; b.load_bits(a);
ymm = _mm256_castsi256_ps(b);
return *this;
}
#ifndef FIX_CLANG_VECTOR_ALIAS_AMBIGUITY
// Type cast operator to convert to type Vec8ib used as Boolean for integer vectors
operator Vec8ib() const {
return _mm256_castps_si256(ymm);
}
#endif
#else // AVX version
// Constructor to convert from type Vec8ib used as Boolean for integer vectors
Vec8fb(Vec8ib const x) {
ymm = set_m128r(_mm_castsi128_ps(x.get_low()), _mm_castsi128_ps(x.get_high()));
}
// Assignment operator to convert from type Vec8ib used as Boolean for integer vectors
Vec8fb & operator = (Vec8ib const x) {
ymm = set_m128r(_mm_castsi128_ps(x.get_low()), _mm_castsi128_ps(x.get_high()));
return *this;
}
// Member function to change a bitfield to a boolean vector
// AVX version. Cannot use float instructions if subnormals are disabled
Vec8fb & load_bits(uint8_t a) {
Vec4fb y0 = Vec4fb().load_bits(a);
Vec4fb y1 = Vec4fb().load_bits(uint8_t(a >> 4u));
*this = Vec8fb(y0, y1);
return *this;
}
// Type cast operator to convert to type Vec8ib used as Boolean for integer vectors
operator Vec8ib() const {
return Vec8i(_mm_castps_si128(get_low()), _mm_castps_si128(get_high()));
}
#endif // AVX2
// Member function to change a single element in vector
Vec8fb const insert(int index, bool value) {
const int32_t maskl[16] = {0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0};
__m256 mask = _mm256_loadu_ps((float const*)(maskl+8-(index & 7))); // mask with FFFFFFFF at index position
if (value) {
ymm = _mm256_or_ps(ymm,mask);
}
else {
ymm = _mm256_andnot_ps(mask,ymm);
}
return *this;
}
// Member function extract a single element from vector
bool extract(int index) const {
union {
float f[8];
int32_t i[8];
} u;
_mm256_storeu_ps(u.f, ymm);
return u.i[index & 7] != 0;
}
// Extract a single element. Operator [] can only read an element, not write.
bool operator [] (int index) const {
return extract(index);
}
// Member functions to split into two Vec4fb:
Vec4fb get_low() const {
return _mm256_castps256_ps128(ymm);
}
Vec4fb get_high() const {
return _mm256_extractf128_ps(ymm,1);
}
static constexpr int size() {
return 8;
}
static constexpr int elementtype() {
return 3;
}
// Prevent constructing from int, etc.
Vec8fb(int b) = delete;
Vec8fb & operator = (int x) = delete;
};
#else
typedef Vec8b Vec8fb; // compact boolean vector
#endif
/*****************************************************************************
*
* Operators and functions for Vec8fb
*
*****************************************************************************/
#if INSTRSET < 10 // broad boolean vectors
// vector operator & : bitwise and
static inline Vec8fb operator & (Vec8fb const a, Vec8fb const b) {
return _mm256_and_ps(a, b);
}
static inline Vec8fb operator && (Vec8fb const a, Vec8fb const b) {
return a & b;
}
// vector operator &= : bitwise and
static inline Vec8fb & operator &= (Vec8fb & a, Vec8fb const b) {
a = a & b;
return a;
}
// vector operator | : bitwise or
static inline Vec8fb operator | (Vec8fb const a, Vec8fb const b) {
return _mm256_or_ps(a, b);
}
static inline Vec8fb operator || (Vec8fb const a, Vec8fb const b) {
return a | b;
}
// vector operator |= : bitwise or
static inline Vec8fb & operator |= (Vec8fb & a, Vec8fb const b) {
a = a | b;
return a;
}
// vector operator ~ : bitwise not
static inline Vec8fb operator ~ (Vec8fb const a) {
return _mm256_xor_ps(a, constant8f<0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu>());
}
// vector operator ^ : bitwise xor
static inline Vec8fb operator ^ (Vec8fb const a, Vec8fb const b) {
return _mm256_xor_ps(a, b);
}
// vector operator == : xnor
static inline Vec8fb operator == (Vec8fb const a, Vec8fb const b) {
return Vec8fb(a ^ Vec8fb(~b));
}
// vector operator != : xor
static inline Vec8fb operator != (Vec8fb const a, Vec8fb const b) {
return _mm256_xor_ps(a, b);
}
// vector operator ^= : bitwise xor
static inline Vec8fb & operator ^= (Vec8fb & a, Vec8fb const b) {
a = a ^ b;
return a;
}
// vector operator ! : logical not
// (operator ! is less efficient than operator ~. Use only where not all bits in an element are the same)
static inline Vec8fb operator ! (Vec8fb const a) {
return Vec8fb( !Vec8ib(a));
}
// Functions for Vec8fb
// andnot: a & ~ b
static inline Vec8fb andnot(Vec8fb const a, Vec8fb const b) {
return _mm256_andnot_ps(b, a);
}
// horizontal_and. Returns true if all bits are 1
static inline bool horizontal_and (Vec8fb const a) {
return _mm256_testc_ps(a,constant8f<0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu>()) != 0;
}
// horizontal_or. Returns true if at least one bit is 1
static inline bool horizontal_or (Vec8fb const a) {
return ! _mm256_testz_ps(a,a);
}
// to_bits: convert boolean vector to integer bitfield
static inline uint8_t to_bits(Vec8fb const x) {
return to_bits(Vec8ib(x));
}
#endif
/*****************************************************************************
*
* Vec4db: Vector of 4 Booleans for use with Vec4d
*
*****************************************************************************/
#if INSTRSET < 10 // broad boolean vectors
class Vec4db {
protected:
__m256d ymm; // double vector
public:
// Default constructor:
Vec4db() = default;
// Constructor to build from all elements:
Vec4db(bool b0, bool b1, bool b2, bool b3) {
#if INSTRSET >= 8 // AVX2
ymm = _mm256_castsi256_pd(_mm256_setr_epi64x(-(int64_t)b0, -(int64_t)b1, -(int64_t)b2, -(int64_t)b3));
#else
__m128 blo = _mm_castsi128_ps(_mm_setr_epi32(-(int)b0, -(int)b0, -(int)b1, -(int)b1));
__m128 bhi = _mm_castsi128_ps(_mm_setr_epi32(-(int)b2, -(int)b2, -(int)b3, -(int)b3));
ymm = _mm256_castps_pd(set_m128r(blo, bhi));
#endif
}
// Constructor to build from two Vec2db:
Vec4db(Vec2db const a0, Vec2db const a1) {
ymm = _mm256_castps_pd(set_m128r(_mm_castpd_ps(a0),_mm_castpd_ps(a1)));
//ymm = _mm256_set_m128d(a1, a0);
}
// Constructor to convert from type __m256d used in intrinsics:
Vec4db(__m256d const x) {
ymm = x;
}
// Assignment operator to convert from type __m256d used in intrinsics:
Vec4db & operator = (__m256d const x) {
ymm = x;
return *this;
}
// Constructor to broadcast the same value into all elements:
Vec4db(bool b) {
#if INSTRSET >= 8 // AVX2
ymm = _mm256_castsi256_pd(_mm256_set1_epi64x(-(int64_t)b));
#else
__m128 b1 = _mm_castsi128_ps(_mm_set1_epi32(-(int)b));
ymm = _mm256_castps_pd(set_m128r(b1,b1));
#endif
}
// Assignment operator to broadcast scalar value:
Vec4db & operator = (bool b) {
ymm = _mm256_castsi256_pd(_mm256_set1_epi32(-int32_t(b)));
return *this;
}
// Type cast operator to convert to __m256d used in intrinsics
operator __m256d() const {
return ymm;
}
#if INSTRSET >= 8 // 256 bit integer vectors are available, AVX2
// Constructor to convert from type Vec4qb used as Boolean for integer vectors
Vec4db(Vec4qb const x) {
ymm = _mm256_castsi256_pd(x);
}
// Assignment operator to convert from type Vec4qb used as Boolean for integer vectors
Vec4db & operator = (Vec4qb const x) {
ymm = _mm256_castsi256_pd(x);
return *this;
}
// Member function to change a bitfield to a boolean vector
Vec4db & load_bits(uint8_t a) {
Vec4qb b; b.load_bits(a);
ymm = _mm256_castsi256_pd(b);
return *this;
}
#ifndef FIX_CLANG_VECTOR_ALIAS_AMBIGUITY
// Type cast operator to convert to type Vec4qb used as Boolean for integer vectors
operator Vec4qb() const {
return _mm256_castpd_si256(ymm);
}
#endif
#else // 256 bit integer vectors emulated without AVX2
// Constructor to convert from type Vec4qb used as Boolean for integer vectors
Vec4db(Vec4qb const x) {
*this = Vec4db(_mm_castsi128_pd(x.get_low()), _mm_castsi128_pd(x.get_high()));
}
// Assignment operator to convert from type Vec4qb used as Boolean for integer vectors
Vec4db & operator = (Vec4qb const x) {
*this = Vec4db(_mm_castsi128_pd(x.get_low()), _mm_castsi128_pd(x.get_high()));
return *this;
}
// Type cast operator to convert to type Vec4qb used as Boolean for integer vectors
operator Vec4qb() const {
return Vec4q(_mm_castpd_si128(get_low()), _mm_castpd_si128(get_high()));
}
// Member function to change a bitfield to a boolean vector
// AVX version. Cannot use float instructions if subnormals are disabled
Vec4db & load_bits(uint8_t a) {
Vec2db a0 = Vec2db().load_bits(a);
Vec2db a1 = Vec2db().load_bits(uint8_t(a>>2u));
*this = Vec4db(a0, a1);
return *this;
}
#endif // AVX2
// Member function to change a single element in vector
Vec4db const insert(int index, bool value) {
const int32_t maskl[16] = {0,0,0,0,0,0,0,0,-1,-1,0,0,0,0,0,0};
const size_t two = 2; // avoid silly warning from MS compiler
__m256d mask = _mm256_loadu_pd((double const*)(maskl+8-(index&3)*two)); // mask with FFFFFFFFFFFFFFFF at index position
if (value) {
ymm = _mm256_or_pd(ymm,mask);
}
else {
ymm = _mm256_andnot_pd(mask,ymm);
}
return *this;
}
// Member function extract a single element from vector
bool extract(int index) const {
union {
double f[8];
int32_t i[16];
} u;
_mm256_storeu_pd(u.f, ymm);
return u.i[(index & 3) * 2 + 1] != 0;
}
// Extract a single element. Operator [] can only read an element, not write.
bool operator [] (int index) const {
return extract(index);
}
// Member functions to split into two Vec4fb:
Vec2db get_low() const {
return _mm256_castpd256_pd128(ymm);
}
Vec2db get_high() const {
return _mm256_extractf128_pd(ymm,1);
}
static constexpr int size() {
return 4;
}
static constexpr int elementtype() {
return 3;
}
// Prevent constructing from int, etc.
Vec4db(int b) = delete;
Vec4db & operator = (int x) = delete;
};
#else
typedef Vec4b Vec4db; // compact boolean vector
#endif
/*****************************************************************************
*
* Operators and functions for Vec4db
*
*****************************************************************************/
#if INSTRSET < 10 // broad boolean vectors
// vector operator & : bitwise and
static inline Vec4db operator & (Vec4db const a, Vec4db const b) {
return _mm256_and_pd(a, b);
}
static inline Vec4db operator && (Vec4db const a, Vec4db const b) {
return a & b;
}
// vector operator &= : bitwise and
static inline Vec4db & operator &= (Vec4db & a, Vec4db const b) {
a = a & b;
return a;
}
// vector operator | : bitwise or
static inline Vec4db operator | (Vec4db const a, Vec4db const b) {
return _mm256_or_pd(a, b);
}
static inline Vec4db operator || (Vec4db const a, Vec4db const b) {
return a | b;
}
// vector operator |= : bitwise or
static inline Vec4db & operator |= (Vec4db & a, Vec4db const b) {
a = a | b;
return a;
}
// vector operator ~ : bitwise not
static inline Vec4db operator ~ (Vec4db const a) {
return _mm256_xor_pd(a, _mm256_castps_pd (constant8f<0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu,0xFFFFFFFFu>()));
}
// vector operator ^ : bitwise xor
static inline Vec4db operator ^ (Vec4db const a, Vec4db const b) {
return _mm256_xor_pd(a, b);
}
// vector operator == : xnor
static inline Vec4db operator == (Vec4db const a, Vec4db const b) {
return Vec4db(a ^ Vec4db(~b));
}
// vector operator != : xor
static inline Vec4db operator != (Vec4db const a, Vec4db const b) {
return _mm256_xor_pd(a, b);
}
// vector operator ^= : bitwise xor
static inline Vec4db & operator ^= (Vec4db & a, Vec4db const b) {
a = a ^ b;
return a;
}
// vector operator ! : logical not
// (operator ! is less efficient than operator ~. Use only where not all bits in an element are the same)
static inline Vec4db operator ! (Vec4db const a) {
return Vec4db( ! Vec4qb(a));
}
// Functions for Vec8fb
// andnot: a & ~ b
static inline Vec4db andnot(Vec4db const a, Vec4db const b) {
return _mm256_andnot_pd(b, a);
}
// horizontal_and. Returns true if all bits are 1
static inline bool horizontal_and (Vec4db const a) {
#if INSTRSET >= 8 // 256 bit integer vectors are available, AVX2
return horizontal_and(Vec256b(_mm256_castpd_si256(a)));
#else // split into 128 bit vectors
return horizontal_and(a.get_low() & a.get_high());
#endif
}
// horizontal_or. Returns true if at least one bit is 1
static inline bool horizontal_or (Vec4db const a) {
#if INSTRSET >= 8 // 256 bit integer vectors are available, AVX2
return horizontal_or(Vec256b(_mm256_castpd_si256(a)));
#else // split into 128 bit vectors
return horizontal_or(a.get_low() | a.get_high());
#endif
}
// to_bits: convert boolean vector to integer bitfield
static inline uint8_t to_bits(Vec4db const x) {
return to_bits(Vec4qb(x));
}
#endif
/*****************************************************************************
*
* Vec8f: Vector of 8 single precision floating point values
*
*****************************************************************************/
class Vec8f {
protected:
__m256 ymm; // Float vector
public:
// Default constructor:
Vec8f() = default;
// Constructor to broadcast the same value into all elements:
Vec8f(float f) {
ymm = _mm256_set1_ps(f);
}
// Constructor to build from all elements:
Vec8f(float f0, float f1, float f2, float f3, float f4, float f5, float f6, float f7) {
ymm = _mm256_setr_ps(f0, f1, f2, f3, f4, f5, f6, f7);
}
// Constructor to build from two Vec4f:
Vec8f(Vec4f const a0, Vec4f const a1) {
ymm = set_m128r(a0, a1);
//ymm = _mm256_set_m128(a1, a0);
}
// Constructor to convert from type __m256 used in intrinsics:
Vec8f(__m256 const x) {
ymm = x;
}
// Assignment operator to convert from type __m256 used in intrinsics:
Vec8f & operator = (__m256 const x) {
ymm = x;
return *this;
}
// Type cast operator to convert to __m256 used in intrinsics
operator __m256() const {
return ymm;
}
// Member function to load from array (unaligned)
Vec8f & load(float const * p) {
ymm = _mm256_loadu_ps(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
Vec8f & load_a(float const * p) {
ymm = _mm256_load_ps(p);
return *this;
}
// Member function to store into array (unaligned)
void store(float * p) const {
_mm256_storeu_ps(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(float * p) const {
_mm256_store_ps(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(float * p) const {
_mm256_stream_ps(p, ymm);
}
// Partial load. Load n elements and set the rest to 0
Vec8f & load_partial(int n, float const * p) {
#if INSTRSET >= 10 // AVX512VL
ymm = _mm256_maskz_loadu_ps(__mmask8((1u << n) - 1), p);
#else
if (n > 0 && n <= 4) {
*this = Vec8f(Vec4f().load_partial(n, p), _mm_setzero_ps());
}
else if (n > 4 && n <= 8) {
*this = Vec8f(Vec4f().load(p), Vec4f().load_partial(n - 4, p + 4));
}
else {
ymm = _mm256_setzero_ps();
}
#endif
return *this;
}
// Partial store. Store n elements
void store_partial(int n, float * p) const {
#if INSTRSET >= 10 // AVX512VL
_mm256_mask_storeu_ps(p, __mmask8((1u << n) - 1), ymm);
#else
if (n <= 4) {
get_low().store_partial(n, p);
}
else if (n <= 8) {
get_low().store(p);
get_high().store_partial(n - 4, p + 4);
}
#endif
}
// cut off vector to n elements. The last 8-n elements are set to zero
Vec8f & cutoff(int n) {
#if INSTRSET >= 10
ymm = _mm256_maskz_mov_ps(__mmask8((1u << n) - 1), ymm);
#else
if (uint32_t(n) >= 8) return *this;
const union {
int32_t i[16];
float f[16];
} mask = {{-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0}};
*this = Vec8fb(*this) & Vec8fb(Vec8f().load(mask.f + 8 - n));
#endif
return *this;
}
// Member function to change a single element in vector
Vec8f const insert(int index, float value) {
#if INSTRSET >= 10 // AVX512VL
ymm = _mm256_mask_broadcastss_ps (ymm, __mmask8(1u << index), _mm_set_ss(value));
#else
__m256 v0 = _mm256_broadcast_ss(&value);
switch (index) {
case 0:
ymm = _mm256_blend_ps (ymm, v0, 1); break;
case 1:
ymm = _mm256_blend_ps (ymm, v0, 2); break;
case 2:
ymm = _mm256_blend_ps (ymm, v0, 4); break;
case 3:
ymm = _mm256_blend_ps (ymm, v0, 8); break;
case 4:
ymm = _mm256_blend_ps (ymm, v0, 0x10); break;
case 5:
ymm = _mm256_blend_ps (ymm, v0, 0x20); break;
case 6:
ymm = _mm256_blend_ps (ymm, v0, 0x40); break;
default:
ymm = _mm256_blend_ps (ymm, v0, 0x80); break;
}
#endif
return *this;
}
// Member function extract a single element from vector
float extract(int index) const {
#if INSTRSET >= 10
__m256 x = _mm256_maskz_compress_ps(__mmask8(1u << index), ymm);
return _mm256_cvtss_f32(x);
#else
float x[8];
store(x);
return x[index & 7];
#endif
}
// Extract a single element. Use store function if extracting more than one element.
// Operator [] can only read an element, not write.
float operator [] (int index) const {
return extract(index);
}
// Member functions to split into two Vec4f:
Vec4f get_low() const {
return _mm256_castps256_ps128(ymm);
}
Vec4f get_high() const {
return _mm256_extractf128_ps(ymm,1);
}
static constexpr int size() {
return 8;
}
static constexpr int elementtype() {
return 16;
}
typedef __m256 registertype;
};
/*****************************************************************************
*
* Operators for Vec8f
*
*****************************************************************************/
// vector operator + : add element by element
static inline Vec8f operator + (Vec8f const a, Vec8f const b) {
return _mm256_add_ps(a, b);
}
// vector operator + : add vector and scalar
static inline Vec8f operator + (Vec8f const a, float b) {
return a + Vec8f(b);
}
static inline Vec8f operator + (float a, Vec8f const b) {
return Vec8f(a) + b;
}
// vector operator += : add
static inline Vec8f & operator += (Vec8f & a, Vec8f const b) {
a = a + b;
return a;
}
// postfix operator ++
static inline Vec8f operator ++ (Vec8f & a, int) {
Vec8f a0 = a;
a = a + 1.0f;
return a0;
}
// prefix operator ++
static inline Vec8f & operator ++ (Vec8f & a) {
a = a + 1.0f;
return a;
}
// vector operator - : subtract element by element
static inline Vec8f operator - (Vec8f const a, Vec8f const b) {
return _mm256_sub_ps(a, b);
}
// vector operator - : subtract vector and scalar
static inline Vec8f operator - (Vec8f const a, float b) {
return a - Vec8f(b);
}
static inline Vec8f operator - (float a, Vec8f const b) {
return Vec8f(a) - b;
}
// vector operator - : unary minus
// Change sign bit, even for 0, INF and NAN
static inline Vec8f operator - (Vec8f const a) {
return _mm256_xor_ps(a, Vec8f(-0.0f));
}
// vector operator -= : subtract
static inline Vec8f & operator -= (Vec8f & a, Vec8f const b) {
a = a - b;
return a;
}
// postfix operator --
static inline Vec8f operator -- (Vec8f & a, int) {
Vec8f a0 = a;
a = a - 1.0f;
return a0;
}
// prefix operator --
static inline Vec8f & operator -- (Vec8f & a) {
a = a - 1.0f;
return a;
}
// vector operator * : multiply element by element
static inline Vec8f operator * (Vec8f const a, Vec8f const b) {
return _mm256_mul_ps(a, b);
}
// vector operator * : multiply vector and scalar
static inline Vec8f operator * (Vec8f const a, float b) {
return a * Vec8f(b);
}
static inline Vec8f operator * (float a, Vec8f const b) {
return Vec8f(a) * b;
}
// vector operator *= : multiply
static inline Vec8f & operator *= (Vec8f & a, Vec8f const b) {
a = a * b;
return a;
}
// vector operator / : divide all elements by same integer
static inline Vec8f operator / (Vec8f const a, Vec8f const b) {
return _mm256_div_ps(a, b);
}
// vector operator / : divide vector and scalar
static inline Vec8f operator / (Vec8f const a, float b) {
return a / Vec8f(b);
}
static inline Vec8f operator / (float a, Vec8f const b) {
return Vec8f(a) / b;
}
// vector operator /= : divide
static inline Vec8f & operator /= (Vec8f & a, Vec8f const b) {
a = a / b;
return a;
}
// vector operator == : returns true for elements for which a == b
static inline Vec8fb operator == (Vec8f const a, Vec8f const b) {
#if INSTRSET >= 10 // compact boolean vectors
return _mm256_cmp_ps_mask(a, b, 0);
#else
return _mm256_cmp_ps(a, b, 0);
#endif
}
// vector operator != : returns true for elements for which a != b
static inline Vec8fb operator != (Vec8f const a, Vec8f const b) {
#if INSTRSET >= 10 // compact boolean vectors
return _mm256_cmp_ps_mask(a, b, 4);
#else
return _mm256_cmp_ps(a, b, 4);
#endif
}
// vector operator < : returns true for elements for which a < b
static inline Vec8fb operator < (Vec8f const a, Vec8f const b) {
#if INSTRSET >= 10 // compact boolean vectors
return _mm256_cmp_ps_mask(a, b, 1);
#else
return _mm256_cmp_ps(a, b, 1);
#endif
}
// vector operator <= : returns true for elements for which a <= b
static inline Vec8fb operator <= (Vec8f const a, Vec8f const b) {
#if INSTRSET >= 10 // compact boolean vectors
return _mm256_cmp_ps_mask(a, b, 2);
#else
return _mm256_cmp_ps(a, b, 2);
#endif
}
// vector operator > : returns true for elements for which a > b
static inline Vec8fb operator > (Vec8f const a, Vec8f const b) {
#if INSTRSET >= 10 // compact boolean vectors
return _mm256_cmp_ps_mask(a, b, 6+8);
#else
return b < a;
#endif
}
// vector operator >= : returns true for elements for which a >= b
static inline Vec8fb operator >= (Vec8f const a, Vec8f const b) {
#if INSTRSET >= 10 // compact boolean vectors
return _mm256_cmp_ps_mask(a, b, 5+8);
#else
return b <= a;
#endif
}
// Bitwise logical operators
// vector operator & : bitwise and
static inline Vec8f operator & (Vec8f const a, Vec8f const b) {
return _mm256_and_ps(a, b);
}
// vector operator &= : bitwise and
static inline Vec8f & operator &= (Vec8f & a, Vec8f const b) {
a = a & b;
return a;
}
// vector operator & : bitwise and of Vec8f and Vec8fb
static inline Vec8f operator & (Vec8f const a, Vec8fb const b) {
#if INSTRSET >= 10 // compact boolean vectors
return _mm256_maskz_mov_ps(b, a);
#else
return _mm256_and_ps(a, b);
#endif
}
static inline Vec8f operator & (Vec8fb const a, Vec8f const b) {
return b & a;
}
// vector operator | : bitwise or
static inline Vec8f operator | (Vec8f const a, Vec8f const b) {
return _mm256_or_ps(a, b);
}
// vector operator |= : bitwise or
static inline Vec8f & operator |= (Vec8f & a, Vec8f const b) {
a = a | b;
return a;
}
// vector operator ^ : bitwise xor
static inline Vec8f operator ^ (Vec8f const a, Vec8f const b) {
return _mm256_xor_ps(a, b);
}
// vector operator ^= : bitwise xor
static inline Vec8f & operator ^= (Vec8f & a, Vec8f const b) {
a = a ^ b;
return a;
}
// vector operator ! : logical not. Returns Boolean vector
static inline Vec8fb operator ! (Vec8f const a) {
return a == Vec8f(0.0f);
}
/*****************************************************************************
*
* Functions for Vec8f
*
*****************************************************************************/
// Select between two operands. Corresponds to this pseudocode:
// for (int i = 0; i < 8; i++) result[i] = s[i] ? a[i] : b[i];
static inline Vec8f select (Vec8fb const s, Vec8f const a, Vec8f const b) {
#if INSTRSET >= 10 // compact boolean vectors
return _mm256_mask_mov_ps(b, s, a);
#else
return _mm256_blendv_ps (b, a, s);
#endif
}
// Conditional add: For all vector elements i: result[i] = f[i] ? (a[i] + b[i]) : a[i]
static inline Vec8f if_add (Vec8fb const f, Vec8f const a, Vec8f const b) {
#if INSTRSET >= 10
return _mm256_mask_add_ps (a, f, a, b);
#else
return a + (Vec8f(f) & b);
#endif
}
// Conditional subtract
static inline Vec8f if_sub (Vec8fb const f, Vec8f const a, Vec8f const b) {
#if INSTRSET >= 10
return _mm256_mask_sub_ps (a, f, a, b);
#else
return a - (Vec8f(f) & b);
#endif
}
// Conditional multiply
static inline Vec8f if_mul (Vec8fb const f, Vec8f const a, Vec8f const b) {
#if INSTRSET >= 10
return _mm256_mask_mul_ps (a, f, a, b);
#else
return a * select(f, b, 1.f);
#endif
}