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 <stdint.h> | 8 #include <stdint.h> |
9 #include "src/base/macros.h" | 9 #include "src/base/macros.h" |
10 #if V8_CC_MSVC | 10 #if V8_CC_MSVC |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 value = value | (value >> 2); | 85 value = value | (value >> 2); |
86 value = value | (value >> 4); | 86 value = value | (value >> 4); |
87 value = value | (value >> 8); | 87 value = value | (value >> 8); |
88 value = value | (value >> 16); | 88 value = value | (value >> 16); |
89 value = value | (value >> 32); | 89 value = value | (value >> 32); |
90 return CountPopulation64(~value); | 90 return CountPopulation64(~value); |
91 #endif | 91 #endif |
92 } | 92 } |
93 | 93 |
94 | 94 |
| 95 // ReverseBits(value) returns |value| in reverse bit order. |
| 96 template <typename T> |
| 97 T ReverseBits(T value) { |
| 98 DCHECK((sizeof(value) == 1) || (sizeof(value) == 2) || (sizeof(value) == 4) || |
| 99 (sizeof(value) == 8)); |
| 100 T result = 0; |
| 101 for (unsigned i = 0; i < (sizeof(value) * 8); i++) { |
| 102 result = (result << 1) | (value & 1); |
| 103 value >>= 1; |
| 104 } |
| 105 return result; |
| 106 } |
| 107 |
| 108 |
95 // CountTrailingZeros32(value) returns the number of zero bits preceding the | 109 // CountTrailingZeros32(value) returns the number of zero bits preceding the |
96 // least significant 1 bit in |value| if |value| is non-zero, otherwise it | 110 // least significant 1 bit in |value| if |value| is non-zero, otherwise it |
97 // returns 32. | 111 // returns 32. |
98 inline unsigned CountTrailingZeros32(uint32_t value) { | 112 inline unsigned CountTrailingZeros32(uint32_t value) { |
99 #if V8_HAS_BUILTIN_CTZ | 113 #if V8_HAS_BUILTIN_CTZ |
100 return value ? __builtin_ctz(value) : 32; | 114 return value ? __builtin_ctz(value) : 32; |
101 #elif V8_CC_MSVC | 115 #elif V8_CC_MSVC |
102 unsigned long result; // NOLINT(runtime/int) | 116 unsigned long result; // NOLINT(runtime/int) |
103 if (!_BitScanForward(&result, value)) return 32; | 117 if (!_BitScanForward(&result, value)) return 32; |
104 return static_cast<unsigned>(result); | 118 return static_cast<unsigned>(result); |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 // truncated to uint32. If |rhs| is zero, then zero is returned. | 294 // truncated to uint32. If |rhs| is zero, then zero is returned. |
281 inline uint32_t UnsignedMod32(uint32_t lhs, uint32_t rhs) { | 295 inline uint32_t UnsignedMod32(uint32_t lhs, uint32_t rhs) { |
282 return rhs ? lhs % rhs : 0u; | 296 return rhs ? lhs % rhs : 0u; |
283 } | 297 } |
284 | 298 |
285 } // namespace bits | 299 } // namespace bits |
286 } // namespace base | 300 } // namespace base |
287 } // namespace v8 | 301 } // namespace v8 |
288 | 302 |
289 #endif // V8_BASE_BITS_H_ | 303 #endif // V8_BASE_BITS_H_ |
OLD | NEW |