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