| 1 | // Integer comparison functions -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2020-2026 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | /** @file bits/intcmp.h |
| 26 | * This is an internal header file, included by other library headers. |
| 27 | * Do not attempt to use it directly. @headername{utility} |
| 28 | */ |
| 29 | |
| 30 | #ifndef _GLIBCXX_INTCMP_H |
| 31 | #define _GLIBCXX_INTCMP_H 1 |
| 32 | |
| 33 | #ifdef _GLIBCXX_SYSHDR |
| 34 | #pragma GCC system_header |
| 35 | #endif |
| 36 | |
| 37 | #include <bits/version.h> |
| 38 | |
| 39 | #ifdef __glibcxx_integer_comparison_functions // C++ >= 20 |
| 40 | |
| 41 | #include <type_traits> |
| 42 | #include <ext/numeric_traits.h> // __int_traits |
| 43 | |
| 44 | namespace std _GLIBCXX_VISIBILITY(default) |
| 45 | { |
| 46 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 47 | |
| 48 | template<typename _Tp, typename _Up> |
| 49 | constexpr bool |
| 50 | cmp_equal(_Tp __t, _Up __u) noexcept |
| 51 | { |
| 52 | static_assert(__is_signed_or_unsigned_integer<_Tp>::value); |
| 53 | static_assert(__is_signed_or_unsigned_integer<_Up>::value); |
| 54 | |
| 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 && make_unsigned_t<_Tp>(__t) == __u; |
| 59 | else |
| 60 | return __u >= 0 && __t == make_unsigned_t<_Up>(__u); |
| 61 | } |
| 62 | |
| 63 | template<typename _Tp, typename _Up> |
| 64 | constexpr bool |
| 65 | cmp_not_equal(_Tp __t, _Up __u) noexcept |
| 66 | { return !std::cmp_equal(__t, __u); } |
| 67 | |
| 68 | template<typename _Tp, typename _Up> |
| 69 | constexpr bool |
| 70 | cmp_less(_Tp __t, _Up __u) noexcept |
| 71 | { |
| 72 | static_assert(__is_signed_or_unsigned_integer<_Tp>::value); |
| 73 | static_assert(__is_signed_or_unsigned_integer<_Up>::value); |
| 74 | |
| 75 | if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>) |
| 76 | return __t < __u; |
| 77 | else if constexpr (is_signed_v<_Tp>) |
| 78 | return __t < 0 || make_unsigned_t<_Tp>(__t) < __u; |
| 79 | else |
| 80 | return __u >= 0 && __t < make_unsigned_t<_Up>(__u); |
| 81 | } |
| 82 | |
| 83 | template<typename _Tp, typename _Up> |
| 84 | constexpr bool |
| 85 | cmp_greater(_Tp __t, _Up __u) noexcept |
| 86 | { return std::cmp_less(__u, __t); } |
| 87 | |
| 88 | template<typename _Tp, typename _Up> |
| 89 | constexpr bool |
| 90 | cmp_less_equal(_Tp __t, _Up __u) noexcept |
| 91 | { return !std::cmp_less(__u, __t); } |
| 92 | |
| 93 | template<typename _Tp, typename _Up> |
| 94 | constexpr bool |
| 95 | cmp_greater_equal(_Tp __t, _Up __u) noexcept |
| 96 | { return !std::cmp_less(__t, __u); } |
| 97 | |
| 98 | template<typename _Res, typename _Tp> |
| 99 | constexpr bool |
| 100 | in_range(_Tp __t) noexcept |
| 101 | { |
| 102 | static_assert(__is_signed_or_unsigned_integer<_Res>::value); |
| 103 | static_assert(__is_signed_or_unsigned_integer<_Tp>::value); |
| 104 | using __gnu_cxx::__int_traits; |
| 105 | |
| 106 | if constexpr (is_signed_v<_Tp> == is_signed_v<_Res>) |
| 107 | return __int_traits<_Res>::__min <= __t |
| 108 | && __t <= __int_traits<_Res>::__max; |
| 109 | else if constexpr (is_signed_v<_Tp>) |
| 110 | return __t >= 0 |
| 111 | && make_unsigned_t<_Tp>(__t) <= __int_traits<_Res>::__max; |
| 112 | else |
| 113 | return __t <= make_unsigned_t<_Res>(__int_traits<_Res>::__max); |
| 114 | } |
| 115 | |
| 116 | _GLIBCXX_END_NAMESPACE_VERSION |
| 117 | } // namespace |
| 118 | |
| 119 | #endif // __glibcxx_integer_comparison_functions |
| 120 | #endif // _GLIBCXX_INTCMP_H |
| 121 | |