[libcxx-commits] [libcxx] [libc++] Check explicit values in the partial_ordering comparators for better code gen (PR #81366)

via libcxx-commits libcxx-commits at lists.llvm.org
Sat Feb 10 08:43:35 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

<details>
<summary>Changes</summary>

This allows the compiler to check for specific bit patterns instead of value ranges.


---
Full diff: https://github.com/llvm/llvm-project/pull/81366.diff


1 Files Affected:

- (modified) libcxx/include/__compare/ordering.h (+9-13) 


``````````diff
diff --git a/libcxx/include/__compare/ordering.h b/libcxx/include/__compare/ordering.h
index 2995d381304f0e..0bd9c5f27b3c1c 100644
--- a/libcxx/include/__compare/ordering.h
+++ b/libcxx/include/__compare/ordering.h
@@ -47,10 +47,6 @@ class partial_ordering {
 
   _LIBCPP_HIDE_FROM_ABI explicit constexpr partial_ordering(_NCmpResult __v) noexcept : __value_(_ValueT(__v)) {}
 
-  _LIBCPP_HIDE_FROM_ABI constexpr bool __is_ordered() const noexcept {
-    return __value_ != _ValueT(_NCmpResult::__unordered);
-  }
-
 public:
   // valid values
   static const partial_ordering less;
@@ -62,39 +58,39 @@ class partial_ordering {
   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(partial_ordering, partial_ordering) noexcept = default;
 
   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
-    return __v.__is_ordered() && __v.__value_ == 0;
+    return __v.__value_ == 0;
   }
 
   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
-    return __v.__is_ordered() && __v.__value_ < 0;
+    return __v.__value_ == -1;
   }
 
   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
-    return __v.__is_ordered() && __v.__value_ <= 0;
+    return __v.__value_ == 0 || __v.__value_ == -1;
   }
 
   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
-    return __v.__is_ordered() && __v.__value_ > 0;
+    return __v.__value_ == 1;
   }
 
   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
-    return __v.__is_ordered() && __v.__value_ >= 0;
+    return __v.__value_ == 0 || __v.__value_ == 1;
   }
 
   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
-    return __v.__is_ordered() && 0 < __v.__value_;
+    return __v.__value_ == 1;
   }
 
   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
-    return __v.__is_ordered() && 0 <= __v.__value_;
+    return __v.__value_ == 0 || __v.__value_ == 1;
   }
 
   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
-    return __v.__is_ordered() && 0 > __v.__value_;
+    return __v.__value_ == -1;
   }
 
   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
-    return __v.__is_ordered() && 0 >= __v.__value_;
+    return __v.__value_ == 0 || __v.__value_ == -1;
   }
 
   _LIBCPP_HIDE_FROM_ABI friend constexpr partial_ordering

``````````

</details>


https://github.com/llvm/llvm-project/pull/81366


More information about the libcxx-commits mailing list