Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add abs specializations to ccmath #690

Merged
merged 1 commit into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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