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___UTILITY_CMP_H 10 #define _LIBCPP___UTILITY_CMP_H 11 12 #include <__concepts/arithmetic.h> 13 #include <__config> 14 #include <__type_traits/is_signed.h> 15 #include <__type_traits/make_unsigned.h> 16 #include <limits> 17 18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 19 # pragma GCC system_header 20 #endif 21 22 _LIBCPP_PUSH_MACROS 23 #include <__undef_macros> 24 25 _LIBCPP_BEGIN_NAMESPACE_STD 26 27 #if _LIBCPP_STD_VER >= 20 28 29 template <__libcpp_integer _Tp, __libcpp_integer _Up> 30 _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_equal(_Tp __t, _Up __u) noexcept { 31 if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>) 32 return __t == __u; 33 else if constexpr (is_signed_v<_Tp>) 34 return __t < 0 ? false : make_unsigned_t<_Tp>(__t) == __u; 35 else 36 return __u < 0 ? false : __t == make_unsigned_t<_Up>(__u); 37 } 38 39 template <__libcpp_integer _Tp, __libcpp_integer _Up> 40 _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_not_equal(_Tp __t, _Up __u) noexcept { 41 return !std::cmp_equal(__t, __u); 42 } 43 44 template <__libcpp_integer _Tp, __libcpp_integer _Up> 45 _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_less(_Tp __t, _Up __u) noexcept { 46 if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>) 47 return __t < __u; 48 else if constexpr (is_signed_v<_Tp>) 49 return __t < 0 ? true : make_unsigned_t<_Tp>(__t) < __u; 50 else 51 return __u < 0 ? false : __t < make_unsigned_t<_Up>(__u); 52 } 53 54 template <__libcpp_integer _Tp, __libcpp_integer _Up> 55 _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_greater(_Tp __t, _Up __u) noexcept { 56 return std::cmp_less(__u, __t); 57 } 58 59 template <__libcpp_integer _Tp, __libcpp_integer _Up> 60 _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_less_equal(_Tp __t, _Up __u) noexcept { 61 return !std::cmp_greater(__t, __u); 62 } 63 64 template <__libcpp_integer _Tp, __libcpp_integer _Up> 65 _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_greater_equal(_Tp __t, _Up __u) noexcept { 66 return !std::cmp_less(__t, __u); 67 } 68 69 template <__libcpp_integer _Tp, __libcpp_integer _Up> 70 _LIBCPP_HIDE_FROM_ABI constexpr bool in_range(_Up __u) noexcept { 71 return std::cmp_less_equal(__u, numeric_limits<_Tp>::max()) && 72 std::cmp_greater_equal(__u, numeric_limits<_Tp>::min()); 73 } 74 75 #endif // _LIBCPP_STD_VER >= 20 76 77 _LIBCPP_END_NAMESPACE_STD 78 79 _LIBCPP_POP_MACROS 80 81 #endif // _LIBCPP___UTILITY_CMP_H 82