Skip to content

Commit

Permalink
Add floating point overloads for std::signbit
Browse files Browse the repository at this point in the history
By using `_LIBCPP_PREFERRED_OVERLOAD` we make
sure that a given overload is a better match
than an otherwise equally good function
declaration.

Why is there an equally good function
declaration in the first place? Underlying the
Windows SDK is the UCRT, the universal C runtime,
which clang-cl makes use of. The UCRT should
provide only C library headers, but does on top
comes with overloads for all cv-unqualified floating
point types (float, double, long double) for
`std::signbit()` in 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.
  • Loading branch information
robincaloudis committed Sep 5, 2024
1 parent 7804824 commit 8810607
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions libcxx/include/__math/traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,33 @@ 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 {
_LIBCPP_NODISCARD inline _LIBCPP_SIGNBIT_CONSTEXPR
_LIBCPP_HIDE_FROM_ABI
#ifdef _LIBCPP_PREFERRED_OVERLOAD
_LIBCPP_PREFERRED_OVERLOAD
#endif
bool
signbit(float __x) _NOEXCEPT {
return __builtin_signbit(__x);
}

_LIBCPP_NODISCARD inline _LIBCPP_SIGNBIT_CONSTEXPR
_LIBCPP_HIDE_FROM_ABI
#ifdef _LIBCPP_PREFERRED_OVERLOAD
_LIBCPP_PREFERRED_OVERLOAD
#endif
bool
signbit(double __x) _NOEXCEPT {
return __builtin_signbit(__x);
}

_LIBCPP_NODISCARD inline _LIBCPP_SIGNBIT_CONSTEXPR
_LIBCPP_HIDE_FROM_ABI
#ifdef _LIBCPP_PREFERRED_OVERLOAD
_LIBCPP_PREFERRED_OVERLOAD
#endif
bool
signbit(long double __x) _NOEXCEPT {
return __builtin_signbit(__x);
}

Expand Down

0 comments on commit 8810607

Please sign in to comment.