forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestMathematicalFunctions.hpp
1797 lines (1687 loc) · 86.2 KB
/
TestMathematicalFunctions.hpp
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
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER
#include <gtest/gtest.h>
#include <Kokkos_Core.hpp>
#include <algorithm>
#include <initializer_list>
#include <type_traits>
#include <cfloat>
#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || \
defined(KOKKOS_ENABLE_SYCL) || defined(KOKKOS_ENABLE_OPENMPTARGET) || \
defined(KOKKOS_ENABLE_OPENACC)
#else
#define MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
#endif
#if defined KOKKOS_COMPILER_INTEL || \
(defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC >= 1130)
#define MATHEMATICAL_FUNCTIONS_TEST_UNREACHABLE __builtin_unreachable();
#else
#define MATHEMATICAL_FUNCTIONS_TEST_UNREACHABLE
#endif
namespace KE = Kokkos::Experimental;
// clang-format off
template <class>
struct math_unary_function_return_type;
// Floating-point types
#if defined(KOKKOS_HALF_T_IS_FLOAT) && !KOKKOS_HALF_T_IS_FLOAT
template <> struct math_unary_function_return_type<KE::half_t> { using type = KE::half_t; };
#endif // defined(KOKKOS_HALF_T_IS_FLOAT) && !KOKKOS_HALF_T_IS_FLOAT
#if defined(KOKKOS_BHALF_T_IS_FLOAT) && !KOKKOS_BHALF_T_IS_FLOAT
template <> struct math_unary_function_return_type<KE::bhalf_t> { using type = KE::bhalf_t; };
#endif // defined(KOKKOS_BHALF_T_IS_FLOAT) && !KOKKOS_BHALF_T_IS_FLOAT
template <> struct math_unary_function_return_type< float> { using type = float; };
template <> struct math_unary_function_return_type< double> { using type = double; };
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
template <> struct math_unary_function_return_type<long double> { using type = long double; };
#endif
// Integral types
template <> struct math_unary_function_return_type< bool> { using type = double; };
template <> struct math_unary_function_return_type< short> { using type = double; };
template <> struct math_unary_function_return_type< unsigned short> { using type = double; };
template <> struct math_unary_function_return_type< int> { using type = double; };
template <> struct math_unary_function_return_type< unsigned int> { using type = double; };
template <> struct math_unary_function_return_type< long> { using type = double; };
template <> struct math_unary_function_return_type< unsigned long> { using type = double; };
template <> struct math_unary_function_return_type< long long> { using type = double; };
template <> struct math_unary_function_return_type<unsigned long long> { using type = double; };
template <class T>
using math_unary_function_return_type_t = typename math_unary_function_return_type<T>::type;
template <class, class>
struct math_binary_function_return_type;
#if defined(KOKKOS_HALF_T_IS_FLOAT) && !KOKKOS_HALF_T_IS_FLOAT
template <> struct math_binary_function_return_type<KE::half_t, KE::half_t> { using type = KE::half_t; };
template <> struct math_binary_function_return_type<short, KE::half_t> { using type = double; };
template <> struct math_binary_function_return_type<unsigned short, KE::half_t> { using type = double; };
template <> struct math_binary_function_return_type<int, KE::half_t> { using type = double; };
template <> struct math_binary_function_return_type<unsigned int, KE::half_t> { using type = double; };
template <> struct math_binary_function_return_type<long, KE::half_t> { using type = double; };
template <> struct math_binary_function_return_type<unsigned long, KE::half_t> { using type = double; };
template <> struct math_binary_function_return_type<long long, KE::half_t> { using type = double; };
template <> struct math_binary_function_return_type<unsigned long long, KE::half_t> { using type = double; };
#endif // defined(KOKKOS_HALF_T_IS_FLOAT) && !KOKKOS_HALF_T_IS_FLOAT
#if defined(KOKKOS_BHALF_T_IS_FLOAT) && !KOKKOS_BHALF_T_IS_FLOAT
template <> struct math_binary_function_return_type<KE::bhalf_t, KE::bhalf_t> { using type = KE::bhalf_t; };
template <> struct math_binary_function_return_type<KE::half_t, KE::bhalf_t> { using type = KE::half_t; };
template <> struct math_binary_function_return_type<short, KE::bhalf_t> { using type = double; };
template <> struct math_binary_function_return_type<unsigned short, KE::bhalf_t> { using type = double; };
template <> struct math_binary_function_return_type<int, KE::bhalf_t> { using type = double; };
template <> struct math_binary_function_return_type<unsigned int, KE::bhalf_t> { using type = double; };
template <> struct math_binary_function_return_type<long, KE::bhalf_t> { using type = double; };
template <> struct math_binary_function_return_type<unsigned long, KE::bhalf_t> { using type = double; };
template <> struct math_binary_function_return_type<long long, KE::bhalf_t> { using type = double; };
template <> struct math_binary_function_return_type<unsigned long long, KE::bhalf_t> { using type = double; };
#endif // defined(KOKKOS_BHALF_T_IS_FLOAT) && !KOKKOS_BHALF_T_IS_FLOAT
template <> struct math_binary_function_return_type< float, float> { using type = float; };
template <> struct math_binary_function_return_type< float, double> { using type = double; };
template <> struct math_binary_function_return_type< float, bool> { using type = double; };
template <> struct math_binary_function_return_type< float, short> { using type = double; };
template <> struct math_binary_function_return_type< float, int> { using type = double; };
template <> struct math_binary_function_return_type< float, long> { using type = double; };
template <> struct math_binary_function_return_type< float, long long> { using type = double; };
template <> struct math_binary_function_return_type< float, unsigned short> { using type = double; };
template <> struct math_binary_function_return_type< float, unsigned int> { using type = double; };
template <> struct math_binary_function_return_type< float, unsigned long> { using type = double; };
template <> struct math_binary_function_return_type< float, unsigned long long> { using type = double; };
template <> struct math_binary_function_return_type< double, float> { using type = double; };
template <> struct math_binary_function_return_type< double, double> { using type = double; };
template <> struct math_binary_function_return_type< double, bool> { using type = double; };
template <> struct math_binary_function_return_type< double, short> { using type = double; };
template <> struct math_binary_function_return_type< double, int> { using type = double; };
template <> struct math_binary_function_return_type< double, long> { using type = double; };
template <> struct math_binary_function_return_type< double, long long> { using type = double; };
template <> struct math_binary_function_return_type< double, unsigned short> { using type = double; };
template <> struct math_binary_function_return_type< double, unsigned int> { using type = double; };
template <> struct math_binary_function_return_type< double, unsigned long> { using type = double; };
template <> struct math_binary_function_return_type< double, unsigned long long> { using type = double; };
template <> struct math_binary_function_return_type< short, float> { using type = double; };
template <> struct math_binary_function_return_type< short, double> { using type = double; };
template <> struct math_binary_function_return_type< short, bool> { using type = double; };
template <> struct math_binary_function_return_type< short, short> { using type = double; };
template <> struct math_binary_function_return_type< short, int> { using type = double; };
template <> struct math_binary_function_return_type< short, long> { using type = double; };
template <> struct math_binary_function_return_type< short, long long> { using type = double; };
template <> struct math_binary_function_return_type< short, unsigned short> { using type = double; };
template <> struct math_binary_function_return_type< short, unsigned int> { using type = double; };
template <> struct math_binary_function_return_type< short, unsigned long> { using type = double; };
template <> struct math_binary_function_return_type< short, unsigned long long> { using type = double; };
template <> struct math_binary_function_return_type< int, float> { using type = double; };
template <> struct math_binary_function_return_type< int, double> { using type = double; };
template <> struct math_binary_function_return_type< int, bool> { using type = double; };
template <> struct math_binary_function_return_type< int, short> { using type = double; };
template <> struct math_binary_function_return_type< int, int> { using type = double; };
template <> struct math_binary_function_return_type< int, long> { using type = double; };
template <> struct math_binary_function_return_type< int, long long> { using type = double; };
template <> struct math_binary_function_return_type< int, unsigned short> { using type = double; };
template <> struct math_binary_function_return_type< int, unsigned int> { using type = double; };
template <> struct math_binary_function_return_type< int, unsigned long> { using type = double; };
template <> struct math_binary_function_return_type< int, unsigned long long> { using type = double; };
template <> struct math_binary_function_return_type< long, float> { using type = double; };
template <> struct math_binary_function_return_type< long, double> { using type = double; };
template <> struct math_binary_function_return_type< long, bool> { using type = double; };
template <> struct math_binary_function_return_type< long, short> { using type = double; };
template <> struct math_binary_function_return_type< long, int> { using type = double; };
template <> struct math_binary_function_return_type< long, long> { using type = double; };
template <> struct math_binary_function_return_type< long, long long> { using type = double; };
template <> struct math_binary_function_return_type< long, unsigned short> { using type = double; };
template <> struct math_binary_function_return_type< long, unsigned int> { using type = double; };
template <> struct math_binary_function_return_type< long, unsigned long> { using type = double; };
template <> struct math_binary_function_return_type< long, unsigned long long> { using type = double; };
template <> struct math_binary_function_return_type< long long, float> { using type = double; };
template <> struct math_binary_function_return_type< long long, double> { using type = double; };
template <> struct math_binary_function_return_type< long long, bool> { using type = double; };
template <> struct math_binary_function_return_type< long long, short> { using type = double; };
template <> struct math_binary_function_return_type< long long, int> { using type = double; };
template <> struct math_binary_function_return_type< long long, long> { using type = double; };
template <> struct math_binary_function_return_type< long long, long long> { using type = double; };
template <> struct math_binary_function_return_type< long long, unsigned short> { using type = double; };
template <> struct math_binary_function_return_type< long long, unsigned int> { using type = double; };
template <> struct math_binary_function_return_type< long long, unsigned long> { using type = double; };
template <> struct math_binary_function_return_type< long long, unsigned long long> { using type = double; };
template <> struct math_binary_function_return_type< unsigned short, float> { using type = double; };
template <> struct math_binary_function_return_type< unsigned short, double> { using type = double; };
template <> struct math_binary_function_return_type< unsigned short, bool> { using type = double; };
template <> struct math_binary_function_return_type< unsigned short, short> { using type = double; };
template <> struct math_binary_function_return_type< unsigned short, int> { using type = double; };
template <> struct math_binary_function_return_type< unsigned short, long> { using type = double; };
template <> struct math_binary_function_return_type< unsigned short, long long> { using type = double; };
template <> struct math_binary_function_return_type< unsigned short, unsigned short> { using type = double; };
template <> struct math_binary_function_return_type< unsigned short, unsigned int> { using type = double; };
template <> struct math_binary_function_return_type< unsigned short, unsigned long> { using type = double; };
template <> struct math_binary_function_return_type< unsigned short, unsigned long long> { using type = double; };
template <> struct math_binary_function_return_type< unsigned int, float> { using type = double; };
template <> struct math_binary_function_return_type< unsigned int, double> { using type = double; };
template <> struct math_binary_function_return_type< unsigned int, bool> { using type = double; };
template <> struct math_binary_function_return_type< unsigned int, short> { using type = double; };
template <> struct math_binary_function_return_type< unsigned int, int> { using type = double; };
template <> struct math_binary_function_return_type< unsigned int, long> { using type = double; };
template <> struct math_binary_function_return_type< unsigned int, long long> { using type = double; };
template <> struct math_binary_function_return_type< unsigned int, unsigned short> { using type = double; };
template <> struct math_binary_function_return_type< unsigned int, unsigned int> { using type = double; };
template <> struct math_binary_function_return_type< unsigned int, unsigned long> { using type = double; };
template <> struct math_binary_function_return_type< unsigned int, unsigned long long> { using type = double; };
template <> struct math_binary_function_return_type< unsigned long, float> { using type = double; };
template <> struct math_binary_function_return_type< unsigned long, double> { using type = double; };
template <> struct math_binary_function_return_type< unsigned long, bool> { using type = double; };
template <> struct math_binary_function_return_type< unsigned long, short> { using type = double; };
template <> struct math_binary_function_return_type< unsigned long, int> { using type = double; };
template <> struct math_binary_function_return_type< unsigned long, long> { using type = double; };
template <> struct math_binary_function_return_type< unsigned long, long long> { using type = double; };
template <> struct math_binary_function_return_type< unsigned long, unsigned short> { using type = double; };
template <> struct math_binary_function_return_type< unsigned long, unsigned int> { using type = double; };
template <> struct math_binary_function_return_type< unsigned long, unsigned long> { using type = double; };
template <> struct math_binary_function_return_type< unsigned long, unsigned long long> { using type = double; };
template <> struct math_binary_function_return_type<unsigned long long, float> { using type = double; };
template <> struct math_binary_function_return_type<unsigned long long, double> { using type = double; };
template <> struct math_binary_function_return_type<unsigned long long, bool> { using type = double; };
template <> struct math_binary_function_return_type<unsigned long long, short> { using type = double; };
template <> struct math_binary_function_return_type<unsigned long long, int> { using type = double; };
template <> struct math_binary_function_return_type<unsigned long long, long> { using type = double; };
template <> struct math_binary_function_return_type<unsigned long long, long long> { using type = double; };
template <> struct math_binary_function_return_type<unsigned long long, unsigned short> { using type = double; };
template <> struct math_binary_function_return_type<unsigned long long, unsigned int> { using type = double; };
template <> struct math_binary_function_return_type<unsigned long long, unsigned long> { using type = double; };
template <> struct math_binary_function_return_type<unsigned long long, unsigned long long> { using type = double; };
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
template <> struct math_binary_function_return_type< float, long double> { using type = long double; };
template <> struct math_binary_function_return_type< double, long double> { using type = long double; };
template <> struct math_binary_function_return_type< long double, float> { using type = long double; };
template <> struct math_binary_function_return_type< long double, double> { using type = long double; };
template <> struct math_binary_function_return_type< long double, long double> { using type = long double; };
template <> struct math_binary_function_return_type< long double, bool> { using type = long double; };
template <> struct math_binary_function_return_type< long double, short> { using type = long double; };
template <> struct math_binary_function_return_type< long double, int> { using type = long double; };
template <> struct math_binary_function_return_type< long double, long> { using type = long double; };
template <> struct math_binary_function_return_type< long double, long long> { using type = long double; };
template <> struct math_binary_function_return_type< long double, unsigned short> { using type = long double; };
template <> struct math_binary_function_return_type< long double, unsigned int> { using type = long double; };
template <> struct math_binary_function_return_type< long double, unsigned long> { using type = long double; };
template <> struct math_binary_function_return_type< long double, unsigned long long> { using type = long double; };
template <> struct math_binary_function_return_type< short, long double> { using type = long double; };
template <> struct math_binary_function_return_type< int, long double> { using type = long double; };
template <> struct math_binary_function_return_type< long, long double> { using type = long double; };
template <> struct math_binary_function_return_type< long long, long double> { using type = long double; };
template <> struct math_binary_function_return_type< unsigned short, long double> { using type = long double; };
template <> struct math_binary_function_return_type< unsigned int, long double> { using type = long double; };
template <> struct math_binary_function_return_type< unsigned long, long double> { using type = long double; };
template <> struct math_binary_function_return_type<unsigned long long, long double> { using type = long double; };
#endif
template <class T, class U>
using math_binary_function_return_type_t = typename math_binary_function_return_type<T, U>::type;
// clang-format on
template <class T, class U, class V>
using math_ternary_function_return_type_t = math_binary_function_return_type_t<
T, math_binary_function_return_type_t<U, V>>;
struct FloatingPointComparison {
private:
template <class T>
KOKKOS_FUNCTION double eps(T) const {
return DBL_EPSILON;
}
#if defined(KOKKOS_HALF_T_IS_FLOAT) && !KOKKOS_HALF_T_IS_FLOAT
KOKKOS_FUNCTION
KE::half_t eps(KE::half_t) const {
// FIXME_NVHPC compile-time error
#ifdef KOKKOS_COMPILER_NVHPC
return 0.0009765625F;
#else
return KE::epsilon<KE::half_t>::value;
#endif
}
#endif
#if defined(KOKKOS_BHALF_T_IS_FLOAT) && !KOKKOS_BHALF_T_IS_FLOAT
KOKKOS_FUNCTION
KE::bhalf_t eps(KE::bhalf_t) const {
// FIXME_NVHPC compile-time error
#ifdef KOKKOS_COMPILER_NVHPC
return 0.0078125;
#else
return KE::epsilon<KE::bhalf_t>::value;
#endif
}
#endif
KOKKOS_FUNCTION
double eps(float) const { return FLT_EPSILON; }
// POWER9 gives unexpected values with LDBL_EPSILON issues
// https://stackoverflow.com/questions/68960416/ppc64-long-doubles-machine-epsilon-calculation
#if defined(KOKKOS_ARCH_POWER9) || defined(KOKKOS_ARCH_POWER8)
KOKKOS_FUNCTION
double eps(long double) const { return DBL_EPSILON; }
#else
KOKKOS_FUNCTION
double eps(long double) const { return LDBL_EPSILON; }
#endif
// Using absolute here instead of abs, since we actually test abs ...
template <class T>
KOKKOS_FUNCTION std::enable_if_t<std::is_signed<T>::value, T> absolute(
T val) const {
return val < T(0) ? -val : val;
}
template <class T>
KOKKOS_FUNCTION std::enable_if_t<!std::is_signed<T>::value, T> absolute(
T val) const {
return val;
}
public:
template <class FPT>
KOKKOS_FUNCTION bool compare_near_zero(FPT const& fpv, int ulp) const {
auto abs_tol = eps(fpv) * ulp;
bool ar = absolute(fpv) < abs_tol;
if (!ar) {
Kokkos::printf("absolute value exceeds tolerance [|%e| > %e]\n",
(double)fpv, abs_tol);
}
return ar;
}
template <class Lhs, class Rhs>
KOKKOS_FUNCTION bool compare(Lhs const& lhs, Rhs const& rhs, int ulp) const {
if (lhs == 0) {
return compare_near_zero(rhs, ulp);
} else if (rhs == 0) {
return compare_near_zero(lhs, ulp);
} else {
auto rel_tol = (eps(lhs) < eps(rhs) ? eps(lhs) : eps(rhs)) * ulp;
double abs_diff = static_cast<double>(rhs > lhs ? rhs - lhs : lhs - rhs);
double min_denom = static_cast<double>(
absolute(rhs) < absolute(lhs) ? absolute(rhs) : absolute(lhs));
double rel_diff = abs_diff / min_denom;
bool ar = abs_diff == 0 || rel_diff < rel_tol;
if (!ar) {
Kokkos::printf("relative difference exceeds tolerance [%e > %e]\n",
(double)rel_diff, rel_tol);
}
return ar;
}
}
};
template <class>
struct math_function_name;
#define DEFINE_UNARY_FUNCTION_EVAL(FUNC, ULP_FACTOR) \
struct MathUnaryFunction_##FUNC { \
template <typename T> \
static KOKKOS_FUNCTION auto eval(T x) { \
static_assert( \
std::is_same<decltype(Kokkos::FUNC((T)0)), \
math_unary_function_return_type_t<T>>::value); \
return Kokkos::FUNC(x); \
} \
template <typename T> \
static auto eval_std(T x) { \
if constexpr (std::is_same<T, KE::half_t>::value || \
std::is_same<T, KE::bhalf_t>::value) { \
return std::FUNC(static_cast<float>(x)); \
} else { \
static_assert( \
std::is_same<decltype(std::FUNC((T)0)), \
math_unary_function_return_type_t<T>>::value); \
return std::FUNC(x); \
} \
MATHEMATICAL_FUNCTIONS_TEST_UNREACHABLE \
} \
static KOKKOS_FUNCTION int ulp_factor() { return ULP_FACTOR; } \
}; \
using kk_##FUNC = MathUnaryFunction_##FUNC; \
template <> \
struct math_function_name<MathUnaryFunction_##FUNC> { \
static constexpr char name[] = #FUNC; \
}; \
constexpr char math_function_name<MathUnaryFunction_##FUNC>::name[]
#define DEFINE_UNARY_FUNCTION_EVAL_CUSTOM(FUNC, ULP_FACTOR, REF_FUNC) \
struct MathUnaryFunction_##FUNC { \
template <typename T> \
static KOKKOS_FUNCTION auto eval(T x) { \
static_assert( \
std::is_same<decltype(Kokkos::FUNC((T)0)), \
math_unary_function_return_type_t<T>>::value); \
return Kokkos::FUNC(x); \
} \
template <typename T> \
static auto eval_std(T x) { \
static_assert( \
std::is_same<decltype(REF_FUNC), \
math_unary_function_return_type_t<T>>::value); \
return REF_FUNC; \
} \
static KOKKOS_FUNCTION int ulp_factor() { return ULP_FACTOR; } \
}; \
using kk_##FUNC = MathUnaryFunction_##FUNC; \
template <> \
struct math_function_name<MathUnaryFunction_##FUNC> { \
static constexpr char name[] = #FUNC; \
}; \
constexpr char math_function_name<MathUnaryFunction_##FUNC>::name[]
#ifndef KOKKOS_MATHEMATICAL_FUNCTIONS_SKIP_3
// Generally the expected ULP error should come from here:
// https://www.gnu.org/software/libc/manual/html_node/Errors-in-Math-Functions.html
// For now 1s largely seem to work ...
DEFINE_UNARY_FUNCTION_EVAL(exp, 2);
DEFINE_UNARY_FUNCTION_EVAL(exp2, 2);
DEFINE_UNARY_FUNCTION_EVAL(expm1, 2);
DEFINE_UNARY_FUNCTION_EVAL(log, 2);
DEFINE_UNARY_FUNCTION_EVAL(log10, 2);
DEFINE_UNARY_FUNCTION_EVAL(log2, 2);
DEFINE_UNARY_FUNCTION_EVAL(log1p, 2);
#endif
#ifndef KOKKOS_MATHEMATICAL_FUNCTIONS_SKIP_1
DEFINE_UNARY_FUNCTION_EVAL(sqrt, 2);
DEFINE_UNARY_FUNCTION_EVAL(cbrt, 2);
DEFINE_UNARY_FUNCTION_EVAL(sin, 2);
DEFINE_UNARY_FUNCTION_EVAL(cos, 2);
DEFINE_UNARY_FUNCTION_EVAL(tan, 2);
DEFINE_UNARY_FUNCTION_EVAL(asin, 2);
DEFINE_UNARY_FUNCTION_EVAL(acos, 2);
DEFINE_UNARY_FUNCTION_EVAL(atan, 2);
DEFINE_UNARY_FUNCTION_EVAL(sinh, 2);
DEFINE_UNARY_FUNCTION_EVAL(cosh, 2);
DEFINE_UNARY_FUNCTION_EVAL(tanh, 2);
DEFINE_UNARY_FUNCTION_EVAL(asinh, 4);
DEFINE_UNARY_FUNCTION_EVAL(acosh, 2);
DEFINE_UNARY_FUNCTION_EVAL(atanh, 2);
// non-standard math functions
DEFINE_UNARY_FUNCTION_EVAL_CUSTOM(rsqrt, 2,
decltype(std::sqrt(x))(1) / std::sqrt(x));
#endif
#ifndef KOKKOS_MATHEMATICAL_FUNCTIONS_SKIP_2
#if defined(__APPLE__)
// Apple's standard library implementation seems to have a poor implementation
DEFINE_UNARY_FUNCTION_EVAL(erf, 5);
#else
DEFINE_UNARY_FUNCTION_EVAL(erf, 2);
#endif
DEFINE_UNARY_FUNCTION_EVAL(erfc, 5);
// has a larger error due to some impls doing integer exact.
// We cast always to double leading to larger difference when comparing our
// tgamma to std::tgamma on the host.
DEFINE_UNARY_FUNCTION_EVAL(tgamma, 200);
DEFINE_UNARY_FUNCTION_EVAL(lgamma, 2);
DEFINE_UNARY_FUNCTION_EVAL(ceil, 2);
DEFINE_UNARY_FUNCTION_EVAL(floor, 2);
DEFINE_UNARY_FUNCTION_EVAL(trunc, 2);
DEFINE_UNARY_FUNCTION_EVAL(round, 1);
#ifndef KOKKOS_ENABLE_SYCL
DEFINE_UNARY_FUNCTION_EVAL(nearbyint, 2);
#endif
DEFINE_UNARY_FUNCTION_EVAL(logb, 2);
#endif
#undef DEFINE_UNARY_FUNCTION_EVAL
#define DEFINE_BINARY_FUNCTION_EVAL(FUNC, ULP_FACTOR) \
struct MathBinaryFunction_##FUNC { \
template <typename T, typename U> \
static KOKKOS_FUNCTION auto eval(T x, U y) { \
static_assert( \
std::is_same<decltype(Kokkos::FUNC((T)0, (U)0)), \
math_binary_function_return_type_t<T, U>>::value); \
return Kokkos::FUNC(x, y); \
} \
template <typename T, typename U> \
static auto eval_std(T x, U y) { \
constexpr bool const x_is_half = \
(KE::Impl::is_float16<T>::value || KE::Impl::is_bfloat16<T>::value); \
constexpr bool const y_is_half = \
(KE::Impl::is_float16<U>::value || KE::Impl::is_bfloat16<U>::value); \
if constexpr (x_is_half && y_is_half) \
return std::FUNC(static_cast<float>(x), static_cast<float>(y)); \
else if constexpr (x_is_half) \
return std::FUNC(static_cast<float>(x), y); \
else if constexpr (y_is_half) \
return std::FUNC(x, static_cast<float>(y)); \
else { \
static_assert( \
std::is_same<decltype(std::FUNC((T)0, (U)0)), \
math_binary_function_return_type_t<T, U>>::value); \
return std::FUNC(x, y); \
} \
MATHEMATICAL_FUNCTIONS_TEST_UNREACHABLE \
} \
static KOKKOS_FUNCTION int ulp_factor() { return ULP_FACTOR; } \
}; \
using kk_##FUNC = MathBinaryFunction_##FUNC; \
template <> \
struct math_function_name<MathBinaryFunction_##FUNC> { \
static constexpr char name[] = #FUNC; \
}; \
constexpr char math_function_name<MathBinaryFunction_##FUNC>::name[]
#ifndef KOKKOS_MATHEMATICAL_FUNCTIONS_SKIP_1
DEFINE_BINARY_FUNCTION_EVAL(pow, 2);
DEFINE_BINARY_FUNCTION_EVAL(hypot, 2);
#endif
#ifndef KOKKOS_MATHEMATICAL_FUNCTIONS_SKIP_2
DEFINE_BINARY_FUNCTION_EVAL(nextafter, 1);
DEFINE_BINARY_FUNCTION_EVAL(copysign, 1);
#endif
#undef DEFINE_BINARY_FUNCTION_EVAL
#define DEFINE_TERNARY_FUNCTION_EVAL(FUNC, ULP_FACTOR) \
struct MathTernaryFunction_##FUNC { \
template <typename T, typename U, typename V> \
static KOKKOS_FUNCTION auto eval(T x, U y, V z) { \
static_assert( \
std::is_same<decltype(Kokkos::FUNC((T)0, (U)0, (V)0)), \
math_ternary_function_return_type_t<T, U, V>>::value); \
return Kokkos::FUNC(x, y, z); \
} \
template <typename T, typename U, typename V> \
static auto eval_std(T x, U y, V z) { \
static_assert( \
std::is_same<decltype(std::FUNC((T)0, (U)0, (V)0)), \
math_ternary_function_return_type_t<T, U, V>>::value); \
return std::FUNC(x, y, z); \
} \
static KOKKOS_FUNCTION int ulp_factor() { return ULP_FACTOR; } \
}; \
using kk3_##FUNC = MathTernaryFunction_##FUNC; \
template <> \
struct math_function_name<MathTernaryFunction_##FUNC> { \
static constexpr char name[] = #FUNC; \
}; \
constexpr char math_function_name<MathTernaryFunction_##FUNC>::name[]
#ifndef KOKKOS_MATHEMATICAL_FUNCTIONS_SKIP_1
DEFINE_TERNARY_FUNCTION_EVAL(hypot, 2);
DEFINE_TERNARY_FUNCTION_EVAL(fma, 2);
#endif
#undef DEFINE_TERNARY_FUNCTION_EVAL
// clang-format off
template <class>
struct type_helper;
#define DEFINE_TYPE_NAME(T) \
template <> struct type_helper<T> { static char const * name() { return #T; } };
DEFINE_TYPE_NAME(bool)
DEFINE_TYPE_NAME(int)
DEFINE_TYPE_NAME(long)
DEFINE_TYPE_NAME(long long)
DEFINE_TYPE_NAME(unsigned int)
DEFINE_TYPE_NAME(unsigned long)
DEFINE_TYPE_NAME(unsigned long long)
#if defined(KOKKOS_HALF_T_IS_FLOAT) && !KOKKOS_HALF_T_IS_FLOAT
DEFINE_TYPE_NAME(KE::half_t)
#endif
#if defined(KOKKOS_BHALF_T_IS_FLOAT) && !KOKKOS_BHALF_T_IS_FLOAT
DEFINE_TYPE_NAME(KE::bhalf_t)
#endif
DEFINE_TYPE_NAME(float)
DEFINE_TYPE_NAME(double)
DEFINE_TYPE_NAME(long double)
#undef DEFINE_TYPE_NAME
// clang-format on
template <class Space, class Func, class Arg, std::size_t N,
class Ret = math_unary_function_return_type_t<Arg>>
struct TestMathUnaryFunction : FloatingPointComparison {
Arg val_[N];
Ret res_[N];
TestMathUnaryFunction(const Arg (&val)[N]) {
std::copy(val, val + N, val_);
std::transform(val, val + N, res_,
[](auto x) { return Func::eval_std(x); });
run();
}
void run() {
int errors = 0;
Kokkos::parallel_reduce(Kokkos::RangePolicy<Space>(0, N), *this, errors);
ASSERT_EQ(errors, 0) << "Failed check no error for "
<< math_function_name<Func>::name << "("
<< type_helper<Arg>::name() << ")";
}
KOKKOS_FUNCTION void operator()(int i, int& e) const {
bool ar = compare(Func::eval(val_[i]), res_[i], Func::ulp_factor());
if (!ar) {
++e;
Kokkos::printf("value at %f which is %f was expected to be %f\n",
(double)val_[i], (double)Func::eval(val_[i]),
(double)res_[i]);
}
}
};
template <class Space, class... Func, class Arg, std::size_t N>
void do_test_math_unary_function(const Arg (&x)[N]) {
(void)std::initializer_list<int>{
(TestMathUnaryFunction<Space, Func, Arg, N>(x), 0)...};
// test if potentially device specific math functions also work on host
if constexpr (!std::is_same_v<Space, Kokkos::DefaultHostExecutionSpace>)
(void)std::initializer_list<int>{
(TestMathUnaryFunction<Kokkos::DefaultHostExecutionSpace, Func, Arg, N>(
x),
0)...};
}
#define TEST_MATH_FUNCTION(FUNC) \
do_test_math_unary_function<TEST_EXECSPACE, MathUnaryFunction_##FUNC>
template <class Half, class Space, class... Func, class Arg, std::size_t N>
void do_test_half_math_unary_function(const Arg (&x)[N]) {
Half y[N];
std::copy(x, x + N, y); // cast to array of half type
(void)std::initializer_list<int>{
(TestMathUnaryFunction<Space, Func, Half, N>(y), 0)...};
// test if potentially device specific math functions also work on host
if constexpr (!std::is_same_v<Space, Kokkos::DefaultHostExecutionSpace>)
(void)std::initializer_list<int>{(
TestMathUnaryFunction<Kokkos::DefaultHostExecutionSpace, Func, Half, N>(
y),
0)...};
}
#define TEST_HALF_MATH_FUNCTION(FUNC, T) \
do_test_half_math_unary_function<T, TEST_EXECSPACE, MathUnaryFunction_##FUNC>
template <class Space, class Func, class Arg1, class Arg2,
class Ret = math_binary_function_return_type_t<Arg1, Arg2>>
struct TestMathBinaryFunction : FloatingPointComparison {
Arg1 val1_;
Arg2 val2_;
Ret res_;
TestMathBinaryFunction(Arg1 val1, Arg2 val2)
: val1_(val1), val2_(val2), res_(Func::eval_std(val1, val2)) {
run();
}
void run() {
int errors = 0;
Kokkos::parallel_reduce(Kokkos::RangePolicy<Space>(0, 1), *this, errors);
ASSERT_EQ(errors, 0) << "Failed check no error for "
<< math_function_name<Func>::name << "("
<< type_helper<Arg1>::name() << ", "
<< type_helper<Arg2>::name() << ")";
}
KOKKOS_FUNCTION void operator()(int, int& e) const {
bool ar = compare(Func::eval(val1_, val2_), res_, Func::ulp_factor());
if (!ar) {
++e;
Kokkos::printf("value at %f, %f which is %f was expected to be %f\n",
(double)val1_, (double)val2_,
(double)Func::eval(val1_, val2_), (double)res_);
}
}
};
template <class Space, class... Func, class Arg1, class Arg2>
void do_test_math_binary_function(Arg1 arg1, Arg2 arg2) {
(void)std::initializer_list<int>{
(TestMathBinaryFunction<Space, Func, Arg1, Arg2>(arg1, arg2), 0)...};
}
template <class Space, class Func, class Arg1, class Arg2, class Arg3,
class Ret = math_ternary_function_return_type_t<Arg1, Arg2, Arg3>>
struct TestMathTernaryFunction : FloatingPointComparison {
Arg1 val1_;
Arg2 val2_;
Arg3 val3_;
Ret res_;
TestMathTernaryFunction(Arg1 val1, Arg2 val2, Arg3 val3)
: val1_(val1),
val2_(val2),
val3_(val3),
res_(Func::eval_std(val1, val2, val3)) {
run();
}
void run() {
int errors = 0;
Kokkos::parallel_reduce(Kokkos::RangePolicy<Space>(0, 1), *this, errors);
ASSERT_EQ(errors, 0) << "Failed check no error for "
<< math_function_name<Func>::name << "("
<< type_helper<Arg1>::name() << ", "
<< type_helper<Arg1>::name() << ", "
<< type_helper<Arg3>::name() << ")";
}
KOKKOS_FUNCTION void operator()(int, int& e) const {
bool ar =
compare(Func::eval(val1_, val2_, val3_), res_, Func::ulp_factor());
if (!ar) {
++e;
Kokkos::printf("value at %f, %f, %f which is %f was expected to be %f\n",
(double)val1_, (double)val2_, (double)val3_,
(double)Func::eval(val1_, val2_, val3_), (double)res_);
}
}
};
template <class Space, class... Func, class Arg1, class Arg2, class Arg3>
void do_test_math_ternary_function(Arg1 arg1, Arg2 arg2, Arg3 arg3) {
(void)std::initializer_list<int>{
(TestMathTernaryFunction<Space, Func, Arg1, Arg2, Arg3>(arg1, arg2, arg3),
0)...};
}
#ifndef KOKKOS_MATHEMATICAL_FUNCTIONS_SKIP_1
TEST(TEST_CATEGORY, mathematical_functions_trigonometric_functions) {
TEST_MATH_FUNCTION(sin)({true, false});
TEST_MATH_FUNCTION(sin)({-3, -2, -1, 0, 1});
TEST_MATH_FUNCTION(sin)({-3l, -2l, -1l, 0l, 1l});
TEST_MATH_FUNCTION(sin)({-3ll, -2ll, -1ll, 0ll, 1ll});
TEST_MATH_FUNCTION(sin)({2u, 3u, 4u, 5u, 6u});
TEST_MATH_FUNCTION(sin)({2ul, 3ul, 4ul, 5ul, 6ul});
TEST_MATH_FUNCTION(sin)({2ull, 3ull, 4ull, 5ull, 6ull});
TEST_HALF_MATH_FUNCTION(sin, KE::half_t)({.1f, .2f, .3f});
TEST_HALF_MATH_FUNCTION(sin, KE::bhalf_t)({.1f, .2f, .3f});
TEST_MATH_FUNCTION(sin)({.1f, .2f, .3f});
TEST_MATH_FUNCTION(sin)({.4, .5, .6});
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
TEST_MATH_FUNCTION(sin)({.7l, .8l, .9l});
#endif
TEST_MATH_FUNCTION(cos)({true, false});
TEST_MATH_FUNCTION(cos)({-3, -2, -1, 0, 1});
TEST_MATH_FUNCTION(cos)({-3l, -2l, -1l, 0l, 1l});
TEST_MATH_FUNCTION(cos)({-3ll, -2ll, -1ll, 0ll, 1ll});
TEST_MATH_FUNCTION(cos)({2u, 3u, 4u, 5u, 6u});
TEST_MATH_FUNCTION(cos)({2ul, 3ul, 4ul, 5ul, 6ul});
TEST_MATH_FUNCTION(cos)({2ull, 3ull, 4ull, 5ull, 6ull});
TEST_HALF_MATH_FUNCTION(cos, KE::half_t)({.1f, .2f, .3f});
TEST_HALF_MATH_FUNCTION(cos, KE::bhalf_t)({.1f, .2f, .3f});
TEST_MATH_FUNCTION(cos)({.1f, .2f, .3f});
TEST_MATH_FUNCTION(cos)({.4, .5, .6});
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
TEST_MATH_FUNCTION(cos)({.7l, .8l, .9l});
#endif
TEST_MATH_FUNCTION(tan)({true, false});
TEST_MATH_FUNCTION(tan)({-3, -2, -1, 0, 1});
TEST_MATH_FUNCTION(tan)({-3l, -2l, -1l, 0l, 1l});
TEST_MATH_FUNCTION(tan)({-3ll, -2ll, -1ll, 0ll, 1ll});
TEST_MATH_FUNCTION(tan)({2u, 3u, 4u, 5u, 6u});
TEST_MATH_FUNCTION(tan)({2ul, 3ul, 4ul, 5ul, 6ul});
TEST_MATH_FUNCTION(tan)({2ull, 3ull, 4ull, 5ull, 6ull});
TEST_HALF_MATH_FUNCTION(tan, KE::half_t)({.1f, .2f, .3f});
TEST_HALF_MATH_FUNCTION(tan, KE::bhalf_t)({.1f, .2f, .3f});
TEST_MATH_FUNCTION(tan)({.1f, .2f, .3f});
TEST_MATH_FUNCTION(tan)({.4, .5, .6});
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
TEST_MATH_FUNCTION(tan)({.7l, .8l, .9l});
#endif
TEST_MATH_FUNCTION(asin)({true, false});
TEST_MATH_FUNCTION(asin)({-1, 0, 1});
TEST_MATH_FUNCTION(asin)({-1l, 0l, 1l});
TEST_MATH_FUNCTION(asin)({-1ll, 0ll, 1ll});
TEST_MATH_FUNCTION(asin)({0u, 1u});
TEST_MATH_FUNCTION(asin)({0ul, 1ul});
TEST_MATH_FUNCTION(asin)({0ull, 1ull});
TEST_HALF_MATH_FUNCTION(asin, KE::half_t)({-1.f, .9f, -.8f, .7f, -.6f});
TEST_HALF_MATH_FUNCTION(asin, KE::bhalf_t)({-1.f, .9f, -.8f, .7f, -.6f});
TEST_MATH_FUNCTION(asin)({-1.f, .9f, -.8f, .7f, -.6f});
TEST_MATH_FUNCTION(asin)({-.5, .4, -.3, .2, -.1, 0.});
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
TEST_MATH_FUNCTION(asin)({-.5l, .3l, 0.l, .2l, .4l, .6l});
#endif
TEST_MATH_FUNCTION(acos)({true, false});
TEST_MATH_FUNCTION(acos)({-1, 0, 1});
TEST_MATH_FUNCTION(acos)({-1l, 0l, 1l});
TEST_MATH_FUNCTION(acos)({-1ll, 0ll, 1ll});
TEST_MATH_FUNCTION(acos)({0u, 1u});
TEST_MATH_FUNCTION(acos)({0ul, 1ul});
TEST_MATH_FUNCTION(acos)({0ull, 1ull});
TEST_HALF_MATH_FUNCTION(acos, KE::half_t)({-1.f, .9f, -.8f, .7f, -.6f});
TEST_HALF_MATH_FUNCTION(acos, KE::bhalf_t)({-1.f, .9f, -.8f, .7f, -.6f});
TEST_MATH_FUNCTION(acos)({-1.f, .9f, -.8f, .7f, -.6f});
TEST_MATH_FUNCTION(acos)({-.5, .4, -.3, .2, -.1, 0.});
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
TEST_MATH_FUNCTION(acos)({-.5l, .3l, 0.l, .2l, .4l, .6l});
#endif
TEST_MATH_FUNCTION(atan)({true, false});
TEST_MATH_FUNCTION(atan)({-1, 0, 1});
TEST_MATH_FUNCTION(atan)({-1l, 0l, 1l});
TEST_MATH_FUNCTION(atan)({-1ll, 0ll, 1ll});
TEST_MATH_FUNCTION(atan)({0u, 1u});
TEST_MATH_FUNCTION(atan)({0ul, 1ul});
TEST_MATH_FUNCTION(atan)({0ull, 1ull});
TEST_HALF_MATH_FUNCTION(atan, KE::half_t)
({-1.5f, 1.3f, -1.1f, .9f, -.7f, .5f});
TEST_HALF_MATH_FUNCTION(atan, KE::bhalf_t)
({-1.5f, 1.3f, -1.1f, .9f, -.7f, .5f});
TEST_MATH_FUNCTION(atan)({-1.5f, 1.3f, -1.1f, .9f, -.7f, .5f});
TEST_MATH_FUNCTION(atan)({1.4, -1.2, 1., -.8, .6, -.4, .2, -0.});
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
TEST_MATH_FUNCTION(atan)({-.98l, .67l, -54.l, .34l, -.21l});
#endif
// TODO atan2
}
TEST(TEST_CATEGORY, mathematical_functions_power_functions) {
TEST_MATH_FUNCTION(sqrt)({0, 1, 2, 3, 5, 7, 11});
TEST_MATH_FUNCTION(sqrt)({0l, 1l, 2l, 3l, 5l, 7l, 11l});
TEST_MATH_FUNCTION(sqrt)({0ll, 1ll, 2ll, 3ll, 5ll, 7ll, 11ll});
TEST_MATH_FUNCTION(sqrt)({0u, 1u, 2u, 3u, 5u, 7u});
TEST_MATH_FUNCTION(sqrt)({0ul, 1ul, 2ul, 3ul, 5ul, 7ul});
TEST_MATH_FUNCTION(sqrt)({0ull, 1ull, 2ull, 3ull, 5ull, 7ull});
TEST_HALF_MATH_FUNCTION(sqrt, KE::half_t)({10.f, 20.f, 30.f, 40.f});
TEST_HALF_MATH_FUNCTION(sqrt, KE::bhalf_t)({10.f, 20.f, 30.f, 40.f});
TEST_MATH_FUNCTION(sqrt)({10.f, 20.f, 30.f, 40.f});
TEST_MATH_FUNCTION(sqrt)({11.1, 22.2, 33.3, 44.4});
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
TEST_MATH_FUNCTION(sqrt)({10.l, 20.l, 30.l, 40.l});
#endif
TEST_MATH_FUNCTION(cbrt)({-5, -3, -1, 2, 4, 6});
TEST_MATH_FUNCTION(cbrt)({-5l, -3l, -1l, 2l, 4l, 6l});
TEST_MATH_FUNCTION(cbrt)({-5ll, -3ll, -1ll, 2ll, 4ll, 6ll});
TEST_MATH_FUNCTION(cbrt)({0u, 1u, 2u, 3u, 4u, 5u});
TEST_MATH_FUNCTION(cbrt)({0ul, 1ul, 2ul, 3ul, 4ul, 5ul});
TEST_MATH_FUNCTION(cbrt)({0ull, 1ull, 2ull, 3ull, 4ull, 5ull});
TEST_HALF_MATH_FUNCTION(cbrt, KE::half_t)({-1.f, .2f, -3.f, .4f, -5.f});
TEST_HALF_MATH_FUNCTION(cbrt, KE::bhalf_t)({-1.f, .2f, -3.f, .4f, -5.f});
TEST_MATH_FUNCTION(cbrt)({-1.f, .2f, -3.f, .4f, -5.f});
TEST_MATH_FUNCTION(cbrt)({11.1, -2.2, 33.3, -4.4, 55.5});
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
TEST_MATH_FUNCTION(cbrt)({-10.l, 20.l, -30.l, 40.l, -50.l});
#endif
do_test_math_binary_function<TEST_EXECSPACE, kk_pow>(
static_cast<KE::half_t>(2.f), static_cast<KE::half_t>(3.f));
do_test_math_binary_function<TEST_EXECSPACE, kk_pow>(
static_cast<KE::bhalf_t>(2.f), static_cast<KE::bhalf_t>(3.f));
do_test_math_binary_function<TEST_EXECSPACE, kk_pow>(2.f, 3.f);
do_test_math_binary_function<TEST_EXECSPACE, kk_pow>(2.f, 3.f);
do_test_math_binary_function<TEST_EXECSPACE, kk_pow>(2., 3.);
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
do_test_math_binary_function<TEST_EXECSPACE, kk_pow>(2.l, 3.l);
#endif
do_test_math_binary_function<TEST_EXECSPACE, kk_hypot>(
static_cast<KE::half_t>(2.f), static_cast<KE::half_t>(3.f));
do_test_math_binary_function<TEST_EXECSPACE, kk_hypot>(
static_cast<KE::bhalf_t>(2.f), static_cast<KE::bhalf_t>(3.f));
do_test_math_binary_function<TEST_EXECSPACE, kk_hypot>(2.f, 3.f);
do_test_math_binary_function<TEST_EXECSPACE, kk_hypot>(2., 3.);
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
// FIXME: fails with gcc on Power platforms
#if !(defined(KOKKOS_ARCH_POWER8) || defined(KOKKOS_ARCH_POWER9))
do_test_math_binary_function<TEST_EXECSPACE, kk_hypot>(2.l, 3.l);
#endif
#endif
do_test_math_ternary_function<TEST_EXECSPACE, kk3_hypot>(2.f, 3.f, 4.f);
do_test_math_ternary_function<TEST_EXECSPACE, kk3_hypot>(2., 3., 4.);
do_test_math_ternary_function<TEST_EXECSPACE, kk3_hypot>(2, 3.f, 4.);
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
#if !(defined(KOKKOS_ARCH_POWER8) || defined(KOKKOS_ARCH_POWER9))
do_test_math_ternary_function<TEST_EXECSPACE, kk3_hypot>(2.l, 3.l, 4.l);
#endif
#endif
}
TEST(TEST_CATEGORY, mathematical_functions_fma) {
do_test_math_ternary_function<TEST_EXECSPACE, kk3_fma>(2.f, 3.f, 4.f);
do_test_math_ternary_function<TEST_EXECSPACE, kk3_fma>(2., 3., 4.);
do_test_math_ternary_function<TEST_EXECSPACE, kk3_fma>(2, 3.f, 4.);
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
do_test_math_ternary_function<TEST_EXECSPACE, kk3_fma>(2.l, 3.l, 4.l);
#endif
}
#endif
#ifndef KOKKOS_MATHEMATICAL_FUNCTIONS_SKIP_3
TEST(TEST_CATEGORY, mathematical_functions_exponential_functions) {
TEST_MATH_FUNCTION(exp)({-9, -8, -7, -6, -5, 4, 3, 2, 1, 0});
TEST_MATH_FUNCTION(exp)({-9l, -8l, -7l, -6l, -5l, 4l, 3l, 2l, 1l, 0l});
TEST_MATH_FUNCTION(exp)({-9ll, -8ll, -7ll, -6ll, -5ll, 4ll, 3ll, 2ll, 1ll});
TEST_MATH_FUNCTION(exp)({0u, 1u, 2u, 3u, 4u, 5u});
TEST_MATH_FUNCTION(exp)({0ul, 1ul, 2ul, 3ul, 4ul, 5ul});
TEST_MATH_FUNCTION(exp)({0ull, 1ull, 2ull, 3ull, 4ull, 5ull});
TEST_HALF_MATH_FUNCTION(exp, KE::half_t)
({-98.f, -7.6f, -.54f, 3.2f, 1.f, -0.f});
TEST_HALF_MATH_FUNCTION(exp, KE::bhalf_t)
({-98.f, -7.6f, -.54f, 3.2f, 1.f, -0.f});
TEST_MATH_FUNCTION(exp)({-98.f, -7.6f, -.54f, 3.2f, 1.f, -0.f});
TEST_MATH_FUNCTION(exp)({-98., -7.6, -.54, 3.2, 1., -0.});
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
TEST_MATH_FUNCTION(exp)({-98.l, -7.6l, -.54l, 3.2l, 1.l, -0.l});
#endif
TEST_MATH_FUNCTION(exp2)({-9, -8, -7, -6, -5, 4, 3, 2, 1, 0});
TEST_MATH_FUNCTION(exp2)({-9l, -8l, -7l, -6l, -5l, 4l, 3l, 2l, 1l, 0l});
TEST_MATH_FUNCTION(exp2)({-9ll, -8ll, -7ll, -6ll, -5ll, 4ll, 3ll, 2ll, 1ll});
TEST_MATH_FUNCTION(exp2)({0u, 1u, 2u, 3u, 4u, 5u});
TEST_MATH_FUNCTION(exp2)({0ul, 1ul, 2ul, 3ul, 4ul, 5ul});
TEST_MATH_FUNCTION(exp2)({0ull, 1ull, 2ull, 3ull, 4ull, 5ull});
TEST_HALF_MATH_FUNCTION(exp2, KE::half_t)
({-98.f, -7.6f, -.54f, 3.2f, 1.f, -0.f});
TEST_HALF_MATH_FUNCTION(exp2, KE::bhalf_t)
({-98.f, -7.6f, -.54f, 3.2f, 1.f, -0.f});
TEST_MATH_FUNCTION(exp2)({-98.f, -7.6f, -.54f, 3.2f, 1.f, -0.f});
TEST_MATH_FUNCTION(exp2)({-98., -7.6, -.54, 3.2, 1., -0.});
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
TEST_MATH_FUNCTION(exp2)({-98.l, -7.6l, -.54l, 3.2l, 1.l, -0.l});
#endif
TEST_MATH_FUNCTION(expm1)({-9, -8, -7, -6, -5, 4, 3, 2, 1, 0});
TEST_MATH_FUNCTION(expm1)({-9l, -8l, -7l, -6l, -5l, 4l, 3l, 2l, 1l, 0l});
TEST_MATH_FUNCTION(expm1)({-9ll, -8ll, -7ll, -6ll, -5ll, 4ll, 3ll, 2ll, 1ll});
TEST_MATH_FUNCTION(expm1)({0u, 1u, 2u, 3u, 4u, 5u});
TEST_MATH_FUNCTION(expm1)({0ul, 1ul, 2ul, 3ul, 4ul, 5ul});
TEST_MATH_FUNCTION(expm1)({0ull, 1ull, 2ull, 3ull, 4ull, 5ull});
TEST_HALF_MATH_FUNCTION(expm1, KE::half_t)
({-98.f, -7.6f, -.54f, 3.2f, 1.f, -0.f});
TEST_HALF_MATH_FUNCTION(expm1, KE::bhalf_t)
({-98.f, -7.6f, -.54f, 3.2f, 1.f, -0.f});
TEST_MATH_FUNCTION(expm1)({-98.f, -7.6f, -.54f, 3.2f, 1.f, -0.f});
TEST_MATH_FUNCTION(expm1)({-98., -7.6, -.54, 3.2, 1., -0.});
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
TEST_MATH_FUNCTION(expm1)({-98.l, -7.6l, -.54l, 3.2l, 1.l, -0.l});
#endif
TEST_MATH_FUNCTION(log)({1, 23, 456, 7890});
TEST_MATH_FUNCTION(log)({1l, 23l, 456l, 7890l});
TEST_MATH_FUNCTION(log)({1ll, 23ll, 456ll, 7890ll});
TEST_MATH_FUNCTION(log)({1u, 23u, 456u, 7890u});
TEST_MATH_FUNCTION(log)({1ul, 23ul, 456ul, 7890ul});
TEST_MATH_FUNCTION(log)({1ull, 23ull, 456ull, 7890ull});
TEST_HALF_MATH_FUNCTION(log, KE::half_t)({1234.f, 567.f, 89.f, .1f});
TEST_HALF_MATH_FUNCTION(log, KE::bhalf_t)({1234.f, 567.f, 89.f, .1f});
TEST_MATH_FUNCTION(log)({1234.f, 567.f, 89.f, .1f});
TEST_MATH_FUNCTION(log)({1234., 567., 89., .02});
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
TEST_MATH_FUNCTION(log)({1234.l, 567.l, 89.l, .003l});
#endif
TEST_MATH_FUNCTION(log10)({1, 23, 456, 7890});
TEST_MATH_FUNCTION(log10)({1l, 23l, 456l, 7890l});
TEST_MATH_FUNCTION(log10)({1ll, 23ll, 456ll, 7890ll});
TEST_MATH_FUNCTION(log10)({1u, 23u, 456u, 7890u});
TEST_MATH_FUNCTION(log10)({1ul, 23ul, 456ul, 7890ul});
TEST_MATH_FUNCTION(log10)({1ull, 23ull, 456ull, 7890ull});
TEST_HALF_MATH_FUNCTION(log10, KE::half_t)({1234.f, 567.f, 89.f, .1f});
TEST_HALF_MATH_FUNCTION(log10, KE::bhalf_t)({1234.f, 567.f, 89.f, .1f});
TEST_MATH_FUNCTION(log10)({1234.f, 567.f, 89.f, .1f});
TEST_MATH_FUNCTION(log10)({1234., 567., 89., .02});
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
TEST_MATH_FUNCTION(log10)({1234.l, 567.l, 89.l, .003l});
#endif
// FIXME_OPENMPTARGET FIXME_AMD
#if defined(KOKKOS_ENABLE_OPENMPTARGET) && \
(defined(KOKKOS_ARCH_AMD_GFX906) || defined(KOKKOS_ARCH_AMD_GFX908) || \
defined(KOKKOS_ARCH_AMD_GFX90A) || defined(KOKKOS_ARCH_AMD_GFX942))
TEST_MATH_FUNCTION(log2)({1, 23, 456, 7890});
#endif
TEST_MATH_FUNCTION(log2)({1l, 23l, 456l, 7890l});
TEST_MATH_FUNCTION(log2)({1ll, 23ll, 456ll, 7890ll});
TEST_MATH_FUNCTION(log2)({1u, 23u, 456u, 7890u});
TEST_MATH_FUNCTION(log2)({1ul, 23ul, 456ul, 7890ul});
TEST_MATH_FUNCTION(log2)({1ull, 23ull, 456ull, 7890ull});
TEST_HALF_MATH_FUNCTION(log2, KE::half_t)({1234.f, 567.f, 89.f, .1f});
TEST_HALF_MATH_FUNCTION(log2, KE::bhalf_t)({1234.f, 567.f, 89.f, .1f});
TEST_MATH_FUNCTION(log2)({1234.f, 567.f, 89.f, .1f});
TEST_MATH_FUNCTION(log2)({1234., 567., 89., .02});
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
TEST_MATH_FUNCTION(log2)({1234.l, 567.l, 89.l, .003l});
#endif
TEST_MATH_FUNCTION(log1p)({1, 23, 456, 7890, 0});
TEST_MATH_FUNCTION(log1p)({1l, 23l, 456l, 7890l, 0l});
TEST_MATH_FUNCTION(log1p)({1ll, 23ll, 456ll, 7890ll, 0ll});
TEST_MATH_FUNCTION(log1p)({1u, 23u, 456u, 7890u, 0u});
TEST_MATH_FUNCTION(log1p)({1ul, 23ul, 456ul, 7890ul, 0ul});
TEST_MATH_FUNCTION(log1p)({1ull, 23ull, 456ull, 7890ull, 0ull});
TEST_HALF_MATH_FUNCTION(log1p, KE::half_t)({1234.f, 567.f, 89.f, -.9f});
TEST_HALF_MATH_FUNCTION(log1p, KE::bhalf_t)({1234.f, 567.f, 89.f, -.9f});
TEST_MATH_FUNCTION(log1p)({1234.f, 567.f, 89.f, -.9f});
TEST_MATH_FUNCTION(log1p)({1234., 567., 89., -.08});
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
TEST_MATH_FUNCTION(log1p)({1234.l, 567.l, 89.l, -.007l});
#endif
}
#endif
#ifndef KOKKOS_MATHEMATICAL_FUNCTIONS_SKIP_1
TEST(TEST_CATEGORY, mathematical_functions_hyperbolic_functions) {
TEST_MATH_FUNCTION(sinh)({-3, -2, -1, 0, 1});
TEST_MATH_FUNCTION(sinh)({-3l, -2l, -1l, 0l, 1l});
TEST_MATH_FUNCTION(sinh)({-3ll, -2ll, -1ll, 0ll, 1ll});
TEST_MATH_FUNCTION(sinh)({2u, 3u, 4u, 5u, 6u});
TEST_MATH_FUNCTION(sinh)({2ul, 3ul, 4ul, 5ul, 6ul});
TEST_MATH_FUNCTION(sinh)({2ull, 3ull, 4ull, 5ull, 6ull});
TEST_HALF_MATH_FUNCTION(sinh, KE::half_t)({.1f, -2.f, 3.f});
TEST_HALF_MATH_FUNCTION(sinh, KE::bhalf_t)({.1f, -2.f, 3.f});
TEST_MATH_FUNCTION(sinh)({.1f, -2.f, 3.f});
TEST_MATH_FUNCTION(sinh)({-4., .5, -.6});
#ifdef MATHEMATICAL_FUNCTIONS_HAVE_LONG_DOUBLE_OVERLOADS
TEST_MATH_FUNCTION(sinh)({.7l, .8l, .9l});
#endif
TEST_MATH_FUNCTION(cosh)({-3, -2, -1, 0, 1});
TEST_MATH_FUNCTION(cosh)({-3l, -2l, -1l, 0l, 1l});
TEST_MATH_FUNCTION(cosh)({-3ll, -2ll, -1ll, 0ll, 1ll});
TEST_MATH_FUNCTION(cosh)({2u, 3u, 4u, 5u, 6u});
TEST_MATH_FUNCTION(cosh)({2ul, 3ul, 4ul, 5ul, 6ul});
TEST_MATH_FUNCTION(cosh)({2ull, 3ull, 4ull, 5ull, 6ull});
TEST_HALF_MATH_FUNCTION(cosh, KE::half_t)({.1f, -2.f, 3.f});
TEST_HALF_MATH_FUNCTION(cosh, KE::bhalf_t)({.1f, -2.f, 3.f});