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 "include/v8stdint.h" | 8 #include "include/v8stdint.h" |
| 9 #include "src/base/macros.h" | |
| 9 #if V8_CC_MSVC | 10 #if V8_CC_MSVC |
| 10 #include <intrin.h> | 11 #include <intrin.h> |
| 11 #endif | 12 #endif |
| 12 #if V8_OS_WIN32 | 13 #if V8_OS_WIN32 |
| 13 #include "src/base/win32-headers.h" | 14 #include "src/base/win32-headers.h" |
| 14 #endif | 15 #endif |
| 15 | 16 |
| 16 namespace v8 { | 17 namespace v8 { |
| 17 namespace base { | 18 namespace base { |
| 18 namespace bits { | 19 namespace bits { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 if (shift == 0) return value; | 108 if (shift == 0) return value; |
| 108 return (value >> shift) | (value << (32 - shift)); | 109 return (value >> shift) | (value << (32 - shift)); |
| 109 } | 110 } |
| 110 | 111 |
| 111 | 112 |
| 112 inline uint64_t RotateRight64(uint64_t value, uint64_t shift) { | 113 inline uint64_t RotateRight64(uint64_t value, uint64_t shift) { |
| 113 if (shift == 0) return value; | 114 if (shift == 0) return value; |
| 114 return (value >> shift) | (value << (64 - shift)); | 115 return (value >> shift) | (value << (64 - shift)); |
| 115 } | 116 } |
| 116 | 117 |
| 118 | |
| 119 // SignedAddOverflow32(lhs,rhs,val) performs a signed summation of |lhs| and | |
| 120 // |rhs| and stores the result into the variable pointed to by |val| and | |
| 121 // returns true if the signed summation resulted in an overflow. | |
| 122 inline bool SignedAddOverflow32(int32_t lhs, int32_t rhs, int32_t* val) { | |
| 123 #if V8_HAS_BUILTIN_SADD_OVERFLOW | |
| 124 return __builtin_sadd_overflow(lhs, rhs, val); | |
| 125 #else | |
| 126 uint32_t res = static_cast<uint32_t>(lhs) + static_cast<uint32_t>(rhs); | |
| 127 *val = bit_cast<int32_t>(res); | |
| 128 return (((res ^ lhs) & (res ^ rhs)) >> 31) & 1; | |
|
Sven Panne
2014/09/09 13:10:48
I think that
return (((res ^ lhs) & (res ^ rhs
Benedikt Meurer
2014/09/09 14:15:23
Done.
| |
| 129 #endif | |
| 130 } | |
| 131 | |
| 132 | |
| 133 // SignedSubOverflow32(lhs,rhs,val) performs a signed subtraction of |lhs| and | |
| 134 // |rhs| and stores the result into the variable pointed to by |val| and | |
| 135 // returns true if the signed subtraction resulted in an overflow. | |
| 136 inline bool SignedSubOverflow32(int32_t lhs, int32_t rhs, int32_t* val) { | |
| 137 #if V8_HAS_BUILTIN_SSUB_OVERFLOW | |
| 138 return __builtin_ssub_overflow(lhs, rhs, val); | |
| 139 #else | |
| 140 uint32_t res = static_cast<uint32_t>(lhs) - static_cast<uint32_t>(rhs); | |
| 141 *val = bit_cast<int32_t>(res); | |
| 142 return (((res ^ lhs) & (res ^ ~rhs)) >> 31) & 1; | |
| 143 #endif | |
| 144 } | |
| 145 | |
| 117 } // namespace bits | 146 } // namespace bits |
| 118 } // namespace base | 147 } // namespace base |
| 119 } // namespace v8 | 148 } // namespace v8 |
| 120 | 149 |
| 121 #endif // V8_BASE_BITS_H_ | 150 #endif // V8_BASE_BITS_H_ |
| OLD | NEW |