bit 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // <bit> -*- C++ -*-
  2. // Copyright (C) 2018-2020 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. /// @cond undoc
  56. template<typename _Tp>
  57. constexpr _Tp
  58. __rotl(_Tp __x, int __s) noexcept
  59. {
  60. constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
  61. const int __r = __s % _Nd;
  62. if (__r == 0)
  63. return __x;
  64. else if (__r > 0)
  65. return (__x << __r) | (__x >> ((_Nd - __r) % _Nd));
  66. else
  67. return (__x >> -__r) | (__x << ((_Nd + __r) % _Nd)); // rotr(x, -r)
  68. }
  69. template<typename _Tp>
  70. constexpr _Tp
  71. __rotr(_Tp __x, int __s) noexcept
  72. {
  73. constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
  74. const int __r = __s % _Nd;
  75. if (__r == 0)
  76. return __x;
  77. else if (__r > 0)
  78. return (__x >> __r) | (__x << ((_Nd - __r) % _Nd));
  79. else
  80. return (__x << -__r) | (__x >> ((_Nd + __r) % _Nd)); // rotl(x, -r)
  81. }
  82. template<typename _Tp>
  83. constexpr int
  84. __countl_zero(_Tp __x) noexcept
  85. {
  86. using __gnu_cxx::__int_traits;
  87. constexpr auto _Nd = __int_traits<_Tp>::__digits;
  88. if (__x == 0)
  89. return _Nd;
  90. constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
  91. constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
  92. constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
  93. if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
  94. {
  95. constexpr int __diff = _Nd_u - _Nd;
  96. return __builtin_clz(__x) - __diff;
  97. }
  98. else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
  99. {
  100. constexpr int __diff = _Nd_ul - _Nd;
  101. return __builtin_clzl(__x) - __diff;
  102. }
  103. else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
  104. {
  105. constexpr int __diff = _Nd_ull - _Nd;
  106. return __builtin_clzll(__x) - __diff;
  107. }
  108. else // (_Nd > _Nd_ull)
  109. {
  110. static_assert(_Nd <= (2 * _Nd_ull),
  111. "Maximum supported integer size is 128-bit");
  112. unsigned long long __high = __x >> _Nd_ull;
  113. if (__high != 0)
  114. {
  115. constexpr int __diff = (2 * _Nd_ull) - _Nd;
  116. return __builtin_clzll(__high) - __diff;
  117. }
  118. constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
  119. unsigned long long __low = __x & __max_ull;
  120. return (_Nd - _Nd_ull) + __builtin_clzll(__low);
  121. }
  122. }
  123. template<typename _Tp>
  124. constexpr int
  125. __countl_one(_Tp __x) noexcept
  126. {
  127. return std::__countl_zero<_Tp>((_Tp)~__x);
  128. }
  129. template<typename _Tp>
  130. constexpr int
  131. __countr_zero(_Tp __x) noexcept
  132. {
  133. using __gnu_cxx::__int_traits;
  134. constexpr auto _Nd = __int_traits<_Tp>::__digits;
  135. if (__x == 0)
  136. return _Nd;
  137. constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
  138. constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
  139. constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
  140. if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
  141. return __builtin_ctz(__x);
  142. else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
  143. return __builtin_ctzl(__x);
  144. else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
  145. return __builtin_ctzll(__x);
  146. else // (_Nd > _Nd_ull)
  147. {
  148. static_assert(_Nd <= (2 * _Nd_ull),
  149. "Maximum supported integer size is 128-bit");
  150. constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
  151. unsigned long long __low = __x & __max_ull;
  152. if (__low != 0)
  153. return __builtin_ctzll(__low);
  154. unsigned long long __high = __x >> _Nd_ull;
  155. return __builtin_ctzll(__high) + _Nd_ull;
  156. }
  157. }
  158. template<typename _Tp>
  159. constexpr int
  160. __countr_one(_Tp __x) noexcept
  161. {
  162. return std::__countr_zero((_Tp)~__x);
  163. }
  164. template<typename _Tp>
  165. constexpr int
  166. __popcount(_Tp __x) noexcept
  167. {
  168. using __gnu_cxx::__int_traits;
  169. constexpr auto _Nd = __int_traits<_Tp>::__digits;
  170. constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
  171. constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
  172. constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
  173. if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
  174. return __builtin_popcount(__x);
  175. else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
  176. return __builtin_popcountl(__x);
  177. else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
  178. return __builtin_popcountll(__x);
  179. else // (_Nd > _Nd_ull)
  180. {
  181. static_assert(_Nd <= (2 * _Nd_ull),
  182. "Maximum supported integer size is 128-bit");
  183. constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
  184. unsigned long long __low = __x & __max_ull;
  185. unsigned long long __high = __x >> _Nd_ull;
  186. return __builtin_popcountll(__low) + __builtin_popcountll(__high);
  187. }
  188. }
  189. template<typename _Tp>
  190. constexpr bool
  191. __has_single_bit(_Tp __x) noexcept
  192. { return std::__popcount(__x) == 1; }
  193. template<typename _Tp>
  194. constexpr _Tp
  195. __bit_ceil(_Tp __x) noexcept
  196. {
  197. using __gnu_cxx::__int_traits;
  198. constexpr auto _Nd = __int_traits<_Tp>::__digits;
  199. if (__x == 0 || __x == 1)
  200. return 1;
  201. auto __shift_exponent = _Nd - std::__countl_zero((_Tp)(__x - 1u));
  202. // If the shift exponent equals _Nd then the correct result is not
  203. // representable as a value of _Tp, and so the result is undefined.
  204. // Want that undefined behaviour to be detected in constant expressions,
  205. // by UBSan, and by debug assertions.
  206. #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
  207. if (!__builtin_is_constant_evaluated())
  208. {
  209. __glibcxx_assert( __shift_exponent != __int_traits<_Tp>::__digits );
  210. }
  211. #endif
  212. using __promoted_type = decltype(__x << 1);
  213. if _GLIBCXX17_CONSTEXPR (!is_same<__promoted_type, _Tp>::value)
  214. {
  215. // If __x undergoes integral promotion then shifting by _Nd is
  216. // not undefined. In order to make the shift undefined, so that
  217. // it is diagnosed in constant expressions and by UBsan, we also
  218. // need to "promote" the shift exponent to be too large for the
  219. // promoted type.
  220. const int __extra_exp = sizeof(__promoted_type) / sizeof(_Tp) / 2;
  221. __shift_exponent |= (__shift_exponent & _Nd) << __extra_exp;
  222. }
  223. return (_Tp)1u << __shift_exponent;
  224. }
  225. template<typename _Tp>
  226. constexpr _Tp
  227. __bit_floor(_Tp __x) noexcept
  228. {
  229. constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
  230. if (__x == 0)
  231. return 0;
  232. return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
  233. }
  234. template<typename _Tp>
  235. constexpr _Tp
  236. __bit_width(_Tp __x) noexcept
  237. {
  238. constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
  239. return _Nd - std::__countl_zero(__x);
  240. }
  241. /// @endcond
  242. #if __cplusplus > 201703L
  243. #define __cpp_lib_bitops 201907L
  244. /// @cond undoc
  245. template<typename _Tp, typename _Up = _Tp>
  246. using _If_is_unsigned_integer
  247. = enable_if_t<__is_unsigned_integer<_Tp>::value, _Up>;
  248. /// @endcond
  249. // [bit.rot], rotating
  250. /// Rotate `x` to the left by `s` bits.
  251. template<typename _Tp>
  252. [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
  253. rotl(_Tp __x, int __s) noexcept
  254. { return std::__rotl(__x, __s); }
  255. /// Rotate `x` to the right by `s` bits.
  256. template<typename _Tp>
  257. [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
  258. rotr(_Tp __x, int __s) noexcept
  259. { return std::__rotr(__x, __s); }
  260. // [bit.count], counting
  261. /// The number of contiguous zero bits, starting from the highest bit.
  262. template<typename _Tp>
  263. constexpr _If_is_unsigned_integer<_Tp, int>
  264. countl_zero(_Tp __x) noexcept
  265. { return std::__countl_zero(__x); }
  266. /// The number of contiguous one bits, starting from the highest bit.
  267. template<typename _Tp>
  268. constexpr _If_is_unsigned_integer<_Tp, int>
  269. countl_one(_Tp __x) noexcept
  270. { return std::__countl_one(__x); }
  271. /// The number of contiguous zero bits, starting from the lowest bit.
  272. template<typename _Tp>
  273. constexpr _If_is_unsigned_integer<_Tp, int>
  274. countr_zero(_Tp __x) noexcept
  275. { return std::__countr_zero(__x); }
  276. /// The number of contiguous one bits, starting from the lowest bit.
  277. template<typename _Tp>
  278. constexpr _If_is_unsigned_integer<_Tp, int>
  279. countr_one(_Tp __x) noexcept
  280. { return std::__countr_one(__x); }
  281. /// The number of bits set in `x`.
  282. template<typename _Tp>
  283. constexpr _If_is_unsigned_integer<_Tp, int>
  284. popcount(_Tp __x) noexcept
  285. { return std::__popcount(__x); }
  286. // [bit.pow.two], integral powers of 2
  287. #define __cpp_lib_int_pow2 202002L
  288. /// True if `x` is a power of two, false otherwise.
  289. template<typename _Tp>
  290. constexpr _If_is_unsigned_integer<_Tp, bool>
  291. has_single_bit(_Tp __x) noexcept
  292. { return std::__has_single_bit(__x); }
  293. /// The smallest power-of-two not less than `x`.
  294. template<typename _Tp>
  295. constexpr _If_is_unsigned_integer<_Tp>
  296. bit_ceil(_Tp __x) noexcept
  297. { return std::__bit_ceil(__x); }
  298. /// The largest power-of-two not greater than `x`.
  299. template<typename _Tp>
  300. constexpr _If_is_unsigned_integer<_Tp>
  301. bit_floor(_Tp __x) noexcept
  302. { return std::__bit_floor(__x); }
  303. /// The smallest integer greater than the base-2 logarithm of `x`.
  304. template<typename _Tp>
  305. constexpr _If_is_unsigned_integer<_Tp>
  306. bit_width(_Tp __x) noexcept
  307. { return std::__bit_width(__x); }
  308. #define __cpp_lib_endian 201907L
  309. /// Byte order
  310. enum class endian
  311. {
  312. little = __ORDER_LITTLE_ENDIAN__,
  313. big = __ORDER_BIG_ENDIAN__,
  314. native = __BYTE_ORDER__
  315. };
  316. #endif // C++2a
  317. /// @}
  318. _GLIBCXX_END_NAMESPACE_VERSION
  319. } // namespace std
  320. #endif // C++14
  321. #endif // _GLIBCXX_BIT