xref: /freebsd/contrib/llvm-project/libcxx/include/__compare/strong_order.h (revision 734e82fe33aa764367791a7d603b383996c6b40b)
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 > 17
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
42         __go(_Tp&& __t, _Up&& __u, __priority_tag<2>)
43             noexcept(noexcept(strong_ordering(strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))))
44             -> decltype(      strong_ordering(strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
45             { return          strong_ordering(strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))); }
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
51         __go(_Tp&& __t, _Up&& __u, __priority_tag<1>) noexcept
52         {
53             if constexpr (numeric_limits<_Dp>::is_iec559 && sizeof(_Dp) == sizeof(int32_t)) {
54                 int32_t __rx = _VSTD::bit_cast<int32_t>(__t);
55                 int32_t __ry = _VSTD::bit_cast<int32_t>(__u);
56                 __rx = (__rx < 0) ? (numeric_limits<int32_t>::min() - __rx - 1) : __rx;
57                 __ry = (__ry < 0) ? (numeric_limits<int32_t>::min() - __ry - 1) : __ry;
58                 return (__rx <=> __ry);
59             } else if constexpr (numeric_limits<_Dp>::is_iec559 && sizeof(_Dp) == sizeof(int64_t)) {
60                 int64_t __rx = _VSTD::bit_cast<int64_t>(__t);
61                 int64_t __ry = _VSTD::bit_cast<int64_t>(__u);
62                 __rx = (__rx < 0) ? (numeric_limits<int64_t>::min() - __rx - 1) : __rx;
63                 __ry = (__ry < 0) ? (numeric_limits<int64_t>::min() - __ry - 1) : __ry;
64                 return (__rx <=> __ry);
65             } else if (__t < __u) {
66                 return strong_ordering::less;
67             } else if (__t > __u) {
68                 return strong_ordering::greater;
69             } else if (__t == __u) {
70                 if constexpr (numeric_limits<_Dp>::radix == 2) {
71                     return _VSTD::signbit(__u) <=> _VSTD::signbit(__t);
72                 } else {
73                     // This is bullet 3 of the IEEE754 algorithm, relevant
74                     // only for decimal floating-point;
75                     // see https://stackoverflow.com/questions/69068075/
76                     if (__t == 0 || _VSTD::isinf(__t)) {
77                         return _VSTD::signbit(__u) <=> _VSTD::signbit(__t);
78                     } else {
79                         int __texp, __uexp;
80                         (void)_VSTD::frexp(__t, &__texp);
81                         (void)_VSTD::frexp(__u, &__uexp);
82                         return (__t < 0) ? (__texp <=> __uexp) : (__uexp <=> __texp);
83                     }
84                 }
85             } else {
86                 // They're unordered, so one of them must be a NAN.
87                 // The order is -QNAN, -SNAN, numbers, +SNAN, +QNAN.
88                 bool __t_is_nan = _VSTD::isnan(__t);
89                 bool __u_is_nan = _VSTD::isnan(__u);
90                 bool __t_is_negative = _VSTD::signbit(__t);
91                 bool __u_is_negative = _VSTD::signbit(__u);
92                 using _IntType = conditional_t<
93                     sizeof(__t) == sizeof(int32_t), int32_t, conditional_t<
94                     sizeof(__t) == sizeof(int64_t), int64_t, void>
95                 >;
96                 if constexpr (is_same_v<_IntType, void>) {
97                     static_assert(sizeof(_Dp) == 0, "std::strong_order is unimplemented for this floating-point type");
98                 } else if (__t_is_nan && __u_is_nan) {
99                     // Order by sign bit, then by "payload bits" (we'll just use bit_cast).
100                     if (__t_is_negative != __u_is_negative) {
101                         return (__u_is_negative <=> __t_is_negative);
102                     } else {
103                         return _VSTD::bit_cast<_IntType>(__t) <=> _VSTD::bit_cast<_IntType>(__u);
104                     }
105                 } else if (__t_is_nan) {
106                     return __t_is_negative ? strong_ordering::less : strong_ordering::greater;
107                 } else {
108                     return __u_is_negative ? strong_ordering::greater : strong_ordering::less;
109                 }
110             }
111         }
112 
113         template<class _Tp, class _Up>
114             requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
115         _LIBCPP_HIDE_FROM_ABI static constexpr auto
116         __go(_Tp&& __t, _Up&& __u, __priority_tag<0>)
117             noexcept(noexcept(strong_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))))
118             -> decltype(      strong_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
119             { return          strong_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))); }
120 
121         template<class _Tp, class _Up>
122         _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t, _Up&& __u) const
123             noexcept(noexcept(__go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<2>())))
124             -> decltype(      __go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<2>()))
125             { return          __go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<2>()); }
126     };
127 } // namespace __strong_order
128 
129 inline namespace __cpo {
130     inline constexpr auto strong_order = __strong_order::__fn{};
131 } // namespace __cpo
132 
133 #endif // _LIBCPP_STD_VER > 17
134 
135 _LIBCPP_END_NAMESPACE_STD
136 
137 _LIBCPP_POP_MACROS
138 
139 #endif // _LIBCPP___COMPARE_STRONG_ORDER
140