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

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

Merged
merged 5 commits into from
Sep 12, 2024
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
18 changes: 15 additions & 3 deletions libcxx/include/__math/traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <__config>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_arithmetic.h>
#include <__type_traits/is_floating_point.h>
#include <__type_traits/is_integral.h>
#include <__type_traits/is_signed.h>
#include <__type_traits/promote.h>
Expand All @@ -34,8 +33,21 @@ namespace __math {
# define _LIBCPP_SIGNBIT_CONSTEXPR
#endif

template <class _A1, __enable_if_t<is_floating_point<_A1>::value, int> = 0>
_LIBCPP_NODISCARD inline _LIBCPP_SIGNBIT_CONSTEXPR _LIBCPP_HIDE_FROM_ABI bool signbit(_A1 __x) _NOEXCEPT {
// The universal C runtime (UCRT) in the WinSDK provides floating point overloads
robincaloudis marked this conversation as resolved.
Show resolved Hide resolved
// for std::signbit(). By defining our overloads as templates, we can work around
// this issue as templates are less preferred than non-template functions.
template <class = void>
_LIBCPP_NODISCARD inline _LIBCPP_SIGNBIT_CONSTEXPR _LIBCPP_HIDE_FROM_ABI bool signbit(float __x) _NOEXCEPT {
return __builtin_signbit(__x);
}

template <class = void>
_LIBCPP_NODISCARD inline _LIBCPP_SIGNBIT_CONSTEXPR _LIBCPP_HIDE_FROM_ABI bool signbit(double __x) _NOEXCEPT {
return __builtin_signbit(__x);
}

template <class = void>
_LIBCPP_NODISCARD inline _LIBCPP_SIGNBIT_CONSTEXPR _LIBCPP_HIDE_FROM_ABI bool signbit(long double __x) _NOEXCEPT {
return __builtin_signbit(__x);
}

Expand Down
13 changes: 13 additions & 0 deletions libcxx/test/std/numerics/c.math/signbit.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,22 @@ struct TestInt {
}
};

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

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::signbit` with convertible types. This checks
// whether overloads for all cv-unqualified floating-point types are working
// as expected.
{
assert(!std::signbit(ConvertibleTo<float>()));
assert(!std::signbit(ConvertibleTo<double>()));
assert(!std::signbit(ConvertibleTo<long double>()));
}
return 0;
}
Loading