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 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame^] | 18 | template <class T> |
| 19 | constexpr bool ispow2(T x) noexcept; // C++20 |
| 20 | template <class T> |
| 21 | constexpr T ceil2(T x); // C++20 |
| 22 | template <class T> |
| 23 | constexpr T floor2(T x) noexcept; // C++20 |
| 24 | template <class T> |
| 25 | constexpr T log2p1(T x) noexcept; // C++20 |
| 26 | |
| 27 | // 23.20.2, rotating |
| 28 | template<class T> |
| 29 | constexpr T rotl(T x, unsigned int s) noexcept; // C++20 |
| 30 | template<class T> |
| 31 | constexpr T rotr(T x, unsigned int s) noexcept; // C++20 |
| 32 | |
| 33 | // 23.20.3, counting |
| 34 | template<class T> |
| 35 | constexpr int countl_zero(T x) noexcept; // C++20 |
| 36 | template<class T> |
| 37 | constexpr int countl_one(T x) noexcept; // C++20 |
| 38 | template<class T> |
| 39 | constexpr int countr_zero(T x) noexcept; // C++20 |
| 40 | template<class T> |
| 41 | constexpr int countr_one(T x) noexcept; // C++20 |
| 42 | template<class T> |
| 43 | constexpr int popcount(T x) noexcept; // C++20 |
| 44 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 45 | } // namespace std |
| 46 | |
| 47 | */ |
| 48 | |
| 49 | #include <__config> |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame^] | 50 | #include <limits> |
| 51 | #include <type_traits> |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 52 | #include <version> |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame^] | 53 | #include <__debug> |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 54 | |
| 55 | #if defined(__IBMCPP__) |
| 56 | #include "support/ibm/support.h" |
| 57 | #endif |
| 58 | #if defined(_LIBCPP_COMPILER_MSVC) |
| 59 | #include <intrin.h> |
| 60 | #endif |
| 61 | |
| 62 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 63 | #pragma GCC system_header |
| 64 | #endif |
| 65 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame^] | 66 | _LIBCPP_PUSH_MACROS |
| 67 | #include <__undef_macros> |
| 68 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 69 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 70 | |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 71 | #ifndef _LIBCPP_COMPILER_MSVC |
| 72 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame^] | 73 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 74 | int __ctz(unsigned __x) _NOEXCEPT { return __builtin_ctz(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 75 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame^] | 76 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 77 | int __ctz(unsigned long __x) _NOEXCEPT { return __builtin_ctzl(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 78 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame^] | 79 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 80 | int __ctz(unsigned long long __x) _NOEXCEPT { return __builtin_ctzll(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 81 | |
| 82 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame^] | 83 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 84 | int __clz(unsigned __x) _NOEXCEPT { return __builtin_clz(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 85 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame^] | 86 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 87 | int __clz(unsigned long __x) _NOEXCEPT { return __builtin_clzl(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 88 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame^] | 89 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 90 | int __clz(unsigned long long __x) _NOEXCEPT { return __builtin_clzll(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 91 | |
| 92 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame^] | 93 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 94 | int __popcount(unsigned __x) _NOEXCEPT { return __builtin_popcount(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 95 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame^] | 96 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 97 | int __popcount(unsigned long __x) _NOEXCEPT { return __builtin_popcountl(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 98 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame^] | 99 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 100 | int __popcount(unsigned long long __x) _NOEXCEPT { return __builtin_popcountll(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 101 | |
| 102 | #else // _LIBCPP_COMPILER_MSVC |
| 103 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 104 | // Precondition: __x != 0 |
| 105 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 106 | int __ctz(unsigned __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 107 | static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); |
| 108 | static_assert(sizeof(unsigned long) == 4, ""); |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 109 | unsigned long __where; |
| 110 | if (_BitScanForward(&__where, __x)) |
| 111 | return static_cast<int>(__where); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 112 | return 32; |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 116 | int __ctz(unsigned long __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 117 | static_assert(sizeof(unsigned long) == sizeof(unsigned), ""); |
| 118 | return __ctz(static_cast<unsigned>(__x)); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 122 | int __ctz(unsigned long long __x) { |
| 123 | unsigned long __where; |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 124 | #if defined(_LIBCPP_HAS_BITSCAN64) |
| 125 | (defined(_M_AMD64) || defined(__x86_64__)) |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 126 | if (_BitScanForward64(&__where, __x)) |
| 127 | return static_cast<int>(__where); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 128 | #else |
| 129 | // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls. |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 130 | if (_BitScanForward(&__where, static_cast<unsigned long>(__x))) |
| 131 | return static_cast<int>(__where); |
| 132 | if (_BitScanForward(&__where, static_cast<unsigned long>(__x >> 32))) |
| 133 | return static_cast<int>(__where + 32); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 134 | #endif |
| 135 | return 64; |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // Precondition: __x != 0 |
| 139 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 140 | int __clz(unsigned __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 141 | static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); |
| 142 | static_assert(sizeof(unsigned long) == 4, ""); |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 143 | unsigned long __where; |
| 144 | if (_BitScanReverse(&__where, __x)) |
| 145 | return static_cast<int>(31 - __where); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 146 | return 32; // Undefined Behavior. |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 150 | int __clz(unsigned long __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 151 | static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); |
| 152 | return __clz(static_cast<unsigned>(__x)); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 156 | int __clz(unsigned long long __x) { |
| 157 | unsigned long __where; |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 158 | #if defined(_LIBCPP_HAS_BITSCAN64) |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 159 | if (_BitScanReverse64(&__where, __x)) |
| 160 | return static_cast<int>(63 - __where); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 161 | #else |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 162 | // Win32 doesn't have _BitScanReverse64 so emulate it with two 32 bit calls. |
| 163 | if (_BitScanReverse(&__where, static_cast<unsigned long>(__x >> 32))) |
| 164 | return static_cast<int>(63 - (__where + 32)); |
| 165 | if (_BitScanReverse(&__where, static_cast<unsigned long>(__x))) |
| 166 | return static_cast<int>(63 - __where); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 167 | #endif |
| 168 | return 64; // Undefined Behavior. |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 171 | inline _LIBCPP_INLINE_VISIBILITY int __popcount(unsigned __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 172 | static_assert(sizeof(unsigned) == 4, ""); |
| 173 | return __popcnt(__x); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 176 | inline _LIBCPP_INLINE_VISIBILITY int __popcount(unsigned long __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 177 | static_assert(sizeof(unsigned long) == 4, ""); |
| 178 | return __popcnt(__x); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 181 | inline _LIBCPP_INLINE_VISIBILITY int __popcount(unsigned long long __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 182 | static_assert(sizeof(unsigned long long) == 8, ""); |
| 183 | return __popcnt64(__x); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 186 | #endif // _LIBCPP_COMPILER_MSVC |
| 187 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame^] | 188 | #if _LIBCPP_STD_VER > 17 |
| 189 | |
| 190 | template <class _Tp> |
| 191 | using __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool, |
| 192 | is_integral_v<_Tp> && |
| 193 | is_unsigned_v<_Tp> && |
| 194 | _IsNotSame<remove_cv_t<_Tp>, bool>::value && |
| 195 | _IsNotSame<remove_cv_t<_Tp>, signed char>::value && |
| 196 | _IsNotSame<remove_cv_t<_Tp>, wchar_t>::value && |
| 197 | _IsNotSame<remove_cv_t<_Tp>, char16_t>::value && |
| 198 | _IsNotSame<remove_cv_t<_Tp>, char32_t>::value |
| 199 | >; |
| 200 | |
| 201 | |
| 202 | // rotl |
| 203 | template<class _Tp> |
| 204 | inline _LIBCPP_INLINE_VISIBILITY constexpr |
| 205 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> |
| 206 | rotl(_Tp __t, unsigned int __cnt) noexcept |
| 207 | { |
| 208 | const unsigned int __dig = numeric_limits<_Tp>::digits; |
| 209 | if ((__cnt % __dig) == 0) |
| 210 | return __t; |
| 211 | return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig))); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | // rotr |
| 216 | template<class _Tp> |
| 217 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 218 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> |
| 219 | rotr(_Tp __t, unsigned int __cnt) noexcept |
| 220 | { |
| 221 | const unsigned int __dig = numeric_limits<_Tp>::digits; |
| 222 | if ((__cnt % __dig) == 0) |
| 223 | return __t; |
| 224 | return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig))); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | // countl_zero |
| 229 | template<class _Tp> |
| 230 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 231 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> |
| 232 | countl_zero(_Tp __t) noexcept |
| 233 | { |
| 234 | if (__t == 0) |
| 235 | return numeric_limits<_Tp>::digits; |
| 236 | |
| 237 | if constexpr (sizeof(_Tp) <= sizeof(unsigned int)) |
| 238 | return __clz(static_cast<unsigned int>(__t)) |
| 239 | - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits); |
| 240 | else if constexpr (sizeof(_Tp) <= sizeof(unsigned long)) |
| 241 | return __clz(static_cast<unsigned long>(__t)) |
| 242 | - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits); |
| 243 | else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long)) |
| 244 | return __clz(static_cast<unsigned long long>(__t)) |
| 245 | - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits); |
| 246 | else |
| 247 | { |
| 248 | int __ret = 0; |
| 249 | int __iter = 0; |
| 250 | const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits; |
| 251 | while (true) { |
| 252 | __t = rotr(__t, __ulldigits); |
| 253 | if ((__iter = countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits) |
| 254 | break; |
| 255 | __ret += __iter; |
| 256 | } |
| 257 | return __ret + __iter; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | |
| 262 | // countl_one |
| 263 | template<class _Tp> |
| 264 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 265 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> |
| 266 | countl_one(_Tp __t) noexcept |
| 267 | { |
| 268 | return __t != numeric_limits<_Tp>::max() |
| 269 | ? countl_zero(static_cast<_Tp>(~__t)) |
| 270 | : numeric_limits<_Tp>::digits; |
| 271 | } |
| 272 | |
| 273 | |
| 274 | // countr_zero |
| 275 | template<class _Tp> |
| 276 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 277 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> |
| 278 | countr_zero(_Tp __t) noexcept |
| 279 | { |
| 280 | if (__t == 0) |
| 281 | return numeric_limits<_Tp>::digits; |
| 282 | |
| 283 | if constexpr (sizeof(_Tp) <= sizeof(unsigned int)) |
| 284 | return __ctz(static_cast<unsigned int>(__t)); |
| 285 | else if constexpr (sizeof(_Tp) <= sizeof(unsigned long)) |
| 286 | return __ctz(static_cast<unsigned long>(__t)); |
| 287 | else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long)) |
| 288 | return __ctz(static_cast<unsigned long long>(__t)); |
| 289 | else |
| 290 | { |
| 291 | int __ret = 0; |
| 292 | int __iter = 0; |
| 293 | const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits; |
| 294 | while ((__iter = countr_zero(static_cast<unsigned long long>(__t))) == __ulldigits) |
| 295 | { |
| 296 | __ret += __iter; |
| 297 | __t >>= __ulldigits; |
| 298 | } |
| 299 | return __ret + __iter; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | |
| 304 | // countr_one |
| 305 | template<class _Tp> |
| 306 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 307 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> |
| 308 | countr_one(_Tp __t) noexcept |
| 309 | { |
| 310 | return __t != numeric_limits<_Tp>::max() |
| 311 | ? countr_zero(static_cast<_Tp>(~__t)) |
| 312 | : numeric_limits<_Tp>::digits; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | // popcount |
| 317 | template<class _Tp> |
| 318 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 319 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> |
| 320 | popcount(_Tp __t) noexcept |
| 321 | { |
| 322 | if constexpr (sizeof(_Tp) <= sizeof(unsigned int)) |
| 323 | return __popcount(static_cast<unsigned int>(__t)); |
| 324 | else if constexpr (sizeof(_Tp) <= sizeof(unsigned long)) |
| 325 | return __popcount(static_cast<unsigned long>(__t)); |
| 326 | else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long)) |
| 327 | return __popcount(static_cast<unsigned long long>(__t)); |
| 328 | else |
| 329 | { |
| 330 | int __ret = 0; |
| 331 | while (__t != 0) |
| 332 | { |
| 333 | __ret += __popcount(static_cast<unsigned long long>(__t)); |
| 334 | __t >>= numeric_limits<unsigned long long>::digits; |
| 335 | } |
| 336 | return __ret; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | |
| 341 | // integral log base 2 |
| 342 | template<class _Tp> |
| 343 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 344 | unsigned __bit_log2(_Tp __t) noexcept |
| 345 | { return std::numeric_limits<_Tp>::digits - 1 - countl_zero(__t); } |
| 346 | |
| 347 | |
| 348 | template <class _Tp> |
| 349 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 350 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, bool> |
| 351 | ispow2(_Tp __t) noexcept { return popcount(__t) == 1; } |
| 352 | |
| 353 | template <class _Tp> |
| 354 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 355 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> |
| 356 | floor2(_Tp __t) noexcept { return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t); } |
| 357 | |
| 358 | template <class _Tp> |
| 359 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 360 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> |
| 361 | ceil2(_Tp __t) noexcept |
| 362 | { |
| 363 | if (__t < 2) return 1; |
| 364 | const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u)); |
| 365 | |
| 366 | #ifndef _LIBCPP_HAS_NO_BUILTIN_IS_CONSTANT_EVALUATED |
| 367 | if (!__builtin_is_constant_evaluated ()) |
| 368 | _LIBCPP_DEBUG_ASSERT( __n != numeric_limits<_Tp>::digits, "Bad input to ceil2" ); |
| 369 | #endif |
| 370 | |
| 371 | if constexpr (sizeof(_Tp) >= sizeof(unsigned)) |
| 372 | return _Tp{1} << __n; |
| 373 | else |
| 374 | { |
| 375 | const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits; |
| 376 | const unsigned __retVal = 1u << (__n + __extra); |
| 377 | return (_Tp) (__retVal >> __extra); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | template <class _Tp> |
| 382 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 383 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> |
| 384 | log2p1(_Tp __t) noexcept |
| 385 | { return __t == 0 ? 0 : __bit_log2(__t) + 1; } |
| 386 | |
| 387 | #endif // _LIBCPP_STD_VER > 17 |
| 388 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 389 | _LIBCPP_END_NAMESPACE_STD |
| 390 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame^] | 391 | _LIBCPP_POP_MACROS |
| 392 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 393 | #endif // _LIBCPP_BIT |