bits.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright (c) 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // This file defines some bit utilities.
  5. #ifndef BASE_BITS_H_
  6. #define BASE_BITS_H_
  7. #include <limits.h>
  8. #include <stddef.h>
  9. #include <stdint.h>
  10. #include <type_traits>
  11. #include "base/check.h"
  12. #include "base/compiler_specific.h"
  13. #include "build/build_config.h"
  14. #if defined(COMPILER_MSVC)
  15. #include <intrin.h>
  16. #endif
  17. namespace base {
  18. namespace bits {
  19. // Returns true iff |value| is a power of 2.
  20. template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
  21. constexpr bool IsPowerOfTwo(T value) {
  22. // From "Hacker's Delight": Section 2.1 Manipulating Rightmost Bits.
  23. //
  24. // Only positive integers with a single bit set are powers of two. If only one
  25. // bit is set in x (e.g. 0b00000100000000) then |x-1| will have that bit set
  26. // to zero and all bits to its right set to 1 (e.g. 0b00000011111111). Hence
  27. // |x & (x-1)| is 0 iff x is a power of two.
  28. return value > 0 && (value & (value - 1)) == 0;
  29. }
  30. // Round up |size| to a multiple of alignment, which must be a power of two.
  31. inline size_t Align(size_t size, size_t alignment) {
  32. DCHECK(IsPowerOfTwo(alignment));
  33. return (size + alignment - 1) & ~(alignment - 1);
  34. }
  35. // Advance |ptr| to the next multiple of alignment, which must be a power of
  36. // two. Defined for types where sizeof(T) is one byte.
  37. template <typename T, typename = typename std::enable_if<sizeof(T) == 1>::type>
  38. inline T* Align(T* ptr, size_t alignment) {
  39. return reinterpret_cast<T*>(Align(reinterpret_cast<size_t>(ptr), alignment));
  40. }
  41. // Round down |size| to a multiple of alignment, which must be a power of two.
  42. inline size_t AlignDown(size_t size, size_t alignment) {
  43. DCHECK(IsPowerOfTwo(alignment));
  44. return size & ~(alignment - 1);
  45. }
  46. // Move |ptr| back to the previous multiple of alignment, which must be a power
  47. // of two. Defined for types where sizeof(T) is one byte.
  48. template <typename T, typename = typename std::enable_if<sizeof(T) == 1>::type>
  49. inline T* AlignDown(T* ptr, size_t alignment) {
  50. return reinterpret_cast<T*>(
  51. AlignDown(reinterpret_cast<size_t>(ptr), alignment));
  52. }
  53. // CountLeadingZeroBits(value) returns the number of zero bits following the
  54. // most significant 1 bit in |value| if |value| is non-zero, otherwise it
  55. // returns {sizeof(T) * 8}.
  56. // Example: 00100010 -> 2
  57. //
  58. // CountTrailingZeroBits(value) returns the number of zero bits preceding the
  59. // least significant 1 bit in |value| if |value| is non-zero, otherwise it
  60. // returns {sizeof(T) * 8}.
  61. // Example: 00100010 -> 1
  62. //
  63. // C does not have an operator to do this, but fortunately the various
  64. // compilers have built-ins that map to fast underlying processor instructions.
  65. #if defined(COMPILER_MSVC)
  66. template <typename T, unsigned bits = sizeof(T) * 8>
  67. ALWAYS_INLINE
  68. typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) <= 4,
  69. unsigned>::type
  70. CountLeadingZeroBits(T x) {
  71. static_assert(bits > 0, "invalid instantiation");
  72. unsigned long index;
  73. return LIKELY(_BitScanReverse(&index, static_cast<uint32_t>(x)))
  74. ? (31 - index - (32 - bits))
  75. : bits;
  76. }
  77. template <typename T, unsigned bits = sizeof(T) * 8>
  78. ALWAYS_INLINE
  79. typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) == 8,
  80. unsigned>::type
  81. CountLeadingZeroBits(T x) {
  82. static_assert(bits > 0, "invalid instantiation");
  83. unsigned long index;
  84. // MSVC only supplies _BitScanReverse64 when building for a 64-bit target.
  85. #if defined(ARCH_CPU_64_BITS)
  86. return LIKELY(_BitScanReverse64(&index, static_cast<uint64_t>(x)))
  87. ? (63 - index)
  88. : 64;
  89. #else
  90. uint32_t left = static_cast<uint32_t>(x >> 32);
  91. if (LIKELY(_BitScanReverse(&index, left)))
  92. return 31 - index;
  93. uint32_t right = static_cast<uint32_t>(x);
  94. if (LIKELY(_BitScanReverse(&index, right)))
  95. return 63 - index;
  96. return 64;
  97. #endif
  98. }
  99. template <typename T, unsigned bits = sizeof(T) * 8>
  100. ALWAYS_INLINE
  101. typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) <= 4,
  102. unsigned>::type
  103. CountTrailingZeroBits(T x) {
  104. static_assert(bits > 0, "invalid instantiation");
  105. unsigned long index;
  106. return LIKELY(_BitScanForward(&index, static_cast<uint32_t>(x))) ? index
  107. : bits;
  108. }
  109. template <typename T, unsigned bits = sizeof(T) * 8>
  110. ALWAYS_INLINE
  111. typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) == 8,
  112. unsigned>::type
  113. CountTrailingZeroBits(T x) {
  114. static_assert(bits > 0, "invalid instantiation");
  115. unsigned long index;
  116. // MSVC only supplies _BitScanForward64 when building for a 64-bit target.
  117. #if defined(ARCH_CPU_64_BITS)
  118. return LIKELY(_BitScanForward64(&index, static_cast<uint64_t>(x))) ? index
  119. : 64;
  120. #else
  121. uint32_t right = static_cast<uint32_t>(x);
  122. if (LIKELY(_BitScanForward(&index, right)))
  123. return index;
  124. uint32_t left = static_cast<uint32_t>(x >> 32);
  125. if (LIKELY(_BitScanForward(&index, left)))
  126. return 32 + index;
  127. return 64;
  128. #endif
  129. }
  130. ALWAYS_INLINE uint32_t CountLeadingZeroBits32(uint32_t x) {
  131. return CountLeadingZeroBits(x);
  132. }
  133. ALWAYS_INLINE uint64_t CountLeadingZeroBits64(uint64_t x) {
  134. return CountLeadingZeroBits(x);
  135. }
  136. #elif defined(COMPILER_GCC)
  137. // __builtin_clz has undefined behaviour for an input of 0, even though there's
  138. // clearly a return value that makes sense, and even though some processor clz
  139. // instructions have defined behaviour for 0. We could drop to raw __asm__ to
  140. // do better, but we'll avoid doing that unless we see proof that we need to.
  141. template <typename T, unsigned bits = sizeof(T) * 8>
  142. ALWAYS_INLINE
  143. typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) <= 8,
  144. unsigned>::type
  145. CountLeadingZeroBits(T value) {
  146. static_assert(bits > 0, "invalid instantiation");
  147. return LIKELY(value)
  148. ? bits == 64
  149. ? __builtin_clzll(static_cast<uint64_t>(value))
  150. : __builtin_clz(static_cast<uint32_t>(value)) - (32 - bits)
  151. : bits;
  152. }
  153. template <typename T, unsigned bits = sizeof(T) * 8>
  154. ALWAYS_INLINE
  155. typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) <= 8,
  156. unsigned>::type
  157. CountTrailingZeroBits(T value) {
  158. return LIKELY(value) ? bits == 64
  159. ? __builtin_ctzll(static_cast<uint64_t>(value))
  160. : __builtin_ctz(static_cast<uint32_t>(value))
  161. : bits;
  162. }
  163. ALWAYS_INLINE uint32_t CountLeadingZeroBits32(uint32_t x) {
  164. return CountLeadingZeroBits(x);
  165. }
  166. ALWAYS_INLINE uint64_t CountLeadingZeroBits64(uint64_t x) {
  167. return CountLeadingZeroBits(x);
  168. }
  169. #endif
  170. ALWAYS_INLINE size_t CountLeadingZeroBitsSizeT(size_t x) {
  171. return CountLeadingZeroBits(x);
  172. }
  173. ALWAYS_INLINE size_t CountTrailingZeroBitsSizeT(size_t x) {
  174. return CountTrailingZeroBits(x);
  175. }
  176. // Returns the integer i such as 2^i <= n < 2^(i+1)
  177. inline int Log2Floor(uint32_t n) {
  178. return 31 - CountLeadingZeroBits(n);
  179. }
  180. // Returns the integer i such as 2^(i-1) < n <= 2^i
  181. inline int Log2Ceiling(uint32_t n) {
  182. // When n == 0, we want the function to return -1.
  183. // When n == 0, (n - 1) will underflow to 0xFFFFFFFF, which is
  184. // why the statement below starts with (n ? 32 : -1).
  185. return (n ? 32 : -1) - CountLeadingZeroBits(n - 1);
  186. }
  187. // Returns a value of type T with a single bit set in the left-most position.
  188. // Can be used instead of manually shifting a 1 to the left.
  189. template <typename T>
  190. constexpr T LeftmostBit() {
  191. static_assert(std::is_integral<T>::value,
  192. "This function can only be used with integral types.");
  193. T one(1u);
  194. return one << ((CHAR_BIT * sizeof(T) - 1));
  195. }
  196. } // namespace bits
  197. } // namespace base
  198. #endif // BASE_BITS_H_