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 | 9 #if V8_CC_MSVC |
10 #include <intrin.h> | 10 #include <intrin.h> |
11 #endif | 11 #endif |
12 #if V8_OS_WIN32 | 12 #if V8_OS_WIN32 |
13 #include "src/base/win32-headers.h" | 13 #include "src/base/win32-headers.h" |
14 #endif | 14 #endif |
15 | 15 |
16 namespace v8 { | 16 namespace v8 { |
17 namespace base { | 17 namespace base { |
18 namespace bits { | 18 namespace bits { |
19 | 19 |
20 // CountSetBits32(value) returns the number of bits set in |value|. | 20 // CountPopulation32(value) returns the number of bits set in |value|. |
21 inline uint32_t CountSetBits32(uint32_t value) { | 21 inline uint32_t CountPopulation32(uint32_t value) { |
22 #if V8_HAS_BUILTIN_POPCOUNT | 22 #if V8_HAS_BUILTIN_POPCOUNT |
23 return __builtin_popcount(value); | 23 return __builtin_popcount(value); |
24 #else | 24 #else |
25 value = ((value >> 1) & 0x55555555) + (value & 0x55555555); | 25 value = ((value >> 1) & 0x55555555) + (value & 0x55555555); |
26 value = ((value >> 2) & 0x33333333) + (value & 0x33333333); | 26 value = ((value >> 2) & 0x33333333) + (value & 0x33333333); |
27 value = ((value >> 4) & 0x0f0f0f0f) + (value & 0x0f0f0f0f); | 27 value = ((value >> 4) & 0x0f0f0f0f) + (value & 0x0f0f0f0f); |
28 value = ((value >> 8) & 0x00ff00ff) + (value & 0x00ff00ff); | 28 value = ((value >> 8) & 0x00ff00ff) + (value & 0x00ff00ff); |
29 value = ((value >> 16) & 0x0000ffff) + (value & 0x0000ffff); | 29 value = ((value >> 16) & 0x0000ffff) + (value & 0x0000ffff); |
30 return value; | 30 return value; |
31 #endif | 31 #endif |
32 } | 32 } |
33 | 33 |
34 | 34 |
35 // CountLeadingZeros32(value) returns the number of zero bits following the most | 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. | 36 // significant 1 bit in |value| if |value| is non-zero, otherwise it returns 32. |
37 inline uint32_t CountLeadingZeros32(uint32_t value) { | 37 inline uint32_t CountLeadingZeros32(uint32_t value) { |
38 #if V8_HAS_BUILTIN_CLZ | 38 #if V8_HAS_BUILTIN_CLZ |
39 return value ? __builtin_clz(value) : 32; | 39 return value ? __builtin_clz(value) : 32; |
40 #elif V8_CC_MSVC | 40 #elif V8_CC_MSVC |
41 unsigned long result; // NOLINT(runtime/int) | 41 unsigned long result; // NOLINT(runtime/int) |
42 if (!_BitScanReverse(&result, value)) return 32; | 42 if (!_BitScanReverse(&result, value)) return 32; |
43 return static_cast<uint32_t>(31 - result); | 43 return static_cast<uint32_t>(31 - result); |
44 #else | 44 #else |
45 value = value | (value >> 1); | 45 value = value | (value >> 1); |
46 value = value | (value >> 2); | 46 value = value | (value >> 2); |
47 value = value | (value >> 4); | 47 value = value | (value >> 4); |
48 value = value | (value >> 8); | 48 value = value | (value >> 8); |
49 value = value | (value >> 16); | 49 value = value | (value >> 16); |
50 return CountSetBits32(~value); | 50 return CountPopulation32(~value); |
51 #endif | 51 #endif |
52 } | 52 } |
53 | 53 |
54 | 54 |
55 // CountTrailingZeros32(value) returns the number of zero bits preceding the | 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 | 56 // least significant 1 bit in |value| if |value| is non-zero, otherwise it |
57 // returns 32. | 57 // returns 32. |
58 inline uint32_t CountTrailingZeros32(uint32_t value) { | 58 inline uint32_t CountTrailingZeros32(uint32_t value) { |
59 #if V8_HAS_BUILTIN_CTZ | 59 #if V8_HAS_BUILTIN_CTZ |
60 return value ? __builtin_ctz(value) : 32; | 60 return value ? __builtin_ctz(value) : 32; |
(...skipping 20 matching lines...) Expand all Loading... |
81 inline uint64_t RotateRight64(uint64_t value, uint64_t shift) { | 81 inline uint64_t RotateRight64(uint64_t value, uint64_t shift) { |
82 if (shift == 0) return value; | 82 if (shift == 0) return value; |
83 return (value >> shift) | (value << (64 - shift)); | 83 return (value >> shift) | (value << (64 - shift)); |
84 } | 84 } |
85 | 85 |
86 } // namespace bits | 86 } // namespace bits |
87 } // namespace base | 87 } // namespace base |
88 } // namespace v8 | 88 } // namespace v8 |
89 | 89 |
90 #endif // V8_BASE_BITS_H_ | 90 #endif // V8_BASE_BITS_H_ |
OLD | NEW |