| 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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 DCHECK((sizeof(value) == 1) || (sizeof(value) == 2) || (sizeof(value) == 4) || | 104 DCHECK((sizeof(value) == 1) || (sizeof(value) == 2) || (sizeof(value) == 4) || |
| 105 (sizeof(value) == 8)); | 105 (sizeof(value) == 8)); |
| 106 T result = 0; | 106 T result = 0; |
| 107 for (unsigned i = 0; i < (sizeof(value) * 8); i++) { | 107 for (unsigned i = 0; i < (sizeof(value) * 8); i++) { |
| 108 result = (result << 1) | (value & 1); | 108 result = (result << 1) | (value & 1); |
| 109 value >>= 1; | 109 value >>= 1; |
| 110 } | 110 } |
| 111 return result; | 111 return result; |
| 112 } | 112 } |
| 113 | 113 |
| 114 | |
| 115 // CountTrailingZeros32(value) returns the number of zero bits preceding the | 114 // CountTrailingZeros32(value) returns the number of zero bits preceding the |
| 116 // least significant 1 bit in |value| if |value| is non-zero, otherwise it | 115 // least significant 1 bit in |value| if |value| is non-zero, otherwise it |
| 117 // returns 32. | 116 // returns 32. |
| 118 inline unsigned CountTrailingZeros32(uint32_t value) { | 117 inline unsigned CountTrailingZeros32(uint32_t value) { |
| 119 #if V8_HAS_BUILTIN_CTZ | 118 #if V8_HAS_BUILTIN_CTZ |
| 120 return value ? __builtin_ctz(value) : 32; | 119 return value ? __builtin_ctz(value) : 32; |
| 121 #elif V8_CC_MSVC | 120 #elif V8_CC_MSVC |
| 122 unsigned long result; // NOLINT(runtime/int) | 121 unsigned long result; // NOLINT(runtime/int) |
| 123 if (!_BitScanForward(&result, value)) return 32; | 122 if (!_BitScanForward(&result, value)) return 32; |
| 124 return static_cast<unsigned>(result); | 123 return static_cast<unsigned>(result); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 140 return value ? __builtin_ctzll(value) : 64; | 139 return value ? __builtin_ctzll(value) : 64; |
| 141 #else | 140 #else |
| 142 if (value == 0) return 64; | 141 if (value == 0) return 64; |
| 143 unsigned count = 0; | 142 unsigned count = 0; |
| 144 for (value ^= value - 1; value >>= 1; ++count) { | 143 for (value ^= value - 1; value >>= 1; ++count) { |
| 145 } | 144 } |
| 146 return count; | 145 return count; |
| 147 #endif | 146 #endif |
| 148 } | 147 } |
| 149 | 148 |
| 149 // Overloaded versions of CountTrailingZeros32/64. |
| 150 inline unsigned CountTrailingZeros(uint32_t value) { |
| 151 return CountTrailingZeros32(value); |
| 152 } |
| 153 |
| 154 inline unsigned CountTrailingZeros(uint64_t value) { |
| 155 return CountTrailingZeros64(value); |
| 156 } |
| 150 | 157 |
| 151 // Returns true iff |value| is a power of 2. | 158 // Returns true iff |value| is a power of 2. |
| 152 inline bool IsPowerOfTwo32(uint32_t value) { | 159 inline bool IsPowerOfTwo32(uint32_t value) { |
| 153 return value && !(value & (value - 1)); | 160 return value && !(value & (value - 1)); |
| 154 } | 161 } |
| 155 | 162 |
| 156 | 163 |
| 157 // Returns true iff |value| is a power of 2. | 164 // Returns true iff |value| is a power of 2. |
| 158 inline bool IsPowerOfTwo64(uint64_t value) { | 165 inline bool IsPowerOfTwo64(uint64_t value) { |
| 159 return value && !(value & (value - 1)); | 166 return value && !(value & (value - 1)); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 // SignedSaturatedSub64(lhs, rhs) substracts |lhs| by |rhs|, | 322 // SignedSaturatedSub64(lhs, rhs) substracts |lhs| by |rhs|, |
| 316 // checks and returns the result. | 323 // checks and returns the result. |
| 317 int64_t SignedSaturatedSub64(int64_t lhs, int64_t rhs); | 324 int64_t SignedSaturatedSub64(int64_t lhs, int64_t rhs); |
| 318 | 325 |
| 319 | 326 |
| 320 } // namespace bits | 327 } // namespace bits |
| 321 } // namespace base | 328 } // namespace base |
| 322 } // namespace v8 | 329 } // namespace v8 |
| 323 | 330 |
| 324 #endif // V8_BASE_BITS_H_ | 331 #endif // V8_BASE_BITS_H_ |
| OLD | NEW |