bits.h 7.2 KB

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