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