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

bits.h 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // Copyright 2020 The Abseil Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_NUMERIC_INTERNAL_BITS_H_
  15. #define ABSL_NUMERIC_INTERNAL_BITS_H_
  16. #include <cstdint>
  17. #include <limits>
  18. #include <type_traits>
  19. // Clang on Windows has __builtin_clzll; otherwise we need to use the
  20. // windows intrinsic functions.
  21. #if defined(_MSC_VER) && !defined(__clang__)
  22. #include <intrin.h>
  23. #endif
  24. #include "absl/base/attributes.h"
  25. #include "absl/base/config.h"
  26. #if defined(__GNUC__) && !defined(__clang__)
  27. // GCC
  28. #define ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(x) 1
  29. #else
  30. #define ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(x) ABSL_HAVE_BUILTIN(x)
  31. #endif
  32. #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_popcountl) && \
  33. ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_popcountll)
  34. #define ABSL_INTERNAL_CONSTEXPR_POPCOUNT constexpr
  35. #define ABSL_INTERNAL_HAS_CONSTEXPR_POPCOUNT 1
  36. #else
  37. #define ABSL_INTERNAL_CONSTEXPR_POPCOUNT
  38. #define ABSL_INTERNAL_HAS_CONSTEXPR_POPCOUNT 0
  39. #endif
  40. #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clz) && \
  41. ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clzll)
  42. #define ABSL_INTERNAL_CONSTEXPR_CLZ constexpr
  43. #define ABSL_INTERNAL_HAS_CONSTEXPR_CLZ 1
  44. #else
  45. #define ABSL_INTERNAL_CONSTEXPR_CLZ
  46. #define ABSL_INTERNAL_HAS_CONSTEXPR_CLZ 0
  47. #endif
  48. #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctz) && \
  49. ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctzll)
  50. #define ABSL_INTERNAL_CONSTEXPR_CTZ constexpr
  51. #define ABSL_INTERNAL_HAS_CONSTEXPR_CTZ 1
  52. #else
  53. #define ABSL_INTERNAL_CONSTEXPR_CTZ
  54. #define ABSL_INTERNAL_HAS_CONSTEXPR_CTZ 0
  55. #endif
  56. namespace absl
  57. {
  58. ABSL_NAMESPACE_BEGIN
  59. namespace numeric_internal
  60. {
  61. constexpr bool IsPowerOf2(unsigned int x) noexcept
  62. {
  63. return x != 0 && (x & (x - 1)) == 0;
  64. }
  65. template<class T>
  66. ABSL_MUST_USE_RESULT ABSL_ATTRIBUTE_ALWAYS_INLINE constexpr T RotateRight(
  67. T x, int s
  68. ) noexcept
  69. {
  70. static_assert(std::is_unsigned<T>::value, "T must be unsigned");
  71. static_assert(IsPowerOf2(std::numeric_limits<T>::digits), "T must have a power-of-2 size");
  72. return static_cast<T>(x >> (s & (std::numeric_limits<T>::digits - 1))) |
  73. static_cast<T>(x << ((-s) & (std::numeric_limits<T>::digits - 1)));
  74. }
  75. template<class T>
  76. ABSL_MUST_USE_RESULT ABSL_ATTRIBUTE_ALWAYS_INLINE constexpr T RotateLeft(
  77. T x, int s
  78. ) noexcept
  79. {
  80. static_assert(std::is_unsigned<T>::value, "T must be unsigned");
  81. static_assert(IsPowerOf2(std::numeric_limits<T>::digits), "T must have a power-of-2 size");
  82. return static_cast<T>(x << (s & (std::numeric_limits<T>::digits - 1))) |
  83. static_cast<T>(x >> ((-s) & (std::numeric_limits<T>::digits - 1)));
  84. }
  85. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline int
  86. Popcount32(uint32_t x) noexcept
  87. {
  88. #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_popcount)
  89. static_assert(sizeof(unsigned int) == sizeof(x), "__builtin_popcount does not take 32-bit arg");
  90. return __builtin_popcount(x);
  91. #else
  92. x -= ((x >> 1) & 0x55555555);
  93. x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
  94. return static_cast<int>((((x + (x >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24);
  95. #endif
  96. }
  97. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline int
  98. Popcount64(uint64_t x) noexcept
  99. {
  100. #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_popcountll)
  101. static_assert(sizeof(unsigned long long) == sizeof(x), // NOLINT(runtime/int)
  102. "__builtin_popcount does not take 64-bit arg");
  103. return __builtin_popcountll(x);
  104. #else
  105. x -= (x >> 1) & 0x5555555555555555ULL;
  106. x = ((x >> 2) & 0x3333333333333333ULL) + (x & 0x3333333333333333ULL);
  107. return static_cast<int>(
  108. (((x + (x >> 4)) & 0xF0F0F0F0F0F0F0FULL) * 0x101010101010101ULL) >> 56
  109. );
  110. #endif
  111. }
  112. template<class T>
  113. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline int
  114. Popcount(T x) noexcept
  115. {
  116. static_assert(std::is_unsigned<T>::value, "T must be unsigned");
  117. static_assert(IsPowerOf2(std::numeric_limits<T>::digits), "T must have a power-of-2 size");
  118. static_assert(sizeof(x) <= sizeof(uint64_t), "T is too large");
  119. return sizeof(x) <= sizeof(uint32_t) ? Popcount32(x) : Popcount64(x);
  120. }
  121. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
  122. CountLeadingZeroes32(uint32_t x)
  123. {
  124. #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clz)
  125. // Use __builtin_clz, which uses the following instructions:
  126. // x86: bsr, lzcnt
  127. // ARM64: clz
  128. // PPC: cntlzd
  129. static_assert(sizeof(unsigned int) == sizeof(x), "__builtin_clz does not take 32-bit arg");
  130. // Handle 0 as a special case because __builtin_clz(0) is undefined.
  131. return x == 0 ? 32 : __builtin_clz(x);
  132. #elif defined(_MSC_VER) && !defined(__clang__)
  133. unsigned long result = 0; // NOLINT(runtime/int)
  134. if (_BitScanReverse(&result, x))
  135. {
  136. return 31 - result;
  137. }
  138. return 32;
  139. #else
  140. int zeroes = 28;
  141. if (x >> 16)
  142. {
  143. zeroes -= 16;
  144. x >>= 16;
  145. }
  146. if (x >> 8)
  147. {
  148. zeroes -= 8;
  149. x >>= 8;
  150. }
  151. if (x >> 4)
  152. {
  153. zeroes -= 4;
  154. x >>= 4;
  155. }
  156. return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
  157. #endif
  158. }
  159. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
  160. CountLeadingZeroes16(uint16_t x)
  161. {
  162. #if ABSL_HAVE_BUILTIN(__builtin_clzs)
  163. static_assert(sizeof(unsigned short) == sizeof(x), // NOLINT(runtime/int)
  164. "__builtin_clzs does not take 16-bit arg");
  165. return x == 0 ? 16 : __builtin_clzs(x);
  166. #else
  167. return CountLeadingZeroes32(x) - 16;
  168. #endif
  169. }
  170. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
  171. CountLeadingZeroes64(uint64_t x)
  172. {
  173. #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clzll)
  174. // Use __builtin_clzll, which uses the following instructions:
  175. // x86: bsr, lzcnt
  176. // ARM64: clz
  177. // PPC: cntlzd
  178. static_assert(sizeof(unsigned long long) == sizeof(x), // NOLINT(runtime/int)
  179. "__builtin_clzll does not take 64-bit arg");
  180. // Handle 0 as a special case because __builtin_clzll(0) is undefined.
  181. return x == 0 ? 64 : __builtin_clzll(x);
  182. #elif defined(_MSC_VER) && !defined(__clang__) && \
  183. (defined(_M_X64) || defined(_M_ARM64))
  184. // MSVC does not have __buitin_clzll. Use _BitScanReverse64.
  185. unsigned long result = 0; // NOLINT(runtime/int)
  186. if (_BitScanReverse64(&result, x))
  187. {
  188. return 63 - result;
  189. }
  190. return 64;
  191. #elif defined(_MSC_VER) && !defined(__clang__)
  192. // MSVC does not have __buitin_clzll. Compose two calls to _BitScanReverse
  193. unsigned long result = 0; // NOLINT(runtime/int)
  194. if ((x >> 32) &&
  195. _BitScanReverse(&result, static_cast<unsigned long>(x >> 32)))
  196. {
  197. return 31 - result;
  198. }
  199. if (_BitScanReverse(&result, static_cast<unsigned long>(x)))
  200. {
  201. return 63 - result;
  202. }
  203. return 64;
  204. #else
  205. int zeroes = 60;
  206. if (x >> 32)
  207. {
  208. zeroes -= 32;
  209. x >>= 32;
  210. }
  211. if (x >> 16)
  212. {
  213. zeroes -= 16;
  214. x >>= 16;
  215. }
  216. if (x >> 8)
  217. {
  218. zeroes -= 8;
  219. x >>= 8;
  220. }
  221. if (x >> 4)
  222. {
  223. zeroes -= 4;
  224. x >>= 4;
  225. }
  226. return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
  227. #endif
  228. }
  229. template<typename T>
  230. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
  231. CountLeadingZeroes(T x)
  232. {
  233. static_assert(std::is_unsigned<T>::value, "T must be unsigned");
  234. static_assert(IsPowerOf2(std::numeric_limits<T>::digits), "T must have a power-of-2 size");
  235. static_assert(sizeof(T) <= sizeof(uint64_t), "T too large");
  236. return sizeof(T) <= sizeof(uint16_t) ? CountLeadingZeroes16(static_cast<uint16_t>(x)) -
  237. (std::numeric_limits<uint16_t>::digits -
  238. std::numeric_limits<T>::digits) :
  239. (sizeof(T) <= sizeof(uint32_t) ? CountLeadingZeroes32(static_cast<uint32_t>(x)) -
  240. (std::numeric_limits<uint32_t>::digits -
  241. std::numeric_limits<T>::digits) :
  242. CountLeadingZeroes64(x));
  243. }
  244. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
  245. CountTrailingZeroesNonzero32(uint32_t x)
  246. {
  247. #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctz)
  248. static_assert(sizeof(unsigned int) == sizeof(x), "__builtin_ctz does not take 32-bit arg");
  249. return __builtin_ctz(x);
  250. #elif defined(_MSC_VER) && !defined(__clang__)
  251. unsigned long result = 0; // NOLINT(runtime/int)
  252. _BitScanForward(&result, x);
  253. return result;
  254. #else
  255. int c = 31;
  256. x &= ~x + 1;
  257. if (x & 0x0000FFFF)
  258. c -= 16;
  259. if (x & 0x00FF00FF)
  260. c -= 8;
  261. if (x & 0x0F0F0F0F)
  262. c -= 4;
  263. if (x & 0x33333333)
  264. c -= 2;
  265. if (x & 0x55555555)
  266. c -= 1;
  267. return c;
  268. #endif
  269. }
  270. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
  271. CountTrailingZeroesNonzero64(uint64_t x)
  272. {
  273. #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctzll)
  274. static_assert(sizeof(unsigned long long) == sizeof(x), // NOLINT(runtime/int)
  275. "__builtin_ctzll does not take 64-bit arg");
  276. return __builtin_ctzll(x);
  277. #elif defined(_MSC_VER) && !defined(__clang__) && \
  278. (defined(_M_X64) || defined(_M_ARM64))
  279. unsigned long result = 0; // NOLINT(runtime/int)
  280. _BitScanForward64(&result, x);
  281. return result;
  282. #elif defined(_MSC_VER) && !defined(__clang__)
  283. unsigned long result = 0; // NOLINT(runtime/int)
  284. if (static_cast<uint32_t>(x) == 0)
  285. {
  286. _BitScanForward(&result, static_cast<unsigned long>(x >> 32));
  287. return result + 32;
  288. }
  289. _BitScanForward(&result, static_cast<unsigned long>(x));
  290. return result;
  291. #else
  292. int c = 63;
  293. x &= ~x + 1;
  294. if (x & 0x00000000FFFFFFFF)
  295. c -= 32;
  296. if (x & 0x0000FFFF0000FFFF)
  297. c -= 16;
  298. if (x & 0x00FF00FF00FF00FF)
  299. c -= 8;
  300. if (x & 0x0F0F0F0F0F0F0F0F)
  301. c -= 4;
  302. if (x & 0x3333333333333333)
  303. c -= 2;
  304. if (x & 0x5555555555555555)
  305. c -= 1;
  306. return c;
  307. #endif
  308. }
  309. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
  310. CountTrailingZeroesNonzero16(uint16_t x)
  311. {
  312. #if ABSL_HAVE_BUILTIN(__builtin_ctzs)
  313. static_assert(sizeof(unsigned short) == sizeof(x), // NOLINT(runtime/int)
  314. "__builtin_ctzs does not take 16-bit arg");
  315. return __builtin_ctzs(x);
  316. #else
  317. return CountTrailingZeroesNonzero32(x);
  318. #endif
  319. }
  320. template<class T>
  321. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
  322. CountTrailingZeroes(T x) noexcept
  323. {
  324. static_assert(std::is_unsigned<T>::value, "T must be unsigned");
  325. static_assert(IsPowerOf2(std::numeric_limits<T>::digits), "T must have a power-of-2 size");
  326. static_assert(sizeof(T) <= sizeof(uint64_t), "T too large");
  327. return x == 0 ? std::numeric_limits<T>::digits : (sizeof(T) <= sizeof(uint16_t) ? CountTrailingZeroesNonzero16(static_cast<uint16_t>(x)) : (sizeof(T) <= sizeof(uint32_t) ? CountTrailingZeroesNonzero32(static_cast<uint32_t>(x)) : CountTrailingZeroesNonzero64(x)));
  328. }
  329. // If T is narrower than unsigned, T{1} << bit_width will be promoted. We
  330. // want to force it to wraparound so that bit_ceil of an invalid value are not
  331. // core constant expressions.
  332. template<class T>
  333. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline
  334. typename std::enable_if<std::is_unsigned<T>::value, T>::type
  335. BitCeilPromotionHelper(T x, T promotion)
  336. {
  337. return (T{1} << (x + promotion)) >> promotion;
  338. }
  339. template<class T>
  340. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline
  341. typename std::enable_if<std::is_unsigned<T>::value, T>::type
  342. BitCeilNonPowerOf2(T x)
  343. {
  344. // If T is narrower than unsigned, it undergoes promotion to unsigned when we
  345. // shift. We calculate the number of bits added by the wider type.
  346. return BitCeilPromotionHelper(
  347. static_cast<T>(std::numeric_limits<T>::digits - CountLeadingZeroes(x)),
  348. T{sizeof(T) >= sizeof(unsigned) ? 0 : std::numeric_limits<unsigned>::digits - std::numeric_limits<T>::digits}
  349. );
  350. }
  351. } // namespace numeric_internal
  352. ABSL_NAMESPACE_END
  353. } // namespace absl
  354. #endif // ABSL_NUMERIC_INTERNAL_BITS_H_