12
12
#include < type_traits>
13
13
#include < limits>
14
14
#include < boost/math/tools/is_constant_evaluated.hpp>
15
+ #include < boost/math/tools/assert.hpp>
15
16
#include < boost/math/ccmath/isnan.hpp>
16
17
#include < boost/math/ccmath/isinf.hpp>
17
18
@@ -20,19 +21,29 @@ namespace boost::math::ccmath {
20
21
namespace detail {
21
22
22
23
template <typename T>
23
- inline constexpr T abs_impl (T x) noexcept
24
+ constexpr T abs_impl (T x) noexcept
24
25
{
25
- return boost::math::ccmath::isnan (x) ? std::numeric_limits<T>::quiet_NaN () :
26
- boost::math::ccmath::isinf (x) ? std::numeric_limits<T>::infinity () :
27
- x == -0 ? T (0 ) :
28
- x == (std::numeric_limits<T>::min)() ? std::numeric_limits<T>::quiet_NaN () :
29
- x > 0 ? x : -x;
26
+ if (boost::math::ccmath::isnan (x))
27
+ {
28
+ return std::numeric_limits<T>::quiet_NaN ();
29
+ }
30
+ else if (x == static_cast <T>(-0 ))
31
+ {
32
+ return static_cast <T>(0 );
33
+ }
34
+
35
+ if constexpr (std::is_integral_v<T>)
36
+ {
37
+ BOOST_MATH_ASSERT (x != (std::numeric_limits<T>::min)());
38
+ }
39
+
40
+ return x >= 0 ? x : -x;
30
41
}
31
42
32
43
} // Namespace detail
33
44
34
45
template <typename T, std::enable_if_t <!std::is_unsigned_v<T>, bool > = true >
35
- inline constexpr T abs (T x) noexcept
46
+ constexpr T abs (T x) noexcept
36
47
{
37
48
if (BOOST_MATH_IS_CONSTANT_EVALUATED (x))
38
49
{
@@ -48,7 +59,7 @@ inline constexpr T abs(T x) noexcept
48
59
// If abs() is called with an argument of type X for which is_unsigned_v<X> is true and if X
49
60
// cannot be converted to int by integral promotion (7.3.7), the program is ill-formed.
50
61
template <typename T, std::enable_if_t <std::is_unsigned_v<T>, bool > = true >
51
- inline constexpr T abs (T x) noexcept
62
+ constexpr T abs (T x) noexcept
52
63
{
53
64
if constexpr (std::is_convertible_v<T, int >)
54
65
{
@@ -61,12 +72,12 @@ inline constexpr T abs(T x) noexcept
61
72
}
62
73
}
63
74
64
- inline constexpr long int labs (long int j) noexcept
75
+ constexpr long int labs (long int j) noexcept
65
76
{
66
77
return boost::math::ccmath::abs (j);
67
78
}
68
79
69
- inline constexpr long long int llabs (long long int j) noexcept
80
+ constexpr long long int llabs (long long int j) noexcept
70
81
{
71
82
return boost::math::ccmath::abs (j);
72
83
}
0 commit comments