Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / include / __utility / cmp.h
blobb7c1ed614dfcb63d59933307ba8b18d8a947645b
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___UTILITY_CMP_H
10 #define _LIBCPP___UTILITY_CMP_H
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>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 # pragma GCC system_header
20 #endif
22 _LIBCPP_PUSH_MACROS
23 #include <__undef_macros>
25 _LIBCPP_BEGIN_NAMESPACE_STD
27 #if _LIBCPP_STD_VER >= 20
29 template <__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);
39 template <__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);
44 template <__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);
54 template <__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);
59 template <__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);
64 template <__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);
69 template <__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());
75 #endif // _LIBCPP_STD_VER >= 20
77 _LIBCPP_END_NAMESPACE_STD
79 _LIBCPP_POP_MACROS
81 #endif // _LIBCPP___UTILITY_CMP_H