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 <__math/traits.h>
17#include <__type_traits/conditional.h>
18#include <__type_traits/decay.h>
19#include <__type_traits/is_floating_point.h>
20#include <__type_traits/is_same.h>
21#include <__utility/forward.h>
22#include <__utility/priority_tag.h>
23#include <cstdint>
24#include <limits>
25
26#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
27# pragma GCC system_header
28#endif
29
30_LIBCPP_PUSH_MACROS
31#include <__undef_macros>
32
33_LIBCPP_BEGIN_NAMESPACE_STD
34
35#if _LIBCPP_STD_VER >= 20
36
37// [cmp.alg]
38namespace __strong_order {
39void strong_order() = delete;
40
41struct __fn {
42 // NOLINTBEGIN(libcpp-robust-against-adl) strong_order should use ADL, but only here
43 template <class _Tp, class _Up>
44 requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
45 _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<2>) noexcept(
46 noexcept(strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
47 -> decltype(strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
48 return strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
49 }
50 // NOLINTEND(libcpp-robust-against-adl)
51
52 template <class _Tp, class _Up, class _Dp = decay_t<_Tp>>
53 requires is_same_v<_Dp, decay_t<_Up>> && is_floating_point_v<_Dp>
54 _LIBCPP_HIDE_FROM_ABI static constexpr strong_ordering __go(_Tp&& __t, _Up&& __u, __priority_tag<1>) noexcept {
55 if constexpr (numeric_limits<_Dp>::is_iec559 &&
56 (sizeof(_Dp) == sizeof(int32_t) || sizeof(_Dp) == sizeof(int64_t))) {
57 using _IntT = conditional_t<sizeof(_Dp) == sizeof(int32_t), int32_t, int64_t>;
58 _IntT __rx = std::bit_cast<_IntT>(__t);
59 _IntT __ry = std::bit_cast<_IntT>(__u);
60 __rx = (__rx < 0) ? (numeric_limits<_IntT>::min() - __rx - 1) : __rx;
61 __ry = (__ry < 0) ? (numeric_limits<_IntT>::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 static_assert(numeric_limits<_Dp>::radix == 2, "floating point type with a radix other than 2?");
69 return __math::signbit(__u) <=> __math::signbit(__t);
70 } else {
71 // They're unordered, so one of them must be a NAN.
72 // The order is -QNAN, -SNAN, numbers, +SNAN, +QNAN.
73 bool __t_is_nan = __math::isnan(__t);
74 bool __u_is_nan = __math::isnan(__u);
75 bool __t_is_negative = __math::signbit(__t);
76 bool __u_is_negative = __math::signbit(__u);
77 using _IntType =
78 conditional_t<sizeof(__t) == sizeof(int32_t),
79 int32_t,
80 conditional_t<sizeof(__t) == sizeof(int64_t), int64_t, void>>;
81 if constexpr (is_same_v<_IntType, void>) {
82 static_assert(sizeof(_Dp) == 0, "std::strong_order is unimplemented for this floating-point type");
83 } else if (__t_is_nan && __u_is_nan) {
84 // Order by sign bit, then by "payload bits" (we'll just use bit_cast).
85 if (__t_is_negative != __u_is_negative) {
86 return (__u_is_negative <=> __t_is_negative);
87 } else {
88 return std::bit_cast<_IntType>(__t) <=> std::bit_cast<_IntType>(__u);
89 }
90 } else if (__t_is_nan) {
91 return __t_is_negative ? strong_ordering::less : strong_ordering::greater;
92 } else {
93 return __u_is_negative ? strong_ordering::greater : strong_ordering::less;
94 }
95 }
96 }
97
98 template <class _Tp, class _Up>
99 requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
100 _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<0>) noexcept(
101 noexcept(strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
102 -> decltype(strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
103 return strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
104 }
105
106 template <class _Tp, class _Up>
107 _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t, _Up&& __u) const
108 noexcept(noexcept(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>())))
109 -> decltype(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>())) {
110 return __go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>());
111 }
112};
113} // namespace __strong_order
114
115inline namespace __cpo {
116inline constexpr auto strong_order = __strong_order::__fn{};
117} // namespace __cpo
118
119#endif // _LIBCPP_STD_VER >= 20
120
121_LIBCPP_END_NAMESPACE_STD
122
123_LIBCPP_POP_MACROS
124
125#endif // _LIBCPP___COMPARE_STRONG_ORDER
126