From e88d863cbde6ee90709d291670a843346f84d3b6 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Tue, 25 Jun 2019 14:18:36 +0100 Subject: [PATCH] Fix non-portable use of std::abs(double) in constexpr function Although libstdc++ adds 'constexpr' to its std::abs(floating-point) overloads (as a non-conforming extension), those overloads are not used if the target libc provides them, which is the case on Solaris. The fix is to avoid std::abs and simply apply the negation when needed. * include/std/numeric (midpoint(T, T)): Avoid std::abs in constexpr function. From-SVN: r272653 --- libstdc++-v3/ChangeLog | 5 +++++ libstdc++-v3/include/std/numeric | 9 +++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index d7392553901..7222810984d 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,8 @@ +2019-06-25 Jonathan Wakely + + * include/std/numeric (midpoint(T, T)): Avoid std::abs in constexpr + function. + 2019-06-25 Jakub Jelinek * include/pstl/pstl_config.h (_PSTL_PRAGMA_SIMD_SCAN, diff --git a/libstdc++-v3/include/std/numeric b/libstdc++-v3/include/std/numeric index af684469769..2f9462183dd 100644 --- a/libstdc++-v3/include/std/numeric +++ b/libstdc++-v3/include/std/numeric @@ -162,7 +162,6 @@ _GLIBCXX_END_NAMESPACE_VERSION #if __cplusplus > 201703L #include -#include namespace std _GLIBCXX_VISIBILITY(default) { @@ -196,11 +195,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { constexpr _Tp __lo = numeric_limits<_Tp>::min() * 2; constexpr _Tp __hi = numeric_limits<_Tp>::max() / 2; - if (std::abs(__a) <= __hi && std::abs(__b) <= __hi) [[likely]] + const _Tp __abs_a = __a < 0 ? -__a : __a; + const _Tp __abs_b = __b < 0 ? -__b : __b; + if (__abs_a <= __hi && __abs_b <= __hi) [[likely]] return (__a + __b) / 2; // always correctly rounded - if (std::abs(__a) < __lo) // not safe to halve __a + if (__abs_a < __lo) // not safe to halve __a return __a + __b/2; - if (std::abs(__b) < __lo) // not safe to halve __b + if (__abs_b < __lo) // not safe to halve __b return __a/2 + __b; return __a/2 + __b/2; // otherwise correctly rounded } -- 2.30.2