bits.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 Y_ABSL_NUMERIC_INTERNAL_BITS_H_
  15. #define Y_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 "y_absl/base/attributes.h"
  25. #include "y_absl/base/config.h"
  26. #if defined(__GNUC__) && !defined(__clang__)
  27. // GCC
  28. #define Y_ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(x) 1
  29. #else
  30. #define Y_ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(x) Y_ABSL_HAVE_BUILTIN(x)
  31. #endif
  32. #if Y_ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_popcountl) && \
  33. Y_ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_popcountll)
  34. #define Y_ABSL_INTERNAL_CONSTEXPR_POPCOUNT constexpr
  35. #define Y_ABSL_INTERNAL_HAS_CONSTEXPR_POPCOUNT 1
  36. #else
  37. #define Y_ABSL_INTERNAL_CONSTEXPR_POPCOUNT
  38. #define Y_ABSL_INTERNAL_HAS_CONSTEXPR_POPCOUNT 0
  39. #endif
  40. #if Y_ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clz) && \
  41. Y_ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clzll)
  42. #define Y_ABSL_INTERNAL_CONSTEXPR_CLZ constexpr
  43. #define Y_ABSL_INTERNAL_HAS_CONSTEXPR_CLZ 1
  44. #else
  45. #define Y_ABSL_INTERNAL_CONSTEXPR_CLZ
  46. #define Y_ABSL_INTERNAL_HAS_CONSTEXPR_CLZ 0
  47. #endif
  48. #if Y_ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctz) && \
  49. Y_ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctzll)
  50. #define Y_ABSL_INTERNAL_CONSTEXPR_CTZ constexpr
  51. #define Y_ABSL_INTERNAL_HAS_CONSTEXPR_CTZ 1
  52. #else
  53. #define Y_ABSL_INTERNAL_CONSTEXPR_CTZ
  54. #define Y_ABSL_INTERNAL_HAS_CONSTEXPR_CTZ 0
  55. #endif
  56. namespace y_absl {
  57. Y_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. Y_ABSL_MUST_USE_RESULT Y_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. Y_ABSL_MUST_USE_RESULT Y_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. Y_ABSL_ATTRIBUTE_ALWAYS_INLINE Y_ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline int
  81. Popcount32(uint32_t x) noexcept {
  82. #if Y_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. Y_ABSL_ATTRIBUTE_ALWAYS_INLINE Y_ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline int
  93. Popcount64(uint64_t x) noexcept {
  94. #if Y_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. Y_ABSL_ATTRIBUTE_ALWAYS_INLINE Y_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. Y_ABSL_ATTRIBUTE_ALWAYS_INLINE Y_ABSL_INTERNAL_CONSTEXPR_CLZ inline int
  115. CountLeadingZeroes32(uint32_t x) {
  116. #if Y_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. Y_ABSL_ATTRIBUTE_ALWAYS_INLINE Y_ABSL_INTERNAL_CONSTEXPR_CLZ inline int
  149. CountLeadingZeroes16(uint16_t x) {
  150. #if Y_ABSL_HAVE_BUILTIN(__builtin_clzg)
  151. return x == 0 ? 16 : __builtin_clzg(x);
  152. #elif Y_ABSL_HAVE_BUILTIN(__builtin_clzs)
  153. static_assert(sizeof(unsigned short) == sizeof(x), // NOLINT(runtime/int)
  154. "__builtin_clzs does not take 16-bit arg");
  155. return x == 0 ? 16 : __builtin_clzs(x);
  156. #else
  157. return CountLeadingZeroes32(x) - 16;
  158. #endif
  159. }
  160. Y_ABSL_ATTRIBUTE_ALWAYS_INLINE Y_ABSL_INTERNAL_CONSTEXPR_CLZ inline int
  161. CountLeadingZeroes64(uint64_t x) {
  162. #if Y_ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clzll)
  163. // Use __builtin_clzll, which uses the following instructions:
  164. // x86: bsr, lzcnt
  165. // ARM64: clz
  166. // PPC: cntlzd
  167. static_assert(sizeof(unsigned long long) == sizeof(x), // NOLINT(runtime/int)
  168. "__builtin_clzll does not take 64-bit arg");
  169. // Handle 0 as a special case because __builtin_clzll(0) is undefined.
  170. return x == 0 ? 64 : __builtin_clzll(x);
  171. #elif defined(_MSC_VER) && !defined(__clang__) && \
  172. (defined(_M_X64) || defined(_M_ARM64))
  173. // MSVC does not have __buitin_clzll. Use _BitScanReverse64.
  174. unsigned long result = 0; // NOLINT(runtime/int)
  175. if (_BitScanReverse64(&result, x)) {
  176. return 63 - result;
  177. }
  178. return 64;
  179. #elif defined(_MSC_VER) && !defined(__clang__)
  180. // MSVC does not have __buitin_clzll. Compose two calls to _BitScanReverse
  181. unsigned long result = 0; // NOLINT(runtime/int)
  182. if ((x >> 32) &&
  183. _BitScanReverse(&result, static_cast<unsigned long>(x >> 32))) {
  184. return 31 - result;
  185. }
  186. if (_BitScanReverse(&result, static_cast<unsigned long>(x))) {
  187. return 63 - result;
  188. }
  189. return 64;
  190. #else
  191. int zeroes = 60;
  192. if (x >> 32) {
  193. zeroes -= 32;
  194. x >>= 32;
  195. }
  196. if (x >> 16) {
  197. zeroes -= 16;
  198. x >>= 16;
  199. }
  200. if (x >> 8) {
  201. zeroes -= 8;
  202. x >>= 8;
  203. }
  204. if (x >> 4) {
  205. zeroes -= 4;
  206. x >>= 4;
  207. }
  208. return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
  209. #endif
  210. }
  211. template <typename T>
  212. Y_ABSL_ATTRIBUTE_ALWAYS_INLINE Y_ABSL_INTERNAL_CONSTEXPR_CLZ inline int
  213. CountLeadingZeroes(T x) {
  214. static_assert(std::is_unsigned<T>::value, "T must be unsigned");
  215. static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
  216. "T must have a power-of-2 size");
  217. static_assert(sizeof(T) <= sizeof(uint64_t), "T too large");
  218. return sizeof(T) <= sizeof(uint16_t)
  219. ? CountLeadingZeroes16(static_cast<uint16_t>(x)) -
  220. (std::numeric_limits<uint16_t>::digits -
  221. std::numeric_limits<T>::digits)
  222. : (sizeof(T) <= sizeof(uint32_t)
  223. ? CountLeadingZeroes32(static_cast<uint32_t>(x)) -
  224. (std::numeric_limits<uint32_t>::digits -
  225. std::numeric_limits<T>::digits)
  226. : CountLeadingZeroes64(x));
  227. }
  228. Y_ABSL_ATTRIBUTE_ALWAYS_INLINE Y_ABSL_INTERNAL_CONSTEXPR_CTZ inline int
  229. CountTrailingZeroesNonzero32(uint32_t x) {
  230. #if Y_ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctz)
  231. static_assert(sizeof(unsigned int) == sizeof(x),
  232. "__builtin_ctz does not take 32-bit arg");
  233. return __builtin_ctz(x);
  234. #elif defined(_MSC_VER) && !defined(__clang__)
  235. unsigned long result = 0; // NOLINT(runtime/int)
  236. _BitScanForward(&result, x);
  237. return result;
  238. #else
  239. int c = 31;
  240. x &= ~x + 1;
  241. if (x & 0x0000FFFF) c -= 16;
  242. if (x & 0x00FF00FF) c -= 8;
  243. if (x & 0x0F0F0F0F) c -= 4;
  244. if (x & 0x33333333) c -= 2;
  245. if (x & 0x55555555) c -= 1;
  246. return c;
  247. #endif
  248. }
  249. Y_ABSL_ATTRIBUTE_ALWAYS_INLINE Y_ABSL_INTERNAL_CONSTEXPR_CTZ inline int
  250. CountTrailingZeroesNonzero64(uint64_t x) {
  251. #if Y_ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctzll)
  252. static_assert(sizeof(unsigned long long) == sizeof(x), // NOLINT(runtime/int)
  253. "__builtin_ctzll does not take 64-bit arg");
  254. return __builtin_ctzll(x);
  255. #elif defined(_MSC_VER) && !defined(__clang__) && \
  256. (defined(_M_X64) || defined(_M_ARM64))
  257. unsigned long result = 0; // NOLINT(runtime/int)
  258. _BitScanForward64(&result, x);
  259. return result;
  260. #elif defined(_MSC_VER) && !defined(__clang__)
  261. unsigned long result = 0; // NOLINT(runtime/int)
  262. if (static_cast<uint32_t>(x) == 0) {
  263. _BitScanForward(&result, static_cast<unsigned long>(x >> 32));
  264. return result + 32;
  265. }
  266. _BitScanForward(&result, static_cast<unsigned long>(x));
  267. return result;
  268. #else
  269. int c = 63;
  270. x &= ~x + 1;
  271. if (x & 0x00000000FFFFFFFF) c -= 32;
  272. if (x & 0x0000FFFF0000FFFF) c -= 16;
  273. if (x & 0x00FF00FF00FF00FF) c -= 8;
  274. if (x & 0x0F0F0F0F0F0F0F0F) c -= 4;
  275. if (x & 0x3333333333333333) c -= 2;
  276. if (x & 0x5555555555555555) c -= 1;
  277. return c;
  278. #endif
  279. }
  280. Y_ABSL_ATTRIBUTE_ALWAYS_INLINE Y_ABSL_INTERNAL_CONSTEXPR_CTZ inline int
  281. CountTrailingZeroesNonzero16(uint16_t x) {
  282. #if Y_ABSL_HAVE_BUILTIN(__builtin_ctzg)
  283. return __builtin_ctzg(x);
  284. #elif Y_ABSL_HAVE_BUILTIN(__builtin_ctzs)
  285. static_assert(sizeof(unsigned short) == sizeof(x), // NOLINT(runtime/int)
  286. "__builtin_ctzs does not take 16-bit arg");
  287. return __builtin_ctzs(x);
  288. #else
  289. return CountTrailingZeroesNonzero32(x);
  290. #endif
  291. }
  292. template <class T>
  293. Y_ABSL_ATTRIBUTE_ALWAYS_INLINE Y_ABSL_INTERNAL_CONSTEXPR_CTZ inline int
  294. CountTrailingZeroes(T x) noexcept {
  295. static_assert(std::is_unsigned<T>::value, "T must be unsigned");
  296. static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
  297. "T must have a power-of-2 size");
  298. static_assert(sizeof(T) <= sizeof(uint64_t), "T too large");
  299. return x == 0 ? std::numeric_limits<T>::digits
  300. : (sizeof(T) <= sizeof(uint16_t)
  301. ? CountTrailingZeroesNonzero16(static_cast<uint16_t>(x))
  302. : (sizeof(T) <= sizeof(uint32_t)
  303. ? CountTrailingZeroesNonzero32(
  304. static_cast<uint32_t>(x))
  305. : CountTrailingZeroesNonzero64(x)));
  306. }
  307. // If T is narrower than unsigned, T{1} << bit_width will be promoted. We
  308. // want to force it to wraparound so that bit_ceil of an invalid value are not
  309. // core constant expressions.
  310. template <class T>
  311. Y_ABSL_ATTRIBUTE_ALWAYS_INLINE Y_ABSL_INTERNAL_CONSTEXPR_CLZ inline
  312. typename std::enable_if<std::is_unsigned<T>::value, T>::type
  313. BitCeilPromotionHelper(T x, T promotion) {
  314. return (T{1} << (x + promotion)) >> promotion;
  315. }
  316. template <class T>
  317. Y_ABSL_ATTRIBUTE_ALWAYS_INLINE Y_ABSL_INTERNAL_CONSTEXPR_CLZ inline
  318. typename std::enable_if<std::is_unsigned<T>::value, T>::type
  319. BitCeilNonPowerOf2(T x) {
  320. // If T is narrower than unsigned, it undergoes promotion to unsigned when we
  321. // shift. We calculate the number of bits added by the wider type.
  322. return BitCeilPromotionHelper(
  323. static_cast<T>(std::numeric_limits<T>::digits - CountLeadingZeroes(x)),
  324. T{sizeof(T) >= sizeof(unsigned) ? 0
  325. : std::numeric_limits<unsigned>::digits -
  326. std::numeric_limits<T>::digits});
  327. }
  328. } // namespace numeric_internal
  329. Y_ABSL_NAMESPACE_END
  330. } // namespace y_absl
  331. #endif // Y_ABSL_NUMERIC_INTERNAL_BITS_H_