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
@@ -28,19 +29,29 @@ namespace boost::math::ccmath {
28
29
namespace detail {
29
30
30
31
template <typename T>
31
- inline constexpr T abs_impl (T x) noexcept
32
+ constexpr T abs_impl (T x) noexcept
32
33
{
33
- return boost::math::ccmath::isnan (x) ? std::numeric_limits<T>::quiet_NaN () :
34
- boost::math::ccmath::isinf (x) ? std::numeric_limits<T>::infinity () :
35
- x == -0 ? T (0 ) :
36
- x == (std::numeric_limits<T>::min)() ? std::numeric_limits<T>::quiet_NaN () :
37
- x > 0 ? x : -x;
34
+ if (boost::math::ccmath::isnan (x))
35
+ {
36
+ return std::numeric_limits<T>::quiet_NaN ();
37
+ }
38
+ else if (x == static_cast <T>(-0 ))
39
+ {
40
+ return static_cast <T>(0 );
41
+ }
42
+
43
+ if constexpr (std::is_integral_v<T>)
44
+ {
45
+ BOOST_MATH_ASSERT (x != (std::numeric_limits<T>::min)());
46
+ }
47
+
48
+ return x >= 0 ? x : -x;
38
49
}
39
50
40
51
} // Namespace detail
41
52
42
53
template <typename T, std::enable_if_t <!std::is_unsigned_v<T>, bool > = true >
43
- inline constexpr T abs (T x) noexcept
54
+ constexpr T abs (T x) noexcept
44
55
{
45
56
if (BOOST_MATH_IS_CONSTANT_EVALUATED (x))
46
57
{
@@ -56,7 +67,7 @@ inline constexpr T abs(T x) noexcept
56
67
// If abs() is called with an argument of type X for which is_unsigned_v<X> is true and if X
57
68
// cannot be converted to int by integral promotion (7.3.7), the program is ill-formed.
58
69
template <typename T, std::enable_if_t <std::is_unsigned_v<T>, bool > = true >
59
- inline constexpr T abs (T x) noexcept
70
+ constexpr T abs (T x) noexcept
60
71
{
61
72
if constexpr (std::is_convertible_v<T, int >)
62
73
{
@@ -69,12 +80,12 @@ inline constexpr T abs(T x) noexcept
69
80
}
70
81
}
71
82
72
- inline constexpr long int labs (long int j) noexcept
83
+ constexpr long int labs (long int j) noexcept
73
84
{
74
85
return boost::math::ccmath::abs (j);
75
86
}
76
87
77
- inline constexpr long long int llabs (long long int j) noexcept
88
+ constexpr long long int llabs (long long int j) noexcept
78
89
{
79
90
return boost::math::ccmath::abs (j);
80
91
}
0 commit comments