bit.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/ADT/bit.h - C++20 <bit> ----------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file implements the C++20 <bit> header.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ADT_BIT_H
  19. #define LLVM_ADT_BIT_H
  20. #include "llvm/Support/Compiler.h"
  21. #include <cstdint>
  22. #include <limits>
  23. #include <type_traits>
  24. #if !__has_builtin(__builtin_bit_cast)
  25. #include <cstring>
  26. #endif
  27. #if defined(_MSC_VER) && !defined(_DEBUG)
  28. #include <cstdlib> // for _byteswap_{ushort,ulong,uint64}
  29. #endif
  30. #ifdef _MSC_VER
  31. // Declare these intrinsics manually rather including intrin.h. It's very
  32. // expensive, and bit.h is popular via MathExtras.h.
  33. // #include <intrin.h>
  34. extern "C" {
  35. unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
  36. unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
  37. unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
  38. unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
  39. }
  40. #endif
  41. namespace llvm {
  42. // This implementation of bit_cast is different from the C++20 one in two ways:
  43. // - It isn't constexpr because that requires compiler support.
  44. // - It requires trivially-constructible To, to avoid UB in the implementation.
  45. template <
  46. typename To, typename From,
  47. typename = std::enable_if_t<sizeof(To) == sizeof(From)>,
  48. typename = std::enable_if_t<std::is_trivially_constructible<To>::value>,
  49. typename = std::enable_if_t<std::is_trivially_copyable<To>::value>,
  50. typename = std::enable_if_t<std::is_trivially_copyable<From>::value>>
  51. [[nodiscard]] inline To bit_cast(const From &from) noexcept {
  52. #if __has_builtin(__builtin_bit_cast)
  53. return __builtin_bit_cast(To, from);
  54. #else
  55. To to;
  56. std::memcpy(&to, &from, sizeof(To));
  57. return to;
  58. #endif
  59. }
  60. /// Reverses the bytes in the given integer value V.
  61. template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
  62. [[nodiscard]] constexpr T byteswap(T V) noexcept {
  63. if constexpr (sizeof(T) == 1) {
  64. return V;
  65. } else if constexpr (sizeof(T) == 2) {
  66. uint16_t UV = V;
  67. #if defined(_MSC_VER) && !defined(_DEBUG)
  68. // The DLL version of the runtime lacks these functions (bug!?), but in a
  69. // release build they're replaced with BSWAP instructions anyway.
  70. return _byteswap_ushort(UV);
  71. #else
  72. uint16_t Hi = UV << 8;
  73. uint16_t Lo = UV >> 8;
  74. return Hi | Lo;
  75. #endif
  76. } else if constexpr (sizeof(T) == 4) {
  77. uint32_t UV = V;
  78. #if __has_builtin(__builtin_bswap32)
  79. return __builtin_bswap32(UV);
  80. #elif defined(_MSC_VER) && !defined(_DEBUG)
  81. return _byteswap_ulong(UV);
  82. #else
  83. uint32_t Byte0 = UV & 0x000000FF;
  84. uint32_t Byte1 = UV & 0x0000FF00;
  85. uint32_t Byte2 = UV & 0x00FF0000;
  86. uint32_t Byte3 = UV & 0xFF000000;
  87. return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
  88. #endif
  89. } else if constexpr (sizeof(T) == 8) {
  90. uint64_t UV = V;
  91. #if __has_builtin(__builtin_bswap64)
  92. return __builtin_bswap64(UV);
  93. #elif defined(_MSC_VER) && !defined(_DEBUG)
  94. return _byteswap_uint64(UV);
  95. #else
  96. uint64_t Hi = llvm::byteswap<uint32_t>(UV);
  97. uint32_t Lo = llvm::byteswap<uint32_t>(UV >> 32);
  98. return (Hi << 32) | Lo;
  99. #endif
  100. } else {
  101. static_assert(!sizeof(T *), "Don't know how to handle the given type.");
  102. return 0;
  103. }
  104. }
  105. template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
  106. [[nodiscard]] constexpr inline bool has_single_bit(T Value) noexcept {
  107. return (Value != 0) && ((Value & (Value - 1)) == 0);
  108. }
  109. namespace detail {
  110. template <typename T, std::size_t SizeOfT> struct TrailingZerosCounter {
  111. static unsigned count(T Val) {
  112. if (!Val)
  113. return std::numeric_limits<T>::digits;
  114. if (Val & 0x1)
  115. return 0;
  116. // Bisection method.
  117. unsigned ZeroBits = 0;
  118. T Shift = std::numeric_limits<T>::digits >> 1;
  119. T Mask = std::numeric_limits<T>::max() >> Shift;
  120. while (Shift) {
  121. if ((Val & Mask) == 0) {
  122. Val >>= Shift;
  123. ZeroBits |= Shift;
  124. }
  125. Shift >>= 1;
  126. Mask >>= Shift;
  127. }
  128. return ZeroBits;
  129. }
  130. };
  131. #if defined(__GNUC__) || defined(_MSC_VER)
  132. template <typename T> struct TrailingZerosCounter<T, 4> {
  133. static unsigned count(T Val) {
  134. if (Val == 0)
  135. return 32;
  136. #if __has_builtin(__builtin_ctz) || defined(__GNUC__)
  137. return __builtin_ctz(Val);
  138. #elif defined(_MSC_VER)
  139. unsigned long Index;
  140. _BitScanForward(&Index, Val);
  141. return Index;
  142. #endif
  143. }
  144. };
  145. #if !defined(_MSC_VER) || defined(_M_X64)
  146. template <typename T> struct TrailingZerosCounter<T, 8> {
  147. static unsigned count(T Val) {
  148. if (Val == 0)
  149. return 64;
  150. #if __has_builtin(__builtin_ctzll) || defined(__GNUC__)
  151. return __builtin_ctzll(Val);
  152. #elif defined(_MSC_VER)
  153. unsigned long Index;
  154. _BitScanForward64(&Index, Val);
  155. return Index;
  156. #endif
  157. }
  158. };
  159. #endif
  160. #endif
  161. } // namespace detail
  162. /// Count number of 0's from the least significant bit to the most
  163. /// stopping at the first 1.
  164. ///
  165. /// Only unsigned integral types are allowed.
  166. ///
  167. /// Returns std::numeric_limits<T>::digits on an input of 0.
  168. template <typename T> [[nodiscard]] int countr_zero(T Val) {
  169. static_assert(std::is_unsigned_v<T>,
  170. "Only unsigned integral types are allowed.");
  171. return llvm::detail::TrailingZerosCounter<T, sizeof(T)>::count(Val);
  172. }
  173. namespace detail {
  174. template <typename T, std::size_t SizeOfT> struct LeadingZerosCounter {
  175. static unsigned count(T Val) {
  176. if (!Val)
  177. return std::numeric_limits<T>::digits;
  178. // Bisection method.
  179. unsigned ZeroBits = 0;
  180. for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
  181. T Tmp = Val >> Shift;
  182. if (Tmp)
  183. Val = Tmp;
  184. else
  185. ZeroBits |= Shift;
  186. }
  187. return ZeroBits;
  188. }
  189. };
  190. #if defined(__GNUC__) || defined(_MSC_VER)
  191. template <typename T> struct LeadingZerosCounter<T, 4> {
  192. static unsigned count(T Val) {
  193. if (Val == 0)
  194. return 32;
  195. #if __has_builtin(__builtin_clz) || defined(__GNUC__)
  196. return __builtin_clz(Val);
  197. #elif defined(_MSC_VER)
  198. unsigned long Index;
  199. _BitScanReverse(&Index, Val);
  200. return Index ^ 31;
  201. #endif
  202. }
  203. };
  204. #if !defined(_MSC_VER) || defined(_M_X64)
  205. template <typename T> struct LeadingZerosCounter<T, 8> {
  206. static unsigned count(T Val) {
  207. if (Val == 0)
  208. return 64;
  209. #if __has_builtin(__builtin_clzll) || defined(__GNUC__)
  210. return __builtin_clzll(Val);
  211. #elif defined(_MSC_VER)
  212. unsigned long Index;
  213. _BitScanReverse64(&Index, Val);
  214. return Index ^ 63;
  215. #endif
  216. }
  217. };
  218. #endif
  219. #endif
  220. } // namespace detail
  221. /// Count number of 0's from the most significant bit to the least
  222. /// stopping at the first 1.
  223. ///
  224. /// Only unsigned integral types are allowed.
  225. ///
  226. /// Returns std::numeric_limits<T>::digits on an input of 0.
  227. template <typename T> [[nodiscard]] int countl_zero(T Val) {
  228. static_assert(std::is_unsigned_v<T>,
  229. "Only unsigned integral types are allowed.");
  230. return llvm::detail::LeadingZerosCounter<T, sizeof(T)>::count(Val);
  231. }
  232. /// Count the number of ones from the most significant bit to the first
  233. /// zero bit.
  234. ///
  235. /// Ex. countl_one(0xFF0FFF00) == 8.
  236. /// Only unsigned integral types are allowed.
  237. ///
  238. /// Returns std::numeric_limits<T>::digits on an input of all ones.
  239. template <typename T> [[nodiscard]] int countl_one(T Value) {
  240. static_assert(std::is_unsigned_v<T>,
  241. "Only unsigned integral types are allowed.");
  242. return llvm::countl_zero<T>(~Value);
  243. }
  244. /// Count the number of ones from the least significant bit to the first
  245. /// zero bit.
  246. ///
  247. /// Ex. countr_one(0x00FF00FF) == 8.
  248. /// Only unsigned integral types are allowed.
  249. ///
  250. /// Returns std::numeric_limits<T>::digits on an input of all ones.
  251. template <typename T> [[nodiscard]] int countr_one(T Value) {
  252. static_assert(std::is_unsigned_v<T>,
  253. "Only unsigned integral types are allowed.");
  254. return llvm::countr_zero<T>(~Value);
  255. }
  256. /// Returns the number of bits needed to represent Value if Value is nonzero.
  257. /// Returns 0 otherwise.
  258. ///
  259. /// Ex. bit_width(5) == 3.
  260. template <typename T> [[nodiscard]] int bit_width(T Value) {
  261. static_assert(std::is_unsigned_v<T>,
  262. "Only unsigned integral types are allowed.");
  263. return std::numeric_limits<T>::digits - llvm::countl_zero(Value);
  264. }
  265. /// Returns the largest integral power of two no greater than Value if Value is
  266. /// nonzero. Returns 0 otherwise.
  267. ///
  268. /// Ex. bit_floor(5) == 4.
  269. template <typename T> [[nodiscard]] T bit_floor(T Value) {
  270. static_assert(std::is_unsigned_v<T>,
  271. "Only unsigned integral types are allowed.");
  272. if (!Value)
  273. return 0;
  274. return T(1) << (llvm::bit_width(Value) - 1);
  275. }
  276. /// Returns the smallest integral power of two no smaller than Value if Value is
  277. /// nonzero. Returns 0 otherwise.
  278. ///
  279. /// Ex. bit_ceil(5) == 8.
  280. ///
  281. /// The return value is undefined if the input is larger than the largest power
  282. /// of two representable in T.
  283. template <typename T> [[nodiscard]] T bit_ceil(T Value) {
  284. static_assert(std::is_unsigned_v<T>,
  285. "Only unsigned integral types are allowed.");
  286. if (Value < 2)
  287. return 1;
  288. return T(1) << llvm::bit_width<T>(Value - 1u);
  289. }
  290. namespace detail {
  291. template <typename T, std::size_t SizeOfT> struct PopulationCounter {
  292. static int count(T Value) {
  293. // Generic version, forward to 32 bits.
  294. static_assert(SizeOfT <= 4, "Not implemented!");
  295. #if defined(__GNUC__)
  296. return (int)__builtin_popcount(Value);
  297. #else
  298. uint32_t v = Value;
  299. v = v - ((v >> 1) & 0x55555555);
  300. v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
  301. return int(((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24);
  302. #endif
  303. }
  304. };
  305. template <typename T> struct PopulationCounter<T, 8> {
  306. static int count(T Value) {
  307. #if defined(__GNUC__)
  308. return (int)__builtin_popcountll(Value);
  309. #else
  310. uint64_t v = Value;
  311. v = v - ((v >> 1) & 0x5555555555555555ULL);
  312. v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
  313. v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
  314. return int((uint64_t)(v * 0x0101010101010101ULL) >> 56);
  315. #endif
  316. }
  317. };
  318. } // namespace detail
  319. /// Count the number of set bits in a value.
  320. /// Ex. popcount(0xF000F000) = 8
  321. /// Returns 0 if the word is zero.
  322. template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
  323. [[nodiscard]] inline int popcount(T Value) noexcept {
  324. return detail::PopulationCounter<T, sizeof(T)>::count(Value);
  325. }
  326. } // namespace llvm
  327. #endif
  328. #ifdef __GNUC__
  329. #pragma GCC diagnostic pop
  330. #endif