forked from ispc/ispc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib.ispc
5903 lines (4983 loc) · 255 KB
/
stdlib.ispc
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
// -*- mode: c++ -*-
/*
Copyright (c) 2010-2022, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @file stdlib.ispc
@brief Portion of the ispc standard library implementation that's in
ispc code
*/
#if (ISPC_MASK_BITS == 1)
#define IntMaskType bool
#define UIntMaskType bool
#elif (ISPC_MASK_BITS == 8)
#define IntMaskType int8
#define UIntMaskType unsigned int8
#elif (ISPC_MASK_BITS == 16)
#define IntMaskType int16
#define UIntMaskType unsigned int16
#elif (ISPC_MASK_BITS == 32)
#define IntMaskType int32
#define UIntMaskType unsigned int32
#elif (ISPC_MASK_BITS == 64)
#define IntMaskType int64
#define UIntMaskType unsigned int64
#else
#error Unknown value of ISPC_MASK_BITS
#endif
typedef uniform int8 *uniform opaque_ptr_t;
///////////////////////////////////////////////////////////////////////////
/* Limits of integral types. */
#ifndef INT8_MAX
#define INT8_MAX (127)
#endif
#ifndef INT16_MAX
#define INT16_MAX (32767)
#endif
#ifndef INT32_MAX
#define INT32_MAX (2147483647)
#endif
#ifndef INT64_MAX
#define INT64_MAX (9223372036854775807)
#endif
#ifndef UINT8_MAX
#define UINT8_MAX (255)
#endif
#ifndef UINT16_MAX
#define UINT16_MAX (65535)
#endif
#ifndef UINT32_MAX
#define UINT32_MAX (4294967295)
#endif
#ifndef UINT64_MAX
#define UINT64_MAX (18446744073709551615)
#endif
#ifndef INT8_MIN
#define INT8_MIN (-INT8_MAX - 1)
#endif
#ifndef INT16_MIN
#define INT16_MIN (-INT16_MAX - 1)
#endif
#ifndef INT32_MIN
#define INT32_MIN (-INT32_MAX - 1)
#endif
#ifndef INT64_MIN
#define INT64_MIN (-INT64_MAX - 1)
#endif
///////////////////////////////////////////////////////////////////////////
// GEN target specific
// 4 bytes by default
#ifndef PREFETCH_DATASIZE_DEFAULT
#define PREFETCH_DATASIZE_DEFAULT 4
#endif
///////////////////////////////////////////////////////////////////////////
// Low level primitives
__declspec(safe, cost0) static inline float16 float16bits(unsigned int16 a) { return __halfbits_varying_int16(a); }
__declspec(safe, cost0) static inline uniform float16 float16bits(uniform unsigned int16 a) {
return __halfbits_uniform_int16(a);
}
__declspec(safe, cost0) static inline float16 float16bits(int16 a) { return __halfbits_varying_int16(a); }
__declspec(safe, cost0) static inline uniform float16 float16bits(uniform int16 a) {
return __halfbits_uniform_int16(a);
}
__declspec(safe, cost0) static inline float floatbits(unsigned int a) { return __floatbits_varying_int32(a); }
__declspec(safe, cost0) static inline uniform float floatbits(uniform unsigned int a) {
return __floatbits_uniform_int32(a);
}
__declspec(safe, cost0) static inline float floatbits(int a) { return __floatbits_varying_int32(a); }
__declspec(safe, cost0) static inline uniform float floatbits(uniform int a) { return __floatbits_uniform_int32(a); }
__declspec(safe, cost0) static inline double doublebits(unsigned int64 a) { return __doublebits_varying_int64(a); }
__declspec(safe, cost0) static inline uniform double doublebits(uniform unsigned int64 a) {
return __doublebits_uniform_int64(a);
}
__declspec(safe, cost0) static inline unsigned int16 intbits(float16 a) { return __intbits_varying_half(a); }
__declspec(safe, cost0) static inline uniform unsigned int16 intbits(uniform float16 a) {
return __intbits_uniform_half(a);
}
__declspec(safe, cost0) static inline unsigned int intbits(float a) { return __intbits_varying_float(a); }
__declspec(safe, cost0) static inline uniform unsigned int intbits(uniform float a) {
return __intbits_uniform_float(a);
}
__declspec(safe, cost0) static inline unsigned int64 intbits(double d) { return __intbits_varying_double(d); }
__declspec(safe, cost0) static inline uniform unsigned int64 intbits(uniform double d) {
return __intbits_uniform_double(d);
}
__declspec(safe) static inline float broadcast(float v, uniform int i) { return __broadcast_float(v, i); }
__declspec(safe) static inline int8 broadcast(int8 v, uniform int i) { return __broadcast_i8(v, i); }
__declspec(safe) static inline int16 broadcast(int16 v, uniform int i) { return __broadcast_i16(v, i); }
__declspec(safe) static inline float16 broadcast(float16 v, uniform int i) { return __broadcast_half(v, i); }
__declspec(safe) static inline int32 broadcast(int32 v, uniform int i) { return __broadcast_i32(v, i); }
__declspec(safe) static inline double broadcast(double v, uniform int i) { return __broadcast_double(v, i); }
__declspec(safe) static inline int64 broadcast(int64 v, uniform int i) { return __broadcast_i64(v, i); }
__declspec(safe) static inline float rotate(float v, uniform int i) { return __rotate_float(v, i); }
__declspec(safe) static inline int8 rotate(int8 v, uniform int i) { return __rotate_i8(v, i); }
__declspec(safe) static inline int16 rotate(int16 v, uniform int i) { return __rotate_i16(v, i); }
__declspec(safe) static inline float16 rotate(float16 v, uniform int i) { return __rotate_half(v, i); }
__declspec(safe) static inline int32 rotate(int32 v, uniform int i) { return __rotate_i32(v, i); }
__declspec(safe) static inline double rotate(double v, uniform int i) { return __rotate_double(v, i); }
__declspec(safe) static inline int64 rotate(int64 v, uniform int i) { return __rotate_i64(v, i); }
__declspec(safe) static inline float shift(float v, uniform int i) {
varying float result;
unmasked { result = __shift_float(v, i); }
return result;
}
__declspec(safe) static inline int8 shift(int8 v, uniform int i) {
varying int8 result;
unmasked { result = __shift_i8(v, i); }
return result;
}
__declspec(safe) static inline int16 shift(int16 v, uniform int i) {
varying int16 result;
unmasked { result = __shift_i16(v, i); }
return result;
}
__declspec(safe) static inline float16 shift(float16 v, uniform int i) {
varying int16 result;
unmasked { result = __shift_half(v, i); }
return result;
}
__declspec(safe) static inline int32 shift(int32 v, uniform int i) {
varying int32 result;
unmasked { result = __shift_i32(v, i); }
return result;
}
__declspec(safe) static inline double shift(double v, uniform int i) {
varying double result;
unmasked { result = __shift_double(v, i); }
return result;
}
__declspec(safe) static inline int64 shift(int64 v, uniform int i) {
varying int64 result;
unmasked { result = __shift_i64(v, i); }
return result;
}
__declspec(safe) static inline float shuffle(float v, int i) { return __shuffle_float(v, i); }
__declspec(safe) static inline int8 shuffle(int8 v, int i) { return __shuffle_i8(v, i); }
__declspec(safe) static inline int16 shuffle(int16 v, int i) { return __shuffle_i16(v, i); }
__declspec(safe) static inline float16 shuffle(float16 v, int i) { return __shuffle_half(v, i); }
__declspec(safe) static inline int32 shuffle(int32 v, int i) { return __shuffle_i32(v, i); }
__declspec(safe) static inline double shuffle(double v, int i) { return __shuffle_double(v, i); }
__declspec(safe) static inline int64 shuffle(int64 v, int i) { return __shuffle_i64(v, i); }
__declspec(safe) static inline float shuffle(float v0, float v1, int i) { return __shuffle2_float(v0, v1, i); }
__declspec(safe) static inline int8 shuffle(int8 v0, int8 v1, int i) { return __shuffle2_i8(v0, v1, i); }
__declspec(safe) static inline int16 shuffle(int16 v0, int16 v1, int i) { return __shuffle2_i16(v0, v1, i); }
__declspec(safe) static inline float16 shuffle(float16 v0, float16 v1, int i) { return __shuffle2_half(v0, v1, i); }
__declspec(safe) static inline int32 shuffle(int32 v0, int32 v1, int i) { return __shuffle2_i32(v0, v1, i); }
__declspec(safe) static inline double shuffle(double v0, double v1, int i) { return __shuffle2_double(v0, v1, i); }
__declspec(safe) static inline int64 shuffle(int64 v0, int64 v1, int i) { return __shuffle2_i64(v0, v1, i); }
// x[i]
__declspec(safe, cost1) static inline uniform float extract(float x, uniform int i) {
return floatbits(__extract_int32((int)intbits(x), i));
}
__declspec(safe, cost1) static inline uniform bool extract(bool x, uniform int i) { return __extract_bool(x, i); }
__declspec(safe, cost1) static inline uniform int8 extract(int8 x, uniform int i) { return __extract_int8(x, i); }
__declspec(safe, cost1) static inline uniform unsigned int8 extract(unsigned int8 x, uniform int i) {
return __extract_int8(x, (uniform unsigned int)i);
}
__declspec(safe, cost1) static inline uniform int16 extract(int16 x, uniform int i) { return __extract_int16(x, i); }
__declspec(safe, cost1) static inline uniform unsigned int16 extract(unsigned int16 x, uniform int i) {
return __extract_int16(x, (uniform unsigned int)i);
}
__declspec(safe, cost1) static inline uniform float16 extract(float16 x, uniform int i) {
return float16bits(__extract_int16((int16)intbits(x), i));
}
__declspec(safe, cost1) static inline uniform int32 extract(int32 x, uniform int i) { return __extract_int32(x, i); }
__declspec(safe, cost1) static inline uniform unsigned int32 extract(unsigned int32 x, uniform int i) {
return __extract_int32(x, (uniform unsigned int)i);
}
__declspec(safe, cost1) static inline uniform double extract(double x, uniform int i) {
return doublebits(__extract_int64((int64)intbits(x), i));
}
__declspec(safe, cost1) static inline uniform int64 extract(int64 x, uniform int i) { return __extract_int64(x, i); }
__declspec(safe, cost1) static inline uniform unsigned int64 extract(unsigned int64 x, uniform int i) {
return __extract_int64(x, (uniform unsigned int)i);
}
// x[i] = v
__declspec(safe, cost1) static inline float insert(float x, uniform int i, uniform float v) {
return floatbits(__insert_int32((int)intbits(x), i, (uniform int)intbits(v)));
}
__declspec(safe, cost1) static inline bool insert(bool x, uniform int i, uniform bool v) {
return __insert_bool(x, i, v);
}
__declspec(safe, cost1) static inline int8 insert(int8 x, uniform int i, uniform int8 v) {
return __insert_int8(x, i, v);
}
__declspec(safe, cost1) static inline unsigned int8 insert(unsigned int8 x, uniform int i, uniform unsigned int8 v) {
return __insert_int8(x, (uniform unsigned int)i, v);
}
__declspec(safe, cost1) static inline float16 insert(float16 x, uniform int i, uniform float16 v) {
return float16bits(__insert_int16((int16)intbits(x), i, (uniform int16)intbits(v)));
}
__declspec(safe, cost1) static inline int16 insert(int16 x, uniform int i, uniform int16 v) {
return __insert_int16(x, i, v);
}
__declspec(safe, cost1) static inline unsigned int16 insert(unsigned int16 x, uniform int i, uniform unsigned int16 v) {
return __insert_int16(x, (uniform unsigned int)i, v);
}
__declspec(safe, cost1) static inline int32 insert(int32 x, uniform int i, uniform int32 v) {
return __insert_int32(x, i, v);
}
__declspec(safe, cost1) static inline unsigned int32 insert(unsigned int32 x, uniform int i, uniform unsigned int32 v) {
return __insert_int32(x, (uniform unsigned int)i, v);
}
__declspec(safe, cost1) static inline double insert(double x, uniform int i, uniform double v) {
return doublebits(__insert_int64((int64)intbits(x), i, (uniform int64)intbits(v)));
}
__declspec(safe, cost1) static inline int64 insert(int64 x, uniform int i, uniform int64 v) {
return __insert_int64(x, i, v);
}
__declspec(safe, cost1) static inline unsigned int64 insert(unsigned int64 x, uniform int i, uniform unsigned int64 v) {
return __insert_int64(x, (uniform unsigned int)i, v);
}
__declspec(safe, cost1) static inline uniform int32 sign_extend(uniform bool v) { return __sext_uniform_bool(v); }
__declspec(safe, cost1) static inline int32 sign_extend(bool v) { return __sext_varying_bool(v); }
__declspec(safe) static inline uniform bool any(bool v) {
// We only care about whether "any" is true for the active program instances,
// so we have to make v with the current program mask.
#if (ISPC_MASK_BITS == 1)
return __any(v & __mask);
#else
return __any((UIntMaskType)__sext_varying_bool(v) & __mask);
#endif
}
__declspec(safe) static inline uniform bool all(bool v) {
// As with any(), we need to explicitly mask v with the current program mask
// so we're only looking at the current lanes
#if (ISPC_MASK_BITS == 1)
return __all(v | !__mask);
#else
// !__mask returns a 'bool' type. But for the logic to work, we need to
// convert this to a 'UIntMaskType' type. For 'TRUE' bool, we can be
// certain that the LSB will be set to '1'. Therefore using
// '__sext_varying_bool(!__mask)' to convert '!__mask' to 'UIntMaskType'
// is the safest option to ensure all bits are set to '1' for 'TRUE' and
// '0' for 'False'.
return __all((UIntMaskType)__sext_varying_bool(v) | (UIntMaskType)__sext_varying_bool(!__mask));
#endif
}
__declspec(safe) static inline uniform bool none(bool v) {
// As with any(), we need to explicitly mask v with the current program mask
// so we're only looking at the current lanes
#if (ISPC_MASK_BITS == 1)
return __none(v & __mask);
#else
return __none((UIntMaskType)__sext_varying_bool(v) & __mask);
#endif
}
__declspec(safe) static inline uniform int32 popcnt(uniform int32 v) { return __popcnt_int32(v); }
__declspec(safe) static inline uniform int popcnt(uniform int64 v) { return (int32)__popcnt_int64(v); }
__declspec(safe) static inline int popcnt(int v) {
int r;
for (uniform int i = 0; i < programCount; ++i)
r = insert(r, i, popcnt(extract(v, i)));
return __mask ? r : 0;
}
__declspec(safe) static inline int popcnt(int64 v) {
int r;
for (uniform int i = 0; i < programCount; ++i)
r = insert(r, i, popcnt(extract(v, i)));
return __mask ? r : 0;
}
__declspec(safe) static inline uniform int popcnt(bool v) {
// As with any() and all(), only count across the active lanes
#if (ISPC_MASK_BITS == 1)
return __popcnt_int64(__movmsk(v & __mask));
#else
return __popcnt_int64(__movmsk((UIntMaskType)__sext_varying_bool(v) & __mask));
#endif
}
__declspec(safe) static inline uniform unsigned int64 lanemask() { return __movmsk(__mask); }
__declspec(safe) static inline uniform unsigned int64 packmask(bool v) {
#if (ISPC_MASK_BITS == 1)
return __movmsk(v & __mask);
#else
return __movmsk((UIntMaskType)__sext_varying_bool(v) & __mask);
#endif
}
///////////////////////////////////////////////////////////////////////////
// memcpy/memmove/memset
static inline void memcpy(void *uniform dst, void *uniform src, uniform int32 count) {
if (__is_xe_target) {
for (uniform int j = 0; j < count; j++) {
((int8 * uniform) dst)[j] = ((int8 * uniform) src)[j];
}
} else {
__memcpy32((int8 * uniform) dst, (int8 * uniform) src, count);
}
}
static inline void memcpy64(void *uniform dst, void *uniform src, uniform int64 count) {
if (__is_xe_target) {
for (uniform int64 j = 0; j < count; j++) {
((int8 * uniform) dst)[j] = ((int8 * uniform) src)[j];
}
} else {
__memcpy64((int8 * uniform) dst, (int8 * uniform) src, count);
}
}
static inline void memcpy(void *varying dst, void *varying src, int32 count) {
void *uniform da[programCount];
void *uniform sa[programCount];
da[programIndex] = dst;
sa[programIndex] = src;
foreach_active(i) {
void *uniform d = da[i], *uniform s = sa[i];
if (__is_xe_target) {
for (uniform int j = 0; j < extract(count, i); j++) {
((int8 * uniform) d)[j] = ((int8 * uniform) s)[j];
}
} else {
__memcpy32((int8 * uniform) d, (int8 * uniform) s, extract(count, i));
}
}
}
static inline void memcpy64(void *varying dst, void *varying src, int64 count) {
void *uniform da[programCount];
void *uniform sa[programCount];
da[programIndex] = dst;
sa[programIndex] = src;
foreach_active(i) {
void *uniform d = da[i], *uniform s = sa[i];
if (__is_xe_target) {
for (uniform int64 j = 0; j < extract(count, i); j++) {
((int8 * uniform) d)[j] = ((int8 * uniform) s)[j];
}
} else {
__memcpy64((int8 * uniform) d, (int8 * uniform) s, extract(count, i));
}
}
}
static inline void memmove(void *uniform dst, void *uniform src, uniform int32 count) {
if (__is_xe_target) {
if ((uintptr_t)dst - (uintptr_t)src >= (size_t)count) {
for (uniform int j = 0; j < count; j++) {
((int8 * uniform) dst)[j] = ((int8 * uniform) src)[j];
}
} else {
for (uniform int j = count - 1; j >= 0; j--) {
((int8 * uniform) dst)[j] = ((int8 * uniform) src)[j];
}
}
} else {
__memmove32((int8 * uniform) dst, (int8 * uniform) src, count);
}
}
static inline void memmove64(void *uniform dst, void *uniform src, uniform int64 count) {
if (__is_xe_target) {
if ((uintptr_t)dst - (uintptr_t)src >= (size_t)count) {
for (uniform int64 j = 0; j < count; j++) {
((int8 * uniform) dst)[j] = ((int8 * uniform) src)[j];
}
} else {
for (uniform int64 j = count - 1; j >= 0; j--) {
((int8 * uniform) dst)[j] = ((int8 * uniform) src)[j];
}
}
} else {
__memmove64((int8 * uniform) dst, (int8 * uniform) src, count);
}
}
static inline void memmove(void *varying dst, void *varying src, int32 count) {
void *uniform da[programCount];
void *uniform sa[programCount];
da[programIndex] = dst;
sa[programIndex] = src;
foreach_active(i) {
void *uniform d = da[i], *uniform s = sa[i];
uniform int c = extract(count, i);
if (__is_xe_target) {
if ((uintptr_t)d - (uintptr_t)s >= (size_t)c) {
for (uniform int j = 0; j < count; j++) {
((int8 * uniform) d)[j] = ((int8 * uniform) s)[j];
}
} else {
for (uniform int j = c - 1; j >= 0; j--) {
((int8 * uniform) d)[j] = ((int8 * uniform) s)[j];
}
}
} else {
__memmove32((int8 * uniform) d, (int8 * uniform) s, c);
}
}
}
static inline void memmove64(void *varying dst, void *varying src, int64 count) {
void *uniform da[programCount];
void *uniform sa[programCount];
da[programIndex] = dst;
sa[programIndex] = src;
foreach_active(i) {
void *uniform d = da[i], *uniform s = sa[i];
uniform int64 c = extract(count, i);
if (__is_xe_target) {
if ((uintptr_t)d - (uintptr_t)s >= (size_t)c) {
for (uniform int64 j = 0; j < count; j++) {
((int8 * uniform) d)[j] = ((int8 * uniform) s)[j];
}
} else {
for (uniform int64 j = c - 1; j >= 0; j--) {
((int8 * uniform) d)[j] = ((int8 * uniform) s)[j];
}
}
} else {
__memmove64((int8 * uniform) d, (int8 * uniform) s, c);
}
}
}
static inline void memset(void *uniform ptr, uniform int8 val, uniform int32 count) {
if (__is_xe_target) {
for (uniform int j = 0; j < count; j++) {
((int8 * uniform) ptr)[j] = val;
}
} else {
__memset32((int8 * uniform) ptr, val, count);
}
}
static inline void memset64(void *uniform ptr, uniform int8 val, uniform int64 count) {
if (__is_xe_target) {
for (uniform int64 j = 0; j < count; j++) {
((int8 * uniform) ptr)[j] = val;
}
} else {
__memset64((int8 * uniform) ptr, val, count);
}
}
static inline void memset(void *varying ptr, int8 val, int32 count) {
void *uniform pa[programCount];
pa[programIndex] = ptr;
foreach_active(i) {
if (__is_xe_target) {
void *uniform d = pa[i];
for (uniform int j = 0; j < extract(count, i); j++) {
((int8 * uniform) d)[j] = extract(val, i);
}
} else {
__memset32((int8 * uniform) pa[i], extract(val, i), extract(count, i));
}
}
}
static inline void memset64(void *varying ptr, int8 val, int64 count) {
void *uniform pa[programCount];
pa[programIndex] = ptr;
foreach_active(i) {
if (__is_xe_target) {
void *uniform d = pa[i];
for (uniform int64 j = 0; j < extract(count, i); j++) {
((int8 * uniform) d)[j] = extract(val, i);
}
} else {
__memset64((int8 * uniform) pa[i], extract(val, i), extract(count, i));
}
}
}
///////////////////////////////////////////////////////////////////////////
// count leading/trailing zeros
__declspec(safe, cost1) static inline uniform unsigned int32 count_leading_zeros(uniform unsigned int32 v) {
return __count_leading_zeros_i32(v);
}
__declspec(safe, cost1) static inline uniform unsigned int64 count_leading_zeros(uniform unsigned int64 v) {
return __count_leading_zeros_i64(v);
}
__declspec(safe, cost1) static inline uniform unsigned int32 count_trailing_zeros(uniform unsigned int32 v) {
return __count_trailing_zeros_i32(v);
}
__declspec(safe, cost1) static inline uniform unsigned int64 count_trailing_zeros(uniform unsigned int64 v) {
return __count_trailing_zeros_i64(v);
}
__declspec(safe, cost1) static inline uniform int32 count_leading_zeros(uniform int32 v) {
return __count_leading_zeros_i32(v);
}
__declspec(safe, cost1) static inline uniform int64 count_leading_zeros(uniform int64 v) {
return __count_leading_zeros_i64(v);
}
__declspec(safe, cost1) static inline uniform int32 count_trailing_zeros(uniform int32 v) {
return __count_trailing_zeros_i32(v);
}
__declspec(safe, cost1) static inline uniform int64 count_trailing_zeros(uniform int64 v) {
return __count_trailing_zeros_i64(v);
}
__declspec(safe) static inline unsigned int32 count_leading_zeros(unsigned int32 v) {
unsigned int32 r;
for (uniform int i = 0; i < programCount; ++i)
r = insert(r, i, __count_leading_zeros_i32(extract(v, i)));
return r;
}
__declspec(safe) static inline unsigned int64 count_leading_zeros(unsigned int64 v) {
unsigned int64 r;
for (uniform int i = 0; i < programCount; ++i)
r = insert(r, i, __count_leading_zeros_i64(extract(v, i)));
return r;
}
__declspec(safe) static inline unsigned int32 count_trailing_zeros(unsigned int32 v) {
unsigned int32 r;
for (uniform int i = 0; i < programCount; ++i)
r = insert(r, i, __count_trailing_zeros_i32(extract(v, i)));
return r;
}
__declspec(safe) static inline unsigned int64 count_trailing_zeros(unsigned int64 v) {
unsigned int64 r;
for (uniform int i = 0; i < programCount; ++i)
r = insert(r, i, __count_trailing_zeros_i64(extract(v, i)));
return r;
}
__declspec(safe) static inline int32 count_leading_zeros(int32 v) {
int32 r;
for (uniform int i = 0; i < programCount; ++i)
r = insert(r, i, __count_leading_zeros_i32(extract(v, i)));
return r;
}
__declspec(safe) static inline int64 count_leading_zeros(int64 v) {
int64 r;
for (uniform int i = 0; i < programCount; ++i)
r = insert(r, i, __count_leading_zeros_i64(extract(v, i)));
return r;
}
__declspec(safe) static inline int32 count_trailing_zeros(int32 v) {
int32 r;
for (uniform int i = 0; i < programCount; ++i)
r = insert(r, i, __count_trailing_zeros_i32(extract(v, i)));
return r;
}
__declspec(safe) static inline int64 count_trailing_zeros(int64 v) {
int64 r;
for (uniform int i = 0; i < programCount; ++i)
r = insert(r, i, __count_trailing_zeros_i64(extract(v, i)));
return r;
}
///////////////////////////////////////////////////////////////////////////
// AOS/SOA conversion
static inline void aos_to_soa2(uniform float a[], varying float *uniform v0, varying float *uniform v1) {
__aos_to_soa2_float((opaque_ptr_t)a, (opaque_ptr_t)v0, (opaque_ptr_t)v1);
}
static inline void soa_to_aos2(float v0, float v1, uniform float a[]) { __soa_to_aos2_float(v0, v1, (opaque_ptr_t)a); }
static inline void aos_to_soa3(uniform float a[], varying float *uniform v0, varying float *uniform v1,
varying float *uniform v2) {
__aos_to_soa3_float((opaque_ptr_t)a, (opaque_ptr_t)v0, (opaque_ptr_t)v1, (opaque_ptr_t)v2);
}
static inline void soa_to_aos3(float v0, float v1, float v2, uniform float a[]) {
__soa_to_aos3_float(v0, v1, v2, (opaque_ptr_t)a);
}
static inline void aos_to_soa4(uniform float a[], varying float *uniform v0, varying float *uniform v1,
varying float *uniform v2, varying float *uniform v3) {
__aos_to_soa4_float((opaque_ptr_t)a, (opaque_ptr_t)v0, (opaque_ptr_t)v1, (opaque_ptr_t)v2, (opaque_ptr_t)v3);
}
static inline void soa_to_aos4(float v0, float v1, float v2, float v3, uniform float a[]) {
__soa_to_aos4_float(v0, v1, v2, v3, (opaque_ptr_t)a);
}
static inline void aos_to_soa2(uniform int32 a[], varying int32 *uniform v0, varying int32 *uniform v1) {
aos_to_soa2((uniform float *uniform)a, (varying float *uniform)v0, (varying float *uniform)v1);
}
static inline void soa_to_aos2(int32 v0, int32 v1, uniform int32 a[]) {
soa_to_aos2(floatbits(v0), floatbits(v1), (uniform float *uniform)a);
}
static inline void aos_to_soa3(uniform int32 a[], varying int32 *uniform v0, varying int32 *uniform v1,
varying int32 *uniform v2) {
aos_to_soa3((uniform float *uniform)a, (varying float *uniform)v0, (varying float *uniform)v1,
(varying float *uniform)v2);
}
static inline void soa_to_aos3(int32 v0, int32 v1, int32 v2, uniform int32 a[]) {
soa_to_aos3(floatbits(v0), floatbits(v1), floatbits(v2), (uniform float *uniform)a);
}
static inline void aos_to_soa4(uniform int32 a[], varying int32 *uniform v0, varying int32 *uniform v1,
varying int32 *uniform v2, varying int32 *uniform v3) {
aos_to_soa4((uniform float *uniform)a, (varying float *uniform)v0, (varying float *uniform)v1,
(varying float *uniform)v2, (varying float *uniform)v3);
}
static inline void soa_to_aos4(int32 v0, int32 v1, int32 v2, int32 v3, uniform int32 a[]) {
soa_to_aos4(floatbits(v0), floatbits(v1), floatbits(v2), floatbits(v3), (uniform float *uniform)a);
}
static inline void aos_to_soa2(uniform double a[], varying double *uniform v0, varying double *uniform v1) {
__aos_to_soa2_double((opaque_ptr_t)a, (opaque_ptr_t)v0, (opaque_ptr_t)v1);
}
static inline void soa_to_aos2(double v0, double v1, uniform double a[]) {
__soa_to_aos2_double(v0, v1, (opaque_ptr_t)a);
}
static inline void aos_to_soa3(uniform double a[], varying double *uniform v0, varying double *uniform v1,
varying double *uniform v2) {
__aos_to_soa3_double((opaque_ptr_t)a, (opaque_ptr_t)v0, (opaque_ptr_t)v1, (opaque_ptr_t)v2);
}
static inline void soa_to_aos3(double v0, double v1, double v2, uniform double a[]) {
__soa_to_aos3_double(v0, v1, v2, (opaque_ptr_t)a);
}
static inline void aos_to_soa4(uniform double a[], varying double *uniform v0, varying double *uniform v1,
varying double *uniform v2, varying double *uniform v3) {
__aos_to_soa4_double((opaque_ptr_t)a, (opaque_ptr_t)v0, (opaque_ptr_t)v1, (opaque_ptr_t)v2, (opaque_ptr_t)v3);
}
static inline void soa_to_aos4(double v0, double v1, double v2, double v3, uniform double a[]) {
__soa_to_aos4_double(v0, v1, v2, v3, (opaque_ptr_t)a);
}
static inline void aos_to_soa2(uniform int64 a[], varying int64 *uniform v0, varying int64 *uniform v1) {
aos_to_soa2((uniform double *uniform)a, (varying double *uniform)v0, (varying double *uniform)v1);
}
static inline void soa_to_aos2(int64 v0, int64 v1, uniform int64 a[]) {
soa_to_aos2(doublebits(v0), doublebits(v1), (uniform double *uniform)a);
}
static inline void aos_to_soa3(uniform int64 a[], varying int64 *uniform v0, varying int64 *uniform v1,
varying int64 *uniform v2) {
aos_to_soa3((uniform double *uniform)a, (varying double *uniform)v0, (varying double *uniform)v1,
(varying double *uniform)v2);
}
static inline void soa_to_aos3(int64 v0, int64 v1, int64 v2, uniform int64 a[]) {
soa_to_aos3(doublebits(v0), doublebits(v1), doublebits(v2), (uniform double *uniform)a);
}
static inline void aos_to_soa4(uniform int64 a[], varying int64 *uniform v0, varying int64 *uniform v1,
varying int64 *uniform v2, varying int64 *uniform v3) {
aos_to_soa4((uniform double *uniform)a, (varying double *uniform)v0, (varying double *uniform)v1,
(varying double *uniform)v2, (varying double *uniform)v3);
}
static inline void soa_to_aos4(int64 v0, int64 v1, int64 v2, int64 v3, uniform int64 a[]) {
soa_to_aos4(doublebits(v0), doublebits(v1), doublebits(v2), doublebits(v3), (uniform double *uniform)a);
}
///////////////////////////////////////////////////////////////////////////
// Prefetching
__declspec(safe, cost1) static inline void prefetch_l1(const void *uniform ptr) {
if (__have_xe_prefetch) {
__prefetch_read_sized_uniform_1((opaque_ptr_t)ptr, (uniform int8)PREFETCH_DATASIZE_DEFAULT);
} else {
__prefetch_read_uniform_1((opaque_ptr_t)ptr);
}
}
__declspec(safe, cost1) static inline void prefetch_l1(const void *uniform ptr, uniform int8 size) {
if (__have_xe_prefetch) {
__prefetch_read_sized_uniform_1((opaque_ptr_t)ptr, size);
} else {
__prefetch_read_uniform_1((opaque_ptr_t)ptr);
}
}
__declspec(safe, cost1) static inline void prefetch_l2(const void *uniform ptr) {
if (__have_xe_prefetch) {
__prefetch_read_sized_uniform_2((opaque_ptr_t)ptr, (uniform int8)PREFETCH_DATASIZE_DEFAULT);
} else {
__prefetch_read_uniform_2((opaque_ptr_t)ptr);
}
}
__declspec(safe, cost1) static inline void prefetch_l2(const void *uniform ptr, uniform int8 size) {
if (__have_xe_prefetch) {
__prefetch_read_sized_uniform_2((opaque_ptr_t)ptr, size);
} else {
__prefetch_read_uniform_2((opaque_ptr_t)ptr);
}
}
__declspec(safe, cost1) static inline void prefetch_l3(const void *uniform ptr) {
if (__have_xe_prefetch) {
__prefetch_read_sized_uniform_3((opaque_ptr_t)ptr, (uniform int8)PREFETCH_DATASIZE_DEFAULT);
} else {
__prefetch_read_uniform_3((opaque_ptr_t)ptr);
}
}
__declspec(safe, cost1) static inline void prefetch_l3(const void *uniform ptr, uniform int8 size) {
if (__have_xe_prefetch) {
__prefetch_read_sized_uniform_3((opaque_ptr_t)ptr, size);
} else {
__prefetch_read_uniform_3((opaque_ptr_t)ptr);
}
}
__declspec(safe, cost1) static inline void prefetch_nt(const void *uniform ptr) {
if (__have_xe_prefetch) {
__prefetch_read_sized_uniform_nt((opaque_ptr_t)ptr, (uniform int8)PREFETCH_DATASIZE_DEFAULT);
} else {
__prefetch_read_uniform_nt((opaque_ptr_t)ptr);
}
}
__declspec(safe, cost1) static inline void prefetch_nt(const void *uniform ptr, uniform int8 size) {
if (__have_xe_prefetch) {
__prefetch_read_sized_uniform_nt((opaque_ptr_t)ptr, size);
} else {
__prefetch_read_uniform_nt((opaque_ptr_t)ptr);
}
}
__declspec(safe, cost1) static inline void prefetchw_l1(const void *uniform ptr) {
__prefetch_write_uniform_1((opaque_ptr_t)ptr);
}
__declspec(safe, cost1) static inline void prefetchw_l2(const void *uniform ptr) {
__prefetch_write_uniform_2((opaque_ptr_t)ptr);
}
__declspec(safe, cost1) static inline void prefetchw_l3(const void *uniform ptr) {
__prefetch_write_uniform_3((opaque_ptr_t)ptr);
}
static inline void prefetch_l1(const void *varying ptr) {
if (__have_xe_prefetch) {
__prefetch_read_sized_varying_1((int64)ptr, (uniform int8)PREFETCH_DATASIZE_DEFAULT, (IntMaskType)__mask);
} else {
__pseudo_prefetch_read_varying_1((int64)ptr, (IntMaskType)__mask);
}
}
static inline void prefetch_l1(const void *varying ptr, uniform int8 size) {
if (__have_xe_prefetch) {
__prefetch_read_sized_varying_1((int64)ptr, size, (IntMaskType)__mask);
} else {
__pseudo_prefetch_read_varying_1((int64)ptr, (IntMaskType)__mask);
}
}
static inline void prefetch_l2(const void *varying ptr) {
if (__have_xe_prefetch) {
__prefetch_read_sized_varying_2((int64)ptr, (uniform int8)PREFETCH_DATASIZE_DEFAULT, (IntMaskType)__mask);
} else {
__pseudo_prefetch_read_varying_2((int64)ptr, (IntMaskType)__mask);
}
}
static inline void prefetch_l2(const void *varying ptr, uniform int8 size) {
if (__have_xe_prefetch) {
__prefetch_read_sized_varying_2((int64)ptr, size, (IntMaskType)__mask);
} else {
__pseudo_prefetch_read_varying_2((int64)ptr, (IntMaskType)__mask);
}
}
static inline void prefetch_l3(const void *varying ptr) {
if (__have_xe_prefetch) {
__prefetch_read_sized_varying_3((int64)ptr, (uniform int8)PREFETCH_DATASIZE_DEFAULT, (IntMaskType)__mask);
} else {
__pseudo_prefetch_read_varying_3((int64)ptr, (IntMaskType)__mask);
}
}
static inline void prefetch_l3(const void *varying ptr, uniform int8 size) {
if (__have_xe_prefetch) {
__prefetch_read_sized_varying_3((int64)ptr, size, (IntMaskType)__mask);
} else {
__pseudo_prefetch_read_varying_3((int64)ptr, (IntMaskType)__mask);
}
}
static inline void prefetch_nt(const void *varying ptr) {
if (__have_xe_prefetch) {
__prefetch_read_sized_varying_nt((int64)ptr, (uniform int8)PREFETCH_DATASIZE_DEFAULT, (IntMaskType)__mask);
} else {
__pseudo_prefetch_read_varying_nt((int64)ptr, (IntMaskType)__mask);
}
}
static inline void prefetch_nt(const void *varying ptr, uniform int8 size) {
if (__have_xe_prefetch) {
__prefetch_read_sized_varying_nt((int64)ptr, size, (IntMaskType)__mask);
} else {
__pseudo_prefetch_read_varying_nt((int64)ptr, (IntMaskType)__mask);
}
}
__declspec(safe, cost1) static inline void prefetchw_l1(const void *varying ptr) {
__pseudo_prefetch_write_varying_1((int64)ptr, (IntMaskType)__mask);
}
__declspec(safe, cost1) static inline void prefetchw_l2(const void *varying ptr) {
__pseudo_prefetch_write_varying_2((int64)ptr, (IntMaskType)__mask);
}
__declspec(safe, cost1) static inline void prefetchw_l3(const void *varying ptr) {
__pseudo_prefetch_write_varying_3((int64)ptr, (IntMaskType)__mask);
}
///////////////////////////////////////////////////////////////////////////
// non-short-circuiting alternatives
__declspec(safe, cost1) static inline bool and (bool a, bool b) { return a && b; }
__declspec(safe, cost1) static inline uniform bool and (uniform bool a, uniform bool b) { return a && b; }
__declspec(safe, cost1) static inline bool or (bool a, bool b) { return a || b; }
__declspec(safe, cost1) static inline uniform bool or (uniform bool a, uniform bool b) { return a || b; }
__declspec(safe, cost1) static inline int8 select(bool cond, int8 t, int8 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline int8 select(uniform bool cond, int8 t, int8 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uniform int8 select(uniform bool cond, uniform int8 t, uniform int8 f) {
return cond ? t : f;
}
__declspec(safe, cost1) static inline int16 select(bool cond, int16 t, int16 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline int16 select(uniform bool cond, int16 t, int16 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uniform int16 select(uniform bool cond, uniform int16 t, uniform int16 f) {
return cond ? t : f;
}
__declspec(safe, cost1) static inline float16 select(bool cond, float16 t, float16 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline float16 select(uniform bool cond, float16 t, float16 f) { return cond ? t : f; }
__declspec(safe, cost1) static inline uniform float16 select(uniform bool cond, uniform float16 t, uniform float16 f) {
return cond ? t : f;
}