Skip to content

Commit b07730b

Browse files
authored
[libc++] Check explicit values in the partial_ordering comparators for better code gen (#81366)
This allows the compiler to check for specific bit patterns instead of value ranges.
1 parent a75565a commit b07730b

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

libcxx/include/__compare/ordering.h

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2424
// exposition only
2525
enum class _OrdResult : signed char { __less = -1, __equiv = 0, __greater = 1 };
2626

27-
enum class _NCmpResult : signed char { __unordered = -127 };
27+
enum class _PartialOrdResult : signed char {
28+
__less = static_cast<signed char>(_OrdResult::__less),
29+
__equiv = static_cast<signed char>(_OrdResult::__equiv),
30+
__greater = static_cast<signed char>(_OrdResult::__greater),
31+
__unordered = -127,
32+
};
2833

2934
class partial_ordering;
3035
class weak_ordering;
@@ -47,15 +52,7 @@ struct _CmpUnspecifiedParam {
4752
};
4853

4954
class partial_ordering {
50-
using _ValueT = signed char;
51-
52-
_LIBCPP_HIDE_FROM_ABI explicit constexpr partial_ordering(_OrdResult __v) noexcept : __value_(_ValueT(__v)) {}
53-
54-
_LIBCPP_HIDE_FROM_ABI explicit constexpr partial_ordering(_NCmpResult __v) noexcept : __value_(_ValueT(__v)) {}
55-
56-
_LIBCPP_HIDE_FROM_ABI constexpr bool __is_ordered() const noexcept {
57-
return __value_ != _ValueT(_NCmpResult::__unordered);
58-
}
55+
_LIBCPP_HIDE_FROM_ABI explicit constexpr partial_ordering(_PartialOrdResult __v) noexcept : __value_(__v) {}
5956

6057
public:
6158
// valid values
@@ -68,39 +65,39 @@ class partial_ordering {
6865
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(partial_ordering, partial_ordering) noexcept = default;
6966

7067
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
71-
return __v.__is_ordered() && __v.__value_ == 0;
68+
return __v.__value_ == _PartialOrdResult::__equiv;
7269
}
7370

7471
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
75-
return __v.__is_ordered() && __v.__value_ < 0;
72+
return __v.__value_ == _PartialOrdResult::__less;
7673
}
7774

7875
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
79-
return __v.__is_ordered() && __v.__value_ <= 0;
76+
return __v.__value_ == _PartialOrdResult::__equiv || __v.__value_ == _PartialOrdResult::__less;
8077
}
8178

8279
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
83-
return __v.__is_ordered() && __v.__value_ > 0;
80+
return __v.__value_ == _PartialOrdResult::__greater;
8481
}
8582

8683
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
87-
return __v.__is_ordered() && __v.__value_ >= 0;
84+
return __v.__value_ == _PartialOrdResult::__equiv || __v.__value_ == _PartialOrdResult::__greater;
8885
}
8986

9087
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
91-
return __v.__is_ordered() && 0 < __v.__value_;
88+
return __v.__value_ == _PartialOrdResult::__greater;
9289
}
9390

9491
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
95-
return __v.__is_ordered() && 0 <= __v.__value_;
92+
return __v.__value_ == _PartialOrdResult::__equiv || __v.__value_ == _PartialOrdResult::__greater;
9693
}
9794

9895
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
99-
return __v.__is_ordered() && 0 > __v.__value_;
96+
return __v.__value_ == _PartialOrdResult::__less;
10097
}
10198

10299
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
103-
return __v.__is_ordered() && 0 >= __v.__value_;
100+
return __v.__value_ == _PartialOrdResult::__equiv || __v.__value_ == _PartialOrdResult::__less;
104101
}
105102

106103
_LIBCPP_HIDE_FROM_ABI friend constexpr partial_ordering
@@ -114,13 +111,13 @@ class partial_ordering {
114111
}
115112

116113
private:
117-
_ValueT __value_;
114+
_PartialOrdResult __value_;
118115
};
119116

120-
inline constexpr partial_ordering partial_ordering::less(_OrdResult::__less);
121-
inline constexpr partial_ordering partial_ordering::equivalent(_OrdResult::__equiv);
122-
inline constexpr partial_ordering partial_ordering::greater(_OrdResult::__greater);
123-
inline constexpr partial_ordering partial_ordering::unordered(_NCmpResult ::__unordered);
117+
inline constexpr partial_ordering partial_ordering::less(_PartialOrdResult::__less);
118+
inline constexpr partial_ordering partial_ordering::equivalent(_PartialOrdResult::__equiv);
119+
inline constexpr partial_ordering partial_ordering::greater(_PartialOrdResult::__greater);
120+
inline constexpr partial_ordering partial_ordering::unordered(_PartialOrdResult::__unordered);
124121

125122
class weak_ordering {
126123
using _ValueT = signed char;

0 commit comments

Comments
 (0)