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/integer_traits.h>
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
29template <typename _Tp, typename _Ip>
30concept __comparison_can_promote_to =
31 sizeof(_Tp) < sizeof(_Ip) || (sizeof(_Tp) == sizeof(_Ip) && __signed_integer<_Tp>);
32
33template <__signed_or_unsigned_integer _Tp, __signed_or_unsigned_integer _Up>
34[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_equal(_Tp __t, _Up __u) noexcept {
35 if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
36 return __t == __u;
37 else if constexpr (__comparison_can_promote_to<_Tp, int> && __comparison_can_promote_to<_Up, int>)
38 return static_cast<int>(__t) == static_cast<int>(__u);
39 else if constexpr (__comparison_can_promote_to<_Tp, long long> && __comparison_can_promote_to<_Up, long long>)
40 return static_cast<long long>(__t) == static_cast<long long>(__u);
41 else if constexpr (is_signed_v<_Tp>)
42 return __t < 0 ? false : make_unsigned_t<_Tp>(__t) == __u;
43 else
44 return __u < 0 ? false : __t == make_unsigned_t<_Up>(__u);
45}
46
47template <__signed_or_unsigned_integer _Tp, __signed_or_unsigned_integer _Up>
48[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_not_equal(_Tp __t, _Up __u) noexcept {
49 return !std::cmp_equal(__t, __u);
50}
51
52template <__signed_or_unsigned_integer _Tp, __signed_or_unsigned_integer _Up>
53[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_less(_Tp __t, _Up __u) noexcept {
54 if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
55 return __t < __u;
56 else if constexpr (__comparison_can_promote_to<_Tp, int> && __comparison_can_promote_to<_Up, int>)
57 return static_cast<int>(__t) < static_cast<int>(__u);
58 else if constexpr (__comparison_can_promote_to<_Tp, long long> && __comparison_can_promote_to<_Up, long long>)
59 return static_cast<long long>(__t) < static_cast<long long>(__u);
60 else if constexpr (is_signed_v<_Tp>)
61 return __t < 0 ? true : make_unsigned_t<_Tp>(__t) < __u;
62 else
63 return __u < 0 ? false : __t < make_unsigned_t<_Up>(__u);
64}
65
66template <__signed_or_unsigned_integer _Tp, __signed_or_unsigned_integer _Up>
67[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_greater(_Tp __t, _Up __u) noexcept {
68 return std::cmp_less(__u, __t);
69}
70
71template <__signed_or_unsigned_integer _Tp, __signed_or_unsigned_integer _Up>
72[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_less_equal(_Tp __t, _Up __u) noexcept {
73 return !std::cmp_greater(__t, __u);
74}
75
76template <__signed_or_unsigned_integer _Tp, __signed_or_unsigned_integer _Up>
77[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_greater_equal(_Tp __t, _Up __u) noexcept {
78 return !std::cmp_less(__t, __u);
79}
80
81template <__signed_or_unsigned_integer _Tp, __signed_or_unsigned_integer _Up>
82[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool in_range(_Up __u) noexcept {
83 return std::cmp_less_equal(__u, numeric_limits<_Tp>::max()) &&
84 std::cmp_greater_equal(__u, numeric_limits<_Tp>::min());
85}
86
87#endif // _LIBCPP_STD_VER >= 20
88
89_LIBCPP_END_NAMESPACE_STD
90
91_LIBCPP_POP_MACROS
92
93#endif // _LIBCPP___UTILITY_CMP_H
94