Skip to content

[libc++] Add std::fpclassify overloads for floating-point. #67913

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

Merged
merged 7 commits into from
Oct 5, 2023
15 changes: 13 additions & 2 deletions libcxx/include/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,19 @@ namespace __math {

// fpclassify

template <class _A1, std::__enable_if_t<std::is_floating_point<_A1>::value, int> = 0>
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI int fpclassify(_A1 __x) _NOEXCEPT {
// template on non-double overloads to make them weaker than same overloads from MSVC runtime
template <class = int>
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI int fpclassify(float __x) _NOEXCEPT {
return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, __x);
}

template <class = int>
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI int fpclassify(double __x) _NOEXCEPT {
return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, __x);
}

template <class = int>
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI int fpclassify(long double __x) _NOEXCEPT {
return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, __x);
}

Expand Down
8 changes: 8 additions & 0 deletions libcxx/test/std/numerics/c.math/cmath.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,12 +603,20 @@ void test_fpclassify()
static_assert((std::is_same<decltype(std::fpclassify(0)), int>::value), "");
static_assert((std::is_same<decltype(std::fpclassify((long double)0)), int>::value), "");
static_assert((std::is_same<decltype(fpclassify(Ambiguous())), Ambiguous>::value), "");
static_assert((std::is_same<decltype(fpclassify(Value<float>())), int>::value), "");
static_assert((std::is_same<decltype(fpclassify(Value<double>())), int>::value), "");
static_assert((std::is_same<decltype(fpclassify(Value<long double>())), int>::value), "");
ASSERT_NOEXCEPT(std::fpclassify((float)0));
ASSERT_NOEXCEPT(std::fpclassify((double)0));
ASSERT_NOEXCEPT(std::fpclassify((long double)0));
ASSERT_NOEXCEPT(std::fpclassify(0));
assert(std::fpclassify(-1.0) == FP_NORMAL);
assert(std::fpclassify(0) == FP_ZERO);
assert(std::fpclassify(1) == FP_NORMAL);
assert(std::fpclassify(-1) == FP_NORMAL);
assert(std::fpclassify(std::numeric_limits<int>::max()) == FP_NORMAL);
assert(std::fpclassify(std::numeric_limits<int>::min()) == FP_NORMAL);
assert(std::fpclassify(Value<double, 1>()) == FP_NORMAL);
}

void test_isfinite()
Expand Down