xref: /freebsd/contrib/llvm-project/libcxx/include/__utility/cmp.h (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
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 <__type_traits/disjunction.h>
14 #include <__type_traits/is_integral.h>
15 #include <__type_traits/is_same.h>
16 #include <__type_traits/is_signed.h>
17 #include <__type_traits/make_unsigned.h>
18 #include <__utility/forward.h>
19 #include <__utility/move.h>
20 #include <limits>
21 
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 #  pragma GCC system_header
24 #endif
25 
26 _LIBCPP_PUSH_MACROS
27 #include <__undef_macros>
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 #if _LIBCPP_STD_VER >= 20
32 template<class _Tp, class... _Up>
33 struct _IsSameAsAny : _Or<_IsSame<_Tp, _Up>...> {};
34 
35 template<class _Tp>
36 concept __is_safe_integral_cmp = is_integral_v<_Tp> &&
37                       !_IsSameAsAny<_Tp, bool, char, char16_t, char32_t
38 #ifndef _LIBCPP_HAS_NO_CHAR8_T
39                                     , char8_t
40 #endif
41 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
42                                     , wchar_t
43 #endif
44                                     >::value;
45 
46 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
47 _LIBCPP_INLINE_VISIBILITY constexpr
48 bool cmp_equal(_Tp __t, _Up __u) noexcept
49 {
50   if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
51     return __t == __u;
52   else if constexpr (is_signed_v<_Tp>)
53     return __t < 0 ? false : make_unsigned_t<_Tp>(__t) == __u;
54   else
55     return __u < 0 ? false : __t == make_unsigned_t<_Up>(__u);
56 }
57 
58 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
59 _LIBCPP_INLINE_VISIBILITY constexpr
60 bool cmp_not_equal(_Tp __t, _Up __u) noexcept
61 {
62   return !_VSTD::cmp_equal(__t, __u);
63 }
64 
65 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
66 _LIBCPP_INLINE_VISIBILITY constexpr
67 bool cmp_less(_Tp __t, _Up __u) noexcept
68 {
69   if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
70     return __t < __u;
71   else if constexpr (is_signed_v<_Tp>)
72     return __t < 0 ? true : make_unsigned_t<_Tp>(__t) < __u;
73   else
74     return __u < 0 ? false : __t < make_unsigned_t<_Up>(__u);
75 }
76 
77 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
78 _LIBCPP_INLINE_VISIBILITY constexpr
79 bool cmp_greater(_Tp __t, _Up __u) noexcept
80 {
81   return _VSTD::cmp_less(__u, __t);
82 }
83 
84 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
85 _LIBCPP_INLINE_VISIBILITY constexpr
86 bool cmp_less_equal(_Tp __t, _Up __u) noexcept
87 {
88   return !_VSTD::cmp_greater(__t, __u);
89 }
90 
91 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
92 _LIBCPP_INLINE_VISIBILITY constexpr
93 bool cmp_greater_equal(_Tp __t, _Up __u) noexcept
94 {
95   return !_VSTD::cmp_less(__t, __u);
96 }
97 
98 template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
99 _LIBCPP_INLINE_VISIBILITY constexpr
100 bool in_range(_Up __u) noexcept
101 {
102   return _VSTD::cmp_less_equal(__u, numeric_limits<_Tp>::max()) &&
103          _VSTD::cmp_greater_equal(__u, numeric_limits<_Tp>::min());
104 }
105 #endif // _LIBCPP_STD_VER >= 20
106 
107 _LIBCPP_END_NAMESPACE_STD
108 
109 _LIBCPP_POP_MACROS
110 
111 #endif // _LIBCPP___UTILITY_CMP_H
112