forked from darealshinji/vectorclass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectorf128.h
2637 lines (2337 loc) · 92.4 KB
/
vectorf128.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
/**************************** vectorf128.h *******************************
* Author: Agner Fog
* Date created: 2012-05-30
* Last modified: 2016-04-26
* Version: 1.22
* Project: vector classes
* Description:
* Header file defining floating point 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:
* Vec4f Vector of 4 single precision floating point numbers
* Vec4fb Vector of 4 Booleans for use with Vec4f
* Vec2d Vector of 2 double precision floating point numbers
* Vec2db Vector of 2 Booleans for use with Vec2d
*
* 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:
* Vec2d a(1.0, 2.0), b(3.0, 4.0), c;
* c = a + b; // now c contains (4.0, 6.0)
*
* For detailed instructions, see VectorClass.pdf
*
* (c) Copyright 2012 - 2016 GNU General Public License http://www.gnu.org/licenses
*****************************************************************************/
#ifndef VECTORF128_H
#define VECTORF128_H
#if defined _MSC_VER && _MSC_VER >= 1800
// solve problem with ambiguous overloading of pow function in Microsoft math.h:
// make sure math.h is included first rather than last
#include <math.h>
#endif
#include "vectori128.h" // Define integer vectors
#ifdef VCL_NAMESPACE
namespace VCL_NAMESPACE {
#endif
/*****************************************************************************
*
* select functions
*
*****************************************************************************/
// Select between two __m128 sources, element by element. Used in various functions
// and operators. Corresponds to this pseudocode:
// for (int i = 0; i < 4; i++) result[i] = s[i] ? a[i] : b[i];
// Each element in s must be either 0 (false) or 0xFFFFFFFF (true). No other values are
// allowed. The implementation depends on the instruction set:
// If SSE4.1 is supported then only bit 31 in each dword of s is checked,
// otherwise all bits in s are used.
static inline __m128 selectf (__m128 const & s, __m128 const & a, __m128 const & b) {
#if INSTRSET >= 5 // SSE4.1 supported
return _mm_blendv_ps (b, a, s);
#else
return _mm_or_ps(
_mm_and_ps(s,a),
_mm_andnot_ps(s,b));
#endif
}
// Same, with two __m128d sources.
// and operators. Corresponds to this pseudocode:
// for (int i = 0; i < 2; i++) result[i] = s[i] ? a[i] : b[i];
// Each element in s must be either 0 (false) or 0xFFFFFFFFFFFFFFFF (true). No other
// values are allowed. The implementation depends on the instruction set:
// If SSE4.1 is supported then only bit 63 in each dword of s is checked,
// otherwise all bits in s are used.
static inline __m128d selectd (__m128d const & s, __m128d const & a, __m128d const & b) {
#if INSTRSET >= 5 // SSE4.1 supported
return _mm_blendv_pd (b, a, s);
#else
return _mm_or_pd(
_mm_and_pd(s,a),
_mm_andnot_pd(s,b));
#endif
}
/*****************************************************************************
*
* Vec4fb: Vector of 4 Booleans for use with Vec4f
*
*****************************************************************************/
class Vec4fb {
protected:
__m128 xmm; // Float vector
public:
// Default constructor:
Vec4fb() {
}
// Constructor to build from all elements:
Vec4fb(bool b0, bool b1, bool b2, bool b3) {
xmm = _mm_castsi128_ps(_mm_setr_epi32(-(int)b0, -(int)b1, -(int)b2, -(int)b3));
}
// Constructor to convert from type __m128 used in intrinsics:
Vec4fb(__m128 const & x) {
xmm = x;
}
// Assignment operator to convert from type __m128 used in intrinsics:
Vec4fb & operator = (__m128 const & x) {
xmm = x;
return *this;
}
// Constructor to broadcast scalar value:
Vec4fb(bool b) {
xmm = _mm_castsi128_ps(_mm_set1_epi32(-int32_t(b)));
}
// Assignment operator to broadcast scalar value:
Vec4fb & operator = (bool b) {
*this = Vec4fb(b);
return *this;
}
private: // Prevent constructing from int, etc.
Vec4fb(int b);
Vec4fb & operator = (int x);
public:
// Constructor to convert from type Vec4ib used as Boolean for integer vectors
Vec4fb(Vec4ib const & x) {
xmm = _mm_castsi128_ps(x);
}
// Assignment operator to convert from type Vec4ib used as Boolean for integer vectors
Vec4fb & operator = (Vec4ib const & x) {
xmm = _mm_castsi128_ps(x);
return *this;
}
// Type cast operator to convert to __m128 used in intrinsics
operator __m128() const {
return xmm;
}
#if defined (__clang__) && CLANG_VERSION < 30900 || defined(__apple_build_version__)
#define FIX_CLANG_VECTOR_ALIAS_AMBIGUITY // clang 3.3 - 3.5 has silent conversion between intrinsic vector types.
// I expected this to be fixed in version 3.4 but it still exists!
// http://llvm.org/bugs/show_bug.cgi?id=17164
// Problem: The version number is not consistent across platforms
// The Apple build has different version numbers. Too bad!
// http://llvm.org/bugs/show_bug.cgi?id=12643
#else
// Type cast operator to convert to type Vec4ib used as Boolean for integer vectors
operator Vec4ib() const {
return _mm_castps_si128(xmm);
}
#endif
// Member function to change a single element in vector
// Note: This function is inefficient. Use load function if changing more than one element
Vec4fb const & insert(uint32_t index, bool value) {
static const int32_t maskl[8] = {0,0,0,0,-1,0,0,0};
__m128 mask = _mm_loadu_ps((float const*)(maskl+4-(index & 3))); // mask with FFFFFFFF at index position
if (value) {
xmm = _mm_or_ps(xmm,mask);
}
else {
xmm = _mm_andnot_ps(mask,xmm);
}
return *this;
}
// Member function extract a single element from vector
bool extract(uint32_t index) const {
//return Vec4ib(*this).extract(index);
return Vec4ib(_mm_castps_si128(xmm)).extract(index);
}
// Extract a single element. Operator [] can only read an element, not write.
bool operator [] (uint32_t index) const {
return extract(index);
}
static int size() {
return 4;
}
};
/*****************************************************************************
*
* Operators for Vec4fb
*
*****************************************************************************/
// vector operator & : bitwise and
static inline Vec4fb operator & (Vec4fb const & a, Vec4fb const & b) {
return _mm_and_ps(a, b);
}
static inline Vec4fb operator && (Vec4fb const & a, Vec4fb const & b) {
return a & b;
}
// vector operator &= : bitwise and
static inline Vec4fb & operator &= (Vec4fb & a, Vec4fb const & b) {
a = a & b;
return a;
}
// vector operator | : bitwise or
static inline Vec4fb operator | (Vec4fb const & a, Vec4fb const & b) {
return _mm_or_ps(a, b);
}
static inline Vec4fb operator || (Vec4fb const & a, Vec4fb const & b) {
return a | b;
}
// vector operator |= : bitwise or
static inline Vec4fb & operator |= (Vec4fb & a, Vec4fb const & b) {
a = a | b;
return a;
}
// vector operator ^ : bitwise xor
static inline Vec4fb operator ^ (Vec4fb const & a, Vec4fb const & b) {
return _mm_xor_ps(a, b);
}
// vector operator ^= : bitwise xor
static inline Vec4fb & operator ^= (Vec4fb & a, Vec4fb const & b) {
a = a ^ b;
return a;
}
// vector operator ~ : bitwise not
static inline Vec4fb operator ~ (Vec4fb const & a) {
return _mm_xor_ps(a, _mm_castsi128_ps(_mm_set1_epi32(-1)));
}
// vector operator ! : logical not
// (operator ! is less efficient than operator ~. Use only where not
// all bits in an element are the same)
static inline Vec4fb operator ! (Vec4fb const & a) {
return Vec4fb( ! Vec4ib(a));
}
// Functions for Vec4fb
// andnot: a & ~ b
static inline Vec4fb andnot(Vec4fb const & a, Vec4fb const & b) {
return _mm_andnot_ps(b, a);
}
/*****************************************************************************
*
* Horizontal Boolean functions
*
*****************************************************************************/
// horizontal_and. Returns true if all bits are 1
static inline bool horizontal_and (Vec4fb const & a) {
return _mm_movemask_ps(a) == 0x0F;
//return horizontal_and(Vec128b(_mm_castps_si128(a)));
}
// horizontal_or. Returns true if at least one bit is 1
static inline bool horizontal_or (Vec4fb const & a) {
return _mm_movemask_ps(a) != 0;
//return horizontal_or(Vec128b(_mm_castps_si128(a)));
}
/*****************************************************************************
*
* Vec2db: Vector of 2 Booleans for use with Vec2d
*
*****************************************************************************/
class Vec2db {
protected:
__m128d xmm; // Double vector
public:
// Default constructor:
Vec2db() {
}
// Constructor to broadcast the same value into all elements:
// Constructor to build from all elements:
Vec2db(bool b0, bool b1) {
xmm = _mm_castsi128_pd(_mm_setr_epi32(-(int)b0, -(int)b0, -(int)b1, -(int)b1));
}
// Constructor to convert from type __m128d used in intrinsics:
Vec2db(__m128d const & x) {
xmm = x;
}
// Assignment operator to convert from type __m128d used in intrinsics:
Vec2db & operator = (__m128d const & x) {
xmm = x;
return *this;
}
// Constructor to broadcast scalar value:
Vec2db(bool b) {
xmm = _mm_castsi128_pd(_mm_set1_epi32(-int32_t(b)));
}
// Assignment operator to broadcast scalar value:
Vec2db & operator = (bool b) {
*this = Vec2db(b);
return *this;
}
private: // Prevent constructing from int, etc.
Vec2db(int b);
Vec2db & operator = (int x);
public:
// Constructor to convert from type Vec2qb used as Boolean for integer vectors
Vec2db(Vec2qb const & x) {
xmm = _mm_castsi128_pd(x);
}
// Assignment operator to convert from type Vec2qb used as Boolean for integer vectors
Vec2db & operator = (Vec2qb const & x) {
xmm = _mm_castsi128_pd(x);
return *this;
}
// Type cast operator to convert to __m128d used in intrinsics
operator __m128d() const {
return xmm;
}
#ifndef FIX_CLANG_VECTOR_ALIAS_AMBIGUITY
// Type cast operator to convert to type Vec2qb used as Boolean for integer vectors
operator Vec2qb() const {
return _mm_castpd_si128(xmm);
}
#endif
// Member function to change a single element in vector
// Note: This function is inefficient. Use load function if changing more than one element
Vec2db const & insert(uint32_t index, bool value) {
static const int32_t maskl[8] = {0,0,0,0,-1,-1,0,0};
__m128 mask = _mm_loadu_ps((float const*)(maskl+4-(index&1)*2)); // mask with FFFFFFFFFFFFFFFF at index position
if (value) {
xmm = _mm_or_pd(xmm,_mm_castps_pd(mask));
}
else {
xmm = _mm_andnot_pd(_mm_castps_pd(mask),xmm);
}
return *this;
}
// Member function extract a single element from vector
bool extract(uint32_t index) const {
return Vec2qb(*this).extract(index);
}
// Extract a single element. Operator [] can only read an element, not write.
bool operator [] (uint32_t index) const {
return extract(index);
}
static int size() {
return 2;
}
};
/*****************************************************************************
*
* Operators for Vec2db
*
*****************************************************************************/
// vector operator & : bitwise and
static inline Vec2db operator & (Vec2db const & a, Vec2db const & b) {
return _mm_and_pd(a, b);
}
static inline Vec2db operator && (Vec2db const & a, Vec2db const & b) {
return a & b;
}
// vector operator &= : bitwise and
static inline Vec2db & operator &= (Vec2db & a, Vec2db const & b) {
a = a & b;
return a;
}
// vector operator | : bitwise or
static inline Vec2db operator | (Vec2db const & a, Vec2db const & b) {
return _mm_or_pd(a, b);
}
static inline Vec2db operator || (Vec2db const & a, Vec2db const & b) {
return a | b;
}
// vector operator |= : bitwise or
static inline Vec2db & operator |= (Vec2db & a, Vec2db const & b) {
a = a | b;
return a;
}
// vector operator ^ : bitwise xor
static inline Vec2db operator ^ (Vec2db const & a, Vec2db const & b) {
return _mm_xor_pd(a, b);
}
// vector operator ^= : bitwise xor
static inline Vec2db & operator ^= (Vec2db & a, Vec2db const & b) {
a = a ^ b;
return a;
}
// vector operator ~ : bitwise not
static inline Vec2db operator ~ (Vec2db const & a) {
return _mm_xor_pd(a, _mm_castsi128_pd(_mm_set1_epi32(-1)));
}
// vector operator ! : logical not
// (operator ! is less efficient than operator ~. Use only where not
// all bits in an element are the same)
static inline Vec2db operator ! (Vec2db const & a) {
return Vec2db (! Vec2qb(a));
}
// Functions for Vec2db
// andnot: a & ~ b
static inline Vec2db andnot(Vec2db const & a, Vec2db const & b) {
return _mm_andnot_pd(b, a);
}
/*****************************************************************************
*
* Horizontal Boolean functions
*
*****************************************************************************/
// horizontal_and. Returns true if all bits are 1
static inline bool horizontal_and (Vec2db const & a) {
return _mm_movemask_pd(a) == 3;
//return horizontal_and(Vec128b(_mm_castpd_si128(a)));
}
// horizontal_or. Returns true if at least one bit is 1
static inline bool horizontal_or (Vec2db const & a) {
return _mm_movemask_pd(a) != 0;
//return horizontal_or(Vec128b(_mm_castpd_si128(a)));
}
/*****************************************************************************
*
* Vec4f: Vector of 4 single precision floating point values
*
*****************************************************************************/
class Vec4f {
protected:
__m128 xmm; // Float vector
public:
// Default constructor:
Vec4f() {
}
// Constructor to broadcast the same value into all elements:
Vec4f(float f) {
xmm = _mm_set1_ps(f);
}
// Constructor to build from all elements:
Vec4f(float f0, float f1, float f2, float f3) {
xmm = _mm_setr_ps(f0, f1, f2, f3);
}
// Constructor to convert from type __m128 used in intrinsics:
Vec4f(__m128 const & x) {
xmm = x;
}
// Assignment operator to convert from type __m128 used in intrinsics:
Vec4f & operator = (__m128 const & x) {
xmm = x;
return *this;
}
// Type cast operator to convert to __m128 used in intrinsics
operator __m128() const {
return xmm;
}
// Member function to load from array (unaligned)
Vec4f & load(float const * p) {
xmm = _mm_loadu_ps(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.
Vec4f & load_a(float const * p) {
xmm = _mm_load_ps(p);
return *this;
}
// Member function to store into array (unaligned)
void store(float * p) const {
_mm_storeu_ps(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(float * p) const {
_mm_store_ps(p, xmm);
}
// Partial load. Load n elements and set the rest to 0
Vec4f & load_partial(int n, float const * p) {
__m128 t1, t2;
switch (n) {
case 1:
xmm = _mm_load_ss(p); break;
case 2:
xmm = _mm_castpd_ps(_mm_load_sd((double const*)p)); break;
case 3:
t1 = _mm_castpd_ps(_mm_load_sd((double const*)p));
t2 = _mm_load_ss(p + 2);
xmm = _mm_movelh_ps(t1, t2); break;
case 4:
load(p); break;
default:
xmm = _mm_setzero_ps();
}
return *this;
}
// Partial store. Store n elements
void store_partial(int n, float * p) const {
__m128 t1;
switch (n) {
case 1:
_mm_store_ss(p, xmm); break;
case 2:
_mm_store_sd((double*)p, _mm_castps_pd(xmm)); break;
case 3:
_mm_store_sd((double*)p, _mm_castps_pd(xmm));
t1 = _mm_movehl_ps(xmm,xmm);
_mm_store_ss(p + 2, t1); break;
case 4:
store(p); break;
default:;
}
}
// cut off vector to n elements. The last 4-n elements are set to zero
Vec4f & cutoff(int n) {
if (uint32_t(n) >= 4) return *this;
static const union {
int32_t i[8];
float f[8];
} mask = {{1,-1,-1,-1,0,0,0,0}};
xmm = _mm_and_ps(xmm, Vec4f().load(mask.f + 4 - 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
Vec4f const & insert(uint32_t index, float value) {
#if INSTRSET >= 5 // SSE4.1 supported
switch (index & 3) {
case 0:
xmm = _mm_insert_ps(xmm, _mm_set_ss(value), 0 << 4); break;
case 1:
xmm = _mm_insert_ps(xmm, _mm_set_ss(value), 1 << 4); break;
case 2:
xmm = _mm_insert_ps(xmm, _mm_set_ss(value), 2 << 4); break;
default:
xmm = _mm_insert_ps(xmm, _mm_set_ss(value), 3 << 4); break;
}
#else
static const int32_t maskl[8] = {0,0,0,0,-1,0,0,0};
__m128 broad = _mm_set1_ps(value); // broadcast value into all elements
__m128 mask = _mm_loadu_ps((float const*)(maskl+4-(index & 3))); // mask with FFFFFFFF at index position
xmm = selectf(mask,broad,xmm);
#endif
return *this;
};
// Member function extract a single element from vector
float extract(uint32_t index) const {
float x[4];
store(x);
return x[index & 3];
}
// Extract a single element. Use store function if extracting more than one element.
// Operator [] can only read an element, not write.
float operator [] (uint32_t index) const {
return extract(index);
}
static int size() {
return 4;
}
};
/*****************************************************************************
*
* Operators for Vec4f
*
*****************************************************************************/
// vector operator + : add element by element
static inline Vec4f operator + (Vec4f const & a, Vec4f const & b) {
return _mm_add_ps(a, b);
}
// vector operator + : add vector and scalar
static inline Vec4f operator + (Vec4f const & a, float b) {
return a + Vec4f(b);
}
static inline Vec4f operator + (float a, Vec4f const & b) {
return Vec4f(a) + b;
}
// vector operator += : add
static inline Vec4f & operator += (Vec4f & a, Vec4f const & b) {
a = a + b;
return a;
}
// postfix operator ++
static inline Vec4f operator ++ (Vec4f & a, int) {
Vec4f a0 = a;
a = a + 1.0f;
return a0;
}
// prefix operator ++
static inline Vec4f & operator ++ (Vec4f & a) {
a = a + 1.0f;
return a;
}
// vector operator - : subtract element by element
static inline Vec4f operator - (Vec4f const & a, Vec4f const & b) {
return _mm_sub_ps(a, b);
}
// vector operator - : subtract vector and scalar
static inline Vec4f operator - (Vec4f const & a, float b) {
return a - Vec4f(b);
}
static inline Vec4f operator - (float a, Vec4f const & b) {
return Vec4f(a) - b;
}
// vector operator - : unary minus
// Change sign bit, even for 0, INF and NAN
static inline Vec4f operator - (Vec4f const & a) {
return _mm_xor_ps(a, _mm_castsi128_ps(_mm_set1_epi32(0x80000000)));
}
// vector operator -= : subtract
static inline Vec4f & operator -= (Vec4f & a, Vec4f const & b) {
a = a - b;
return a;
}
// postfix operator --
static inline Vec4f operator -- (Vec4f & a, int) {
Vec4f a0 = a;
a = a - 1.0f;
return a0;
}
// prefix operator --
static inline Vec4f & operator -- (Vec4f & a) {
a = a - 1.0f;
return a;
}
// vector operator * : multiply element by element
static inline Vec4f operator * (Vec4f const & a, Vec4f const & b) {
return _mm_mul_ps(a, b);
}
// vector operator * : multiply vector and scalar
static inline Vec4f operator * (Vec4f const & a, float b) {
return a * Vec4f(b);
}
static inline Vec4f operator * (float a, Vec4f const & b) {
return Vec4f(a) * b;
}
// vector operator *= : multiply
static inline Vec4f & operator *= (Vec4f & a, Vec4f const & b) {
a = a * b;
return a;
}
// vector operator / : divide all elements by same integer
static inline Vec4f operator / (Vec4f const & a, Vec4f const & b) {
return _mm_div_ps(a, b);
}
// vector operator / : divide vector and scalar
static inline Vec4f operator / (Vec4f const & a, float b) {
return a / Vec4f(b);
}
static inline Vec4f operator / (float a, Vec4f const & b) {
return Vec4f(a) / b;
}
// vector operator /= : divide
static inline Vec4f & operator /= (Vec4f & a, Vec4f const & b) {
a = a / b;
return a;
}
// vector operator == : returns true for elements for which a == b
static inline Vec4fb operator == (Vec4f const & a, Vec4f const & b) {
return _mm_cmpeq_ps(a, b);
}
// vector operator != : returns true for elements for which a != b
static inline Vec4fb operator != (Vec4f const & a, Vec4f const & b) {
return _mm_cmpneq_ps(a, b);
}
// vector operator < : returns true for elements for which a < b
static inline Vec4fb operator < (Vec4f const & a, Vec4f const & b) {
return _mm_cmplt_ps(a, b);
}
// vector operator <= : returns true for elements for which a <= b
static inline Vec4fb operator <= (Vec4f const & a, Vec4f const & b) {
return _mm_cmple_ps(a, b);
}
// vector operator > : returns true for elements for which a > b
static inline Vec4fb operator > (Vec4f const & a, Vec4f const & b) {
return b < a;
}
// vector operator >= : returns true for elements for which a >= b
static inline Vec4fb operator >= (Vec4f const & a, Vec4f const & b) {
return b <= a;
}
// Bitwise logical operators
// vector operator & : bitwise and
static inline Vec4f operator & (Vec4f const & a, Vec4f const & b) {
return _mm_and_ps(a, b);
}
// vector operator &= : bitwise and
static inline Vec4f & operator &= (Vec4f & a, Vec4f const & b) {
a = a & b;
return a;
}
// vector operator & : bitwise and of Vec4f and Vec4fb
static inline Vec4f operator & (Vec4f const & a, Vec4fb const & b) {
return _mm_and_ps(a, b);
}
static inline Vec4f operator & (Vec4fb const & a, Vec4f const & b) {
return _mm_and_ps(a, b);
}
// vector operator | : bitwise or
static inline Vec4f operator | (Vec4f const & a, Vec4f const & b) {
return _mm_or_ps(a, b);
}
// vector operator |= : bitwise or
static inline Vec4f & operator |= (Vec4f & a, Vec4f const & b) {
a = a | b;
return a;
}
// vector operator ^ : bitwise xor
static inline Vec4f operator ^ (Vec4f const & a, Vec4f const & b) {
return _mm_xor_ps(a, b);
}
// vector operator ^= : bitwise xor
static inline Vec4f & operator ^= (Vec4f & a, Vec4f const & b) {
a = a ^ b;
return a;
}
// vector operator ! : logical not. Returns Boolean vector
static inline Vec4fb operator ! (Vec4f const & a) {
return a == Vec4f(0.0f);
}
/*****************************************************************************
*
* Functions for Vec4f
*
*****************************************************************************/
// Select between two operands. Corresponds to this pseudocode:
// for (int i = 0; i < 4; i++) result[i] = s[i] ? a[i] : b[i];
// Each byte in s must be either 0 (false) or 0xFFFFFFFF (true). No other values are allowed.
static inline Vec4f select (Vec4fb const & s, Vec4f const & a, Vec4f const & b) {
return selectf(s,a,b);
}
// Conditional add: For all vector elements i: result[i] = f[i] ? (a[i] + b[i]) : a[i]
static inline Vec4f if_add (Vec4fb const & f, Vec4f const & a, Vec4f const & b) {
return a + (Vec4f(f) & b);
}
// Conditional multiply: For all vector elements i: result[i] = f[i] ? (a[i] * b[i]) : a[i]
static inline Vec4f if_mul (Vec4fb const & f, Vec4f const & a, Vec4f const & b) {
return a * select(f, b, 1.f);
}
// General arithmetic functions, etc.
// Horizontal add: Calculates the sum of all vector elements.
static inline float horizontal_add (Vec4f const & a) {
#if INSTRSET >= 3 // SSE3
__m128 t1 = _mm_hadd_ps(a,a);
__m128 t2 = _mm_hadd_ps(t1,t1);
return _mm_cvtss_f32(t2);
#else
__m128 t1 = _mm_movehl_ps(a,a);
__m128 t2 = _mm_add_ps(a,t1);
__m128 t3 = _mm_shuffle_ps(t2,t2,1);
__m128 t4 = _mm_add_ss(t2,t3);
return _mm_cvtss_f32(t4);
#endif
}
// function max: a > b ? a : b
static inline Vec4f max(Vec4f const & a, Vec4f const & b) {
return _mm_max_ps(a,b);
}
// function min: a < b ? a : b
static inline Vec4f min(Vec4f const & a, Vec4f const & b) {
return _mm_min_ps(a,b);
}
// function abs: absolute value
// Removes sign bit, even for -0.0f, -INF and -NAN
static inline Vec4f abs(Vec4f const & a) {
__m128 mask = _mm_castsi128_ps(_mm_set1_epi32(0x7FFFFFFF));
return _mm_and_ps(a,mask);
}
// function sqrt: square root
static inline Vec4f sqrt(Vec4f const & a) {
return _mm_sqrt_ps(a);
}
// function square: a * a
static inline Vec4f square(Vec4f const & a) {
return a * a;
}
// pow(vector,int) function template
template <typename VTYPE>
static inline VTYPE pow_template_i(VTYPE const & x0, int n) {
VTYPE x = x0; // a^(2^i)
VTYPE y(1.0f); // accumulator
if (n >= 0) { // make sure n is not negative
while (true) { // loop for each bit in n
if (n & 1) y *= x; // multiply if bit = 1
n >>= 1; // get next bit of n
if (n == 0) return y; // finished
x *= x; // x = a^2, a^4, a^8, etc.
}
}
else { // n < 0
return VTYPE(1.0f)/pow_template_i<VTYPE>(x0,-n); // reciprocal
}
}
// pow(Vec4f, int):
// The purpose of this template is to prevent implicit conversion of a float
// exponent to int when calling pow(vector, float) and vectormath_exp.h is
// not included
template <typename TT> static Vec4f pow(Vec4f const & a, TT n);
// Raise floating point numbers to integer power n
template <>
inline Vec4f pow<int>(Vec4f const & x0, int n) {
return pow_template_i<Vec4f>(x0, n);
}
// allow conversion from unsigned int
template <>
inline Vec4f pow<uint32_t>(Vec4f const & x0, uint32_t n) {
return pow_template_i<Vec4f>(x0, (int)n);
}
// Raise floating point numbers to integer power n, where n is a compile-time constant
template <int n>
static inline Vec4f pow_n(Vec4f const & a) {
if (n < 0) return Vec4f(1.0f) / pow_n<-n>(a);
if (n == 0) return Vec4f(1.0f);
if (n >= 256) return pow(a, n);
Vec4f x = a; // a^(2^i)
Vec4f y; // accumulator
const int lowest = n - (n & (n-1));// lowest set bit in n
if (n & 1) y = x;
if (n < 2) return y;
x = x*x; // x^2
if (n & 2) {
if (lowest == 2) y = x; else y *= x;
}
if (n < 4) return y;
x = x*x; // x^4
if (n & 4) {
if (lowest == 4) y = x; else y *= x;
}
if (n < 8) return y;
x = x*x; // x^8
if (n & 8) {
if (lowest == 8) y = x; else y *= x;
}
if (n < 16) return y;
x = x*x; // x^16
if (n & 16) {
if (lowest == 16) y = x; else y *= x;
}
if (n < 32) return y;
x = x*x; // x^32
if (n & 32) {
if (lowest == 32) y = x; else y *= x;
}
if (n < 64) return y;
x = x*x; // x^64
if (n & 64) {
if (lowest == 64) y = x; else y *= x;
}
if (n < 128) return y;
x = x*x; // x^128
if (n & 128) {
if (lowest == 128) y = x; else y *= x;
}
return y;
}
// implement as function pow(vector, const_int)
template <int n>
static inline Vec4f pow(Vec4f const & a, Const_int_t<n>) {
return pow_n<n>(a);
}
// implement the same as macro pow_const(vector, int)
#define pow_const(x,n) pow_n<n>(x)
// avoid unsafe optimization in function round
#if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__) && INSTRSET < 5
static inline Vec4f round(Vec4f const & a) __attribute__ ((optimize("-fno-unsafe-math-optimizations")));
#elif defined(__clang__) && INSTRSET < 5
// static inline Vec4f round(Vec4f const & a) __attribute__ ((optnone));
// This doesn't work, but current versions of Clang (3.5) don't optimize away signedmagic, even with -funsafe-math-optimizations
// Add volatile to b if future versions fail
#elif defined (_MSC_VER) || defined(__INTEL_COMPILER) && INSTRSET < 5
#pragma float_control(push)
#pragma float_control(precise,on)
#define FLOAT_CONTROL_PRECISE_FOR_ROUND
#endif
// function round: round to nearest integer (even). (result as float vector)
static inline Vec4f round(Vec4f const & a) {
#if INSTRSET >= 5 // SSE4.1 supported
return _mm_round_ps(a, 8);
#else // SSE2. Use magic number method
// Note: assume MXCSR control register is set to rounding
// (don't use conversion to int, it will limit the value to +/- 2^31)
Vec4f signmask = _mm_castsi128_ps(constant4i<(int)0x80000000,(int)0x80000000,(int)0x80000000,(int)0x80000000>()); // -0.0
Vec4f magic = _mm_castsi128_ps(constant4i<0x4B000000,0x4B000000,0x4B000000,0x4B000000>()); // magic number = 2^23
Vec4f sign = _mm_and_ps(a, signmask); // signbit of a
Vec4f signedmagic = _mm_or_ps(magic, sign); // magic number with sign of a
// volatile
Vec4f b = a + signedmagic; // round by adding magic number
return b - signedmagic; // .. and subtracting it again
#endif
}
#ifdef FLOAT_CONTROL_PRECISE_FOR_ROUND
#pragma float_control(pop)
#endif
// function truncate: round towards zero. (result as float vector)
static inline Vec4f truncate(Vec4f const & a) {
#if INSTRSET >= 5 // SSE4.1 supported
return _mm_round_ps(a, 3+8);
#else // SSE2. Use magic number method (conversion to int would limit the value to 2^31)
uint32_t t1 = _mm_getcsr(); // MXCSR
uint32_t t2 = t1 | (3 << 13); // bit 13-14 = 11
_mm_setcsr(t2); // change MXCSR
Vec4f r = round(a); // use magic number method
_mm_setcsr(t1); // restore MXCSR
return r;
#endif
}
// function floor: round towards minus infinity. (result as float vector)
static inline Vec4f floor(Vec4f const & a) {
#if INSTRSET >= 5 // SSE4.1 supported
return _mm_round_ps(a, 1+8);
#else // SSE2. Use magic number method (conversion to int would limit the value to 2^31)
uint32_t t1 = _mm_getcsr(); // MXCSR
uint32_t t2 = t1 | (1 << 13); // bit 13-14 = 01
_mm_setcsr(t2); // change MXCSR
Vec4f r = round(a); // use magic number method
_mm_setcsr(t1); // restore MXCSR
return r;
#endif
}
// function ceil: round towards plus infinity. (result as float vector)