forked from darealshinji/vectorclass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectori512.h
More file actions
2752 lines (2393 loc) · 103 KB
/
vectori512.h
File metadata and controls
2752 lines (2393 loc) · 103 KB
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
/**************************** vectori512.h *******************************
* Author: Agner Fog
* Date created: 2014-07-23
* Last modified: 2016-04-26
* Version: 1.22
* Project: vector classes
* Description:
* Header file defining integer vector classes as interface to intrinsic
* functions in x86 microprocessors with AVX512 and later instruction sets.
*
* Instructions:
* Use Gnu, Intel or Microsoft C++ compiler. Compile for the desired
* instruction set, which must be at least AVX512.
*
* The following vector classes are defined here:
* Vec16i Vector of 16 32-bit signed integers
* Vec16ui Vector of 16 32-bit unsigned integers
* Vec16ib Vector of 16 Booleans for use with Vec16i and Vec16ui
* Vec8q Vector of 8 64-bit signed integers
* Vec8uq Vector of 8 64-bit unsigned integers
* Vec8qb Vector of 8 Booleans for use with Vec8q and Vec8uq
*
* Each vector object is represented internally in the CPU as a 512-bit register.
* This header file defines operators and functions for these vectors.
*
* For detailed instructions, see VectorClass.pdf
*
* (c) Copyright 2014-2016 GNU General Public License http://www.gnu.org/licenses
*****************************************************************************/
// check combination of header files
#if defined (VECTORI512_H)
#if VECTORI512_H != 2
#error Two different versions of vectori512.h included
#endif
#else
#define VECTORI512_H 2
#ifdef VECTORF512_H
#error Please put header file vectori512.h before vectorf512.h
#endif
#if INSTRSET < 9 // AVX512 required
#error Wrong instruction set for vectori512.h, AVX512 required or use vectori512e.h
#endif
#include "vectori256.h"
#ifdef VCL_NAMESPACE
namespace VCL_NAMESPACE {
#endif
// Bug fix for missing intrinsics:
// _mm512_cmpgt_epu32_mask, _mm512_cmpgt_epu64_mask
// all typecast intrinsics
// Fix expected in GCC version 4.9.3 or 5.0. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61878
// questionable
// _mm512_mask_mov_epi32 check select(). Doc at https://software.intel.com/en-us/node/513888 is wrong. Bug report filed
#if defined (GCC_VERSION) && GCC_VERSION < 50000 && !defined(__INTEL_COMPILER) && !defined(__clang__)
static inline __m512i _mm512_castsi256_si512(__m256i x) {
union {
__m512i a;
__m256i b;
} u;
u.b = x;
return u.a;
}
static inline __m256i _mm512_castsi512_si256(__m512i x) {
union {
__m512i a;
__m256i b;
} u;
u.a = x;
return u.b;
}
static inline __m512i _mm512_castsi128_si512(__m128i x) {
union {
__m128i a;
__m512i b;
} u;
u.a = x;
return u.b;
}
static inline __m128i _mm512_castsi512_si128(__m512i x) {
union {
__m512i a;
__m128i b;
} u;
u.a = x;
return u.b;
}
#endif
/*****************************************************************************
*
* Generate compile-time constant vector
*
*****************************************************************************/
// Generate a constant vector of 8 integers stored in memory.
// Can be converted to any integer vector type
template <int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7,
int i8, int i9, int i10, int i11, int i12, int i13, int i14, int i15>
static inline __m512i constant16i() {
static const union {
int32_t i[16];
__m512i zmm;
} u = {{i0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15}};
return u.zmm;
}
/*****************************************************************************
*
* Boolean vector base classes for AVX512
*
*****************************************************************************/
class Vec16b {
protected:
__mmask16 m16; // Boolean vector
public:
// Default constructor:
Vec16b () {
}
// Constructor to convert from type __mmask16 used in intrinsics:
Vec16b (__mmask16 x) {
m16 = x;
}
// Constructor to build from all elements:
Vec16b(bool b0, bool b1, bool b2, bool b3, bool b4, bool b5, bool b6, bool b7,
bool b8, bool b9, bool b10, bool b11, bool b12, bool b13, bool b14, bool b15) {
m16 = uint16_t(b0 | b1<<1 | b2<<2 | b3<<3 | b4<<4 | b5<<5 | b6<<6 | b7<<7 |
b8<<8 | b9<<9 | b10<<10 | b11<<11 | b12<<12 | b13<<13 | b14<<14 | b15<<15);
}
// Constructor to broadcast single value:
Vec16b(bool b) {
m16 = __mmask16(-int16_t(b));
}
private: // Prevent constructing from int, etc.
Vec16b(int b);
public:
// Constructor to make from two halves
Vec16b (Vec8ib const & x0, Vec8ib const & x1) {
// = Vec16i(x0,x1) != 0; (not defined yet)
__m512i z = _mm512_inserti64x4(_mm512_castsi256_si512(x0), x1, 1);
m16 = _mm512_cmpneq_epi32_mask(z, _mm512_setzero_epi32());
}
// Assignment operator to convert from type __mmask16 used in intrinsics:
Vec16b & operator = (__mmask16 x) {
m16 = x;
return *this;
}
// Assignment operator to broadcast scalar value:
Vec16b & operator = (bool b) {
m16 = Vec16b(b);
return *this;
}
private: // Prevent assigning int because of ambiguity
Vec16b & operator = (int x);
public:
// Type cast operator to convert to __mmask16 used in intrinsics
operator __mmask16() const {
return m16;
}
// split into two halves
Vec8ib get_low() const {
return to_Vec8ib((uint8_t)m16);
}
Vec8ib get_high() const {
return to_Vec8ib((uint16_t)m16 >> 8);
}
// Member function to change a single element in vector
// Note: This function is inefficient. Use load function if changing more than one element
Vec16b const & insert(uint32_t index, bool value) {
m16 = __mmask16(((uint16_t)m16 & ~(1 << index)) | (int)value << index);
return *this;
}
// Member function extract a single element from vector
bool extract(uint32_t index) const {
return ((uint32_t)m16 >> index) & 1;
}
// Extract a single element. Operator [] can only read an element, not write.
bool operator [] (uint32_t index) const {
return extract(index);
}
static int size () {
return 16;
}
};
// Define operators for this class
// vector operator & : bitwise and
static inline Vec16b operator & (Vec16b a, Vec16b b) {
return _mm512_kand(a, b);
}
static inline Vec16b operator && (Vec16b a, Vec16b b) {
return a & b;
}
// vector operator | : bitwise or
static inline Vec16b operator | (Vec16b a, Vec16b b) {
return _mm512_kor(a, b);
}
static inline Vec16b operator || (Vec16b a, Vec16b b) {
return a | b;
}
// vector operator ^ : bitwise xor
static inline Vec16b operator ^ (Vec16b a, Vec16b b) {
return _mm512_kxor(a, b);
}
// vector operator ~ : bitwise not
static inline Vec16b operator ~ (Vec16b a) {
return _mm512_knot(a);
}
// vector operator ! : element not
static inline Vec16b operator ! (Vec16b a) {
return ~a;
}
// vector operator &= : bitwise and
static inline Vec16b & operator &= (Vec16b & a, Vec16b b) {
a = a & b;
return a;
}
// vector operator |= : bitwise or
static inline Vec16b & operator |= (Vec16b & a, Vec16b b) {
a = a | b;
return a;
}
// vector operator ^= : bitwise xor
static inline Vec16b & operator ^= (Vec16b & a, Vec16b b) {
a = a ^ b;
return a;
}
/*****************************************************************************
*
* Functions for boolean vectors
*
*****************************************************************************/
// function andnot: a & ~ b
static inline Vec16b andnot (Vec16b a, Vec16b b) {
return _mm512_kandn(b, a);
}
// horizontal_and. Returns true if all bits are 1
static inline bool horizontal_and (Vec16b const & a) {
return (uint16_t)(__mmask16)a == 0xFFFF;
}
// horizontal_or. Returns true if at least one bit is 1
static inline bool horizontal_or (Vec16b const & a) {
return (uint16_t)(__mmask16)a != 0;
}
/*****************************************************************************
*
* Vec16ib: Vector of 16 Booleans for use with Vec16i and Vec16ui
*
*****************************************************************************/
class Vec16ib : public Vec16b {
public:
// Default constructor:
Vec16ib () {
}
Vec16ib (Vec16b x) {
m16 = x;
}
// Constructor to build from all elements:
Vec16ib(bool x0, bool x1, bool x2, bool x3, bool x4, bool x5, bool x6, bool x7,
bool x8, bool x9, bool x10, bool x11, bool x12, bool x13, bool x14, bool x15) :
Vec16b(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15) {
}
// Constructor to convert from type __mmask16 used in intrinsics:
Vec16ib (__mmask16 x) {
m16 = x;
}
// Constructor to broadcast single value:
Vec16ib(bool b) : Vec16b(b) {}
private: // Prevent constructing from int, etc.
Vec16ib(int b);
public:
// Constructor to make from two halves
Vec16ib (Vec8ib const & x0, Vec8ib const & x1) {
m16 = Vec16b(x0, x1);
}
// Assignment operator to convert from type __mmask16 used in intrinsics:
Vec16ib & operator = (__mmask16 x) {
m16 = x;
return *this;
}
// Assignment operator to broadcast scalar value:
Vec16ib & operator = (bool b) {
m16 = Vec16b(b);
return *this;
}
private: // Prevent assigning int because of ambiguity
Vec16ib & operator = (int x);
public:
};
// Define operators for Vec16ib
// vector operator & : bitwise and
static inline Vec16ib operator & (Vec16ib a, Vec16ib b) {
return Vec16b(a) & Vec16b(b);
}
static inline Vec16ib operator && (Vec16ib a, Vec16ib b) {
return a & b;
}
// vector operator | : bitwise or
static inline Vec16ib operator | (Vec16ib a, Vec16ib b) {
return Vec16b(a) | Vec16b(b);
}
static inline Vec16ib operator || (Vec16ib a, Vec16ib b) {
return a | b;
}
// vector operator ^ : bitwise xor
static inline Vec16ib operator ^ (Vec16ib a, Vec16ib b) {
return Vec16b(a) ^ Vec16b(b);
}
// vector operator ~ : bitwise not
static inline Vec16ib operator ~ (Vec16ib a) {
return ~Vec16b(a);
}
// vector operator ! : element not
static inline Vec16ib operator ! (Vec16ib a) {
return ~a;
}
// vector operator &= : bitwise and
static inline Vec16ib & operator &= (Vec16ib & a, Vec16ib b) {
a = a & b;
return a;
}
// vector operator |= : bitwise or
static inline Vec16ib & operator |= (Vec16ib & a, Vec16ib b) {
a = a | b;
return a;
}
// vector operator ^= : bitwise xor
static inline Vec16ib & operator ^= (Vec16ib & a, Vec16ib b) {
a = a ^ b;
return a;
}
// vector function andnot
static inline Vec16ib andnot (Vec16ib a, Vec16ib b) {
return Vec16ib(andnot(Vec16b(a), Vec16b(b)));
}
/*****************************************************************************
*
* Vec8b: Base class vector of 8 Booleans
*
*****************************************************************************/
class Vec8b : public Vec16b {
public:
// Default constructor:
Vec8b () {
}
// Constructor to convert from type __mmask8 used in intrinsics:
Vec8b (__mmask8 x) {
m16 = x;
}
// Constructor to build from all elements:
Vec8b(bool b0, bool b1, bool b2, bool b3, bool b4, bool b5, bool b6, bool b7) {
m16 = uint16_t(b0 | b1<<1 | b2<<2 | b3<<3 | b4<<4 | b5<<5 | b6<<6 | b7<<7);
}
Vec8b (Vec16b const & x) {
m16 = __mmask8(x);
}
// Constructor to broadcast single value:
Vec8b(bool b) {
m16 = __mmask8(-int8_t(b));
}
// Assignment operator to convert from type __mmask8 used in intrinsics:
Vec8b & operator = (__mmask8 x) {
m16 = x;
return *this;
}
private: // Prevent constructing from int etc. because of ambiguity
Vec8b(int b);
Vec8b & operator = (int x);
public:
// split into two halves
Vec4qb get_low() const {
return Vec4qb(Vec4q(_mm512_castsi512_si256(_mm512_maskz_set1_epi64(__mmask16(m16), -1LL))));
}
Vec4qb get_high() const {
return Vec8b(__mmask8(m16 >> 4)).get_low();
}
static int size () {
return 8;
}
};
/*****************************************************************************
*
* Vec8qb: Vector of 8 Booleans for use with Vec8q and Vec8qu
*
*****************************************************************************/
class Vec8qb : public Vec8b {
public:
// Default constructor:
Vec8qb () {
}
Vec8qb (Vec16b x) {
m16 = x;
}
// Constructor to build from all elements:
Vec8qb(bool x0, bool x1, bool x2, bool x3, bool x4, bool x5, bool x6, bool x7) :
Vec8b(x0, x1, x2, x3, x4, x5, x6, x7) {
}
// Constructor to convert from type __mmask8 used in intrinsics:
Vec8qb (__mmask8 x) {
m16 = x;
}
// Assignment operator to convert from type __mmask8 used in intrinsics:
Vec8qb & operator = (__mmask8 x) {
m16 = x;
return *this;
}
// Constructor to broadcast single value:
Vec8qb(bool b) : Vec8b(b) {}
// Assignment operator to broadcast scalar:
Vec8qb & operator = (bool b) {
m16 = Vec8b(b);
return *this;
}
private: // Prevent constructing from int, etc.
Vec8qb(int b);
Vec8qb & operator = (int x);
public:
// Constructor to make from two halves
Vec8qb (Vec4qb const & x0, Vec4qb const & x1) {
// = Vec8q(x0,x1) != 0; (not defined yet)
__m512i z = _mm512_inserti64x4(_mm512_castsi256_si512(x0), x1, 1);
m16 = _mm512_cmpneq_epi64_mask(z, _mm512_setzero_si512());
}
};
// Define operators for Vec8qb
// vector operator & : bitwise and
static inline Vec8qb operator & (Vec8qb a, Vec8qb b) {
return Vec16b(a) & Vec16b(b);
}
static inline Vec8qb operator && (Vec8qb a, Vec8qb b) {
return a & b;
}
// vector operator | : bitwise or
static inline Vec8qb operator | (Vec8qb a, Vec8qb b) {
return Vec16b(a) | Vec16b(b);
}
static inline Vec8qb operator || (Vec8qb a, Vec8qb b) {
return a | b;
}
// vector operator ^ : bitwise xor
static inline Vec8qb operator ^ (Vec8qb a, Vec8qb b) {
return Vec16b(a) ^ Vec16b(b);
}
// vector operator ~ : bitwise not
static inline Vec8qb operator ~ (Vec8qb a) {
return ~Vec16b(a);
}
// vector operator ! : element not
static inline Vec8qb operator ! (Vec8qb a) {
return ~a;
}
// vector operator &= : bitwise and
static inline Vec8qb & operator &= (Vec8qb & a, Vec8qb b) {
a = a & b;
return a;
}
// vector operator |= : bitwise or
static inline Vec8qb & operator |= (Vec8qb & a, Vec8qb b) {
a = a | b;
return a;
}
// vector operator ^= : bitwise xor
static inline Vec8qb & operator ^= (Vec8qb & a, Vec8qb b) {
a = a ^ b;
return a;
}
// to_bits: convert to integer bitfield
static inline uint32_t to_bits(Vec8qb a) {
return (uint8_t)(__mmask16)a;
}
// vector function andnot
static inline Vec8qb andnot (Vec8qb a, Vec8qb b) {
return Vec8qb(andnot(Vec16b(a), Vec16b(b)));
}
/*****************************************************************************
*
* Vector of 512 1-bit unsigned integers (base class for Vec16i)
*
*****************************************************************************/
class Vec512b {
protected:
__m512i zmm; // Integer vector
public:
// Default constructor:
Vec512b() {
}
// Constructor to build from two Vec256b:
Vec512b(Vec256b const & a0, Vec256b const & a1) {
zmm = _mm512_inserti64x4(_mm512_castsi256_si512(a0), a1, 1);
}
// Constructor to convert from type __m512i used in intrinsics:
Vec512b(__m512i const & x) {
zmm = x;
}
// Assignment operator to convert from type __m512i used in intrinsics:
Vec512b & operator = (__m512i const & x) {
zmm = x;
return *this;
}
// Type cast operator to convert to __m512i used in intrinsics
operator __m512i() const {
return zmm;
}
// Member function to load from array (unaligned)
Vec512b & load(void const * p) {
zmm = _mm512_loadu_si512(p);
return *this;
}
// Member function to load from array, aligned by 64
// You may use load_a instead of load if you are certain that p points to an address
// divisible by 64, but there is hardly any speed advantage of load_a on modern processors
Vec512b & load_a(void const * p) {
zmm = _mm512_load_si512(p);
return *this;
}
// Member function to store into array (unaligned)
void store(void * p) const {
_mm512_storeu_si512(p, zmm);
}
// Member function to store into array, aligned by 64
// You may use store_a instead of store if you are certain that p points to an address
// divisible by 64, but there is hardly any speed advantage of store_a on modern processors
void store_a(void * p) const {
_mm512_store_si512(p, zmm);
}
// Member function to change a single bit, mainly for test purposes
// Note: This function is inefficient. Use load function if changing more than one bit
Vec512b const & set_bit(uint32_t index, int value) {
static uint64_t m[16] = {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0};
int wi = (index >> 6) & 7; // qword index
int bi = index & 0x3F; // bit index within qword w
__m512i mask = Vec512b().load(m+8-wi); // 1 in qword number wi
mask = _mm512_sll_epi64(mask,_mm_cvtsi32_si128(bi)); // mask with bit number b set
if (value & 1) {
zmm = _mm512_or_si512(mask,zmm);
}
else {
zmm = _mm512_andnot_si512(mask,zmm);
}
return *this;
}
// Member function to get a single bit, mainly for test purposes
// Note: This function is inefficient. Use store function if reading more than one bit
int get_bit(uint32_t index) const {
union {
__m512i z;
uint8_t i[64];
} u;
u.z = zmm;
int wi = (index >> 3) & 0x3F; // byte index
int bi = index & 7; // bit index within byte w
return (u.i[wi] >> bi) & 1;
}
// Member functions to split into two Vec256b:
Vec256b get_low() const {
return _mm512_castsi512_si256(zmm);
}
Vec256b get_high() const {
return _mm512_extracti64x4_epi64(zmm,1);
}
static int size () {
return 512;
}
};
// Define operators for this class
// vector operator & : bitwise and
static inline Vec512b operator & (Vec512b const & a, Vec512b const & b) {
return _mm512_and_epi32(a, b);
}
static inline Vec512b operator && (Vec512b const & a, Vec512b const & b) {
return a & b;
}
// vector operator | : bitwise or
static inline Vec512b operator | (Vec512b const & a, Vec512b const & b) {
return _mm512_or_epi32(a, b);
}
static inline Vec512b operator || (Vec512b const & a, Vec512b const & b) {
return a | b;
}
// vector operator ^ : bitwise xor
static inline Vec512b operator ^ (Vec512b const & a, Vec512b const & b) {
return _mm512_xor_epi32(a, b);
}
// vector operator ~ : bitwise not
static inline Vec512b operator ~ (Vec512b const & a) {
return _mm512_xor_epi32(a, _mm512_set1_epi32(-1));
}
// vector operator &= : bitwise and
static inline Vec512b & operator &= (Vec512b & a, Vec512b const & b) {
a = a & b;
return a;
}
// vector operator |= : bitwise or
static inline Vec512b & operator |= (Vec512b & a, Vec512b const & b) {
a = a | b;
return a;
}
// vector operator ^= : bitwise xor
static inline Vec512b & operator ^= (Vec512b & a, Vec512b const & b) {
a = a ^ b;
return a;
}
// Define functions for this class
// function andnot: a & ~ b
static inline Vec512b andnot (Vec512b const & a, Vec512b const & b) {
return _mm512_andnot_epi32(b, a);
}
/*****************************************************************************
*
* Vector of 16 32-bit signed integers
*
*****************************************************************************/
class Vec16i: public Vec512b {
public:
// Default constructor:
Vec16i() {
};
// Constructor to broadcast the same value into all elements:
Vec16i(int i) {
zmm = _mm512_set1_epi32(i);
};
// Constructor to build from all elements:
Vec16i(int32_t i0, int32_t i1, int32_t i2, int32_t i3, int32_t i4, int32_t i5, int32_t i6, int32_t i7,
int32_t i8, int32_t i9, int32_t i10, int32_t i11, int32_t i12, int32_t i13, int32_t i14, int32_t i15) {
zmm = _mm512_setr_epi32(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15);
};
// Constructor to build from two Vec8i:
Vec16i(Vec8i const & a0, Vec8i const & a1) {
zmm = _mm512_inserti64x4(_mm512_castsi256_si512(a0), a1, 1);
}
// Constructor to convert from type __m512i used in intrinsics:
Vec16i(__m512i const & x) {
zmm = x;
};
// Assignment operator to convert from type __m512i used in intrinsics:
Vec16i & operator = (__m512i const & x) {
zmm = x;
return *this;
};
// Type cast operator to convert to __m512i used in intrinsics
operator __m512i() const {
return zmm;
};
// Member function to load from array (unaligned)
Vec16i & load(void const * p) {
zmm = _mm512_loadu_si512(p);
return *this;
}
// Member function to load from array, aligned by 64
Vec16i & load_a(void const * p) {
zmm = _mm512_load_si512(p);
return *this;
}
// Partial load. Load n elements and set the rest to 0
Vec16i & load_partial(int n, void const * p) {
zmm = _mm512_maskz_loadu_epi32(__mmask16((1 << n) - 1), p);
return *this;
}
// Partial store. Store n elements
void store_partial(int n, void * p) const {
_mm512_mask_storeu_epi32(p, __mmask16((1 << n) - 1), zmm);
}
// cut off vector to n elements. The last 16-n elements are set to zero
Vec16i & cutoff(int n) {
zmm = _mm512_maskz_mov_epi32(__mmask16((1 << n) - 1), zmm);
return *this;
}
// Member function to change a single element in vector
Vec16i const & insert(uint32_t index, int32_t value) {
zmm = _mm512_mask_set1_epi32(zmm, __mmask16(1 << index), value);
return *this;
};
// Member function extract a single element from vector
int32_t extract(uint32_t index) const {
int32_t a[16];
store(a);
return a[index & 15];
}
// Extract a single element. Use store function if extracting more than one element.
// Operator [] can only read an element, not write.
int32_t operator [] (uint32_t index) const {
return extract(index);
}
// Member functions to split into two Vec8i:
Vec8i get_low() const {
return _mm512_castsi512_si256(zmm);
}
Vec8i get_high() const {
return _mm512_extracti64x4_epi64(zmm,1);
}
static int size () {
return 16;
}
};
// Define operators for Vec16i
// vector operator + : add element by element
static inline Vec16i operator + (Vec16i const & a, Vec16i const & b) {
return _mm512_add_epi32(a, b);
}
// vector operator += : add
static inline Vec16i & operator += (Vec16i & a, Vec16i const & b) {
a = a + b;
return a;
}
// postfix operator ++
static inline Vec16i operator ++ (Vec16i & a, int) {
Vec16i a0 = a;
a = a + 1;
return a0;
}
// prefix operator ++
static inline Vec16i & operator ++ (Vec16i & a) {
a = a + 1;
return a;
}
// vector operator - : subtract element by element
static inline Vec16i operator - (Vec16i const & a, Vec16i const & b) {
return _mm512_sub_epi32(a, b);
}
// vector operator - : unary minus
static inline Vec16i operator - (Vec16i const & a) {
return _mm512_sub_epi32(_mm512_setzero_epi32(), a);
}
// vector operator -= : subtract
static inline Vec16i & operator -= (Vec16i & a, Vec16i const & b) {
a = a - b;
return a;
}
// postfix operator --
static inline Vec16i operator -- (Vec16i & a, int) {
Vec16i a0 = a;
a = a - 1;
return a0;
}
// prefix operator --
static inline Vec16i & operator -- (Vec16i & a) {
a = a - 1;
return a;
}
// vector operator * : multiply element by element
static inline Vec16i operator * (Vec16i const & a, Vec16i const & b) {
return _mm512_mullo_epi32(a, b);
}
// vector operator *= : multiply
static inline Vec16i & operator *= (Vec16i & a, Vec16i const & b) {
a = a * b;
return a;
}
// vector operator / : divide all elements by same integer
// See bottom of file
// vector operator << : shift left
static inline Vec16i operator << (Vec16i const & a, int32_t b) {
return _mm512_sll_epi32(a, _mm_cvtsi32_si128(b));
}
// vector operator <<= : shift left
static inline Vec16i & operator <<= (Vec16i & a, int32_t b) {
a = a << b;
return a;
}
// vector operator >> : shift right arithmetic
static inline Vec16i operator >> (Vec16i const & a, int32_t b) {
return _mm512_sra_epi32(a, _mm_cvtsi32_si128(b));
}
// vector operator >>= : shift right arithmetic
static inline Vec16i & operator >>= (Vec16i & a, int32_t b) {
a = a >> b;
return a;
}
// vector operator == : returns true for elements for which a == b
static inline Vec16ib operator == (Vec16i const & a, Vec16i const & b) {
return _mm512_cmpeq_epi32_mask(a, b);
}
// vector operator != : returns true for elements for which a != b
static inline Vec16ib operator != (Vec16i const & a, Vec16i const & b) {
return _mm512_cmpneq_epi32_mask(a, b);
}
// vector operator > : returns true for elements for which a > b
static inline Vec16ib operator > (Vec16i const & a, Vec16i const & b) {
return _mm512_cmpgt_epi32_mask(a, b);
}
// vector operator < : returns true for elements for which a < b
static inline Vec16ib operator < (Vec16i const & a, Vec16i const & b) {
return b > a;
}
// vector operator >= : returns true for elements for which a >= b (signed)
static inline Vec16ib operator >= (Vec16i const & a, Vec16i const & b) {
return _mm512_cmpge_epi32_mask(a, b);
}
// vector operator <= : returns true for elements for which a <= b (signed)
static inline Vec16ib operator <= (Vec16i const & a, Vec16i const & b) {
return b >= a;
}
// vector operator & : bitwise and
static inline Vec16i operator & (Vec16i const & a, Vec16i const & b) {
return _mm512_and_epi32(a, b);
}
// vector operator &= : bitwise and
static inline Vec16i & operator &= (Vec16i & a, Vec16i const & b) {
a = a & b;
return a;
}
// vector operator | : bitwise or
static inline Vec16i operator | (Vec16i const & a, Vec16i const & b) {
return _mm512_or_epi32(a, b);
}
// vector operator |= : bitwise or
static inline Vec16i & operator |= (Vec16i & a, Vec16i const & b) {
a = a | b;
return a;
}
// vector operator ^ : bitwise xor
static inline Vec16i operator ^ (Vec16i const & a, Vec16i const & b) {
return _mm512_xor_epi32(a, b);
}
// vector operator ^= : bitwise xor
static inline Vec16i & operator ^= (Vec16i & a, Vec16i const & b) {
a = a ^ b;
return a;
}
// vector operator ~ : bitwise not
static inline Vec16i operator ~ (Vec16i const & a) {
return a ^ Vec16i(-1);
}
// Functions for this class
// Select between two operands. Corresponds to this pseudocode:
// for (int i = 0; i < 16; i++) result[i] = s[i] ? a[i] : b[i];
static inline Vec16i select (Vec16ib const & s, Vec16i const & a, Vec16i const & b) {
return _mm512_mask_mov_epi32(b, s, a); // conditional move may be optimized better by the compiler than blend
// return _mm512_mask_blend_epi32(s, b, a);
}
// Conditional add: For all vector elements i: result[i] = f[i] ? (a[i] + b[i]) : a[i]
static inline Vec16i if_add (Vec16ib const & f, Vec16i const & a, Vec16i const & b) {
return _mm512_mask_add_epi32(a, f, a, b);
}
// Horizontal add: Calculates the sum of all vector elements.
// Overflow will wrap around
static inline int32_t horizontal_add (Vec16i const & a) {
#if defined(__INTEL_COMPILER)
return _mm512_reduce_add_epi32(a);
#else
return horizontal_add(a.get_low() + a.get_high());
#endif
}
// function add_saturated: add element by element, signed with saturation
// (is it faster to up-convert to 64 bit integers, and then downconvert the sum with saturation?)
static inline Vec16i add_saturated(Vec16i const & a, Vec16i const & b) {
__m512i sum = _mm512_add_epi32(a, b); // a + b
__m512i axb = _mm512_xor_epi32(a, b); // check if a and b have different sign
__m512i axs = _mm512_xor_epi32(a, sum); // check if a and sum have different sign
__m512i ovf1 = _mm512_andnot_epi32(axb,axs); // check if sum has wrong sign
__m512i ovf2 = _mm512_srai_epi32(ovf1,31); // -1 if overflow
__mmask16 ovf3 = _mm512_cmpneq_epi32_mask(ovf2, _mm512_setzero_epi32()); // same, as mask
__m512i asign = _mm512_srli_epi32(a,31); // 1 if a < 0
__m512i sat1 = _mm512_srli_epi32(ovf2,1); // 7FFFFFFF if overflow
__m512i sat2 = _mm512_add_epi32(sat1,asign); // 7FFFFFFF if positive overflow 80000000 if negative overflow
return _mm512_mask_blend_epi32(ovf3, sum, sat2); // sum if not overflow, else sat2
}
// function sub_saturated: subtract element by element, signed with saturation
static inline Vec16i sub_saturated(Vec16i const & a, Vec16i const & b) {
__m512i diff = _mm512_sub_epi32(a, b); // a + b
__m512i axb = _mm512_xor_si512(a, b); // check if a and b have different sign
__m512i axs = _mm512_xor_si512(a, diff); // check if a and sum have different sign
__m512i ovf1 = _mm512_and_si512(axb,axs); // check if sum has wrong sign
__m512i ovf2 = _mm512_srai_epi32(ovf1,31); // -1 if overflow
__mmask16 ovf3 = _mm512_cmpneq_epi32_mask(ovf2, _mm512_setzero_epi32()); // same, as mask
__m512i asign = _mm512_srli_epi32(a,31); // 1 if a < 0
__m512i sat1 = _mm512_srli_epi32(ovf2,1); // 7FFFFFFF if overflow
__m512i sat2 = _mm512_add_epi32(sat1,asign); // 7FFFFFFF if positive overflow 80000000 if negative overflow
return _mm512_mask_blend_epi32(ovf3, diff, sat2); // sum if not overflow, else sat2
}
// function max: a > b ? a : b
static inline Vec16i max(Vec16i const & a, Vec16i const & b) {
return _mm512_max_epi32(a,b);
}
// function min: a < b ? a : b
static inline Vec16i min(Vec16i const & a, Vec16i const & b) {
return _mm512_min_epi32(a,b);
}
// function abs: a >= 0 ? a : -a
static inline Vec16i abs(Vec16i const & a) {
return _mm512_abs_epi32(a);
}
// function abs_saturated: same as abs, saturate if overflow