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 !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 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
41                                     , wchar_t
42 #endif
43                                     >::value;
44 
45 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
46 _LIBCPP_INLINE_VISIBILITY constexpr
47 bool cmp_equal(_Tp __t, _Up __u) noexcept
48 {
49   if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
50     return __t == __u;
51   else if constexpr (is_signed_v<_Tp>)
52     return __t < 0 ? false : make_unsigned_t<_Tp>(__t) == __u;
53   else
54     return __u < 0 ? false : __t == make_unsigned_t<_Up>(__u);
55 }
56 
57 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
58 _LIBCPP_INLINE_VISIBILITY constexpr
59 bool cmp_not_equal(_Tp __t, _Up __u) noexcept
60 {
61   return !_VSTD::cmp_equal(__t, __u);
62 }
63 
64 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
65 _LIBCPP_INLINE_VISIBILITY constexpr
66 bool cmp_less(_Tp __t, _Up __u) noexcept
67 {
68   if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
69     return __t < __u;
70   else if constexpr (is_signed_v<_Tp>)
71     return __t < 0 ? true : make_unsigned_t<_Tp>(__t) < __u;
72   else
73     return __u < 0 ? false : __t < make_unsigned_t<_Up>(__u);
74 }
75 
76 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
77 _LIBCPP_INLINE_VISIBILITY constexpr
78 bool cmp_greater(_Tp __t, _Up __u) noexcept
79 {
80   return _VSTD::cmp_less(__u, __t);
81 }
82 
83 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
84 _LIBCPP_INLINE_VISIBILITY constexpr
85 bool cmp_less_equal(_Tp __t, _Up __u) noexcept
86 {
87   return !_VSTD::cmp_greater(__t, __u);
88 }
89 
90 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
91 _LIBCPP_INLINE_VISIBILITY constexpr
92 bool cmp_greater_equal(_Tp __t, _Up __u) noexcept
93 {
94   return !_VSTD::cmp_less(__t, __u);
95 }
96 
97 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
98 _LIBCPP_INLINE_VISIBILITY constexpr
99 bool in_range(_Up __u) noexcept
100 {
101   return _VSTD::cmp_less_equal(__u, numeric_limits<_Tp>::max()) &&
102          _VSTD::cmp_greater_equal(__u, numeric_limits<_Tp>::min());
103 }
104 #endif
105 
106 _LIBCPP_END_NAMESPACE_STD
107 
108 _LIBCPP_POP_MACROS
109 
110 #endif // _LIBCPP___UTILITY_CMP_H
111