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> |
Thorsten Schütt | 9082109 | 2021-01-18 13:21:00 +0100 | [diff] [blame] | 58 | #include <__bits> |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 59 | #include <limits> |
| 60 | #include <type_traits> |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 61 | #include <version> |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 62 | #include <__debug> |
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 | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 80 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 81 | template <class _Tp> |
| 82 | using __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool, |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 83 | is_integral<_Tp>::value && |
| 84 | is_unsigned<_Tp>::value && |
| 85 | _IsNotSame<typename remove_cv<_Tp>::type, bool>::value && |
| 86 | _IsNotSame<typename remove_cv<_Tp>::type, signed char>::value && |
| 87 | _IsNotSame<typename remove_cv<_Tp>::type, wchar_t>::value && |
| 88 | _IsNotSame<typename remove_cv<_Tp>::type, char16_t>::value && |
| 89 | _IsNotSame<typename remove_cv<_Tp>::type, char32_t>::value |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 90 | >; |
| 91 | |
| 92 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 93 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 94 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 95 | _Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 96 | { |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 97 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotl requires unsigned"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 98 | const unsigned int __dig = numeric_limits<_Tp>::digits; |
| 99 | if ((__cnt % __dig) == 0) |
| 100 | return __t; |
| 101 | return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig))); |
| 102 | } |
| 103 | |
| 104 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 105 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 106 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 107 | _Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 108 | { |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 109 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotr requires unsigned"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 110 | const unsigned int __dig = numeric_limits<_Tp>::digits; |
| 111 | if ((__cnt % __dig) == 0) |
| 112 | return __t; |
| 113 | return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig))); |
| 114 | } |
| 115 | |
| 116 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 117 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 118 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 119 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 120 | int __countr_zero(_Tp __t) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 121 | { |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 122 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_zero requires unsigned"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 123 | if (__t == 0) |
| 124 | return numeric_limits<_Tp>::digits; |
| 125 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 126 | if (sizeof(_Tp) <= sizeof(unsigned int)) |
| 127 | return __libcpp_ctz(static_cast<unsigned int>(__t)); |
| 128 | else if (sizeof(_Tp) <= sizeof(unsigned long)) |
| 129 | return __libcpp_ctz(static_cast<unsigned long>(__t)); |
| 130 | else if (sizeof(_Tp) <= sizeof(unsigned long long)) |
| 131 | return __libcpp_ctz(static_cast<unsigned long long>(__t)); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 132 | else |
| 133 | { |
| 134 | int __ret = 0; |
| 135 | int __iter = 0; |
| 136 | const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits; |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 137 | while ((__iter = __libcpp_ctz(static_cast<unsigned long long>(__t))) == __ulldigits) |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 138 | { |
| 139 | __ret += __iter; |
| 140 | __t >>= __ulldigits; |
| 141 | } |
| 142 | return __ret + __iter; |
| 143 | } |
| 144 | } |
| 145 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 146 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 147 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 148 | int __countl_zero(_Tp __t) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 149 | { |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 150 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_zero requires unsigned"); |
| 151 | if (__t == 0) |
| 152 | return numeric_limits<_Tp>::digits; |
| 153 | |
| 154 | if (sizeof(_Tp) <= sizeof(unsigned int)) |
| 155 | return __libcpp_clz(static_cast<unsigned int>(__t)) |
| 156 | - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits); |
| 157 | else if (sizeof(_Tp) <= sizeof(unsigned long)) |
| 158 | return __libcpp_clz(static_cast<unsigned long>(__t)) |
| 159 | - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits); |
| 160 | else if (sizeof(_Tp) <= sizeof(unsigned long long)) |
| 161 | return __libcpp_clz(static_cast<unsigned long long>(__t)) |
| 162 | - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits); |
| 163 | else |
| 164 | { |
| 165 | int __ret = 0; |
| 166 | int __iter = 0; |
| 167 | const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits; |
| 168 | while (true) { |
| 169 | __t = __rotr(__t, __ulldigits); |
| 170 | if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits) |
| 171 | break; |
| 172 | __ret += __iter; |
| 173 | } |
| 174 | return __ret + __iter; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | template<class _Tp> |
| 179 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 180 | int __countl_one(_Tp __t) _NOEXCEPT |
| 181 | { |
| 182 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_one requires unsigned"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 183 | return __t != numeric_limits<_Tp>::max() |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 184 | ? __countl_zero(static_cast<_Tp>(~__t)) |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 185 | : numeric_limits<_Tp>::digits; |
| 186 | } |
| 187 | |
| 188 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 189 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 190 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 191 | int __countr_one(_Tp __t) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 192 | { |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 193 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_one requires unsigned"); |
| 194 | return __t != numeric_limits<_Tp>::max() |
| 195 | ? __countr_zero(static_cast<_Tp>(~__t)) |
| 196 | : numeric_limits<_Tp>::digits; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | template<class _Tp> |
| 201 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 202 | int |
| 203 | __popcount(_Tp __t) _NOEXCEPT |
| 204 | { |
| 205 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__libcpp_popcount requires unsigned"); |
| 206 | if (sizeof(_Tp) <= sizeof(unsigned int)) |
| 207 | return __libcpp_popcount(static_cast<unsigned int>(__t)); |
| 208 | else if (sizeof(_Tp) <= sizeof(unsigned long)) |
| 209 | return __libcpp_popcount(static_cast<unsigned long>(__t)); |
| 210 | else if (sizeof(_Tp) <= sizeof(unsigned long long)) |
| 211 | return __libcpp_popcount(static_cast<unsigned long long>(__t)); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 212 | else |
| 213 | { |
| 214 | int __ret = 0; |
| 215 | while (__t != 0) |
| 216 | { |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 217 | __ret += __libcpp_popcount(static_cast<unsigned long long>(__t)); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 218 | __t >>= numeric_limits<unsigned long long>::digits; |
| 219 | } |
| 220 | return __ret; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | // integral log base 2 |
| 226 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 227 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 228 | unsigned __bit_log2(_Tp __t) _NOEXCEPT |
| 229 | { |
| 230 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__bit_log2 requires unsigned"); |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 231 | return numeric_limits<_Tp>::digits - 1 - __countl_zero(__t); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | template <class _Tp> |
| 235 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 236 | bool __has_single_bit(_Tp __t) _NOEXCEPT |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 237 | { |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 238 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__has_single_bit requires unsigned"); |
Louis Dionne | a1e3b7d | 2020-05-28 14:28:09 -0400 | [diff] [blame] | 239 | return __t != 0 && (((__t & (__t - 1)) == 0)); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
| 243 | #if _LIBCPP_STD_VER > 17 |
| 244 | |
| 245 | template<class _Tp> |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 246 | _LIBCPP_INLINE_VISIBILITY constexpr |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 247 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> |
| 248 | rotl(_Tp __t, unsigned int __cnt) noexcept |
| 249 | { |
| 250 | return __rotl(__t, __cnt); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | // rotr |
| 255 | template<class _Tp> |
| 256 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 257 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> |
| 258 | rotr(_Tp __t, unsigned int __cnt) noexcept |
| 259 | { |
| 260 | return __rotr(__t, __cnt); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | template<class _Tp> |
| 265 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 266 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> |
| 267 | countl_zero(_Tp __t) noexcept |
| 268 | { |
| 269 | return __countl_zero(__t); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | template<class _Tp> |
| 274 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 275 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> |
| 276 | countl_one(_Tp __t) noexcept |
| 277 | { |
| 278 | return __countl_one(__t); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | template<class _Tp> |
| 283 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 284 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> |
| 285 | countr_zero(_Tp __t) noexcept |
| 286 | { |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 287 | return __countr_zero(__t); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | |
| 291 | template<class _Tp> |
| 292 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 293 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> |
| 294 | countr_one(_Tp __t) noexcept |
| 295 | { |
| 296 | return __countr_one(__t); |
| 297 | } |
| 298 | |
| 299 | |
| 300 | template<class _Tp> |
| 301 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 302 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> |
| 303 | popcount(_Tp __t) noexcept |
| 304 | { |
| 305 | return __popcount(__t); |
| 306 | } |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 307 | |
| 308 | |
| 309 | template <class _Tp> |
| 310 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 311 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, bool> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 312 | has_single_bit(_Tp __t) noexcept |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 313 | { |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 314 | return __has_single_bit(__t); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 315 | } |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 316 | |
| 317 | template <class _Tp> |
| 318 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 319 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 320 | bit_floor(_Tp __t) noexcept |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 321 | { |
| 322 | return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t); |
| 323 | } |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 324 | |
| 325 | template <class _Tp> |
| 326 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 327 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 328 | bit_ceil(_Tp __t) noexcept |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 329 | { |
| 330 | if (__t < 2) return 1; |
| 331 | 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] | 332 | _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] | 333 | |
| 334 | if constexpr (sizeof(_Tp) >= sizeof(unsigned)) |
| 335 | return _Tp{1} << __n; |
| 336 | else |
| 337 | { |
| 338 | const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits; |
| 339 | const unsigned __retVal = 1u << (__n + __extra); |
| 340 | return (_Tp) (__retVal >> __extra); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | template <class _Tp> |
| 345 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 346 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 347 | bit_width(_Tp __t) noexcept |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 348 | { |
| 349 | return __t == 0 ? 0 : __bit_log2(__t) + 1; |
| 350 | } |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 351 | |
Marshall Clow | e8a18f3 | 2019-07-23 04:20:19 +0000 | [diff] [blame] | 352 | enum class endian |
| 353 | { |
| 354 | little = 0xDEAD, |
| 355 | big = 0xFACE, |
| 356 | #if defined(_LIBCPP_LITTLE_ENDIAN) |
| 357 | native = little |
| 358 | #elif defined(_LIBCPP_BIG_ENDIAN) |
| 359 | native = big |
| 360 | #else |
| 361 | native = 0xCAFE |
| 362 | #endif |
| 363 | }; |
| 364 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 365 | #endif // _LIBCPP_STD_VER > 17 |
| 366 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 367 | _LIBCPP_END_NAMESPACE_STD |
| 368 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 369 | _LIBCPP_POP_MACROS |
| 370 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 371 | #endif // _LIBCPP_BIT |