Skip to content

[libc++][math] Provide overloads for cv-unqualified floating point types for std::isnormal #104773

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
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
15 changes: 11 additions & 4 deletions libcxx/include/__math/traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,21 @@ _LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI boo

// isnormal

template <class _A1, __enable_if_t<is_floating_point<_A1>::value, int> = 0>
template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
_LIBCPP_NODISCARD _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnormal(_A1 __x) _NOEXCEPT {
return __x != 0;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The is_floating_point constrained overload is now unused, right? In that case it should be removed.

Copy link
Contributor Author

@robincaloudis robincaloudis Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely. You are right. Corrected it. In addition, I opened #106224 that removes the constrained overloads for std::{isinf,isfinite,isnan} too. Feel free to review both.

_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnormal(float __x) _NOEXCEPT {
return __builtin_isnormal(__x);
}

template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
_LIBCPP_NODISCARD _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnormal(_A1 __x) _NOEXCEPT {
return __x != 0;
_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnormal(double __x) _NOEXCEPT {
return __builtin_isnormal(__x);
}

_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnormal(long double __x) _NOEXCEPT {
return __builtin_isnormal(__x);
}

// isgreater
Expand Down
14 changes: 14 additions & 0 deletions libcxx/test/std/numerics/c.math/isnormal.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,23 @@ struct TestInt {
}
};

template <typename T>
struct ConvertibleTo {
operator T() const { return T(1); }
};

int main(int, char**) {
types::for_each(types::floating_point_types(), TestFloat());
types::for_each(types::integral_types(), TestInt());

// Make sure we can call `std::isnormal` with convertible types. This checks
// whether overloads for all cv-unqualified floating-point types are working
// as expected.
{
assert(std::isnormal(ConvertibleTo<float>()));
assert(std::isnormal(ConvertibleTo<double>()));
assert(std::isnormal(ConvertibleTo<long double>()));
}

return 0;
}
Loading