-
Notifications
You must be signed in to change notification settings - Fork 148
/
vectori128.h
6982 lines (6308 loc) · 262 KB
/
vectori128.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
/**************************** vectori128.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 128-bit integer vector classes
*
* Instructions: see vcl_manual.pdf
*
* The following vector classes are defined here:
* Vec128b Vector of 128 bits. Used internally as base class
* Vec16c Vector of 16 8-bit signed integers
* Vec16uc Vector of 16 8-bit unsigned integers
* Vec16cb Vector of 16 Booleans for use with Vec16c and Vec16uc
* Vec8s Vector of 8 16-bit signed integers
* Vec8us Vector of 8 16-bit unsigned integers
* Vec8sb Vector of 8 Booleans for use with Vec8s and Vec8us
* Vec4i Vector of 4 32-bit signed integers
* Vec4ui Vector of 4 32-bit unsigned integers
* Vec4ib Vector of 4 Booleans for use with Vec4i and Vec4ui
* Vec2q Vector of 2 64-bit signed integers
* Vec2uq Vector of 2 64-bit unsigned integers
* Vec2qb Vector of 2 Booleans for use with Vec2q and Vec2uq
*
* Each vector object is represented internally in the CPU as a 128-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 VECTORI128_H
#define VECTORI128_H
#ifndef VECTORCLASS_H
#include "vectorclass.h"
#endif
#if VECTORCLASS_H < 20200
#error Incompatible versions of vector class library mixed
#endif
#ifdef VCL_NAMESPACE // optional namespace
namespace VCL_NAMESPACE {
#endif
// Generate a constant vector of 4 integers stored in memory.
template <uint32_t i0, uint32_t i1, uint32_t i2, uint32_t i3>
static inline constexpr __m128i constant4ui() {
/*
const union {
uint32_t i[4];
__m128i xmm;
} u = { {i0,i1,i2,i3} };
return u.xmm;
*/
return _mm_setr_epi32(i0, i1, i2, i3);
}
/*****************************************************************************
*
* Compact boolean vectors
*
*****************************************************************************/
#if INSTRSET >= 9
class Vec8b; // allow forward reference to Vec8b
#if INSTRSET == 9 && MAX_VECTOR_SIZE >= 512 // special case of mixed compact and broad vectors
class Vec8ib;
class Vec8fb;
class Vec4qb;
class Vec4db;
#endif
// Compact vector of 16 booleans
class Vec16b {
protected:
__mmask16 mm; // Boolean mask register
public:
// Default constructor:
Vec16b() = default;
// Constructor to convert from type __mmask16 used in intrinsics
Vec16b(__mmask16 x) {
mm = 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) {
mm = uint16_t(
(uint16_t)b0 | (uint16_t)b1 << 1 | (uint16_t)b2 << 2 | (uint16_t)b3 << 3 |
(uint16_t)b4 << 4 | (uint16_t)b5 << 5 | (uint16_t)b6 << 6 | (uint16_t)b7 << 7 |
(uint16_t)b8 << 8 | (uint16_t)b9 << 9 | (uint16_t)b10 << 10 | (uint16_t)b11 << 11 |
(uint16_t)b12 << 12 | (uint16_t)b13 << 13 | (uint16_t)b14 << 14 | (uint16_t)b15 << 15);
}
// Constructor to broadcast single value:
Vec16b(bool b) {
mm = __mmask16(-int16_t(b));
}
// Constructor to make from two halves. Implemented below after declaration of Vec8b
inline Vec16b(Vec8b const x0, Vec8b const x1);
#if INSTRSET == 9 && MAX_VECTOR_SIZE >= 512 // special case of mixed compact and broad vectors
inline Vec16b(Vec8ib const x0, Vec8ib const x1); // in vectorf512.h
inline Vec16b(Vec8fb const x0, Vec8fb const x1); // in vectorf512.h
#endif
// Assignment operator to convert from type __mmask16 used in intrinsics:
Vec16b & operator = (__mmask16 x) {
mm = x;
return *this;
}
// Assignment operator to broadcast scalar value:
Vec16b & operator = (bool b) {
mm = Vec16b(b);
return *this;
}
// Type cast operator to convert to __mmask16 used in intrinsics
operator __mmask16() const {
return mm;
}
// split into two halves
#if INSTRSET >= 10
Vec8b get_low() const;
Vec8b get_high() const;
#elif INSTRSET == 9 && MAX_VECTOR_SIZE >= 512 // special case of mixed compact and broad vectors
Vec8ib get_low() const; // in vectorf512.h
Vec8ib get_high() const; // in vectorf512.h
#endif
// Member function to change a single element in vector
Vec16b const insert(int index, bool value) {
mm = __mmask16(((uint16_t)mm & ~(1 << index)) | (int)value << index);
return *this;
}
// Member function extract a single element from vector
bool extract(int index) const {
return ((uint32_t)mm >> index) & 1;
}
// Extract a single element. Operator [] can only read an element, not write.
bool operator [] (int index) const {
return extract(index);
}
// Member function to change a bitfield to a boolean vector
Vec16b & load_bits(uint16_t a) {
mm = __mmask16(a);
return *this;
}
// Number of elements
static constexpr int size() {
return 16;
}
// Type of elements
static constexpr int elementtype() {
return 2;
}
// I would like to prevent implicit conversion from int, but this is
// not possible because __mmask16 and int16_t are treated as the same type:
// Vec16b(int b) = delete;
// Vec16b & operator = (int x) = delete;
};
#if INSTRSET >= 10
class Vec2b;
class Vec4b;
#endif
// Compact vector of 8 booleans
class Vec8b {
#if INSTRSET < 10
// There is a problem in the case where we have AVX512F but not AVX512DQ:
// We have 8-bit masks, but 8-bit mask operations (KMOVB, KANDB, etc.) require AVX512DQ.
// We have to use 16-bit mask operations on 8-bit masks (KMOVW, KANDW, etc.).
// I don't know if this is necessary, but I am using __mmask16 rather than __mmask8
// in this case to avoid that the compiler generates 8-bit mask instructions.
// We may get warnings in MS compiler when using __mmask16 on intrinsic functions
// that require __mmask8, but I would rather have warnings than code that crashes.
#define Vec8b_masktype __mmask16
#else
#define Vec8b_masktype __mmask8
#endif
protected:
Vec8b_masktype mm; // Boolean mask register
public:
// Default constructor:
Vec8b() = default;
// Constructor to convert from type __mmask8 used in intrinsics
Vec8b(__mmask8 x) {
mm = __mmask8(x);
}
// Constructor to convert from type __mmask16 used in intrinsics
Vec8b(__mmask16 x) {
mm = Vec8b_masktype(x);
}
// Constructor to make from two halves
#if INSTRSET >= 10
inline Vec8b(Vec4b const x0, Vec4b const x1); // Implemented below after declaration of Vec4b
#elif INSTRSET == 9 && MAX_VECTOR_SIZE >= 512 // special case of mixed compact and broad vectors
inline Vec8b(Vec4qb const x0, Vec4qb const x1); // in vectorf512.h
inline Vec8b(Vec4db const x0, Vec4db const x1); // in vectorf512.h
#endif
// Assignment operator to convert from type __mmask16 used in intrinsics:
Vec8b & operator = (Vec8b_masktype x) {
mm = Vec8b_masktype(x);
return *this;
}
// Constructor to build from all elements:
Vec8b(bool b0, bool b1, bool b2, bool b3, bool b4, bool b5, bool b6, bool b7) {
mm = uint8_t(
(uint8_t)b0 | (uint8_t)b1 << 1 | (uint8_t)b2 << 2 | (uint8_t)b3 << 3 |
(uint8_t)b4 << 4 | (uint8_t)b5 << 5 | (uint8_t)b6 << 6 | (uint8_t)b7 << 7);
}
// Constructor to broadcast single value:
Vec8b(bool b) {
mm = Vec8b_masktype(-int16_t(b));
}
// Assignment operator to broadcast scalar value:
Vec8b & operator = (bool b) {
mm = Vec8b_masktype(Vec8b(b));
return *this;
}
// Type cast operator to convert to __mmask16 used in intrinsics
operator Vec8b_masktype() const {
return mm;
}
// split into two halves
#if INSTRSET >= 10
Vec4b get_low() const;
Vec4b get_high() const;
#elif INSTRSET == 9 && MAX_VECTOR_SIZE >= 512 // special case of mixed compact and broad vectors
Vec4qb get_low() const; // in vectorf512.h
Vec4qb get_high() const; // in vectorf512.h
#endif
// Member function to change a single element in vector
Vec8b const insert(int index, bool value) {
mm = Vec8b_masktype(((uint8_t)mm & ~(1 << index)) | (int)value << index);
return *this;
}
// Member function extract a single element from vector
bool extract(int index) const {
return ((uint32_t)mm >> index) & 1;
}
// Extract a single element. Operator [] can only read an element, not write.
bool operator [] (int index) const {
return extract(index);
}
// Member function to change a bitfield to a boolean vector
Vec8b & load_bits(uint8_t a) {
mm = Vec8b_masktype(a);
return *this;
}
// Number of elements
static constexpr int size() {
return 8;
}
// Type of elements
static constexpr int elementtype() {
return 2;
}
};
// Members of Vec16b that refer to Vec8b:
inline Vec16b::Vec16b(Vec8b const x0, Vec8b const x1) {
mm = __mmask16(uint8_t(x0) | uint16_t(x1) << 8);
}
#if INSTRSET >= 10
inline Vec8b Vec16b::get_low() const {
return Vec8b().load_bits(uint8_t(mm));
}
inline Vec8b Vec16b::get_high() const {
return Vec8b().load_bits(uint8_t((uint16_t)mm >> 8u));
}
#endif
#endif // INSTRSET >= 9
#if INSTRSET >= 10
class Vec4b : public Vec8b {
public:
// Default constructor:
Vec4b() = default;
// Constructor to make from two halves
inline Vec4b(Vec2b const x0, Vec2b const x1); // Implemented below after declaration of Vec4b
// Constructor to convert from type __mmask8 used in intrinsics
Vec4b(__mmask8 x) {
mm = x;
}
// Assignment operator to convert from type __mmask16 used in intrinsics:
Vec4b & operator = (__mmask8 x) {
mm = x;
return *this;
}
// Constructor to build from all elements:
Vec4b(bool b0, bool b1, bool b2, bool b3) {
mm = Vec8b_masktype((uint8_t)b0 | (uint8_t)b1 << 1 | (uint8_t)b2 << 2 | (uint8_t)b3 << 3);
}
// Constructor to broadcast single value:
Vec4b(bool b) {
mm = Vec8b_masktype(-int8_t(b) & 0x0F);
}
// Assignment operator to broadcast scalar value:
Vec4b & operator = (bool b) {
mm = Vec4b(b);
return *this;
}
// split into two halves
Vec2b get_low() const; // Implemented below after declaration of Vec4b
Vec2b get_high() const; // Implemented below after declaration of Vec4b
// Member function to change a bitfield to a boolean vector
Vec4b & load_bits(uint8_t a) {
mm = Vec8b_masktype(a & 0x0F);
return *this;
}
// Number of elements
static constexpr int size() {
return 4;
}
};
class Vec2b : public Vec8b {
public:
// Default constructor:
Vec2b() = default;
// Constructor to convert from type __mmask8 used in intrinsics
Vec2b(__mmask8 x) {
mm = x;
}
// Assignment operator to convert from type __mmask16 used in intrinsics:
Vec2b & operator = (__mmask8 x) {
mm = x;
return *this;
}
// Constructor to build from all elements:
Vec2b(bool b0, bool b1) {
mm = Vec8b_masktype((uint8_t)b0 | (uint8_t)b1 << 1);
}
// Constructor to broadcast single value:
Vec2b(bool b) {
mm = Vec8b_masktype(-int8_t(b) & 0x03);
}
// Assignment operator to broadcast scalar value:
Vec2b & operator = (bool b) {
mm = Vec2b(b);
return *this;
}
// Member function to change a bitfield to a boolean vector
Vec2b & load_bits(uint8_t a) {
mm = Vec8b_masktype(a & 0x03);
return *this;
}
// Number of elements
static constexpr int size() {
return 2;
}
};
// Members of Vec8b that refer to Vec4b:
inline Vec8b::Vec8b(Vec4b const x0, Vec4b const x1) {
mm = Vec8b_masktype((uint8_t(x0) & 0x0F) | (uint8_t(x1) << 4));
}
inline Vec4b Vec8b::get_low() const {
return Vec4b().load_bits(uint8_t(mm & 0xF));
}
inline Vec4b Vec8b::get_high() const {
return Vec4b().load_bits(uint8_t(mm >> 4u));
}
// Members of Vec4b that refer to Vec2b:
inline Vec4b::Vec4b(Vec2b const x0, Vec2b const x1) {
mm = Vec8b_masktype((uint8_t(x0) & 0x03) | (uint8_t(x1) << 2));
}
inline Vec2b Vec4b::get_low() const {
return Vec2b().load_bits(uint8_t(mm & 3));
}
inline Vec2b Vec4b::get_high() const {
return Vec2b().load_bits(uint8_t(mm >> 2u));
}
#endif
/*****************************************************************************
*
* Define operators and functions for Vec16b
*
*****************************************************************************/
#if INSTRSET >= 9
// vector operator & : and
static inline Vec16b operator & (Vec16b a, Vec16b b) {
return _mm512_kand(__mmask16(a), __mmask16(b));
}
static inline Vec16b operator && (Vec16b a, Vec16b b) {
return a & b;
}
// vector operator | : or
static inline Vec16b operator | (Vec16b a, Vec16b b) {
return _mm512_kor(__mmask16(a), __mmask16(b));
}
static inline Vec16b operator || (Vec16b a, Vec16b b) {
return a | b;
}
// vector operator ^ : xor
static inline Vec16b operator ^ (Vec16b a, Vec16b b) {
return _mm512_kxor(__mmask16(a), __mmask16(b));
}
// vector operator == : xnor
static inline Vec16b operator == (Vec16b a, Vec16b b) {
return _mm512_kxnor(__mmask16(a), __mmask16(b));
}
// vector operator != : xor
static inline Vec16b operator != (Vec16b a, Vec16b b) {
return a ^ b;
}
// vector operator ~ : not
static inline Vec16b operator ~ (Vec16b a) {
return _mm512_knot(__mmask16(a));
}
// vector operator ! : element not
static inline Vec16b operator ! (Vec16b a) {
return ~a;
}
// vector operator &= : and
static inline Vec16b & operator &= (Vec16b & a, Vec16b b) {
a = a & b;
return a;
}
// vector operator |= : or
static inline Vec16b & operator |= (Vec16b & a, Vec16b b) {
a = a | b;
return a;
}
// vector operator ^= : xor
static inline Vec16b & operator ^= (Vec16b & a, Vec16b b) {
a = a ^ b;
return a;
}
// horizontal_and. Returns true if all elements are true
static inline bool horizontal_and(Vec16b const a) {
return __mmask16(a) == 0xFFFF;
}
// horizontal_or. Returns true if at least one element is true
static inline bool horizontal_or(Vec16b const a) {
return __mmask16(a) != 0;
}
// function andnot: a & ~ b
static inline Vec16b andnot(Vec16b const a, Vec16b const b) {
return _mm512_kandn(b, a);
}
#endif
/*****************************************************************************
*
* Define operators and functions for Vec8b
*
*****************************************************************************/
#if INSTRSET >= 9 // compact boolean vectors
// vector operator & : and
static inline Vec8b operator & (Vec8b a, Vec8b b) {
#if INSTRSET >= 10 // 8-bit mask operations require AVX512DQ
// _kand_mask8(__mmask8(a), __mmask8(b)) // not defined
// must convert result to 8 bit, because bitwise operators promote everything to 32 bit results
return __mmask8(__mmask8(a) & __mmask8(b));
#else
return _mm512_kand(__mmask16(a), __mmask16(b));
#endif
}
static inline Vec8b operator && (Vec8b a, Vec8b b) {
return a & b;
}
// vector operator | : or
static inline Vec8b operator | (Vec8b a, Vec8b b) {
#if INSTRSET >= 10 // 8-bit mask operations require AVX512DQ
return __mmask8(__mmask8(a) | __mmask8(b)); // _kor_mask8(__mmask8(a), __mmask8(b));
#else
return _mm512_kor(__mmask16(a), __mmask16(b));
#endif
}
static inline Vec8b operator || (Vec8b a, Vec8b b) {
return a | b;
}
// vector operator ^ : xor
static inline Vec8b operator ^ (Vec8b a, Vec8b b) {
#if INSTRSET >= 10 // 8-bit mask operations require AVX512DQ
return __mmask8(__mmask8(a) ^ __mmask8(b)); // _kxor_mask8(__mmask8(a), __mmask8(b));
#else
return _mm512_kxor(__mmask16(a), __mmask16(b));
#endif
}
// vector operator == : xnor
static inline Vec8b operator == (Vec8b a, Vec8b b) {
#if INSTRSET >= 10 // 8-bit mask operations require AVX512DQ
return __mmask8(~(__mmask8(a) ^ __mmask8(b))); // _kxnor_mask8(__mmask8(a), __mmask8(b));
#else
return __mmask16(uint8_t(__mmask8(a) ^ __mmask8(b)));
#endif
}
// vector operator != : xor
static inline Vec8b operator != (Vec8b a, Vec8b b) {
return a ^ b;
}
// vector operator ~ : not
static inline Vec8b operator ~ (Vec8b a) {
#if INSTRSET >= 10 // 8-bit mask operations require AVX512DQ
return __mmask8(~__mmask8(a)); //_knot_mask8(__mmask8(a));
#else
return _mm512_knot(__mmask16(a));
#endif
}
// vector operator ! : element not
static inline Vec8b operator ! (Vec8b a) {
return ~a;
}
// vector operator &= : and
static inline Vec8b & operator &= (Vec8b & a, Vec8b b) {
a = a & b;
return a;
}
// vector operator |= : or
static inline Vec8b & operator |= (Vec8b & a, Vec8b b) {
a = a | b;
return a;
}
// vector operator ^= : xor
static inline Vec8b & operator ^= (Vec8b & a, Vec8b b) {
a = a ^ b;
return a;
}
// horizontal_and. Returns true if all elements are true
static inline bool horizontal_and(Vec8b const a) {
return uint8_t(Vec8b_masktype(a)) == 0xFFu;
}
// horizontal_or. Returns true if at least one element is true
static inline bool horizontal_or(Vec8b const a) {
return uint8_t(Vec8b_masktype(a)) != 0;
}
// function andnot: a & ~ b
static inline Vec8b andnot(Vec8b const a, Vec8b const b) {
return Vec8b_masktype(_mm512_kandn(b, a));
}
#endif
/*****************************************************************************
*
* Define operators for Vec4b
*
*****************************************************************************/
#if INSTRSET >= 10 // compact boolean vectors
// vector operator & : and
static inline Vec4b operator & (Vec4b a, Vec4b b) {
return __mmask8(__mmask8(a) & __mmask8(b)); // _kand_mask8(__mmask8(a), __mmask8(b)) // not defined
}
static inline Vec4b operator && (Vec4b a, Vec4b b) {
return a & b;
}
// vector operator | : or
static inline Vec4b operator | (Vec4b a, Vec4b b) {
return __mmask8(__mmask8(a) | __mmask8(b)); // _kor_mask8(__mmask8(a), __mmask8(b));
}
static inline Vec4b operator || (Vec4b a, Vec4b b) {
return a | b;
}
// vector operator ^ : xor
static inline Vec4b operator ^ (Vec4b a, Vec4b b) {
return __mmask8(__mmask8(a) ^ __mmask8(b)); // _kxor_mask8(__mmask8(a), __mmask8(b));
}
// vector operator ~ : not
static inline Vec4b operator ~ (Vec4b a) {
return __mmask8(__mmask8(a) ^ 0x0F);
}
// vector operator == : xnor
static inline Vec4b operator == (Vec4b a, Vec4b b) {
return ~(a ^ b);
}
// vector operator != : xor
static inline Vec4b operator != (Vec4b a, Vec4b b) {
return a ^ b;
}
// vector operator ! : element not
static inline Vec4b operator ! (Vec4b a) {
return ~a;
}
// vector operator &= : and
static inline Vec4b & operator &= (Vec4b & a, Vec4b b) {
a = a & b;
return a;
}
// vector operator |= : or
static inline Vec4b & operator |= (Vec4b & a, Vec4b b) {
a = a | b;
return a;
}
// vector operator ^= : xor
static inline Vec4b & operator ^= (Vec4b & a, Vec4b b) {
a = a ^ b;
return a;
}
// horizontal_and. Returns true if all elements are true
static inline bool horizontal_and(Vec4b const a) {
return (__mmask8(a) & 0x0F) == 0x0F;
}
// horizontal_or. Returns true if at least one element is true
static inline bool horizontal_or(Vec4b const a) {
return (__mmask8(a) & 0x0F) != 0;
}
// function andnot: a & ~ b
static inline Vec4b andnot(Vec4b const a, Vec4b const b) {
return __mmask8(andnot(Vec8b(a), Vec8b(b)));
}
/*****************************************************************************
*
* Define operators for Vec2b
*
*****************************************************************************/
// vector operator & : and
static inline Vec2b operator & (Vec2b a, Vec2b b) {
return __mmask8(__mmask8(a) & __mmask8(b)); // _kand_mask8(__mmask8(a), __mmask8(b)) // not defined
}
static inline Vec2b operator && (Vec2b a, Vec2b b) {
return a & b;
}
// vector operator | : or
static inline Vec2b operator | (Vec2b a, Vec2b b) {
return __mmask8(__mmask8(a) | __mmask8(b)); // _kor_mask8(__mmask8(a), __mmask8(b));
}
static inline Vec2b operator || (Vec2b a, Vec2b b) {
return a | b;
}
// vector operator ^ : xor
static inline Vec2b operator ^ (Vec2b a, Vec2b b) {
return __mmask8(__mmask8(a) ^ __mmask8(b)); // _kxor_mask8(__mmask8(a), __mmask8(b));
}
// vector operator ~ : not
static inline Vec2b operator ~ (Vec2b a) {
return __mmask8(__mmask8(a) ^ 0x03);
}
// vector operator == : xnor
static inline Vec2b operator == (Vec2b a, Vec2b b) {
return ~(a ^ b);
}
// vector operator != : xor
static inline Vec2b operator != (Vec2b a, Vec2b b) {
return a ^ b;
}
// vector operator ! : element not
static inline Vec2b operator ! (Vec2b a) {
return ~a;
}
// vector operator &= : and
static inline Vec2b & operator &= (Vec2b & a, Vec2b b) {
a = a & b;
return a;
}
// vector operator |= : or
static inline Vec2b & operator |= (Vec2b & a, Vec2b b) {
a = a | b;
return a;
}
// vector operator ^= : xor
static inline Vec2b & operator ^= (Vec2b & a, Vec2b b) {
a = a ^ b;
return a;
}
// horizontal_and. Returns true if all elements are true
static inline bool horizontal_and(Vec2b const a) {
return (__mmask8(a) & 0x03) == 0x03;
}
// horizontal_or. Returns true if at least one element is true
static inline bool horizontal_or(Vec2b const a) {
return (__mmask8(a) & 0x03) != 0;
}
// function andnot: a & ~ b
static inline Vec2b andnot(Vec2b const a, Vec2b const b) {
return __mmask8(andnot(Vec8b(a), Vec8b(b)));
}
#endif
/*****************************************************************************
*
* Vector of 128 bits. Used internally as base class
*
*****************************************************************************/
class Vec128b {
protected:
__m128i xmm; // Integer vector
public:
// Default constructor:
Vec128b() = default;
// Constructor to convert from type __m128i used in intrinsics:
Vec128b(__m128i const x) {
xmm = x;
}
// Assignment operator to convert from type __m128i used in intrinsics:
Vec128b & operator = (__m128i const x) {
xmm = x;
return *this;
}
// Type cast operator to convert to __m128i used in intrinsics
operator __m128i() const {
return xmm;
}
// Member function to load from array (unaligned)
Vec128b & load(void const * p) {
xmm = _mm_loadu_si128((__m128i const*)p);
return *this;
}
// Member function to load from array, aligned by 16
// "load_a" is faster than "load" on older Intel processors (Pentium 4, Pentium M, Core 1,
// Merom, Wolfdale, and Atom), but not on other processors from Intel, AMD or VIA.
// You may use load_a instead of load if you are certain that p points to an address
// divisible by 16.
void load_a(void const * p) {
xmm = _mm_load_si128((__m128i const*)p);
}
// Member function to store into array (unaligned)
void store(void * p) const {
_mm_storeu_si128((__m128i*)p, xmm);
}
// Member function storing into array, aligned by 16
// "store_a" is faster than "store" on older Intel processors (Pentium 4, Pentium M, Core 1,
// Merom, Wolfdale, and Atom), but not on other processors from Intel, AMD or VIA.
// You may use store_a instead of store if you are certain that p points to an address
// divisible by 16.
void store_a(void * p) const {
_mm_store_si128((__m128i*)p, xmm);
}
// Member function storing to aligned uncached memory (non-temporal store).
// This may be more efficient than store_a when storing large blocks of memory if it
// is unlikely that the data will stay in the cache until it is read again.
// Note: Will generate runtime error if p is not aligned by 16
void store_nt(void * p) const {
_mm_stream_si128((__m128i*)p, xmm);
}
static constexpr int size() {
return 128;
}
static constexpr int elementtype() {
return 1;
}
typedef __m128i registertype;
};
// Define operators for this class
// vector operator & : bitwise and
static inline Vec128b operator & (Vec128b const a, Vec128b const b) {
return _mm_and_si128(a, b);
}
static inline Vec128b operator && (Vec128b const a, Vec128b const b) {
return a & b;
}
// vector operator | : bitwise or
static inline Vec128b operator | (Vec128b const a, Vec128b const b) {
return _mm_or_si128(a, b);
}
static inline Vec128b operator || (Vec128b const a, Vec128b const b) {
return a | b;
}
// vector operator ^ : bitwise xor
static inline Vec128b operator ^ (Vec128b const a, Vec128b const b) {
return _mm_xor_si128(a, b);
}
// vector operator ~ : bitwise not
static inline Vec128b operator ~ (Vec128b const a) {
return _mm_xor_si128(a, _mm_set1_epi32(-1));
}
// vector operator &= : bitwise and
static inline Vec128b & operator &= (Vec128b & a, Vec128b const b) {
a = a & b;
return a;
}
// vector operator |= : bitwise or
static inline Vec128b & operator |= (Vec128b & a, Vec128b const b) {
a = a | b;
return a;
}
// vector operator ^= : bitwise xor
static inline Vec128b & operator ^= (Vec128b & a, Vec128b const b) {
a = a ^ b;
return a;
}
// Define functions for this class
// function andnot: a & ~ b
static inline Vec128b andnot(Vec128b const a, Vec128b const b) {
return _mm_andnot_si128(b, a);
}
/*****************************************************************************
*
* selectb function
*
*****************************************************************************/
// Select between two sources, byte by byte, using broad boolean vector s.
// Used in various functions and operators
// Corresponds to this pseudocode:
// for (int i = 0; i < 16; i++) result[i] = s[i] ? a[i] : b[i];
// Each byte in s must be either 0 (false) or 0xFF (true). No other values are allowed.
// The implementation depends on the instruction set:
// If SSE4.1 is supported then only bit 7 in each byte of s is checked,
// otherwise all bits in s are used.
static inline __m128i selectb(__m128i const s, __m128i const a, __m128i const b) {
#if INSTRSET >= 5 // SSE4.1
return _mm_blendv_epi8(b, a, s);
#else
return _mm_or_si128(_mm_and_si128(s, a), _mm_andnot_si128(s, b));
#endif
}
/*****************************************************************************
*
* Horizontal Boolean functions
*
*****************************************************************************/
static inline bool horizontal_and(Vec128b const a) {
#if INSTRSET >= 5 // SSE4.1. Use PTEST
return _mm_testc_si128(a, _mm_set1_epi32(-1)) != 0;
#else
__m128i t1 = _mm_unpackhi_epi64(a, a); // get 64 bits down
__m128i t2 = _mm_and_si128(a, t1); // and 64 bits
#ifdef __x86_64__
int64_t t5 = _mm_cvtsi128_si64(t2); // transfer 64 bits to integer
return t5 == int64_t(-1);
#else
__m128i t3 = _mm_srli_epi64(t2, 32); // get 32 bits down
__m128i t4 = _mm_and_si128(t2, t3); // and 32 bits
int t5 = _mm_cvtsi128_si32(t4); // transfer 32 bits to integer
return t5 == -1;
#endif // __x86_64__
#endif // INSTRSET
}
// horizontal_or. Returns true if at least one bit is 1
static inline bool horizontal_or(Vec128b const a) {
#if INSTRSET >= 5 // SSE4.1. Use PTEST
return !_mm_testz_si128(a, a);
#else
__m128i t1 = _mm_unpackhi_epi64(a, a); // get 64 bits down
__m128i t2 = _mm_or_si128(a, t1); // and 64 bits
#ifdef __x86_64__
int64_t t5 = _mm_cvtsi128_si64(t2); // transfer 64 bits to integer
return t5 != int64_t(0);
#else
__m128i t3 = _mm_srli_epi64(t2, 32); // get 32 bits down
__m128i t4 = _mm_or_si128(t2, t3); // and 32 bits
int t5 = _mm_cvtsi128_si32(t4); // transfer to integer
return t5 != 0;
#endif // __x86_64__
#endif // INSTRSET
}
/*****************************************************************************
*
* Vector of 16 8-bit signed integers
*
*****************************************************************************/
class Vec16c : public Vec128b {
public:
// Default constructor:
Vec16c() = default;
// Constructor to broadcast the same value into all elements:
Vec16c(int i) {
xmm = _mm_set1_epi8((char)i);
}
// Constructor to build from all elements:
Vec16c(int8_t i0, int8_t i1, int8_t i2, int8_t i3, int8_t i4, int8_t i5, int8_t i6, int8_t i7,
int8_t i8, int8_t i9, int8_t i10, int8_t i11, int8_t i12, int8_t i13, int8_t i14, int8_t i15) {
xmm = _mm_setr_epi8(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15);
}
// Constructor to convert from type __m128i used in intrinsics:
Vec16c(__m128i const x) {
xmm = x;
}
// Assignment operator to convert from type __m128i used in intrinsics:
Vec16c & operator = (__m128i const x) {
xmm = x;
return *this;
}
// Type cast operator to convert to __m128i used in intrinsics
operator __m128i() const {
return xmm;
}
// Member function to load from array (unaligned)
Vec16c & load(void const * p) {
xmm = _mm_loadu_si128((__m128i const*)p);
return *this;
}
// Member function to load from array (aligned)
Vec16c & load_a(void const * p) {
xmm = _mm_load_si128((__m128i const*)p);
return *this;
}
// Partial load. Load n elements and set the rest to 0
Vec16c & load_partial(int n, void const * p) {
#if INSTRSET >= 10 // AVX512VL + AVX512BW
xmm = _mm_maskz_loadu_epi8(__mmask16((1u << n) - 1), p);
#else
if (n >= 16) load(p);
else if (n <= 0) *this = 0;
else if (((int)(intptr_t)p & 0xFFF) < 0xFF0) {
// p is at least 16 bytes from a page boundary. OK to read 16 bytes
load(p);
}
else {
// worst case. read 1 byte at a time and suffer store forwarding penalty
// unless the compiler can optimize this
char x[16] = {0};
for (int i = 0; i < n; i++) x[i] = ((char const *)p)[i];
load(x);
}
cutoff(n);
#endif
return *this;
}
// Partial store. Store n elements
void store_partial(int n, void * p) const {
#if INSTRSET >= 10 // AVX512VL + AVX512BW
_mm_mask_storeu_epi8(p, __mmask16((1u << n) - 1), xmm);
#else // storing in bigger blocks may be unsafe unless compiler option -fno-strict-aliasing is specified,
// therefore we have to rely on the compiler to optimize this
int8_t s[16];
store(s);
if (uint32_t(n) > 16) n = 16;
for (int i = 0; i < n; i++) {
((int8_t*)p)[i] = s[i];
}
#endif