-
Notifications
You must be signed in to change notification settings - Fork 148
/
vectori512.h
2156 lines (1918 loc) · 84.4 KB
/
vectori512.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
/**************************** vectori512.h *******************************
* Author: Agner Fog
* Date created: 2014-07-23
* Last modified: 2023-06-03
* Version: 2.02.01
* Project: vector class library
* Description:
* Header file defining 512-bit integer vector classes for 32 and 64 bit integers.
* For x86 microprocessors with AVX512F and later instruction sets.
*
* Instructions: see vcl_manual.pdf
*
* 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
* Other 512-bit integer vectors are defined in Vectori512s.h
*
* Each vector object is represented internally in the CPU as a 512-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 VECTORI512_H
#define VECTORI512_H
#ifndef VECTORCLASS_H
#include "vectorclass.h"
#endif
#if VECTORCLASS_H < 20200
#error Incompatible versions of vector class library mixed
#endif
// check combination of header files
#ifdef VECTORI512E_H
#error Two different versions of vectori512.h included
#endif
#ifdef VCL_NAMESPACE
namespace VCL_NAMESPACE {
#endif
// Generate a constant vector of 16 integers stored in memory.
// Can be converted to any integer vector type
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,
uint32_t i8, uint32_t i9, uint32_t i10, uint32_t i11, uint32_t i12, uint32_t i13, uint32_t i14, uint32_t i15>
static inline __m512i constant16ui() {
/*
const union {
uint32_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;
*/
return _mm512_setr_epi32(i0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15);
}
/*****************************************************************************
*
* Boolean vector classes for AVX512
*
*****************************************************************************/
typedef Vec16b Vec16ib;
typedef Vec16b Vec16uib;
typedef Vec8b Vec8qb;
typedef Vec8b Vec8uqb;
/*****************************************************************************
*
* Vector of 512 bits. Used as base class for Vec16i and Vec8q
*
*****************************************************************************/
class Vec512b {
protected:
__m512i zmm; // Integer vector
public:
// Default constructor:
Vec512b() = default;
// 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 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 64
void store_nt(void * p) const {
_mm512_stream_si512((__m512i*)p, zmm);
}
// 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 constexpr int size() {
return 512;
}
static constexpr int elementtype() {
return 1;
}
typedef __m512i registertype;
};
// Define operators and functions 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;
}
// 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() = default;
// 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((1u << n) - 1), p);
return *this;
}
// Partial store. Store n elements
void store_partial(int n, void * p) const {
_mm512_mask_storeu_epi32(p, __mmask16((1u << 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((1u << n) - 1), zmm);
return *this;
}
// Member function to change a single element in vector
Vec16i const insert(int index, int32_t value) {
zmm = _mm512_mask_set1_epi32(zmm, __mmask16(1u << index), value);
return *this;
}
// Member function extract a single element from vector
int32_t extract(int index) const {
__m512i x = _mm512_maskz_compress_epi32(__mmask16(1u << index), zmm);
return _mm_cvtsi128_si32(_mm512_castsi512_si128(x));
}
// Extract a single element. Use store function if extracting more than one element.
// Operator [] can only read an element, not write.
int32_t operator [] (int 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 constexpr int size() {
return 16;
}
static constexpr int elementtype() {
return 8;
}
};
// 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_cmp_epi32_mask(a, b, 6);
}
// vector operator < : returns true for elements for which a < b
static inline Vec16ib operator < (Vec16i const a, Vec16i const b) {
return _mm512_cmp_epi32_mask(a, b, 1);
}
// vector operator >= : returns true for elements for which a >= b (signed)
static inline Vec16ib operator >= (Vec16i const a, Vec16i const b) {
return _mm512_cmp_epi32_mask(a, b, 5);
}
// vector operator <= : returns true for elements for which a <= b (signed)
static inline Vec16ib operator <= (Vec16i const a, Vec16i const b) {
return _mm512_cmp_epi32_mask(a, b, 2);
}
// 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);
// This is potentially faster, but not on any current compiler:
//return _mm512_ternarylogic_epi32(_mm512_undefined_epi32(), _mm512_undefined_epi32(), a, 0x55);
}
// 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);
}
// Conditional subtract
static inline Vec16i if_sub (Vec16ib const f, Vec16i const a, Vec16i const b) {
return _mm512_mask_sub_epi32(a, f, a, b);
}
// Conditional multiply
static inline Vec16i if_mul (Vec16ib const f, Vec16i const a, Vec16i const b) {
return _mm512_mask_mullo_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
static inline Vec16i abs_saturated(Vec16i const a) {
return _mm512_min_epu32(abs(a), Vec16i(0x7FFFFFFF));
}
// function rotate_left all elements
// Use negative count to rotate right
static inline Vec16i rotate_left(Vec16i const a, int b) {
return _mm512_rolv_epi32(a, Vec16i(b));
}
/*****************************************************************************
*
* Vector of 16 32-bit unsigned integers
*
*****************************************************************************/
class Vec16ui : public Vec16i {
public:
// Default constructor:
Vec16ui() = default;
// Constructor to broadcast the same value into all elements:
Vec16ui(uint32_t i) {
zmm = _mm512_set1_epi32((int32_t)i);
}
// Constructor to build from all elements:
Vec16ui(uint32_t i0, uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4, uint32_t i5, uint32_t i6, uint32_t i7,
uint32_t i8, uint32_t i9, uint32_t i10, uint32_t i11, uint32_t i12, uint32_t i13, uint32_t i14, uint32_t i15) {
zmm = _mm512_setr_epi32((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);
}
// Constructor to build from two Vec8ui:
Vec16ui(Vec8ui const a0, Vec8ui const a1) {
zmm = Vec16i(Vec8i(a0), Vec8i(a1));
}
// Constructor to convert from type __m512i used in intrinsics:
Vec16ui(__m512i const x) {
zmm = x;
}
// Assignment operator to convert from type __m512i used in intrinsics:
Vec16ui & operator = (__m512i const x) {
zmm = x;
return *this;
}
// Member function to load from array (unaligned)
Vec16ui & load(void const * p) {
Vec16i::load(p);
return *this;
}
// Member function to load from array, aligned by 64
Vec16ui & load_a(void const * p) {
Vec16i::load_a(p);
return *this;
}
// Member function to change a single element in vector
Vec16ui const insert(int index, uint32_t value) {
Vec16i::insert(index, (int32_t)value);
return *this;
}
// Member function extract a single element from vector
uint32_t extract(int index) const {
return (uint32_t)Vec16i::extract(index);
}
// Extract a single element. Use store function if extracting more than one element.
// Operator [] can only read an element, not write.
uint32_t operator [] (int index) const {
return extract(index);
}
// Member functions to split into two Vec4ui:
Vec8ui get_low() const {
return Vec8ui(Vec16i::get_low());
}
Vec8ui get_high() const {
return Vec8ui(Vec16i::get_high());
}
static constexpr int elementtype() {
return 9;
}
};
// Define operators for this class
// vector operator + : add
static inline Vec16ui operator + (Vec16ui const a, Vec16ui const b) {
return Vec16ui (Vec16i(a) + Vec16i(b));
}
// vector operator - : subtract
static inline Vec16ui operator - (Vec16ui const a, Vec16ui const b) {
return Vec16ui (Vec16i(a) - Vec16i(b));
}
// vector operator * : multiply
static inline Vec16ui operator * (Vec16ui const a, Vec16ui const b) {
return Vec16ui (Vec16i(a) * Vec16i(b));
}
// vector operator / : divide
// See bottom of file
// vector operator >> : shift right logical all elements
static inline Vec16ui operator >> (Vec16ui const a, uint32_t b) {
return _mm512_srl_epi32(a, _mm_cvtsi32_si128((int32_t)b));
}
static inline Vec16ui operator >> (Vec16ui const a, int32_t b) {
return a >> (uint32_t)b;
}
// vector operator >>= : shift right logical
static inline Vec16ui & operator >>= (Vec16ui & a, uint32_t b) {
a = a >> b;
return a;
}
// vector operator >>= : shift right logical
static inline Vec16ui & operator >>= (Vec16ui & a, int32_t b) {
a = a >> uint32_t(b);
return a;
}
// vector operator << : shift left all elements
static inline Vec16ui operator << (Vec16ui const a, uint32_t b) {
return Vec16ui ((Vec16i)a << (int32_t)b);
}
// vector operator << : shift left all elements
static inline Vec16ui operator << (Vec16ui const a, int32_t b) {
return Vec16ui ((Vec16i)a << (int32_t)b);
}
// vector operator < : returns true for elements for which a < b (unsigned)
static inline Vec16ib operator < (Vec16ui const a, Vec16ui const b) {
return _mm512_cmp_epu32_mask(a, b, 1);
}
// vector operator > : returns true for elements for which a > b (unsigned)
static inline Vec16ib operator > (Vec16ui const a, Vec16ui const b) {
return _mm512_cmp_epu32_mask(a, b, 6);
}
// vector operator >= : returns true for elements for which a >= b (unsigned)
static inline Vec16ib operator >= (Vec16ui const a, Vec16ui const b) {
return _mm512_cmp_epu32_mask(a, b, 5);
}
// vector operator <= : returns true for elements for which a <= b (unsigned)
static inline Vec16ib operator <= (Vec16ui const a, Vec16ui const b) {
return _mm512_cmp_epu32_mask(a, b, 2);
}
// vector operator & : bitwise and
static inline Vec16ui operator & (Vec16ui const a, Vec16ui const b) {
return Vec16ui(Vec16i(a) & Vec16i(b));
}
// vector operator | : bitwise or
static inline Vec16ui operator | (Vec16ui const a, Vec16ui const b) {
return Vec16ui(Vec16i(a) | Vec16i(b));
}
// vector operator ^ : bitwise xor
static inline Vec16ui operator ^ (Vec16ui const a, Vec16ui const b) {
return Vec16ui(Vec16i(a) ^ Vec16i(b));
}
// vector operator ~ : bitwise not
static inline Vec16ui operator ~ (Vec16ui const a) {
return Vec16ui( ~ Vec16i(a));
}
// 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 Vec16ui select (Vec16ib const s, Vec16ui const a, Vec16ui const b) {
return Vec16ui(select(s, Vec16i(a), Vec16i(b)));
}
// Conditional add: For all vector elements i: result[i] = f[i] ? (a[i] + b[i]) : a[i]
static inline Vec16ui if_add (Vec16ib const f, Vec16ui const a, Vec16ui const b) {
return Vec16ui(if_add(f, Vec16i(a), Vec16i(b)));
}
// Conditional subtract
static inline Vec16ui if_sub (Vec16ib const f, Vec16ui const a, Vec16ui const b) {
return Vec16ui(if_sub(f, Vec16i(a), Vec16i(b)));
}
// Conditional multiply
static inline Vec16ui if_mul (Vec16ib const f, Vec16ui const a, Vec16ui const b) {
return Vec16ui(if_mul(f, Vec16i(a), Vec16i(b)));
}
// Horizontal add: Calculates the sum of all vector elements. Overflow will wrap around
static inline uint32_t horizontal_add (Vec16ui const a) {
return (uint32_t)horizontal_add((Vec16i)a);
}
// horizontal_add_x: Horizontal add extended: Calculates the sum of all vector elements. Defined later in this file
// function add_saturated: add element by element, unsigned with saturation
static inline Vec16ui add_saturated(Vec16ui const a, Vec16ui const b) {
Vec16ui sum = a + b;
Vec16ib overflow = sum < (a | b); // overflow if (a + b) < (a | b)
return _mm512_mask_set1_epi32(sum, overflow, -1); // 0xFFFFFFFF if overflow
}
// function sub_saturated: subtract element by element, unsigned with saturation
static inline Vec16ui sub_saturated(Vec16ui const a, Vec16ui const b) {
Vec16ui diff = a - b;
return _mm512_maskz_mov_epi32(diff <= a, diff); // underflow if diff > a gives zero
}
// function max: a > b ? a : b
static inline Vec16ui max(Vec16ui const a, Vec16ui const b) {
return _mm512_max_epu32(a,b);
}
// function min: a < b ? a : b
static inline Vec16ui min(Vec16ui const a, Vec16ui const b) {
return _mm512_min_epu32(a,b);
}
/*****************************************************************************
*
* Vector of 8 64-bit signed integers
*
*****************************************************************************/
class Vec8q : public Vec512b {
public:
// Default constructor:
Vec8q() = default;
// Constructor to broadcast the same value into all elements:
Vec8q(int64_t i) {
zmm = _mm512_set1_epi64(i);
}
// Constructor to build from all elements:
Vec8q(int64_t i0, int64_t i1, int64_t i2, int64_t i3, int64_t i4, int64_t i5, int64_t i6, int64_t i7) {
zmm = _mm512_setr_epi64(i0, i1, i2, i3, i4, i5, i6, i7);
}
// Constructor to build from two Vec4q:
Vec8q(Vec4q const a0, Vec4q const a1) {
zmm = _mm512_inserti64x4(_mm512_castsi256_si512(a0), a1, 1);
}
// Constructor to convert from type __m512i used in intrinsics:
Vec8q(__m512i const x) {
zmm = x;
}
// Assignment operator to convert from type __m512i used in intrinsics:
Vec8q & 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)
Vec8q & load(void const * p) {
zmm = _mm512_loadu_si512(p);
return *this;
}
// Member function to load from array, aligned by 64
Vec8q & load_a(void const * p) {
zmm = _mm512_load_si512(p);
return *this;
}
// Partial load. Load n elements and set the rest to 0
Vec8q & load_partial(int n, void const * p) {
zmm = _mm512_maskz_loadu_epi64(__mmask16((1 << n) - 1), p);
return *this;
}
// Partial store. Store n elements
void store_partial(int n, void * p) const {
_mm512_mask_storeu_epi64(p, __mmask16((1 << n) - 1), zmm);
}
// cut off vector to n elements. The last 8-n elements are set to zero
Vec8q & cutoff(int n) {
zmm = _mm512_maskz_mov_epi64(__mmask16((1 << n) - 1), zmm);
return *this;
}
// Member function to change a single element in vector
Vec8q const insert(int index, int64_t value) {
#ifdef __x86_64__
zmm = _mm512_mask_set1_epi64(zmm, __mmask16(1 << index), value);
#else
__m512i v = Vec8q(value);
zmm = _mm512_mask_mov_epi64(zmm, __mmask16(1 << index), v);
#endif
return *this;
}
// Member function extract a single element from vector
int64_t extract(int index) const {
__m512i x = _mm512_maskz_compress_epi64(__mmask8(1u << index), zmm);
return _emulate_movq(_mm512_castsi512_si128(x));
}
// Extract a single element. Use store function if extracting more than one element.
// Operator [] can only read an element, not write.
int64_t operator [] (int index) const {
return extract(index);
}
// Member functions to split into two Vec2q:
Vec4q get_low() const {
return _mm512_castsi512_si256(zmm);
}
Vec4q get_high() const {
return _mm512_extracti64x4_epi64(zmm,1);
}
static constexpr int size() {
return 8;
}
static constexpr int elementtype() {
return 10;
}
};
// Define operators for Vec8q
// vector operator + : add element by element
static inline Vec8q operator + (Vec8q const a, Vec8q const b) {
return _mm512_add_epi64(a, b);
}
// vector operator += : add
static inline Vec8q & operator += (Vec8q & a, Vec8q const b) {
a = a + b;
return a;
}
// postfix operator ++
static inline Vec8q operator ++ (Vec8q & a, int) {
Vec8q a0 = a;
a = a + 1;
return a0;
}
// prefix operator ++
static inline Vec8q & operator ++ (Vec8q & a) {
a = a + 1;
return a;
}
// vector operator - : subtract element by element
static inline Vec8q operator - (Vec8q const a, Vec8q const b) {
return _mm512_sub_epi64(a, b);
}
// vector operator - : unary minus
static inline Vec8q operator - (Vec8q const a) {
return _mm512_sub_epi64(_mm512_setzero_epi32(), a);
}
// vector operator -= : subtract
static inline Vec8q & operator -= (Vec8q & a, Vec8q const b) {
a = a - b;
return a;
}
// postfix operator --
static inline Vec8q operator -- (Vec8q & a, int) {
Vec8q a0 = a;
a = a - 1;
return a0;
}
// prefix operator --
static inline Vec8q & operator -- (Vec8q & a) {
a = a - 1;
return a;
}
// vector operator * : multiply element by element
static inline Vec8q operator * (Vec8q const a, Vec8q const b) {
#if INSTRSET >= 10 // __AVX512DQ__
return _mm512_mullo_epi64(a, b);
#elif defined (__INTEL_COMPILER)
return _mm512_mullox_epi64(a, b); // _mm512_mullox_epi64 missing in gcc
#else
// instruction does not exist. Split into 32-bit multiplications
//__m512i ahigh = _mm512_shuffle_epi32(a, 0xB1); // swap H<->L
__m512i ahigh = _mm512_srli_epi64(a, 32); // high 32 bits of each a
__m512i bhigh = _mm512_srli_epi64(b, 32); // high 32 bits of each b
__m512i prodahb = _mm512_mul_epu32(ahigh, b); // ahigh*b
__m512i prodbha = _mm512_mul_epu32(bhigh, a); // bhigh*a
__m512i prodhl = _mm512_add_epi64(prodahb, prodbha); // sum of high*low products
__m512i prodhi = _mm512_slli_epi64(prodhl, 32); // same, shifted high
__m512i prodll = _mm512_mul_epu32(a, b); // alow*blow = 64 bit unsigned products
__m512i prod = _mm512_add_epi64(prodll, prodhi); // low*low+(high*low)<<32
return prod;
#endif
}
// vector operator *= : multiply
static inline Vec8q & operator *= (Vec8q & a, Vec8q const b) {
a = a * b;
return a;
}
// vector operator << : shift left
static inline Vec8q operator << (Vec8q const a, int32_t b) {
return _mm512_sll_epi64(a, _mm_cvtsi32_si128(b));
}
// vector operator <<= : shift left
static inline Vec8q & operator <<= (Vec8q & a, int32_t b) {
a = a << b;
return a;
}
// vector operator >> : shift right arithmetic
static inline Vec8q operator >> (Vec8q const a, int32_t b) {
return _mm512_sra_epi64(a, _mm_cvtsi32_si128(b));
}
// vector operator >>= : shift right arithmetic
static inline Vec8q & operator >>= (Vec8q & a, int32_t b) {
a = a >> b;
return a;
}
// vector operator == : returns true for elements for which a == b
static inline Vec8qb operator == (Vec8q const a, Vec8q const b) {
return Vec8qb(_mm512_cmpeq_epi64_mask(a, b));
}
// vector operator != : returns true for elements for which a != b
static inline Vec8qb operator != (Vec8q const a, Vec8q const b) {
return Vec8qb(_mm512_cmpneq_epi64_mask(a, b));
}
// vector operator < : returns true for elements for which a < b
static inline Vec8qb operator < (Vec8q const a, Vec8q const b) {
return _mm512_cmp_epi64_mask(a, b, 1);
}
// vector operator > : returns true for elements for which a > b
static inline Vec8qb operator > (Vec8q const a, Vec8q const b) {
return _mm512_cmp_epi64_mask(a, b, 6);
}
// vector operator >= : returns true for elements for which a >= b (signed)
static inline Vec8qb operator >= (Vec8q const a, Vec8q const b) {
return _mm512_cmp_epi64_mask(a, b, 5);
}
// vector operator <= : returns true for elements for which a <= b (signed)
static inline Vec8qb operator <= (Vec8q const a, Vec8q const b) {
return _mm512_cmp_epi64_mask(a, b, 2);
}
// vector operator & : bitwise and
static inline Vec8q operator & (Vec8q const a, Vec8q const b) {
return _mm512_and_epi32(a, b);
}
// vector operator &= : bitwise and
static inline Vec8q & operator &= (Vec8q & a, Vec8q const b) {
a = a & b;
return a;
}
// vector operator | : bitwise or
static inline Vec8q operator | (Vec8q const a, Vec8q const b) {
return _mm512_or_epi32(a, b);
}
// vector operator |= : bitwise or
static inline Vec8q & operator |= (Vec8q & a, Vec8q const b) {
a = a | b;
return a;
}
// vector operator ^ : bitwise xor
static inline Vec8q operator ^ (Vec8q const a, Vec8q const b) {
return _mm512_xor_epi32(a, b);
}
// vector operator ^= : bitwise xor
static inline Vec8q & operator ^= (Vec8q & a, Vec8q const b) {
a = a ^ b;
return a;
}
// vector operator ~ : bitwise not
static inline Vec8q operator ~ (Vec8q const a) {
return Vec8q(~ Vec16i(a));
//return _mm512_ternarylogic_epi64(_mm512_undefined_epi32(), _mm512_undefined_epi32(), a, 0x55);