bit 14 KB

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