Chromium Code Reviews| Index: src/base/bits.h |
| diff --git a/src/base/bits.h b/src/base/bits.h |
| index e6a733a45dbb24ce4c95a30a817b2de2b4ee2354..38a7c57d98c453de6ba2c417f5a4c2c8f7ae64a8 100644 |
| --- a/src/base/bits.h |
| +++ b/src/base/bits.h |
| @@ -33,6 +33,17 @@ inline uint32_t CountPopulation32(uint32_t value) { |
| } |
| +// CountPopulation64(value) returns the number of bits set in |value|. |
| +inline uint32_t CountPopulation64(uint64_t value) { |
| +#if V8_HAS_BUILTIN_POPCOUNT |
| + return __builtin_popcountll(value); |
| +#else |
| + return CountPopulation32(static_cast<uint32_t>(value)) + |
| + CountPopulation32(static_cast<uint32_t>(value >> 32)); |
| +#endif |
| +} |
| + |
| + |
| // CountLeadingZeros32(value) returns the number of zero bits following the most |
| // significant 1 bit in |value| if |value| is non-zero, otherwise it returns 32. |
| inline uint32_t CountLeadingZeros32(uint32_t value) { |
| @@ -53,6 +64,23 @@ inline uint32_t CountLeadingZeros32(uint32_t value) { |
| } |
| +// CountLeadingZeros64(value) returns the number of zero bits following the most |
| +// significant 1 bit in |value| if |value| is non-zero, otherwise it returns 64. |
| +inline uint32_t CountLeadingZeros64(uint64_t value) { |
| +#if V8_HAS_BUILTIN_CLZ |
| + return value ? __builtin_clzll(value) : 64; |
| +#else |
| + value = value | (value >> 1); |
| + value = value | (value >> 2); |
| + value = value | (value >> 4); |
| + value = value | (value >> 8); |
| + value = value | (value >> 16); |
| + value = value | (value >> 32); |
| + return CountPopulation64(~value); |
| +#endif |
| +} |
| + |
| + |
| // CountTrailingZeros32(value) returns the number of zero bits preceding the |
| // least significant 1 bit in |value| if |value| is non-zero, otherwise it |
| // returns 32. |
| @@ -73,6 +101,22 @@ inline uint32_t CountTrailingZeros32(uint32_t value) { |
| } |
| +// CountTrailingZeros64(value) returns the number of zero bits preceding the |
| +// least significant 1 bit in |value| if |value| is non-zero, otherwise it |
| +// returns 64. |
| +inline uint64_t CountTrailingZeros64(uint64_t value) { |
|
Benedikt Meurer
2014/10/08 11:40:12
I think the result type of all these Count* functi
m.m.capewell
2014/10/08 17:35:01
Done.
|
| +#if V8_HAS_BUILTIN_CTZ |
| + return value ? __builtin_ctzll(value) : 64; |
| +#else |
| + if (value == 0) return 64; |
| + unsigned count = 0; |
| + for (value ^= value - 1; value >>= 1; ++count) |
| + ; |
| + return count; |
| +#endif |
| +} |
| + |
| + |
| // Returns true iff |value| is a power of 2. |
| inline bool IsPowerOfTwo32(uint32_t value) { |
| return value && !(value & (value - 1)); |