forked from richgel999/CppSPMD_Fast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcppspmd_float4.h
2066 lines (1677 loc) · 85.8 KB
/
cppspmd_float4.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_float4.h
// This is *very* slow, but it's useful for debugging, verification, and porting.
// 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 <algorithm>
// Set to 1 to use std::fmaf()
#define CPPSPMD_USE_FMAF 0
#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 0
#define CPPSPMD_AVX1 0
#define CPPSPMD_AVX2 0
#define CPPSPMD_FLOAT4 1
#define CPPSPMD_INT16 0
#ifdef _MSC_VER
#ifndef CPPSPMD_DECL
#define CPPSPMD_DECL(type, name) __declspec(align(16)) 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
#define CPPSPMD cppspmd_float4
#define CPPSPMD_ARCH _float4
#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 (16)
namespace CPPSPMD
{
const int PROGRAM_COUNT_SHIFT = 2;
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_128[4]) = { UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX };
CPPSPMD_DECL(const float, g_onef_128[4]) = { 1.0f, 1.0f, 1.0f, 1.0f };
CPPSPMD_DECL(const uint32_t, g_oneu_128[4]) = { 1, 1, 1, 1 };
struct int4;
CPPSPMD_ALIGN(16)
struct float4
{
float c[4];
CPPSPMD_FORCE_INLINE float4() = default;
CPPSPMD_FORCE_INLINE float4(const float4 &a) { c[0] = a.c[0]; c[1] = a.c[1]; c[2] = a.c[2]; c[3] = a.c[3]; }
CPPSPMD_FORCE_INLINE float4(float x, float y, float z, float w) { c[0] = x; c[1] = y; c[2] = z; c[3] = w; }
CPPSPMD_FORCE_INLINE float4& operator=(const float4 &a) { c[0] = a.c[0]; c[1] = a.c[1]; c[2] = a.c[2]; c[3] = a.c[3]; return *this; }
};
CPPSPMD_ALIGN(16)
struct int4
{
int32_t c[4];
CPPSPMD_FORCE_INLINE int4() = default;
CPPSPMD_FORCE_INLINE int4(const int4 &a) { c[0] = a.c[0]; c[1] = a.c[1]; c[2] = a.c[2]; c[3] = a.c[3]; }
CPPSPMD_FORCE_INLINE int4(int x, int y, int z, int w) { c[0] = x; c[1] = y; c[2] = z; c[3] = w; }
CPPSPMD_FORCE_INLINE int4& operator=(const int4 &a) { c[0] = a.c[0]; c[1] = a.c[1]; c[2] = a.c[2]; c[3] = a.c[3]; return *this; }
};
#define CAST_F_TO_I(f) *(int *)(&(f))
#define CAST_I_TO_F(i) *(float *)(&(i))
CPPSPMD_FORCE_INLINE int4 cast_float4_to_int4(const float4 &a) { return int4(CAST_F_TO_I(a.c[0]), CAST_F_TO_I(a.c[1]), CAST_F_TO_I(a.c[2]), CAST_F_TO_I(a.c[3])); }
CPPSPMD_FORCE_INLINE float4 cast_int4_to_float4(const int4 &a) { return float4(CAST_I_TO_F(a.c[0]), CAST_I_TO_F(a.c[1]), CAST_I_TO_F(a.c[2]), CAST_I_TO_F(a.c[3])); }
CPPSPMD_FORCE_INLINE float4 create_float4_rev(float w, float z, float y, float x) { return float4(x, y, z, w); }
CPPSPMD_FORCE_INLINE float4 create_float4(float x, float y, float z, float w) { return float4(x, y, z, w); }
CPPSPMD_FORCE_INLINE float4 set1_float4(float x) { return float4(x, x, x, x); }
CPPSPMD_FORCE_INLINE float minf(float a, float b) { return a < b ? a : b; }
CPPSPMD_FORCE_INLINE float maxf(float a, float b) { return a > b ? a : b; }
CPPSPMD_FORCE_INLINE int mini(int a, int b) { return a < b ? a : b; }
CPPSPMD_FORCE_INLINE int maxi(int a, int b) { return a > b ? a : b; }
CPPSPMD_FORCE_INLINE int minu(int a, int b) { return (uint32_t)a < (uint32_t)b ? a : b; }
CPPSPMD_FORCE_INLINE int maxu(int a, int b) { return (uint32_t)a > (uint32_t)b ? a : b; }
#if CPPSPMD_USE_FMAF
CPPSPMD_FORCE_INLINE float my_fmaf(float a, float b, float c) { return std::fmaf(a, b, c); }
#else
CPPSPMD_FORCE_INLINE float my_fmaf(float a, float b, float c) { return (a * b) + c; }
#endif
// Work around for std::roundf(): halfway cases rounded away from zero, while with _mm_round_ss() it rounds halfway cases towards zero.
CPPSPMD_FORCE_INLINE float my_roundf(float a)
{
float f = std::roundf(a);
// Are we exactly halfway?
if (fabs(a - f) == .5f)
{
int q = (int)f;
if (q & 1)
{
// Fix rounding so it's like _mm_round_ss().
f += ((a < 0.0f) ? 1.0f : -1.0f);
if ((a < 0.0f) && (f == 0.0f))
{
f = -0.0f;
}
}
}
return f;
}
CPPSPMD_FORCE_INLINE float my_truncf(float a) { return std::truncf(a); }
CPPSPMD_FORCE_INLINE float4 setzero_float4() { return float4(0.0f, 0.0f, 0.0f, 0.0f); }
CPPSPMD_FORCE_INLINE float4 add_float4(const float4 &a, const float4 &b) { return float4(a.c[0] + b.c[0], a.c[1] + b.c[1], a.c[2] + b.c[2], a.c[3] + b.c[3]); }
CPPSPMD_FORCE_INLINE float4 sub_float4(const float4 &a, const float4 &b) { return float4(a.c[0] - b.c[0], a.c[1] - b.c[1], a.c[2] - b.c[2], a.c[3] - b.c[3]); }
CPPSPMD_FORCE_INLINE float4 mul_float4(const float4 &a, const float4 &b) { return float4(a.c[0] * b.c[0], a.c[1] * b.c[1], a.c[2] * b.c[2], a.c[3] * b.c[3]); }
CPPSPMD_FORCE_INLINE float4 div_float4(const float4 &a, const float4 &b) { return float4(a.c[0] / b.c[0], a.c[1] / b.c[1], a.c[2] / b.c[2], a.c[3] / b.c[3]); }
CPPSPMD_FORCE_INLINE float4 fma_float4(const float4 &a, const float4 &b, const float4 &c) { return float4(my_fmaf(a.c[0], b.c[0], c.c[0]), my_fmaf(a.c[1], b.c[1], c.c[1]), my_fmaf(a.c[2], b.c[2], c.c[2]), my_fmaf(a.c[3], b.c[3], c.c[3])); }
CPPSPMD_FORCE_INLINE float4 fms_float4(const float4 &a, const float4 &b, const float4 &c) { return float4(my_fmaf(a.c[0], b.c[0], -c.c[0]), my_fmaf(a.c[1], b.c[1], -c.c[1]), my_fmaf(a.c[2], b.c[2], -c.c[2]), my_fmaf(a.c[3], b.c[3], -c.c[3])); }
CPPSPMD_FORCE_INLINE float4 fnma_float4(const float4 &a, const float4 &b, const float4 &c) { return float4(c.c[0] - (a.c[0] * b.c[0]), c.c[1] - (a.c[1] * b.c[1]), c.c[2] - (a.c[2] * b.c[2]), c.c[3] - (a.c[3] * b.c[3])); }
CPPSPMD_FORCE_INLINE float4 fnms_float4(const float4 &a, const float4 &b, const float4 &c) { return float4(-c.c[0] - (a.c[0] * b.c[0]), -c.c[1] - (a.c[1] * b.c[1]), -c.c[2] - (a.c[2] * b.c[2]), -c.c[3] - (a.c[3] * b.c[3])); }
// Emulate what we do in SSE, so -0 does the same thing
CPPSPMD_FORCE_INLINE float4 neg_float4(const float4 &a) { return float4(0.0f - a.c[0], 0.0f - a.c[1], 0.0f - a.c[2], 0.0f - a.c[3]); }
CPPSPMD_FORCE_INLINE int4 convert_float4_to_int4(const float4 &a) { return int4((int)a.c[0], (int)a.c[1], (int)a.c[2], (int)a.c[3]); }
CPPSPMD_FORCE_INLINE float4 convert_int4_to_float4(const int4 &a) { return float4((float)a.c[0], (float)a.c[1], (float)a.c[2], (float)a.c[3]); }
CPPSPMD_FORCE_INLINE float4 floor_float4(const float4 &a) { return float4(floorf(a.c[0]), floorf(a.c[1]), floorf(a.c[2]), floorf(a.c[3])); }
CPPSPMD_FORCE_INLINE float4 ceil_float4(const float4 &a) { return float4(ceilf(a.c[0]), ceilf(a.c[1]), ceilf(a.c[2]), ceilf(a.c[3])); }
CPPSPMD_FORCE_INLINE float4 min_float4(const float4 &a, const float4 &b) { return float4(minf(a.c[0], b.c[0]), minf(a.c[1], b.c[1]), minf(a.c[2], b.c[2]), minf(a.c[3], b.c[3])); }
CPPSPMD_FORCE_INLINE float4 max_float4(const float4 &a, const float4 &b) { return float4(maxf(a.c[0], b.c[0]), maxf(a.c[1], b.c[1]), maxf(a.c[2], b.c[2]), maxf(a.c[3], b.c[3])); }
CPPSPMD_FORCE_INLINE float4 sqrt_float4(const float4 &a) { return float4(sqrtf(a.c[0]), sqrtf(a.c[1]), sqrtf(a.c[2]), sqrtf(a.c[3])); }
CPPSPMD_FORCE_INLINE float4 abs_float4(const float4 &a) { return float4(fabsf(a.c[0]), fabsf(a.c[1]), fabsf(a.c[2]), fabsf(a.c[3])); }
CPPSPMD_FORCE_INLINE float4 round_float4(const float4 &a) { return float4(my_roundf(a.c[0]), my_roundf(a.c[1]), my_roundf(a.c[2]), my_roundf(a.c[3])); }
CPPSPMD_FORCE_INLINE float4 truncate_float4(const float4 &a) { return float4(my_truncf(a.c[0]), my_truncf(a.c[1]), my_truncf(a.c[2]), my_truncf(a.c[3])); }
CPPSPMD_FORCE_INLINE int4 cmp_eq_float4(const float4 &a, const float4 &b) { return int4(-(a.c[0] == b.c[0]), -(a.c[1] == b.c[1]), -(a.c[2] == b.c[2]), -(a.c[3] == b.c[3])); }
CPPSPMD_FORCE_INLINE int4 cmp_lt_float4(const float4 &a, const float4 &b) { return int4(-(a.c[0] < b.c[0]), -(a.c[1] < b.c[1]), -(a.c[2] < b.c[2]), -(a.c[3] < b.c[3])); }
CPPSPMD_FORCE_INLINE int4 cmp_le_float4(const float4 &a, const float4 &b) { return int4(-(a.c[0] <= b.c[0]), -(a.c[1] <= b.c[1]), -(a.c[2] <= b.c[2]), -(a.c[3] <= b.c[3])); }
CPPSPMD_FORCE_INLINE int4 cmp_gt_float4(const float4 &a, const float4 &b) { return int4(-(a.c[0] > b.c[0]), -(a.c[1] > b.c[1]), -(a.c[2] > b.c[2]), -(a.c[3] > b.c[3])); }
CPPSPMD_FORCE_INLINE int4 cmp_ge_float4(const float4 &a, const float4 &b) { return int4(-(a.c[0] >= b.c[0]), -(a.c[1] >= b.c[1]), -(a.c[2] >= b.c[2]), -(a.c[3] >= b.c[3])); }
CPPSPMD_FORCE_INLINE int4 create_int4_rev(int w, int z, int y, int x) { return int4(x, y, z, w); }
CPPSPMD_FORCE_INLINE int4 create_int4(int x, int y, int z, int w) { return int4(x, y, z, w); }
CPPSPMD_FORCE_INLINE int4 set1_int4(int x) { return int4(x, x, x, x); }
CPPSPMD_FORCE_INLINE int4 setzero_int4() { return int4(0, 0, 0, 0); }
CPPSPMD_FORCE_INLINE int4 add_int4(const int4 &a, const int4 &b) { return int4(a.c[0] + b.c[0], a.c[1] + b.c[1], a.c[2] + b.c[2], a.c[3] + b.c[3]); }
CPPSPMD_FORCE_INLINE int4 sub_int4(const int4 &a, const int4 &b) { return int4(a.c[0] - b.c[0], a.c[1] - b.c[1], a.c[2] - b.c[2], a.c[3] - b.c[3]); }
CPPSPMD_FORCE_INLINE int4 mul_int4(const int4 &a, const int4 &b) { return int4(a.c[0] * b.c[0], a.c[1] * b.c[1], a.c[2] * b.c[2], a.c[3] * b.c[3]); }
CPPSPMD_FORCE_INLINE int4 mulhiu_int4(const int4 &a, const int4 &b)
{
return int4(
(int)(((uint64_t)((uint32_t)a.c[0]) * (uint64_t)((uint32_t)b.c[0])) >> 32U),
(int)(((uint64_t)((uint32_t)a.c[1]) * (uint64_t)((uint32_t)b.c[1])) >> 32U),
(int)(((uint64_t)((uint32_t)a.c[2]) * (uint64_t)((uint32_t)b.c[2])) >> 32U),
(int)(((uint64_t)((uint32_t)a.c[3]) * (uint64_t)((uint32_t)b.c[3])) >> 32U) );
}
CPPSPMD_FORCE_INLINE int4 div_int4(const int4 &a, const int4 &b) { return int4(b.c[0] ? (a.c[0] / b.c[0]) : INT_MIN, b.c[1] ? (a.c[1] / b.c[1]) : INT_MIN, b.c[2] ? (a.c[2] / b.c[2]) : INT_MIN, b.c[3] ? (a.c[3] / b.c[3]) : INT_MIN); }
CPPSPMD_FORCE_INLINE int4 div_int4(const int4 &a, int b) { return b ? int4(a.c[0] / b, a.c[1] / b, a.c[2] / b, a.c[3] / b) : int4(INT_MIN, INT_MIN, INT_MIN, INT_MIN); }
CPPSPMD_FORCE_INLINE int4 mod_int4(const int4 &a, const int4 &b) { return int4(b.c[0] ? (a.c[0] % b.c[0]) : 0, b.c[1] ? (a.c[1] % b.c[1]) : 0, b.c[2] ? (a.c[2] % b.c[2]) : 0, b.c[3] ? (a.c[3] % b.c[3]) : 0); }
CPPSPMD_FORCE_INLINE int4 mod_int4(const int4 &a, int b) { return b ? int4(a.c[0] % b, a.c[1] % b, a.c[2] % b, a.c[3] % b) : int4(0, 0, 0, 0); }
CPPSPMD_FORCE_INLINE int4 neg_int4(const int4 &a) { return int4(-a.c[0], -a.c[1], -a.c[2], -a.c[3]); }
CPPSPMD_FORCE_INLINE int4 inv_int4(const int4 &a) { return int4(~a.c[0], ~a.c[1], ~a.c[2], ~a.c[3]); }
CPPSPMD_FORCE_INLINE int4 and_int4(const int4 &a, const int4 &b) { return int4(a.c[0] & b.c[0], a.c[1] & b.c[1], a.c[2] & b.c[2], a.c[3] & b.c[3]); }
CPPSPMD_FORCE_INLINE int4 or_int4(const int4 &a, const int4 &b) { return int4(a.c[0] | b.c[0], a.c[1] | b.c[1], a.c[2] | b.c[2], a.c[3] | b.c[3]); }
CPPSPMD_FORCE_INLINE int4 xor_int4(const int4 &a, const int4 &b) { return int4(a.c[0] ^ b.c[0], a.c[1] ^ b.c[1], a.c[2] ^ b.c[2], a.c[3] ^ b.c[3]); }
CPPSPMD_FORCE_INLINE int4 andnot_int4(const int4 &a, const int4 &b) { return int4((~a.c[0]) & b.c[0], (~a.c[1]) & b.c[1], (~a.c[2]) & b.c[2], (~a.c[3]) & b.c[3]); }
// C's shifts have undefined behavior, but SSE's doesn't, so we need to emulate.
CPPSPMD_FORCE_INLINE int4 shift_left_int4(const int4 &a, const int4 &b)
{
return int4((b.c[0] > 31) ? 0 : (a.c[0] << b.c[0]), (b.c[1] > 31) ? 0 : (a.c[1] << b.c[1]), (b.c[2] > 31) ? 0 : (a.c[2] << b.c[2]), (b.c[3] > 31) ? 0 : (a.c[3] << b.c[3]));
}
CPPSPMD_FORCE_INLINE int4 shift_right_int4(const int4 &a, const int4 &b)
{
return int4(a.c[0] >> std::min(31, b.c[0]), a.c[1] >> std::min(31, b.c[1]), a.c[2] >> std::min(31, b.c[2]), a.c[3] >> std::min(31, b.c[3]));
}
CPPSPMD_FORCE_INLINE int4 unsigned_shift_right_int4(const int4 &a, const int4 &b)
{
return int4( (b.c[0] > 31) ? 0 : ((uint32_t)a.c[0]) >> b.c[0], (b.c[1] > 31) ? 0 : ((uint32_t)a.c[1]) >> b.c[1], (b.c[2] > 31) ? 0 : ((uint32_t)a.c[2]) >> b.c[2], (b.c[3] > 31) ? 0 : ((uint32_t)a.c[3]) >> b.c[3]);
}
CPPSPMD_FORCE_INLINE int4 shift_left_int4(const int4 &a, int b)
{
if (b > 31)
return int4(0, 0, 0, 0);
else
return int4(a.c[0] << b, a.c[1] << b, a.c[2] << b, a.c[3] << b);
}
CPPSPMD_FORCE_INLINE int4 shift_right_int4(const int4 &a, int b)
{
b = std::min(b, 31);
return int4(a.c[0] >> b, a.c[1] >> b, a.c[2] >> b, a.c[3] >> b);
}
CPPSPMD_FORCE_INLINE int4 unsigned_shift_right_int4(const int4 &a, int b)
{
if (b > 31)
return int4(0, 0, 0, 0);
else
return int4(((uint32_t)a.c[0]) >> b, ((uint32_t)a.c[1]) >> b, ((uint32_t)a.c[2]) >> b, ((uint32_t)a.c[3]) >> b);
}
CPPSPMD_FORCE_INLINE int4 cmp_eq_int4(const int4 &a, const int4 &b) { return int4(-(a.c[0] == b.c[0]), -(a.c[1] == b.c[1]), -(a.c[2] == b.c[2]), -(a.c[3] == b.c[3])); }
CPPSPMD_FORCE_INLINE int4 cmp_gt_int4(const int4 &a, const int4 &b) { return int4(-(a.c[0] > b.c[0]), -(a.c[1] > b.c[1]), -(a.c[2] > b.c[2]), -(a.c[3] > b.c[3])); }
CPPSPMD_FORCE_INLINE int movemask_int4(const int4 &a)
{
return ((a.c[0] >> 31) & 1) | (((a.c[1] >> 31) & 1) << 1)| (((a.c[2] >> 31) & 1) << 2)| (((a.c[3] >> 31) & 1) << 3);
}
CPPSPMD_FORCE_INLINE int4 min_int4(const int4 &a, const int4 &b) { return int4(mini(a.c[0], b.c[0]), mini(a.c[1], b.c[1]), mini(a.c[2], b.c[2]), mini(a.c[3], b.c[3])); }
CPPSPMD_FORCE_INLINE int4 max_int4(const int4 &a, const int4 &b) { return int4(maxi(a.c[0], b.c[0]), maxi(a.c[1], b.c[1]), maxi(a.c[2], b.c[2]), maxi(a.c[3], b.c[3])); }
CPPSPMD_FORCE_INLINE int4 min_uint4(const int4& a, const int4& b) { return int4(minu(a.c[0], b.c[0]), minu(a.c[1], b.c[1]), minu(a.c[2], b.c[2]), minu(a.c[3], b.c[3])); }
CPPSPMD_FORCE_INLINE int4 max_uint4(const int4& a, const int4& b) { return int4(maxu(a.c[0], b.c[0]), maxu(a.c[1], b.c[1]), maxu(a.c[2], b.c[2]), maxu(a.c[3], b.c[3])); }
CPPSPMD_FORCE_INLINE int4 abs_int4(const int4 &a) { return int4(::abs(a.c[0]), ::abs(a.c[1]), ::abs(a.c[2]), ::abs(a.c[3])); }
CPPSPMD_FORCE_INLINE uint32_t byteswap_uint32(uint32_t v) { uint32_t b0 = v & 0xFF; uint32_t b1 = (v >> 8U) & 0xFF; uint32_t b2 = (v >> 16U) & 0xFF; uint32_t b3 = (v >> 24U) & 0xFF; return b3 | (b2 << 8) | (b1 << 16) | (b0 << 24); }
CPPSPMD_FORCE_INLINE int4 byteswap_int4(const int4& a) { return int4(byteswap_uint32(a.c[0]), byteswap_uint32(a.c[1]), byteswap_uint32(a.c[2]), byteswap_uint32(a.c[3])); }
CPPSPMD_FORCE_INLINE int4 blendv_int4(const int4 &a, const int4 &b, const int4 &c)
{
const int m0 = c.c[0] >> 31, m1 = c.c[1] >> 31, m2 = c.c[2] >> 31, m3 = c.c[3] >> 31;
//return int4( (b.c[0] & c.c[0]) | (a.c[0] & (~c.c[0])), (b.c[1] & c.c[1]) | (a.c[1] & (~c.c[1])), (b.c[2] & c.c[2]) | (a.c[2] & (~c.c[2])), (b.c[3] & c.c[3]) | (a.c[3] & (~c.c[3])) );
return int4( (b.c[0] & m0) | (a.c[0] & (~m0)), (b.c[1] & m1) | (a.c[1] & (~m1)), (b.c[2] & m2) | (a.c[2] & (~m2)), (b.c[3] & m3) | (a.c[3] & (~m3)) );
}
CPPSPMD_FORCE_INLINE int4 blendv_epi8_int4(const int4 &a, const int4 &b, const int4 &c)
{
int4 r;
for (int i = 0; i < 16; i++)
{
uint8_t ab = ((const uint8_t *)&a)[i];
uint8_t bb = ((const uint8_t *)&b)[i];
uint8_t mask = (((const int8_t *)&c)[i]) >> 7;
((uint8_t *)&r)[i] = (bb & mask) | (ab & (~mask));
}
return r;
}
CPPSPMD_FORCE_INLINE int clamp32(int a, int lo, int hi) { return std::max(lo, std::min(a, hi)); }
CPPSPMD_FORCE_INLINE int saturate_i8_32(int a) { return std::max(-128, std::min(a, 127)); }
CPPSPMD_FORCE_INLINE int clamp_lo_u8_32(int a) { return std::max(a, 0); }
CPPSPMD_FORCE_INLINE int clamp_hi_u8_32(int a) { return std::min(a, 255); }
CPPSPMD_FORCE_INLINE int4 add_epu8(const int4& a, const int4& b) { int4 result; for (int i = 0; i < 16; i++) ((uint8_t*)&result)[i] = (uint8_t)(((const uint8_t*)&a)[i] + ((const uint8_t*)&b)[i]); return result; }
CPPSPMD_FORCE_INLINE int4 sub_epu8(const int4& a, const int4& b) { int4 result; for (int i = 0; i < 16; i++) ((uint8_t*)&result)[i] = (uint8_t)(((const uint8_t*)&a)[i] - ((const uint8_t*)&b)[i]); return result; }
CPPSPMD_FORCE_INLINE int4 adds_epu8(const int4& a, const int4& b) { int4 result; for (int i = 0; i < 16; i++) ((uint8_t*)&result)[i] = (uint8_t)clamp_hi_u8_32(((const uint8_t*)&a)[i] + ((const uint8_t*)&b)[i]); return result; }
CPPSPMD_FORCE_INLINE int4 subs_epu8(const int4& a, const int4& b) { int4 result; for (int i = 0; i < 16; i++) ((uint8_t*)&result)[i] = (uint8_t)clamp_lo_u8_32(((const uint8_t*)&a)[i] - ((const uint8_t*)&b)[i]); return result; }
CPPSPMD_FORCE_INLINE int4 avg_epu8(const int4& a, const int4& b) { int4 result; for (int i = 0; i < 16; i++) ((uint8_t*)&result)[i] = (uint8_t)((((const uint8_t*)&a)[i] + ((const uint8_t*)&b)[i] + 1) >> 1); return result; }
CPPSPMD_FORCE_INLINE int4 max_epu8(const int4& a, const int4& b) { int4 result; for (int i = 0; i < 16; i++) ((uint8_t*)&result)[i] = (uint8_t)std::max<uint8_t>(((const uint8_t*)&a)[i], ((const uint8_t*)&b)[i]); return result; }
CPPSPMD_FORCE_INLINE int4 min_epu8(const int4& a, const int4& b) { int4 result; for (int i = 0; i < 16; i++) ((uint8_t*)&result)[i] = (uint8_t)std::min<uint8_t>(((const uint8_t*)&a)[i], ((const uint8_t*)&b)[i]); return result; }
CPPSPMD_FORCE_INLINE int4 sad_epu8(const int4& a, const int4& b)
{
uint8_t vals[16];
for (int i = 0; i < 16; i++)
vals[i] = (uint8_t)std::abs((int)((const uint8_t*)&a)[i] - (int)((const uint8_t*)&b)[i]);
int4 result;
result.c[0] = (uint16_t)(vals[0] + vals[1] + vals[2] + vals[3] + vals[4] + vals[5] + vals[6] + vals[7]);
result.c[1] = 0;
result.c[2] = (uint16_t)(vals[8] + vals[9] + vals[10] + vals[11] + vals[12] + vals[13] + vals[14] + vals[15]);
result.c[3] = 0;
return result;
}
CPPSPMD_FORCE_INLINE int4 unpacklo_epi8(const int4& a, const int4& b)
{
int4 result;
uint8_t* pDst = (uint8_t*)&result;
const uint8_t* pA = (const uint8_t*)&a;
const uint8_t* pB = (const uint8_t*)&b;
for (int i = 0; i < 8; i++)
{
pDst[i * 2 + 0] = pA[i];
pDst[i * 2 + 1] = pB[i];
}
return result;
}
CPPSPMD_FORCE_INLINE int4 unpackhi_epi8(const int4& a, const int4& b)
{
int4 result;
uint8_t* pDst = (uint8_t*)&result;
const uint8_t* pA = (const uint8_t*)&a;
const uint8_t* pB = (const uint8_t*)&b;
for (int i = 0; i < 8; i++)
{
pDst[i * 2 + 0] = pA[8 + i];
pDst[i * 2 + 1] = pB[8 + i];
}
return result;
}
CPPSPMD_FORCE_INLINE int movemask_epi8(const int4& a)
{
const uint8_t* pA = (const uint8_t*)&a;
int mask = 0;
for (int i = 0; i < 16; i++)
mask |= ((pA[i] >> 7) << i);
return mask;
}
CPPSPMD_FORCE_INLINE int4 lane_shuffle_epi32_int4(const int4& a, int control)
{
assert(control >= 0 && control < 256);
int4 result;
result.c[0] = a.c[control & 3];
result.c[1] = a.c[(control >> 2) & 3];
result.c[2] = a.c[(control >> 4) & 3];
result.c[3] = a.c[(control >> 6) & 3];
return result;
}
CPPSPMD_FORCE_INLINE int4 lane_shufflelo_epi16_int4(const int4& a, int control)
{
assert(control >= 0 && control < 256);
int4 result;
((int16_t*)&result)[0] = ((const int16_t*)&a)[control & 3];
((int16_t*)&result)[1] = ((const int16_t*)&a)[(control >> 2) & 3];
((int16_t*)&result)[2] = ((const int16_t*)&a)[(control >> 4) & 3];
((int16_t*)&result)[3] = ((const int16_t*)&a)[(control >> 6) & 3];
result.c[2] = a.c[2];
result.c[3] = a.c[3];
return result;
}
CPPSPMD_FORCE_INLINE int4 lane_shufflehi_epi16_int4(const int4& a, int control)
{
assert(control >= 0 && control < 256);
int4 result;
result.c[0] = a.c[0];
result.c[1] = a.c[1];
((int16_t*)&result)[4] = ((const int16_t*)&a)[4 + (control & 3)];
((int16_t*)&result)[5] = ((const int16_t*)&a)[4 + ((control >> 2) & 3)];
((int16_t*)&result)[6] = ((const int16_t*)&a)[4 + ((control >> 4) & 3)];
((int16_t*)&result)[7] = ((const int16_t*)&a)[4 + ((control >> 6) & 3)];
return result;
}
CPPSPMD_FORCE_INLINE int4 lane_shift_left_bytes(const int4& a, int l)
{
if ((uint32_t)l > 16)
l = 16;
int4 result;
for (int i = 0; i < 16; i++)
{
uint8_t c = (i >= l) ? ((const uint8_t *)&a)[i - l] : 0;
((uint8_t *)&result)[i] = c;
}
return result;
}
CPPSPMD_FORCE_INLINE int4 lane_shift_right_bytes(const int4& a, int l)
{
if ((uint32_t)l > 16)
l = 16;
int4 result;
for (int i = 0; i < 16; i++)
{
uint8_t c = ((i + l) <= 15) ? ((const uint8_t *)&a)[i + l] : 0;
((uint8_t *)&result)[i] = c;
}
return result;
}
CPPSPMD_FORCE_INLINE int4 unpacklo_epi8_int4(const int4& a, const int4& b)
{
int4 result;
const uint8_t* pA = (const uint8_t*)&a;
const uint8_t* pB = (const uint8_t*)&b;
uint8_t* pR = (uint8_t*)&result;
for (int i = 0; i < 8; i++)
{
pR[i * 2 + 0] = pA[i];
pR[i * 2 + 1] = pB[i];
}
return result;
}
CPPSPMD_FORCE_INLINE int4 unpackhi_epi8_int4(const int4& a, const int4& b)
{
int4 result;
const uint8_t* pA = (const uint8_t*)&a;
const uint8_t* pB = (const uint8_t*)&b;
uint8_t* pR = (uint8_t*)&result;
for (int i = 0; i < 8; i++)
{
pR[i * 2 + 0] = pA[8 + i];
pR[i * 2 + 1] = pB[8 + i];
}
return result;
}
CPPSPMD_FORCE_INLINE int4 unpacklo_epi16_int4(const int4& a, const int4& b)
{
int4 result;
const uint16_t* pA = (const uint16_t*)&a;
const uint16_t* pB = (const uint16_t*)&b;
uint16_t* pR = (uint16_t*)&result;
for (int i = 0; i < 4; i++)
{
pR[i * 2 + 0] = pA[i];
pR[i * 2 + 1] = pB[i];
}
return result;
}
CPPSPMD_FORCE_INLINE int4 unpackhi_epi16_int4(const int4& a, const int4& b)
{
int4 result;
const uint16_t* pA = (const uint16_t*)&a;
const uint16_t* pB = (const uint16_t*)&b;
uint16_t* pR = (uint16_t*)&result;
for (int i = 0; i < 4; i++)
{
pR[i * 2 + 0] = pA[4 + i];
pR[i * 2 + 1] = pB[4 + i];
}
return result;
}
CPPSPMD_FORCE_INLINE int4 unpacklo_epi32_int4(const int4& a, const int4& b)
{
int4 result;
result.c[0] = a.c[0];
result.c[1] = b.c[0];
result.c[2] = a.c[1];
result.c[3] = b.c[1];
return result;
}
CPPSPMD_FORCE_INLINE int4 unpackhi_epi32_int4(const int4& a, const int4& b)
{
int4 result;
result.c[0] = a.c[2];
result.c[1] = b.c[2];
result.c[2] = a.c[3];
result.c[3] = b.c[3];
return result;
}
CPPSPMD_FORCE_INLINE int4 unpacklo_epi64_int4(const int4& a, const int4& b)
{
int4 result;
result.c[0] = a.c[0];
result.c[1] = a.c[1];
result.c[2] = b.c[0];
result.c[3] = b.c[1];
return result;
}
CPPSPMD_FORCE_INLINE int4 unpackhi_epi64_int4(const int4& a, const int4& b)
{
int4 result;
result.c[0] = a.c[2];
result.c[1] = a.c[3];
result.c[2] = b.c[2];
result.c[3] = b.c[3];
return result;
}
CPPSPMD_FORCE_INLINE int4 add_epi8(const int4& a, const int4& b) { int4 result; for (int i = 0; i < 16; i++) ((int8_t*)&result)[i] = (int8_t)(((const int8_t*)&a)[i] + ((const int8_t*)&b)[i]); return result; }
CPPSPMD_FORCE_INLINE int4 sub_epi8(const int4& a, const int4& b) { int4 result; for (int i = 0; i < 16; i++) ((int8_t*)&result)[i] = (int8_t)(((const int8_t*)&a)[i] - ((const int8_t*)&b)[i]); return result; }
CPPSPMD_FORCE_INLINE int4 adds_epi8(const int4& a, const int4& b) { int4 result; for (int i = 0; i < 16; i++) ((int8_t*)&result)[i] = (int8_t)saturate_i8_32(((const int8_t*)&a)[i] + ((const int8_t*)&b)[i]); return result; }
CPPSPMD_FORCE_INLINE int4 subs_epi8(const int4& a, const int4& b) { int4 result; for (int i = 0; i < 16; i++) ((int8_t*)&result)[i] = (int8_t)saturate_i8_32(((const int8_t*)&a)[i] - ((const int8_t*)&b)[i]); return result; }
CPPSPMD_FORCE_INLINE int4 max_epi8(const int4& a, const int4& b) { int4 result; for (int i = 0; i < 16; i++) ((int8_t*)&result)[i] = (int8_t)std::max<int8_t>(((const int8_t*)&a)[i], ((const int8_t*)&b)[i]); return result; }
CPPSPMD_FORCE_INLINE int4 min_epi8(const int4& a, const int4& b) { int4 result; for (int i = 0; i < 16; i++) ((int8_t*)&result)[i] = (int8_t)std::min<int8_t>(((const int8_t*)&a)[i], ((const int8_t*)&b)[i]); return result; }
CPPSPMD_FORCE_INLINE int4 cmpeq_epi8(const int4& a, const int4& b) { int4 result; for (int i = 0; i < 16; i++) ((uint8_t*)&result)[i] = (((const int8_t*)&a)[i] == ((const int8_t*)&b)[i]) ? 0xFF : 00; return result; }
CPPSPMD_FORCE_INLINE int4 cmplt_epi8(const int4& a, const int4& b) { int4 result; for (int i = 0; i < 16; i++) ((uint8_t*)&result)[i] = (((const int8_t*)&a)[i] < ((const int8_t*)&b)[i]) ? 0xFF : 00; return result; }
CPPSPMD_FORCE_INLINE int4 cmpgt_epi8(const int4& a, const int4& b) { int4 result; for (int i = 0; i < 16; i++) ((uint8_t*)&result)[i] = (((const int8_t*)&a)[i] > ((const int8_t*)&b)[i]) ? 0xFF : 00; return result; }
CPPSPMD_FORCE_INLINE float4 blend_float4(const float4 &a, const float4 &b, const float4 &c) { return cast_int4_to_float4(blendv_int4(cast_float4_to_int4(a), cast_float4_to_int4(b), cast_float4_to_int4(c))); }
CPPSPMD_FORCE_INLINE float4 blend_float4(const float4 &a, const float4 &b, const int4 &c) { return cast_int4_to_float4(blendv_int4(cast_float4_to_int4(a), cast_float4_to_int4(b), c)); }
CPPSPMD_FORCE_INLINE void store_float4(float *pDst, const float4 &v, const uint32_t stride = 1) { pDst[0] = v.c[0]; pDst[stride] = v.c[1]; pDst[stride*2] = v.c[2]; pDst[stride*3] = v.c[3]; }
CPPSPMD_FORCE_INLINE float4 load_float4(const float *pSrc, const uint32_t stride = 1) { return float4(pSrc[0], pSrc[stride], pSrc[stride*2], pSrc[stride*3]); }
CPPSPMD_FORCE_INLINE void store_int4(int *pDst, const int4 &v, const uint32_t stride = 1) { pDst[0] = v.c[0]; pDst[stride] = v.c[1]; pDst[stride*2] = v.c[2]; pDst[stride*3] = v.c[3]; }
CPPSPMD_FORCE_INLINE int4 load_int4(const int *pSrc, const uint32_t stride = 1) { return int4(pSrc[0], pSrc[stride], pSrc[stride*2], pSrc[stride*3]); }
CPPSPMD_FORCE_INLINE void store_mask_float4(float *pDst, const float4 &v, const int4 &m, const uint32_t stride = 1) { if (m.c[0]) pDst[0] = v.c[0]; if (m.c[1]) pDst[stride] = v.c[1]; if (m.c[2]) pDst[stride*2] = v.c[2]; if (m.c[3]) pDst[stride*3] = v.c[3]; }
CPPSPMD_FORCE_INLINE float4 load_mask_float4(const float *pSrc, const int4 &m, const uint32_t stride = 1) { float4 result(set1_float4(0.0f)); if (m.c[0]) result.c[0] = pSrc[0]; if (m.c[1]) result.c[1] = pSrc[stride]; if (m.c[2]) result.c[2] = pSrc[stride*2]; if (m.c[3]) result.c[3] = pSrc[stride*3]; return result; }
CPPSPMD_FORCE_INLINE void store_mask_int4(int *pDst, const int4 &v, const int4 &m, const uint32_t stride = 1) { if (m.c[0]) pDst[0] = v.c[0]; if (m.c[1]) pDst[stride] = v.c[1]; if (m.c[2]) pDst[stride*2] = v.c[2]; if (m.c[3]) pDst[stride*3] = v.c[3]; }
CPPSPMD_FORCE_INLINE int4 load_mask_int4(const int *pSrc, const int4 &m, const uint32_t stride = 1) { int4 result(set1_int4(0)); if (m.c[0]) result.c[0] = pSrc[0]; if (m.c[1]) result.c[1] = pSrc[stride]; if (m.c[2]) result.c[2] = pSrc[stride*2]; if (m.c[3]) result.c[3] = pSrc[stride*3]; return result; }
CPPSPMD_FORCE_INLINE void scatter_mask_float4(float *pDst, const float4 &v, const int4 &ofs, const int4 &m) { if (m.c[0]) pDst[ofs.c[0]] = v.c[0]; if (m.c[1]) pDst[ofs.c[1]] = v.c[1]; if (m.c[2]) pDst[ofs.c[2]] = v.c[2]; if (m.c[3]) pDst[ofs.c[3]] = v.c[3]; }
CPPSPMD_FORCE_INLINE float4 gather_mask_float4(const float *pSrc, const int4 &ofs, const int4 &m) { float4 result(set1_float4(0.0f)); if (m.c[0]) result.c[0] = pSrc[ofs.c[0]]; if (m.c[1]) result.c[1] = pSrc[ofs.c[1]]; if (m.c[2]) result.c[2] = pSrc[ofs.c[2]]; if (m.c[3]) result.c[3] = pSrc[ofs.c[3]]; return result; }
CPPSPMD_FORCE_INLINE void scatter_mask_int4(int *pDst, const int4 &v, const int4 &ofs, const int4 &m) { if (m.c[0]) pDst[ofs.c[0]] = v.c[0]; if (m.c[1]) pDst[ofs.c[1]] = v.c[1]; if (m.c[2]) pDst[ofs.c[2]] = v.c[2]; if (m.c[3]) pDst[ofs.c[3]] = v.c[3]; }
CPPSPMD_FORCE_INLINE int4 gather_mask_int4(const int *pSrc, const int4 &ofs, const int4 &m) { int4 result(set1_int4(0)); if (m.c[0]) result.c[0] = pSrc[ofs.c[0]]; if (m.c[1]) result.c[1] = pSrc[ofs.c[1]]; if (m.c[2]) result.c[2] = pSrc[ofs.c[2]]; if (m.c[3]) result.c[3] = pSrc[ofs.c[3]]; return result; }
CPPSPMD_FORCE_INLINE void scatter_float4(float *pDst, const float4 &v, const int4 &ofs) { pDst[ofs.c[0]] = v.c[0]; pDst[ofs.c[1]] = v.c[1]; pDst[ofs.c[2]] = v.c[2]; pDst[ofs.c[3]] = v.c[3]; }
CPPSPMD_FORCE_INLINE float4 gather_float4(const float *pSrc, const int4 &ofs) { float4 result(set1_float4(0.0f)); result.c[0] = pSrc[ofs.c[0]]; result.c[1] = pSrc[ofs.c[1]]; result.c[2] = pSrc[ofs.c[2]]; result.c[3] = pSrc[ofs.c[3]]; return result; }
CPPSPMD_FORCE_INLINE void scatter_int4(int *pDst, const int4 &v, const int4 &ofs) { pDst[ofs.c[0]] = v.c[0]; pDst[ofs.c[1]] = v.c[1]; pDst[ofs.c[2]] = v.c[2]; pDst[ofs.c[3]] = v.c[3]; }
CPPSPMD_FORCE_INLINE int4 gather_int4(const int *pSrc, const int4 &ofs) { int4 result(set1_int4(0)); result.c[0] = pSrc[ofs.c[0]]; result.c[1] = pSrc[ofs.c[1]]; result.c[2] = pSrc[ofs.c[2]]; result.c[3] = pSrc[ofs.c[3]]; return result; }
CPPSPMD_FORCE_INLINE int4 shuffle_epi8_int4(const int4 &a, const int4 &b)
{
int4 result;
for (int i = 0; i < 16; i++)
{
int v = ((const uint8_t*)&b)[i];
((uint8_t*)&result)[i] = (v & 128) ? 0 : ((const uint8_t*)&a)[v & 15];
}
return result;
}
CPPSPMD_FORCE_INLINE int4 add_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int16_t *pB = (const int16_t *)&b;
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)(pA[i] + pB[i]);
return r;
}
CPPSPMD_FORCE_INLINE int4 adds_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int16_t *pB = (const int16_t *)&b;
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)clamp32(pA[i] + pB[i], INT16_MIN, INT16_MAX);
return r;
}
CPPSPMD_FORCE_INLINE int4 adds_epu16_int4(const int4& a, const int4& b)
{
int4 r;
const uint16_t *pA = (const uint16_t *)&a;
const uint16_t *pB = (const uint16_t *)&b;
uint16_t *pR = (uint16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (uint16_t)std::min<uint32_t>(pA[i] + pB[i], UINT16_MAX);
return r;
}
CPPSPMD_FORCE_INLINE int4 sub_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int16_t *pB = (const int16_t *)&b;
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)(pA[i] - pB[i]);
return r;
}
CPPSPMD_FORCE_INLINE int4 subs_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int16_t *pB = (const int16_t *)&b;
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)clamp32((int)pA[i] - (int)pB[i], INT16_MIN, INT16_MAX);
return r;
}
CPPSPMD_FORCE_INLINE int4 subs_epu16_int4(const int4& a, const int4& b)
{
int4 r;
const uint16_t *pA = (const uint16_t *)&a;
const uint16_t *pB = (const uint16_t *)&b;
uint16_t *pR = (uint16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (uint16_t)std::max<int>((int)pA[i] - (int)pB[i], 0);
return r;
}
CPPSPMD_FORCE_INLINE int4 avg_epu16_int4(const int4& a, const int4& b)
{
int4 r;
const uint16_t *pA = (const uint16_t *)&a;
const uint16_t *pB = (const uint16_t *)&b;
uint16_t *pR = (uint16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (uint16_t)((pA[i] + pB[i] + 1) >> 1U);
return r;
}
CPPSPMD_FORCE_INLINE int4 mullo_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int16_t *pB = (const int16_t *)&b;
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)((int)pA[i] * (int)pB[i]);
return r;
}
CPPSPMD_FORCE_INLINE int4 mulhi_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int16_t *pB = (const int16_t *)&b;
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)(((int)pA[i] * (int)pB[i]) >> 16);
return r;
}
CPPSPMD_FORCE_INLINE int4 mulhi_epu16_int4(const int4& a, const int4& b)
{
int4 r;
const uint16_t *pA = (const uint16_t *)&a;
const uint16_t *pB = (const uint16_t *)&b;
uint16_t *pR = (uint16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (uint16_t)(((uint32_t)pA[i] * (uint32_t)pB[i]) >> 16U);
return r;
}
CPPSPMD_FORCE_INLINE int4 min_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int16_t *pB = (const int16_t *)&b;
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)std::min(pA[i], pB[i]);
return r;
}
CPPSPMD_FORCE_INLINE int4 max_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int16_t *pB = (const int16_t *)&b;
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)std::max(pA[i], pB[i]);
return r;
}
CPPSPMD_FORCE_INLINE int4 madd_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int16_t *pB = (const int16_t *)&b;
int32_t *pR = (int32_t *)&r;
for (int i = 0; i < 4; i++)
pR[i] = pA[i * 2] * pB[i * 2] + pA[i * 2 + 1] * pB[i * 2 + 1];
return r;
}
CPPSPMD_FORCE_INLINE int4 cmpeq_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int16_t *pB = (const int16_t *)&b;
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)((pA[i] == pB[i]) ? 0xFFFF : 0);
return r;
}
CPPSPMD_FORCE_INLINE int4 cmpgt_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int16_t *pB = (const int16_t *)&b;
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)((pA[i] > pB[i]) ? 0xFFFF : 0);
return r;
}
CPPSPMD_FORCE_INLINE int4 cmplt_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int16_t *pB = (const int16_t *)&b;
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)((pA[i] < pB[i]) ? 0xFFFF : 0);
return r;
}
CPPSPMD_FORCE_INLINE int4 packs_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int16_t *pB = (const int16_t *)&b;
int8_t *pR = (int8_t *)&r;
for (int i = 0; i < 8; i++)
{
pR[i] = (int8_t)clamp32(pA[i], INT8_MIN, INT8_MAX);
pR[i + 8] = (int8_t)clamp32(pB[i], INT8_MIN, INT8_MAX);
}
return r;
}
CPPSPMD_FORCE_INLINE int4 packus_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int16_t *pB = (const int16_t *)&b;
uint8_t *pR = (uint8_t *)&r;
for (int i = 0; i < 8; i++)
{
pR[i] = (uint8_t)clamp32(pA[i], 0, 255);
pR[i + 8] = (uint8_t)clamp32(pB[i], 0, 255);
}
return r;
}
CPPSPMD_FORCE_INLINE int4 sll_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int64_t *pB = (const int64_t *)&b;
uint32_t c = pB[0];
c = std::min(c, 16U);
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)(pA[i] << c);
return r;
}
CPPSPMD_FORCE_INLINE int4 sra_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int64_t *pB = (const int64_t *)&b;
uint32_t c = pB[0];
c = std::min(c, 16U);
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)(pA[i] >> c);
return r;
}
CPPSPMD_FORCE_INLINE int4 srl_epi16_int4(const int4& a, const int4& b)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
const int64_t *pB = (const int64_t *)&b;
uint32_t c = pB[0];
c = std::min(c, 16U);
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)(((uint16_t)pA[i]) >> c);
return r;
}
CPPSPMD_FORCE_INLINE int4 slli_epi16_int4(const int4& a, uint32_t c)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
c = std::min(c, 16U);
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)(pA[i] << c);
return r;
}
CPPSPMD_FORCE_INLINE int4 srai_epi16_int4(const int4& a, uint32_t c)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
c = std::min(c, 16U);
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)(pA[i] >> c);
return r;
}
CPPSPMD_FORCE_INLINE int4 srli_epi16_int4(const int4& a, uint32_t c)
{
int4 r;
const int16_t *pA = (const int16_t *)&a;
c = std::min(c, 16U);
int16_t *pR = (int16_t *)&r;
for (int i = 0; i < 8; i++)
pR[i] = (int16_t)(((uint16_t)pA[i]) >> c);
return r;
}
CPPSPMD_FORCE_INLINE int4 mul_epu32_int4(const int4& a, const int4& b)
{
int4 result;
uint64_t *pR = (uint64_t *)&result;
pR[0] = (uint64_t)a.c[0] * (uint32_t)b.c[0];
pR[1] = (uint64_t)a.c[1] * (uint32_t)b.c[1];
return result;
}
const uint32_t ALL_ON_MOVEMASK = 0xF;
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
{
int4 m_mask;
exec_mask() = default;
CPPSPMD_FORCE_INLINE exec_mask(const exec_mask& b) { m_mask.c[0] = b.m_mask.c[0]; m_mask.c[1] = b.m_mask.c[1]; m_mask.c[2] = b.m_mask.c[2]; m_mask.c[3] = b.m_mask.c[3]; }
CPPSPMD_FORCE_INLINE explicit exec_mask(const vbool& b);
CPPSPMD_FORCE_INLINE explicit exec_mask(const int4& mask) : m_mask(mask) { }
CPPSPMD_FORCE_INLINE void enable_lane(uint32_t lane) { memset(&m_mask, 0, sizeof(m_mask)); m_mask.c[lane] = UINT32_MAX; }
static CPPSPMD_FORCE_INLINE exec_mask all_on() { return exec_mask{ set1_int4(UINT32_MAX) }; }
static CPPSPMD_FORCE_INLINE exec_mask all_off() { return exec_mask{ set1_int4(0) }; }
CPPSPMD_FORCE_INLINE uint32_t get_movemask() const { return movemask_int4(m_mask); }
};
friend CPPSPMD_FORCE_INLINE bool all(const exec_mask& e);
friend CPPSPMD_FORCE_INLINE bool any(const exec_mask& e);
// true if all lanes active
CPPSPMD_FORCE_INLINE bool spmd_all() const { return all(m_exec); }
// true if any lanes active
CPPSPMD_FORCE_INLINE bool spmd_any() const { return any(m_exec); }
// true if no lanes active
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; }
// false if cond is true for any active lanes
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
{
int4 m_value;
vbool() = default;
CPPSPMD_FORCE_INLINE vbool(bool value) : m_value(set1_int4(value ? UINT32_MAX : 0)) { }
CPPSPMD_FORCE_INLINE vbool(const vbool& b) { m_value.c[0] = b.m_value.c[0]; m_value.c[1] = b.m_value.c[1]; m_value.c[2] = b.m_value.c[2]; m_value.c[3] = b.m_value.c[3]; }
CPPSPMD_FORCE_INLINE explicit vbool(const int4& value) : m_value(value) { assert((m_value.c[0] == 0) || ((uint32_t)m_value.c[0] == UINT32_MAX)); assert((m_value.c[1] == 0) || ((uint32_t)m_value.c[1] == UINT32_MAX)); assert((m_value.c[2] == 0) || ((uint32_t)m_value.c[2] == UINT32_MAX)); assert((m_value.c[3] == 0) || ((uint32_t)m_value.c[3] == UINT32_MAX)); }
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 = blendv_int4(dst.m_value, src.m_value, 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
{
float4 m_value;
vfloat() = default;
CPPSPMD_FORCE_INLINE vfloat(const vfloat& b) { m_value.c[0] = b.m_value.c[0]; m_value.c[1] = b.m_value.c[1]; m_value.c[2] = b.m_value.c[2]; m_value.c[3] = b.m_value.c[3]; }
CPPSPMD_FORCE_INLINE explicit vfloat(const float4& v) : m_value(v) { }