Skip to content

Commit

Permalink
Merge pull request #690 from mborland/constexpr_abs_overloads
Browse files Browse the repository at this point in the history
Add abs specializations to ccmath
  • Loading branch information
jzmaddock authored Sep 12, 2021
2 parents ee919fa + 5f48b50 commit e592fae
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
16 changes: 13 additions & 3 deletions include/boost/math/ccmath/abs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace boost::math::ccmath {
namespace detail {

template <typename T>
inline constexpr T abs_impl(T x)
inline constexpr T abs_impl(T x) noexcept
{
return boost::math::ccmath::isnan(x) ? std::numeric_limits<T>::quiet_NaN() :
boost::math::ccmath::isinf(x) ? std::numeric_limits<T>::infinity() :
Expand All @@ -32,7 +32,7 @@ inline constexpr T abs_impl(T x)
} // Namespace detail

template <typename T, std::enable_if_t<!std::is_unsigned_v<T>, bool> = true>
inline constexpr T abs(T x)
inline constexpr T abs(T x) noexcept
{
if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
{
Expand All @@ -48,7 +48,7 @@ inline constexpr T abs(T x)
// If abs() is called with an argument of type X for which is_unsigned_v<X> is true and if X
// cannot be converted to int by integral promotion (7.3.7), the program is ill-formed.
template <typename T, std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
inline constexpr T abs(T x)
inline constexpr T abs(T x) noexcept
{
if constexpr (std::is_convertible_v<T, int>)
{
Expand All @@ -61,6 +61,16 @@ inline constexpr T abs(T x)
}
}

inline constexpr long int labs(long int j) noexcept
{
return boost::math::ccmath::abs(j);
}

inline constexpr long long int llabs(long long int j) noexcept
{
return boost::math::ccmath::abs(j);
}

} // Namespaces

#endif // BOOST_MATH_CCMATH_ABS
14 changes: 13 additions & 1 deletion include/boost/math/ccmath/fabs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,23 @@
namespace boost::math::ccmath {

template <typename T>
inline constexpr auto fabs(T x)
inline constexpr auto fabs(T x) noexcept
{
return boost::math::ccmath::abs(x);
}

inline constexpr float fabsf(float x) noexcept
{
return boost::math::ccmath::abs(x);
}

#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
inline constexpr long double fabsl(long double x) noexcept
{
return boost::math::ccmath::abs(x);
}
#endif

}

#endif // BOOST_MATH_CCMATH_FABS
1 change: 1 addition & 0 deletions test/ccmath_abs_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ int main()

test<int>();
test<long>();
test<long long>();
test<std::int32_t>();
test<std::int64_t>();

Expand Down

0 comments on commit e592fae

Please sign in to comment.