| 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 <__math/traits.h> |
| 17 |
#include <__type_traits/decay.h> |
| 18 |
#include <__type_traits/is_floating_point.h> |
| 19 |
#include <__type_traits/is_same.h> |
| 20 |
#include <__utility/forward.h> |
| 21 |
#include <__utility/priority_tag.h> |
| 22 |
|
| 23 |
#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER |
| 24 |
# pragma GCC system_header |
| 25 |
#endif |
| 26 |
|
| 27 |
_LIBCPP_BEGIN_NAMESPACE_STD |
| 28 |
|
| 29 |
#if _LIBCPP_STD_VER >= 20 |
| 30 |
|
| 31 |
// [cmp.alg] |
| 32 |
namespace __weak_order { |
| 33 |
void weak_order() = delete; |
| 34 |
|
| 35 |
struct __fn { |
| 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))); |
| 43 |
} |
| 44 |
// NOLINTEND(libcpp-robust-against-adl) |
| 45 |
|
| 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; |
| 56 |
} else { |
| 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; |
| 66 |
} else { |
| 67 |
return __u_is_negative ? weak_ordering::greater : weak_ordering::less; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 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))); |
| 78 |
} |
| 79 |
|
| 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))); |
| 86 |
} |
| 87 |
|
| 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>()); |
| 93 |
} |
| 94 |
}; |
| 95 |
} // namespace __weak_order |
| 96 |
|
| 97 |
inline namespace __cpo { |
| 98 |
inline constexpr auto weak_order = __weak_order::__fn{}; |
| 99 |
} // namespace __cpo |
| 100 |
|
| 101 |
#endif // _LIBCPP_STD_VER >= 20 |
| 102 |
|
| 103 |
_LIBCPP_END_NAMESPACE_STD |
| 104 |
|
| 105 |
#endif // _LIBCPP___COMPARE_WEAK_ORDER |