bit 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // <bit> -*- C++ -*-
  2. // Copyright (C) 2018-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/bit
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_BIT
  24. #define _GLIBCXX_BIT 1
  25. #pragma GCC system_header
  26. #if __cplusplus >= 201402L
  27. #include <type_traits>
  28. #if _GLIBCXX_HOSTED
  29. # include <ext/numeric_traits.h>
  30. #else
  31. # include <limits>
  32. /// @cond undocumented
  33. namespace __gnu_cxx
  34. {
  35. template<typename _Tp>
  36. struct __int_traits
  37. {
  38. static constexpr int __digits = std::numeric_limits<_Tp>::digits;
  39. static constexpr _Tp __max = std::numeric_limits<_Tp>::max();
  40. };
  41. }
  42. /// @endcond
  43. #endif
  44. namespace std _GLIBCXX_VISIBILITY(default)
  45. {
  46. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  47. /**
  48. * @defgroup bit_manip Bit manipulation
  49. * @ingroup numerics
  50. *
  51. * Utilities for examining and manipulating individual bits.
  52. *
  53. * @{
  54. */
  55. #if __cplusplus > 201703l && __has_builtin(__builtin_bit_cast)
  56. #define __cpp_lib_bit_cast 201806L
  57. /// Create a value of type `To` from the bits of `from`.
  58. /**
  59. * @tparam _To A trivially-copyable type.
  60. * @param __from A trivially-copyable object of the same size as `_To`.
  61. * @return An object of type `_To`.
  62. * @since C++20
  63. */
  64. template<typename _To, typename _From>
  65. [[nodiscard]]
  66. constexpr _To
  67. bit_cast(const _From& __from) noexcept
  68. #ifdef __cpp_concepts
  69. requires (sizeof(_To) == sizeof(_From))
  70. && __is_trivially_copyable(_To) && __is_trivially_copyable(_From)
  71. #endif
  72. {
  73. return __builtin_bit_cast(_To, __from);
  74. }
  75. #endif
  76. #if __cplusplus > 202002L
  77. #define __cpp_lib_byteswap 202110L
  78. /// Reverse order of bytes in the object representation of `value`.
  79. /**
  80. * @tparam _Tp An integral type.
  81. * @param __value An object of integer type.
  82. * @return An object of the same type, with the bytes reversed.
  83. * @since C++23
  84. */
  85. template<typename _Tp>
  86. [[nodiscard]]
  87. constexpr enable_if_t<is_integral<_Tp>::value, _Tp>
  88. byteswap(_Tp __value) noexcept
  89. {
  90. if constexpr (sizeof(_Tp) == 1)
  91. return __value;
  92. #if __cpp_if_consteval >= 202106L && __CHAR_BIT__ == 8
  93. if !consteval
  94. {
  95. if constexpr (sizeof(_Tp) == 2)
  96. return __builtin_bswap16(__value);
  97. if constexpr (sizeof(_Tp) == 4)
  98. return __builtin_bswap32(__value);
  99. if constexpr (sizeof(_Tp) == 8)
  100. return __builtin_bswap64(__value);
  101. if constexpr (sizeof(_Tp) == 16)
  102. #if __has_builtin(__builtin_bswap128)
  103. return __builtin_bswap128(__value);
  104. #else
  105. return (__builtin_bswap64(__value >> 64)
  106. | (static_cast<_Tp>(__builtin_bswap64(__value)) << 64));
  107. #endif
  108. }
  109. #endif
  110. // Fallback implementation that handles even __int24 etc.
  111. using _Up = typename __make_unsigned<__remove_cv_t<_Tp>>::__type;
  112. size_t __diff = __CHAR_BIT__ * (sizeof(_Tp) - 1);
  113. _Up __mask1 = static_cast<unsigned char>(~0);
  114. _Up __mask2 = __mask1 << __diff;
  115. _Up __val = __value;
  116. for (size_t __i = 0; __i < sizeof(_Tp) / 2; ++__i)
  117. {
  118. _Up __byte1 = __val & __mask1;
  119. _Up __byte2 = __val & __mask2;
  120. __val = (__val ^ __byte1 ^ __byte2
  121. ^ (__byte1 << __diff) ^ (__byte2 >> __diff));
  122. __mask1 <<= __CHAR_BIT__;
  123. __mask2 >>= __CHAR_BIT__;
  124. __diff -= 2 * __CHAR_BIT__;
  125. }
  126. return __val;
  127. }
  128. #endif
  129. /// @cond undoc
  130. template<typename _Tp>
  131. constexpr _Tp
  132. __rotl(_Tp __x, int __s) noexcept
  133. {
  134. constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
  135. if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
  136. {
  137. // Variant for power of two _Nd which the compiler can
  138. // easily pattern match.
  139. constexpr unsigned __uNd = _Nd;
  140. const unsigned __r = __s;
  141. return (__x << (__r % __uNd)) | (__x >> ((-__r) % __uNd));
  142. }
  143. const int __r = __s % _Nd;
  144. if (__r == 0)
  145. return __x;
  146. else if (__r > 0)
  147. return (__x << __r) | (__x >> ((_Nd - __r) % _Nd));
  148. else
  149. return (__x >> -__r) | (__x << ((_Nd + __r) % _Nd)); // rotr(x, -r)
  150. }
  151. template<typename _Tp>
  152. constexpr _Tp
  153. __rotr(_Tp __x, int __s) noexcept
  154. {
  155. constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
  156. if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
  157. {
  158. // Variant for power of two _Nd which the compiler can
  159. // easily pattern match.
  160. constexpr unsigned __uNd = _Nd;
  161. const unsigned __r = __s;
  162. return (__x >> (__r % __uNd)) | (__x << ((-__r) % __uNd));
  163. }
  164. const int __r = __s % _Nd;
  165. if (__r == 0)
  166. return __x;
  167. else if (__r > 0)
  168. return (__x >> __r) | (__x << ((_Nd - __r) % _Nd));
  169. else
  170. return (__x << -__r) | (__x >> ((_Nd + __r) % _Nd)); // rotl(x, -r)
  171. }
  172. template<typename _Tp>
  173. constexpr int
  174. __countl_zero(_Tp __x) noexcept
  175. {
  176. using __gnu_cxx::__int_traits;
  177. constexpr auto _Nd = __int_traits<_Tp>::__digits;
  178. if (__x == 0)
  179. return _Nd;
  180. constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
  181. constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
  182. constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
  183. if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
  184. {
  185. constexpr int __diff = _Nd_u - _Nd;
  186. return __builtin_clz(__x) - __diff;
  187. }
  188. else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
  189. {
  190. constexpr int __diff = _Nd_ul - _Nd;
  191. return __builtin_clzl(__x) - __diff;
  192. }
  193. else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
  194. {
  195. constexpr int __diff = _Nd_ull - _Nd;
  196. return __builtin_clzll(__x) - __diff;
  197. }
  198. else // (_Nd > _Nd_ull)
  199. {
  200. static_assert(_Nd <= (2 * _Nd_ull),
  201. "Maximum supported integer size is 128-bit");
  202. unsigned long long __high = __x >> _Nd_ull;
  203. if (__high != 0)
  204. {
  205. constexpr int __diff = (2 * _Nd_ull) - _Nd;
  206. return __builtin_clzll(__high) - __diff;
  207. }
  208. constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
  209. unsigned long long __low = __x & __max_ull;
  210. return (_Nd - _Nd_ull) + __builtin_clzll(__low);
  211. }
  212. }
  213. template<typename _Tp>
  214. constexpr int
  215. __countl_one(_Tp __x) noexcept
  216. {
  217. return std::__countl_zero<_Tp>((_Tp)~__x);
  218. }
  219. template<typename _Tp>
  220. constexpr int
  221. __countr_zero(_Tp __x) noexcept
  222. {
  223. using __gnu_cxx::__int_traits;
  224. constexpr auto _Nd = __int_traits<_Tp>::__digits;
  225. if (__x == 0)
  226. return _Nd;
  227. constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
  228. constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
  229. constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
  230. if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
  231. return __builtin_ctz(__x);
  232. else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
  233. return __builtin_ctzl(__x);
  234. else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
  235. return __builtin_ctzll(__x);
  236. else // (_Nd > _Nd_ull)
  237. {
  238. static_assert(_Nd <= (2 * _Nd_ull),
  239. "Maximum supported integer size is 128-bit");
  240. constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
  241. unsigned long long __low = __x & __max_ull;
  242. if (__low != 0)
  243. return __builtin_ctzll(__low);
  244. unsigned long long __high = __x >> _Nd_ull;
  245. return __builtin_ctzll(__high) + _Nd_ull;
  246. }
  247. }
  248. template<typename _Tp>
  249. constexpr int
  250. __countr_one(_Tp __x) noexcept
  251. {
  252. return std::__countr_zero((_Tp)~__x);
  253. }
  254. template<typename _Tp>
  255. constexpr int
  256. __popcount(_Tp __x) noexcept
  257. {
  258. using __gnu_cxx::__int_traits;
  259. constexpr auto _Nd = __int_traits<_Tp>::__digits;
  260. constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
  261. constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
  262. constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
  263. if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
  264. return __builtin_popcount(__x);
  265. else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
  266. return __builtin_popcountl(__x);
  267. else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
  268. return __builtin_popcountll(__x);
  269. else // (_Nd > _Nd_ull)
  270. {
  271. static_assert(_Nd <= (2 * _Nd_ull),
  272. "Maximum supported integer size is 128-bit");
  273. constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
  274. unsigned long long __low = __x & __max_ull;
  275. unsigned long long __high = __x >> _Nd_ull;
  276. return __builtin_popcountll(__low) + __builtin_popcountll(__high);
  277. }
  278. }
  279. template<typename _Tp>
  280. constexpr bool
  281. __has_single_bit(_Tp __x) noexcept
  282. { return std::__popcount(__x) == 1; }
  283. template<typename _Tp>
  284. constexpr _Tp
  285. __bit_ceil(_Tp __x) noexcept
  286. {
  287. using __gnu_cxx::__int_traits;
  288. constexpr auto _Nd = __int_traits<_Tp>::__digits;
  289. if (__x == 0 || __x == 1)
  290. return 1;
  291. auto __shift_exponent = _Nd - std::__countl_zero((_Tp)(__x - 1u));
  292. // If the shift exponent equals _Nd then the correct result is not
  293. // representable as a value of _Tp, and so the result is undefined.
  294. // Want that undefined behaviour to be detected in constant expressions,
  295. // by UBSan, and by debug assertions.
  296. if (!std::__is_constant_evaluated())
  297. {
  298. __glibcxx_assert( __shift_exponent != __int_traits<_Tp>::__digits );
  299. }
  300. using __promoted_type = decltype(__x << 1);
  301. if _GLIBCXX17_CONSTEXPR (!is_same<__promoted_type, _Tp>::value)
  302. {
  303. // If __x undergoes integral promotion then shifting by _Nd is
  304. // not undefined. In order to make the shift undefined, so that
  305. // it is diagnosed in constant expressions and by UBsan, we also
  306. // need to "promote" the shift exponent to be too large for the
  307. // promoted type.
  308. const int __extra_exp = sizeof(__promoted_type) / sizeof(_Tp) / 2;
  309. __shift_exponent |= (__shift_exponent & _Nd) << __extra_exp;
  310. }
  311. return (_Tp)1u << __shift_exponent;
  312. }
  313. template<typename _Tp>
  314. constexpr _Tp
  315. __bit_floor(_Tp __x) noexcept
  316. {
  317. constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
  318. if (__x == 0)
  319. return 0;
  320. return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
  321. }
  322. template<typename _Tp>
  323. constexpr _Tp
  324. __bit_width(_Tp __x) noexcept
  325. {
  326. constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
  327. return _Nd - std::__countl_zero(__x);
  328. }
  329. /// @endcond
  330. #if __cplusplus > 201703L
  331. #define __cpp_lib_bitops 201907L
  332. /// @cond undoc
  333. template<typename _Tp, typename _Up = _Tp>
  334. using _If_is_unsigned_integer
  335. = enable_if_t<__is_unsigned_integer<_Tp>::value, _Up>;
  336. /// @endcond
  337. // [bit.rot], rotating
  338. /// Rotate `x` to the left by `s` bits.
  339. template<typename _Tp>
  340. [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
  341. rotl(_Tp __x, int __s) noexcept
  342. { return std::__rotl(__x, __s); }
  343. /// Rotate `x` to the right by `s` bits.
  344. template<typename _Tp>
  345. [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
  346. rotr(_Tp __x, int __s) noexcept
  347. { return std::__rotr(__x, __s); }
  348. // [bit.count], counting
  349. /// The number of contiguous zero bits, starting from the highest bit.
  350. template<typename _Tp>
  351. constexpr _If_is_unsigned_integer<_Tp, int>
  352. countl_zero(_Tp __x) noexcept
  353. { return std::__countl_zero(__x); }
  354. /// The number of contiguous one bits, starting from the highest bit.
  355. template<typename _Tp>
  356. constexpr _If_is_unsigned_integer<_Tp, int>
  357. countl_one(_Tp __x) noexcept
  358. { return std::__countl_one(__x); }
  359. /// The number of contiguous zero bits, starting from the lowest bit.
  360. template<typename _Tp>
  361. constexpr _If_is_unsigned_integer<_Tp, int>
  362. countr_zero(_Tp __x) noexcept
  363. { return std::__countr_zero(__x); }
  364. /// The number of contiguous one bits, starting from the lowest bit.
  365. template<typename _Tp>
  366. constexpr _If_is_unsigned_integer<_Tp, int>
  367. countr_one(_Tp __x) noexcept
  368. { return std::__countr_one(__x); }
  369. /// The number of bits set in `x`.
  370. template<typename _Tp>
  371. constexpr _If_is_unsigned_integer<_Tp, int>
  372. popcount(_Tp __x) noexcept
  373. { return std::__popcount(__x); }
  374. // [bit.pow.two], integral powers of 2
  375. #define __cpp_lib_int_pow2 202002L
  376. /// True if `x` is a power of two, false otherwise.
  377. template<typename _Tp>
  378. constexpr _If_is_unsigned_integer<_Tp, bool>
  379. has_single_bit(_Tp __x) noexcept
  380. { return std::__has_single_bit(__x); }
  381. /// The smallest power-of-two not less than `x`.
  382. template<typename _Tp>
  383. constexpr _If_is_unsigned_integer<_Tp>
  384. bit_ceil(_Tp __x) noexcept
  385. { return std::__bit_ceil(__x); }
  386. /// The largest power-of-two not greater than `x`.
  387. template<typename _Tp>
  388. constexpr _If_is_unsigned_integer<_Tp>
  389. bit_floor(_Tp __x) noexcept
  390. { return std::__bit_floor(__x); }
  391. /// The smallest integer greater than the base-2 logarithm of `x`.
  392. template<typename _Tp>
  393. constexpr _If_is_unsigned_integer<_Tp>
  394. bit_width(_Tp __x) noexcept
  395. { return std::__bit_width(__x); }
  396. #define __cpp_lib_endian 201907L
  397. /// Byte order constants
  398. /**
  399. * The platform endianness can be checked by comparing `std::endian::native`
  400. * to one of `std::endian::big` or `std::endian::little`.
  401. *
  402. * @since C++20
  403. */
  404. enum class endian
  405. {
  406. little = __ORDER_LITTLE_ENDIAN__,
  407. big = __ORDER_BIG_ENDIAN__,
  408. native = __BYTE_ORDER__
  409. };
  410. #endif // C++2a
  411. /// @}
  412. _GLIBCXX_END_NAMESPACE_VERSION
  413. } // namespace std
  414. #endif // C++14
  415. #endif // _GLIBCXX_BIT