OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_BASE_BITS_H_ | 5 #ifndef V8_BASE_BITS_H_ |
6 #define V8_BASE_BITS_H_ | 6 #define V8_BASE_BITS_H_ |
7 | 7 |
8 #include "include/v8stdint.h" | 8 #include "include/v8stdint.h" |
| 9 #if V8_CC_MSVC |
| 10 #include <intrin.h> |
| 11 #endif |
9 #if V8_OS_WIN32 | 12 #if V8_OS_WIN32 |
10 #include "src/base/win32-headers.h" | 13 #include "src/base/win32-headers.h" |
11 #endif | 14 #endif |
12 | 15 |
13 namespace v8 { | 16 namespace v8 { |
14 namespace base { | 17 namespace base { |
15 namespace bits { | 18 namespace bits { |
16 | 19 |
| 20 // CountSetBits32(value) returns the number of bits set in |value|. |
| 21 inline uint32_t CountSetBits32(uint32_t value) { |
| 22 #if V8_HAS_BUILTIN_POPCOUNT |
| 23 return __builtin_popcount(value); |
| 24 #else |
| 25 value = ((value >> 1) & 0x55555555) + (value & 0x55555555); |
| 26 value = ((value >> 2) & 0x33333333) + (value & 0x33333333); |
| 27 value = ((value >> 4) & 0x0f0f0f0f) + (value & 0x0f0f0f0f); |
| 28 value = ((value >> 8) & 0x00ff00ff) + (value & 0x00ff00ff); |
| 29 value = ((value >> 16) & 0x0000ffff) + (value & 0x0000ffff); |
| 30 return value; |
| 31 #endif |
| 32 } |
| 33 |
| 34 |
| 35 // CountLeadingZeros32(value) returns the number of zero bits following the most |
| 36 // significant 1 bit in |value| if |value| is non-zero, otherwise it returns 32. |
| 37 inline uint32_t CountLeadingZeros32(uint32_t value) { |
| 38 #if V8_HAS_BUILTIN_CLZ |
| 39 return value ? __builtin_clz(value) : 32; |
| 40 #elif V8_CC_MSVC |
| 41 unsigned long result; // NOLINT(runtime/int) |
| 42 if (!_BitScanReverse(&result, value)) return 32; |
| 43 return static_cast<uint32_t>(31 - result); |
| 44 #else |
| 45 value = value | (value >> 1); |
| 46 value = value | (value >> 2); |
| 47 value = value | (value >> 4); |
| 48 value = value | (value >> 8); |
| 49 value = value | (value >> 16); |
| 50 return CountSetBits32(~value); |
| 51 #endif |
| 52 } |
| 53 |
| 54 |
| 55 // CountTrailingZeros32(value) returns the number of zero bits preceding the |
| 56 // least significant 1 bit in |value| if |value| is non-zero, otherwise it |
| 57 // returns 32. |
| 58 inline uint32_t CountTrailingZeros32(uint32_t value) { |
| 59 #if V8_HAS_BUILTIN_CTZ |
| 60 return value ? __builtin_ctz(value) : 32; |
| 61 #elif V8_CC_MSVC |
| 62 unsigned long result; // NOLINT(runtime/int) |
| 63 if (!_BitScanForward(&result, value)) return 32; |
| 64 return static_cast<uint32_t>(result); |
| 65 #else |
| 66 if (value == 0) return 32; |
| 67 unsigned count = 0; |
| 68 for (value ^= value - 1; value >>= 1; ++count) |
| 69 ; |
| 70 return count; |
| 71 #endif |
| 72 } |
| 73 |
| 74 |
17 inline uint32_t RotateRight32(uint32_t value, uint32_t shift) { | 75 inline uint32_t RotateRight32(uint32_t value, uint32_t shift) { |
18 if (shift == 0) return value; | 76 if (shift == 0) return value; |
19 return (value >> shift) | (value << (32 - shift)); | 77 return (value >> shift) | (value << (32 - shift)); |
20 } | 78 } |
21 | 79 |
22 | 80 |
23 inline uint64_t RotateRight64(uint64_t value, uint64_t shift) { | 81 inline uint64_t RotateRight64(uint64_t value, uint64_t shift) { |
24 if (shift == 0) return value; | 82 if (shift == 0) return value; |
25 return (value >> shift) | (value << (64 - shift)); | 83 return (value >> shift) | (value << (64 - shift)); |
26 } | 84 } |
27 | 85 |
28 } // namespace bits | 86 } // namespace bits |
29 } // namespace base | 87 } // namespace base |
30 } // namespace v8 | 88 } // namespace v8 |
31 | 89 |
32 #endif // V8_BASE_BITS_H_ | 90 #endif // V8_BASE_BITS_H_ |
OLD | NEW |