Skip to content

Commit ebb3ab2

Browse files
philnik777dvbuka
authored andcommitted
[libc++] Simplify std::strong_order (llvm#164233)
This patch does two things: 1) The branches for `sizeof(_Dp) == sizeof(int32_t)` and `sizeof(_Dp) == sizeof(int64_t)` are merged, since these are basically identical. 2) The branch where `numeric_limits<_Dp>::radix != 2` is removed, since no platform we support (nor any I'm aware of) have a floating point type with `radix != 2`. This means that the branch is basically dead code.
1 parent c869ae4 commit ebb3ab2

File tree

1 file changed

+12
-30
lines changed

1 file changed

+12
-30
lines changed

libcxx/include/__compare/strong_order.h

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <__compare/compare_three_way.h>
1414
#include <__compare/ordering.h>
1515
#include <__config>
16-
#include <__math/exponential_functions.h>
1716
#include <__math/traits.h>
1817
#include <__type_traits/conditional.h>
1918
#include <__type_traits/decay.h>
@@ -53,38 +52,21 @@ struct __fn {
5352
template <class _Tp, class _Up, class _Dp = decay_t<_Tp>>
5453
requires is_same_v<_Dp, decay_t<_Up>> && is_floating_point_v<_Dp>
5554
_LIBCPP_HIDE_FROM_ABI static constexpr strong_ordering __go(_Tp&& __t, _Up&& __u, __priority_tag<1>) noexcept {
56-
if constexpr (numeric_limits<_Dp>::is_iec559 && sizeof(_Dp) == sizeof(int32_t)) {
57-
int32_t __rx = std::bit_cast<int32_t>(__t);
58-
int32_t __ry = std::bit_cast<int32_t>(__u);
59-
__rx = (__rx < 0) ? (numeric_limits<int32_t>::min() - __rx - 1) : __rx;
60-
__ry = (__ry < 0) ? (numeric_limits<int32_t>::min() - __ry - 1) : __ry;
61-
return (__rx <=> __ry);
62-
} else if constexpr (numeric_limits<_Dp>::is_iec559 && sizeof(_Dp) == sizeof(int64_t)) {
63-
int64_t __rx = std::bit_cast<int64_t>(__t);
64-
int64_t __ry = std::bit_cast<int64_t>(__u);
65-
__rx = (__rx < 0) ? (numeric_limits<int64_t>::min() - __rx - 1) : __rx;
66-
__ry = (__ry < 0) ? (numeric_limits<int64_t>::min() - __ry - 1) : __ry;
55+
if constexpr (numeric_limits<_Dp>::is_iec559 &&
56+
(sizeof(_Dp) == sizeof(int32_t) || sizeof(_Dp) == sizeof(int64_t))) {
57+
using _IntT = conditional_t<sizeof(_Dp) == sizeof(int32_t), int32_t, int64_t>;
58+
_IntT __rx = std::bit_cast<_IntT>(__t);
59+
_IntT __ry = std::bit_cast<_IntT>(__u);
60+
__rx = (__rx < 0) ? (numeric_limits<_IntT>::min() - __rx - 1) : __rx;
61+
__ry = (__ry < 0) ? (numeric_limits<_IntT>::min() - __ry - 1) : __ry;
6762
return (__rx <=> __ry);
6863
} else if (__t < __u) {
6964
return strong_ordering::less;
7065
} else if (__t > __u) {
7166
return strong_ordering::greater;
7267
} else if (__t == __u) {
73-
if constexpr (numeric_limits<_Dp>::radix == 2) {
74-
return __math::signbit(__u) <=> __math::signbit(__t);
75-
} else {
76-
// This is bullet 3 of the IEEE754 algorithm, relevant
77-
// only for decimal floating-point;
78-
// see https://stackoverflow.com/questions/69068075/
79-
if (__t == 0 || __math::isinf(__t)) {
80-
return __math::signbit(__u) <=> __math::signbit(__t);
81-
} else {
82-
int __texp, __uexp;
83-
(void)__math::frexp(__t, &__texp);
84-
(void)__math::frexp(__u, &__uexp);
85-
return (__t < 0) ? (__texp <=> __uexp) : (__uexp <=> __texp);
86-
}
87-
}
68+
static_assert(numeric_limits<_Dp>::radix == 2, "floating point type with a radix other than 2?");
69+
return __math::signbit(__u) <=> __math::signbit(__t);
8870
} else {
8971
// They're unordered, so one of them must be a NAN.
9072
// The order is -QNAN, -SNAN, numbers, +SNAN, +QNAN.
@@ -93,9 +75,9 @@ struct __fn {
9375
bool __t_is_negative = __math::signbit(__t);
9476
bool __u_is_negative = __math::signbit(__u);
9577
using _IntType =
96-
conditional_t< sizeof(__t) == sizeof(int32_t),
97-
int32_t,
98-
conditional_t< sizeof(__t) == sizeof(int64_t), int64_t, void> >;
78+
conditional_t<sizeof(__t) == sizeof(int32_t),
79+
int32_t,
80+
conditional_t<sizeof(__t) == sizeof(int64_t), int64_t, void>>;
9981
if constexpr (is_same_v<_IntType, void>) {
10082
static_assert(sizeof(_Dp) == 0, "std::strong_order is unimplemented for this floating-point type");
10183
} else if (__t_is_nan && __u_is_nan) {

0 commit comments

Comments
 (0)