-
Notifications
You must be signed in to change notification settings - Fork 1
/
sycl_ext_complex.hpp
2209 lines (1897 loc) · 82.8 KB
/
sycl_ext_complex.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
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Adapted from the LLVM Project, under the Apache License v2.0 with LLVM
// Exceptions. See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef _SYCL_EXT_CPLX_COMPLEX
#define _SYCL_EXT_CPLX_COMPLEX
// clang-format off
// Last time synced SyclCPLX with llvm-project
// https://github.com/llvm/llvm-project/commit/385cc25a531a72c393cee44689e2c3194615bcec
/*
complex synopsis
namespace sycl::ext::cplx
{
template<class T>
class complex
{
public:
typedef T value_type;
complex(const T& re = T(), const T& im = T()); // constexpr in C++14
complex(const complex&); // constexpr in C++14
template<class X> complex(const complex<X>&); // constexpr in C++14
T real() const; // constexpr in C++14
T imag() const; // constexpr in C++14
void real(T);
void imag(T);
complex<T>& operator= (const T&);
complex<T>& operator+=(const T&);
complex<T>& operator-=(const T&);
complex<T>& operator*=(const T&);
complex<T>& operator/=(const T&);
complex& operator=(const complex&);
template<class X> complex<T>& operator= (const complex<X>&);
template<class X> complex<T>& operator+=(const complex<X>&);
template<class X> complex<T>& operator-=(const complex<X>&);
template<class X> complex<T>& operator*=(const complex<X>&);
template<class X> complex<T>& operator/=(const complex<X>&);
};
template<>
class complex<sycl::half>
{
public:
typedef sycl::half value_type;
constexpr complex(sycl::half re = 0.0f, sycl::half im = 0.0f);
explicit constexpr complex(const complex<float>&);
explicit constexpr complex(const complex<double>&);
constexpr operator std::complex<sycl::half>();
constexpr sycl::half real() const;
void real(sycl::half);
constexpr sycl::half imag() const;
void imag(sycl::half);
complex<sycl::half>& operator= (sycl::half);
complex<sycl::half>& operator+=(sycl::half);
complex<sycl::half>& operator-=(sycl::half);
complex<sycl::half>& operator*=(sycl::half);
complex<sycl::half>& operator/=(sycl::half);
complex<sycl::half>& operator=(const complex<sycl::half>&);
template<class X> complex<sycl::half>& operator= (const complex<X>&);
template<class X> complex<sycl::half>& operator+=(const complex<X>&);
template<class X> complex<sycl::half>& operator-=(const complex<X>&);
template<class X> complex<sycl::half>& operator*=(const complex<X>&);
template<class X> complex<sycl::half>& operator/=(const complex<X>&);
};
template<>
class complex<float>
{
public:
typedef float value_type;
constexpr complex(float re = 0.0f, float im = 0.0f);
constexpr complex(const complex<sycl::half>&);
explicit constexpr complex(const complex<double>&);
constexpr complex(const std::complex<float>&);
constexpr operator std::complex<float>();
constexpr float real() const;
void real(float);
constexpr float imag() const;
void imag(float);
complex<float>& operator= (float);
complex<float>& operator+=(float);
complex<float>& operator-=(float);
complex<float>& operator*=(float);
complex<float>& operator/=(float);
complex<float>& operator=(const complex<float>&);
template<class X> complex<float>& operator= (const complex<X>&);
template<class X> complex<float>& operator+=(const complex<X>&);
template<class X> complex<float>& operator-=(const complex<X>&);
template<class X> complex<float>& operator*=(const complex<X>&);
template<class X> complex<float>& operator/=(const complex<X>&);
};
template<>
class complex<double>
{
public:
typedef double value_type;
constexpr complex(double re = 0.0, double im = 0.0);
constexpr complex(const complex<sycl::half>&);
constexpr complex(const complex<float>&);
constexpr complex(const std::complex<double>&);
constexpr operator std::complex<double>();
constexpr double real() const;
void real(double);
constexpr double imag() const;
void imag(double);
complex<double>& operator= (double);
complex<double>& operator+=(double);
complex<double>& operator-=(double);
complex<double>& operator*=(double);
complex<double>& operator/=(double);
complex<double>& operator=(const complex<double>&);
template<class X> complex<double>& operator= (const complex<X>&);
template<class X> complex<double>& operator+=(const complex<X>&);
template<class X> complex<double>& operator-=(const complex<X>&);
template<class X> complex<double>& operator*=(const complex<X>&);
template<class X> complex<double>& operator/=(const complex<X>&);
};
// 26.3.6 operators:
template<class T> complex<T> operator+(const complex<T>&, const complex<T>&);
template<class T> complex<T> operator+(const complex<T>&, const T&);
template<class T> complex<T> operator+(const T&, const complex<T>&);
template<class T> complex<T> operator-(const complex<T>&, const complex<T>&);
template<class T> complex<T> operator-(const complex<T>&, const T&);
template<class T> complex<T> operator-(const T&, const complex<T>&);
template<class T> complex<T> operator*(const complex<T>&, const complex<T>&);
template<class T> complex<T> operator*(const complex<T>&, const T&);
template<class T> complex<T> operator*(const T&, const complex<T>&);
template<class T> complex<T> operator/(const complex<T>&, const complex<T>&);
template<class T> complex<T> operator/(const complex<T>&, const T&);
template<class T> complex<T> operator/(const T&, const complex<T>&);
template<class T> complex<T> operator+(const complex<T>&);
template<class T> complex<T> operator-(const complex<T>&);
template<class T> bool operator==(const complex<T>&, const complex<T>&); // constexpr in C++14
template<class T> bool operator==(const complex<T>&, const T&); // constexpr in C++14
template<class T> bool operator==(const T&, const complex<T>&); // constexpr in C++14
template<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14
template<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14
template<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14
template<class T, class charT, class traits>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>&, complex<T>&);
template<class T, class charT, class traits>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>&, const complex<T>&);
template<class T>
const sycl::stream&
operator<<(const sycl::stream&, const complex<T>&);
// 26.3.7 values:
template<class T> T real(const complex<T>&); // constexpr in C++14
double real(double); // constexpr in C++14
template<Integral T> double real(T); // constexpr in C++14
float real(float); // constexpr in C++14
template<class T> T imag(const complex<T>&); // constexpr in C++14
double imag(double); // constexpr in C++14
template<Integral T> double imag(T); // constexpr in C++14
float imag(float); // constexpr in C++14
template<class T> T abs(const complex<T>&);
template<class T> T arg(const complex<T>&);
double arg(double);
template<Integral T> double arg(T);
float arg(float);
template<class T> T norm(const complex<T>&);
double norm(double);
template<Integral T> double norm(T);
float norm(float);
template<class T> complex<T> conj(const complex<T>&);
complex<double> conj(double);
template<Integral T> complex<double> conj(T);
complex<float> conj(float);
template<class T> complex<T> proj(const complex<T>&);
complex<double> proj(double);
template<Integral T> complex<double> proj(T);
complex<float> proj(float);
template<class T> complex<T> polar(const T&, const T& = T());
// 26.3.8 transcendentals:
template<class T> complex<T> acos(const complex<T>&);
template<class T> complex<T> asin(const complex<T>&);
template<class T> complex<T> atan(const complex<T>&);
template<class T> complex<T> acosh(const complex<T>&);
template<class T> complex<T> asinh(const complex<T>&);
template<class T> complex<T> atanh(const complex<T>&);
template<class T> complex<T> cos (const complex<T>&);
template<class T> complex<T> cosh (const complex<T>&);
template<class T> complex<T> exp (const complex<T>&);
template<class T> complex<T> log (const complex<T>&);
template<class T> complex<T> log10(const complex<T>&);
template<class T> complex<T> pow(const complex<T>&, const T&);
template<class T> complex<T> pow(const complex<T>&, const complex<T>&);
template<class T> complex<T> pow(const T&, const complex<T>&);
template<class T> complex<T> sin (const complex<T>&);
template<class T> complex<T> sinh (const complex<T>&);
template<class T> complex<T> sqrt (const complex<T>&);
template<class T> complex<T> tan (const complex<T>&);
template<class T> complex<T> tanh (const complex<T>&);
} // sycl::ext::cplx
*/
// clang-format on
#ifndef _SYCL_CPLX_NAMESPACE
#ifdef __HIPSYCL__
#define _SYCL_CPLX_NAMESPACE hipsycl::sycl::ext::cplx
#else
#define _SYCL_CPLX_NAMESPACE sycl::ext::cplx
#endif
#endif
#define _SYCL_CPLX_QUALIFY(x) _SYCL_CPLX_NAMESPACE::x
#define _SYCL_EXT_CPLX_BEGIN_NAMESPACE_STD namespace _SYCL_CPLX_NAMESPACE {
#define _SYCL_EXT_CPLX_END_NAMESPACE_STD }
#ifndef _SYCL_MARRAY_NAMESPACE
#ifdef __HIPSYCL__
#define _SYCL_MARRAY_NAMESPACE hipsycl::sycl
#else
#define _SYCL_MARRAY_NAMESPACE sycl
#endif
#endif
#define _SYCL_MARRAY_BEGIN_NAMESPACE namespace _SYCL_MARRAY_NAMESPACE {
#define _SYCL_MARRAY_END_NAMESPACE }
#if defined(__FAST_MATH__) || defined(_M_FP_FAST)
#define _SYCL_EXT_CPLX_FAST_MATH
#endif
#define _SYCL_EXT_CPLX_INLINE_VISIBILITY \
[[gnu::always_inline]] [[clang::always_inline]] inline
#include <complex>
#include <sstream> // for std::basic_ostringstream
#if __has_include(<sycl/sycl.hpp>)
#include <sycl/sycl.hpp>
#elif __has_include(<CL/sycl.hpp>)
// support oneAPI 2022.X and earlier
#include <CL/sycl.hpp>
#else
#error "SYCL header not found"
#endif
#include <type_traits>
_SYCL_EXT_CPLX_BEGIN_NAMESPACE_STD
namespace cplex::detail {
template <class _Tp> struct __numeric_type {
static void __test(...);
static sycl::half __test(sycl::half);
static float __test(float);
static double __test(char);
static double __test(int);
static double __test(unsigned);
static double __test(long);
static double __test(unsigned long);
static double __test(long long);
static double __test(unsigned long long);
static double __test(double);
typedef decltype(__test(std::declval<_Tp>())) type;
static const bool value = !std::is_same<type, void>::value;
};
template <> struct __numeric_type<void> { static const bool value = true; };
template <class _A1, class _A2 = void, class _A3 = void,
bool = __numeric_type<_A1>::value &&__numeric_type<_A2>::value
&&__numeric_type<_A3>::value>
class __promote_imp {
public:
static const bool value = false;
};
template <class _A1, class _A2, class _A3>
class __promote_imp<_A1, _A2, _A3, true> {
private:
typedef typename __promote_imp<_A1>::type __type1;
typedef typename __promote_imp<_A2>::type __type2;
typedef typename __promote_imp<_A3>::type __type3;
public:
typedef decltype(__type1() + __type2() + __type3()) type;
static const bool value = true;
};
template <class _A1, class _A2> class __promote_imp<_A1, _A2, void, true> {
private:
typedef typename __promote_imp<_A1>::type __type1;
typedef typename __promote_imp<_A2>::type __type2;
public:
typedef decltype(__type1() + __type2()) type;
static const bool value = true;
};
template <class _A1> class __promote_imp<_A1, void, void, true> {
public:
typedef typename __numeric_type<_A1>::type type;
static const bool value = true;
};
template <class _A1, class _A2 = void, class _A3 = void>
class __promote : public __promote_imp<_A1, _A2, _A3> {};
// Define our own fast-math aware wrappers for these routines, because
// some compilers are not able to perform the appropriate optimization
// without this extra help.
template <typename T>
_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr bool isnan(const T a) {
#ifdef _SYCL_EXT_CPLX_FAST_MATH
return false;
#else
return sycl::isnan(a);
#endif
}
template <typename T>
_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr bool isfinite(const T a) {
#ifdef _SYCL_EXT_CPLX_FAST_MATH
return true;
#else
return sycl::isfinite(a);
#endif
}
template <typename T>
_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr bool isinf(const T a) {
#ifdef _SYCL_EXT_CPLX_FAST_MATH
return false;
#else
return sycl::isinf(a);
#endif
}
// To ensure loop unrolling is done when processing dimensions.
template <size_t... Inds, class F>
void loop_impl(std::integer_sequence<size_t, Inds...>, F &&f) {
(f(std::integral_constant<size_t, Inds>{}), ...);
}
template <size_t count, class F> void loop(F &&f) {
loop_impl(std::make_index_sequence<count>{}, std::forward<F>(f));
}
} // namespace cplex::detail
////////////////////////////////////////////////////////////////////////////////
// COMPLEX IMPLEMENTATION
////////////////////////////////////////////////////////////////////////////////
template <class _Tp, class _Enable = void> class complex;
template <class _Tp>
struct is_gencomplex
: std::integral_constant<bool,
std::is_same_v<_Tp, complex<double>> ||
std::is_same_v<_Tp, complex<float>> ||
std::is_same_v<_Tp, complex<sycl::half>>> {};
template <typename _Tp>
inline constexpr bool is_gencomplex_v = is_gencomplex<_Tp>::value;
template <class _Tp>
struct is_genfloat
: std::integral_constant<bool, std::is_same_v<_Tp, double> ||
std::is_same_v<_Tp, float> ||
std::is_same_v<_Tp, sycl::half>> {};
template <typename _Tp>
inline constexpr bool is_genfloat_v = is_genfloat<_Tp>::value;
template <class _Tp>
class complex<_Tp, typename std::enable_if<is_genfloat<_Tp>::value>::type> {
public:
typedef _Tp value_type;
private:
value_type __re_;
value_type __im_;
public:
_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr complex(
value_type __re = value_type(), value_type __im = value_type())
: __re_(__re), __im_(__im) {}
template <typename _Xp>
_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr complex(const complex<_Xp> &__c)
: __re_(__c.real()), __im_(__c.imag()) {}
template <class _Xp, class = std::enable_if<is_genfloat<_Xp>::value>>
_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr complex(
const std::complex<_Xp> &__c)
: __re_(static_cast<value_type>(__c.real())),
__im_(static_cast<value_type>(__c.imag())) {}
template <class _Xp, class = std::enable_if<is_genfloat<_Xp>::value>>
_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr
operator std::complex<_Xp>() const {
return std::complex<_Xp>(static_cast<_Xp>(__re_), static_cast<_Xp>(__im_));
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr value_type real() const {
return __re_;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr value_type imag() const {
return __im_;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY void real(value_type __re) { __re_ = __re; }
_SYCL_EXT_CPLX_INLINE_VISIBILITY void imag(value_type __im) { __im_ = __im; }
_SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator=(value_type __re) {
__re_ = __re;
__im_ = value_type();
return *this;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex &
operator+=(complex<value_type> &__c, value_type __re) {
__c.__re_ += __re;
return __c;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex &
operator-=(complex<value_type> &__c, value_type __re) {
__c.__re_ -= __re;
return __c;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex &
operator*=(complex<value_type> &__c, value_type __re) {
__c.__re_ *= __re;
__c.__im_ *= __re;
return __c;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex &
operator/=(complex<value_type> &__c, value_type __re) {
__c.__re_ /= __re;
__c.__im_ /= __re;
return __c;
}
template <class _Xp>
_SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator=(const complex<_Xp> &__c) {
__re_ = __c.real();
__im_ = __c.imag();
return *this;
}
template <class _Xp>
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex &
operator+=(complex<value_type> &__x, const complex<_Xp> &__y) {
__x.__re_ += __y.real();
__x.__im_ += __y.imag();
return __x;
}
template <class _Xp>
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex &
operator-=(complex<value_type> &__x, const complex<_Xp> &__y) {
__x.__re_ -= __y.real();
__x.__im_ -= __y.imag();
return __x;
}
template <class _Xp>
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex &
operator*=(complex<value_type> &__x, const complex<_Xp> &__y) {
__x = __x * complex(__y.real(), __y.imag());
return __x;
}
template <class _Xp>
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex &
operator/=(complex<value_type> &__x, const complex<_Xp> &__y) {
__x = __x / complex(__y.real(), __y.imag());
return __x;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex<value_type>
operator+(const complex<value_type> &__x, const complex<value_type> &__y) {
complex<value_type> __t(__x);
__t += __y;
return __t;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex<value_type>
operator+(const complex<value_type> &__x, value_type __y) {
complex<value_type> __t(__x);
__t += __y;
return __t;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex<value_type>
operator+(value_type __x, const complex<value_type> &__y) {
complex<value_type> __t(__y);
__t += __x;
return __t;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex<value_type>
operator+(const complex<value_type> &__x) {
return __x;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex<value_type>
operator-(const complex<value_type> &__x, const complex<value_type> &__y) {
complex<value_type> __t(__x);
__t -= __y;
return __t;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex<value_type>
operator-(const complex<value_type> &__x, value_type __y) {
complex<value_type> __t(__x);
__t -= __y;
return __t;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex<value_type>
operator-(value_type __x, const complex<value_type> &__y) {
complex<value_type> __t(-__y);
__t += __x;
return __t;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex<value_type>
operator-(const complex<value_type> &__x) {
return complex<value_type>(-__x.__re_, -__x.__im_);
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex<value_type>
operator*(const complex<value_type> &__z, const complex<value_type> &__w) {
value_type __a = __z.__re_;
value_type __b = __z.__im_;
value_type __c = __w.__re_;
value_type __d = __w.__im_;
value_type __ac = __a * __c;
value_type __bd = __b * __d;
value_type __ad = __a * __d;
value_type __bc = __b * __c;
value_type __x = __ac - __bd;
value_type __y = __ad + __bc;
if (cplex::detail::isnan(__x) && cplex::detail::isnan(__y)) {
bool __recalc = false;
if (cplex::detail::isinf(__a) || cplex::detail::isinf(__b)) {
__a = sycl::copysign(
cplex::detail::isinf(__a) ? value_type(1) : value_type(0), __a);
__b = sycl::copysign(
cplex::detail::isinf(__b) ? value_type(1) : value_type(0), __b);
if (cplex::detail::isnan(__c))
__c = sycl::copysign(value_type(0), __c);
if (cplex::detail::isnan(__d))
__d = sycl::copysign(value_type(0), __d);
__recalc = true;
}
if (cplex::detail::isinf(__c) || cplex::detail::isinf(__d)) {
__c = sycl::copysign(
cplex::detail::isinf(__c) ? value_type(1) : value_type(0), __c);
__d = sycl::copysign(
cplex::detail::isinf(__d) ? value_type(1) : value_type(0), __d);
if (cplex::detail::isnan(__a))
__a = sycl::copysign(value_type(0), __a);
if (cplex::detail::isnan(__b))
__b = sycl::copysign(value_type(0), __b);
__recalc = true;
}
if (!__recalc &&
(cplex::detail::isinf(__ac) || cplex::detail::isinf(__bd) ||
cplex::detail::isinf(__ad) || cplex::detail::isinf(__bc))) {
if (cplex::detail::isnan(__a))
__a = sycl::copysign(value_type(0), __a);
if (cplex::detail::isnan(__b))
__b = sycl::copysign(value_type(0), __b);
if (cplex::detail::isnan(__c))
__c = sycl::copysign(value_type(0), __c);
if (cplex::detail::isnan(__d))
__d = sycl::copysign(value_type(0), __d);
__recalc = true;
}
if (__recalc) {
__x = value_type(INFINITY) * (__a * __c - __b * __d);
__y = value_type(INFINITY) * (__a * __d + __b * __c);
}
}
return complex<value_type>(__x, __y);
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex<value_type>
operator*(const complex<value_type> &__x, value_type __y) {
complex<value_type> __t(__x);
__t *= __y;
return __t;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex<value_type>
operator*(value_type __x, const complex<value_type> &__y) {
complex<value_type> __t(__y);
__t *= __x;
return __t;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex<value_type>
operator/(const complex<value_type> &__z, const complex<value_type> &__w) {
#if defined(_SYCL_EXT_CPLX_FAST_MATH)
// This implementation is around 20% faster for single precision, 5% for
// double, at the expense of larger error in some cases, because no scaling
// is done.
value_type __a = __z.__re_;
value_type __b = __z.__im_;
value_type __c = __w.__re_;
value_type __d = __w.__im_;
value_type __r = __a * __c + __b * __d;
value_type __n = __b * __b + __d * __d;
value_type __x = __r / __n;
value_type __y = (__b * __c - __a * __d) / __n;
return complex<value_type>(__x, __y);
#else
int __ilogbw = 0;
value_type __a = __z.__re_;
value_type __b = __z.__im_;
value_type __c = __w.__re_;
value_type __d = __w.__im_;
value_type __logbw =
sycl::logb(sycl::fmax(sycl::fabs(__c), sycl::fabs(__d)));
if (cplex::detail::isfinite(__logbw)) {
__ilogbw = static_cast<int>(__logbw);
__c = sycl::ldexp(__c, -__ilogbw);
__d = sycl::ldexp(__d, -__ilogbw);
}
value_type __denom = __c * __c + __d * __d;
value_type __x = sycl::ldexp((__a * __c + __b * __d) / __denom, -__ilogbw);
value_type __y = sycl::ldexp((__b * __c - __a * __d) / __denom, -__ilogbw);
if (cplex::detail::isnan(__x) && cplex::detail::isnan(__y)) {
if ((__denom == value_type(0)) &&
(!cplex::detail::isnan(__a) || !cplex::detail::isnan(__b))) {
__x = sycl::copysign(value_type(INFINITY), __c) * __a;
__y = sycl::copysign(value_type(INFINITY), __c) * __b;
} else if ((cplex::detail::isinf(__a) || cplex::detail::isinf(__b)) &&
cplex::detail::isfinite(__c) && cplex::detail::isfinite(__d)) {
__a = sycl::copysign(
cplex::detail::isinf(__a) ? value_type(1) : value_type(0), __a);
__b = sycl::copysign(
cplex::detail::isinf(__b) ? value_type(1) : value_type(0), __b);
__x = value_type(INFINITY) * (__a * __c + __b * __d);
__y = value_type(INFINITY) * (__b * __c - __a * __d);
} else if (cplex::detail::isinf(__logbw) && __logbw > value_type(0) &&
cplex::detail::isfinite(__a) && cplex::detail::isfinite(__b)) {
__c = sycl::copysign(
cplex::detail::isinf(__c) ? value_type(1) : value_type(0), __c);
__d = sycl::copysign(
cplex::detail::isinf(__d) ? value_type(1) : value_type(0), __d);
__x = value_type(0) * (__a * __c + __b * __d);
__y = value_type(0) * (__b * __c - __a * __d);
}
}
return complex<value_type>(__x, __y);
#endif
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex<value_type>
operator/(const complex<value_type> &__x, value_type __y) {
return complex<value_type>(__x.__re_ / __y, __x.__im_ / __y);
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex<value_type>
operator/(value_type __x, const complex<value_type> &__y) {
complex<value_type> __t(__x);
__t /= __y;
return __t;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend constexpr bool
operator==(const complex<value_type> &__x, const complex<value_type> &__y) {
return __x.__re_ == __y.__re_ && __x.__im_ == __y.__im_;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend constexpr bool
operator==(const complex<value_type> &__x, value_type __y) {
return __x.__re_ == __y && __x.__im_ == 0;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend constexpr bool
operator==(value_type __x, const complex<value_type> &__y) {
return __x == __y.__re_ && 0 == __y.__im_;
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend constexpr bool
operator!=(const complex<value_type> &__x, const complex<value_type> &__y) {
return !(__x == __y);
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend constexpr bool
operator!=(const complex<value_type> &__x, value_type __y) {
return !(__x == __y);
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend constexpr bool
operator!=(value_type __x, const complex<value_type> &__y) {
return !(__x == __y);
}
template <class _CharT, class _Traits>
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend std::basic_istream<_CharT, _Traits> &
operator>>(std::basic_istream<_CharT, _Traits> &__is,
complex<value_type> &__x) {
if (__is.good()) {
ws(__is);
if (__is.peek() == _CharT('(')) {
__is.get();
value_type __r;
__is >> __r;
if (!__is.fail()) {
ws(__is);
_CharT __c = __is.peek();
if (__c == _CharT(',')) {
__is.get();
value_type __i;
__is >> __i;
if (!__is.fail()) {
ws(__is);
__c = __is.peek();
if (__c == _CharT(')')) {
__is.get();
__x = complex<value_type>(__r, __i);
} else
__is.setstate(__is.failbit);
} else
__is.setstate(__is.failbit);
} else if (__c == _CharT(')')) {
__is.get();
__x = complex<value_type>(__r, value_type(0));
} else
__is.setstate(__is.failbit);
} else
__is.setstate(__is.failbit);
} else {
value_type __r;
__is >> __r;
if (!__is.fail())
__x = complex<value_type>(__r, value_type(0));
else
__is.setstate(__is.failbit);
}
} else
__is.setstate(__is.failbit);
return __is;
}
template <class _CharT, class _Traits>
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend std::basic_ostream<_CharT, _Traits> &
operator<<(std::basic_ostream<_CharT, _Traits> &__os,
const complex<value_type> &__x) {
std::basic_ostringstream<_CharT, _Traits> __s;
__s.flags(__os.flags());
__s.imbue(__os.getloc());
__s.precision(__os.precision());
__s << '(' << __x.__re_ << ',' << __x.__im_ << ')';
return __os << __s.str();
}
_SYCL_EXT_CPLX_INLINE_VISIBILITY friend const sycl::stream &
operator<<(const sycl::stream &__ss, const complex<value_type> &_x) {
return __ss << "(" << _x.__re_ << "," << _x.__im_ << ")";
}
};
namespace cplex::detail {
template <class _Tp, bool = std::is_integral<_Tp>::value,
bool = is_genfloat<_Tp>::value>
struct __libcpp_complex_overload_traits {};
// Integral Types
template <class _Tp> struct __libcpp_complex_overload_traits<_Tp, true, false> {
typedef double _ValueType;
typedef complex<double> _ComplexType;
};
// Floating point types
template <class _Tp> struct __libcpp_complex_overload_traits<_Tp, false, true> {
typedef _Tp _ValueType;
typedef complex<_Tp> _ComplexType;
};
} // namespace cplex::detail
// real
template <class _Tp, class = std::enable_if<is_gencomplex<_Tp>::value>>
_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr _Tp real(const complex<_Tp> &__c) {
return __c.real();
}
template <class _Tp>
_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr
typename cplex::detail::__libcpp_complex_overload_traits<_Tp>::_ValueType
real(_Tp __re) {
return __re;
}
// imag
template <class _Tp, class = std::enable_if<is_gencomplex<_Tp>::value>>
_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr _Tp imag(const complex<_Tp> &__c) {
return __c.imag();
}
template <class _Tp>
_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr
typename cplex::detail::__libcpp_complex_overload_traits<_Tp>::_ValueType
imag(_Tp) {
return 0;
}
// abs
template <class _Tp, class = std::enable_if<is_gencomplex<_Tp>::value>>
_SYCL_EXT_CPLX_INLINE_VISIBILITY _Tp abs(const complex<_Tp> &__c) {
return sycl::hypot(__c.real(), __c.imag());
}
// arg
template <class _Tp, class = std::enable_if<is_gencomplex<_Tp>::value>>
_SYCL_EXT_CPLX_INLINE_VISIBILITY _Tp arg(const complex<_Tp> &__c) {
return sycl::atan2(__c.imag(), __c.real());
}
template <class _Tp>
_SYCL_EXT_CPLX_INLINE_VISIBILITY
typename cplex::detail::__libcpp_complex_overload_traits<_Tp>::_ValueType
arg(_Tp __re) {
typedef
typename cplex::detail::__libcpp_complex_overload_traits<_Tp>::_ValueType
_ValueType;
return sycl::atan2(static_cast<_ValueType>(0), static_cast<_ValueType>(__re));
}
// norm
template <class _Tp, class = std::enable_if<is_gencomplex<_Tp>::value>>
_SYCL_EXT_CPLX_INLINE_VISIBILITY _Tp norm(const complex<_Tp> &__c) {
if (cplex::detail::isinf(__c.real()))
return sycl::fabs(__c.real());
if (cplex::detail::isinf(__c.imag()))
return sycl::fabs(__c.imag());
return __c.real() * __c.real() + __c.imag() * __c.imag();
}
template <class _Tp>
_SYCL_EXT_CPLX_INLINE_VISIBILITY
typename cplex::detail::__libcpp_complex_overload_traits<_Tp>::_ValueType
norm(_Tp __re) {
typedef
typename cplex::detail::__libcpp_complex_overload_traits<_Tp>::_ValueType
_ValueType;
return static_cast<_ValueType>(__re) * __re;
}
// conj
template <class _Tp, class = std::enable_if<is_gencomplex<_Tp>::value>>
_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> conj(const complex<_Tp> &__c) {
return complex<_Tp>(__c.real(), -__c.imag());
}
template <class _Tp>
_SYCL_EXT_CPLX_INLINE_VISIBILITY
typename cplex::detail::__libcpp_complex_overload_traits<_Tp>::_ComplexType
conj(_Tp __re) {
typedef typename cplex::detail::__libcpp_complex_overload_traits<
_Tp>::_ComplexType _ComplexType;
return _ComplexType(__re);
}
// proj
template <class _Tp, class = std::enable_if<is_gencomplex<_Tp>::value>>
_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> proj(const complex<_Tp> &__c) {
complex<_Tp> __r = __c;
if (cplex::detail::isinf(__c.real()) || cplex::detail::isinf(__c.imag()))
__r = complex<_Tp>(INFINITY, sycl::copysign(_Tp(0), __c.imag()));
return __r;
}
template <class _Tp>
_SYCL_EXT_CPLX_INLINE_VISIBILITY
typename cplex::detail::__libcpp_complex_overload_traits<_Tp>::_ComplexType
proj(_Tp __re) {
typedef typename cplex::detail::__libcpp_complex_overload_traits<
_Tp>::_ComplexType _ComplexType;
if constexpr (!std::is_integral_v<_Tp>) {
if (cplex::detail::isinf(__re))
__re = sycl::fabs(__re);
}
return _ComplexType(__re);
}
// polar
template <class _Tp, class = std::enable_if<is_gencomplex<_Tp>::value>>
_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp>
polar(const _Tp &__rho, const _Tp &__theta = _Tp()) {
if (cplex::detail::isnan(__rho) || sycl::signbit(__rho))
return complex<_Tp>(_Tp(NAN), _Tp(NAN));
if (cplex::detail::isnan(__theta)) {
if (cplex::detail::isinf(__rho))
return complex<_Tp>(__rho, __theta);
return complex<_Tp>(__theta, __theta);
}
if (cplex::detail::isinf(__theta)) {
if (cplex::detail::isinf(__rho))
return complex<_Tp>(__rho, _Tp(NAN));
return complex<_Tp>(_Tp(NAN), _Tp(NAN));
}
_Tp __x = __rho * sycl::cos(__theta);
if (cplex::detail::isnan(__x))
__x = 0;
_Tp __y = __rho * sycl::sin(__theta);
if (cplex::detail::isnan(__y))
__y = 0;
return complex<_Tp>(__x, __y);
}
// log
template <class _Tp, class = std::enable_if<is_gencomplex<_Tp>::value>>
_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> log(const complex<_Tp> &__x) {
return complex<_Tp>(sycl::log(abs(__x)), arg(__x));
}
// log10
template <class _Tp, class = std::enable_if<is_gencomplex<_Tp>::value>>
_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> log10(const complex<_Tp> &__x) {
return log(__x) / sycl::log(_Tp(10));
}
// sqrt
template <class _Tp, class = std::enable_if<is_gencomplex<_Tp>::value>>
_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> sqrt(const complex<_Tp> &__x) {
if (cplex::detail::isinf(__x.imag()))
return complex<_Tp>(_Tp(INFINITY), __x.imag());
if (cplex::detail::isinf(__x.real())) {
if (__x.real() > _Tp(0))
return complex<_Tp>(__x.real(), cplex::detail::isnan(__x.imag())
? __x.imag()
: sycl::copysign(_Tp(0), __x.imag()));
return complex<_Tp>(cplex::detail::isnan(__x.imag()) ? __x.imag() : _Tp(0),
sycl::copysign(__x.real(), __x.imag()));
}
return polar(sycl::sqrt(abs(__x)), arg(__x) / _Tp(2));
}
// exp
template <class _Tp, class = std::enable_if<is_gencomplex<_Tp>::value>>
_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> exp(const complex<_Tp> &__x) {
_Tp __i = __x.imag();
if (__i == 0) {
return complex<_Tp>(sycl::exp(__x.real()),
sycl::copysign(_Tp(0), __x.imag()));
}
if (cplex::detail::isinf(__x.real())) {
if (__x.real() < _Tp(0)) {
if (!cplex::detail::isfinite(__i))
__i = _Tp(1);
} else if (__i == 0 || !cplex::detail::isfinite(__i)) {
if (cplex::detail::isinf(__i))
__i = _Tp(NAN);
return complex<_Tp>(__x.real(), __i);
}
}
_Tp __e = sycl::exp(__x.real());
return complex<_Tp>(__e * sycl::cos(__i), __e * sycl::sin(__i));