Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:25

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___CXX03___COMPARE_STRONG_ORDER
0010 #define _LIBCPP___CXX03___COMPARE_STRONG_ORDER
0011 
0012 #include <__cxx03/__bit/bit_cast.h>
0013 #include <__cxx03/__compare/compare_three_way.h>
0014 #include <__cxx03/__compare/ordering.h>
0015 #include <__cxx03/__config>
0016 #include <__cxx03/__math/exponential_functions.h>
0017 #include <__cxx03/__math/traits.h>
0018 #include <__cxx03/__type_traits/conditional.h>
0019 #include <__cxx03/__type_traits/decay.h>
0020 #include <__cxx03/__type_traits/is_floating_point.h>
0021 #include <__cxx03/__type_traits/is_same.h>
0022 #include <__cxx03/__utility/forward.h>
0023 #include <__cxx03/__utility/priority_tag.h>
0024 #include <__cxx03/cstdint>
0025 #include <__cxx03/limits>
0026 
0027 #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
0028 #  pragma GCC system_header
0029 #endif
0030 
0031 _LIBCPP_PUSH_MACROS
0032 #include <__cxx03/__undef_macros>
0033 
0034 _LIBCPP_BEGIN_NAMESPACE_STD
0035 
0036 #if _LIBCPP_STD_VER >= 20
0037 
0038 // [cmp.alg]
0039 namespace __strong_order {
0040 void strong_order() = delete;
0041 
0042 struct __fn {
0043   // NOLINTBEGIN(libcpp-robust-against-adl) strong_order should use ADL, but only here
0044   template <class _Tp, class _Up>
0045     requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
0046   _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<2>) noexcept(
0047       noexcept(strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
0048       -> decltype(strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
0049     return strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
0050   }
0051   // NOLINTEND(libcpp-robust-against-adl)
0052 
0053   template <class _Tp, class _Up, class _Dp = decay_t<_Tp>>
0054     requires is_same_v<_Dp, decay_t<_Up>> && is_floating_point_v<_Dp>
0055   _LIBCPP_HIDE_FROM_ABI static constexpr strong_ordering __go(_Tp&& __t, _Up&& __u, __priority_tag<1>) noexcept {
0056     if constexpr (numeric_limits<_Dp>::is_iec559 && sizeof(_Dp) == sizeof(int32_t)) {
0057       int32_t __rx = std::bit_cast<int32_t>(__t);
0058       int32_t __ry = std::bit_cast<int32_t>(__u);
0059       __rx         = (__rx < 0) ? (numeric_limits<int32_t>::min() - __rx - 1) : __rx;
0060       __ry         = (__ry < 0) ? (numeric_limits<int32_t>::min() - __ry - 1) : __ry;
0061       return (__rx <=> __ry);
0062     } else if constexpr (numeric_limits<_Dp>::is_iec559 && sizeof(_Dp) == sizeof(int64_t)) {
0063       int64_t __rx = std::bit_cast<int64_t>(__t);
0064       int64_t __ry = std::bit_cast<int64_t>(__u);
0065       __rx         = (__rx < 0) ? (numeric_limits<int64_t>::min() - __rx - 1) : __rx;
0066       __ry         = (__ry < 0) ? (numeric_limits<int64_t>::min() - __ry - 1) : __ry;
0067       return (__rx <=> __ry);
0068     } else if (__t < __u) {
0069       return strong_ordering::less;
0070     } else if (__t > __u) {
0071       return strong_ordering::greater;
0072     } else if (__t == __u) {
0073       if constexpr (numeric_limits<_Dp>::radix == 2) {
0074         return __math::signbit(__u) <=> __math::signbit(__t);
0075       } else {
0076         // This is bullet 3 of the IEEE754 algorithm, relevant
0077         // only for decimal floating-point;
0078         // see https://stackoverflow.com/questions/69068075/
0079         if (__t == 0 || __math::isinf(__t)) {
0080           return __math::signbit(__u) <=> __math::signbit(__t);
0081         } else {
0082           int __texp, __uexp;
0083           (void)__math::frexp(__t, &__texp);
0084           (void)__math::frexp(__u, &__uexp);
0085           return (__t < 0) ? (__texp <=> __uexp) : (__uexp <=> __texp);
0086         }
0087       }
0088     } else {
0089       // They're unordered, so one of them must be a NAN.
0090       // The order is -QNAN, -SNAN, numbers, +SNAN, +QNAN.
0091       bool __t_is_nan      = __math::isnan(__t);
0092       bool __u_is_nan      = __math::isnan(__u);
0093       bool __t_is_negative = __math::signbit(__t);
0094       bool __u_is_negative = __math::signbit(__u);
0095       using _IntType =
0096           conditional_t< sizeof(__t) == sizeof(int32_t),
0097                          int32_t,
0098                          conditional_t< sizeof(__t) == sizeof(int64_t), int64_t, void> >;
0099       if constexpr (is_same_v<_IntType, void>) {
0100         static_assert(sizeof(_Dp) == 0, "std::strong_order is unimplemented for this floating-point type");
0101       } else if (__t_is_nan && __u_is_nan) {
0102         // Order by sign bit, then by "payload bits" (we'll just use bit_cast).
0103         if (__t_is_negative != __u_is_negative) {
0104           return (__u_is_negative <=> __t_is_negative);
0105         } else {
0106           return std::bit_cast<_IntType>(__t) <=> std::bit_cast<_IntType>(__u);
0107         }
0108       } else if (__t_is_nan) {
0109         return __t_is_negative ? strong_ordering::less : strong_ordering::greater;
0110       } else {
0111         return __u_is_negative ? strong_ordering::greater : strong_ordering::less;
0112       }
0113     }
0114   }
0115 
0116   template <class _Tp, class _Up>
0117     requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
0118   _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<0>) noexcept(
0119       noexcept(strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
0120       -> decltype(strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
0121     return strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
0122   }
0123 
0124   template <class _Tp, class _Up>
0125   _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t, _Up&& __u) const
0126       noexcept(noexcept(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>())))
0127           -> decltype(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>())) {
0128     return __go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>());
0129   }
0130 };
0131 } // namespace __strong_order
0132 
0133 inline namespace __cpo {
0134 inline constexpr auto strong_order = __strong_order::__fn{};
0135 } // namespace __cpo
0136 
0137 #endif // _LIBCPP_STD_VER >= 20
0138 
0139 _LIBCPP_END_NAMESPACE_STD
0140 
0141 _LIBCPP_POP_MACROS
0142 
0143 #endif // _LIBCPP___CXX03___COMPARE_STRONG_ORDER