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 36 __go(_Tp&& __t, _Up&& __u, __priority_tag<3>) 37 noexcept(noexcept(weak_ordering(weak_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))) 38 -> decltype( weak_ordering(weak_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))) 39 { return weak_ordering(weak_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))); } 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 45 __go(_Tp&& __t, _Up&& __u, __priority_tag<2>) noexcept 46 { 47 partial_ordering __po = (__t <=> __u); 48 if (__po == partial_ordering::less) { 49 return weak_ordering::less; 50 } else if (__po == partial_ordering::equivalent) { 51 return weak_ordering::equivalent; 52 } else if (__po == partial_ordering::greater) { 53 return weak_ordering::greater; 54 } else { 55 // Otherwise, at least one of them is a NaN. 56 bool __t_is_nan = _VSTD::isnan(__t); 57 bool __u_is_nan = _VSTD::isnan(__u); 58 bool __t_is_negative = _VSTD::signbit(__t); 59 bool __u_is_negative = _VSTD::signbit(__u); 60 if (__t_is_nan && __u_is_nan) { 61 return (__u_is_negative <=> __t_is_negative); 62 } else if (__t_is_nan) { 63 return __t_is_negative ? weak_ordering::less : weak_ordering::greater; 64 } else { 65 return __u_is_negative ? weak_ordering::greater : weak_ordering::less; 66 } 67 } 68 } 69 70 template<class _Tp, class _Up> 71 requires is_same_v<decay_t<_Tp>, decay_t<_Up>> 72 _LIBCPP_HIDE_FROM_ABI static constexpr auto 73 __go(_Tp&& __t, _Up&& __u, __priority_tag<1>) 74 noexcept(noexcept(weak_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))) 75 -> decltype( weak_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))) 76 { return weak_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))); } 77 78 template<class _Tp, class _Up> 79 requires is_same_v<decay_t<_Tp>, decay_t<_Up>> 80 _LIBCPP_HIDE_FROM_ABI static constexpr auto 81 __go(_Tp&& __t, _Up&& __u, __priority_tag<0>) 82 noexcept(noexcept(weak_ordering(_VSTD::strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))) 83 -> decltype( weak_ordering(_VSTD::strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))) 84 { return weak_ordering(_VSTD::strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))); } 85 86 template<class _Tp, class _Up> 87 _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t, _Up&& __u) const 88 noexcept(noexcept(__go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<3>()))) 89 -> decltype( __go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<3>())) 90 { return __go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<3>()); } 91 }; 92 } // namespace __weak_order 93 94 inline namespace __cpo { 95 inline constexpr auto weak_order = __weak_order::__fn{}; 96 } // namespace __cpo 97 98 #endif // _LIBCPP_STD_VER >= 20 99 100 _LIBCPP_END_NAMESPACE_STD 101 102 #endif // _LIBCPP___COMPARE_WEAK_ORDER 103