xref: /freebsd/contrib/llvm-project/libcxx/include/__compare/weak_order.h (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
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_WEAK_ORDER
10 #define _LIBCPP___COMPARE_WEAK_ORDER
11 
12 #include <__compare/compare_three_way.h>
13 #include <__compare/ordering.h>
14 #include <__compare/strong_order.h>
15 #include <__config>
16 #include <__type_traits/decay.h>
17 #include <__utility/forward.h>
18 #include <__utility/priority_tag.h>
19 #include <cmath>
20 
21 #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
22 #  pragma GCC system_header
23 #endif
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 #if _LIBCPP_STD_VER >= 20
28 
29 // [cmp.alg]
30 namespace __weak_order {
31 struct __fn {
32   // NOLINTBEGIN(libcpp-robust-against-adl) weak_order should use ADL, but only here
33   template <class _Tp, class _Up>
34     requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
35   _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<3>) noexcept(
36       noexcept(weak_ordering(weak_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
37       -> decltype(weak_ordering(weak_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
38     return weak_ordering(weak_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
39   }
40   // NOLINTEND(libcpp-robust-against-adl)
41 
42   template <class _Tp, class _Up, class _Dp = decay_t<_Tp>>
43     requires is_same_v<_Dp, decay_t<_Up>> && is_floating_point_v<_Dp>
44   _LIBCPP_HIDE_FROM_ABI static constexpr weak_ordering __go(_Tp&& __t, _Up&& __u, __priority_tag<2>) noexcept {
45     partial_ordering __po = (__t <=> __u);
46     if (__po == partial_ordering::less) {
47       return weak_ordering::less;
48     } else if (__po == partial_ordering::equivalent) {
49       return weak_ordering::equivalent;
50     } else if (__po == partial_ordering::greater) {
51       return weak_ordering::greater;
52     } else {
53       // Otherwise, at least one of them is a NaN.
54       bool __t_is_nan      = std::isnan(__t);
55       bool __u_is_nan      = std::isnan(__u);
56       bool __t_is_negative = std::signbit(__t);
57       bool __u_is_negative = std::signbit(__u);
58       if (__t_is_nan && __u_is_nan) {
59         return (__u_is_negative <=> __t_is_negative);
60       } else if (__t_is_nan) {
61         return __t_is_negative ? weak_ordering::less : weak_ordering::greater;
62       } else {
63         return __u_is_negative ? weak_ordering::greater : weak_ordering::less;
64       }
65     }
66   }
67 
68   template <class _Tp, class _Up>
69     requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
70   _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<1>) noexcept(
71       noexcept(weak_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
72       -> decltype(weak_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
73     return weak_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
74   }
75 
76   template <class _Tp, class _Up>
77     requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
78   _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<0>) noexcept(
79       noexcept(weak_ordering(std::strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
80       -> decltype(weak_ordering(std::strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
81     return weak_ordering(std::strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
82   }
83 
84   template <class _Tp, class _Up>
85   _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t, _Up&& __u) const
86       noexcept(noexcept(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<3>())))
87           -> decltype(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<3>())) {
88     return __go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<3>());
89   }
90 };
91 } // namespace __weak_order
92 
93 inline namespace __cpo {
94 inline constexpr auto weak_order = __weak_order::__fn{};
95 } // namespace __cpo
96 
97 #endif // _LIBCPP_STD_VER >= 20
98 
99 _LIBCPP_END_NAMESPACE_STD
100 
101 #endif // _LIBCPP___COMPARE_WEAK_ORDER
102