bits.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. ABSL_NAMESPACE_BEGIN
  58. namespace numeric_internal {
  59. constexpr bool IsPowerOf2(unsigned int x) noexcept {
  60. return x != 0 && (x & (x - 1)) == 0;
  61. }
  62. template <class T>
  63. ABSL_MUST_USE_RESULT ABSL_ATTRIBUTE_ALWAYS_INLINE constexpr T RotateRight(
  64. T x, int s) noexcept {
  65. static_assert(std::is_unsigned<T>::value, "T must be unsigned");
  66. static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
  67. "T must have a power-of-2 size");
  68. return static_cast<T>(x >> (s & (std::numeric_limits<T>::digits - 1))) |
  69. static_cast<T>(x << ((-s) & (std::numeric_limits<T>::digits - 1)));
  70. }
  71. template <class T>
  72. ABSL_MUST_USE_RESULT ABSL_ATTRIBUTE_ALWAYS_INLINE constexpr T RotateLeft(
  73. T x, int s) noexcept {
  74. static_assert(std::is_unsigned<T>::value, "T must be unsigned");
  75. static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
  76. "T must have a power-of-2 size");
  77. return static_cast<T>(x << (s & (std::numeric_limits<T>::digits - 1))) |
  78. static_cast<T>(x >> ((-s) & (std::numeric_limits<T>::digits - 1)));
  79. }
  80. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline int
  81. Popcount32(uint32_t x) noexcept {
  82. #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_popcount)
  83. static_assert(sizeof(unsigned int) == sizeof(x),
  84. "__builtin_popcount does not take 32-bit arg");
  85. return __builtin_popcount(x);
  86. #else
  87. x -= ((x >> 1) & 0x55555555);
  88. x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
  89. return static_cast<int>((((x + (x >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24);
  90. #endif
  91. }
  92. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline int
  93. Popcount64(uint64_t x) noexcept {
  94. #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_popcountll)
  95. static_assert(sizeof(unsigned long long) == sizeof(x), // NOLINT(runtime/int)
  96. "__builtin_popcount does not take 64-bit arg");
  97. return __builtin_popcountll(x);
  98. #else
  99. x -= (x >> 1) & 0x5555555555555555ULL;
  100. x = ((x >> 2) & 0x3333333333333333ULL) + (x & 0x3333333333333333ULL);
  101. return static_cast<int>(
  102. (((x + (x >> 4)) & 0xF0F0F0F0F0F0F0FULL) * 0x101010101010101ULL) >> 56);
  103. #endif
  104. }
  105. template <class T>
  106. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline int
  107. Popcount(T x) noexcept {
  108. static_assert(std::is_unsigned<T>::value, "T must be unsigned");
  109. static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
  110. "T must have a power-of-2 size");
  111. static_assert(sizeof(x) <= sizeof(uint64_t), "T is too large");
  112. return sizeof(x) <= sizeof(uint32_t) ? Popcount32(x) : Popcount64(x);
  113. }
  114. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
  115. CountLeadingZeroes32(uint32_t x) {
  116. #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clz)
  117. // Use __builtin_clz, which uses the following instructions:
  118. // x86: bsr, lzcnt
  119. // ARM64: clz
  120. // PPC: cntlzd
  121. static_assert(sizeof(unsigned int) == sizeof(x),
  122. "__builtin_clz does not take 32-bit arg");
  123. // Handle 0 as a special case because __builtin_clz(0) is undefined.
  124. return x == 0 ? 32 : __builtin_clz(x);
  125. #elif defined(_MSC_VER) && !defined(__clang__)
  126. unsigned long result = 0; // NOLINT(runtime/int)
  127. if (_BitScanReverse(&result, x)) {
  128. return 31 - result;
  129. }
  130. return 32;
  131. #else
  132. int zeroes = 28;
  133. if (x >> 16) {
  134. zeroes -= 16;
  135. x >>= 16;
  136. }
  137. if (x >> 8) {
  138. zeroes -= 8;
  139. x >>= 8;
  140. }
  141. if (x >> 4) {
  142. zeroes -= 4;
  143. x >>= 4;
  144. }
  145. return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
  146. #endif
  147. }
  148. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
  149. CountLeadingZeroes16(uint16_t x) {
  150. #if ABSL_HAVE_BUILTIN(__builtin_clzs)
  151. static_assert(sizeof(unsigned short) == sizeof(x), // NOLINT(runtime/int)
  152. "__builtin_clzs does not take 16-bit arg");
  153. return x == 0 ? 16 : __builtin_clzs(x);
  154. #else
  155. return CountLeadingZeroes32(x) - 16;
  156. #endif
  157. }
  158. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
  159. CountLeadingZeroes64(uint64_t x) {
  160. #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clzll)
  161. // Use __builtin_clzll, which uses the following instructions:
  162. // x86: bsr, lzcnt
  163. // ARM64: clz
  164. // PPC: cntlzd
  165. static_assert(sizeof(unsigned long long) == sizeof(x), // NOLINT(runtime/int)
  166. "__builtin_clzll does not take 64-bit arg");
  167. // Handle 0 as a special case because __builtin_clzll(0) is undefined.
  168. return x == 0 ? 64 : __builtin_clzll(x);
  169. #elif defined(_MSC_VER) && !defined(__clang__) && \
  170. (defined(_M_X64) || defined(_M_ARM64))
  171. // MSVC does not have __buitin_clzll. Use _BitScanReverse64.
  172. unsigned long result = 0; // NOLINT(runtime/int)
  173. if (_BitScanReverse64(&result, x)) {
  174. return 63 - result;
  175. }
  176. return 64;
  177. #elif defined(_MSC_VER) && !defined(__clang__)
  178. // MSVC does not have __buitin_clzll. Compose two calls to _BitScanReverse
  179. unsigned long result = 0; // NOLINT(runtime/int)
  180. if ((x >> 32) &&
  181. _BitScanReverse(&result, static_cast<unsigned long>(x >> 32))) {
  182. return 31 - result;
  183. }
  184. if (_BitScanReverse(&result, static_cast<unsigned long>(x))) {
  185. return 63 - result;
  186. }
  187. return 64;
  188. #else
  189. int zeroes = 60;
  190. if (x >> 32) {
  191. zeroes -= 32;
  192. x >>= 32;
  193. }
  194. if (x >> 16) {
  195. zeroes -= 16;
  196. x >>= 16;
  197. }
  198. if (x >> 8) {
  199. zeroes -= 8;
  200. x >>= 8;
  201. }
  202. if (x >> 4) {
  203. zeroes -= 4;
  204. x >>= 4;
  205. }
  206. return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
  207. #endif
  208. }
  209. template <typename T>
  210. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
  211. CountLeadingZeroes(T x) {
  212. static_assert(std::is_unsigned<T>::value, "T must be unsigned");
  213. static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
  214. "T must have a power-of-2 size");
  215. static_assert(sizeof(T) <= sizeof(uint64_t), "T too large");
  216. return sizeof(T) <= sizeof(uint16_t)
  217. ? CountLeadingZeroes16(static_cast<uint16_t>(x)) -
  218. (std::numeric_limits<uint16_t>::digits -
  219. std::numeric_limits<T>::digits)
  220. : (sizeof(T) <= sizeof(uint32_t)
  221. ? CountLeadingZeroes32(static_cast<uint32_t>(x)) -
  222. (std::numeric_limits<uint32_t>::digits -
  223. std::numeric_limits<T>::digits)
  224. : CountLeadingZeroes64(x));
  225. }
  226. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
  227. CountTrailingZeroesNonzero32(uint32_t x) {
  228. #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctz)
  229. static_assert(sizeof(unsigned int) == sizeof(x),
  230. "__builtin_ctz does not take 32-bit arg");
  231. return __builtin_ctz(x);
  232. #elif defined(_MSC_VER) && !defined(__clang__)
  233. unsigned long result = 0; // NOLINT(runtime/int)
  234. _BitScanForward(&result, x);
  235. return result;
  236. #else
  237. int c = 31;
  238. x &= ~x + 1;
  239. if (x & 0x0000FFFF) c -= 16;
  240. if (x & 0x00FF00FF) c -= 8;
  241. if (x & 0x0F0F0F0F) c -= 4;
  242. if (x & 0x33333333) c -= 2;
  243. if (x & 0x55555555) c -= 1;
  244. return c;
  245. #endif
  246. }
  247. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
  248. CountTrailingZeroesNonzero64(uint64_t x) {
  249. #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctzll)
  250. static_assert(sizeof(unsigned long long) == sizeof(x), // NOLINT(runtime/int)
  251. "__builtin_ctzll does not take 64-bit arg");
  252. return __builtin_ctzll(x);
  253. #elif defined(_MSC_VER) && !defined(__clang__) && \
  254. (defined(_M_X64) || defined(_M_ARM64))
  255. unsigned long result = 0; // NOLINT(runtime/int)
  256. _BitScanForward64(&result, x);
  257. return result;
  258. #elif defined(_MSC_VER) && !defined(__clang__)
  259. unsigned long result = 0; // NOLINT(runtime/int)
  260. if (static_cast<uint32_t>(x) == 0) {
  261. _BitScanForward(&result, static_cast<unsigned long>(x >> 32));
  262. return result + 32;
  263. }
  264. _BitScanForward(&result, static_cast<unsigned long>(x));
  265. return result;
  266. #else
  267. int c = 63;
  268. x &= ~x + 1;
  269. if (x & 0x00000000FFFFFFFF) c -= 32;
  270. if (x & 0x0000FFFF0000FFFF) c -= 16;
  271. if (x & 0x00FF00FF00FF00FF) c -= 8;
  272. if (x & 0x0F0F0F0F0F0F0F0F) c -= 4;
  273. if (x & 0x3333333333333333) c -= 2;
  274. if (x & 0x5555555555555555) c -= 1;
  275. return c;
  276. #endif
  277. }
  278. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
  279. CountTrailingZeroesNonzero16(uint16_t x) {
  280. #if ABSL_HAVE_BUILTIN(__builtin_ctzs)
  281. static_assert(sizeof(unsigned short) == sizeof(x), // NOLINT(runtime/int)
  282. "__builtin_ctzs does not take 16-bit arg");
  283. return __builtin_ctzs(x);
  284. #else
  285. return CountTrailingZeroesNonzero32(x);
  286. #endif
  287. }
  288. template <class T>
  289. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
  290. CountTrailingZeroes(T x) noexcept {
  291. static_assert(std::is_unsigned<T>::value, "T must be unsigned");
  292. static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
  293. "T must have a power-of-2 size");
  294. static_assert(sizeof(T) <= sizeof(uint64_t), "T too large");
  295. return x == 0 ? std::numeric_limits<T>::digits
  296. : (sizeof(T) <= sizeof(uint16_t)
  297. ? CountTrailingZeroesNonzero16(static_cast<uint16_t>(x))
  298. : (sizeof(T) <= sizeof(uint32_t)
  299. ? CountTrailingZeroesNonzero32(
  300. static_cast<uint32_t>(x))
  301. : CountTrailingZeroesNonzero64(x)));
  302. }
  303. // If T is narrower than unsigned, T{1} << bit_width will be promoted. We
  304. // want to force it to wraparound so that bit_ceil of an invalid value are not
  305. // core constant expressions.
  306. template <class T>
  307. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline
  308. typename std::enable_if<std::is_unsigned<T>::value, T>::type
  309. BitCeilPromotionHelper(T x, T promotion) {
  310. return (T{1} << (x + promotion)) >> promotion;
  311. }
  312. template <class T>
  313. ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline
  314. typename std::enable_if<std::is_unsigned<T>::value, T>::type
  315. BitCeilNonPowerOf2(T x) {
  316. // If T is narrower than unsigned, it undergoes promotion to unsigned when we
  317. // shift. We calculate the number of bits added by the wider type.
  318. return BitCeilPromotionHelper(
  319. static_cast<T>(std::numeric_limits<T>::digits - CountLeadingZeroes(x)),
  320. T{sizeof(T) >= sizeof(unsigned) ? 0
  321. : std::numeric_limits<unsigned>::digits -
  322. std::numeric_limits<T>::digits});
  323. }
  324. } // namespace numeric_internal
  325. ABSL_NAMESPACE_END
  326. } // namespace absl
  327. #endif // ABSL_NUMERIC_INTERNAL_BITS_H_