1// -*- C++ -*- 2//===------------------------------ bit ----------------------------------===// 3// 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 7// 8//===---------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_BIT 11#define _LIBCPP_BIT 12 13/* 14 bit synopsis 15 16namespace std { 17 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 45} // namespace std 46 47*/ 48 49#include <__config> 50#include <limits> 51#include <type_traits> 52#include <version> 53#include <__debug> 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 66_LIBCPP_PUSH_MACROS 67#include <__undef_macros> 68 69_LIBCPP_BEGIN_NAMESPACE_STD 70 71#ifndef _LIBCPP_COMPILER_MSVC 72 73inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 74int __libcpp_ctz(unsigned __x) _NOEXCEPT { return __builtin_ctz(__x); } 75 76inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 77int __libcpp_ctz(unsigned long __x) _NOEXCEPT { return __builtin_ctzl(__x); } 78 79inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 80int __libcpp_ctz(unsigned long long __x) _NOEXCEPT { return __builtin_ctzll(__x); } 81 82 83inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 84int __libcpp_clz(unsigned __x) _NOEXCEPT { return __builtin_clz(__x); } 85 86inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 87int __libcpp_clz(unsigned long __x) _NOEXCEPT { return __builtin_clzl(__x); } 88 89inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 90int __libcpp_clz(unsigned long long __x) _NOEXCEPT { return __builtin_clzll(__x); } 91 92 93inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 94int __libcpp_popcount(unsigned __x) _NOEXCEPT { return __builtin_popcount(__x); } 95 96inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 97int __libcpp_popcount(unsigned long __x) _NOEXCEPT { return __builtin_popcountl(__x); } 98 99inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 100int __libcpp_popcount(unsigned long long __x) _NOEXCEPT { return __builtin_popcountll(__x); } 101 102#else // _LIBCPP_COMPILER_MSVC 103 104// Precondition: __x != 0 105inline _LIBCPP_INLINE_VISIBILITY 106int __libcpp_ctz(unsigned __x) { 107 static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); 108 static_assert(sizeof(unsigned long) == 4, ""); 109 unsigned long __where; 110 if (_BitScanForward(&__where, __x)) 111 return static_cast<int>(__where); 112 return 32; 113} 114 115inline _LIBCPP_INLINE_VISIBILITY 116int __libcpp_ctz(unsigned long __x) { 117 static_assert(sizeof(unsigned long) == sizeof(unsigned), ""); 118 return __ctz(static_cast<unsigned>(__x)); 119} 120 121inline _LIBCPP_INLINE_VISIBILITY 122int __libcpp_ctz(unsigned long long __x) { 123 unsigned long __where; 124#if defined(_LIBCPP_HAS_BITSCAN64) 125 (defined(_M_AMD64) || defined(__x86_64__)) 126 if (_BitScanForward64(&__where, __x)) 127 return static_cast<int>(__where); 128#else 129 // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls. 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); 134#endif 135 return 64; 136} 137 138// Precondition: __x != 0 139inline _LIBCPP_INLINE_VISIBILITY 140int __libcpp_clz(unsigned __x) { 141 static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); 142 static_assert(sizeof(unsigned long) == 4, ""); 143 unsigned long __where; 144 if (_BitScanReverse(&__where, __x)) 145 return static_cast<int>(31 - __where); 146 return 32; // Undefined Behavior. 147} 148 149inline _LIBCPP_INLINE_VISIBILITY 150int __libcpp_clz(unsigned long __x) { 151 static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); 152 return __libcpp_clz(static_cast<unsigned>(__x)); 153} 154 155inline _LIBCPP_INLINE_VISIBILITY 156int __libcpp_clz(unsigned long long __x) { 157 unsigned long __where; 158#if defined(_LIBCPP_HAS_BITSCAN64) 159 if (_BitScanReverse64(&__where, __x)) 160 return static_cast<int>(63 - __where); 161#else 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); 167#endif 168 return 64; // Undefined Behavior. 169} 170 171inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned __x) { 172 static_assert(sizeof(unsigned) == 4, ""); 173 return __popcnt(__x); 174} 175 176inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long __x) { 177 static_assert(sizeof(unsigned long) == 4, ""); 178 return __popcnt(__x); 179} 180 181inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long long __x) { 182 static_assert(sizeof(unsigned long long) == 8, ""); 183 return __popcnt64(__x); 184} 185 186#endif // _LIBCPP_COMPILER_MSVC 187 188template <class _Tp> 189using __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool, 190 is_integral<_Tp>::value && 191 is_unsigned<_Tp>::value && 192 _IsNotSame<typename remove_cv<_Tp>::type, bool>::value && 193 _IsNotSame<typename remove_cv<_Tp>::type, signed char>::value && 194 _IsNotSame<typename remove_cv<_Tp>::type, wchar_t>::value && 195 _IsNotSame<typename remove_cv<_Tp>::type, char16_t>::value && 196 _IsNotSame<typename remove_cv<_Tp>::type, char32_t>::value 197 >; 198 199 200template<class _Tp> 201_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 202_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT 203{ 204 static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotl requires unsigned"); 205 const unsigned int __dig = numeric_limits<_Tp>::digits; 206 if ((__cnt % __dig) == 0) 207 return __t; 208 return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig))); 209} 210 211 212template<class _Tp> 213_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 214_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT 215{ 216 static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotr requires unsigned"); 217 const unsigned int __dig = numeric_limits<_Tp>::digits; 218 if ((__cnt % __dig) == 0) 219 return __t; 220 return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig))); 221} 222 223 224 225template<class _Tp> 226_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 227int __countr_zero(_Tp __t) _NOEXCEPT 228{ 229 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_zero requires unsigned"); 230 if (__t == 0) 231 return numeric_limits<_Tp>::digits; 232 233 if (sizeof(_Tp) <= sizeof(unsigned int)) 234 return __libcpp_ctz(static_cast<unsigned int>(__t)); 235 else if (sizeof(_Tp) <= sizeof(unsigned long)) 236 return __libcpp_ctz(static_cast<unsigned long>(__t)); 237 else if (sizeof(_Tp) <= sizeof(unsigned long long)) 238 return __libcpp_ctz(static_cast<unsigned long long>(__t)); 239 else 240 { 241 int __ret = 0; 242 int __iter = 0; 243 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits; 244 while ((__iter = __libcpp_ctz(static_cast<unsigned long long>(__t))) == __ulldigits) 245 { 246 __ret += __iter; 247 __t >>= __ulldigits; 248 } 249 return __ret + __iter; 250 } 251} 252 253template<class _Tp> 254_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 255int __countl_zero(_Tp __t) _NOEXCEPT 256{ 257 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_zero requires unsigned"); 258 if (__t == 0) 259 return numeric_limits<_Tp>::digits; 260 261 if (sizeof(_Tp) <= sizeof(unsigned int)) 262 return __libcpp_clz(static_cast<unsigned int>(__t)) 263 - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits); 264 else if (sizeof(_Tp) <= sizeof(unsigned long)) 265 return __libcpp_clz(static_cast<unsigned long>(__t)) 266 - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits); 267 else if (sizeof(_Tp) <= sizeof(unsigned long long)) 268 return __libcpp_clz(static_cast<unsigned long long>(__t)) 269 - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits); 270 else 271 { 272 int __ret = 0; 273 int __iter = 0; 274 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits; 275 while (true) { 276 __t = __rotr(__t, __ulldigits); 277 if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits) 278 break; 279 __ret += __iter; 280 } 281 return __ret + __iter; 282 } 283} 284 285template<class _Tp> 286_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 287int __countl_one(_Tp __t) _NOEXCEPT 288{ 289 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_one requires unsigned"); 290 return __t != numeric_limits<_Tp>::max() 291 ? __countl_zero(static_cast<_Tp>(~__t)) 292 : numeric_limits<_Tp>::digits; 293} 294 295 296template<class _Tp> 297_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 298int __countr_one(_Tp __t) _NOEXCEPT 299{ 300 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_one requires unsigned"); 301 return __t != numeric_limits<_Tp>::max() 302 ? __countr_zero(static_cast<_Tp>(~__t)) 303 : numeric_limits<_Tp>::digits; 304} 305 306 307template<class _Tp> 308_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 309int 310__popcount(_Tp __t) _NOEXCEPT 311{ 312 static_assert(__bitop_unsigned_integer<_Tp>::value, "__libcpp_popcount requires unsigned"); 313 if (sizeof(_Tp) <= sizeof(unsigned int)) 314 return __libcpp_popcount(static_cast<unsigned int>(__t)); 315 else if (sizeof(_Tp) <= sizeof(unsigned long)) 316 return __libcpp_popcount(static_cast<unsigned long>(__t)); 317 else if (sizeof(_Tp) <= sizeof(unsigned long long)) 318 return __libcpp_popcount(static_cast<unsigned long long>(__t)); 319 else 320 { 321 int __ret = 0; 322 while (__t != 0) 323 { 324 __ret += __libcpp_popcount(static_cast<unsigned long long>(__t)); 325 __t >>= numeric_limits<unsigned long long>::digits; 326 } 327 return __ret; 328 } 329} 330 331 332// integral log base 2 333template<class _Tp> 334_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 335unsigned __bit_log2(_Tp __t) _NOEXCEPT 336{ 337 static_assert(__bitop_unsigned_integer<_Tp>::value, "__bit_log2 requires unsigned"); 338 return std::numeric_limits<_Tp>::digits - 1 - __countl_zero(__t); 339} 340 341template <class _Tp> 342_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 343bool __ispow2(_Tp __t) _NOEXCEPT 344{ 345 static_assert(__bitop_unsigned_integer<_Tp>::value, "__ispow2 requires unsigned"); 346 return __t != 0 && (((__t & (__t - 1)) == 0)); 347} 348 349 350#if _LIBCPP_STD_VER > 17 351 352template<class _Tp> 353_LIBCPP_INLINE_VISIBILITY constexpr 354enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> 355rotl(_Tp __t, unsigned int __cnt) noexcept 356{ 357 return __rotl(__t, __cnt); 358} 359 360 361// rotr 362template<class _Tp> 363_LIBCPP_INLINE_VISIBILITY constexpr 364enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> 365rotr(_Tp __t, unsigned int __cnt) noexcept 366{ 367 return __rotr(__t, __cnt); 368} 369 370 371template<class _Tp> 372_LIBCPP_INLINE_VISIBILITY constexpr 373enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> 374countl_zero(_Tp __t) noexcept 375{ 376 return __countl_zero(__t); 377} 378 379 380template<class _Tp> 381_LIBCPP_INLINE_VISIBILITY constexpr 382enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> 383countl_one(_Tp __t) noexcept 384{ 385 return __countl_one(__t); 386} 387 388 389template<class _Tp> 390_LIBCPP_INLINE_VISIBILITY constexpr 391enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> 392countr_zero(_Tp __t) noexcept 393{ 394 return __countr_zero(__t); 395} 396 397 398template<class _Tp> 399_LIBCPP_INLINE_VISIBILITY constexpr 400enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> 401countr_one(_Tp __t) noexcept 402{ 403 return __countr_one(__t); 404} 405 406 407template<class _Tp> 408_LIBCPP_INLINE_VISIBILITY constexpr 409enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> 410popcount(_Tp __t) noexcept 411{ 412 return __popcount(__t); 413} 414 415 416template <class _Tp> 417_LIBCPP_INLINE_VISIBILITY constexpr 418enable_if_t<__bitop_unsigned_integer<_Tp>::value, bool> 419ispow2(_Tp __t) noexcept 420{ 421 return __ispow2(__t); 422} 423 424template <class _Tp> 425_LIBCPP_INLINE_VISIBILITY constexpr 426enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> 427floor2(_Tp __t) noexcept 428{ 429 return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t); 430} 431 432template <class _Tp> 433_LIBCPP_INLINE_VISIBILITY constexpr 434enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> 435ceil2(_Tp __t) noexcept 436{ 437 if (__t < 2) return 1; 438 const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u)); 439 _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to ceil2"); 440 441 if constexpr (sizeof(_Tp) >= sizeof(unsigned)) 442 return _Tp{1} << __n; 443 else 444 { 445 const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits; 446 const unsigned __retVal = 1u << (__n + __extra); 447 return (_Tp) (__retVal >> __extra); 448 } 449} 450 451template <class _Tp> 452_LIBCPP_INLINE_VISIBILITY constexpr 453enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> 454log2p1(_Tp __t) noexcept 455{ 456 return __t == 0 ? 0 : __bit_log2(__t) + 1; 457} 458 459#endif // _LIBCPP_STD_VER > 17 460 461_LIBCPP_END_NAMESPACE_STD 462 463_LIBCPP_POP_MACROS 464 465#endif // _LIBCPP_BIT 466