1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___CXX03___COMPARE_WEAK_ORDER
10 #define _LIBCPP___CXX03___COMPARE_WEAK_ORDER
12 #include <__cxx03/__compare/compare_three_way.h>
13 #include <__cxx03/__compare/ordering.h>
14 #include <__cxx03/__compare/strong_order.h>
15 #include <__cxx03/__config>
16 #include <__cxx03/__math/traits.h>
17 #include <__cxx03/__type_traits/decay.h>
18 #include <__cxx03/__type_traits/is_floating_point.h>
19 #include <__cxx03/__type_traits/is_same.h>
20 #include <__cxx03/__utility/forward.h>
21 #include <__cxx03/__utility/priority_tag.h>
23 #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
24 # pragma GCC system_header
27 _LIBCPP_BEGIN_NAMESPACE_STD
29 #if _LIBCPP_STD_VER >= 20
32 namespace __weak_order
{
33 void weak_order() = delete;
36 // NOLINTBEGIN(libcpp-robust-against-adl) weak_order should use ADL, but only here
37 template <class _Tp
, class _Up
>
38 requires is_same_v
<decay_t
<_Tp
>, decay_t
<_Up
>>
39 _LIBCPP_HIDE_FROM_ABI
static constexpr auto __go(_Tp
&& __t
, _Up
&& __u
, __priority_tag
<3>) noexcept(
40 noexcept(weak_ordering(weak_order(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
)))))
41 -> decltype(weak_ordering(weak_order(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
)))) {
42 return weak_ordering(weak_order(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
)));
44 // NOLINTEND(libcpp-robust-against-adl)
46 template <class _Tp
, class _Up
, class _Dp
= decay_t
<_Tp
>>
47 requires is_same_v
<_Dp
, decay_t
<_Up
>> && is_floating_point_v
<_Dp
>
48 _LIBCPP_HIDE_FROM_ABI
static constexpr weak_ordering
__go(_Tp
&& __t
, _Up
&& __u
, __priority_tag
<2>) noexcept
{
49 partial_ordering __po
= (__t
<=> __u
);
50 if (__po
== partial_ordering::less
) {
51 return weak_ordering::less
;
52 } else if (__po
== partial_ordering::equivalent
) {
53 return weak_ordering::equivalent
;
54 } else if (__po
== partial_ordering::greater
) {
55 return weak_ordering::greater
;
57 // Otherwise, at least one of them is a NaN.
58 bool __t_is_nan
= __math::isnan(__t
);
59 bool __u_is_nan
= __math::isnan(__u
);
60 bool __t_is_negative
= __math::signbit(__t
);
61 bool __u_is_negative
= __math::signbit(__u
);
62 if (__t_is_nan
&& __u_is_nan
) {
63 return (__u_is_negative
<=> __t_is_negative
);
64 } else if (__t_is_nan
) {
65 return __t_is_negative
? weak_ordering::less
: weak_ordering::greater
;
67 return __u_is_negative
? weak_ordering::greater
: weak_ordering::less
;
72 template <class _Tp
, class _Up
>
73 requires is_same_v
<decay_t
<_Tp
>, decay_t
<_Up
>>
74 _LIBCPP_HIDE_FROM_ABI
static constexpr auto __go(_Tp
&& __t
, _Up
&& __u
, __priority_tag
<1>) noexcept(
75 noexcept(weak_ordering(compare_three_way()(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
)))))
76 -> decltype(weak_ordering(compare_three_way()(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
)))) {
77 return weak_ordering(compare_three_way()(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
)));
80 template <class _Tp
, class _Up
>
81 requires is_same_v
<decay_t
<_Tp
>, decay_t
<_Up
>>
82 _LIBCPP_HIDE_FROM_ABI
static constexpr auto __go(_Tp
&& __t
, _Up
&& __u
, __priority_tag
<0>) noexcept(
83 noexcept(weak_ordering(std::strong_order(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
)))))
84 -> decltype(weak_ordering(std::strong_order(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
)))) {
85 return weak_ordering(std::strong_order(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
)));
88 template <class _Tp
, class _Up
>
89 _LIBCPP_HIDE_FROM_ABI
constexpr auto operator()(_Tp
&& __t
, _Up
&& __u
) const
90 noexcept(noexcept(__go(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
), __priority_tag
<3>())))
91 -> decltype(__go(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
), __priority_tag
<3>())) {
92 return __go(std::forward
<_Tp
>(__t
), std::forward
<_Up
>(__u
), __priority_tag
<3>());
95 } // namespace __weak_order
97 inline namespace __cpo
{
98 inline constexpr auto weak_order
= __weak_order::__fn
{};
101 #endif // _LIBCPP_STD_VER >= 20
103 _LIBCPP_END_NAMESPACE_STD
105 #endif // _LIBCPP___CXX03___COMPARE_WEAK_ORDER