You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

372 lines
11KB

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