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>
16bdd1243dSDimitry Andric #include <__type_traits/conditional.h>
17bdd1243dSDimitry Andric #include <__type_traits/decay.h>
184824e7fdSDimitry Andric #include <__utility/forward.h>
194824e7fdSDimitry Andric #include <__utility/priority_tag.h>
204824e7fdSDimitry Andric #include <cmath>
214824e7fdSDimitry Andric #include <cstdint>
224824e7fdSDimitry Andric #include <limits>
234824e7fdSDimitry Andric 
244824e7fdSDimitry Andric #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
254824e7fdSDimitry Andric #  pragma GCC system_header
264824e7fdSDimitry Andric #endif
274824e7fdSDimitry Andric 
284824e7fdSDimitry Andric _LIBCPP_PUSH_MACROS
294824e7fdSDimitry Andric #include <__undef_macros>
304824e7fdSDimitry Andric 
314824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
324824e7fdSDimitry Andric 
3306c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
344824e7fdSDimitry Andric 
354824e7fdSDimitry Andric // [cmp.alg]
364824e7fdSDimitry Andric namespace __strong_order {
374824e7fdSDimitry Andric struct __fn {
38bdd1243dSDimitry Andric   // NOLINTBEGIN(libcpp-robust-against-adl) strong_order should use ADL, but only here
394824e7fdSDimitry Andric   template <class _Tp, class _Up>
404824e7fdSDimitry Andric     requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
41*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<2>) noexcept(
42*cb14a3feSDimitry Andric       noexcept(strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
43*cb14a3feSDimitry Andric       -> decltype(strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
44*cb14a3feSDimitry Andric     return strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
45*cb14a3feSDimitry Andric   }
46bdd1243dSDimitry Andric   // NOLINTEND(libcpp-robust-against-adl)
474824e7fdSDimitry Andric 
484824e7fdSDimitry Andric   template <class _Tp, class _Up, class _Dp = decay_t<_Tp>>
494824e7fdSDimitry Andric     requires is_same_v<_Dp, decay_t<_Up>> && is_floating_point_v<_Dp>
__go__fn50*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr strong_ordering __go(_Tp&& __t, _Up&& __u, __priority_tag<1>) noexcept {
514824e7fdSDimitry Andric     if constexpr (numeric_limits<_Dp>::is_iec559 && sizeof(_Dp) == sizeof(int32_t)) {
525f757f3fSDimitry Andric       int32_t __rx = std::bit_cast<int32_t>(__t);
535f757f3fSDimitry Andric       int32_t __ry = std::bit_cast<int32_t>(__u);
544824e7fdSDimitry Andric       __rx         = (__rx < 0) ? (numeric_limits<int32_t>::min() - __rx - 1) : __rx;
554824e7fdSDimitry Andric       __ry         = (__ry < 0) ? (numeric_limits<int32_t>::min() - __ry - 1) : __ry;
564824e7fdSDimitry Andric       return (__rx <=> __ry);
574824e7fdSDimitry Andric     } else if constexpr (numeric_limits<_Dp>::is_iec559 && sizeof(_Dp) == sizeof(int64_t)) {
585f757f3fSDimitry Andric       int64_t __rx = std::bit_cast<int64_t>(__t);
595f757f3fSDimitry Andric       int64_t __ry = std::bit_cast<int64_t>(__u);
604824e7fdSDimitry Andric       __rx         = (__rx < 0) ? (numeric_limits<int64_t>::min() - __rx - 1) : __rx;
614824e7fdSDimitry Andric       __ry         = (__ry < 0) ? (numeric_limits<int64_t>::min() - __ry - 1) : __ry;
624824e7fdSDimitry Andric       return (__rx <=> __ry);
634824e7fdSDimitry Andric     } else if (__t < __u) {
644824e7fdSDimitry Andric       return strong_ordering::less;
654824e7fdSDimitry Andric     } else if (__t > __u) {
664824e7fdSDimitry Andric       return strong_ordering::greater;
674824e7fdSDimitry Andric     } else if (__t == __u) {
684824e7fdSDimitry Andric       if constexpr (numeric_limits<_Dp>::radix == 2) {
695f757f3fSDimitry Andric         return std::signbit(__u) <=> std::signbit(__t);
704824e7fdSDimitry Andric       } else {
714824e7fdSDimitry Andric         // This is bullet 3 of the IEEE754 algorithm, relevant
724824e7fdSDimitry Andric         // only for decimal floating-point;
734824e7fdSDimitry Andric         // see https://stackoverflow.com/questions/69068075/
745f757f3fSDimitry Andric         if (__t == 0 || std::isinf(__t)) {
755f757f3fSDimitry Andric           return std::signbit(__u) <=> std::signbit(__t);
764824e7fdSDimitry Andric         } else {
774824e7fdSDimitry Andric           int __texp, __uexp;
785f757f3fSDimitry Andric           (void)std::frexp(__t, &__texp);
795f757f3fSDimitry Andric           (void)std::frexp(__u, &__uexp);
804824e7fdSDimitry Andric           return (__t < 0) ? (__texp <=> __uexp) : (__uexp <=> __texp);
814824e7fdSDimitry Andric         }
824824e7fdSDimitry Andric       }
834824e7fdSDimitry Andric     } else {
844824e7fdSDimitry Andric       // They're unordered, so one of them must be a NAN.
854824e7fdSDimitry Andric       // The order is -QNAN, -SNAN, numbers, +SNAN, +QNAN.
865f757f3fSDimitry Andric       bool __t_is_nan      = std::isnan(__t);
875f757f3fSDimitry Andric       bool __u_is_nan      = std::isnan(__u);
885f757f3fSDimitry Andric       bool __t_is_negative = std::signbit(__t);
895f757f3fSDimitry Andric       bool __u_is_negative = std::signbit(__u);
90*cb14a3feSDimitry Andric       using _IntType =
91*cb14a3feSDimitry Andric           conditional_t< sizeof(__t) == sizeof(int32_t),
92*cb14a3feSDimitry Andric                          int32_t,
93*cb14a3feSDimitry Andric                          conditional_t< sizeof(__t) == sizeof(int64_t), int64_t, void> >;
940eae32dcSDimitry Andric       if constexpr (is_same_v<_IntType, void>) {
954824e7fdSDimitry Andric         static_assert(sizeof(_Dp) == 0, "std::strong_order is unimplemented for this floating-point type");
964824e7fdSDimitry Andric       } else if (__t_is_nan && __u_is_nan) {
974824e7fdSDimitry Andric         // Order by sign bit, then by "payload bits" (we'll just use bit_cast).
984824e7fdSDimitry Andric         if (__t_is_negative != __u_is_negative) {
994824e7fdSDimitry Andric           return (__u_is_negative <=> __t_is_negative);
1004824e7fdSDimitry Andric         } else {
1015f757f3fSDimitry Andric           return std::bit_cast<_IntType>(__t) <=> std::bit_cast<_IntType>(__u);
1024824e7fdSDimitry Andric         }
1034824e7fdSDimitry Andric       } else if (__t_is_nan) {
1044824e7fdSDimitry Andric         return __t_is_negative ? strong_ordering::less : strong_ordering::greater;
1054824e7fdSDimitry Andric       } else {
1064824e7fdSDimitry Andric         return __u_is_negative ? strong_ordering::greater : strong_ordering::less;
1074824e7fdSDimitry Andric       }
1084824e7fdSDimitry Andric     }
1094824e7fdSDimitry Andric   }
1104824e7fdSDimitry Andric 
1114824e7fdSDimitry Andric   template <class _Tp, class _Up>
1124824e7fdSDimitry Andric     requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
113*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<0>) noexcept(
114*cb14a3feSDimitry Andric       noexcept(strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
115*cb14a3feSDimitry Andric       -> decltype(strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
116*cb14a3feSDimitry Andric     return strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
117*cb14a3feSDimitry Andric   }
1184824e7fdSDimitry Andric 
1194824e7fdSDimitry Andric   template <class _Tp, class _Up>
1204824e7fdSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t, _Up&& __u) const
1215f757f3fSDimitry Andric       noexcept(noexcept(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>())))
122*cb14a3feSDimitry Andric           -> decltype(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>())) {
123*cb14a3feSDimitry Andric     return __go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>());
124*cb14a3feSDimitry Andric   }
1254824e7fdSDimitry Andric };
1264824e7fdSDimitry Andric } // namespace __strong_order
1274824e7fdSDimitry Andric 
1284824e7fdSDimitry Andric inline namespace __cpo {
1294824e7fdSDimitry Andric inline constexpr auto strong_order = __strong_order::__fn{};
1304824e7fdSDimitry Andric } // namespace __cpo
1314824e7fdSDimitry Andric 
13206c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
1334824e7fdSDimitry Andric 
1344824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD
1354824e7fdSDimitry Andric 
1364824e7fdSDimitry Andric _LIBCPP_POP_MACROS
1374824e7fdSDimitry Andric 
1384824e7fdSDimitry Andric #endif // _LIBCPP___COMPARE_STRONG_ORDER
139