forked from darealshinji/vectorclass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectori128.h
6333 lines (5650 loc) · 251 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: 2016-05-30
* Version: 1.22
* Project: vector classes
* Description:
* Header file defining integer vector classes as interface to intrinsic
* functions in x86 microprocessors with SSE2 and later instruction sets
* up to AVX.
*
* Instructions:
* Use Gnu, Intel or Microsoft C++ compiler. Compile for the desired
* instruction set, which must be at least SSE2. Specify the supported
* instruction set by a command line define, e.g. __SSE4_1__ if the
* compiler does not automatically do so.
*
* The following vector classes are defined here:
* Vec128b Vector of 128 1-bit unsigned integers or Booleans
* 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.
*
* For example:
* Vec4i a(1,2,3,4), b(5,6,7,8), c;
* c = a + b; // now c contains (6,8,10,12)
*
* For detailed instructions, see VectorClass.pdf
*
* (c) Copyright 2012 - 2016 GNU General Public License http://www.gnu.org/licenses
*****************************************************************************/
#ifndef VECTORI128_H
#define VECTORI128_H
#include "instrset.h" // Select supported instruction set
#if INSTRSET < 2 // SSE2 required
#error Please compile for the SSE2 instruction set or higher
#endif
#ifdef VCL_NAMESPACE
namespace VCL_NAMESPACE {
#endif
/*****************************************************************************
*
* Vector of 128 1-bit unsigned integers or Booleans
*
*****************************************************************************/
class Vec128b {
protected:
__m128i xmm; // Integer vector
public:
// Default constructor:
Vec128b() {
}
// Constructor to broadcast the same value into all elements
// Removed because of undesired implicit conversions
// Vec128b(int i) {
// xmm = _mm_set1_epi32(-(i & 1));}
// 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 to store 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 to change a single bit
// Note: This function is inefficient. Use load function if changing more than one bit
Vec128b const & set_bit(uint32_t index, int value) {
static const union {
uint64_t i[4];
__m128i x[2];
} u = {{1,0,0,1}}; // 2 vectors with bit 0 and 64 set, respectively
int w = (index >> 6) & 1; // qword index
int bi = index & 0x3F; // bit index within qword w
__m128i mask = u.x[w];
mask = _mm_sll_epi64(mask,_mm_cvtsi32_si128(bi)); // mask with bit number b set
if (value & 1) {
xmm = _mm_or_si128(mask,xmm);
}
else {
xmm = _mm_andnot_si128(mask,xmm);
}
return *this;
}
// Member function to get a single bit
// Note: This function is inefficient. Use store function if reading more than one bit
int get_bit(uint32_t index) const {
union {
__m128i x;
uint8_t i[16];
} u;
u.x = xmm;
int w = (index >> 3) & 0xF; // byte index
int bi = index & 7; // bit index within byte w
return (u.i[w] >> bi) & 1;
}
// Extract a single element. Use store function if extracting more than one element.
// Operator [] can only read an element, not write.
bool operator [] (uint32_t index) const {
return get_bit(index) != 0;
}
static int size() {
return 128;
}
};
// 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);
}
/*****************************************************************************
*
* Generate compile-time constant vector
*
*****************************************************************************/
// Generate a constant vector of 4 integers stored in memory.
// Can be converted to any integer vector type
template <int i0, int i1, int i2, int i3>
static inline __m128i constant4i() {
static const union {
int i[4];
__m128i xmm;
} u = {{i0,i1,i2,i3}};
return u.xmm;
}
/*****************************************************************************
*
* selectb function
*
*****************************************************************************/
// Select between two sources, byte by byte. 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.
// TODO: detect compile-time constant selector and use an immediate blend if possible?
static inline __m128i selectb (__m128i const & s, __m128i const & a, __m128i const & b) {
#if INSTRSET >= 5 // SSE4.1 supported
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
*
*****************************************************************************/
// horizontal_and. Returns true if all bits are 1
static inline bool horizontal_and (Vec128b const & a) {
#if INSTRSET >= 5 // SSE4.1 supported. Use PTEST
return _mm_testc_si128(a,constant4i<-1,-1,-1,-1>()) != 0;
#else
__m128i cmp = _mm_cmpeq_epi32(a, constant4i<-1,-1,-1,-1>());
int mask = _mm_movemask_epi8(cmp);
return mask == 0xFFFF;
#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 supported. Use PTEST
return ! _mm_testz_si128(a,a);
#else
__m128i cmp = _mm_cmpeq_epi32(a, _mm_setzero_si128());
int mask = _mm_movemask_epi8(cmp);
return mask == 0xFFFF;
#endif // INSTRSET
}
/*****************************************************************************
*
* Vector of 16 8-bit signed integers
*
*****************************************************************************/
class Vec16c : public Vec128b {
public:
// Default constructor:
Vec16c() {
}
// 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 (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
char x[16];
for (int i = 0; i < n; i++) x[i] = ((char const *)p)[i];
load(x);
}
cutoff(n);
return *this;
}
// Partial store. Store n elements
void store_partial(int n, void * p) const {
if (n >= 16) {
store(p);
return;
}
if (n <= 0) return;
// we are not using _mm_maskmoveu_si128 because it is too slow on many processors
union {
int8_t c[16];
int16_t s[8];
int32_t i[4];
int64_t q[2];
} u;
store(u.c);
int j = 0;
if (n & 8) {
*(int64_t*)p = u.q[0];
j += 8;
}
if (n & 4) {
((int32_t*)p)[j/4] = u.i[j/4];
j += 4;
}
if (n & 2) {
((int16_t*)p)[j/2] = u.s[j/2];
j += 2;
}
if (n & 1) {
((int8_t*)p)[j] = u.c[j];
}
}
// cut off vector to n elements. The last 16-n elements are set to zero
Vec16c & cutoff(int n) {
if (uint32_t(n) >= 16) return *this;
static const char mask[32] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
*this &= Vec16c().load(mask+16-n);
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
Vec16c const & insert(uint32_t index, int8_t value) {
static const int8_t maskl[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
__m128i broad = _mm_set1_epi8(value); // broadcast value into all elements
__m128i mask = _mm_loadu_si128((__m128i const*)(maskl+16-(index & 0x0F))); // mask with FF at index position
xmm = selectb(mask,broad,xmm);
return *this;
}
// Member function extract a single element from vector
int8_t extract(uint32_t index) const {
int8_t x[16];
store(x);
return x[index & 0x0F];
}
// Extract a single element. Use store function if extracting more than one element.
// Operator [] can only read an element, not write.
int8_t operator [] (uint32_t index) const {
return extract(index);
}
static int size() {
return 16;
}
};
/*****************************************************************************
*
* Vec16cb: Vector of 16 Booleans for use with Vec16c and Vec16uc
*
*****************************************************************************/
class Vec16cb : public Vec16c {
public:
// Default constructor
Vec16cb() {}
// Constructor to build from all elements:
Vec16cb(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) {
xmm = Vec16c(-int8_t(x0), -int8_t(x1), -int8_t(x2), -int8_t(x3), -int8_t(x4), -int8_t(x5), -int8_t(x6), -int8_t(x7),
-int8_t(x8), -int8_t(x9), -int8_t(x10), -int8_t(x11), -int8_t(x12), -int8_t(x13), -int8_t(x14), -int8_t(x15));
}
// Constructor to convert from type __m128i used in intrinsics:
Vec16cb(__m128i const & x) {
xmm = x;
}
// Assignment operator to convert from type __m128i used in intrinsics:
Vec16cb & operator = (__m128i const & x) {
xmm = x;
return *this;
}
// Constructor to broadcast scalar value:
Vec16cb(bool b) : Vec16c(-int8_t(b)) {
}
// Assignment operator to broadcast scalar value:
Vec16cb & operator = (bool b) {
*this = Vec16cb(b);
return *this;
}
private: // Prevent constructing from int, etc.
Vec16cb(int b);
Vec16cb & operator = (int x);
public:
Vec16cb & insert (int index, bool a) {
Vec16c::insert(index, -(int)a);
return *this;
}
// Member function extract a single element from vector
bool extract(uint32_t index) const {
return Vec16c::extract(index) != 0;
}
// Extract a single element. Use store function if extracting more than one element.
// Operator [] can only read an element, not write.
bool operator [] (uint32_t index) const {
return extract(index);
}
};
/*****************************************************************************
*
* Define operators for Vec16cb
*
*****************************************************************************/
// vector operator & : bitwise and
static inline Vec16cb operator & (Vec16cb const & a, Vec16cb const & b) {
return Vec16cb(Vec128b(a) & Vec128b(b));
}
static inline Vec16cb operator && (Vec16cb const & a, Vec16cb const & b) {
return a & b;
}
// vector operator &= : bitwise and
static inline Vec16cb & operator &= (Vec16cb & a, Vec16cb const & b) {
a = a & b;
return a;
}
// vector operator | : bitwise or
static inline Vec16cb operator | (Vec16cb const & a, Vec16cb const & b) {
return Vec16cb(Vec128b(a) | Vec128b(b));
}
static inline Vec16cb operator || (Vec16cb const & a, Vec16cb const & b) {
return a | b;
}
// vector operator |= : bitwise or
static inline Vec16cb & operator |= (Vec16cb & a, Vec16cb const & b) {
a = a | b;
return a;
}
// vector operator ^ : bitwise xor
static inline Vec16cb operator ^ (Vec16cb const & a, Vec16cb const & b) {
return Vec16cb(Vec128b(a) ^ Vec128b(b));
}
// vector operator ^= : bitwise xor
static inline Vec16cb & operator ^= (Vec16cb & a, Vec16cb const & b) {
a = a ^ b;
return a;
}
// vector operator ~ : bitwise not
static inline Vec16cb operator ~ (Vec16cb const & a) {
return Vec16cb( ~ Vec128b(a));
}
// vector operator ! : element not
static inline Vec16cb operator ! (Vec16cb const & a) {
return ~ a;
}
// vector function andnot
static inline Vec16cb andnot (Vec16cb const & a, Vec16cb const & b) {
return Vec16cb(andnot(Vec128b(a), Vec128b(b)));
}
// Horizontal Boolean functions for Vec16cb
// horizontal_and. Returns true if all elements are true
static inline bool horizontal_and(Vec16cb const & a) {
return _mm_movemask_epi8(a) == 0xFFFF;
}
// horizontal_or. Returns true if at least one element is true
static inline bool horizontal_or(Vec16cb const & a) {
#if INSTRSET >= 5 // SSE4.1 supported. Use PTEST.
// Saves code size but can't macro-fuse with a jcc the way pmovmskb/test can. (And PTEST is 2 uops on Intel)
// Maybe only use PTEST if XOP or SSE4A are available? i.e. tuning for AMD Bulldozer-family or Jaguar
// where pmovmskb is the same number of m-ops as ptest
return !_mm_testz_si128(a, a);
#else
return _mm_movemask_epi8(a) != 0;
#endif
}
/*****************************************************************************
*
* Define operators for Vec16c
*
*****************************************************************************/
// vector operator + : add element by element
static inline Vec16c operator + (Vec16c const & a, Vec16c const & b) {
return _mm_add_epi8(a, b);
}
// vector operator += : add
static inline Vec16c & operator += (Vec16c & a, Vec16c const & b) {
a = a + b;
return a;
}
// postfix operator ++
static inline Vec16c operator ++ (Vec16c & a, int) {
Vec16c a0 = a;
a = a + 1;
return a0;
}
// prefix operator ++
static inline Vec16c & operator ++ (Vec16c & a) {
a = a + 1;
return a;
}
// vector operator - : subtract element by element
static inline Vec16c operator - (Vec16c const & a, Vec16c const & b) {
return _mm_sub_epi8(a, b);
}
// vector operator - : unary minus
static inline Vec16c operator - (Vec16c const & a) {
return _mm_sub_epi8(_mm_setzero_si128(), a);
}
// vector operator -= : add
static inline Vec16c & operator -= (Vec16c & a, Vec16c const & b) {
a = a - b;
return a;
}
// postfix operator --
static inline Vec16c operator -- (Vec16c & a, int) {
Vec16c a0 = a;
a = a - 1;
return a0;
}
// prefix operator --
static inline Vec16c & operator -- (Vec16c & a) {
a = a - 1;
return a;
}
// vector operator * : multiply element by element
static inline Vec16c operator * (Vec16c const & a, Vec16c const & b) {
// There is no 8-bit multiply in SSE2. Split into two 16-bit multiplies
__m128i aodd = _mm_srli_epi16(a,8); // odd numbered elements of a
__m128i bodd = _mm_srli_epi16(b,8); // odd numbered elements of b
__m128i muleven = _mm_mullo_epi16(a,b); // product of even numbered elements
__m128i mulodd = _mm_mullo_epi16(aodd,bodd); // product of odd numbered elements
mulodd = _mm_slli_epi16(mulodd,8); // put odd numbered elements back in place
__m128i mask = _mm_set1_epi32(0x00FF00FF); // mask for even positions
__m128i product = selectb(mask,muleven,mulodd); // interleave even and odd
return product;
}
// vector operator *= : multiply
static inline Vec16c & operator *= (Vec16c & a, Vec16c const & b) {
a = a * b;
return a;
}
// vector operator << : shift left all elements
static inline Vec16c operator << (Vec16c const & a, int b) {
uint32_t mask = (uint32_t)0xFF >> (uint32_t)b; // mask to remove bits that are shifted out
__m128i am = _mm_and_si128(a,_mm_set1_epi8((char)mask)); // remove bits that will overflow
__m128i res = _mm_sll_epi16(am,_mm_cvtsi32_si128(b));// 16-bit shifts
return res;
}
// vector operator <<= : shift left
static inline Vec16c & operator <<= (Vec16c & a, int b) {
a = a << b;
return a;
}
// vector operator >> : shift right arithmetic all elements
static inline Vec16c operator >> (Vec16c const & a, int b) {
__m128i aeven = _mm_slli_epi16(a,8); // even numbered elements of a. get sign bit in position
aeven = _mm_sra_epi16(aeven,_mm_cvtsi32_si128(b+8)); // shift arithmetic, back to position
__m128i aodd = _mm_sra_epi16(a,_mm_cvtsi32_si128(b)); // shift odd numbered elements arithmetic
__m128i mask = _mm_set1_epi32(0x00FF00FF); // mask for even positions
__m128i res = selectb(mask,aeven,aodd); // interleave even and odd
return res;
}
// vector operator >>= : shift right arithmetic
static inline Vec16c & operator >>= (Vec16c & a, int b) {
a = a >> b;
return a;
}
// vector operator == : returns true for elements for which a == b
static inline Vec16cb operator == (Vec16c const & a, Vec16c const & b) {
return _mm_cmpeq_epi8(a,b);
}
// vector operator != : returns true for elements for which a != b
static inline Vec16cb operator != (Vec16c const & a, Vec16c const & b) {
// TODO: AVX512 _mm_cmpneq_epi8_mask generates a mask, not a vector
#ifdef __XOP__ // AMD XOP instruction set
return (Vec16cb)_mm_comneq_epi8(a,b);
#else // SSE2 instruction set
return Vec16cb(Vec16c(~(a == b)));
#endif
}
// vector operator > : returns true for elements for which a > b (signed)
static inline Vec16cb operator > (Vec16c const & a, Vec16c const & b) {
return _mm_cmpgt_epi8(a,b);
}
// vector operator < : returns true for elements for which a < b (signed)
static inline Vec16cb operator < (Vec16c const & a, Vec16c const & b) {
return b > a;
}
// vector operator >= : returns true for elements for which a >= b (signed)
static inline Vec16cb operator >= (Vec16c const & a, Vec16c const & b) {
#ifdef __XOP__ // AMD XOP instruction set
return (Vec16cb)_mm_comge_epi8(a,b);
#else // SSE2 instruction set
return Vec16cb(Vec16c(~(b > a)));
#endif
}
// vector operator <= : returns true for elements for which a <= b (signed)
static inline Vec16cb operator <= (Vec16c const & a, Vec16c const & b) {
return b >= a;
}
// vector operator & : bitwise and
static inline Vec16c operator & (Vec16c const & a, Vec16c const & b) {
return Vec16c(Vec128b(a) & Vec128b(b));
}
static inline Vec16c operator && (Vec16c const & a, Vec16c const & b) {
return a & b;
}
// vector operator &= : bitwise and
static inline Vec16c & operator &= (Vec16c & a, Vec16c const & b) {
a = a & b;
return a;
}
// vector operator | : bitwise or
static inline Vec16c operator | (Vec16c const & a, Vec16c const & b) {
return Vec16c(Vec128b(a) | Vec128b(b));
}
static inline Vec16c operator || (Vec16c const & a, Vec16c const & b) {
return a | b;
}
// vector operator |= : bitwise or
static inline Vec16c & operator |= (Vec16c & a, Vec16c const & b) {
a = a | b;
return a;
}
// vector operator ^ : bitwise xor
static inline Vec16c operator ^ (Vec16c const & a, Vec16c const & b) {
return Vec16c(Vec128b(a) ^ Vec128b(b));
}
// vector operator ^= : bitwise xor
static inline Vec16c & operator ^= (Vec16c & a, Vec16c const & b) {
a = a ^ b;
return a;
}
// vector operator ~ : bitwise not
static inline Vec16c operator ~ (Vec16c const & a) {
return Vec16c( ~ Vec128b(a));
}
// vector operator ! : logical not, returns true for elements == 0
static inline Vec16cb operator ! (Vec16c const & a) {
return _mm_cmpeq_epi8(a,_mm_setzero_si128());
}
// 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];
// Each byte in s must be either 0 (false) or -1 (true). No other values are allowed.
static inline Vec16c select (Vec16cb const & s, Vec16c const & a, Vec16c const & b) {
return selectb(s,a,b);
}
// Conditional add: For all vector elements i: result[i] = f[i] ? (a[i] + b[i]) : a[i]
static inline Vec16c if_add (Vec16cb const & f, Vec16c const & a, Vec16c const & b) {
return a + (Vec16c(f) & b);
}
// function add_saturated: add element by element, signed with saturation
static inline Vec16c add_saturated(Vec16c const & a, Vec16c const & b) {
return _mm_adds_epi8(a, b);
}
// function sub_saturated: subtract element by element, signed with saturation
static inline Vec16c sub_saturated(Vec16c const & a, Vec16c const & b) {
return _mm_subs_epi8(a, b);
}
// function max: a > b ? a : b
static inline Vec16c max(Vec16c const & a, Vec16c const & b) {
#if INSTRSET >= 5 // SSE4.1
return _mm_max_epi8(a,b);
#else // SSE2
__m128i signbit = _mm_set1_epi32(0x80808080);
__m128i a1 = _mm_xor_si128(a,signbit); // add 0x80
__m128i b1 = _mm_xor_si128(b,signbit); // add 0x80
__m128i m1 = _mm_max_epu8(a1,b1); // unsigned max
return _mm_xor_si128(m1,signbit); // sub 0x80
#endif
}
// function min: a < b ? a : b
static inline Vec16c min(Vec16c const & a, Vec16c const & b) {
#if INSTRSET >= 5 // SSE4.1
return _mm_min_epi8(a,b);
#else // SSE2
__m128i signbit = _mm_set1_epi32(0x80808080);
__m128i a1 = _mm_xor_si128(a,signbit); // add 0x80
__m128i b1 = _mm_xor_si128(b,signbit); // add 0x80
__m128i m1 = _mm_min_epu8(a1,b1); // unsigned min
return _mm_xor_si128(m1,signbit); // sub 0x80
#endif
}
// function abs: a >= 0 ? a : -a
// returns -128 for that special-case.
static inline Vec16c abs(Vec16c const & a) {
#if INSTRSET >= 4 // SSSE3 supported
return _mm_abs_epi8(a);
#else // SSE2
__m128i nega = _mm_sub_epi8(_mm_setzero_si128(), a);
return _mm_min_epu8(a, nega); // unsigned min (the negative value is bigger when compared as unsigned)
#endif
}
// function abs_saturated: same as abs, saturate if overflow
static inline Vec16c abs_saturated(Vec16c const & a) {
__m128i absa = abs(a); // abs(a)
__m128i overfl = _mm_cmpgt_epi8(_mm_setzero_si128(),absa);// 0 > a
return _mm_add_epi8(absa,overfl); // subtract 1 if 0x80
}
// function rotate_left: rotate each element left by b bits
// Use negative count to rotate right
static inline Vec16c rotate_left(Vec16c const & a, int b) {
#ifdef __XOP__ // AMD XOP instruction set
return _mm_rot_epi8(a,_mm_set1_epi8(b));
#else // SSE2 instruction set
__m128i bb = _mm_cvtsi32_si128(b & 7); // b modulo 8
__m128i mbb = _mm_cvtsi32_si128((8-b) & 7); // 8-b modulo 8
__m128i maskeven = _mm_set1_epi32(0x00FF00FF); // mask for even numbered bytes
__m128i even = _mm_and_si128(a,maskeven); // even numbered bytes of a
__m128i odd = _mm_andnot_si128(maskeven,a); // odd numbered bytes of a
__m128i evenleft = _mm_sll_epi16(even,bb); // even bytes of a << b
__m128i oddleft = _mm_sll_epi16(odd,bb); // odd bytes of a << b
__m128i evenright = _mm_srl_epi16(even,mbb); // even bytes of a >> 8-b
__m128i oddright = _mm_srl_epi16(odd,mbb); // odd bytes of a >> 8-b
__m128i evenrot = _mm_or_si128(evenleft,evenright); // even bytes of a rotated
__m128i oddrot = _mm_or_si128(oddleft,oddright); // odd bytes of a rotated
__m128i allrot = selectb(maskeven,evenrot,oddrot); // all bytes rotated
return allrot;
#endif
}
/*****************************************************************************
*
* Vector of 16 8-bit unsigned integers
*
*****************************************************************************/
class Vec16uc : public Vec16c {
public:
// Default constructor:
Vec16uc() {
}
// Constructor to broadcast the same value into all elements:
Vec16uc(uint32_t i) {
xmm = _mm_set1_epi8((char)i);
}
// Constructor to build from all elements:
Vec16uc(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3, uint8_t i4, uint8_t i5, uint8_t i6, uint8_t i7,
uint8_t i8, uint8_t i9, uint8_t i10, uint8_t i11, uint8_t i12, uint8_t i13, uint8_t i14, uint8_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:
Vec16uc(__m128i const & x) {
xmm = x;
}
// Assignment operator to convert from type __m128i used in intrinsics:
Vec16uc & operator = (__m128i const & x) {
xmm = x;
return *this;
}
// Member function to load from array (unaligned)
Vec16uc & load(void const * p) {
xmm = _mm_loadu_si128((__m128i const*)p);
return *this;
}
// Member function to load from array (aligned)
Vec16uc & load_a(void const * p) {
xmm = _mm_load_si128((__m128i const*)p);
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
Vec16uc const & insert(uint32_t index, uint8_t value) {
Vec16c::insert(index, value);
return *this;
}
// Member function extract a single element from vector
uint8_t extract(uint32_t index) const {
return Vec16c::extract(index);
}
// Extract a single element. Use store function if extracting more than one element.
// Operator [] can only read an element, not write.
uint8_t operator [] (uint32_t index) const {
return extract(index);
}
};
// Define operators for this class
// vector operator << : shift left all elements
static inline Vec16uc operator << (Vec16uc const & a, uint32_t b) {
uint32_t mask = (uint32_t)0xFF >> (uint32_t)b; // mask to remove bits that are shifted out
__m128i am = _mm_and_si128(a,_mm_set1_epi8((char)mask)); // remove bits that will overflow
__m128i res = _mm_sll_epi16(am,_mm_cvtsi32_si128(b));// 16-bit shifts
return res;
}
// vector operator << : shift left all elements
static inline Vec16uc operator << (Vec16uc const & a, int32_t b) {
return a << (uint32_t)b;
}
// vector operator >> : shift right logical all elements
static inline Vec16uc operator >> (Vec16uc const & a, uint32_t b) {
uint32_t mask = (uint32_t)0xFF << (uint32_t)b; // mask to remove bits that are shifted out
__m128i am = _mm_and_si128(a,_mm_set1_epi8((char)mask)); // remove bits that will overflow
__m128i res = _mm_srl_epi16(am,_mm_cvtsi32_si128(b));// 16-bit shifts
return res;
}
// vector operator >> : shift right logical all elements
static inline Vec16uc operator >> (Vec16uc const & a, int32_t b) {
return a >> (uint32_t)b;
}
// vector operator >>= : shift right logical
static inline Vec16uc & operator >>= (Vec16uc & a, int b) {
a = a >> b;
return a;
}
// vector operator >= : returns true for elements for which a >= b (unsigned)
static inline Vec16cb operator >= (Vec16uc const & a, Vec16uc const & b) {
#ifdef __XOP__ // AMD XOP instruction set
return (Vec16cb)_mm_comge_epu8(a,b);
#else // SSE2 instruction set
return (Vec16cb)_mm_cmpeq_epi8(_mm_max_epu8(a,b),a); // a == max(a,b)
#endif
}
// vector operator <= : returns true for elements for which a <= b (unsigned)
static inline Vec16cb operator <= (Vec16uc const & a, Vec16uc const & b) {
return b >= a;
}
// vector operator > : returns true for elements for which a > b (unsigned)
static inline Vec16cb operator > (Vec16uc const & a, Vec16uc const & b) {
#ifdef __XOP__ // AMD XOP instruction set
return (Vec16cb)_mm_comgt_epu8(a,b);
#else // SSE2 instruction set
return Vec16cb(Vec16c(~(b >= a)));
#endif
}
// vector operator < : returns true for elements for which a < b (unsigned)
static inline Vec16cb operator < (Vec16uc const & a, Vec16uc const & b) {
return b > a;
}
// vector operator + : add
static inline Vec16uc operator + (Vec16uc const & a, Vec16uc const & b) {
return Vec16uc (Vec16c(a) + Vec16c(b));
}
// vector operator - : subtract
static inline Vec16uc operator - (Vec16uc const & a, Vec16uc const & b) {
return Vec16uc (Vec16c(a) - Vec16c(b));
}
// vector operator * : multiply
static inline Vec16uc operator * (Vec16uc const & a, Vec16uc const & b) {
return Vec16uc (Vec16c(a) * Vec16c(b));
}
// vector operator & : bitwise and
static inline Vec16uc operator & (Vec16uc const & a, Vec16uc const & b) {
return Vec16uc(Vec128b(a) & Vec128b(b));
}
static inline Vec16uc operator && (Vec16uc const & a, Vec16uc const & b) {
return a & b;
}
// vector operator | : bitwise or
static inline Vec16uc operator | (Vec16uc const & a, Vec16uc const & b) {
return Vec16uc(Vec128b(a) | Vec128b(b));
}
static inline Vec16uc operator || (Vec16uc const & a, Vec16uc const & b) {
return a | b;
}
// vector operator ^ : bitwise xor
static inline Vec16uc operator ^ (Vec16uc const & a, Vec16uc const & b) {
return Vec16uc(Vec128b(a) ^ Vec128b(b));
}
// vector operator ~ : bitwise not
static inline Vec16uc operator ~ (Vec16uc const & a) {
return Vec16uc( ~ Vec128b(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];
// Each byte in s must be either 0 (false) or -1 (true). No other values are allowed.
// (s is signed)
static inline Vec16uc select (Vec16cb const & s, Vec16uc const & a, Vec16uc const & b) {
return selectb(s,a,b);
}
// Conditional add: For all vector elements i: result[i] = f[i] ? (a[i] + b[i]) : a[i]
static inline Vec16uc if_add (Vec16cb const & f, Vec16uc const & a, Vec16uc const & b) {
return a + (Vec16uc(f) & b);
}
// function add_saturated: add element by element, unsigned with saturation
static inline Vec16uc add_saturated(Vec16uc const & a, Vec16uc const & b) {
return _mm_adds_epu8(a, b);
}
// function sub_saturated: subtract element by element, unsigned with saturation
static inline Vec16uc sub_saturated(Vec16uc const & a, Vec16uc const & b) {