forked from darealshinji/vectorclass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectorf512e.h
2134 lines (1862 loc) · 77.4 KB
/
vectorf512e.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
/**************************** vectorf512.h *******************************
* Author: Agner Fog
* Date created: 2014-07-23
* 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 AVX512 and later instruction sets.
*
* Instructions:
* Use Gnu, Intel or Microsoft C++ compiler. Compile for the desired
* instruction set, which must be at least AVX512F.
*
* The following vector classes are defined here:
* Vec16f Vector of 16 single precision floating point numbers
* Vec16fb Vector of 16 Booleans for use with Vec16f
* Vec8d Vector of 8 double precision floating point numbers
* Vec8db Vector of 8 Booleans for use with Vec8d
*
* Each vector object is represented internally in the CPU as a 512-bit register.
* This header file defines operators and functions for these vectors.
*
* For detailed instructions, see VectorClass.pdf
*
* (c) Copyright 2014-2016 GNU General Public License http://www.gnu.org/licenses
*****************************************************************************/
// check combination of header files
#if defined (VECTORF512_H)
#if VECTORF512_H != 1
#error Two different versions of vectorf512.h included
#endif
#else
#define VECTORF512_H 1
#include "vectori512e.h"
#ifdef VCL_NAMESPACE
namespace VCL_NAMESPACE {
#endif
/*****************************************************************************
*
* Vec16fb: Vector of 16 Booleans for use with Vec16f
*
*****************************************************************************/
class Vec16fb : public Vec16b {
public:
// Default constructor:
Vec16fb () {
}
// Constructor to build from all elements:
Vec16fb(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) :
Vec16b(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15) {
}
// Constructor from Vec16b
Vec16fb (Vec16b const & x) {
z0 = x.get_low();
z1 = x.get_high();
}
// Constructor from two Vec8fb
Vec16fb (Vec8fb const & x0, Vec8fb const & x1) {
z0 = x0;
z1 = x1;
}
// Constructor to broadcast scalar value:
Vec16fb(bool b) : Vec16b(b) {
}
// Assignment operator to broadcast scalar value:
Vec16fb & operator = (bool b) {
*this = Vec16b(b);
return *this;
}
private: // Prevent constructing from int, etc.
Vec16fb(int b);
Vec16fb & operator = (int x);
public:
// Get low and high half
Vec8fb get_low() const {
return reinterpret_f(Vec8i(z0));
}
Vec8fb get_high() const {
return reinterpret_f(Vec8i(z1));
}
};
// Define operators for Vec16fb
// vector operator & : bitwise and
static inline Vec16fb operator & (Vec16fb const & a, Vec16fb const & b) {
return Vec16fb(a.get_low() & b.get_low(), a.get_high() & b.get_high());
}
static inline Vec16fb operator && (Vec16fb const & a, Vec16fb const & b) {
return a & b;
}
// vector operator | : bitwise or
static inline Vec16fb operator | (Vec16fb const & a, Vec16fb const & b) {
return Vec16fb(a.get_low() | b.get_low(), a.get_high() | b.get_high());
}
static inline Vec16fb operator || (Vec16fb const & a, Vec16fb const & b) {
return a | b;
}
// vector operator ^ : bitwise xor
static inline Vec16fb operator ^ (Vec16fb const & a, Vec16fb const & b) {
return Vec16fb(a.get_low() ^ b.get_low(), a.get_high() ^ b.get_high());
}
// vector operator ~ : bitwise not
static inline Vec16fb operator ~ (Vec16fb const & a) {
return Vec16fb(~a.get_low(), ~a.get_high());
}
// vector operator ! : element not
static inline Vec16fb operator ! (Vec16fb const & a) {
return ~a;
}
// vector operator &= : bitwise and
static inline Vec16fb & operator &= (Vec16fb & a, Vec16fb const & b) {
a = a & b;
return a;
}
// vector operator |= : bitwise or
static inline Vec16fb & operator |= (Vec16fb & a, Vec16fb const & b) {
a = a | b;
return a;
}
// vector operator ^= : bitwise xor
static inline Vec16fb & operator ^= (Vec16fb & a, Vec16fb const & b) {
a = a ^ b;
return a;
}
/*****************************************************************************
*
* Vec8db: Vector of 8 Booleans for use with Vec8d
*
*****************************************************************************/
class Vec8db : public Vec512b {
public:
// Default constructor:
Vec8db () {
}
// Constructor to build from all elements:
Vec8db(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);
}
// Construct from Vec512b
Vec8db (Vec512b const & x) {
z0 = x.get_low();
z1 = x.get_high();
}
// Constructor from two Vec4db
Vec8db (Vec4db const & x0, Vec4db const & x1) {
z0 = x0;
z1 = x1;
}
// Constructor to broadcast single value:
Vec8db(bool b) {
z0 = z1 = Vec8i(-int32_t(b));
}
// Assignment operator to broadcast scalar value:
Vec8db & operator = (bool b) {
*this = Vec8db(b);
return *this;
}
private:
// Prevent constructing from int, etc. because of ambiguity
Vec8db(int b);
// Prevent assigning int because of ambiguity
Vec8db & operator = (int x);
public:
Vec8db & insert (int index, bool a) {
if (index < 4) {
z0 = Vec4q(z0).insert(index, -(int64_t)a);
}
else {
z1 = Vec4q(z1).insert(index-4, -(int64_t)a);
}
return *this;
}
// Member function extract a single element from vector
bool extract(uint32_t index) const {
if (index < 4) {
return Vec4q(z0).extract(index) != 0;
}
else {
return Vec4q(z1).extract(index-4) != 0;
}
}
// Extract a single element. Operator [] can only read an element, not write.
bool operator [] (uint32_t index) const {
return extract(index);
}
// Get low and high half
Vec4db get_low() const {
return reinterpret_d(Vec4q(z0));
}
Vec4db get_high() const {
return reinterpret_d(Vec4q(z1));
}
static int size () {
return 8;
}
};
// Define operators for Vec8db
// vector operator & : bitwise and
static inline Vec8db operator & (Vec8db const & a, Vec8db const & b) {
return Vec8db(a.get_low() & b.get_low(), a.get_high() & b.get_high());
}
static inline Vec8db operator && (Vec8db const & a, Vec8db const & b) {
return a & b;
}
// vector operator | : bitwise or
static inline Vec8db operator | (Vec8db const & a, Vec8db const & b) {
return Vec8db(a.get_low() | b.get_low(), a.get_high() | b.get_high());
}
static inline Vec8db operator || (Vec8db const & a, Vec8db const & b) {
return a | b;
}
// vector operator ^ : bitwise xor
static inline Vec8db operator ^ (Vec8db const & a, Vec8db const & b) {
return Vec8db(a.get_low() ^ b.get_low(), a.get_high() ^ b.get_high());
}
// vector operator ~ : bitwise not
static inline Vec8db operator ~ (Vec8db const & a) {
return Vec8db(~a.get_low(), ~a.get_high());
}
// vector operator ! : element not
static inline Vec8db operator ! (Vec8db const & a) {
return ~a;
}
// vector operator &= : bitwise and
static inline Vec8db & operator &= (Vec8db & a, Vec8db const & b) {
a = a & b;
return a;
}
// vector operator |= : bitwise or
static inline Vec8db & operator |= (Vec8db & a, Vec8db const & b) {
a = a | b;
return a;
}
// vector operator ^= : bitwise xor
static inline Vec8db & operator ^= (Vec8db & a, Vec8db const & b) {
a = a ^ b;
return a;
}
/*****************************************************************************
*
* Vec16f: Vector of 16 single precision floating point values
*
*****************************************************************************/
class Vec16f {
protected:
Vec8f z0;
Vec8f z1;
public:
// Default constructor:
Vec16f() {
}
// Constructor to broadcast the same value into all elements:
Vec16f(float f) {
z0 = z1 = Vec8f(f);
}
// Constructor to build from all elements:
Vec16f(float f0, float f1, float f2, float f3, float f4, float f5, float f6, float f7,
float f8, float f9, float f10, float f11, float f12, float f13, float f14, float f15) {
z0 = Vec8f(f0, f1, f2, f3, f4, f5, f6, f7);
z1 = Vec8f(f8, f9, f10, f11, f12, f13, f14, f15);
}
// Constructor to build from two Vec8f:
Vec16f(Vec8f const & a0, Vec8f const & a1) {
z0 = a0;
z1 = a1;
}
// split into two halves
Vec8f get_low() const {
return z0;
}
Vec8f get_high() const {
return z1;
}
// Member function to load from array (unaligned)
Vec16f & load(float const * p) {
z0 = Vec8f().load(p);
z1 = Vec8f().load(p+8);
return *this;
}
// Member function to load from array, aligned by 64
// You may use load_a instead of load if you are certain that p points to an address
// divisible by 64.
Vec16f & load_a(float const * p) {
z0 = Vec8f().load_a(p);
z1 = Vec8f().load_a(p+8);
return *this;
}
// Member function to store into array (unaligned)
void store(float * p) const {
Vec8f(z0).store(p);
Vec8f(z1).store(p+8);
}
// Member function to store into array, aligned by 64
// You may use store_a instead of store if you are certain that p points to an address
// divisible by 64.
void store_a(float * p) const {
Vec8f(z0).store_a(p);
Vec8f(z1).store_a(p+8);
}
// Partial load. Load n elements and set the rest to 0
Vec16f & load_partial(int n, float const * p) {
if (n < 8) {
z0 = Vec8f().load_partial(n, p);
z1 = Vec8f(0.f);
}
else {
z0 = Vec8f().load(p);
z1 = Vec8f().load_partial(n-8, p + 8);
}
return *this;
}
// Partial store. Store n elements
void store_partial(int n, float * p) const {
if (n < 8) {
Vec8f(z0).store_partial(n, p);
}
else {
Vec8f(z0).store(p);
Vec8f(z1).store_partial(n-8, p+8);
}
}
// cut off vector to n elements. The last 8-n elements are set to zero
Vec16f & cutoff(int n) {
if (n < 8) {
z0 = Vec8f(z0).cutoff(n);
z1 = Vec8f(0.f);
}
else {
z1 = Vec8f(z1).cutoff(n-8);
}
return *this;
}
// Member function to change a single element in vector
Vec16f const & insert(uint32_t index, float value) {
if (index < 8) {
z0 = Vec8f(z0).insert(index, value);
}
else {
z1 = Vec8f(z1).insert(index-8, value);
}
return *this;
}
// Member function extract a single element from vector
float extract(uint32_t index) const {
float a[16];
store(a);
return a[index & 15];
}
// 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 16;
}
};
/*****************************************************************************
*
* Operators for Vec16f
*
*****************************************************************************/
// vector operator + : add element by element
static inline Vec16f operator + (Vec16f const & a, Vec16f const & b) {
return Vec16f(a.get_low() + b.get_low(), a.get_high() + b.get_high());
}
// vector operator + : add vector and scalar
static inline Vec16f operator + (Vec16f const & a, float b) {
return a + Vec16f(b);
}
static inline Vec16f operator + (float a, Vec16f const & b) {
return Vec16f(a) + b;
}
// vector operator += : add
static inline Vec16f & operator += (Vec16f & a, Vec16f const & b) {
a = a + b;
return a;
}
// postfix operator ++
static inline Vec16f operator ++ (Vec16f & a, int) {
Vec16f a0 = a;
a = a + 1.0f;
return a0;
}
// prefix operator ++
static inline Vec16f & operator ++ (Vec16f & a) {
a = a + 1.0f;
return a;
}
// vector operator - : subtract element by element
static inline Vec16f operator - (Vec16f const & a, Vec16f const & b) {
return Vec16f(a.get_low() - b.get_low(), a.get_high() - b.get_high());
}
// vector operator - : subtract vector and scalar
static inline Vec16f operator - (Vec16f const & a, float b) {
return a - Vec16f(b);
}
static inline Vec16f operator - (float a, Vec16f const & b) {
return Vec16f(a) - b;
}
// vector operator - : unary minus
// Change sign bit, even for 0, INF and NAN
static inline Vec16f operator - (Vec16f const & a) {
return Vec16f(-a.get_low(), -a.get_high());
}
// vector operator -= : subtract
static inline Vec16f & operator -= (Vec16f & a, Vec16f const & b) {
a = a - b;
return a;
}
// postfix operator --
static inline Vec16f operator -- (Vec16f & a, int) {
Vec16f a0 = a;
a = a - 1.0f;
return a0;
}
// prefix operator --
static inline Vec16f & operator -- (Vec16f & a) {
a = a - 1.0f;
return a;
}
// vector operator * : multiply element by element
static inline Vec16f operator * (Vec16f const & a, Vec16f const & b) {
return Vec16f(a.get_low() * b.get_low(), a.get_high() * b.get_high());
}
// vector operator * : multiply vector and scalar
static inline Vec16f operator * (Vec16f const & a, float b) {
return a * Vec16f(b);
}
static inline Vec16f operator * (float a, Vec16f const & b) {
return Vec16f(a) * b;
}
// vector operator *= : multiply
static inline Vec16f & operator *= (Vec16f & a, Vec16f const & b) {
a = a * b;
return a;
}
// vector operator / : divide all elements by same integer
static inline Vec16f operator / (Vec16f const & a, Vec16f const & b) {
return Vec16f(a.get_low() / b.get_low(), a.get_high() / b.get_high());
}
// vector operator / : divide vector and scalar
static inline Vec16f operator / (Vec16f const & a, float b) {
return a / Vec16f(b);
}
static inline Vec16f operator / (float a, Vec16f const & b) {
return Vec16f(a) / b;
}
// vector operator /= : divide
static inline Vec16f & operator /= (Vec16f & a, Vec16f const & b) {
a = a / b;
return a;
}
// vector operator == : returns true for elements for which a == b
static inline Vec16fb operator == (Vec16f const & a, Vec16f const & b) {
return Vec16fb(a.get_low() == b.get_low(), a.get_high() == b.get_high());
}
// vector operator != : returns true for elements for which a != b
static inline Vec16fb operator != (Vec16f const & a, Vec16f const & b) {
return Vec16fb(a.get_low() != b.get_low(), a.get_high() != b.get_high());
}
// vector operator < : returns true for elements for which a < b
static inline Vec16fb operator < (Vec16f const & a, Vec16f const & b) {
return Vec16fb(a.get_low() < b.get_low(), a.get_high() < b.get_high());
}
// vector operator <= : returns true for elements for which a <= b
static inline Vec16fb operator <= (Vec16f const & a, Vec16f const & b) {
return Vec16fb(a.get_low() <= b.get_low(), a.get_high() <= b.get_high());
}
// vector operator > : returns true for elements for which a > b
static inline Vec16fb operator > (Vec16f const & a, Vec16f const & b) {
return b < a;
}
// vector operator >= : returns true for elements for which a >= b
static inline Vec16fb operator >= (Vec16f const & a, Vec16f const & b) {
return b <= a;
}
// Bitwise logical operators
// vector operator & : bitwise and
static inline Vec16f operator & (Vec16f const & a, Vec16f const & b) {
return Vec16f(a.get_low() & b.get_low(), a.get_high() & b.get_high());
}
// vector operator &= : bitwise and
static inline Vec16f & operator &= (Vec16f & a, Vec16f const & b) {
a = a & b;
return a;
}
// vector operator & : bitwise and of Vec16f and Vec16fb
static inline Vec16f operator & (Vec16f const & a, Vec16fb const & b) {
return Vec16f(a.get_low() & b.get_low(), a.get_high() & b.get_high());
}
static inline Vec16f operator & (Vec16fb const & a, Vec16f const & b) {
return b & a;
}
// vector operator | : bitwise or
static inline Vec16f operator | (Vec16f const & a, Vec16f const & b) {
return Vec16f(a.get_low() | b.get_low(), a.get_high() | b.get_high());
}
// vector operator |= : bitwise or
static inline Vec16f & operator |= (Vec16f & a, Vec16f const & b) {
a = a | b;
return a;
}
// vector operator ^ : bitwise xor
static inline Vec16f operator ^ (Vec16f const & a, Vec16f const & b) {
return Vec16f(a.get_low() ^ b.get_low(), a.get_high() ^ b.get_high());
}
// vector operator ^= : bitwise xor
static inline Vec16f & operator ^= (Vec16f & a, Vec16f const & b) {
a = a ^ b;
return a;
}
// vector operator ! : logical not. Returns Boolean vector
static inline Vec16fb operator ! (Vec16f const & a) {
return Vec16fb(!a.get_low(), !a.get_high());
}
/*****************************************************************************
*
* Functions for Vec16f
*
*****************************************************************************/
// Select between two operands. Corresponds to this pseudocode:
// for (int i = 0; i < 8; 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 Vec16f select (Vec16fb const & s, Vec16f const & a, Vec16f const & b) {
return Vec16f(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 Vec16f if_add (Vec16fb const & f, Vec16f const & a, Vec16f const & b) {
return Vec16f(if_add(f.get_low(), a.get_low(), b.get_low()), if_add(f.get_high(), a.get_high(), b.get_high()));
}
// Conditional multiply: For all vector elements i: result[i] = f[i] ? (a[i] * b[i]) : a[i]
static inline Vec16f if_mul (Vec16fb const & f, Vec16f const & a, Vec16f const & b) {
return Vec16f(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.
static inline float horizontal_add (Vec16f const & a) {
return horizontal_add(a.get_low() + a.get_high());
}
// function max: a > b ? a : b
static inline Vec16f max(Vec16f const & a, Vec16f const & b) {
return Vec16f(max(a.get_low(), b.get_low()), max(a.get_high(), b.get_high()));
}
// function min: a < b ? a : b
static inline Vec16f min(Vec16f const & a, Vec16f const & b) {
return Vec16f(min(a.get_low(), b.get_low()), min(a.get_high(), b.get_high()));
}
// function abs: absolute value
// Removes sign bit, even for -0.0f, -INF and -NAN
static inline Vec16f abs(Vec16f const & a) {
return Vec16f(abs(a.get_low()), abs(a.get_high()));
}
// function sqrt: square root
static inline Vec16f sqrt(Vec16f const & a) {
return Vec16f(sqrt(a.get_low()), sqrt(a.get_high()));
}
// function square: a * a
static inline Vec16f square(Vec16f const & a) {
return a * a;
}
// pow(Vec16f, int):
template <typename TT> static Vec16f pow(Vec16f const & a, TT n);
// Raise floating point numbers to integer power n
template <>
inline Vec16f pow<int>(Vec16f const & x0, int n) {
return pow_template_i<Vec16f>(x0, n);
}
// allow conversion from unsigned int
template <>
inline Vec16f pow<uint32_t>(Vec16f const & x0, uint32_t n) {
return pow_template_i<Vec16f>(x0, (int)n);
}
// Raise floating point numbers to integer power n, where n is a compile-time constant
template <int n>
static inline Vec16f pow_n(Vec16f const & a) {
if (n < 0) return Vec16f(1.0f) / pow_n<-n>(a);
if (n == 0) return Vec16f(1.0f);
if (n >= 256) return pow(a, n);
Vec16f x = a; // a^(2^i)
Vec16f 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;
}
template <int n>
static inline Vec16f pow(Vec16f const & a, Const_int_t<n>) {
return pow_n<n>(a);
}
// function round: round to nearest integer (even). (result as float vector)
static inline Vec16f round(Vec16f const & a) {
return Vec16f(round(a.get_low()), round(a.get_high()));
}
// function truncate: round towards zero. (result as float vector)
static inline Vec16f truncate(Vec16f const & a) {
return Vec16f(truncate(a.get_low()), truncate(a.get_high()));
}
// function floor: round towards minus infinity. (result as float vector)
static inline Vec16f floor(Vec16f const & a) {
return Vec16f(floor(a.get_low()), floor(a.get_high()));
}
// function ceil: round towards plus infinity. (result as float vector)
static inline Vec16f ceil(Vec16f const & a) {
return Vec16f(ceil(a.get_low()), ceil(a.get_high()));
}
// function round_to_int: round to nearest integer (even). (result as integer vector)
static inline Vec16i round_to_int(Vec16f const & a) {
return Vec16i(round_to_int(a.get_low()), round_to_int(a.get_high()));
}
// function truncate_to_int: round towards zero. (result as integer vector)
static inline Vec16i truncate_to_int(Vec16f const & a) {
return Vec16i(truncate_to_int(a.get_low()), truncate_to_int(a.get_high()));
}
// function to_float: convert integer vector to float vector
static inline Vec16f to_float(Vec16i const & a) {
return Vec16f(to_float(a.get_low()), to_float(a.get_high()));
}
// Approximate math functions
// approximate reciprocal (Faster than 1.f / a.
// relative accuracy better than 2^-11 without AVX512, 2^-14 with AVX512)
static inline Vec16f approx_recipr(Vec16f const & a) {
return Vec16f(approx_recipr(a.get_low()), approx_recipr(a.get_high()));
}
// approximate reciprocal squareroot (Faster than 1.f / sqrt(a).
// Relative accuracy better than 2^-11 without AVX512, 2^-14 with AVX512)
static inline Vec16f approx_rsqrt(Vec16f const & a) {
return Vec16f(approx_rsqrt(a.get_low()), approx_rsqrt(a.get_high()));
}
// Fused multiply and add functions
// Multiply and add
static inline Vec16f mul_add(Vec16f const & a, Vec16f const & b, Vec16f const & c) {
return Vec16f(mul_add(a.get_low(), b.get_low(), c.get_low()), mul_add(a.get_high(), b.get_high(), c.get_high()));
}
// Multiply and subtract
static inline Vec16f mul_sub(Vec16f const & a, Vec16f const & b, Vec16f const & c) {
return Vec16f(mul_sub(a.get_low(), b.get_low(), c.get_low()), mul_sub(a.get_high(), b.get_high(), c.get_high()));
}
// Multiply and inverse subtract
static inline Vec16f nmul_add(Vec16f const & a, Vec16f const & b, Vec16f const & c) {
return Vec16f(nmul_add(a.get_low(), b.get_low(), c.get_low()), nmul_add(a.get_high(), b.get_high(), c.get_high()));
}
// Multiply and subtract with extra precision on the intermediate calculations,
// even if FMA instructions not supported, using Veltkamp-Dekker split
static inline Vec16f mul_sub_x(Vec16f const & a, Vec16f const & b, Vec16f const & c) {
return Vec16f(mul_sub_x(a.get_low(), b.get_low(), c.get_low()), mul_sub_x(a.get_high(), b.get_high(), c.get_high()));
}
// Math functions using fast bit manipulation
// Extract the exponent as an integer
// exponent(a) = floor(log2(abs(a)));
// exponent(1.0f) = 0, exponent(0.0f) = -127, exponent(INF) = +128, exponent(NAN) = +128
static inline Vec16i exponent(Vec16f const & a) {
return Vec16i(exponent(a.get_low()), exponent(a.get_high()));
}
// Extract the fraction part of a floating point number
// a = 2^exponent(a) * fraction(a), except for a = 0
// fraction(1.0f) = 1.0f, fraction(5.0f) = 1.25f
static inline Vec16f fraction(Vec16f const & a) {
return Vec16f(fraction(a.get_low()), fraction(a.get_high()));
}
// Fast calculation of pow(2,n) with n integer
// n = 0 gives 1.0f
// n >= 128 gives +INF
// n <= -127 gives 0.0f
// This function will never produce denormals, and never raise exceptions
static inline Vec16f exp2(Vec16i const & n) {
return Vec16f(exp2(n.get_low()), exp2(n.get_high()));
}
//static Vec16f exp2(Vec16f const & x); // defined in vectormath_exp.h
// Categorization functions
// Function sign_bit: gives true for elements that have the sign bit set
// even for -0.0f, -INF and -NAN
// Note that sign_bit(Vec16f(-0.0f)) gives true, while Vec16f(-0.0f) < Vec16f(0.0f) gives false
// (the underscore in the name avoids a conflict with a macro in Intel's mathimf.h)
static inline Vec16fb sign_bit(Vec16f const & a) {
return Vec16fb(sign_bit(a.get_low()), sign_bit(a.get_high()));
}
// Function sign_combine: changes the sign of a when b has the sign bit set
// same as select(sign_bit(b), -a, a)
static inline Vec16f sign_combine(Vec16f const & a, Vec16f const & b) {
return Vec16f(sign_combine(a.get_low(), b.get_low()), sign_combine(a.get_high(), b.get_high()));
}
// Function is_finite: gives true for elements that are normal, denormal or zero,
// false for INF and NAN
// (the underscore in the name avoids a conflict with a macro in Intel's mathimf.h)
static inline Vec16fb is_finite(Vec16f const & a) {
return Vec16fb(is_finite(a.get_low()), is_finite(a.get_high()));
}
// Function is_inf: gives true for elements that are +INF or -INF
// false for finite numbers and NAN
// (the underscore in the name avoids a conflict with a macro in Intel's mathimf.h)
static inline Vec16fb is_inf(Vec16f const & a) {
return Vec16fb(is_inf(a.get_low()), is_inf(a.get_high()));
}
// Function is_nan: gives true for elements that are +NAN or -NAN
// false for finite numbers and +/-INF
// (the underscore in the name avoids a conflict with a macro in Intel's mathimf.h)
static inline Vec16fb is_nan(Vec16f const & a) {
return Vec16fb(is_nan(a.get_low()), is_nan(a.get_high()));
}
// Function is_subnormal: gives true for elements that are denormal (subnormal)
// false for finite numbers, zero, NAN and INF
static inline Vec16fb is_subnormal(Vec16f const & a) {
return Vec16fb(is_subnormal(a.get_low()), is_subnormal(a.get_high()));
}
// Function is_zero_or_subnormal: gives true for elements that are zero or subnormal (denormal)
// false for finite numbers, NAN and INF
static inline Vec16fb is_zero_or_subnormal(Vec16f const & a) {
return Vec16fb(is_zero_or_subnormal(a.get_low()), is_zero_or_subnormal(a.get_high()));
}
// Function infinite4f: returns a vector where all elements are +INF
static inline Vec16f infinite16f() {
Vec8f inf = infinite8f();
return Vec16f(inf, inf);
}
// Function nan4f: returns a vector where all elements are +NAN (quiet)
static inline Vec16f nan16f(int n = 0x10) {
Vec8f nan = nan8f(n);
return Vec16f(nan, nan);
}
// change signs on vectors Vec16f
// Each index i0 - i7 is 1 for changing sign on the corresponding element, 0 for no change
template <int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9, int i10, int i11, int i12, int i13, int i14, int i15>
static inline Vec16f change_sign(Vec16f const & a) {
return Vec16f(change_sign<i0,i1,i2,i3,i4,i5,i6,i7>(a.get_low()), change_sign<i8,i9,i10,i11,i12,i13,i14,i15>(a.get_high()));
}
/*****************************************************************************
*
* Vec8d: Vector of 8 double precision floating point values
*
*****************************************************************************/
class Vec8d {
protected:
Vec4d z0;
Vec4d z1;
public:
// Default constructor:
Vec8d() {
}
// Constructor to broadcast the same value into all elements:
Vec8d(double d) {
z0 = z1 = Vec4d(d);
}
// Constructor to build from all elements:
Vec8d(double d0, double d1, double d2, double d3, double d4, double d5, double d6, double d7) {
z0 = Vec4d(d0, d1, d2, d3);
z1 = Vec4d(d4, d5, d6, d7);
}
// Constructor to build from two Vec4d:
Vec8d(Vec4d const & a0, Vec4d const & a1) {
z0 = a0;
z1 = a1;
}
// Member function to load from array (unaligned)
Vec8d & load(double const * p) {
z0.load(p);
z1.load(p+4);
return *this;
}
// Member function to load from array, aligned by 64
// You may use load_a instead of load if you are certain that p points to an address
// divisible by 64
Vec8d & load_a(double const * p) {
z0.load_a(p);
z1.load_a(p+4);
return *this;
}
// Member function to store into array (unaligned)
void store(double * p) const {
z0.store(p);
z1.store(p+4);
}
// Member function to store into array, aligned by 64
// You may use store_a instead of store if you are certain that p points to an address
// divisible by 64
void store_a(double * p) const {
z0.store_a(p);
z1.store_a(p+4);
}
// Partial load. Load n elements and set the rest to 0
Vec8d & load_partial(int n, double const * p) {
if (n < 4) {
z0.load_partial(n, p);
z1 = Vec4d(0.);
}
else {
z0.load(p);
z1.load_partial(n-4, p+4);
}
return *this;
}
// Partial store. Store n elements
void store_partial(int n, double * p) const {
if (n < 4) {
z0.store_partial(n, p);
}
else {
z0.store(p);
z1.store_partial(n-4, p+4);
}
}
// cut off vector to n elements. The last 8-n elements are set to zero
Vec8d & cutoff(int n) {
if (n < 4) {
z0.cutoff(n);
z1 = Vec4d(0.);
}
else {
z1.cutoff(n-4);
}
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
Vec8d const & insert(uint32_t index, double value) {
if (index < 4) {
z0.insert(index, value);
}
else {
z1.insert(index-4, value);
}
return *this;
}
// Member function extract a single element from vector
double extract(uint32_t index) const {
double a[8];
store(a);
return a[index & 7];
}
// Extract a single element. Use store function if extracting more than one element.
// Operator [] can only read an element, not write.
double operator [] (uint32_t index) const {
return extract(index);
}
// Member functions to split into two Vec4d:
Vec4d get_low() const {
return z0;
}
Vec4d get_high() const {
return z1;
}
static int size () {
return 8;
}
};