xref: /freebsd/contrib/llvm-project/libcxx/include/__utility/cmp.h (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
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 =
37     is_integral_v<_Tp> &&
38     !_IsSameAsAny<_Tp,
39                   bool,
40                   char,
41                   char16_t,
42                   char32_t
43 #  ifndef _LIBCPP_HAS_NO_CHAR8_T
44                   ,
45                   char8_t
46 #  endif
47 #  ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
48                   ,
49                   wchar_t
50 #  endif
51                   >::value;
52 
53 template <__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
54 _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_equal(_Tp __t, _Up __u) noexcept {
55   if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
56     return __t == __u;
57   else if constexpr (is_signed_v<_Tp>)
58     return __t < 0 ? false : make_unsigned_t<_Tp>(__t) == __u;
59   else
60     return __u < 0 ? false : __t == make_unsigned_t<_Up>(__u);
61 }
62 
63 template <__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
64 _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_not_equal(_Tp __t, _Up __u) noexcept {
65   return !std::cmp_equal(__t, __u);
66 }
67 
68 template <__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
69 _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_less(_Tp __t, _Up __u) noexcept {
70   if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
71     return __t < __u;
72   else if constexpr (is_signed_v<_Tp>)
73     return __t < 0 ? true : make_unsigned_t<_Tp>(__t) < __u;
74   else
75     return __u < 0 ? false : __t < make_unsigned_t<_Up>(__u);
76 }
77 
78 template <__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
79 _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_greater(_Tp __t, _Up __u) noexcept {
80   return std::cmp_less(__u, __t);
81 }
82 
83 template <__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
84 _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_less_equal(_Tp __t, _Up __u) noexcept {
85   return !std::cmp_greater(__t, __u);
86 }
87 
88 template <__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
89 _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_greater_equal(_Tp __t, _Up __u) noexcept {
90   return !std::cmp_less(__t, __u);
91 }
92 
93 template <__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
94 _LIBCPP_HIDE_FROM_ABI constexpr bool in_range(_Up __u) noexcept {
95   return std::cmp_less_equal(__u, numeric_limits<_Tp>::max()) &&
96          std::cmp_greater_equal(__u, numeric_limits<_Tp>::min());
97 }
98 #endif // _LIBCPP_STD_VER >= 20
99 
100 _LIBCPP_END_NAMESPACE_STD
101 
102 _LIBCPP_POP_MACROS
103 
104 #endif // _LIBCPP___UTILITY_CMP_H
105