xref: /freebsd-src/contrib/llvm-project/libcxx/include/__compare/strong_order.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
14824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
24824e7fdSDimitry Andric //
34824e7fdSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44824e7fdSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
54824e7fdSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64824e7fdSDimitry Andric //
74824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
84824e7fdSDimitry Andric 
94824e7fdSDimitry Andric #ifndef _LIBCPP___COMPARE_STRONG_ORDER
104824e7fdSDimitry Andric #define _LIBCPP___COMPARE_STRONG_ORDER
114824e7fdSDimitry Andric 
124824e7fdSDimitry Andric #include <__bit/bit_cast.h>
134824e7fdSDimitry Andric #include <__compare/compare_three_way.h>
144824e7fdSDimitry Andric #include <__compare/ordering.h>
154824e7fdSDimitry Andric #include <__config>
16*0fca6ea1SDimitry Andric #include <__math/exponential_functions.h>
17*0fca6ea1SDimitry Andric #include <__math/traits.h>
18bdd1243dSDimitry Andric #include <__type_traits/conditional.h>
19bdd1243dSDimitry Andric #include <__type_traits/decay.h>
20*0fca6ea1SDimitry Andric #include <__type_traits/is_floating_point.h>
21*0fca6ea1SDimitry Andric #include <__type_traits/is_same.h>
224824e7fdSDimitry Andric #include <__utility/forward.h>
234824e7fdSDimitry Andric #include <__utility/priority_tag.h>
244824e7fdSDimitry Andric #include <cstdint>
254824e7fdSDimitry Andric #include <limits>
264824e7fdSDimitry Andric 
274824e7fdSDimitry Andric #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
284824e7fdSDimitry Andric #  pragma GCC system_header
294824e7fdSDimitry Andric #endif
304824e7fdSDimitry Andric 
314824e7fdSDimitry Andric _LIBCPP_PUSH_MACROS
324824e7fdSDimitry Andric #include <__undef_macros>
334824e7fdSDimitry Andric 
344824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
354824e7fdSDimitry Andric 
3606c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
374824e7fdSDimitry Andric 
384824e7fdSDimitry Andric // [cmp.alg]
394824e7fdSDimitry Andric namespace __strong_order {
40*0fca6ea1SDimitry Andric void strong_order() = delete;
41*0fca6ea1SDimitry Andric 
424824e7fdSDimitry Andric struct __fn {
43bdd1243dSDimitry Andric   // NOLINTBEGIN(libcpp-robust-against-adl) strong_order should use ADL, but only here
444824e7fdSDimitry Andric   template <class _Tp, class _Up>
454824e7fdSDimitry Andric     requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
46cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<2>) noexcept(
47cb14a3feSDimitry Andric       noexcept(strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
48cb14a3feSDimitry Andric       -> decltype(strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
49cb14a3feSDimitry Andric     return strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
50cb14a3feSDimitry Andric   }
51bdd1243dSDimitry Andric   // NOLINTEND(libcpp-robust-against-adl)
524824e7fdSDimitry Andric 
534824e7fdSDimitry Andric   template <class _Tp, class _Up, class _Dp = decay_t<_Tp>>
544824e7fdSDimitry Andric     requires is_same_v<_Dp, decay_t<_Up>> && is_floating_point_v<_Dp>
55cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr strong_ordering __go(_Tp&& __t, _Up&& __u, __priority_tag<1>) noexcept {
564824e7fdSDimitry Andric     if constexpr (numeric_limits<_Dp>::is_iec559 && sizeof(_Dp) == sizeof(int32_t)) {
575f757f3fSDimitry Andric       int32_t __rx = std::bit_cast<int32_t>(__t);
585f757f3fSDimitry Andric       int32_t __ry = std::bit_cast<int32_t>(__u);
594824e7fdSDimitry Andric       __rx         = (__rx < 0) ? (numeric_limits<int32_t>::min() - __rx - 1) : __rx;
604824e7fdSDimitry Andric       __ry         = (__ry < 0) ? (numeric_limits<int32_t>::min() - __ry - 1) : __ry;
614824e7fdSDimitry Andric       return (__rx <=> __ry);
624824e7fdSDimitry Andric     } else if constexpr (numeric_limits<_Dp>::is_iec559 && sizeof(_Dp) == sizeof(int64_t)) {
635f757f3fSDimitry Andric       int64_t __rx = std::bit_cast<int64_t>(__t);
645f757f3fSDimitry Andric       int64_t __ry = std::bit_cast<int64_t>(__u);
654824e7fdSDimitry Andric       __rx         = (__rx < 0) ? (numeric_limits<int64_t>::min() - __rx - 1) : __rx;
664824e7fdSDimitry Andric       __ry         = (__ry < 0) ? (numeric_limits<int64_t>::min() - __ry - 1) : __ry;
674824e7fdSDimitry Andric       return (__rx <=> __ry);
684824e7fdSDimitry Andric     } else if (__t < __u) {
694824e7fdSDimitry Andric       return strong_ordering::less;
704824e7fdSDimitry Andric     } else if (__t > __u) {
714824e7fdSDimitry Andric       return strong_ordering::greater;
724824e7fdSDimitry Andric     } else if (__t == __u) {
734824e7fdSDimitry Andric       if constexpr (numeric_limits<_Dp>::radix == 2) {
74*0fca6ea1SDimitry Andric         return __math::signbit(__u) <=> __math::signbit(__t);
754824e7fdSDimitry Andric       } else {
764824e7fdSDimitry Andric         // This is bullet 3 of the IEEE754 algorithm, relevant
774824e7fdSDimitry Andric         // only for decimal floating-point;
784824e7fdSDimitry Andric         // see https://stackoverflow.com/questions/69068075/
79*0fca6ea1SDimitry Andric         if (__t == 0 || __math::isinf(__t)) {
80*0fca6ea1SDimitry Andric           return __math::signbit(__u) <=> __math::signbit(__t);
814824e7fdSDimitry Andric         } else {
824824e7fdSDimitry Andric           int __texp, __uexp;
83*0fca6ea1SDimitry Andric           (void)__math::frexp(__t, &__texp);
84*0fca6ea1SDimitry Andric           (void)__math::frexp(__u, &__uexp);
854824e7fdSDimitry Andric           return (__t < 0) ? (__texp <=> __uexp) : (__uexp <=> __texp);
864824e7fdSDimitry Andric         }
874824e7fdSDimitry Andric       }
884824e7fdSDimitry Andric     } else {
894824e7fdSDimitry Andric       // They're unordered, so one of them must be a NAN.
904824e7fdSDimitry Andric       // The order is -QNAN, -SNAN, numbers, +SNAN, +QNAN.
91*0fca6ea1SDimitry Andric       bool __t_is_nan      = __math::isnan(__t);
92*0fca6ea1SDimitry Andric       bool __u_is_nan      = __math::isnan(__u);
93*0fca6ea1SDimitry Andric       bool __t_is_negative = __math::signbit(__t);
94*0fca6ea1SDimitry Andric       bool __u_is_negative = __math::signbit(__u);
95cb14a3feSDimitry Andric       using _IntType =
96cb14a3feSDimitry Andric           conditional_t< sizeof(__t) == sizeof(int32_t),
97cb14a3feSDimitry Andric                          int32_t,
98cb14a3feSDimitry Andric                          conditional_t< sizeof(__t) == sizeof(int64_t), int64_t, void> >;
990eae32dcSDimitry Andric       if constexpr (is_same_v<_IntType, void>) {
1004824e7fdSDimitry Andric         static_assert(sizeof(_Dp) == 0, "std::strong_order is unimplemented for this floating-point type");
1014824e7fdSDimitry Andric       } else if (__t_is_nan && __u_is_nan) {
1024824e7fdSDimitry Andric         // Order by sign bit, then by "payload bits" (we'll just use bit_cast).
1034824e7fdSDimitry Andric         if (__t_is_negative != __u_is_negative) {
1044824e7fdSDimitry Andric           return (__u_is_negative <=> __t_is_negative);
1054824e7fdSDimitry Andric         } else {
1065f757f3fSDimitry Andric           return std::bit_cast<_IntType>(__t) <=> std::bit_cast<_IntType>(__u);
1074824e7fdSDimitry Andric         }
1084824e7fdSDimitry Andric       } else if (__t_is_nan) {
1094824e7fdSDimitry Andric         return __t_is_negative ? strong_ordering::less : strong_ordering::greater;
1104824e7fdSDimitry Andric       } else {
1114824e7fdSDimitry Andric         return __u_is_negative ? strong_ordering::greater : strong_ordering::less;
1124824e7fdSDimitry Andric       }
1134824e7fdSDimitry Andric     }
1144824e7fdSDimitry Andric   }
1154824e7fdSDimitry Andric 
1164824e7fdSDimitry Andric   template <class _Tp, class _Up>
1174824e7fdSDimitry Andric     requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
118cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<0>) noexcept(
119cb14a3feSDimitry Andric       noexcept(strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
120cb14a3feSDimitry Andric       -> decltype(strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
121cb14a3feSDimitry Andric     return strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
122cb14a3feSDimitry Andric   }
1234824e7fdSDimitry Andric 
1244824e7fdSDimitry Andric   template <class _Tp, class _Up>
1254824e7fdSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t, _Up&& __u) const
1265f757f3fSDimitry Andric       noexcept(noexcept(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>())))
127cb14a3feSDimitry Andric           -> decltype(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>())) {
128cb14a3feSDimitry Andric     return __go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>());
129cb14a3feSDimitry Andric   }
1304824e7fdSDimitry Andric };
1314824e7fdSDimitry Andric } // namespace __strong_order
1324824e7fdSDimitry Andric 
1334824e7fdSDimitry Andric inline namespace __cpo {
1344824e7fdSDimitry Andric inline constexpr auto strong_order = __strong_order::__fn{};
1354824e7fdSDimitry Andric } // namespace __cpo
1364824e7fdSDimitry Andric 
13706c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
1384824e7fdSDimitry Andric 
1394824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD
1404824e7fdSDimitry Andric 
1414824e7fdSDimitry Andric _LIBCPP_POP_MACROS
1424824e7fdSDimitry Andric 
1434824e7fdSDimitry Andric #endif // _LIBCPP___COMPARE_STRONG_ORDER
144