forked from KhronosGroup/SYCL-CTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_reference.h
1731 lines (1479 loc) · 46.7 KB
/
math_reference.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
/*******************************************************************************
//
// SYCL 2020 Conformance Test Suite
//
// Copyright (c) 2017-2022 Codeplay Software LTD. All Rights Reserved.
// Copyright (c) 2022 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
*******************************************************************************/
#ifndef __SYCLCTS_UTIL_MATH_REFERENCE_H
#define __SYCLCTS_UTIL_MATH_REFERENCE_H
#include <sycl/sycl.hpp>
#include "./../oclmath/reference_math.h"
#include "./math_helper.h"
#include <cmath>
namespace reference {
/* two argument relational reference */
template <typename T>
auto isequal(T a, T b) {
return sycl_cts::math::rel_func_dispatcher<std::equal_to>(a, b);
}
template <typename T>
auto isnotequal(T a, T b) {
return sycl_cts::math::rel_func_dispatcher<std::not_equal_to>(a, b);
}
template <typename T>
auto isgreater(T a, T b) {
return sycl_cts::math::rel_func_dispatcher<std::greater>(a, b);
}
template <typename T>
auto isgreaterequal(T a, T b) {
return sycl_cts::math::rel_func_dispatcher<std::greater_equal>(a, b);
}
template <typename T>
auto isless(T a, T b) {
return sycl_cts::math::rel_func_dispatcher<std::less>(a, b);
}
template <typename T>
auto islessequal(T a, T b) {
return sycl_cts::math::rel_func_dispatcher<std::less_equal>(a, b);
}
auto constexpr islessgreater_func = [](const auto& x, const auto& y) {
return (x < y) || (x > y);
};
template <typename T>
auto islessgreater(T a, T b) {
return sycl_cts::math::rel_func_dispatcher(islessgreater_func, a, b);
}
auto constexpr isordered_func = [](const auto& x, const auto& y) {
return (x == x) && (y == y);
};
template <typename T>
auto isordered(T a, T b) {
return sycl_cts::math::rel_func_dispatcher(isordered_func, a, b);
}
auto constexpr isunordered_func = [](const auto& x, const auto& y) {
return !((x == x) && (y == y));
};
template <typename T>
auto isunordered(T a, T b) {
return sycl_cts::math::rel_func_dispatcher(isunordered_func, a, b);
}
/* one argument relational reference */
auto constexpr isfinite_func = [](const auto& x) { return std::isfinite(x); };
template <typename T>
auto isfinite(T a) {
return sycl_cts::math::rel_func_dispatcher(isfinite_func, a);
}
auto constexpr isinf_func = [](const auto& x) { return std::isinf(x); };
template <typename T>
auto isinf(T a) {
return sycl_cts::math::rel_func_dispatcher(isinf_func, a);
}
auto constexpr isnan_func = [](const auto& x) { return std::isnan(x); };
template <typename T>
auto isnan(T a) {
return sycl_cts::math::rel_func_dispatcher(isnan_func, a);
}
auto constexpr isnormal_func = [](const auto& x) { return std::isnormal(x); };
template <typename T>
auto isnormal(T a) {
return sycl_cts::math::rel_func_dispatcher(isnormal_func, a);
}
auto constexpr signbit_func = [](const auto& x) { return std::signbit(x); };
template <typename T>
auto signbit(T a) {
return sycl_cts::math::rel_func_dispatcher(signbit_func, a);
}
template <typename T>
bool any(T x) {
return sycl_cts::math::if_msb_set(x);
}
template <typename T>
bool all(T x) {
return sycl_cts::math::if_msb_set(x);
}
template <typename T>
T bitselect(T a, T b, T c) {
return (c & b) | (~c & a);
}
float bitselect(float a, float b, float c);
template <typename T>
T select(T a, T b, bool c) {
return c ? b : a;
}
/* absolute value */
template <typename T>
sycl_cts::resultRef<T> abs(T x) {
using U = std::make_unsigned_t<T>;
T result = x < 0 ? T(-U(x)) : x;
return result < 0 ? sycl_cts::resultRef<T>(0, true) : result;
}
/* absolute difference */
template <typename T>
sycl_cts::resultRef<T> abs_diff(T a, T b) {
using U = std::make_unsigned_t<T>;
T h = (a > b) ? a : b;
T l = (a <= b) ? a : b;
// Using two's-complement and that unsigned integer underflow is defined as
// modulo 2^n we get the result by computing the distance based on signed
// comparison.
U result = static_cast<U>(h) - static_cast<U>(l);
return result > std::numeric_limits<T>::max()
? sycl_cts::resultRef<T>(0, true)
: T(result);
}
/* add with saturation */
template <typename T>
T add_sat(T a, T b) {
if (std::is_unsigned<T>::value) {
T res = a + b;
if (res < a) res = -1;
return res;
} else {
typedef typename std::make_unsigned<T>::type U;
T r = T(U(a) + U(b));
if (b > 0) {
if (r < a) return std::numeric_limits<T>::max();
} else {
if (r > a) return std::numeric_limits<T>::min();
}
return r;
}
}
/* half add */
template <typename T>
T hadd(T a, T b) {
if (std::is_unsigned<T>::value) return (a >> 1) + (b >> 1) + ((a & b) & 0x1);
return (a >> 1) + (b >> 1) + (a & b & 1);
}
/* round up half add */
template <typename T>
T rhadd(T a, T b) {
return (a >> 1) + (b >> 1) + ((a & 1) | (b & 1));
}
/* clamp */
template <typename T>
sycl_cts::resultRef<T> clamp(T v, T minv, T maxv) {
if (minv > maxv) return sycl_cts::resultRef<T>(T(), true);
return (v < minv) ? minv : ((v > maxv) ? maxv : v);
}
/* count leading zeros */
template <typename T>
T clz(T x) {
int lz = 0;
for (int i = 0; i < sycl_cts::math::num_bits(x); i++)
if (x & (1ull << i))
lz = 0;
else
lz++;
return static_cast<T>(lz);
}
/* count trailing zeros */
template <typename T>
T ctz(T x) {
const int bit_size = sycl_cts::math::num_bits(x);
int tz = 0;
for (int i = 0; i < bit_size; i++)
if (x & (1ull << (bit_size - i - 1)))
tz = 0;
else
tz++;
return static_cast<T>(tz);
}
// mad_hi is after mul_hi
/* multiply add saturate */
unsigned char mad_sat(unsigned char, unsigned char, unsigned char);
unsigned short mad_sat(unsigned short, unsigned short, unsigned short);
unsigned int mad_sat(unsigned int, unsigned int, unsigned int);
unsigned long mad_sat(unsigned long, unsigned long, unsigned long);
unsigned long long mad_sat(unsigned long long, unsigned long long,
unsigned long long);
char mad_sat(char, char, char);
signed char mad_sat(signed char, signed char, signed char);
short mad_sat(short, short, short);
int mad_sat(int, int, int);
long mad_sat(long, long, long);
long long mad_sat(long long, long long, long long);
/* maximum value */
template <typename T>
sycl_cts::resultRef<T> max(T a, T b) {
if constexpr (std::is_integral_v<T>)
return (a < b) ? b : a;
else if (std::isfinite(a) && std::isfinite(b))
return (a < b) ? b : a;
return sycl_cts::resultRef<T>(T(), true);
}
/* minimum value */
template <typename T>
sycl_cts::resultRef<T> min(T a, T b) {
if constexpr (std::is_integral_v<T>)
return (b < a) ? b : a;
else if (std::isfinite(a) && std::isfinite(b))
return (b < a) ? b : a;
return sycl_cts::resultRef<T>(T(), true);
}
/* multiply and return high part */
unsigned char mul_hi(unsigned char, unsigned char);
unsigned short mul_hi(unsigned short, unsigned short);
unsigned int mul_hi(unsigned int, unsigned int);
unsigned long mul_hi(unsigned long, unsigned long);
unsigned long long mul_hi(unsigned long long, unsigned long long);
char mul_hi(char, char);
signed char mul_hi(signed char, signed char);
short mul_hi(short, short);
int mul_hi(int, int);
long mul_hi(long, long);
long long mul_hi(long long, long long);
/* multiply add, get high part */
template <typename T>
T mad_hi(T x, T y, T z) {
return mul_hi(x, y) + z;
}
/* bitwise rotate */
template <typename T>
T rotate(T v, T i) {
if (std::is_unsigned<T>::value) {
i = i % sycl_cts::math::num_bits(v);
if (i == 0) return v;
size_t nBits = sycl_cts::math::num_bits(v) - size_t(i);
return T((v << i) | ((v >> nBits)));
}
typedef typename std::make_unsigned<T>::type R;
R i_mod = R(i) % sycl_cts::math::num_bits(v);
if (i_mod == 0) return v;
T mask = T((T(1) << i_mod) - T(1));
size_t nBits = sycl_cts::math::num_bits(v) - size_t(i_mod);
return T((v << i_mod) | ((v >> nBits) & mask));
}
/* substract with saturation */
template <typename T>
T sub_sat(T x, T y) {
if (std::is_unsigned<T>::value) return x <= y ? 0 : x - y;
const T max_val = std::numeric_limits<T>::max();
const T min_val = std::numeric_limits<T>::min();
if (x > 0) {
if (y > 0) {
return x - y;
} else // x > 0, y <= 0
{
return (x - max_val) > y ? max_val : x - y;
}
} else // x <= 0
{
if (y > 0) {
return (x - min_val) < y ? min_val : x - y;
} else // x <= 0, y <= 0
{
return x - y;
}
}
}
/* upsample */
uint16_t upsample(uint8_t h, uint8_t l);
uint32_t upsample(uint16_t h, uint16_t l);
uint64_t upsample(uint32_t h, uint32_t l);
int16_t upsample(int8_t h, uint8_t l);
int32_t upsample(int16_t h, uint16_t l);
int64_t upsample(int32_t h, uint32_t l);
template <typename T>
struct upsample_t;
template <>
struct upsample_t<uint8_t> {
using type = uint16_t;
};
template <>
struct upsample_t<uint16_t> {
using type = uint32_t;
};
template <>
struct upsample_t<uint32_t> {
using type = uint64_t;
};
template <>
struct upsample_t<int8_t> {
using type = int16_t;
};
template <>
struct upsample_t<int16_t> {
using type = int32_t;
};
template <>
struct upsample_t<int32_t> {
using type = int64_t;
};
/* return number of non zero bits in x */
template <typename T>
T popcount(T x) {
int lz = 0;
for (int i = 0; i < sycl_cts::math::num_bits(x); i++)
if (x & (1ull << i)) lz++;
return lz;
}
/* fast multiply add 24bits */
sycl_cts::resultRef<int32_t> mad24(int32_t x, int32_t y, int32_t z);
sycl_cts::resultRef<uint32_t> mad24(uint32_t x, uint32_t y, uint32_t z);
/* fast multiply 24bits */
sycl_cts::resultRef<int32_t> mul24(int32_t x, int32_t y);
sycl_cts::resultRef<uint32_t> mul24(uint32_t x, uint32_t y);
// Common functions
float degrees(float a);
sycl_cts::resultRef<float> mix(const float a, const float b, const float c);
float radians(float a);
float step(float a, float b);
sycl_cts::resultRef<float> smoothstep(float a, float b, float c);
float sign(float a);
// Math Functions
template <typename T>
struct higher_accuracy;
template <>
struct higher_accuracy<float> {
using type = double;
};
template <typename T>
T acos(T a) {
return std::acos(static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T acosh(T a) {
return std::acosh(static_cast<typename higher_accuracy<T>::type>(a));
}
float acospi(float a);
template <typename T>
T asin(T a) {
return std::asin(static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T asinh(T a) {
return std::asinh(static_cast<typename higher_accuracy<T>::type>(a));
}
float asinpi(float a);
template <typename T>
T atan(T a) {
return std::atan(static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T atan2(T a, T b) {
return std::atan2(static_cast<typename higher_accuracy<T>::type>(a), b);
}
template <typename T>
T atanh(T a) {
return std::atanh(static_cast<typename higher_accuracy<T>::type>(a));
}
float atanpi(float a);
float atan2pi(float a, float b);
template <typename T>
T cbrt(T a) {
return std::cbrt(static_cast<typename higher_accuracy<T>::type>(a));
}
using std::ceil;
using std::copysign;
template <typename T>
T cos(T a) {
return std::cos(static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T cosh(T a) {
return std::cosh(static_cast<typename higher_accuracy<T>::type>(a));
}
float cospi(float a);
template <typename T>
T erfc(T a) {
return std::erfc(static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T erf(T a) {
return std::erf(static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T exp(T a) {
return std::exp(static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T exp2(T a) {
return std::exp2(static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T exp10(T a) {
return std::pow(static_cast<typename higher_accuracy<T>::type>(10),
static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T expm1(T a) {
return std::expm1(static_cast<typename higher_accuracy<T>::type>(a));
}
using std::fabs;
using std::fdim;
using std::floor;
float fma(float a, float b, float c);
using std::fmax;
using std::fmin;
using std::fmod;
float fract(float a, float* b);
using std::frexp;
template <typename T>
T hypot(T a, T b) {
return std::hypot(static_cast<typename higher_accuracy<T>::type>(a), b);
}
using std::ilogb;
using std::ldexp;
using std::lgamma;
template <typename T>
T lgamma_r(T a, int* b) {
*b = (std::tgamma(a) > 0) ? 1 : -1;
return std::lgamma(a);
}
template <typename T>
T log(T a) {
return std::log(static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T log2(T a) {
return std::log2(static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T log10(T a) {
return std::log10(static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T log1p(T a) {
return std::log1p(static_cast<typename higher_accuracy<T>::type>(a));
}
using std::logb;
template <typename T>
T mad(T a, T b, T c) {
return a * b + c;
}
template <typename T>
T maxmag(T a, T b) {
if (fabs(a) > fabs(b))
return a;
else if (fabs(b) > fabs(a))
return b;
return fmax(a, b);
}
template <typename T>
T minmag(T a, T b) {
if (fabs(a) < fabs(b))
return a;
else if (fabs(b) < fabs(a))
return b;
return fmin(a, b);
}
using std::modf;
float nan(unsigned int a);
using std::nextafter;
template <typename T>
T pow(T a, T b) {
return std::pow(static_cast<typename higher_accuracy<T>::type>(a),
static_cast<typename higher_accuracy<T>::type>(b));
}
template <typename T>
T pown(T a, int b) {
return std::pow(static_cast<typename higher_accuracy<T>::type>(a),
static_cast<typename higher_accuracy<T>::type>(b));
}
template <typename T>
sycl_cts::resultRef<T> powr(T a, T b) {
if (a < 0) return sycl_cts::resultRef<T>(T(), true);
return std::pow(static_cast<typename higher_accuracy<T>::type>(a),
static_cast<typename higher_accuracy<T>::type>(b));
}
using std::remainder;
template <typename T>
T remquo(T x, T y, int* quo) {
return reference_remquol(x, y, quo);
}
using std::rint;
template <typename T>
T rootn(T a, int b) {
return std::pow(static_cast<typename higher_accuracy<T>::type>(a),
static_cast<typename higher_accuracy<T>::type>(1.0 / b));
}
using std::round;
template <typename T>
T rsqrt(T a) {
return 1 / std::sqrt(static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T sincos(T a, T* b) {
*b = std::cos(static_cast<typename higher_accuracy<T>::type>(a));
return std::sin(static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T sin(T a) {
return std::sin(static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T sinh(T a) {
return std::sinh(static_cast<typename higher_accuracy<T>::type>(a));
}
float sinpi(float a);
template <typename T>
T sqrt(T a) {
return std::sqrt(static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T tan(T a) {
return std::tan(static_cast<typename higher_accuracy<T>::type>(a));
}
template <typename T>
T tanh(T a) {
return std::tanh(static_cast<typename higher_accuracy<T>::type>(a));
}
float tanpi(float a);
template <typename T>
T tgamma(T a) {
return std::tgamma(static_cast<typename higher_accuracy<T>::type>(a));
}
using std::trunc;
template <typename T>
T recip(T a) {
return 1.0 / a;
}
template <typename T>
T divide(T a, T b) {
return a / b;
}
// Geometric functions
sycl::float4 cross(sycl::float4 p0, sycl::float4 p1);
sycl::float3 cross(sycl::float3 p0, sycl::float3 p1);
// FIXME: AdaptiveCpp does not support marray
#ifndef SYCL_CTS_COMPILING_WITH_ADAPTIVECPP
sycl::mfloat4 cross(sycl::mfloat4 p0, sycl::mfloat4 p1);
sycl::mfloat3 cross(sycl::mfloat3 p0, sycl::mfloat3 p1);
#endif
template <typename T>
T dot(T p0, T p1) {
return p0 * p1;
}
template <typename T>
T normalize(T p) {
if (p < 0) return -1;
return 1;
}
#if SYCL_CTS_ENABLE_HALF_TESTS
template <>
struct higher_accuracy<sycl::half> {
using type = float;
};
sycl::half bitselect(sycl::half a, sycl::half b, sycl::half c);
sycl::half degrees(sycl::half);
sycl_cts::resultRef<sycl::half> mix(const sycl::half a, const sycl::half b,
const sycl::half c);
sycl::half radians(sycl::half);
sycl::half step(sycl::half a, sycl::half b);
sycl_cts::resultRef<sycl::half> smoothstep(sycl::half a, sycl::half b,
sycl::half c);
sycl::half sign(sycl::half a);
sycl::half acospi(sycl::half a);
sycl::half asinpi(sycl::half a);
sycl::half atanpi(sycl::half a);
sycl::half atan2pi(sycl::half a, sycl::half b);
sycl::half cospi(sycl::half a);
sycl::half fdim(sycl::half a, sycl::half b);
sycl::half fma(sycl::half a, sycl::half b, sycl::half c);
sycl::half fract(sycl::half a, sycl::half* b);
sycl::half modf(sycl::half a, sycl::half* b);
sycl::half nan(unsigned short a);
template <int N>
sycl::vec<sycl::half, N> nan(sycl::vec<unsigned short, N> a) {
return sycl_cts::math::run_func_on_vector<sycl::half, unsigned short, N>(
[](unsigned short x) { return nan(x); }, a);
}
// FIXME: AdaptiveCpp does not support marray
#ifndef SYCL_CTS_COMPILING_WITH_ADAPTIVECPP
template <size_t N>
sycl::marray<sycl::half, N> nan(sycl::marray<unsigned short, N> a) {
return sycl_cts::math::run_func_on_marray<sycl::half, unsigned short, N>(
[](unsigned short x) { return nan(x); }, a);
}
#endif
sycl::half nextafter(sycl::half a, sycl::half b);
sycl::half sinpi(sycl::half a);
sycl::half tanpi(sycl::half a);
sycl::half fast_dot(float p0);
sycl::half fast_dot(sycl::float2 p0);
sycl::half fast_dot(sycl::float3 p0);
sycl::half fast_dot(sycl::float4 p0);
// FIXME: AdaptiveCpp does not support marray
#ifndef SYCL_CTS_COMPILING_WITH_ADAPTIVECPP
sycl::half fast_dot(sycl::mfloat2 p0);
sycl::half fast_dot(sycl::mfloat3 p0);
sycl::half fast_dot(sycl::mfloat4 p0);
#endif
#endif // SYCL_CTS_ENABLE_HALF_TESTS
#if SYCL_CTS_ENABLE_DOUBLE_TESTS
template <>
struct higher_accuracy<double> {
using type = long double;
};
double bitselect(double a, double b, double c);
double degrees(double a);
sycl_cts::resultRef<double> mix(const double a, const double b, const double c);
double radians(double a);
double step(double a, double b);
sycl_cts::resultRef<double> smoothstep(double a, double b, double c);
double sign(double a);
double acospi(double a);
double asinpi(double a);
double atanpi(double a);
double atan2pi(double a, double b);
double cospi(double a);
double fma(double a, double b, double c);
double fract(double a, double* b);
double nan(unsigned long a);
double nan(unsigned long long a);
template <typename T, int N>
std::enable_if_t<std::is_same_v<unsigned long, T> ||
std::is_same_v<unsigned long long, T>,
sycl::vec<double, N>>
nan(sycl::vec<T, N> a) {
return sycl_cts::math::run_func_on_vector<double, T, N>(
[](T x) { return nan(x); }, a);
}
// FIXME: AdaptiveCpp does not support marray
#ifndef SYCL_CTS_COMPILING_WITH_ADAPTIVECPP
template <typename T, size_t N>
std::enable_if_t<std::is_same_v<unsigned long, T> ||
std::is_same_v<unsigned long long, T>,
sycl::marray<double, N>>
nan(sycl::marray<T, N> a) {
return sycl_cts::math::run_func_on_marray<double, T, N>(
[](T x) { return nan(x); }, a);
}
#endif
double sinpi(double a);
double tanpi(double a);
sycl::double4 cross(sycl::double4 p0, sycl::double4 p1);
sycl::double3 cross(sycl::double3 p0, sycl::double3 p1);
// FIXME: AdaptiveCpp does not support marray
#ifndef SYCL_CTS_COMPILING_WITH_ADAPTIVECPP
sycl::mdouble4 cross(sycl::mdouble4 p0, sycl::mdouble4 p1);
sycl::mdouble3 cross(sycl::mdouble3 p0, sycl::mdouble3 p1);
#endif
#endif // SYCL_CTS_ENABLE_DOUBLE_TESTS
// sycl::vec overloads of the above. Some vector functions reference their
// scalar counterparts, so all scalar overloads (float, half, double) must have
// been declared previously - otherwise they will not participate in overload
// resolution, even if the point of template instantiation is outside this file.
#define MAKE_VEC_VERSION(func) \
template <typename T, int N> \
sycl::vec<T, N> func(sycl::vec<T, N> a) { \
return sycl_cts::math::run_func_on_vector<T, T, N>( \
[](T x) { return func(x); }, a); \
}
#define MAKE_VEC_VERSION_2ARGS(func) \
template <typename T, int N> \
sycl::vec<T, N> func(sycl::vec<T, N> a, sycl::vec<T, N> b) { \
return sycl_cts::math::run_func_on_vector<T, T, N>( \
[](T x, T y) { return func(x, y); }, a, b); \
}
#define MAKE_VEC_VERSION_3ARGS(func) \
template <typename T, int N> \
sycl::vec<T, N> func(sycl::vec<T, N> a, sycl::vec<T, N> b, \
sycl::vec<T, N> c) { \
return sycl_cts::math::run_func_on_vector<T, T, N>( \
[](T x, T y, T z) { return func(x, y, z); }, a, b, c); \
}
#define MAKE_VEC_VERSION_WITH_SCALAR(func) \
template <typename T, int N> \
sycl::vec<T, N> func(sycl::vec<T, N> a, T b) { \
return sycl_cts::math::run_func_on_vector<T, T, N>( \
[](T x, T y) { return func(x, y); }, a, b); \
}
// Common functions
template <typename T, int N>
int any(sycl::vec<T, N> a) {
for (int i = 0; i < N; i++) {
if (any(a[i]) == 1) return true;
}
return false;
}
template <typename T, int N>
int all(sycl::vec<T, N> a) {
for (int i = 0; i < N; i++) {
if (all(a[i]) == 0) return false;
}
return true;
}
MAKE_VEC_VERSION_3ARGS(bitselect)
template <typename T, typename K, int N>
sycl::vec<T, N> select(sycl::vec<T, N> a, sycl::vec<T, N> b,
sycl::vec<K, N> c) {
sycl::vec<T, N> res;
for (int i = 0; i < N; i++) {
if (any(c[i]) == 1)
res[i] = b[i];
else
res[i] = a[i];
}
return res;
}
template <typename T, int N>
sycl_cts::resultRef<sycl::vec<T, N>> abs(sycl::vec<T, N> a) {
return sycl_cts::math::run_func_on_vector_result_ref<T, N>(
[](T x) { return abs(x); }, a);
}
template <typename T, int N>
sycl_cts::resultRef<sycl::vec<T, N>> abs_diff(sycl::vec<T, N> a,
sycl::vec<T, N> b) {
return sycl_cts::math::run_func_on_vector_result_ref<T, N>(
[](T x, T y) { return abs_diff(x, y); }, a, b);
}
MAKE_VEC_VERSION_2ARGS(add_sat)
MAKE_VEC_VERSION_2ARGS(hadd)
MAKE_VEC_VERSION_2ARGS(rhadd)
template <typename T, int N>
sycl_cts::resultRef<sycl::vec<T, N>> clamp(sycl::vec<T, N> a, sycl::vec<T, N> b,
sycl::vec<T, N> c) {
return sycl_cts::math::run_func_on_vector_result_ref<T, N>(
[](T x, T y, T z) { return clamp(x, y, z); }, a, b, c);
}
template <typename T, int N>
sycl_cts::resultRef<sycl::vec<T, N>> clamp(sycl::vec<T, N> a, T b, T c) {
sycl::vec<T, N> res;
std::map<int, bool> undefined;
for (int i = 0; i < N; i++) {
sycl_cts::resultRef<T> element = clamp(a[i], b, c);
if (element.undefined.empty())
res[i] = element.res;
else
undefined[i] = true;
}
return sycl_cts::resultRef<sycl::vec<T, N>>(res, undefined);
}
MAKE_VEC_VERSION(clz)
MAKE_VEC_VERSION(ctz)
MAKE_VEC_VERSION_3ARGS(mad_sat)
template <typename T, int N>
sycl_cts::resultRef<sycl::vec<T, N>> max(sycl::vec<T, N> a, sycl::vec<T, N> b) {
return sycl_cts::math::run_func_on_vector_result_ref<T, N>(
[](T x, T y) { return max(x, y); }, a, b);
}
template <typename T, int N>
sycl_cts::resultRef<sycl::vec<T, N>> max(sycl::vec<T, N> a, T b) {
return sycl_cts::math::run_func_on_vector_result_ref<T, N>(
[](T x, T y) { return max(x, y); }, a, b);
}
template <typename T, int N>
sycl_cts::resultRef<sycl::vec<T, N>> min(sycl::vec<T, N> a, sycl::vec<T, N> b) {
return sycl_cts::math::run_func_on_vector_result_ref<T, N>(
[](T x, T y) { return min(x, y); }, a, b);
}
template <typename T, int N>
sycl_cts::resultRef<sycl::vec<T, N>> min(sycl::vec<T, N> a, T b) {
return sycl_cts::math::run_func_on_vector_result_ref<T, N>(
[](T x, T y) { return min(x, y); }, a, b);
}
MAKE_VEC_VERSION_2ARGS(mul_hi)
MAKE_VEC_VERSION_3ARGS(mad_hi)
MAKE_VEC_VERSION_2ARGS(rotate)
MAKE_VEC_VERSION_2ARGS(sub_sat)
template <typename T, int N>
sycl::vec<typename upsample_t<T>::type, N> upsample(
sycl::vec<T, N> a, sycl::vec<typename std::make_unsigned<T>::type, N> b) {
return sycl_cts::math::run_func_on_vector<typename upsample_t<T>::type, T, N>(
[](T x, T y) { return upsample(x, y); }, a, b);
}
MAKE_VEC_VERSION(popcount)
template <typename T, int N>
sycl_cts::resultRef<sycl::vec<T, N>> mad24(sycl::vec<T, N> a, sycl::vec<T, N> b,
sycl::vec<T, N> c) {
return sycl_cts::math::run_func_on_vector_result_ref<T, N>(
[](T x, T y, T z) { return mad24(x, y, z); }, a, b, c);
}
template <typename T, int N>
sycl_cts::resultRef<sycl::vec<T, N>> mul24(sycl::vec<T, N> a,
sycl::vec<T, N> b) {
return sycl_cts::math::run_func_on_vector_result_ref<T, N>(
[](T x, T y) { return mul24(x, y); }, a, b);
}
MAKE_VEC_VERSION(degrees)
template <typename T, int N>
sycl_cts::resultRef<sycl::vec<T, N>> mix(sycl::vec<T, N> a, sycl::vec<T, N> b,
sycl::vec<T, N> c) {
return sycl_cts::math::run_func_on_vector_result_ref<T, N>(
[](T x, T y, T z) { return mix(x, y, z); }, a, b, c);
}
template <typename T, int N>
sycl_cts::resultRef<sycl::vec<T, N>> mix(sycl::vec<T, N> a, sycl::vec<T, N> b,
T c) {
sycl::vec<T, N> res;
std::map<int, bool> undefined;
for (int i = 0; i < N; i++) {
sycl_cts::resultRef<T> element = mix(a[i], b[i], c);
if (element.undefined.empty())
res[i] = element.res;
else
undefined[i] = true;
}
return sycl_cts::resultRef<sycl::vec<T, N>>(res, undefined);
}
MAKE_VEC_VERSION(radians)
template <typename T, int N>
sycl::vec<T, N> step(T a, sycl::vec<T, N> b) {
sycl::vec<T, N> res;
for (int i = 0; i < N; i++) {
res[i] = step(a, b[i]);
}
return res;
}
template <typename T, int N>
sycl_cts::resultRef<sycl::vec<T, N>> smoothstep(sycl::vec<T, N> a,
sycl::vec<T, N> b,
sycl::vec<T, N> c) {