bit 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  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. // [bit.cast], bit_cast
  15. template<class To, class From>
  16. constexpr To bit_cast(const From& from) noexcept; // C++20
  17. // [bit.byteswap], byteswap
  18. template<class T>
  19. constexpr T byteswap(T value) noexcept; // C++23
  20. // [bit.pow.two], integral powers of 2
  21. template <class T>
  22. constexpr bool has_single_bit(T x) noexcept; // C++20
  23. template <class T>
  24. constexpr T bit_ceil(T x); // C++20
  25. template <class T>
  26. constexpr T bit_floor(T x) noexcept; // C++20
  27. template <class T>
  28. constexpr T bit_width(T x) noexcept; // C++20
  29. // [bit.rotate], rotating
  30. template<class T>
  31. constexpr T rotl(T x, unsigned int s) noexcept; // C++20
  32. template<class T>
  33. constexpr T rotr(T x, unsigned int s) noexcept; // C++20
  34. // [bit.count], counting
  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. // [bit.endian], endian
  46. enum class endian {
  47. little = see below, // C++20
  48. big = see below, // C++20
  49. native = see below // C++20
  50. };
  51. } // namespace std
  52. */
  53. #include <__assert>
  54. #include <__bit/bit_cast.h>
  55. #include <__bit/byteswap.h>
  56. #include <__bits> // __libcpp_clz
  57. #include <__config>
  58. #include <limits>
  59. #include <type_traits>
  60. #include <version>
  61. #if defined(__IBMCPP__)
  62. #include "__support/ibm/support.h"
  63. #endif
  64. #if defined(_LIBCPP_COMPILER_MSVC)
  65. #include <intrin.h>
  66. #endif
  67. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  68. # pragma GCC system_header
  69. #endif
  70. _LIBCPP_PUSH_MACROS
  71. #include <__undef_macros>
  72. _LIBCPP_BEGIN_NAMESPACE_STD
  73. template<class _Tp>
  74. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  75. _Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
  76. {
  77. static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type");
  78. const unsigned int __dig = numeric_limits<_Tp>::digits;
  79. if ((__cnt % __dig) == 0)
  80. return __t;
  81. return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
  82. }
  83. template<class _Tp>
  84. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  85. _Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
  86. {
  87. static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
  88. const unsigned int __dig = numeric_limits<_Tp>::digits;
  89. if ((__cnt % __dig) == 0)
  90. return __t;
  91. return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
  92. }
  93. template<class _Tp>
  94. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  95. int __countr_zero(_Tp __t) _NOEXCEPT
  96. {
  97. static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_zero requires an unsigned integer type");
  98. if (__t == 0)
  99. return numeric_limits<_Tp>::digits;
  100. if (sizeof(_Tp) <= sizeof(unsigned int))
  101. return __libcpp_ctz(static_cast<unsigned int>(__t));
  102. else if (sizeof(_Tp) <= sizeof(unsigned long))
  103. return __libcpp_ctz(static_cast<unsigned long>(__t));
  104. else if (sizeof(_Tp) <= sizeof(unsigned long long))
  105. return __libcpp_ctz(static_cast<unsigned long long>(__t));
  106. else
  107. {
  108. int __ret = 0;
  109. const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
  110. while (static_cast<unsigned long long>(__t) == 0uLL)
  111. {
  112. __ret += __ulldigits;
  113. __t >>= __ulldigits;
  114. }
  115. return __ret + __libcpp_ctz(static_cast<unsigned long long>(__t));
  116. }
  117. }
  118. template<class _Tp>
  119. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  120. int __countl_zero(_Tp __t) _NOEXCEPT
  121. {
  122. static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
  123. if (__t == 0)
  124. return numeric_limits<_Tp>::digits;
  125. if (sizeof(_Tp) <= sizeof(unsigned int))
  126. return __libcpp_clz(static_cast<unsigned int>(__t))
  127. - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
  128. else if (sizeof(_Tp) <= sizeof(unsigned long))
  129. return __libcpp_clz(static_cast<unsigned long>(__t))
  130. - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
  131. else if (sizeof(_Tp) <= sizeof(unsigned long long))
  132. return __libcpp_clz(static_cast<unsigned long long>(__t))
  133. - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
  134. else
  135. {
  136. int __ret = 0;
  137. int __iter = 0;
  138. const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
  139. while (true) {
  140. __t = __rotr(__t, __ulldigits);
  141. if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
  142. break;
  143. __ret += __iter;
  144. }
  145. return __ret + __iter;
  146. }
  147. }
  148. template<class _Tp>
  149. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  150. int __countl_one(_Tp __t) _NOEXCEPT
  151. {
  152. static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_one requires an unsigned integer type");
  153. return __t != numeric_limits<_Tp>::max()
  154. ? __countl_zero(static_cast<_Tp>(~__t))
  155. : numeric_limits<_Tp>::digits;
  156. }
  157. template<class _Tp>
  158. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  159. int __countr_one(_Tp __t) _NOEXCEPT
  160. {
  161. static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_one requires an unsigned integer type");
  162. return __t != numeric_limits<_Tp>::max()
  163. ? __countr_zero(static_cast<_Tp>(~__t))
  164. : numeric_limits<_Tp>::digits;
  165. }
  166. template<class _Tp>
  167. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  168. int __popcount(_Tp __t) _NOEXCEPT
  169. {
  170. static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__popcount requires an unsigned integer type");
  171. if (sizeof(_Tp) <= sizeof(unsigned int))
  172. return __libcpp_popcount(static_cast<unsigned int>(__t));
  173. else if (sizeof(_Tp) <= sizeof(unsigned long))
  174. return __libcpp_popcount(static_cast<unsigned long>(__t));
  175. else if (sizeof(_Tp) <= sizeof(unsigned long long))
  176. return __libcpp_popcount(static_cast<unsigned long long>(__t));
  177. else
  178. {
  179. int __ret = 0;
  180. while (__t != 0)
  181. {
  182. __ret += __libcpp_popcount(static_cast<unsigned long long>(__t));
  183. __t >>= numeric_limits<unsigned long long>::digits;
  184. }
  185. return __ret;
  186. }
  187. }
  188. // integral log base 2
  189. template<class _Tp>
  190. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  191. unsigned __bit_log2(_Tp __t) _NOEXCEPT
  192. {
  193. static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__bit_log2 requires an unsigned integer type");
  194. return numeric_limits<_Tp>::digits - 1 - __countl_zero(__t);
  195. }
  196. template <class _Tp>
  197. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  198. bool __has_single_bit(_Tp __t) _NOEXCEPT
  199. {
  200. static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__has_single_bit requires an unsigned integer type");
  201. return __t != 0 && (((__t & (__t - 1)) == 0));
  202. }
  203. #if _LIBCPP_STD_VER > 17
  204. template<class _Tp>
  205. _LIBCPP_INLINE_VISIBILITY constexpr
  206. enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
  207. rotl(_Tp __t, unsigned int __cnt) noexcept
  208. {
  209. return __rotl(__t, __cnt);
  210. }
  211. template<class _Tp>
  212. _LIBCPP_INLINE_VISIBILITY constexpr
  213. enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
  214. rotr(_Tp __t, unsigned int __cnt) noexcept
  215. {
  216. return __rotr(__t, __cnt);
  217. }
  218. template<class _Tp>
  219. _LIBCPP_INLINE_VISIBILITY constexpr
  220. enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
  221. countl_zero(_Tp __t) noexcept
  222. {
  223. return __countl_zero(__t);
  224. }
  225. template<class _Tp>
  226. _LIBCPP_INLINE_VISIBILITY constexpr
  227. enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
  228. countl_one(_Tp __t) noexcept
  229. {
  230. return __countl_one(__t);
  231. }
  232. template<class _Tp>
  233. _LIBCPP_INLINE_VISIBILITY constexpr
  234. enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
  235. countr_zero(_Tp __t) noexcept
  236. {
  237. return __countr_zero(__t);
  238. }
  239. template<class _Tp>
  240. _LIBCPP_INLINE_VISIBILITY constexpr
  241. enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
  242. countr_one(_Tp __t) noexcept
  243. {
  244. return __countr_one(__t);
  245. }
  246. template<class _Tp>
  247. _LIBCPP_INLINE_VISIBILITY constexpr
  248. enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
  249. popcount(_Tp __t) noexcept
  250. {
  251. return __popcount(__t);
  252. }
  253. template <class _Tp>
  254. _LIBCPP_INLINE_VISIBILITY constexpr
  255. enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, bool>
  256. has_single_bit(_Tp __t) noexcept
  257. {
  258. return __has_single_bit(__t);
  259. }
  260. template <class _Tp>
  261. _LIBCPP_INLINE_VISIBILITY constexpr
  262. enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
  263. bit_floor(_Tp __t) noexcept
  264. {
  265. return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
  266. }
  267. template <class _Tp>
  268. _LIBCPP_INLINE_VISIBILITY constexpr
  269. enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
  270. bit_ceil(_Tp __t) noexcept
  271. {
  272. if (__t < 2) return 1;
  273. const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
  274. _LIBCPP_ASSERT(__n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
  275. if constexpr (sizeof(_Tp) >= sizeof(unsigned))
  276. return _Tp{1} << __n;
  277. else
  278. {
  279. const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits;
  280. const unsigned __retVal = 1u << (__n + __extra);
  281. return (_Tp) (__retVal >> __extra);
  282. }
  283. }
  284. template <class _Tp>
  285. _LIBCPP_INLINE_VISIBILITY constexpr
  286. enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
  287. bit_width(_Tp __t) noexcept
  288. {
  289. return __t == 0 ? 0 : __bit_log2(__t) + 1;
  290. }
  291. enum class endian
  292. {
  293. little = 0xDEAD,
  294. big = 0xFACE,
  295. #if defined(_LIBCPP_LITTLE_ENDIAN)
  296. native = little
  297. #elif defined(_LIBCPP_BIG_ENDIAN)
  298. native = big
  299. #else
  300. native = 0xCAFE
  301. #endif
  302. };
  303. #endif // _LIBCPP_STD_VER > 17
  304. _LIBCPP_END_NAMESPACE_STD
  305. _LIBCPP_POP_MACROS
  306. #endif // _LIBCPP_BIT