forked from richgel999/CppSPMD_Fast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cppspmd_avx2.h
2531 lines (2063 loc) · 112 KB
/
cppspmd_avx2.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
// cppspmd_avx2.h
// The module is intended for AVX2, but it also supports AVX1. Also supports optional FMA support.
// Originally written by Nicolas Guillemot, Jefferson Amstutz in the "CppSPMD" project.
// 4/20: Richard Geldreich: Macro control flow, more SIMD instruction sets, optimizations, supports using multiple SIMD instruction sets in same executable. Still a work in progress!
// The original CppSPMD header, which this version distantly derives from, used the MIT license:
// Copyright 2016 Nicolas Guillemot
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
// the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
// AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <math.h>
#include <utility>
#include <immintrin.h>
#include <algorithm>
// By default this header is for AVX2, but I've left the older AVX1 code in place for benchmarking purposes.
#ifndef CPPSPMD_USE_AVX2
#define CPPSPMD_USE_AVX2 1
#endif
#ifndef CPPSPMD_USE_FMA
#define CPPSPMD_USE_FMA 0
#endif
#ifndef CPPSPMD_USE_AVX_512
#define CPPSPMD_USE_AVX_512 0
#endif
#ifdef _MSC_VER
#ifndef CPPSPMD_DECL
#define CPPSPMD_DECL(type, name) __declspec(align(32)) type name
#endif
#ifndef CPPSPMD_ALIGN
#define CPPSPMD_ALIGN(v) __declspec(align(v))
#endif
#else
#ifndef CPPSPMD_DECL
#define CPPSPMD_DECL(type, name) type name __attribute__((aligned(32)))
#endif
#ifndef CPPSPMD_ALIGN
#define CPPSPMD_ALIGN(v) __attribute__((aligned(v)))
#endif
#endif
#ifndef CPPSPMD_FORCE_INLINE
#ifdef _DEBUG
#define CPPSPMD_FORCE_INLINE inline
#else
#define CPPSPMD_FORCE_INLINE __forceinline
#endif
#endif
#undef CPPSPMD
#undef CPPSPMD_ARCH
#undef CPPSPMD_SSE
#undef CPPSPMD_AVX1
#undef CPPSPMD_AVX2
#undef CPPSPMD_AVX
#undef CPPSPMD_FLOAT4
#undef CPPSPMD_INT16
#define CPPSPMD_SSE 0
#define CPPSPMD_AVX 1
#define CPPSPMD_FLOAT4 0
#define CPPSPMD_INT16 0
#if CPPSPMD_USE_FMA
#if CPPSPMD_USE_AVX2
#define CPPSPMD cppspmd_avx2_fma
#define CPPSPMD_ARCH _avx2_fma
#define CPPSPMD_AVX1 0
#define CPPSPMD_AVX2 1
#else
#define CPPSPMD cppspmd_avx1_fma
#define CPPSPMD_ARCH _avx1_fma
#define CPPSPMD_AVX1 1
#define CPPSPMD_AVX2 0
#endif
#else
#if CPPSPMD_USE_AVX2
#define CPPSPMD cppspmd_avx2
#define CPPSPMD_ARCH _avx2
#define CPPSPMD_AVX1 0
#define CPPSPMD_AVX2 1
#else
#define CPPSPMD cppspmd_avx1
#define CPPSPMD_ARCH _avx1
#define CPPSPMD_AVX1 1
#define CPPSPMD_AVX2 0
#endif
#endif
#ifndef CPPSPMD_GLUER
#define CPPSPMD_GLUER(a, b) a##b
#endif
#ifndef CPPSPMD_GLUER2
#define CPPSPMD_GLUER2(a, b) CPPSPMD_GLUER(a, b)
#endif
#ifndef CPPSPMD_NAME
#define CPPSPMD_NAME(a) CPPSPMD_GLUER2(a, CPPSPMD_ARCH)
#endif
#undef VASSERT
#define VCOND(cond) ((exec_mask(vbool(cond)) & m_exec).get_movemask() == m_exec.get_movemask())
#define VASSERT(cond) assert( VCOND(cond) )
#undef CPPSPMD_ALIGNMENT
#define CPPSPMD_ALIGNMENT (32)
namespace CPPSPMD
{
const int PROGRAM_COUNT_SHIFT = 3;
const int PROGRAM_COUNT = 1 << PROGRAM_COUNT_SHIFT;
template <typename N> inline N* aligned_new() { void* p = _mm_malloc(sizeof(N), 64); new (p) N; return static_cast<N*>(p); }
template <typename N> void aligned_delete(N* p) { if (p) { p->~N(); _mm_free(p); } }
CPPSPMD_DECL(const uint32_t, g_allones_256[8]) = { UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX };
CPPSPMD_DECL(const float, g_onef_256[8]) = { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
CPPSPMD_DECL(const uint32_t, g_oneu_256[8]) = { 1, 1, 1, 1, 1, 1, 1, 1 };
CPPSPMD_DECL(const uint32_t, g_x_128[4]) = { UINT32_MAX, 0, 0, 0 };
CPPSPMD_DECL(const uint32_t, g_lane_masks_256[8][8]) =
{
{ UINT32_MAX, 0, 0, 0, 0, 0, 0, 0 },
{ 0, UINT32_MAX, 0, 0, 0, 0, 0, 0 },
{ 0, 0, UINT32_MAX, 0, 0, 0, 0, 0 },
{ 0, 0, 0, UINT32_MAX, 0, 0, 0, 0 },
{ 0, 0, 0, 0, UINT32_MAX, 0, 0, 0 },
{ 0, 0, 0, 0, 0, UINT32_MAX, 0, 0 },
{ 0, 0, 0, 0, 0, 0, UINT32_MAX, 0 },
{ 0, 0, 0, 0, 0, 0, 0, UINT32_MAX },
};
CPPSPMD_FORCE_INLINE __m128 get_lo(__m256i v) { return _mm256_castps256_ps128(_mm256_castsi256_ps(v)); }
CPPSPMD_FORCE_INLINE __m128 get_lo(__m256 v) { return _mm256_castps256_ps128(v); }
CPPSPMD_FORCE_INLINE __m128i get_lo_i(__m256i v) { return _mm_castps_si128(_mm256_castps256_ps128(_mm256_castsi256_ps(v))); }
CPPSPMD_FORCE_INLINE __m128i get_lo_i(__m256 v) { return _mm_castps_si128(_mm256_castps256_ps128(v)); }
CPPSPMD_FORCE_INLINE __m128 get_hi(__m256i v) { return _mm256_extractf128_ps(_mm256_castsi256_ps(v), 1); }
CPPSPMD_FORCE_INLINE __m128 get_hi(__m256 v) { return _mm256_extractf128_ps(v, 1); }
CPPSPMD_FORCE_INLINE __m128i get_hi_i(__m256i v) { return _mm_castps_si128(_mm256_extractf128_ps(_mm256_castsi256_ps(v), 1)); }
CPPSPMD_FORCE_INLINE __m128i get_hi_i(__m256 v) { return _mm_castps_si128(_mm256_extractf128_ps(v, 1)); }
CPPSPMD_FORCE_INLINE __m256i combine_i(__m128 lo, __m128 hi) { return _mm256_castps_si256(_mm256_setr_m128(lo, hi)); }
CPPSPMD_FORCE_INLINE __m256i combine_i(__m128i lo, __m128i hi) { return _mm256_setr_m128i(lo, hi); }
CPPSPMD_FORCE_INLINE __m256 combine(__m128 lo, __m128 hi) { return _mm256_setr_m128(lo, hi); }
CPPSPMD_FORCE_INLINE __m256 combine(__m128i lo, __m128i hi) { return _mm256_castsi256_ps(_mm256_setr_m128i(lo, hi)); }
CPPSPMD_FORCE_INLINE __m256i compare_gt_epi32(__m256i a, __m256i b)
{
#if CPPSPMD_USE_AVX2
return _mm256_cmpgt_epi32(a, b);
#else
return combine_i(_mm_cmpgt_epi32(get_lo_i(a), get_lo_i(b)), _mm_cmpgt_epi32(get_hi_i(a), get_hi_i(b)));
#endif
}
CPPSPMD_FORCE_INLINE __m256i compare_eq_epi32(__m256i a, __m256i b)
{
#if CPPSPMD_USE_AVX2
return _mm256_cmpeq_epi32(a, b);
#else
return combine_i(_mm_cmpeq_epi32(get_lo_i(a), get_lo_i(b)), _mm_cmpeq_epi32(get_hi_i(a), get_hi_i(b)));
#endif
}
CPPSPMD_FORCE_INLINE __m256i add_epi32(__m256i a, __m256i b)
{
#if CPPSPMD_USE_AVX2
return _mm256_add_epi32(a, b);
#else
return combine_i(_mm_add_epi32(get_lo_i(a), get_lo_i(b)), _mm_add_epi32(get_hi_i(a), get_hi_i(b)));
#endif
}
CPPSPMD_FORCE_INLINE __m256i sub_epi32(__m256i a, __m256i b)
{
#if CPPSPMD_USE_AVX2
return _mm256_sub_epi32(a, b);
#else
return combine_i(_mm_sub_epi32(get_lo_i(a), get_lo_i(b)), _mm_sub_epi32(get_hi_i(a), get_hi_i(b)));
#endif
}
CPPSPMD_FORCE_INLINE __m256i and_si256(__m256i a, __m256i b)
{
#if CPPSPMD_USE_AVX2
return _mm256_and_si256(a, b);
#else
return _mm256_castps_si256(_mm256_and_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b)));
#endif
}
CPPSPMD_FORCE_INLINE __m256i or_si256(__m256i a, __m256i b)
{
#if CPPSPMD_USE_AVX2
return _mm256_or_si256(a, b);
#else
return _mm256_castps_si256(_mm256_or_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b)));
#endif
}
CPPSPMD_FORCE_INLINE __m256i xor_si256(__m256i a, __m256i b)
{
#if CPPSPMD_USE_AVX2
return _mm256_xor_si256(a, b);
#else
return _mm256_castps_si256(_mm256_xor_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b)));
#endif
}
CPPSPMD_FORCE_INLINE __m256i andnot_si256(__m256i a, __m256i b)
{
#if CPPSPMD_USE_AVX2
return _mm256_andnot_si256(a, b);
#else
return _mm256_castps_si256(_mm256_andnot_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b)));
#endif
}
CPPSPMD_FORCE_INLINE __m256i mullo_epi32(__m256i a, __m256i b)
{
#if CPPSPMD_USE_AVX2
return _mm256_mullo_epi32(a, b);
#else
return combine_i(_mm_mullo_epi32(get_lo_i(a), get_lo_i(b)), _mm_mullo_epi32(get_hi_i(a), get_hi_i(b)));
#endif
}
CPPSPMD_FORCE_INLINE __m128i _mm_blendv_epi32(__m128i a, __m128i b, __m128i c) { return _mm_castps_si128(_mm_blendv_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), _mm_castsi128_ps(c))); }
CPPSPMD_FORCE_INLINE __m256i blendv_epi32(__m256i a, __m256i b, __m256i c)
{
#if CPPSPMD_USE_AVX2
return _mm256_castps_si256(_mm256_blendv_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b), _mm256_castsi256_ps(c)));
#else
return combine_i(_mm_blendv_epi32(get_lo_i(a), get_lo_i(b), get_lo_i(c)), _mm_blendv_epi32(get_hi_i(a), get_hi_i(b), get_hi_i(c)));
#endif
}
#if CPPSPMD_USE_AVX2
CPPSPMD_FORCE_INLINE __m256i mulhi_epu32(__m256i a, __m256i b)
{
__m256i tmp1 = _mm256_mul_epu32(a, b);
__m256i tmp2 = _mm256_mul_epu32(_mm256_srli_si256(a, 4), _mm256_srli_si256(b, 4));
return _mm256_unpacklo_epi32(_mm256_shuffle_epi32(tmp1, _MM_SHUFFLE(0, 0, 3, 1)), _mm256_shuffle_epi32(tmp2, _MM_SHUFFLE(0, 0, 3, 1)));
}
#else
CPPSPMD_FORCE_INLINE __m128i mulhi_epu32(__m128i a, __m128i b)
{
__m128i tmp1 = _mm_mul_epu32(a, b);
__m128i tmp2 = _mm_mul_epu32(_mm_srli_si128(a, 4), _mm_srli_si128(b, 4));
return _mm_unpacklo_epi32(_mm_shuffle_epi32(tmp1, _MM_SHUFFLE(0, 0, 3, 1)), _mm_shuffle_epi32(tmp2, _MM_SHUFFLE(0, 0, 3, 1)));
}
CPPSPMD_FORCE_INLINE __m256i mulhi_epu32(__m256i a, __m256i b)
{
return combine_i(mulhi_epu32(get_lo_i(a), get_lo_i(b)), mulhi_epu32(get_hi_i(a), get_hi_i(b)));
}
#endif
const uint32_t ALL_ON_MOVEMASK = 0xFF;
struct spmd_kernel
{
struct vint;
struct lint;
struct vbool;
struct vfloat;
typedef int int_t;
typedef vint vint_t;
typedef lint lint_t;
// Exec mask
struct exec_mask
{
__m256i m_mask;
exec_mask() = default;
CPPSPMD_FORCE_INLINE explicit exec_mask(const vbool& b);
CPPSPMD_FORCE_INLINE explicit exec_mask(const __m256i& mask) : m_mask(mask) { }
CPPSPMD_FORCE_INLINE void enable_lane(uint32_t lane) { m_mask = _mm256_load_si256((const __m256i *)&g_lane_masks_256[lane][0]); }
static CPPSPMD_FORCE_INLINE exec_mask all_on() { return exec_mask{ _mm256_load_si256((const __m256i*)g_allones_256) }; }
static CPPSPMD_FORCE_INLINE exec_mask all_off() { return exec_mask{ _mm256_setzero_si256() }; }
CPPSPMD_FORCE_INLINE uint32_t get_movemask() const { return _mm256_movemask_ps(_mm256_castsi256_ps(m_mask)); }
};
friend CPPSPMD_FORCE_INLINE bool all(const exec_mask& e);
friend CPPSPMD_FORCE_INLINE bool any(const exec_mask& e);
CPPSPMD_FORCE_INLINE bool spmd_all() const { return all(m_exec); }
CPPSPMD_FORCE_INLINE bool spmd_any() const { return any(m_exec); }
CPPSPMD_FORCE_INLINE bool spmd_none() { return !any(m_exec); }
// true if cond is true for all active lanes - false if no active lanes
CPPSPMD_FORCE_INLINE bool spmd_all(const vbool& e) { uint32_t m = m_exec.get_movemask(); return (m != 0) && ((exec_mask(e) & m_exec).get_movemask() == m); }
// true if cond is true for any active lanes
CPPSPMD_FORCE_INLINE bool spmd_any(const vbool& e) { return (exec_mask(e) & m_exec).get_movemask() != 0; }
CPPSPMD_FORCE_INLINE bool spmd_none(const vbool& e) { return !spmd_any(e); }
friend CPPSPMD_FORCE_INLINE exec_mask operator^ (const exec_mask& a, const exec_mask& b);
friend CPPSPMD_FORCE_INLINE exec_mask operator& (const exec_mask& a, const exec_mask& b);
friend CPPSPMD_FORCE_INLINE exec_mask operator| (const exec_mask& a, const exec_mask& b);
exec_mask m_exec;
exec_mask m_kernel_exec;
exec_mask m_continue_mask;
#ifdef _DEBUG
bool m_in_loop;
#endif
CPPSPMD_FORCE_INLINE uint32_t get_movemask() const { return m_exec.get_movemask(); }
void init(const exec_mask& kernel_exec);
// Varying bool
struct vbool
{
__m256i m_value;
vbool() = default;
CPPSPMD_FORCE_INLINE vbool(bool value) : m_value(_mm256_set1_epi32(value ? UINT32_MAX : 0)) { }
CPPSPMD_FORCE_INLINE explicit vbool(const __m256i& value) : m_value(value) { }
CPPSPMD_FORCE_INLINE explicit operator vfloat() const;
CPPSPMD_FORCE_INLINE explicit operator vint() const;
private:
vbool& operator=(const vbool&);
};
friend vbool operator!(const vbool& v);
CPPSPMD_FORCE_INLINE vbool& store(vbool& dst, const vbool& src)
{
dst.m_value = _mm256_castps_si256(_mm256_blendv_ps(_mm256_castsi256_ps(dst.m_value), _mm256_castsi256_ps(src.m_value), _mm256_castsi256_ps(m_exec.m_mask)));
return dst;
}
CPPSPMD_FORCE_INLINE vbool& store_all(vbool& dst, const vbool& src)
{
dst.m_value = src.m_value;
return dst;
}
// Varying float
struct vfloat
{
__m256 m_value;
vfloat() = default;
CPPSPMD_FORCE_INLINE explicit vfloat(const __m256& v) : m_value(v) { }
CPPSPMD_FORCE_INLINE vfloat(float value) : m_value(_mm256_set1_ps(value)) { }
CPPSPMD_FORCE_INLINE explicit vfloat(int value) : m_value(_mm256_set1_ps((float)value)) { }
private:
vfloat& operator=(const vfloat&);
};
CPPSPMD_FORCE_INLINE vfloat& store(vfloat& dst, const vfloat& src)
{
dst.m_value = _mm256_blendv_ps(dst.m_value, src.m_value, _mm256_castsi256_ps(m_exec.m_mask));
return dst;
}
CPPSPMD_FORCE_INLINE vfloat& store(vfloat&& dst, const vfloat& src)
{
dst.m_value = _mm256_blendv_ps(dst.m_value, src.m_value, _mm256_castsi256_ps(m_exec.m_mask));
return dst;
}
CPPSPMD_FORCE_INLINE vfloat& store_all(vfloat& dst, const vfloat& src)
{
dst.m_value = src.m_value;
return dst;
}
CPPSPMD_FORCE_INLINE vfloat& store_all(vfloat&& dst, const vfloat& src)
{
dst.m_value = src.m_value;
return dst;
}
// Linear ref to floats
struct float_lref
{
float* m_pValue;
private:
float_lref& operator=(const float_lref&);
};
CPPSPMD_FORCE_INLINE const float_lref& store(const float_lref& dst, const vfloat& src)
{
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(m_exec.m_mask));
if (mask == ALL_ON_MOVEMASK)
_mm256_storeu_ps(dst.m_pValue, src.m_value);
else
_mm256_maskstore_ps(dst.m_pValue, m_exec.m_mask, src.m_value);
return dst;
}
CPPSPMD_FORCE_INLINE const float_lref& store(const float_lref&& dst, const vfloat& src)
{
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(m_exec.m_mask));
if (mask == ALL_ON_MOVEMASK)
_mm256_storeu_ps(dst.m_pValue, src.m_value);
else
_mm256_maskstore_ps(dst.m_pValue, m_exec.m_mask, src.m_value);
return dst;
}
CPPSPMD_FORCE_INLINE const float_lref& store_all(const float_lref& dst, const vfloat& src)
{
_mm256_storeu_ps(dst.m_pValue, src.m_value);
return dst;
}
CPPSPMD_FORCE_INLINE const float_lref& store_all(const float_lref&& dst, const vfloat& src)
{
_mm256_storeu_ps(dst.m_pValue, src.m_value);
return dst;
}
CPPSPMD_FORCE_INLINE vfloat load(const float_lref& src)
{
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(m_exec.m_mask));
if (mask == ALL_ON_MOVEMASK)
return vfloat{ _mm256_loadu_ps(src.m_pValue) };
else
return vfloat{ _mm256_maskload_ps(src.m_pValue, m_exec.m_mask) };
}
// Varying ref to floats
struct float_vref
{
__m256i m_vindex;
float* m_pValue;
private:
float_vref& operator=(const float_vref&);
};
// Varying ref to varying float
struct vfloat_vref
{
__m256i m_vindex;
vfloat* m_pValue;
private:
vfloat_vref& operator=(const vfloat_vref&);
};
// Varying ref to varying int
struct vint_vref
{
__m256i m_vindex;
vint* m_pValue;
private:
vint_vref& operator=(const vint_vref&);
};
CPPSPMD_FORCE_INLINE const float_vref& store(const float_vref& dst, const vfloat& src);
CPPSPMD_FORCE_INLINE const float_vref& store(const float_vref&& dst, const vfloat& src);
CPPSPMD_FORCE_INLINE const float_vref& store_all(const float_vref& dst, const vfloat& src);
CPPSPMD_FORCE_INLINE const float_vref& store_all(const float_vref&& dst, const vfloat& src);
CPPSPMD_FORCE_INLINE vfloat load(const float_vref& src)
{
#if CPPSPMD_USE_AVX2
return vfloat{ _mm256_mask_i32gather_ps(_mm256_castsi256_ps(_mm256_setzero_si256()),
src.m_pValue, src.m_vindex,
_mm256_castsi256_ps(m_exec.m_mask),
4) };
#else
__m128i v0_l = _mm_setzero_si128(), v0_h = _mm_setzero_si128();
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(m_exec.m_mask));
const int* pSrc_ints = (const int*)src.m_pValue;
if (mask & 1) v0_l = _mm_insert_epi32(v0_l, pSrc_ints[_mm256_extract_epi32(src.m_vindex, 0)], 0);
if (mask & 2) v0_l = _mm_insert_epi32(v0_l, pSrc_ints[_mm256_extract_epi32(src.m_vindex, 1)], 1);
if (mask & 4) v0_l = _mm_insert_epi32(v0_l, pSrc_ints[_mm256_extract_epi32(src.m_vindex, 2)], 2);
if (mask & 8) v0_l = _mm_insert_epi32(v0_l, pSrc_ints[_mm256_extract_epi32(src.m_vindex, 3)], 3);
if (mask & 16) v0_h = _mm_insert_epi32(v0_h, pSrc_ints[_mm256_extract_epi32(src.m_vindex, 4)], 0);
if (mask & 32) v0_h = _mm_insert_epi32(v0_h, pSrc_ints[_mm256_extract_epi32(src.m_vindex, 5)], 1);
if (mask & 64) v0_h = _mm_insert_epi32(v0_h, pSrc_ints[_mm256_extract_epi32(src.m_vindex, 6)], 2);
if (mask & 128) v0_h = _mm_insert_epi32(v0_h, pSrc_ints[_mm256_extract_epi32(src.m_vindex, 7)], 3);
return vfloat{ _mm256_castsi256_ps(CPPSPMD::combine_i(v0_l, v0_h)) };
#endif
}
CPPSPMD_FORCE_INLINE vfloat load_all(const float_vref& src)
{
#if CPPSPMD_USE_AVX2
return vfloat{ _mm256_i32gather_ps(src.m_pValue, src.m_vindex, 4) };
#else
__m128i v0_l = _mm_undefined_si128(), v0_h = _mm_undefined_si128();
const int* pSrc_ints = (const int*)src.m_pValue;
v0_l = _mm_insert_epi32(v0_l, pSrc_ints[_mm256_extract_epi32(src.m_vindex, 0)], 0);
v0_l = _mm_insert_epi32(v0_l, pSrc_ints[_mm256_extract_epi32(src.m_vindex, 1)], 1);
v0_l = _mm_insert_epi32(v0_l, pSrc_ints[_mm256_extract_epi32(src.m_vindex, 2)], 2);
v0_l = _mm_insert_epi32(v0_l, pSrc_ints[_mm256_extract_epi32(src.m_vindex, 3)], 3);
v0_h = _mm_insert_epi32(v0_h, pSrc_ints[_mm256_extract_epi32(src.m_vindex, 4)], 0);
v0_h = _mm_insert_epi32(v0_h, pSrc_ints[_mm256_extract_epi32(src.m_vindex, 5)], 1);
v0_h = _mm_insert_epi32(v0_h, pSrc_ints[_mm256_extract_epi32(src.m_vindex, 6)], 2);
v0_h = _mm_insert_epi32(v0_h, pSrc_ints[_mm256_extract_epi32(src.m_vindex, 7)], 3);
return vfloat{ _mm256_castsi256_ps(CPPSPMD::combine_i(v0_l, v0_h)) };
#endif
}
// Linear ref to ints
struct int_lref
{
int* m_pValue;
private:
int_lref& operator=(const int_lref&);
};
CPPSPMD_FORCE_INLINE const int_lref& store(const int_lref& dst, const vint& src)
{
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(m_exec.m_mask));
if (mask == ALL_ON_MOVEMASK)
{
_mm256_storeu_si256((__m256i*)dst.m_pValue, src.m_value);
}
else
{
#if CPPSPMD_USE_AVX2
_mm256_maskstore_epi32(dst.m_pValue, m_exec.m_mask, src.m_value);
#else
if (mask & 1) dst.m_pValue[0] = _mm256_extract_epi32(src.m_value, 0);
if (mask & 2) dst.m_pValue[1] = _mm256_extract_epi32(src.m_value, 1);
if (mask & 4) dst.m_pValue[2] = _mm256_extract_epi32(src.m_value, 2);
if (mask & 8) dst.m_pValue[3] = _mm256_extract_epi32(src.m_value, 3);
if (mask & 16) dst.m_pValue[4] = _mm256_extract_epi32(src.m_value, 4);
if (mask & 32) dst.m_pValue[5] = _mm256_extract_epi32(src.m_value, 5);
if (mask & 64) dst.m_pValue[6] = _mm256_extract_epi32(src.m_value, 6);
if (mask & 128) dst.m_pValue[7] = _mm256_extract_epi32(src.m_value, 7);
#endif
}
return dst;
}
CPPSPMD_FORCE_INLINE vint load(const int_lref& src)
{
#if CPPSPMD_USE_AVX2
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(m_exec.m_mask));
if (mask == ALL_ON_MOVEMASK)
return vint{ _mm256_loadu_si256((__m256i*)src.m_pValue) };
else
return vint{ _mm256_maskload_epi32(src.m_pValue, m_exec.m_mask) };
#else
__m256i v = _mm256_loadu_si256((const __m256i*)src.m_pValue);
v = _mm256_castps_si256(_mm256_and_ps(_mm256_castsi256_ps(v), _mm256_castsi256_ps(m_exec.m_mask)));
return vint{ v };
#endif
}
// Linear ref to int16's
struct int16_lref
{
int16_t* m_pValue;
private:
int16_lref& operator=(const int16_lref&);
};
CPPSPMD_FORCE_INLINE int16_lref& store(int16_lref& dst, const vint& src)
{
CPPSPMD_ALIGN(32) int stored[8];
_mm256_store_si256((__m256i*)stored, src.m_value);
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(m_exec.m_mask));
for (int i = 0; i < 8; i++)
{
if (mask & (1 << i))
dst.m_pValue[i] = static_cast<int16_t>(stored[i]);
}
return dst;
}
CPPSPMD_FORCE_INLINE const int16_lref& store_all(const int16_lref& dst, const vint& src)
{
CPPSPMD_ALIGN(32) int stored[8];
_mm256_store_si256((__m256i*)stored, src.m_value);
for (int i = 0; i < 8; i++)
dst.m_pValue[i] = static_cast<int16_t>(stored[i]);
return dst;
}
CPPSPMD_FORCE_INLINE vint load(const int16_lref& src)
{
CPPSPMD_ALIGN(32) int values[8];
for (int i = 0; i < 8; i++)
values[i] = static_cast<int16_t>(src.m_pValue[i]);
__m256i t = _mm256_load_si256( (const __m256i *)values );
return vint{ _mm256_castps_si256(_mm256_and_ps(_mm256_castsi256_ps( t ), _mm256_castsi256_ps(m_exec.m_mask))) };
}
CPPSPMD_FORCE_INLINE vint load_all(const int16_lref& src)
{
CPPSPMD_ALIGN(32) int values[8];
for (int i = 0; i < 8; i++)
values[i] = static_cast<int16_t>(src.m_pValue[i]);
__m256i t = _mm256_load_si256( (const __m256i *)values );
return vint{ t };
}
// Linear ref to constant int's
struct cint_lref
{
const int* m_pValue;
private:
cint_lref& operator=(const cint_lref&);
};
CPPSPMD_FORCE_INLINE vint load(const cint_lref& src)
{
#if CPPSPMD_USE_AVX2
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(m_exec.m_mask));
if (mask == ALL_ON_MOVEMASK)
return vint{ _mm256_loadu_si256((const __m256i*)src.m_pValue) };
else
return vint{ _mm256_maskload_epi32(src.m_pValue, m_exec.m_mask) };
#else
__m256i v = _mm256_loadu_si256((const __m256i*)src.m_pValue);
v = _mm256_castps_si256(_mm256_and_ps(_mm256_castsi256_ps(v), _mm256_castsi256_ps(m_exec.m_mask)));
return vint{ v };
#endif
}
CPPSPMD_FORCE_INLINE vint load_all(const cint_lref& src)
{
return vint{ _mm256_loadu_si256((const __m256i*)src.m_pValue) };
}
// Varying ref to ints
struct int_vref
{
__m256i m_vindex;
int* m_pValue;
private:
int_vref& operator=(const int_vref&);
};
// Varying ref to constant ints
struct cint_vref
{
__m256i m_vindex;
const int* m_pValue;
private:
cint_vref& operator=(const cint_vref&);
};
// Varying int
struct vint
{
__m256i m_value;
vint() = default;
CPPSPMD_FORCE_INLINE explicit vint(const __m256i& value) : m_value(value) { }
CPPSPMD_FORCE_INLINE vint(int value) : m_value(_mm256_set1_epi32(value)) { }
CPPSPMD_FORCE_INLINE explicit vint(float value) : m_value(_mm256_set1_epi32((int)value)) { }
CPPSPMD_FORCE_INLINE explicit vint(const vfloat& other) : m_value(_mm256_cvttps_epi32(other.m_value)) { }
CPPSPMD_FORCE_INLINE explicit operator vbool() const
{
return vbool{ xor_si256( _mm256_load_si256((const __m256i*)g_allones_256), compare_eq_epi32(m_value, _mm256_setzero_si256())) };
}
CPPSPMD_FORCE_INLINE explicit operator vfloat() const
{
return vfloat{ _mm256_cvtepi32_ps(m_value) };
}
CPPSPMD_FORCE_INLINE int_vref operator[](int* ptr) const
{
return int_vref{ m_value, ptr };
}
CPPSPMD_FORCE_INLINE cint_vref operator[](const int* ptr) const
{
return cint_vref{ m_value, ptr };
}
CPPSPMD_FORCE_INLINE float_vref operator[](float* ptr) const
{
return float_vref{ m_value, ptr };
}
CPPSPMD_FORCE_INLINE vfloat_vref operator[](vfloat* ptr) const
{
return vfloat_vref{ m_value, ptr };
}
CPPSPMD_FORCE_INLINE vint_vref operator[](vint* ptr) const
{
return vint_vref{ m_value, ptr };
}
private:
vint& operator=(const vint&);
};
// Load/store linear integer
CPPSPMD_FORCE_INLINE void storeu_linear(int *pDst, const vint& src)
{
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(m_exec.m_mask));
if (mask == ALL_ON_MOVEMASK)
{
_mm256_storeu_si256((__m256i*)pDst, src.m_value);
}
else
{
if (mask & 1) pDst[0] = _mm256_extract_epi32(src.m_value, 0);
if (mask & 2) pDst[1] = _mm256_extract_epi32(src.m_value, 1);
if (mask & 4) pDst[2] = _mm256_extract_epi32(src.m_value, 2);
if (mask & 8) pDst[3] = _mm256_extract_epi32(src.m_value, 3);
if (mask & 16) pDst[4] = _mm256_extract_epi32(src.m_value, 4);
if (mask & 32) pDst[5] = _mm256_extract_epi32(src.m_value, 5);
if (mask & 64) pDst[6] = _mm256_extract_epi32(src.m_value, 6);
if (mask & 128) pDst[7] = _mm256_extract_epi32(src.m_value, 7);
}
}
CPPSPMD_FORCE_INLINE void storeu_linear_all(int *pDst, const vint& src)
{
_mm256_storeu_si256((__m256i*)pDst, src.m_value);
}
CPPSPMD_FORCE_INLINE void store_linear_all(int *pDst, const vint& src)
{
_mm256_store_si256((__m256i*)pDst, src.m_value);
}
CPPSPMD_FORCE_INLINE vint loadu_linear(const int *pSrc)
{
#if CPPSPMD_USE_AVX2
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(m_exec.m_mask));
__m256i result;
if (mask == ALL_ON_MOVEMASK)
result = _mm256_loadu_si256((__m256i*)pSrc);
else
result = _mm256_maskload_epi32(pSrc, m_exec.m_mask);
return vint{ result };
#else
__m256i v = _mm256_loadu_si256((const __m256i*)pSrc);
v = _mm256_castps_si256(_mm256_and_ps(_mm256_castsi256_ps(v), _mm256_castsi256_ps(m_exec.m_mask)));
return vint{ v };
#endif
}
CPPSPMD_FORCE_INLINE vint loadu_linear_all(const int *pSrc)
{
return vint{ _mm256_loadu_si256((__m256i*)pSrc) };
}
CPPSPMD_FORCE_INLINE vint load_linear_all(const int *pSrc)
{
return vint{ _mm256_load_si256((__m256i*)pSrc) };
}
// load/store linear float
CPPSPMD_FORCE_INLINE void storeu_linear(float *pDst, const vfloat& src)
{
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(m_exec.m_mask));
if (mask == ALL_ON_MOVEMASK)
{
_mm256_storeu_ps((float*)pDst, src.m_value);
}
else
{
int *pDstI = (int *)pDst;
if (mask & 1) pDstI[0] = _mm256_extract_epi32(_mm256_castps_si256(src.m_value), 0);
if (mask & 2) pDstI[1] = _mm256_extract_epi32(_mm256_castps_si256(src.m_value), 1);
if (mask & 4) pDstI[2] = _mm256_extract_epi32(_mm256_castps_si256(src.m_value), 2);
if (mask & 8) pDstI[3] = _mm256_extract_epi32(_mm256_castps_si256(src.m_value), 3);
if (mask & 16) pDstI[4] = _mm256_extract_epi32(_mm256_castps_si256(src.m_value), 4);
if (mask & 32) pDstI[5] = _mm256_extract_epi32(_mm256_castps_si256(src.m_value), 5);
if (mask & 64) pDstI[6] = _mm256_extract_epi32(_mm256_castps_si256(src.m_value), 6);
if (mask & 128) pDstI[7] = _mm256_extract_epi32(_mm256_castps_si256(src.m_value), 7);
}
}
CPPSPMD_FORCE_INLINE void storeu_linear_all(float *pDst, const vfloat& src)
{
_mm256_storeu_ps((float*)pDst, src.m_value);
}
CPPSPMD_FORCE_INLINE void store_linear_all(float *pDst, const vfloat& src)
{
_mm256_store_ps((float*)pDst, src.m_value);
}
CPPSPMD_FORCE_INLINE vfloat loadu_linear(const float *pSrc)
{
#if CPPSPMD_USE_AVX2
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(m_exec.m_mask));
if (mask == ALL_ON_MOVEMASK)
return vfloat{ _mm256_loadu_ps((float*)pSrc) };
else
return vfloat{ _mm256_maskload_ps(pSrc, m_exec.m_mask) };
#else
__m256 v = _mm256_loadu_ps((const float*)pSrc);
v = _mm256_and_ps(v, _mm256_castsi256_ps(m_exec.m_mask));
return vfloat{ v };
#endif
}
CPPSPMD_FORCE_INLINE vfloat loadu_linear_all(const float *pSrc)
{
return vfloat{ _mm256_loadu_ps((float*)pSrc) };
}
CPPSPMD_FORCE_INLINE vfloat load_linear_all(const float *pSrc)
{
return vfloat{ _mm256_load_ps((float*)pSrc) };
}
CPPSPMD_FORCE_INLINE vint& store(vint& dst, const vint& src)
{
dst.m_value = _mm256_castps_si256(_mm256_blendv_ps(_mm256_castsi256_ps(dst.m_value), _mm256_castsi256_ps(src.m_value), _mm256_castsi256_ps(m_exec.m_mask)));
return dst;
}
CPPSPMD_FORCE_INLINE const int_vref& store(const int_vref& dst, const vint& src)
{
CPPSPMD_ALIGN(32) int vindex[8];
_mm256_store_si256((__m256i*)vindex, dst.m_vindex);
CPPSPMD_ALIGN(32) int stored[8];
_mm256_store_si256((__m256i*)stored, src.m_value);
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(m_exec.m_mask));
for (int i = 0; i < 8; i++)
{
if (mask & (1 << i))
dst.m_pValue[vindex[i]] = stored[i];
}
return dst;
}
CPPSPMD_FORCE_INLINE vint& store_all(vint& dst, const vint& src)
{
dst.m_value = src.m_value;
return dst;
}
CPPSPMD_FORCE_INLINE const int_vref& store_all(const int_vref& dst, const vint& src)
{
CPPSPMD_ALIGN(32) int stored[8];
_mm256_store_si256((__m256i*)stored, src.m_value);
#if 0
CPPSPMD_ALIGN(32) int vindex[8];
_mm256_store_si256((__m256i*)vindex, dst.m_vindex);
for (int i = 0; i < 8; i++)
dst.m_pValue[vindex[i]] = stored[i];
#else
int *pDst = dst.m_pValue;
pDst[_mm256_extract_epi32(dst.m_vindex, 0)] = stored[0];
pDst[_mm256_extract_epi32(dst.m_vindex, 1)] = stored[1];
pDst[_mm256_extract_epi32(dst.m_vindex, 2)] = stored[2];
pDst[_mm256_extract_epi32(dst.m_vindex, 3)] = stored[3];
pDst[_mm256_extract_epi32(dst.m_vindex, 4)] = stored[4];
pDst[_mm256_extract_epi32(dst.m_vindex, 5)] = stored[5];
pDst[_mm256_extract_epi32(dst.m_vindex, 6)] = stored[6];
pDst[_mm256_extract_epi32(dst.m_vindex, 7)] = stored[7];
#endif
return dst;
}
CPPSPMD_FORCE_INLINE vint load(const int_vref& src)
{
#if CPPSPMD_USE_AVX2
return vint{ _mm256_mask_i32gather_epi32(_mm256_setzero_si256(), src.m_pValue, src.m_vindex, m_exec.m_mask, 4) };
#else
#if 0
CPPSPMD_ALIGN(32) int values[8];
CPPSPMD_ALIGN(32) int indices[8];
_mm256_store_si256((__m256i *)indices, src.m_vindex);
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(m_exec.m_mask));
for (int i = 0; i < 8; i++)
{
if (mask & (1 << i))
values[i] = src.m_pValue[indices[i]];
}
return vint{ _mm256_castps_si256(_mm256_and_ps(_mm256_castsi256_ps(m_exec.m_mask), _mm256_load_ps((const float*)values))) };
#endif
__m128i v0_l = _mm_setzero_si128(), v0_h = _mm_setzero_si128();
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(m_exec.m_mask));
if (mask & 1) v0_l = _mm_insert_epi32(v0_l, src.m_pValue[_mm256_extract_epi32(src.m_vindex, 0)], 0);
if (mask & 2) v0_l = _mm_insert_epi32(v0_l, src.m_pValue[_mm256_extract_epi32(src.m_vindex, 1)], 1);
if (mask & 4) v0_l = _mm_insert_epi32(v0_l, src.m_pValue[_mm256_extract_epi32(src.m_vindex, 2)], 2);
if (mask & 8) v0_l = _mm_insert_epi32(v0_l, src.m_pValue[_mm256_extract_epi32(src.m_vindex, 3)], 3);
if (mask & 16) v0_h = _mm_insert_epi32(v0_h, src.m_pValue[_mm256_extract_epi32(src.m_vindex, 4)], 0);
if (mask & 32) v0_h = _mm_insert_epi32(v0_h, src.m_pValue[_mm256_extract_epi32(src.m_vindex, 5)], 1);
if (mask & 64) v0_h = _mm_insert_epi32(v0_h, src.m_pValue[_mm256_extract_epi32(src.m_vindex, 6)], 2);
if (mask & 128) v0_h = _mm_insert_epi32(v0_h, src.m_pValue[_mm256_extract_epi32(src.m_vindex, 7)], 3);
return vint{ CPPSPMD::combine_i(v0_l, v0_h) };
#endif
}
CPPSPMD_FORCE_INLINE vint load_all(const int_vref& src)
{
#if CPPSPMD_USE_AVX2
return vint{ _mm256_i32gather_epi32(src.m_pValue, src.m_vindex, 4) };
#else
#if 0
CPPSPMD_ALIGN(32) int values[8];
CPPSPMD_ALIGN(32) int indices[8];
_mm256_store_si256((__m256i *)indices, src.m_vindex);
for (int i = 0; i < 8; i++)
values[i] = src.m_pValue[indices[i]];
#endif
#if 0
const int *pSrc = src.m_pValue;
values[0] = pSrc[_mm256_extract_epi32(src.m_vindex, 0)];
values[1] = pSrc[_mm256_extract_epi32(src.m_vindex, 1)];
values[2] = pSrc[_mm256_extract_epi32(src.m_vindex, 2)];
values[3] = pSrc[_mm256_extract_epi32(src.m_vindex, 3)];
values[4] = pSrc[_mm256_extract_epi32(src.m_vindex, 4)];
values[5] = pSrc[_mm256_extract_epi32(src.m_vindex, 5)];
values[6] = pSrc[_mm256_extract_epi32(src.m_vindex, 6)];
values[7] = pSrc[_mm256_extract_epi32(src.m_vindex, 7)];