aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/26_numerics
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2023-05-10 12:20:58 +0100
committerJonathan Wakely <jwakely@redhat.com>2023-05-11 12:16:21 +0100
commitaf595613acbd9863198ae69c7b1c9e856bca9e4f (patch)
tree5b50c79ef814a994cc3b13d59f709db1338b6381 /libstdc++-v3/testsuite/26_numerics
parent0918360d709a32294913aef4392cec6c1dec1857 (diff)
downloadgcc-af595613acbd9863198ae69c7b1c9e856bca9e4f.zip
gcc-af595613acbd9863198ae69c7b1c9e856bca9e4f.tar.gz
gcc-af595613acbd9863198ae69c7b1c9e856bca9e4f.tar.bz2
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.
Diffstat (limited to 'libstdc++-v3/testsuite/26_numerics')
-rw-r--r--libstdc++-v3/testsuite/26_numerics/headers/cmath/109758.cc52
1 files changed, 52 insertions, 0 deletions
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 0000000..c9716d3
--- /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 <cmath>
+#include <testsuite_hooks.h>
+
+#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