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 <__utility/forward.h>
17 #include <__utility/priority_tag.h>
18 #include <cmath>
19 #include <type_traits>
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 > 17
28 
29 // [cmp.alg]
30 namespace __weak_order {
31     struct __fn {
32         template<class _Tp, class _Up>
33             requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
34         _LIBCPP_HIDE_FROM_ABI static constexpr auto
35         __go(_Tp&& __t, _Up&& __u, __priority_tag<3>)
36             noexcept(noexcept(weak_ordering(weak_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))))
37             -> decltype(      weak_ordering(weak_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
38             { return          weak_ordering(weak_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))); }
39 
40         template<class _Tp, class _Up, class _Dp = decay_t<_Tp>>
41             requires is_same_v<_Dp, decay_t<_Up>> && is_floating_point_v<_Dp>
42         _LIBCPP_HIDE_FROM_ABI static constexpr weak_ordering
43         __go(_Tp&& __t, _Up&& __u, __priority_tag<2>) noexcept
44         {
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 = _VSTD::isnan(__t);
55                 bool __u_is_nan = _VSTD::isnan(__u);
56                 bool __t_is_negative = _VSTD::signbit(__t);
57                 bool __u_is_negative = _VSTD::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
71         __go(_Tp&& __t, _Up&& __u, __priority_tag<1>)
72             noexcept(noexcept(weak_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))))
73             -> decltype(      weak_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
74             { return          weak_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))); }
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
79         __go(_Tp&& __t, _Up&& __u, __priority_tag<0>)
80             noexcept(noexcept(weak_ordering(_VSTD::strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))))
81             -> decltype(      weak_ordering(_VSTD::strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
82             { return          weak_ordering(_VSTD::strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))); }
83 
84         template<class _Tp, class _Up>
85         _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t, _Up&& __u) const
86             noexcept(noexcept(__go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<3>())))
87             -> decltype(      __go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<3>()))
88             { return          __go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<3>()); }
89     };
90 } // namespace __weak_order
91 
92 inline namespace __cpo {
93     inline constexpr auto weak_order = __weak_order::__fn{};
94 } // namespace __cpo
95 
96 #endif // _LIBCPP_STD_VER > 17
97 
98 _LIBCPP_END_NAMESPACE_STD
99 
100 #endif // _LIBCPP___COMPARE_WEAK_ORDER
101