diff --git a/sycl/include/sycl/ext/oneapi/experimental/sycl_complex.hpp b/sycl/include/sycl/ext/oneapi/experimental/sycl_complex.hpp index 179c24c5a2fed..0f8223286b852 100644 --- a/sycl/include/sycl/ext/oneapi/experimental/sycl_complex.hpp +++ b/sycl/include/sycl/ext/oneapi/experimental/sycl_complex.hpp @@ -37,6 +37,8 @@ using std::basic_ostringstream; using std::declval; +namespace cplx::detail { + template using _BoolConstant = integral_constant; template @@ -101,8 +103,9 @@ template class __promote_imp<_A1, void, void, true> { template class __promote : public __promote_imp<_A1, _A2, _A3> {}; +} // namespace cplx::detail -template class complex; +template class complex; template struct is_gencomplex @@ -118,11 +121,7 @@ struct is_genfloat std::is_same_v<_Tp, sycl::half>> {}; template -complex<_Tp> operator*(const complex<_Tp> &__z, const complex<_Tp> &__w); -template -complex<_Tp> operator/(const complex<_Tp> &__x, const complex<_Tp> &__y); - -template class complex { +class complex<_Tp, typename std::enable_if::value>::type> { public: typedef _Tp value_type; @@ -132,20 +131,23 @@ template class complex { public: _SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr complex( - const value_type &__re = value_type(), - const value_type &__im = value_type()) + value_type __re = value_type(), value_type __im = value_type()) : __re_(__re), __im_(__im) {} - template + + template _SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr complex(const complex<_Xp> &__c) : __re_(__c.real()), __im_(__c.imag()) {} - template + template ::value>> _SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr complex( const std::complex<_Xp> &__c) - : __re_(__c.real()), __im_(__c.imag()) {} - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY operator std::complex<_Xp>() { - return std::complex<_Xp>(__re_, __im_); + : __re_(static_cast(__c.real())), + __im_(static_cast(__c.imag())) {} + + template ::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 { @@ -158,222 +160,32 @@ template class complex { _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=(const value_type &__re) { - __re_ = __re; - __im_ = value_type(); - return *this; - } - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator+=(const value_type &__re) { - __re_ += __re; - return *this; - } - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator-=(const value_type &__re) { - __re_ -= __re; - return *this; - } - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator*=(const value_type &__re) { - __re_ *= __re; - __im_ *= __re; - return *this; - } - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator/=(const value_type &__re) { - __re_ /= __re; - __im_ /= __re; - return *this; - } - - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator=(const complex<_Xp> &__c) { - __re_ = __c.real(); - __im_ = __c.imag(); - return *this; - } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator=(const std::complex<_Xp> &__c) { - __re_ = __c.real(); - __im_ = __c.imag(); - return *this; - } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator+=(const complex<_Xp> &__c) { - __re_ += __c.real(); - __im_ += __c.imag(); - return *this; - } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator-=(const complex<_Xp> &__c) { - __re_ -= __c.real(); - __im_ -= __c.imag(); - return *this; - } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator*=(const complex<_Xp> &__c) { - *this = *this * complex(__c.real(), __c.imag()); - return *this; - } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator/=(const complex<_Xp> &__c) { - *this = *this / complex(__c.real(), __c.imag()); - return *this; - } -}; - -template <> class complex; -template <> class complex; - -template <> class complex { - sycl::half __re_; - sycl::half __im_; - -public: - typedef sycl::half value_type; - - _SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr complex( - sycl::half __re = sycl::half{}, sycl::half __im = sycl::half{}) - : __re_(__re), __im_(__im) {} - _SYCL_EXT_CPLX_INLINE_VISIBILITY - explicit constexpr complex(const complex &__c); - _SYCL_EXT_CPLX_INLINE_VISIBILITY - explicit constexpr complex(const complex &__c); - _SYCL_EXT_CPLX_INLINE_VISIBILITY - constexpr operator std::complex() { - return std::complex(__re_, __im_); - } - - _SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr sycl::half real() const { - return __re_; - } - _SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr sycl::half 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=(sycl::half __re) { - __re_ = __re; - __im_ = value_type(); - return *this; - } - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator+=(sycl::half __re) { - __re_ += __re; - return *this; - } - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator-=(sycl::half __re) { - __re_ -= __re; - return *this; - } - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator*=(sycl::half __re) { - __re_ *= __re; - __im_ *= __re; - return *this; - } - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator/=(sycl::half __re) { - __re_ /= __re; - __im_ /= __re; - return *this; - } - - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator=(const complex<_Xp> &__c) { - __re_ = __c.real(); - __im_ = __c.imag(); - return *this; - } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator=(const std::complex<_Xp> &__c) { - __re_ = __c.real(); - __im_ = __c.imag(); - return *this; - } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator+=(const complex<_Xp> &__c) { - __re_ += __c.real(); - __im_ += __c.imag(); - return *this; - } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator-=(const complex<_Xp> &__c) { - __re_ -= __c.real(); - __im_ -= __c.imag(); - return *this; - } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator*=(const complex<_Xp> &__c) { - *this = *this * complex(__c.real(), __c.imag()); - return *this; - } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator/=(const complex<_Xp> &__c) { - *this = *this / complex(__c.real(), __c.imag()); - return *this; - } -}; - -template <> class complex { - float __re_; - float __im_; - -public: - typedef float value_type; - - _SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr complex(float __re = 0.0f, - float __im = 0.0f) - : __re_(__re), __im_(__im) {} - _SYCL_EXT_CPLX_INLINE_VISIBILITY - constexpr complex(const complex &__c); - _SYCL_EXT_CPLX_INLINE_VISIBILITY - explicit constexpr complex(const complex &__c); - _SYCL_EXT_CPLX_INLINE_VISIBILITY - constexpr complex(const std::complex &__c) - : __re_(__c.real()), __im_(__c.imag()) {} - _SYCL_EXT_CPLX_INLINE_VISIBILITY - constexpr operator std::complex() { - return std::complex(__re_, __im_); - } - - _SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr float real() const { - return __re_; - } - _SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr float 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=(float __re) { + _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator=(value_type __re) { __re_ = __re; __im_ = value_type(); return *this; } - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator+=(float __re) { - __re_ += __re; - return *this; + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex & + operator+=(complex &__c, value_type __re) { + __c.__re_ += __re; + return __c; } - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator-=(float __re) { - __re_ -= __re; - return *this; + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex & + operator-=(complex &__c, value_type __re) { + __c.__re_ -= __re; + return __c; } - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator*=(float __re) { - __re_ *= __re; - __im_ *= __re; - return *this; + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex & + operator*=(complex &__c, value_type __re) { + __c.__re_ *= __re; + __c.__im_ *= __re; + return __c; } - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator/=(float __re) { - __re_ /= __re; - __im_ /= __re; - return *this; + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex & + operator/=(complex &__c, value_type __re) { + __c.__re_ /= __re; + __c.__im_ /= __re; + return __c; } template @@ -383,376 +195,293 @@ template <> class complex { return *this; } template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator=(const std::complex<_Xp> &__c) { - __re_ = __c.real(); - __im_ = __c.imag(); - return *this; - } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator+=(const complex<_Xp> &__c) { - __re_ += __c.real(); - __im_ += __c.imag(); - return *this; + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex & + operator+=(complex &__x, const complex<_Xp> &__y) { + __x.__re_ += __y.real(); + __x.__im_ += __y.imag(); + return __x; } template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator-=(const complex<_Xp> &__c) { - __re_ -= __c.real(); - __im_ -= __c.imag(); - return *this; + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex & + operator-=(complex &__x, const complex<_Xp> &__y) { + __x.__re_ -= __y.real(); + __x.__im_ -= __y.imag(); + return __x; } template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator*=(const complex<_Xp> &__c) { - *this = *this * complex(__c.real(), __c.imag()); - return *this; + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex & + operator*=(complex &__x, const complex<_Xp> &__y) { + __x = __x * complex(__y.real(), __y.imag()); + return __x; } template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator/=(const complex<_Xp> &__c) { - *this = *this / complex(__c.real(), __c.imag()); - return *this; - } -}; - -template <> class complex { - double __re_; - double __im_; - -public: - typedef double value_type; - - _SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr complex(double __re = 0.0, - double __im = 0.0) - : __re_(__re), __im_(__im) {} - _SYCL_EXT_CPLX_INLINE_VISIBILITY - constexpr complex(const complex &__c); - _SYCL_EXT_CPLX_INLINE_VISIBILITY - constexpr complex(const complex &__c); - _SYCL_EXT_CPLX_INLINE_VISIBILITY - constexpr complex(const std::complex &__c) - : __re_(__c.real()), __im_(__c.imag()) {} - _SYCL_EXT_CPLX_INLINE_VISIBILITY - constexpr operator std::complex() { - return std::complex(__re_, __im_); - } - - _SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr double real() const { - return __re_; - } - _SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr double 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=(double __re) { - __re_ = __re; - __im_ = value_type(); - return *this; - } - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator+=(double __re) { - __re_ += __re; - return *this; - } - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator-=(double __re) { - __re_ -= __re; - return *this; - } - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator*=(double __re) { - __re_ *= __re; - __im_ *= __re; - return *this; - } - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator/=(double __re) { - __re_ /= __re; - __im_ /= __re; - return *this; + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex & + operator/=(complex &__x, const complex<_Xp> &__y) { + __x = __x / complex(__y.real(), __y.imag()); + return __x; } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex &operator=(const complex<_Xp> &__c) { - __re_ = __c.real(); - __im_ = __c.imag(); - return *this; - } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator=(const std::complex<_Xp> &__c) { - __re_ = __c.real(); - __im_ = __c.imag(); - return *this; - } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator+=(const complex<_Xp> &__c) { - __re_ += __c.real(); - __im_ += __c.imag(); - return *this; + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex + operator+(const complex &__x, const complex &__y) { + complex __t(__x); + __t += __y; + return __t; } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator-=(const complex<_Xp> &__c) { - __re_ -= __c.real(); - __im_ -= __c.imag(); - return *this; + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex + operator+(const complex &__x, value_type __y) { + complex __t(__x); + __t += __y; + return __t; } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator*=(const complex<_Xp> &__c) { - *this = *this * complex(__c.real(), __c.imag()); - return *this; + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex + operator+(value_type __x, const complex &__y) { + complex __t(__y); + __t += __x; + return __t; } - template - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex & - operator/=(const complex<_Xp> &__c) { - *this = *this / complex(__c.real(), __c.imag()); - return *this; + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex + operator+(const complex &__x) { + return __x; } -}; - -inline constexpr complex::complex(const complex &__c) - : __re_(__c.real()), __im_(__c.imag()) {} - -inline constexpr complex::complex(const complex &__c) - : __re_(__c.real()), __im_(__c.imag()) {} - -inline constexpr complex::complex(const complex &__c) - : __re_(__c.real()), __im_(__c.imag()) {} - -inline constexpr complex::complex(const complex &__c) - : __re_(__c.real()), __im_(__c.imag()) {} - -inline constexpr complex::complex(const complex &__c) - : __re_(__c.real()), __im_(__c.imag()) {} - -inline constexpr complex::complex(const complex &__c) - : __re_(__c.real()), __im_(__c.imag()) {} - -// 26.3.6 operators: - -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> -operator+(const complex<_Tp> &__x, const complex<_Tp> &__y) { - complex<_Tp> __t(__x); - __t += __y; - return __t; -} - -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> operator+(const complex<_Tp> &__x, - const _Tp &__y) { - complex<_Tp> __t(__x); - __t += __y; - return __t; -} - -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> -operator+(const _Tp &__x, const complex<_Tp> &__y) { - complex<_Tp> __t(__y); - __t += __x; - return __t; -} - -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> -operator-(const complex<_Tp> &__x, const complex<_Tp> &__y) { - complex<_Tp> __t(__x); - __t -= __y; - return __t; -} - -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> operator-(const complex<_Tp> &__x, - const _Tp &__y) { - complex<_Tp> __t(__x); - __t -= __y; - return __t; -} -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> -operator-(const _Tp &__x, const complex<_Tp> &__y) { - complex<_Tp> __t(-__y); - __t += __x; - return __t; -} - -template -complex<_Tp> operator*(const complex<_Tp> &__z, const complex<_Tp> &__w) { - _Tp __a = __z.real(); - _Tp __b = __z.imag(); - _Tp __c = __w.real(); - _Tp __d = __w.imag(); - _Tp __ac = __a * __c; - _Tp __bd = __b * __d; - _Tp __ad = __a * __d; - _Tp __bc = __b * __c; - _Tp __x = __ac - __bd; - _Tp __y = __ad + __bc; - if (sycl::isnan(__x) && sycl::isnan(__y)) { - bool __recalc = false; - if (sycl::isinf(__a) || sycl::isinf(__b)) { - __a = sycl::copysign(sycl::isinf(__a) ? _Tp(1) : _Tp(0), __a); - __b = sycl::copysign(sycl::isinf(__b) ? _Tp(1) : _Tp(0), __b); - if (sycl::isnan(__c)) - __c = sycl::copysign(_Tp(0), __c); - if (sycl::isnan(__d)) - __d = sycl::copysign(_Tp(0), __d); - __recalc = true; - } - if (sycl::isinf(__c) || sycl::isinf(__d)) { - __c = sycl::copysign(sycl::isinf(__c) ? _Tp(1) : _Tp(0), __c); - __d = sycl::copysign(sycl::isinf(__d) ? _Tp(1) : _Tp(0), __d); - if (sycl::isnan(__a)) - __a = sycl::copysign(_Tp(0), __a); - if (sycl::isnan(__b)) - __b = sycl::copysign(_Tp(0), __b); - __recalc = true; + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex + operator-(const complex &__x, const complex &__y) { + complex __t(__x); + __t -= __y; + return __t; + } + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex + operator-(const complex &__x, value_type __y) { + complex __t(__x); + __t -= __y; + return __t; + } + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex + operator-(value_type __x, const complex &__y) { + complex __t(-__y); + __t += __x; + return __t; + } + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex + operator-(const complex &__x) { + return complex(-__x.__re_, -__x.__im_); + } + + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex + operator*(const complex &__z, const complex &__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 (sycl::isnan(__x) && sycl::isnan(__y)) { + bool __recalc = false; + if (sycl::isinf(__a) || sycl::isinf(__b)) { + __a = sycl::copysign(sycl::isinf(__a) ? value_type(1) : value_type(0), + __a); + __b = sycl::copysign(sycl::isinf(__b) ? value_type(1) : value_type(0), + __b); + if (sycl::isnan(__c)) + __c = sycl::copysign(value_type(0), __c); + if (sycl::isnan(__d)) + __d = sycl::copysign(value_type(0), __d); + __recalc = true; + } + if (sycl::isinf(__c) || sycl::isinf(__d)) { + __c = sycl::copysign(sycl::isinf(__c) ? value_type(1) : value_type(0), + __c); + __d = sycl::copysign(sycl::isinf(__d) ? value_type(1) : value_type(0), + __d); + if (sycl::isnan(__a)) + __a = sycl::copysign(value_type(0), __a); + if (sycl::isnan(__b)) + __b = sycl::copysign(value_type(0), __b); + __recalc = true; + } + if (!__recalc && (sycl::isinf(__ac) || sycl::isinf(__bd) || + sycl::isinf(__ad) || sycl::isinf(__bc))) { + if (sycl::isnan(__a)) + __a = sycl::copysign(value_type(0), __a); + if (sycl::isnan(__b)) + __b = sycl::copysign(value_type(0), __b); + if (sycl::isnan(__c)) + __c = sycl::copysign(value_type(0), __c); + if (sycl::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); + } } - if (!__recalc && (sycl::isinf(__ac) || sycl::isinf(__bd) || - sycl::isinf(__ad) || sycl::isinf(__bc))) { - if (sycl::isnan(__a)) - __a = sycl::copysign(_Tp(0), __a); - if (sycl::isnan(__b)) - __b = sycl::copysign(_Tp(0), __b); - if (sycl::isnan(__c)) - __c = sycl::copysign(_Tp(0), __c); - if (sycl::isnan(__d)) - __d = sycl::copysign(_Tp(0), __d); - __recalc = true; + return complex(__x, __y); + } + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex + operator*(const complex &__x, value_type __y) { + complex __t(__x); + __t *= __y; + return __t; + } + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex + operator*(value_type __x, const complex &__y) { + complex __t(__y); + __t *= __x; + return __t; + } + + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex + operator/(const complex &__z, const complex &__w) { + 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 (sycl::isfinite(__logbw)) { + __ilogbw = static_cast(__logbw); + __c = sycl::ldexp(__c, -__ilogbw); + __d = sycl::ldexp(__d, -__ilogbw); } - if (__recalc) { - __x = _Tp(INFINITY) * (__a * __c - __b * __d); - __y = _Tp(INFINITY) * (__a * __d + __b * __c); + 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 (sycl::isnan(__x) && sycl::isnan(__y)) { + if ((__denom == value_type(0)) && + (!sycl::isnan(__a) || !sycl::isnan(__b))) { + __x = sycl::copysign(value_type(INFINITY), __c) * __a; + __y = sycl::copysign(value_type(INFINITY), __c) * __b; + } else if ((sycl::isinf(__a) || sycl::isinf(__b)) && + sycl::isfinite(__c) && sycl::isfinite(__d)) { + __a = sycl::copysign(sycl::isinf(__a) ? value_type(1) : value_type(0), + __a); + __b = sycl::copysign(sycl::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 (sycl::isinf(__logbw) && __logbw > value_type(0) && + sycl::isfinite(__a) && sycl::isfinite(__b)) { + __c = sycl::copysign(sycl::isinf(__c) ? value_type(1) : value_type(0), + __c); + __d = sycl::copysign(sycl::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(__x, __y); + } + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex + operator/(const complex &__x, value_type __y) { + return complex(__x.__re_ / __y, __x.__im_ / __y); + } + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend complex + operator/(value_type __x, const complex &__y) { + complex __t(__x); + __t /= __y; + return __t; + } + + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend constexpr bool + operator==(const complex &__x, const complex &__y) { + return __x.__re_ == __y.__re_ && __x.__im_ == __y.__im_; + } + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend constexpr bool + operator==(const complex &__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 &__y) { + return __x == __y.__re_ && 0 == __y.__im_; + } + + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend constexpr bool + operator!=(const complex &__x, const complex &__y) { + return !(__x == __y); + } + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend constexpr bool + operator!=(const complex &__x, value_type __y) { + return !(__x == __y); + } + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend constexpr bool + operator!=(value_type __x, const complex &__y) { + return !(__x == __y); + } + + template + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend std::basic_istream<_CharT, _Traits> & + operator>>(std::basic_istream<_CharT, _Traits> &__is, + complex &__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(__r, __i); + } else + __is.setstate(__is.failbit); + } else + __is.setstate(__is.failbit); + } else if (__c == _CharT(')')) { + __is.get(); + __x = complex(__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(__r, value_type(0)); + else + __is.setstate(__is.failbit); + } + } else + __is.setstate(__is.failbit); + return __is; } - return complex<_Tp>(__x, __y); -} - -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> operator*(const complex<_Tp> &__x, - const _Tp &__y) { - complex<_Tp> __t(__x); - __t *= __y; - return __t; -} - -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> -operator*(const _Tp &__x, const complex<_Tp> &__y) { - complex<_Tp> __t(__y); - __t *= __x; - return __t; -} -template -complex<_Tp> operator/(const complex<_Tp> &__z, const complex<_Tp> &__w) { - int __ilogbw = 0; - _Tp __a = __z.real(); - _Tp __b = __z.imag(); - _Tp __c = __w.real(); - _Tp __d = __w.imag(); - _Tp __logbw = sycl::logb(sycl::fmax(sycl::fabs(__c), sycl::fabs(__d))); - if (sycl::isfinite(__logbw)) { - __ilogbw = static_cast(__logbw); - __c = sycl::ldexp(__c, -__ilogbw); - __d = sycl::ldexp(__d, -__ilogbw); - } - _Tp __denom = __c * __c + __d * __d; - _Tp __x = sycl::ldexp((__a * __c + __b * __d) / __denom, -__ilogbw); - _Tp __y = sycl::ldexp((__b * __c - __a * __d) / __denom, -__ilogbw); - if (sycl::isnan(__x) && sycl::isnan(__y)) { - if ((__denom == _Tp(0)) && (!sycl::isnan(__a) || !sycl::isnan(__b))) { - __x = sycl::copysign(_Tp(INFINITY), __c) * __a; - __y = sycl::copysign(_Tp(INFINITY), __c) * __b; - } else if ((sycl::isinf(__a) || sycl::isinf(__b)) && sycl::isfinite(__c) && - sycl::isfinite(__d)) { - __a = sycl::copysign(sycl::isinf(__a) ? _Tp(1) : _Tp(0), __a); - __b = sycl::copysign(sycl::isinf(__b) ? _Tp(1) : _Tp(0), __b); - __x = _Tp(INFINITY) * (__a * __c + __b * __d); - __y = _Tp(INFINITY) * (__b * __c - __a * __d); - } else if (sycl::isinf(__logbw) && __logbw > _Tp(0) && - sycl::isfinite(__a) && sycl::isfinite(__b)) { - __c = sycl::copysign(sycl::isinf(__c) ? _Tp(1) : _Tp(0), __c); - __d = sycl::copysign(sycl::isinf(__d) ? _Tp(1) : _Tp(0), __d); - __x = _Tp(0) * (__a * __c + __b * __d); - __y = _Tp(0) * (__b * __c - __a * __d); - } + template + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend std::basic_ostream<_CharT, _Traits> & + operator<<(std::basic_ostream<_CharT, _Traits> &__os, + const complex &__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(); } - return complex<_Tp>(__x, __y); -} -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> operator/(const complex<_Tp> &__x, - const _Tp &__y) { - return complex<_Tp>(__x.real() / __y, __x.imag() / __y); -} - -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> -operator/(const _Tp &__x, const complex<_Tp> &__y) { - complex<_Tp> __t(__x); - __t /= __y; - return __t; -} - -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> -operator+(const complex<_Tp> &__x) { - return __x; -} - -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> -operator-(const complex<_Tp> &__x) { - return complex<_Tp>(-__x.real(), -__x.imag()); -} - -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr bool -operator==(const complex<_Tp> &__x, const complex<_Tp> &__y) { - return __x.real() == __y.real() && __x.imag() == __y.imag(); -} - -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr bool -operator==(const complex<_Tp> &__x, const _Tp &__y) { - return __x.real() == __y && __x.imag() == 0; -} - -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr bool -operator==(const _Tp &__x, const complex<_Tp> &__y) { - return __x == __y.real() && 0 == __y.imag(); -} - -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr bool -operator!=(const complex<_Tp> &__x, const complex<_Tp> &__y) { - return !(__x == __y); -} - -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr bool -operator!=(const complex<_Tp> &__x, const _Tp &__y) { - return !(__x == __y); -} - -template -_SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr bool -operator!=(const _Tp &__x, const complex<_Tp> &__y) { - return !(__x == __y); -} - -// 26.3.7 values: + _SYCL_EXT_CPLX_INLINE_VISIBILITY friend const sycl::stream & + operator<<(const sycl::stream &__ss, const complex &_x) { + return __ss << "(" << _x.__re_ << "," << _x.__im_ << ")"; + } +}; -template ::value, - bool = is_floating_point<_Tp>::value> +namespace cplx::detail { +template ::value, + bool = is_genfloat<_Tp>::value> struct __libcpp_complex_overload_traits {}; // Integral Types @@ -766,10 +495,11 @@ template struct __libcpp_complex_overload_traits<_Tp, false, true> { typedef _Tp _ValueType; typedef complex<_Tp> _ComplexType; }; +} // namespace cplx::detail // real -template +template ::value>> __DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr _Tp real(const complex<_Tp> &__c) { return __c.real(); @@ -777,14 +507,14 @@ real(const complex<_Tp> &__c) { template __DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr - typename __libcpp_complex_overload_traits<_Tp>::_ValueType + typename cplx::detail::__libcpp_complex_overload_traits<_Tp>::_ValueType real(_Tp __re) { return __re; } // imag -template +template ::value>> __DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr _Tp imag(const complex<_Tp> &__c) { return __c.imag(); @@ -792,14 +522,14 @@ imag(const complex<_Tp> &__c) { template __DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY constexpr - typename __libcpp_complex_overload_traits<_Tp>::_ValueType + typename cplx::detail::__libcpp_complex_overload_traits<_Tp>::_ValueType imag(_Tp) { return 0; } // abs -template +template ::value>> __DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY _Tp abs(const complex<_Tp> &__c) { return sycl::hypot(__c.real(), __c.imag()); @@ -807,7 +537,7 @@ abs(const complex<_Tp> &__c) { // arg -template +template ::value>> __DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY _Tp arg(const complex<_Tp> &__c) { return sycl::atan2(__c.imag(), __c.real()); @@ -815,22 +545,17 @@ arg(const complex<_Tp> &__c) { template __DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY - typename enable_if::value || is_same<_Tp, double>::value, - double>::type + typename cplx::detail::__libcpp_complex_overload_traits<_Tp>::_ValueType arg(_Tp __re) { - return sycl::atan2(0., __re); -} - -template -__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY - typename enable_if::value, float>::type - arg(_Tp __re) { - return sycl::atan2(0.F, __re); + typedef + typename cplx::detail::__libcpp_complex_overload_traits<_Tp>::_ValueType + _ValueType; + return sycl::atan2<_ValueType>(0, __re); } // norm -template +template ::value>> __DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY _Tp norm(const complex<_Tp> &__c) { if (sycl::isinf(__c.real())) @@ -842,15 +567,17 @@ norm(const complex<_Tp> &__c) { template __DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY - typename __libcpp_complex_overload_traits<_Tp>::_ValueType + typename cplx::detail::__libcpp_complex_overload_traits<_Tp>::_ValueType norm(_Tp __re) { - typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType; + typedef + typename cplx::detail::__libcpp_complex_overload_traits<_Tp>::_ValueType + _ValueType; return static_cast<_ValueType>(__re) * __re; } // conj -template +template ::value>> __DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> conj(const complex<_Tp> &__c) { return complex<_Tp>(__c.real(), -__c.imag()); @@ -858,16 +585,17 @@ conj(const complex<_Tp> &__c) { template __DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY - typename __libcpp_complex_overload_traits<_Tp>::_ComplexType + typename cplx::detail::__libcpp_complex_overload_traits<_Tp>::_ComplexType conj(_Tp __re) { typedef - typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType; + typename cplx::detail::__libcpp_complex_overload_traits<_Tp>::_ComplexType + _ComplexType; return _ComplexType(__re); } // proj -template +template ::value>> __DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> proj(const complex<_Tp> &__c) { complex<_Tp> __r = __c; @@ -877,30 +605,26 @@ proj(const complex<_Tp> &__c) { } template -__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY typename enable_if< - is_floating_point<_Tp>::value, - typename __libcpp_complex_overload_traits<_Tp>::_ComplexType>::type -proj(_Tp __re) { - if (sycl::isinf(__re)) - __re = sycl::fabs(__re); - return complex<_Tp>(__re); -} - -template -__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY typename enable_if< - is_integral<_Tp>::value, - typename __libcpp_complex_overload_traits<_Tp>::_ComplexType>::type -proj(_Tp __re) { +__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY + typename cplx::detail::__libcpp_complex_overload_traits<_Tp>::_ComplexType + proj(_Tp __re) { typedef - typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType; + typename cplx::detail::__libcpp_complex_overload_traits<_Tp>::_ComplexType + _ComplexType; + + if constexpr (!std::is_integral_v<_Tp>) { + if (sycl::isinf(__re)) + __re = sycl::fabs(__re); + } + return _ComplexType(__re); } // polar template ::value>> -__DPCPP_SYCL_EXTERNAL complex<_Tp> polar(const _Tp &__rho, - const _Tp &__theta = _Tp()) { +__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> +polar(const _Tp &__rho, const _Tp &__theta = _Tp()) { if (sycl::isnan(__rho) || sycl::signbit(__rho)) return complex<_Tp>(_Tp(NAN), _Tp(NAN)); if (sycl::isnan(__theta)) { @@ -941,7 +665,8 @@ log10(const complex<_Tp> &__x) { // sqrt template ::value>> -__DPCPP_SYCL_EXTERNAL complex<_Tp> sqrt(const complex<_Tp> &__x) { +__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> +sqrt(const complex<_Tp> &__x) { if (sycl::isinf(__x.imag())) return complex<_Tp>(_Tp(INFINITY), __x.imag()); if (sycl::isinf(__x.real())) { @@ -958,7 +683,8 @@ __DPCPP_SYCL_EXTERNAL complex<_Tp> sqrt(const complex<_Tp> &__x) { // exp template ::value>> -__DPCPP_SYCL_EXTERNAL complex<_Tp> exp(const complex<_Tp> &__x) { +__DPCPP_SYCL_EXTERNAL _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()), @@ -988,36 +714,34 @@ pow(const complex<_Tp> &__x, const complex<_Tp> &__y) { template ::value>> -__DPCPP_SYCL_EXTERNAL - _SYCL_EXT_CPLX_INLINE_VISIBILITY complex::type> +__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY + complex::type> pow(const complex<_Tp> &__x, const complex<_Up> &__y) { - typedef complex::type> result_type; - return sycl::ext::oneapi::experimental::pow(result_type(__x), - result_type(__y)); + typedef complex::type> result_type; + return pow(result_type(__x), result_type(__y)); } template ::value>> -__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY - typename enable_if::value, - complex::type>>::type - pow(const complex<_Tp> &__x, const _Up &__y) { - typedef complex::type> result_type; - return sycl::ext::oneapi::experimental::pow(result_type(__x), - result_type(__y)); +__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY typename std::enable_if< + is_genfloat<_Up>::value, + complex::type>>::type +pow(const complex<_Tp> &__x, const _Up &__y) { + typedef complex::type> result_type; + return pow(result_type(__x), result_type(__y)); } template ::value>> -__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY - typename enable_if::value, - complex::type>>::type - pow(const _Tp &__x, const complex<_Up> &__y) { - typedef complex::type> result_type; - return sycl::ext::oneapi::experimental::pow(result_type(__x), - result_type(__y)); +__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY typename std::enable_if< + is_genfloat<_Up>::value, + complex::type>>::type +pow(const _Tp &__x, const complex<_Up> &__y) { + typedef complex::type> result_type; + return pow(result_type(__x), result_type(__y)); } +namespace cplx::detail { // __sqr, computes pow(x, 2) template ::value>> @@ -1025,11 +749,13 @@ _SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> __sqr(const complex<_Tp> &__x) { return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()), _Tp(2) * __x.real() * __x.imag()); } +} // namespace cplx::detail // asinh template ::value>> -__DPCPP_SYCL_EXTERNAL complex<_Tp> asinh(const complex<_Tp> &__x) { +__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> +asinh(const complex<_Tp> &__x) { const _Tp __pi(sycl::atan2(_Tp(+0.), _Tp(-0.))); if (sycl::isinf(__x.real())) { if (sycl::isnan(__x.imag())) @@ -1049,7 +775,7 @@ __DPCPP_SYCL_EXTERNAL complex<_Tp> asinh(const complex<_Tp> &__x) { if (sycl::isinf(__x.imag())) return complex<_Tp>(sycl::copysign(__x.imag(), __x.real()), sycl::copysign(__pi / _Tp(2), __x.imag())); - complex<_Tp> __z = log(__x + sqrt(__sqr(__x) + _Tp(1))); + complex<_Tp> __z = log(__x + sqrt(cplx::detail::__sqr(__x) + _Tp(1))); return complex<_Tp>(sycl::copysign(__z.real(), __x.real()), sycl::copysign(__z.imag(), __x.imag())); } @@ -1057,7 +783,8 @@ __DPCPP_SYCL_EXTERNAL complex<_Tp> asinh(const complex<_Tp> &__x) { // acosh template ::value>> -__DPCPP_SYCL_EXTERNAL complex<_Tp> acosh(const complex<_Tp> &__x) { +__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> +acosh(const complex<_Tp> &__x) { const _Tp __pi(sycl::atan2(_Tp(+0.), _Tp(-0.))); if (sycl::isinf(__x.real())) { if (sycl::isnan(__x.imag())) @@ -1082,7 +809,7 @@ __DPCPP_SYCL_EXTERNAL complex<_Tp> acosh(const complex<_Tp> &__x) { if (sycl::isinf(__x.imag())) return complex<_Tp>(sycl::fabs(__x.imag()), sycl::copysign(__pi / _Tp(2), __x.imag())); - complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1))); + complex<_Tp> __z = log(__x + sqrt(cplx::detail::__sqr(__x) - _Tp(1))); return complex<_Tp>(sycl::copysign(__z.real(), _Tp(0)), sycl::copysign(__z.imag(), __x.imag())); } @@ -1090,7 +817,8 @@ __DPCPP_SYCL_EXTERNAL complex<_Tp> acosh(const complex<_Tp> &__x) { // atanh template ::value>> -__DPCPP_SYCL_EXTERNAL complex<_Tp> atanh(const complex<_Tp> &__x) { +__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> +atanh(const complex<_Tp> &__x) { const _Tp __pi(sycl::atan2(_Tp(+0.), _Tp(-0.))); if (sycl::isinf(__x.imag())) { return complex<_Tp>(sycl::copysign(_Tp(0), __x.real()), @@ -1120,7 +848,8 @@ __DPCPP_SYCL_EXTERNAL complex<_Tp> atanh(const complex<_Tp> &__x) { // sinh template ::value>> -__DPCPP_SYCL_EXTERNAL complex<_Tp> sinh(const complex<_Tp> &__x) { +__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> +sinh(const complex<_Tp> &__x) { if (sycl::isinf(__x.real()) && !sycl::isfinite(__x.imag())) return complex<_Tp>(__x.real(), _Tp(NAN)); if (__x.real() == 0 && !sycl::isfinite(__x.imag())) @@ -1134,7 +863,8 @@ __DPCPP_SYCL_EXTERNAL complex<_Tp> sinh(const complex<_Tp> &__x) { // cosh template ::value>> -__DPCPP_SYCL_EXTERNAL complex<_Tp> cosh(const complex<_Tp> &__x) { +__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> +cosh(const complex<_Tp> &__x) { if (sycl::isinf(__x.real()) && !sycl::isfinite(__x.imag())) return complex<_Tp>(sycl::fabs(__x.real()), _Tp(NAN)); if (__x.real() == 0 && !sycl::isfinite(__x.imag())) @@ -1150,7 +880,8 @@ __DPCPP_SYCL_EXTERNAL complex<_Tp> cosh(const complex<_Tp> &__x) { // tanh template ::value>> -__DPCPP_SYCL_EXTERNAL complex<_Tp> tanh(const complex<_Tp> &__x) { +__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> +tanh(const complex<_Tp> &__x) { if (sycl::isinf(__x.real())) { if (!sycl::isfinite(__x.imag())) return complex<_Tp>(sycl::copysign(_Tp(1), __x.real()), _Tp(0)); @@ -1172,7 +903,8 @@ __DPCPP_SYCL_EXTERNAL complex<_Tp> tanh(const complex<_Tp> &__x) { // asin template ::value>> -__DPCPP_SYCL_EXTERNAL complex<_Tp> asin(const complex<_Tp> &__x) { +__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> +asin(const complex<_Tp> &__x) { complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real())); return complex<_Tp>(__z.imag(), -__z.real()); } @@ -1180,7 +912,8 @@ __DPCPP_SYCL_EXTERNAL complex<_Tp> asin(const complex<_Tp> &__x) { // acos template ::value>> -__DPCPP_SYCL_EXTERNAL complex<_Tp> acos(const complex<_Tp> &__x) { +__DPCPP_SYCL_EXTERNAL _SYCL_EXT_CPLX_INLINE_VISIBILITY complex<_Tp> +acos(const complex<_Tp> &__x) { const _Tp __pi(sycl::atan2(_Tp(+0.), _Tp(-0.))); if (sycl::isinf(__x.real())) { if (sycl::isnan(__x.imag())) @@ -1205,7 +938,7 @@ __DPCPP_SYCL_EXTERNAL complex<_Tp> acos(const complex<_Tp> &__x) { return complex<_Tp>(__pi / _Tp(2), -__x.imag()); if (__x.real() == 0 && (__x.imag() == 0 || sycl::isnan(__x.imag()))) return complex<_Tp>(__pi / _Tp(2), -__x.imag()); - complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1))); + complex<_Tp> __z = log(__x + sqrt(cplx::detail::__sqr(__x) - _Tp(1))); if (sycl::signbit(__x.imag())) return complex<_Tp>(sycl::fabs(__z.imag()), sycl::fabs(__z.real())); return complex<_Tp>(sycl::fabs(__z.imag()), -sycl::fabs(__z.real())); @@ -1246,70 +979,6 @@ tan(const complex<_Tp> &__x) { return complex<_Tp>(__z.imag(), -__z.real()); } -template -basic_istream<_CharT, _Traits> &operator>>(basic_istream<_CharT, _Traits> &__is, - complex<_Tp> &__x) { - if (__is.good()) { - ws(__is); - if (__is.peek() == _CharT('(')) { - __is.get(); - _Tp __r; - __is >> __r; - if (!__is.fail()) { - ws(__is); - _CharT __c = __is.peek(); - if (__c == _CharT(',')) { - __is.get(); - _Tp __i; - __is >> __i; - if (!__is.fail()) { - ws(__is); - __c = __is.peek(); - if (__c == _CharT(')')) { - __is.get(); - __x = complex<_Tp>(__r, __i); - } else - __is.setstate(__is.failbit); - } else - __is.setstate(__is.failbit); - } else if (__c == _CharT(')')) { - __is.get(); - __x = complex<_Tp>(__r, _Tp(0)); - } else - __is.setstate(__is.failbit); - } else - __is.setstate(__is.failbit); - } else { - _Tp __r; - __is >> __r; - if (!__is.fail()) - __x = complex<_Tp>(__r, _Tp(0)); - else - __is.setstate(__is.failbit); - } - } else - __is.setstate(__is.failbit); - return __is; -} - -template -basic_ostream<_CharT, _Traits> &operator<<(basic_ostream<_CharT, _Traits> &__os, - const complex<_Tp> &__x) { - basic_ostringstream<_CharT, _Traits> __s; - __s.flags(__os.flags()); - __s.imbue(__os.getloc()); - __s.precision(__os.precision()); - __s << '(' << __x.real() << ',' << __x.imag() << ')'; - return __os << __s.str(); -} - -template ::value>> -__DPCPP_SYCL_EXTERNAL - _SYCL_EXT_CPLX_INLINE_VISIBILITY inline const sycl::stream & - operator<<(const sycl::stream &__ss, const complex<_Tp> &_x) { - return __ss << "(" << _x.real() << "," << _x.imag() << ")"; -} - } // namespace experimental } // namespace oneapi } // namespace ext diff --git a/sycl/test/extensions/test_complex.cpp b/sycl/test/extensions/test_complex.cpp index 045554665e1db..60b466901e0d8 100644 --- a/sycl/test/extensions/test_complex.cpp +++ b/sycl/test/extensions/test_complex.cpp @@ -138,10 +138,62 @@ void check_is_gencomplex() { static_assert(is_gencomplex>::value == false); } +// Check that a std::complex can be cast to a sycl::complex using all genfloat +// types +void check_std_to_sycl_conversion() { + auto complex_f = sycl::ext::oneapi::experimental::complex{42.f, 42.f}; + auto complex_d = sycl::ext::oneapi::experimental::complex{42.f, 42.f}; + auto complex_h = + sycl::ext::oneapi::experimental::complex{42.f, 42.f}; + + { + auto f = static_cast>(complex_f); + auto d = static_cast>(complex_f); + auto h = static_cast>(complex_f); + } + { + auto f = static_cast>(complex_d); + auto d = static_cast>(complex_d); + auto h = static_cast>(complex_d); + } + { + auto f = static_cast>(complex_h); + auto d = static_cast>(complex_h); + auto h = static_cast>(complex_h); + } +} + +// Check that a sycl::complex can be constructed from a std::complex +void check_sycl_constructor_from_std() { + auto complex_f = std::complex{42.f, 42.f}; + auto complex_d = std::complex{42.f, 42.f}; + auto complex_h = std::complex{42.f, 42.f}; + + { + sycl::ext::oneapi::experimental::complex f{complex_f}; + sycl::ext::oneapi::experimental::complex d{complex_f}; + sycl::ext::oneapi::experimental::complex h{complex_f}; + } + { + sycl::ext::oneapi::experimental::complex f{complex_d}; + sycl::ext::oneapi::experimental::complex d{complex_d}; + sycl::ext::oneapi::experimental::complex h{complex_d}; + } + { + sycl::ext::oneapi::experimental::complex f{complex_h}; + sycl::ext::oneapi::experimental::complex d{complex_h}; + sycl::ext::oneapi::experimental::complex h{complex_h}; + } +} + int main() { check_math_function_types(); check_math_operator_types(); + check_is_gencomplex(); + check_std_to_sycl_conversion(); + check_sycl_constructor_from_std(); + return 0; }