Skip to content

Commit c6b2aa1

Browse files
[libc++][math] Provide overloads for cv-unqualified floating point types for std::signbit (#106566)
## Why Following up on #105946, this patch provides the floating point overloads for `std::signbit` as defined by [P0533R9](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0533r9.pdf). ## What * Test and add overloads for cv-unqualified floating point types * Remove constrained overload as it is not needed anymore * Make use of `template<class = void>` as the universal C runtime (UCRT) needed for Clang-Cl comes with overloads for all cv-unqualified floating point types (float, double, long double) for `std::signbit()` by itself [in the WinSDK](https://github.com/microsoft/win32metadata/blob/e012b29924c53aa941fc010850b68331b0c3ea80/generation/WinSDK/RecompiledIdlHeaders/ucrt/corecrt_math.h#L309-L322). In a certain way, this can be seen as a deviation from the C standard. We need to work around it as the compilation would otherwise error out due to duplicated definitions.
1 parent 3332552 commit c6b2aa1

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

libcxx/include/__math/traits.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <__config>
1313
#include <__type_traits/enable_if.h>
1414
#include <__type_traits/is_arithmetic.h>
15-
#include <__type_traits/is_floating_point.h>
1615
#include <__type_traits/is_integral.h>
1716
#include <__type_traits/is_signed.h>
1817
#include <__type_traits/promote.h>
@@ -34,8 +33,21 @@ namespace __math {
3433
# define _LIBCPP_SIGNBIT_CONSTEXPR
3534
#endif
3635

37-
template <class _A1, __enable_if_t<is_floating_point<_A1>::value, int> = 0>
38-
_LIBCPP_NODISCARD inline _LIBCPP_SIGNBIT_CONSTEXPR _LIBCPP_HIDE_FROM_ABI bool signbit(_A1 __x) _NOEXCEPT {
36+
// The universal C runtime (UCRT) in the WinSDK provides floating point overloads
37+
// for std::signbit(). By defining our overloads as templates, we can work around
38+
// this issue as templates are less preferred than non-template functions.
39+
template <class = void>
40+
_LIBCPP_NODISCARD inline _LIBCPP_SIGNBIT_CONSTEXPR _LIBCPP_HIDE_FROM_ABI bool signbit(float __x) _NOEXCEPT {
41+
return __builtin_signbit(__x);
42+
}
43+
44+
template <class = void>
45+
_LIBCPP_NODISCARD inline _LIBCPP_SIGNBIT_CONSTEXPR _LIBCPP_HIDE_FROM_ABI bool signbit(double __x) _NOEXCEPT {
46+
return __builtin_signbit(__x);
47+
}
48+
49+
template <class = void>
50+
_LIBCPP_NODISCARD inline _LIBCPP_SIGNBIT_CONSTEXPR _LIBCPP_HIDE_FROM_ABI bool signbit(long double __x) _NOEXCEPT {
3951
return __builtin_signbit(__x);
4052
}
4153

libcxx/test/std/numerics/c.math/signbit.pass.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,22 @@ struct TestInt {
7070
}
7171
};
7272

73+
template <typename T>
74+
struct ConvertibleTo {
75+
operator T() const { return T(); }
76+
};
77+
7378
int main(int, char**) {
7479
types::for_each(types::floating_point_types(), TestFloat());
7580
types::for_each(types::integral_types(), TestInt());
7681

82+
// Make sure we can call `std::signbit` with convertible types. This checks
83+
// whether overloads for all cv-unqualified floating-point types are working
84+
// as expected.
85+
{
86+
assert(!std::signbit(ConvertibleTo<float>()));
87+
assert(!std::signbit(ConvertibleTo<double>()));
88+
assert(!std::signbit(ConvertibleTo<long double>()));
89+
}
7790
return 0;
7891
}

0 commit comments

Comments
 (0)