bit 10.0 KB

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