From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id A97DA385840C; Mon, 18 Mar 2024 14:05:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A97DA385840C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1710770721; bh=28/+nxrqHOINxO1HvotkiPMMPt/wYgOmIsltGPII74Q=; h=From:To:Subject:Date:From; b=Y4YDjrzJ1e1UTyztTuCv8WBfKMzPXfaqxeC7ButmbHMKdjOR12B1zy0pxGUsQ1w6A d1yoyH1u12urK8dR+J3BO8VRtTrCgzaZy3Pw2PHJKCQAEBqLgCzrLvp4j+232d9H4g ArQ9hwY0/O4YH+r01GWIQxwOct6wjfuHmNz4D/NM= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-10244] libstdc++: Fix std::abs(__float128) for -NaN and -0.0 [PR109758] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 93159e1af6e1d42e0e595bdc49da002fc830353c X-Git-Newrev: 9f381ebb7211c1359f5de87760148096fcba3357 Message-Id: <20240318140521.A97DA385840C@sourceware.org> Date: Mon, 18 Mar 2024 14:05:21 +0000 (GMT) List-Id: https://gcc.gnu.org/g:9f381ebb7211c1359f5de87760148096fcba3357 commit r12-10244-g9f381ebb7211c1359f5de87760148096fcba3357 Author: Jonathan Wakely Date: Wed May 10 12:20:58 2023 +0100 libstdc++: Fix std::abs(__float128) for -NaN and -0.0 [PR109758] The current implementation of this non-standard overload of std::abs incorrectly returns a negative value for negative NaNs and negative zero, because x < 0 is false in both cases. Use fabsl(long double) or fabsf128(_Float128) if those do the right thing. Otherwise, use __builtin_signbit(x) instead of x < 0 to detect negative inputs. This assumes that __builtin_signbit handles __float128 correctly, but that seems to be true for all of GCC, clang and icc. libstdc++-v3/ChangeLog: PR libstdc++/109758 * include/bits/std_abs.h (abs(__float128)): Handle negative NaN and negative zero correctly. * testsuite/26_numerics/headers/cmath/109758.cc: New test. (cherry picked from commit af595613acbd9863198ae69c7b1c9e856bca9e4f) Diff: --- libstdc++-v3/include/bits/std_abs.h | 13 +++++- .../testsuite/26_numerics/headers/cmath/109758.cc | 52 ++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/bits/std_abs.h b/libstdc++-v3/include/bits/std_abs.h index c8d589d2b0a..bd14addd159 100644 --- a/libstdc++-v3/include/bits/std_abs.h +++ b/libstdc++-v3/include/bits/std_abs.h @@ -101,11 +101,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __extension__ inline _GLIBCXX_CONSTEXPR __float128 abs(__float128 __x) - { return __x < 0 ? -__x : __x; } + { +#if defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128) + return __builtin_fabsl(__x); +#elif defined(_GLIBCXX_HAVE_FLOAT128_MATH) + return __builtin_fabsf128(__x); +#else + // Assume that __builtin_signbit works for __float128. + return __builtin_signbit(__x) ? -__x : __x; +#endif + } #endif _GLIBCXX_END_NAMESPACE_VERSION } // namespace -} // extern "C"++" +} // extern "C++" #endif // _GLIBCXX_BITS_STD_ABS_H diff --git a/libstdc++-v3/testsuite/26_numerics/headers/cmath/109758.cc b/libstdc++-v3/testsuite/26_numerics/headers/cmath/109758.cc new file mode 100644 index 00000000000..c9716d3d372 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/headers/cmath/109758.cc @@ -0,0 +1,52 @@ +// { dg-do run } +// PR libstdc++/109758 std::abs(__float128) doesn't support NaN + +#include +#include + +#if !defined(__STRICT_ANSI__) && defined(_GLIBCXX_USE_FLOAT128) +void +test_nan() +{ + __float128 nan = __builtin_nanl(""); + VERIFY( !__builtin_signbit(std::abs(nan)) ); + VERIFY( !__builtin_signbit(std::abs(-nan)) ); +} + +void +test_zero() +{ + __float128 zero = 0.0; + VERIFY( !__builtin_signbit(std::abs(zero)) ); + VERIFY( !__builtin_signbit(std::abs(zero * -2.0)) ); +} + +void +test_neg() +{ + VERIFY( std::abs((__float128)-1.0) == -1.0 ); + VERIFY( std::abs((__float128)-2e9) == -2e9 ); + VERIFY( std::abs((__float128)-3e-4) == 3e-4 ); +} + +void +test_inf() +{ + __float128 inf = __builtin_huge_vall(); + VERIFY( std::abs(inf) == inf ); + VERIFY( std::abs(-inf) == inf ); +} + +#if __cplusplus >= 201103L +static_assert( std::abs((__float128)-1.0) == (__float128)1.0, + "std::abs(__float128) is usable in constant expressions" ); +#endif + +int main() +{ + test_nan(); + test_zero(); +} +#else +int main() { } +#endif