From 4f75543dc417cee2db2fc46f516f7e3137e4b250 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Wed, 1 May 2019 01:08:36 +0100 Subject: PR libstdc++/61761 fix std::proj for targets without C99 cproj The current generic implementation of __complex_proj used when cproj is not available calculates the wrong projection, giving a different result than given by C99's cproj. When C99 cproj is not available but isinf and copysign are, use those to give correct results for float, double and long double. Otherwise, and for other specializations of std::complex, just use a generic version that returns its argument, and so doesn't support infinities. We might want to consider adding additional overloads of __complex_proj to support extended types such as _Float64x, _Float128 etc. PR libstdc++/61761 * include/std/complex (__complex_proj): Return parameter unchanged. [_GLIBCXX_USE_C99_COMPLEX] (__complex_proj): Change overloads for floating-point types to take std::complex arguments. [_GLIBCXX_USE_C99_MATH_TR1] (__complex_proj): Add overloads for floating-point types. * testsuite/26_numerics/complex/proj.cc: New test. From-SVN: r270759 --- libstdc++-v3/include/std/complex | 68 +++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 25 deletions(-) (limited to 'libstdc++-v3/include') diff --git a/libstdc++-v3/include/std/complex b/libstdc++-v3/include/std/complex index 0a4f68b..45450e8 100644 --- a/libstdc++-v3/include/std/complex +++ b/libstdc++-v3/include/std/complex @@ -1898,41 +1898,59 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template std::complex<_Tp> proj(const std::complex<_Tp>&); + // Generic implementation of std::proj, does not work for infinities. template - std::complex<_Tp> + inline std::complex<_Tp> __complex_proj(const std::complex<_Tp>& __z) - { - const _Tp __den = (__z.real() * __z.real() - + __z.imag() * __z.imag() + _Tp(1.0)); - - return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den, - (_Tp(2.0) * __z.imag()) / __den); - } + { return __z; } #if _GLIBCXX_USE_C99_COMPLEX - inline __complex__ float - __complex_proj(__complex__ float __z) - { return __builtin_cprojf(__z); } - - inline __complex__ double - __complex_proj(__complex__ double __z) - { return __builtin_cproj(__z); } - - inline __complex__ long double - __complex_proj(const __complex__ long double& __z) - { return __builtin_cprojl(__z); } + inline complex + __complex_proj(const complex& __z) + { return __builtin_cprojf(__z.__rep()); } + + inline complex + __complex_proj(const complex& __z) + { return __builtin_cproj(__z.__rep()); } + + inline complex + __complex_proj(const complex& __z) + { return __builtin_cprojl(__z.__rep()); } +#elif defined _GLIBCXX_USE_C99_MATH_TR1 + inline complex + __complex_proj(const complex& __z) + { + if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag())) + return complex(__builtin_inff(), + __builtin_copysignf(0.0f, __z.imag())); + return __z; + } + + inline complex + __complex_proj(const complex& __z) + { + if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag())) + return complex(__builtin_inf(), + __builtin_copysign(0.0, __z.imag())); + return __z; + } + + inline complex + __complex_proj(const complex& __z) + { + if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag())) + return complex(__builtin_infl(), + __builtin_copysignl(0.0l, __z.imag())); + return __z; + } +#endif template inline std::complex<_Tp> proj(const std::complex<_Tp>& __z) - { return __complex_proj(__z.__rep()); } -#else - template - inline std::complex<_Tp> - proj(const std::complex<_Tp>& __z) { return __complex_proj(__z); } -#endif + // Overload for scalars template inline std::complex::__type> proj(_Tp __x) -- cgit v1.1