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 <__concepts/arithmetic.h>
13#include <__config>
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 <__libcpp_integer _Tp, __libcpp_integer _Up>
30_LIBCPP_HIDE_FROM_ABI constexpr bool cmp_equal(_Tp __t, _Up __u) noexcept {
31 if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
32 return __t == __u;
33 else if constexpr (is_signed_v<_Tp>)
34 return __t < 0 ? false : make_unsigned_t<_Tp>(__t) == __u;
35 else
36 return __u < 0 ? false : __t == make_unsigned_t<_Up>(__u);
37}
38
39template <__libcpp_integer _Tp, __libcpp_integer _Up>
40_LIBCPP_HIDE_FROM_ABI constexpr bool cmp_not_equal(_Tp __t, _Up __u) noexcept {
41 return !std::cmp_equal(__t, __u);
42}
43
44template <__libcpp_integer _Tp, __libcpp_integer _Up>
45_LIBCPP_HIDE_FROM_ABI constexpr bool cmp_less(_Tp __t, _Up __u) noexcept {
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 ? true : make_unsigned_t<_Tp>(__t) < __u;
50 else
51 return __u < 0 ? false : __t < make_unsigned_t<_Up>(__u);
52}
53
54template <__libcpp_integer _Tp, __libcpp_integer _Up>
55_LIBCPP_HIDE_FROM_ABI constexpr bool cmp_greater(_Tp __t, _Up __u) noexcept {
56 return std::cmp_less(__u, __t);
57}
58
59template <__libcpp_integer _Tp, __libcpp_integer _Up>
60_LIBCPP_HIDE_FROM_ABI constexpr bool cmp_less_equal(_Tp __t, _Up __u) noexcept {
61 return !std::cmp_greater(__t, __u);
62}
63
64template <__libcpp_integer _Tp, __libcpp_integer _Up>
65_LIBCPP_HIDE_FROM_ABI constexpr bool cmp_greater_equal(_Tp __t, _Up __u) noexcept {
66 return !std::cmp_less(__t, __u);
67}
68
69template <__libcpp_integer _Tp, __libcpp_integer _Up>
70_LIBCPP_HIDE_FROM_ABI constexpr bool in_range(_Up __u) noexcept {
71 return std::cmp_less_equal(__u, numeric_limits<_Tp>::max()) &&
72 std::cmp_greater_equal(__u, numeric_limits<_Tp>::min());
73}
74
75#endif // _LIBCPP_STD_VER >= 20
76
77_LIBCPP_END_NAMESPACE_STD
78
79_LIBCPP_POP_MACROS
80
81#endif // _LIBCPP___UTILITY_CMP_H
82