1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef _LIBCPP___COMPARE_STRONG_ORDER 10 #define _LIBCPP___COMPARE_STRONG_ORDER 11 12 #include <__bit/bit_cast.h> 13 #include <__compare/compare_three_way.h> 14 #include <__compare/ordering.h> 15 #include <__config> 16 #include <__type_traits/conditional.h> 17 #include <__type_traits/decay.h> 18 #include <__utility/forward.h> 19 #include <__utility/priority_tag.h> 20 #include <cmath> 21 #include <cstdint> 22 #include <limits> 23 24 #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER 25 # pragma GCC system_header 26 #endif 27 28 _LIBCPP_PUSH_MACROS 29 #include <__undef_macros> 30 31 _LIBCPP_BEGIN_NAMESPACE_STD 32 33 #if _LIBCPP_STD_VER >= 20 34 35 // [cmp.alg] 36 namespace __strong_order { 37 struct __fn { 38 // NOLINTBEGIN(libcpp-robust-against-adl) strong_order should use ADL, but only here 39 template <class _Tp, class _Up> 40 requires is_same_v<decay_t<_Tp>, decay_t<_Up>> 41 _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<2>) noexcept( 42 noexcept(strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u))))) 43 -> decltype(strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) { 44 return strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u))); 45 } 46 // NOLINTEND(libcpp-robust-against-adl) 47 48 template <class _Tp, class _Up, class _Dp = decay_t<_Tp>> 49 requires is_same_v<_Dp, decay_t<_Up>> && is_floating_point_v<_Dp> 50 _LIBCPP_HIDE_FROM_ABI static constexpr strong_ordering __go(_Tp&& __t, _Up&& __u, __priority_tag<1>) noexcept { 51 if constexpr (numeric_limits<_Dp>::is_iec559 && sizeof(_Dp) == sizeof(int32_t)) { 52 int32_t __rx = std::bit_cast<int32_t>(__t); 53 int32_t __ry = std::bit_cast<int32_t>(__u); 54 __rx = (__rx < 0) ? (numeric_limits<int32_t>::min() - __rx - 1) : __rx; 55 __ry = (__ry < 0) ? (numeric_limits<int32_t>::min() - __ry - 1) : __ry; 56 return (__rx <=> __ry); 57 } else if constexpr (numeric_limits<_Dp>::is_iec559 && sizeof(_Dp) == sizeof(int64_t)) { 58 int64_t __rx = std::bit_cast<int64_t>(__t); 59 int64_t __ry = std::bit_cast<int64_t>(__u); 60 __rx = (__rx < 0) ? (numeric_limits<int64_t>::min() - __rx - 1) : __rx; 61 __ry = (__ry < 0) ? (numeric_limits<int64_t>::min() - __ry - 1) : __ry; 62 return (__rx <=> __ry); 63 } else if (__t < __u) { 64 return strong_ordering::less; 65 } else if (__t > __u) { 66 return strong_ordering::greater; 67 } else if (__t == __u) { 68 if constexpr (numeric_limits<_Dp>::radix == 2) { 69 return std::signbit(__u) <=> std::signbit(__t); 70 } else { 71 // This is bullet 3 of the IEEE754 algorithm, relevant 72 // only for decimal floating-point; 73 // see https://stackoverflow.com/questions/69068075/ 74 if (__t == 0 || std::isinf(__t)) { 75 return std::signbit(__u) <=> std::signbit(__t); 76 } else { 77 int __texp, __uexp; 78 (void)std::frexp(__t, &__texp); 79 (void)std::frexp(__u, &__uexp); 80 return (__t < 0) ? (__texp <=> __uexp) : (__uexp <=> __texp); 81 } 82 } 83 } else { 84 // They're unordered, so one of them must be a NAN. 85 // The order is -QNAN, -SNAN, numbers, +SNAN, +QNAN. 86 bool __t_is_nan = std::isnan(__t); 87 bool __u_is_nan = std::isnan(__u); 88 bool __t_is_negative = std::signbit(__t); 89 bool __u_is_negative = std::signbit(__u); 90 using _IntType = 91 conditional_t< sizeof(__t) == sizeof(int32_t), 92 int32_t, 93 conditional_t< sizeof(__t) == sizeof(int64_t), int64_t, void> >; 94 if constexpr (is_same_v<_IntType, void>) { 95 static_assert(sizeof(_Dp) == 0, "std::strong_order is unimplemented for this floating-point type"); 96 } else if (__t_is_nan && __u_is_nan) { 97 // Order by sign bit, then by "payload bits" (we'll just use bit_cast). 98 if (__t_is_negative != __u_is_negative) { 99 return (__u_is_negative <=> __t_is_negative); 100 } else { 101 return std::bit_cast<_IntType>(__t) <=> std::bit_cast<_IntType>(__u); 102 } 103 } else if (__t_is_nan) { 104 return __t_is_negative ? strong_ordering::less : strong_ordering::greater; 105 } else { 106 return __u_is_negative ? strong_ordering::greater : strong_ordering::less; 107 } 108 } 109 } 110 111 template <class _Tp, class _Up> 112 requires is_same_v<decay_t<_Tp>, decay_t<_Up>> 113 _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<0>) noexcept( 114 noexcept(strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u))))) 115 -> decltype(strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) { 116 return strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u))); 117 } 118 119 template <class _Tp, class _Up> 120 _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t, _Up&& __u) const 121 noexcept(noexcept(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>()))) 122 -> decltype(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>())) { 123 return __go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>()); 124 } 125 }; 126 } // namespace __strong_order 127 128 inline namespace __cpo { 129 inline constexpr auto strong_order = __strong_order::__fn{}; 130 } // namespace __cpo 131 132 #endif // _LIBCPP_STD_VER >= 20 133 134 _LIBCPP_END_NAMESPACE_STD 135 136 _LIBCPP_POP_MACROS 137 138 #endif // _LIBCPP___COMPARE_STRONG_ORDER 139