Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
Louis Dionne | 9bd9388 | 2021-11-17 16:25:01 -0500 | [diff] [blame] | 2 | //===----------------------------------------------------------------------===// |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 7 | // |
| 8 | //===---------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_BIT |
| 11 | #define _LIBCPP_BIT |
| 12 | |
| 13 | /* |
| 14 | bit synopsis |
| 15 | |
| 16 | namespace std { |
Nikolas Klauser | b52cfd9 | 2021-11-22 00:22:55 +0100 | [diff] [blame^] | 17 | // [bit.cast], bit_cast |
| 18 | template<class To, class From> |
| 19 | constexpr To bit_cast(const From& from) noexcept; // C++20 |
| 20 | |
| 21 | // [bit.byteswap], byteswap |
| 22 | template<class T> |
| 23 | constexpr T byteswap(T value) noexcept; // C++23 |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 24 | |
Louis Dionne | a1e3b7d | 2020-05-28 14:28:09 -0400 | [diff] [blame] | 25 | // [bit.pow.two], integral powers of 2 |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 26 | template <class T> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 27 | constexpr bool has_single_bit(T x) noexcept; // C++20 |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 28 | template <class T> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 29 | constexpr T bit_ceil(T x); // C++20 |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 30 | template <class T> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 31 | constexpr T bit_floor(T x) noexcept; // C++20 |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 32 | template <class T> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 33 | constexpr T bit_width(T x) noexcept; // C++20 |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 34 | |
Louis Dionne | a1e3b7d | 2020-05-28 14:28:09 -0400 | [diff] [blame] | 35 | // [bit.rotate], rotating |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 36 | template<class T> |
| 37 | constexpr T rotl(T x, unsigned int s) noexcept; // C++20 |
| 38 | template<class T> |
| 39 | constexpr T rotr(T x, unsigned int s) noexcept; // C++20 |
| 40 | |
Louis Dionne | a1e3b7d | 2020-05-28 14:28:09 -0400 | [diff] [blame] | 41 | // [bit.count], counting |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 42 | template<class T> |
| 43 | constexpr int countl_zero(T x) noexcept; // C++20 |
| 44 | template<class T> |
| 45 | constexpr int countl_one(T x) noexcept; // C++20 |
| 46 | template<class T> |
| 47 | constexpr int countr_zero(T x) noexcept; // C++20 |
| 48 | template<class T> |
| 49 | constexpr int countr_one(T x) noexcept; // C++20 |
| 50 | template<class T> |
| 51 | constexpr int popcount(T x) noexcept; // C++20 |
| 52 | |
Louis Dionne | a1e3b7d | 2020-05-28 14:28:09 -0400 | [diff] [blame] | 53 | // [bit.endian], endian |
Marshall Clow | e8a18f3 | 2019-07-23 04:20:19 +0000 | [diff] [blame] | 54 | enum class endian { |
| 55 | little = see below, // C++20 |
| 56 | big = see below, // C++20 |
| 57 | native = see below // C++20 |
Nikolas Klauser | b52cfd9 | 2021-11-22 00:22:55 +0100 | [diff] [blame^] | 58 | }; |
Marshall Clow | e8a18f3 | 2019-07-23 04:20:19 +0000 | [diff] [blame] | 59 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 60 | } // namespace std |
| 61 | |
| 62 | */ |
| 63 | |
Louis Dionne | 1462d4d | 2020-05-28 14:28:38 -0400 | [diff] [blame] | 64 | #include <__bit/bit_cast.h> |
Nikolas Klauser | b52cfd9 | 2021-11-22 00:22:55 +0100 | [diff] [blame^] | 65 | #include <__bit/byteswap.h> |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame] | 66 | #include <__bits> // __libcpp_clz |
Louis Dionne | 1462d4d | 2020-05-28 14:28:38 -0400 | [diff] [blame] | 67 | #include <__config> |
Arthur O'Dwyer | 597cac4 | 2021-05-12 23:04:03 -0400 | [diff] [blame] | 68 | #include <__debug> |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 69 | #include <limits> |
| 70 | #include <type_traits> |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 71 | #include <version> |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 72 | |
| 73 | #if defined(__IBMCPP__) |
Louis Dionne | 90a0449 | 2021-02-02 16:58:38 -0500 | [diff] [blame] | 74 | #include "__support/ibm/support.h" |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 75 | #endif |
| 76 | #if defined(_LIBCPP_COMPILER_MSVC) |
| 77 | #include <intrin.h> |
| 78 | #endif |
| 79 | |
| 80 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 81 | #pragma GCC system_header |
| 82 | #endif |
| 83 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 84 | _LIBCPP_PUSH_MACROS |
| 85 | #include <__undef_macros> |
| 86 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 87 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 88 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 89 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 90 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 91 | _Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 92 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 93 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 94 | const unsigned int __dig = numeric_limits<_Tp>::digits; |
| 95 | if ((__cnt % __dig) == 0) |
| 96 | return __t; |
| 97 | return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig))); |
| 98 | } |
| 99 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 100 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 101 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 102 | _Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 103 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 104 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 105 | const unsigned int __dig = numeric_limits<_Tp>::digits; |
| 106 | if ((__cnt % __dig) == 0) |
| 107 | return __t; |
| 108 | return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig))); |
| 109 | } |
| 110 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 111 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 112 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 113 | int __countr_zero(_Tp __t) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 114 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 115 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_zero requires an unsigned integer type"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 116 | if (__t == 0) |
| 117 | return numeric_limits<_Tp>::digits; |
| 118 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 119 | if (sizeof(_Tp) <= sizeof(unsigned int)) |
| 120 | return __libcpp_ctz(static_cast<unsigned int>(__t)); |
| 121 | else if (sizeof(_Tp) <= sizeof(unsigned long)) |
| 122 | return __libcpp_ctz(static_cast<unsigned long>(__t)); |
| 123 | else if (sizeof(_Tp) <= sizeof(unsigned long long)) |
| 124 | return __libcpp_ctz(static_cast<unsigned long long>(__t)); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 125 | else |
| 126 | { |
| 127 | int __ret = 0; |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 128 | const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits; |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 129 | while (static_cast<unsigned long long>(__t) == 0uLL) |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 130 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 131 | __ret += __ulldigits; |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 132 | __t >>= __ulldigits; |
| 133 | } |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 134 | return __ret + __libcpp_ctz(static_cast<unsigned long long>(__t)); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 138 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 139 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 140 | int __countl_zero(_Tp __t) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 141 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 142 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type"); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 143 | if (__t == 0) |
| 144 | return numeric_limits<_Tp>::digits; |
| 145 | |
| 146 | if (sizeof(_Tp) <= sizeof(unsigned int)) |
| 147 | return __libcpp_clz(static_cast<unsigned int>(__t)) |
| 148 | - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits); |
| 149 | else if (sizeof(_Tp) <= sizeof(unsigned long)) |
| 150 | return __libcpp_clz(static_cast<unsigned long>(__t)) |
| 151 | - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits); |
| 152 | else if (sizeof(_Tp) <= sizeof(unsigned long long)) |
| 153 | return __libcpp_clz(static_cast<unsigned long long>(__t)) |
| 154 | - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits); |
| 155 | else |
| 156 | { |
| 157 | int __ret = 0; |
| 158 | int __iter = 0; |
| 159 | const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits; |
| 160 | while (true) { |
| 161 | __t = __rotr(__t, __ulldigits); |
| 162 | if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits) |
| 163 | break; |
| 164 | __ret += __iter; |
| 165 | } |
| 166 | return __ret + __iter; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | template<class _Tp> |
| 171 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 172 | int __countl_one(_Tp __t) _NOEXCEPT |
| 173 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 174 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_one requires an unsigned integer type"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 175 | return __t != numeric_limits<_Tp>::max() |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 176 | ? __countl_zero(static_cast<_Tp>(~__t)) |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 177 | : numeric_limits<_Tp>::digits; |
| 178 | } |
| 179 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 180 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 181 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 182 | int __countr_one(_Tp __t) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 183 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 184 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_one requires an unsigned integer type"); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 185 | return __t != numeric_limits<_Tp>::max() |
| 186 | ? __countr_zero(static_cast<_Tp>(~__t)) |
| 187 | : numeric_limits<_Tp>::digits; |
| 188 | } |
| 189 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 190 | template<class _Tp> |
| 191 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 192 | int __popcount(_Tp __t) _NOEXCEPT |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 193 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 194 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__popcount requires an unsigned integer type"); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 195 | if (sizeof(_Tp) <= sizeof(unsigned int)) |
| 196 | return __libcpp_popcount(static_cast<unsigned int>(__t)); |
| 197 | else if (sizeof(_Tp) <= sizeof(unsigned long)) |
| 198 | return __libcpp_popcount(static_cast<unsigned long>(__t)); |
| 199 | else if (sizeof(_Tp) <= sizeof(unsigned long long)) |
| 200 | return __libcpp_popcount(static_cast<unsigned long long>(__t)); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 201 | else |
| 202 | { |
| 203 | int __ret = 0; |
| 204 | while (__t != 0) |
| 205 | { |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 206 | __ret += __libcpp_popcount(static_cast<unsigned long long>(__t)); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 207 | __t >>= numeric_limits<unsigned long long>::digits; |
| 208 | } |
| 209 | return __ret; |
| 210 | } |
| 211 | } |
| 212 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 213 | // integral log base 2 |
| 214 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 215 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 216 | unsigned __bit_log2(_Tp __t) _NOEXCEPT |
| 217 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 218 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__bit_log2 requires an unsigned integer type"); |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 219 | return numeric_limits<_Tp>::digits - 1 - __countl_zero(__t); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | template <class _Tp> |
| 223 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 224 | bool __has_single_bit(_Tp __t) _NOEXCEPT |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 225 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 226 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__has_single_bit requires an unsigned integer type"); |
Louis Dionne | a1e3b7d | 2020-05-28 14:28:09 -0400 | [diff] [blame] | 227 | return __t != 0 && (((__t & (__t - 1)) == 0)); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 230 | #if _LIBCPP_STD_VER > 17 |
| 231 | |
| 232 | template<class _Tp> |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 233 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 234 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 235 | rotl(_Tp __t, unsigned int __cnt) noexcept |
| 236 | { |
| 237 | return __rotl(__t, __cnt); |
| 238 | } |
| 239 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 240 | template<class _Tp> |
| 241 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 242 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 243 | rotr(_Tp __t, unsigned int __cnt) noexcept |
| 244 | { |
| 245 | return __rotr(__t, __cnt); |
| 246 | } |
| 247 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 248 | template<class _Tp> |
| 249 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 250 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 251 | countl_zero(_Tp __t) noexcept |
| 252 | { |
| 253 | return __countl_zero(__t); |
| 254 | } |
| 255 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 256 | template<class _Tp> |
| 257 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 258 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 259 | countl_one(_Tp __t) noexcept |
| 260 | { |
| 261 | return __countl_one(__t); |
| 262 | } |
| 263 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 264 | template<class _Tp> |
| 265 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 266 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 267 | countr_zero(_Tp __t) noexcept |
| 268 | { |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 269 | return __countr_zero(__t); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 272 | template<class _Tp> |
| 273 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 274 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 275 | countr_one(_Tp __t) noexcept |
| 276 | { |
| 277 | return __countr_one(__t); |
| 278 | } |
| 279 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 280 | template<class _Tp> |
| 281 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 282 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 283 | popcount(_Tp __t) noexcept |
| 284 | { |
| 285 | return __popcount(__t); |
| 286 | } |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 287 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 288 | template <class _Tp> |
| 289 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 290 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, bool> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 291 | has_single_bit(_Tp __t) noexcept |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 292 | { |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 293 | return __has_single_bit(__t); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 294 | } |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 295 | |
| 296 | template <class _Tp> |
| 297 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 298 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 299 | bit_floor(_Tp __t) noexcept |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 300 | { |
| 301 | return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t); |
| 302 | } |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 303 | |
| 304 | template <class _Tp> |
| 305 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 306 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 307 | bit_ceil(_Tp __t) noexcept |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 308 | { |
| 309 | if (__t < 2) return 1; |
| 310 | const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u)); |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 311 | _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 312 | |
| 313 | if constexpr (sizeof(_Tp) >= sizeof(unsigned)) |
| 314 | return _Tp{1} << __n; |
| 315 | else |
| 316 | { |
| 317 | const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits; |
| 318 | const unsigned __retVal = 1u << (__n + __extra); |
| 319 | return (_Tp) (__retVal >> __extra); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | template <class _Tp> |
| 324 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 325 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 326 | bit_width(_Tp __t) noexcept |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 327 | { |
| 328 | return __t == 0 ? 0 : __bit_log2(__t) + 1; |
| 329 | } |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 330 | |
Marshall Clow | e8a18f3 | 2019-07-23 04:20:19 +0000 | [diff] [blame] | 331 | enum class endian |
| 332 | { |
| 333 | little = 0xDEAD, |
| 334 | big = 0xFACE, |
| 335 | #if defined(_LIBCPP_LITTLE_ENDIAN) |
| 336 | native = little |
| 337 | #elif defined(_LIBCPP_BIG_ENDIAN) |
| 338 | native = big |
| 339 | #else |
| 340 | native = 0xCAFE |
| 341 | #endif |
| 342 | }; |
| 343 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 344 | #endif // _LIBCPP_STD_VER > 17 |
| 345 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 346 | _LIBCPP_END_NAMESPACE_STD |
| 347 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 348 | _LIBCPP_POP_MACROS |
| 349 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 350 | #endif // _LIBCPP_BIT |