Chromium Code Reviews| 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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 // less than or equal to |value|. If you pass in a |value| that is already a | 141 // less than or equal to |value|. If you pass in a |value| that is already a |
| 142 // power of two, it is returned as is. | 142 // power of two, it is returned as is. |
| 143 inline uint32_t RoundDownToPowerOfTwo32(uint32_t value) { | 143 inline uint32_t RoundDownToPowerOfTwo32(uint32_t value) { |
| 144 if (value > 0x80000000u) return 0x80000000u; | 144 if (value > 0x80000000u) return 0x80000000u; |
| 145 uint32_t result = RoundUpToPowerOfTwo32(value); | 145 uint32_t result = RoundUpToPowerOfTwo32(value); |
| 146 if (result > value) result >>= 1; | 146 if (result > value) result >>= 1; |
| 147 return result; | 147 return result; |
| 148 } | 148 } |
| 149 | 149 |
| 150 | 150 |
| 151 // Precondition: 0 <= shift < 32 | |
|
hans
2015/03/04 06:02:51
Any reason the preconditions are not some kind of
Nico
2015/03/04 06:21:10
I was worried that this might be performance-criti
| |
| 151 inline uint32_t RotateRight32(uint32_t value, uint32_t shift) { | 152 inline uint32_t RotateRight32(uint32_t value, uint32_t shift) { |
| 152 if (shift == 0) return value; | 153 if (shift == 0) return value; |
| 153 return (value >> shift) | (value << (32 - shift)); | 154 return (value >> shift) | (value << (32 - shift)); |
| 154 } | 155 } |
| 155 | 156 |
| 157 // Precondition: 0 <= shift < 32 | |
| 158 inline uint32_t RotateLeft32(uint32_t value, uint32_t shift) { | |
| 159 if (shift == 0) return value; | |
| 160 return (value << shift) | (value >> (32 - shift)); | |
| 161 } | |
| 156 | 162 |
| 163 // Precondition: 0 <= shift < 64 | |
| 157 inline uint64_t RotateRight64(uint64_t value, uint64_t shift) { | 164 inline uint64_t RotateRight64(uint64_t value, uint64_t shift) { |
| 158 if (shift == 0) return value; | 165 if (shift == 0) return value; |
| 159 return (value >> shift) | (value << (64 - shift)); | 166 return (value >> shift) | (value << (64 - shift)); |
| 160 } | 167 } |
| 161 | 168 |
| 169 // Precondition: 0 <= shift < 64 | |
| 170 inline uint64_t RotateLeft64(uint64_t value, uint64_t shift) { | |
| 171 if (shift == 0) return value; | |
| 172 return (value << shift) | (value >> (64 - shift)); | |
| 173 } | |
| 174 | |
| 162 | 175 |
| 163 // SignedAddOverflow32(lhs,rhs,val) performs a signed summation of |lhs| and | 176 // SignedAddOverflow32(lhs,rhs,val) performs a signed summation of |lhs| and |
| 164 // |rhs| and stores the result into the variable pointed to by |val| and | 177 // |rhs| and stores the result into the variable pointed to by |val| and |
| 165 // returns true if the signed summation resulted in an overflow. | 178 // returns true if the signed summation resulted in an overflow. |
| 166 inline bool SignedAddOverflow32(int32_t lhs, int32_t rhs, int32_t* val) { | 179 inline bool SignedAddOverflow32(int32_t lhs, int32_t rhs, int32_t* val) { |
| 167 #if V8_HAS_BUILTIN_SADD_OVERFLOW | 180 #if V8_HAS_BUILTIN_SADD_OVERFLOW |
| 168 return __builtin_sadd_overflow(lhs, rhs, val); | 181 return __builtin_sadd_overflow(lhs, rhs, val); |
| 169 #else | 182 #else |
| 170 uint32_t res = static_cast<uint32_t>(lhs) + static_cast<uint32_t>(rhs); | 183 uint32_t res = static_cast<uint32_t>(lhs) + static_cast<uint32_t>(rhs); |
| 171 *val = bit_cast<int32_t>(res); | 184 *val = bit_cast<int32_t>(res); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 // truncated to uint32. If |rhs| is zero, then zero is returned. | 236 // truncated to uint32. If |rhs| is zero, then zero is returned. |
| 224 inline uint32_t UnsignedMod32(uint32_t lhs, uint32_t rhs) { | 237 inline uint32_t UnsignedMod32(uint32_t lhs, uint32_t rhs) { |
| 225 return rhs ? lhs % rhs : 0u; | 238 return rhs ? lhs % rhs : 0u; |
| 226 } | 239 } |
| 227 | 240 |
| 228 } // namespace bits | 241 } // namespace bits |
| 229 } // namespace base | 242 } // namespace base |
| 230 } // namespace v8 | 243 } // namespace v8 |
| 231 | 244 |
| 232 #endif // V8_BASE_BITS_H_ | 245 #endif // V8_BASE_BITS_H_ |
| OLD | NEW |