-
Notifications
You must be signed in to change notification settings - Fork 148
/
vectori512e.h
2362 lines (2051 loc) · 78.7 KB
/
vectori512e.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
/**************************** vectori512e.h *******************************
* Author: Agner Fog
* Date created: 2014-07-23
* Last modified: 2023-06-03
* Version: 2.02.01
* Project: vector classes
* Description:
* Header file defining 512-bit integer vector classes for 32 and 64 bit integers.
* Emulated for processors without AVX512 instruction set.
*
* 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
*
* Each vector object is represented internally in the CPU as two 256-bit registers.
* This header file defines operators and functions for these vectors.
*
* (c) Copyright 2012-2023 Agner Fog.
* Apache License version 2.0 or later.
*****************************************************************************/
#ifndef VECTORI512E_H
#define VECTORI512E_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
#if defined (VECTORI512_H)
#error Two different versions of vectori512.h included
#endif
#ifdef VCL_NAMESPACE
namespace VCL_NAMESPACE {
#endif
/*****************************************************************************
*
* Vector of 512 bits
*
*****************************************************************************/
class Vec512b {
protected:
Vec256b z0; // low half
Vec256b z1; // high half
public:
// Default constructor:
Vec512b() = default;
// Constructor to build from two Vec256b:
Vec512b(Vec256b const a0, Vec256b const a1) {
z0 = a0; z1 = a1;
}
// Member function to load from array (unaligned)
Vec512b & load(void const * p) {
z0 = Vec8i().load(p);
z1 = Vec8i().load((int32_t const*)p+8);
return *this;
}
// Member function to load from array, aligned by 64
Vec512b & load_a(void const * p) {
z0 = Vec8i().load_a(p);
z1 = Vec8i().load_a((int32_t const*)p+8);
return *this;
}
// Member function to store into array (unaligned)
void store(void * p) const {
Vec8i(z0).store(p);
Vec8i(z1).store((int32_t*)p+8);
}
// Member function to store into array, aligned by 64
void store_a(void * p) const {
Vec8i(z0).store_a(p);
Vec8i(z1).store_a((int32_t*)p+8);
}
// 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 {
Vec8i(z0).store_nt(p);
Vec8i(z1).store_nt((int32_t*)p+8);
}
Vec256b get_low() const { // get low half
return z0;
}
Vec256b get_high() const { // get high half
return z1;
}
static constexpr int size() {
return 512;
}
};
// Define operators for this class
// vector operator & : bitwise and
static inline Vec512b operator & (Vec512b const a, Vec512b const b) {
return Vec512b(a.get_low() & b.get_low(), a.get_high() & b.get_high());
}
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 Vec512b(a.get_low() | b.get_low(), a.get_high() | b.get_high());
}
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 Vec512b(a.get_low() ^ b.get_low(), a.get_high() ^ b.get_high());
}
// vector operator ~ : bitwise not
static inline Vec512b operator ~ (Vec512b const a) {
return Vec512b(~a.get_low(), ~a.get_high());
}
// 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 Vec512b(andnot(a.get_low(), b.get_low()), andnot(a.get_high(), b.get_high()));
}
/*****************************************************************************
*
* Boolean vector (broad) base classes
*
*****************************************************************************/
class Vec16b : public Vec512b {
public:
// Default constructor:
Vec16b() = default;
// 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) {
*this = Vec512b(Vec8i(-(int)b0, -(int)b1, -(int)b2, -(int)b3, -(int)b4, -(int)b5, -(int)b6, -(int)b7), Vec8i(-(int)b8, -(int)b9, -(int)b10, -(int)b11, -(int)b12, -(int)b13, -(int)b14, -(int)b15));
}
// Constructor to convert from type Vec512b
Vec16b (Vec512b const & x) { // gcc requires const & here
z0 = x.get_low();
z1 = x.get_high();
}
// Constructor to make from two halves
Vec16b (Vec8ib const x0, Vec8ib const x1) {
z0 = x0;
z1 = x1;
}
// Constructor to make from two halves
Vec16b (Vec8i const x0, Vec8i const x1) {
z0 = x0;
z1 = x1;
}
// Constructor to broadcast single value:
Vec16b(bool b) {
z0 = z1 = Vec8i(-int32_t(b));
}
// Assignment operator to broadcast scalar value:
Vec16b & operator = (bool b) {
z0 = z1 = Vec8i(-int32_t(b));
return *this;
}
// split into two halves
Vec8ib get_low() const {
return Vec8ib(z0);
}
Vec8ib get_high() const {
return Vec8ib(z1);
}
/*
// Assignment operator to convert from type Vec512b
Vec16b & operator = (Vec512b const x) {
z0 = x.get_low();
z1 = x.get_high();
return *this;
} */
// 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(int index, bool value) {
if ((uint32_t)index < 8) {
z0 = Vec8ib(z0).insert(index, value);
}
else {
z1 = Vec8ib(z1).insert(index-8, value);
}
return *this;
}
// Member function extract a single element from vector
bool extract(int index) const {
if ((uint32_t)index < 8) {
return Vec8ib(z0).extract(index);
}
else {
return Vec8ib(z1).extract(index-8);
}
}
// Extract a single element. Operator [] can only read an element, not write.
bool operator [] (int index) const {
return extract(index);
}
static constexpr int size() {
return 16;
}
static constexpr int elementtype() {
return 3;
}
// Prevent constructing from int, etc. because of ambiguity
Vec16b(int b) = delete;
// Prevent assigning int because of ambiguity
Vec16b & operator = (int x) = delete;
};
// Define operators for this class
// vector operator & : bitwise and
static inline Vec16b operator & (Vec16b const a, Vec16b const b) {
return Vec16b(a.get_low() & b.get_low(), a.get_high() & b.get_high());
}
static inline Vec16b operator && (Vec16b const a, Vec16b const b) {
return a & b;
}
// vector operator | : bitwise or
static inline Vec16b operator | (Vec16b const a, Vec16b const b) {
return Vec16b(a.get_low() | b.get_low(), a.get_high() | b.get_high());
}
static inline Vec16b operator || (Vec16b const a, Vec16b const b) {
return a | b;
}
// vector operator ^ : bitwise xor
static inline Vec16b operator ^ (Vec16b const a, Vec16b const b) {
return Vec16b(a.get_low() ^ b.get_low(), a.get_high() ^ b.get_high());
}
// vector operator ~ : bitwise not
static inline Vec16b operator ~ (Vec16b const a) {
return Vec16b(~(a.get_low()), ~(a.get_high()));
}
// vector operator ! : element not
static inline Vec16b operator ! (Vec16b const a) {
return ~a;
}
// vector operator &= : bitwise and
static inline Vec16b & operator &= (Vec16b & a, Vec16b const b) {
a = a & b;
return a;
}
// vector operator |= : bitwise or
static inline Vec16b & operator |= (Vec16b & a, Vec16b const b) {
a = a | b;
return a;
}
// vector operator ^= : bitwise xor
static inline Vec16b & operator ^= (Vec16b & a, Vec16b const b) {
a = a ^ b;
return a;
}
/*****************************************************************************
*
* Functions for boolean vectors
*
*****************************************************************************/
// function andnot: a & ~ b
static inline Vec16b andnot (Vec16b const a, Vec16b const b) {
return Vec16b(Vec8ib(andnot(a.get_low(),b.get_low())), Vec8ib(andnot(a.get_high(),b.get_high())));
}
// horizontal_and. Returns true if all bits are 1
static inline bool horizontal_and (Vec16b const a) {
return horizontal_and(a.get_low() & a.get_high());
}
// horizontal_or. Returns true if at least one bit is 1
static inline bool horizontal_or (Vec16b const a) {
return horizontal_or(a.get_low() | a.get_high());
}
/*****************************************************************************
*
* Vec16ib: Vector of 16 Booleans for use with Vec16i and Vec16ui
*
*****************************************************************************/
class Vec16ib : public Vec16b {
public:
// Default constructor:
Vec16ib () = default;
/*
Vec16ib (Vec16b const & x) {
z0 = x.get_low();
z1 = x.get_high();
} */
// 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) {
z0 = Vec8ib(x0, x1, x2, x3, x4, x5, x6, x7);
z1 = Vec8ib(x8, x9, x10, x11, x12, x13, x14, x15);
}
// Constructor to convert from type Vec512b
Vec16ib (Vec512b const & x) {
z0 = x.get_low();
z1 = x.get_high();
}
// Construct from two halves
Vec16ib (Vec8ib const x0, Vec8ib const x1) {
z0 = x0;
z1 = x1;
}
// Assignment operator to convert from type Vec512b
Vec16ib & operator = (Vec512b const x) {
z0 = x.get_low();
z1 = x.get_high();
return *this;
}
// Constructor to broadcast scalar value:
Vec16ib(bool b) : Vec16b(b) {
}
// Assignment operator to broadcast scalar value:
Vec16ib & operator = (bool b) {
*this = Vec16b(b);
return *this;
}
// Member function to change a bitfield to a boolean vector
Vec16ib & load_bits(uint16_t a) {
z0 = Vec8ib().load_bits(uint8_t(a));
z1 = Vec8ib().load_bits(uint8_t(a>>8));
return *this;
}
// Prevent constructing from int, etc.
Vec16ib(int b) = delete;
Vec16ib & operator = (int x) = delete;
};
// Define operators for Vec16ib
// vector operator & : bitwise and
static inline Vec16ib operator & (Vec16ib const a, Vec16ib const b) {
return Vec16b(a) & Vec16b(b);
}
static inline Vec16ib operator && (Vec16ib const a, Vec16ib const b) {
return a & b;
}
// vector operator | : bitwise or
static inline Vec16ib operator | (Vec16ib const a, Vec16ib const b) {
return Vec16b(a) | Vec16b(b);
}
static inline Vec16ib operator || (Vec16ib const a, Vec16ib const b) {
return a | b;
}
// vector operator ^ : bitwise xor
static inline Vec16ib operator ^ (Vec16ib const a, Vec16ib const b) {
return Vec16b(a) ^ Vec16b(b);
}
// vector operator == : xnor
static inline Vec16ib operator == (Vec16ib const a, Vec16ib const b) {
return Vec16ib(Vec16b(a) ^ Vec16b(~b));
}
// vector operator != : xor
static inline Vec16ib operator != (Vec16ib const a, Vec16ib const b) {
return Vec16ib(a ^ b);
}
// vector operator ~ : bitwise not
static inline Vec16ib operator ~ (Vec16ib const a) {
return ~Vec16b(a);
}
// vector operator ! : element not
static inline Vec16ib operator ! (Vec16ib const a) {
return ~a;
}
// vector operator &= : bitwise and
static inline Vec16ib & operator &= (Vec16ib & a, Vec16ib const b) {
a = a & b;
return a;
}
// vector operator |= : bitwise or
static inline Vec16ib & operator |= (Vec16ib & a, Vec16ib const b) {
a = a | b;
return a;
}
// vector operator ^= : bitwise xor
static inline Vec16ib & operator ^= (Vec16ib & a, Vec16ib const b) {
a = a ^ b;
return a;
}
// vector function andnot
static inline Vec16ib andnot (Vec16ib const a, Vec16ib const b) {
return Vec16ib(andnot(Vec16b(a), Vec16b(b)));
}
/*****************************************************************************
*
* Vec8b: Base class vector of 8 Booleans
*
*****************************************************************************/
class Vec8b : public Vec16b {
public:
// Default constructor:
Vec8b () = default;
/*
Vec8b (Vec16b const & x) {
z0 = x.get_low();
z1 = x.get_high();
} */
// Constructor to convert from type Vec512b
Vec8b (Vec512b const & x) {
z0 = x.get_low();
z1 = x.get_high();
}
// construct from two halves
Vec8b (Vec4qb const x0, Vec4qb const x1) {
z0 = x0;
z1 = x1;
}
// Constructor to broadcast single value:
Vec8b(bool b) {
z0 = z1 = Vec8i(-int32_t(b));
}
// Assignment operator to broadcast scalar value:
Vec8b & operator = (bool b) {
z0 = z1 = Vec8i(-int32_t(b));
return *this;
}
// split into two halves
Vec4qb get_low() const {
return Vec4qb(z0);
}
Vec4qb get_high() const {
return Vec4qb(z1);
}
/*
// Assignment operator to convert from type Vec512b
Vec8b & operator = (Vec512b const x) {
z0 = x.get_low();
z1 = x.get_high();
return *this;
} */
// Member function to change a single element in vector
Vec8b const insert(int index, bool value) {
if ((uint32_t)index < 4) {
z0 = Vec4qb(z0).insert(index, value);
}
else {
z1 = Vec4qb(z1).insert(index-4, value);
}
return *this;
}
bool extract(int index) const {
if ((uint32_t)index < 4) {
return Vec4qb(Vec4q(z0)).extract(index);
}
else {
return Vec4qb(Vec4q(z1)).extract(index-4);
}
}
bool operator [] (int index) const {
return extract(index);
}
static constexpr int size() {
return 8;
}
// Prevent constructing from int, etc. because of ambiguity
Vec8b(int b) = delete;
// Prevent assigning int because of ambiguity
Vec8b & operator = (int x) = delete;
};
/*****************************************************************************
*
* Vec8qb: Vector of 8 Booleans for use with Vec8q and Vec8qu
*
*****************************************************************************/
class Vec8qb : public Vec8b {
public:
// Default constructor:
Vec8qb() = default;
Vec8qb (Vec16b const x) {
z0 = x.get_low();
z1 = x.get_high();
}
// Constructor to build from all elements:
Vec8qb(bool x0, bool x1, bool x2, bool x3, bool x4, bool x5, bool x6, bool x7) {
z0 = Vec4qb(x0, x1, x2, x3);
z1 = Vec4qb(x4, x5, x6, x7);
}
// Constructor to convert from type Vec512b
Vec8qb (Vec512b const & x) {
z0 = x.get_low();
z1 = x.get_high();
}
// construct from two halves
Vec8qb (Vec4qb const x0, Vec4qb const x1) {
z0 = x0;
z1 = x1;
}
// Assignment operator to convert from type Vec512b
Vec8qb & operator = (Vec512b const x) {
z0 = x.get_low();
z1 = x.get_high();
return *this;
}
// Constructor to broadcast single value:
Vec8qb(bool b) : Vec8b(b) {
}
// Assignment operator to broadcast scalar value:
Vec8qb & operator = (bool b) {
*this = Vec8b(b);
return *this;
}
// Member function to change a bitfield to a boolean vector
Vec8qb & load_bits(uint8_t a) {
z0 = Vec4qb().load_bits(a);
z1 = Vec4qb().load_bits(uint8_t(a>>4u));
return *this;
}
// Prevent constructing from int, etc. because of ambiguity
Vec8qb(int b) = delete;
// Prevent assigning int because of ambiguity
Vec8qb & operator = (int x) = delete;
};
// Define operators for Vec8qb
// vector operator & : bitwise and
static inline Vec8qb operator & (Vec8qb const a, Vec8qb const b) {
return Vec16b(a) & Vec16b(b);
}
static inline Vec8qb operator && (Vec8qb const a, Vec8qb const b) {
return a & b;
}
// vector operator | : bitwise or
static inline Vec8qb operator | (Vec8qb const a, Vec8qb const b) {
return Vec16b(a) | Vec16b(b);
}
static inline Vec8qb operator || (Vec8qb const a, Vec8qb const b) {
return a | b;
}
// vector operator ^ : bitwise xor
static inline Vec8qb operator ^ (Vec8qb const a, Vec8qb const b) {
return Vec16b(a) ^ Vec16b(b);
}
// vector operator == : xnor
static inline Vec8qb operator == (Vec8qb const a, Vec8qb const b) {
return Vec8qb(Vec16b(a) ^ Vec16b(~b));
}
// vector operator != : xor
static inline Vec8qb operator != (Vec8qb const a, Vec8qb const b) {
return Vec8qb(a ^ b);
}
// vector operator ~ : bitwise not
static inline Vec8qb operator ~ (Vec8qb const a) {
return ~Vec16b(a);
}
// vector operator ! : element not
static inline Vec8qb operator ! (Vec8qb const a) {
return ~a;
}
// vector operator &= : bitwise and
static inline Vec8qb & operator &= (Vec8qb & a, Vec8qb const b) {
a = a & b;
return a;
}
// vector operator |= : bitwise or
static inline Vec8qb & operator |= (Vec8qb & a, Vec8qb const b) {
a = a | b;
return a;
}
// vector operator ^= : bitwise xor
static inline Vec8qb & operator ^= (Vec8qb & a, Vec8qb const b) {
a = a ^ b;
return a;
}
// vector function andnot
static inline Vec8qb andnot (Vec8qb const a, Vec8qb const b) {
return Vec8qb(andnot(Vec16b(a), Vec16b(b)));
}
/*****************************************************************************
*
* 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) {
z0 = z1 = Vec8i(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) {
z0 = Vec8i(i0, i1, i2, i3, i4, i5, i6, i7);
z1 = Vec8i(i8, i9, i10, i11, i12, i13, i14, i15);
}
// Constructor to build from two Vec8i:
Vec16i(Vec8i const a0, Vec8i const a1) {
*this = Vec512b(a0, a1);
}
// Constructor to convert from type Vec512b
Vec16i(Vec512b const & x) {
z0 = x.get_low();
z1 = x.get_high();
}
// Assignment operator to convert from type Vec512b
Vec16i & operator = (Vec512b const x) {
z0 = x.get_low();
z1 = x.get_high();
return *this;
}
// Member function to load from array (unaligned)
Vec16i & load(void const * p) {
Vec512b::load(p);
return *this;
}
// Member function to load from array, aligned by 64
Vec16i & load_a(void const * p) {
Vec512b::load_a(p);
return *this;
}
// Partial load. Load n elements and set the rest to 0
Vec16i & load_partial(int n, void const * p) {
if (n < 8) {
z0 = Vec8i().load_partial(n, p);
z1 = Vec8i(0);
}
else {
z0 = Vec8i().load(p);
z1 = Vec8i().load_partial(n - 8, (int32_t const*)p + 8);
}
return *this;
}
// Partial store. Store n elements
void store_partial(int n, void * p) const {
if (n < 8) {
Vec8i(get_low()).store_partial(n, p);
}
else {
Vec8i(get_low()).store(p);
Vec8i(get_high()).store_partial(n - 8, (int32_t *)p + 8);
}
}
// cut off vector to n elements. The last 8-n elements are set to zero
Vec16i & cutoff(int n) {
if (n < 8) {
z0 = Vec8i(z0).cutoff(n);
z1 = Vec8i(0);
}
else {
z1 = Vec8i(z1).cutoff(n - 8);
}
return *this;
}
// Member function to change a single element in vector
Vec16i const insert(int index, int32_t value) {
if ((uint32_t)index < 8) {
z0 = Vec8i(z0).insert(index, value);
}
else {
z1 = Vec8i(z1).insert(index - 8, value);
}
return *this;
}
// Member function extract a single element from vector
int32_t extract(int index) const {
if ((uint32_t)index < 8) {
return Vec8i(z0).extract(index);
}
else {
return Vec8i(z1).extract(index - 8);
}
}
// 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 Vec8i(z0);
}
Vec8i get_high() const {
return Vec8i(z1);
}
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 Vec16i(a.get_low() + b.get_low(), a.get_high() + b.get_high());
}
// 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 Vec16i(a.get_low() - b.get_low(), a.get_high() - b.get_high());
}
// vector operator - : unary minus
static inline Vec16i operator - (Vec16i const a) {
return Vec16i(-a.get_low(), -a.get_high());
}
// 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 Vec16i(a.get_low() * b.get_low(), a.get_high() * b.get_high());
}
// 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 Vec16i(a.get_low() << b, a.get_high() << 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 Vec16i(a.get_low() >> b, a.get_high() >> 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 Vec16ib(a.get_low() == b.get_low(), a.get_high() == b.get_high());
}
// vector operator != : returns true for elements for which a != b
static inline Vec16ib operator != (Vec16i const a, Vec16i const b) {
return Vec16ib(a.get_low() != b.get_low(), a.get_high() != b.get_high());
}
// vector operator > : returns true for elements for which a > b
static inline Vec16ib operator > (Vec16i const a, Vec16i const b) {
return Vec16ib(a.get_low() > b.get_low(), a.get_high() > b.get_high());
}
// 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 Vec16ib(a.get_low() >= b.get_low(), a.get_high() >= b.get_high());
}
// 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 Vec16i(a.get_low() & b.get_low(), a.get_high() & b.get_high());
}
// 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 Vec16i(a.get_low() | b.get_low(), a.get_high() | b.get_high());
}
// 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 Vec16i(a.get_low() ^ b.get_low(), a.get_high() ^ b.get_high());
}
// 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 Vec16i(~(a.get_low()), ~(a.get_high()));
}
// 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 Vec16i(select(s.get_low(), a.get_low(), b.get_low()), select(s.get_high(), a.get_high(), b.get_high()));
}
// 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 Vec16i(if_add(f.get_low(), a.get_low(), b.get_low()), if_add(f.get_high(), a.get_high(), b.get_high()));
}
// Conditional subtract
static inline Vec16i if_sub (Vec16ib const f, Vec16i const a, Vec16i const b) {
return Vec16i(if_sub(f.get_low(), a.get_low(), b.get_low()), if_sub(f.get_high(), a.get_high(), b.get_high()));
}
// Conditional multiply
static inline Vec16i if_mul (Vec16ib const f, Vec16i const a, Vec16i const b) {
return Vec16i(if_mul(f.get_low(), a.get_low(), b.get_low()), if_mul(f.get_high(), a.get_high(), b.get_high()));
}
// Horizontal add: Calculates the sum of all vector elements. Overflow will wrap around
static inline int32_t horizontal_add (Vec16i const a) {
return horizontal_add(a.get_low() + a.get_high());
}
// function add_saturated: add element by element, signed with saturation
static inline Vec16i add_saturated(Vec16i const a, Vec16i const b) {
return Vec16i(add_saturated(a.get_low(), b.get_low()), add_saturated(a.get_high(), b.get_high()));
}
// function sub_saturated: subtract element by element, signed with saturation
static inline Vec16i sub_saturated(Vec16i const a, Vec16i const b) {
return Vec16i(sub_saturated(a.get_low(), b.get_low()), sub_saturated(a.get_high(), b.get_high()));
}
// function max: a > b ? a : b
static inline Vec16i max(Vec16i const a, Vec16i const b) {
return Vec16i(max(a.get_low(), b.get_low()), max(a.get_high(), b.get_high()));
}
// function min: a < b ? a : b
static inline Vec16i min(Vec16i const a, Vec16i const b) {
return Vec16i(min(a.get_low(), b.get_low()), min(a.get_high(), b.get_high()));
}
// function abs: a >= 0 ? a : -a
static inline Vec16i abs(Vec16i const a) {
return Vec16i(abs(a.get_low()), abs(a.get_high()));
}
// function abs_saturated: same as abs, saturate if overflow
static inline Vec16i abs_saturated(Vec16i const a) {
return Vec16i(abs_saturated(a.get_low()), abs_saturated(a.get_high()));
}
// function rotate_left all elements
// Use negative count to rotate right
static inline Vec16i rotate_left(Vec16i const a, int b) {
return Vec16i(rotate_left(a.get_low(), b), rotate_left(a.get_high(), 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) {