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 <__config>
13 #include <__utility/forward.h>
14 #include <__utility/move.h>
15 #include <limits>
16 #include <type_traits>
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 > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
28 template<class _Tp, class... _Up>
29 struct _IsSameAsAny : _Or<_IsSame<_Tp, _Up>...> {};
30 
31 template<class _Tp>
32 concept __is_safe_integral_cmp = is_integral_v<_Tp> &&
33                       !_IsSameAsAny<_Tp, bool, char,
34 #ifndef _LIBCPP_HAS_NO_CHAR8_T
35                                     char8_t,
36 #endif
37 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
38                                     char16_t, char32_t,
39 #endif
40                                     wchar_t>::value;
41 
42 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
43 _LIBCPP_INLINE_VISIBILITY constexpr
44 bool cmp_equal(_Tp __t, _Up __u) noexcept
45 {
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 ? false : make_unsigned_t<_Tp>(__t) == __u;
50   else
51     return __u < 0 ? false : __t == make_unsigned_t<_Up>(__u);
52 }
53 
54 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
55 _LIBCPP_INLINE_VISIBILITY constexpr
56 bool cmp_not_equal(_Tp __t, _Up __u) noexcept
57 {
58   return !_VSTD::cmp_equal(__t, __u);
59 }
60 
61 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
62 _LIBCPP_INLINE_VISIBILITY constexpr
63 bool cmp_less(_Tp __t, _Up __u) noexcept
64 {
65   if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
66     return __t < __u;
67   else if constexpr (is_signed_v<_Tp>)
68     return __t < 0 ? true : make_unsigned_t<_Tp>(__t) < __u;
69   else
70     return __u < 0 ? false : __t < make_unsigned_t<_Up>(__u);
71 }
72 
73 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
74 _LIBCPP_INLINE_VISIBILITY constexpr
75 bool cmp_greater(_Tp __t, _Up __u) noexcept
76 {
77   return _VSTD::cmp_less(__u, __t);
78 }
79 
80 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
81 _LIBCPP_INLINE_VISIBILITY constexpr
82 bool cmp_less_equal(_Tp __t, _Up __u) noexcept
83 {
84   return !_VSTD::cmp_greater(__t, __u);
85 }
86 
87 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
88 _LIBCPP_INLINE_VISIBILITY constexpr
89 bool cmp_greater_equal(_Tp __t, _Up __u) noexcept
90 {
91   return !_VSTD::cmp_less(__t, __u);
92 }
93 
94 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
95 _LIBCPP_INLINE_VISIBILITY constexpr
96 bool in_range(_Up __u) noexcept
97 {
98   return _VSTD::cmp_less_equal(__u, numeric_limits<_Tp>::max()) &&
99          _VSTD::cmp_greater_equal(__u, numeric_limits<_Tp>::min());
100 }
101 #endif
102 
103 _LIBCPP_END_NAMESPACE_STD
104 
105 _LIBCPP_POP_MACROS
106 
107 #endif // _LIBCPP___UTILITY_CMP_H
108