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 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 inline bool SignedSubOverflow32(int32_t lhs, int32_t rhs, int32_t* val) { | 231 inline bool SignedSubOverflow32(int32_t lhs, int32_t rhs, int32_t* val) { |
232 #if V8_HAS_BUILTIN_SSUB_OVERFLOW | 232 #if V8_HAS_BUILTIN_SSUB_OVERFLOW |
233 return __builtin_ssub_overflow(lhs, rhs, val); | 233 return __builtin_ssub_overflow(lhs, rhs, val); |
234 #else | 234 #else |
235 uint32_t res = static_cast<uint32_t>(lhs) - static_cast<uint32_t>(rhs); | 235 uint32_t res = static_cast<uint32_t>(lhs) - static_cast<uint32_t>(rhs); |
236 *val = bit_cast<int32_t>(res); | 236 *val = bit_cast<int32_t>(res); |
237 return ((res ^ lhs) & (res ^ ~rhs) & (1U << 31)) != 0; | 237 return ((res ^ lhs) & (res ^ ~rhs) & (1U << 31)) != 0; |
238 #endif | 238 #endif |
239 } | 239 } |
240 | 240 |
| 241 // SignedMulOverflow32(lhs,rhs,val) performs a signed multiplication of |lhs| |
| 242 // and |rhs| and stores the result into the variable pointed to by |val| and |
| 243 // returns true if the signed multiplication resulted in an overflow. |
| 244 bool SignedMulOverflow32(int32_t lhs, int32_t rhs, int32_t* val); |
241 | 245 |
242 // SignedAddOverflow64(lhs,rhs,val) performs a signed summation of |lhs| and | 246 // SignedAddOverflow64(lhs,rhs,val) performs a signed summation of |lhs| and |
243 // |rhs| and stores the result into the variable pointed to by |val| and | 247 // |rhs| and stores the result into the variable pointed to by |val| and |
244 // returns true if the signed summation resulted in an overflow. | 248 // returns true if the signed summation resulted in an overflow. |
245 inline bool SignedAddOverflow64(int64_t lhs, int64_t rhs, int64_t* val) { | 249 inline bool SignedAddOverflow64(int64_t lhs, int64_t rhs, int64_t* val) { |
246 uint64_t res = static_cast<uint64_t>(lhs) + static_cast<uint64_t>(rhs); | 250 uint64_t res = static_cast<uint64_t>(lhs) + static_cast<uint64_t>(rhs); |
247 *val = bit_cast<int64_t>(res); | 251 *val = bit_cast<int64_t>(res); |
248 return ((res ^ lhs) & (res ^ rhs) & (1ULL << 63)) != 0; | 252 return ((res ^ lhs) & (res ^ rhs) & (1ULL << 63)) != 0; |
249 } | 253 } |
250 | 254 |
251 | 255 |
252 // SignedSubOverflow64(lhs,rhs,val) performs a signed subtraction of |lhs| and | 256 // SignedSubOverflow64(lhs,rhs,val) performs a signed subtraction of |lhs| and |
253 // |rhs| and stores the result into the variable pointed to by |val| and | 257 // |rhs| and stores the result into the variable pointed to by |val| and |
254 // returns true if the signed subtraction resulted in an overflow. | 258 // returns true if the signed subtraction resulted in an overflow. |
255 inline bool SignedSubOverflow64(int64_t lhs, int64_t rhs, int64_t* val) { | 259 inline bool SignedSubOverflow64(int64_t lhs, int64_t rhs, int64_t* val) { |
256 uint64_t res = static_cast<uint64_t>(lhs) - static_cast<uint64_t>(rhs); | 260 uint64_t res = static_cast<uint64_t>(lhs) - static_cast<uint64_t>(rhs); |
257 *val = bit_cast<int64_t>(res); | 261 *val = bit_cast<int64_t>(res); |
258 return ((res ^ lhs) & (res ^ ~rhs) & (1ULL << 63)) != 0; | 262 return ((res ^ lhs) & (res ^ ~rhs) & (1ULL << 63)) != 0; |
259 } | 263 } |
260 | 264 |
| 265 // SignedMulOverflow64(lhs,rhs,val) performs a signed multiplication of |lhs| |
| 266 // and |rhs| and stores the result into the variable pointed to by |val| and |
| 267 // returns true if the signed multiplication resulted in an overflow. |
| 268 bool SignedMulOverflow64(int64_t lhs, int64_t rhs, int64_t* val); |
261 | 269 |
262 // SignedMulHigh32(lhs, rhs) multiplies two signed 32-bit values |lhs| and | 270 // SignedMulHigh32(lhs, rhs) multiplies two signed 32-bit values |lhs| and |
263 // |rhs|, extracts the most significant 32 bits of the result, and returns | 271 // |rhs|, extracts the most significant 32 bits of the result, and returns |
264 // those. | 272 // those. |
265 int32_t SignedMulHigh32(int32_t lhs, int32_t rhs); | 273 int32_t SignedMulHigh32(int32_t lhs, int32_t rhs); |
266 | 274 |
267 | 275 |
268 // SignedMulHighAndAdd32(lhs, rhs, acc) multiplies two signed 32-bit values | 276 // SignedMulHighAndAdd32(lhs, rhs, acc) multiplies two signed 32-bit values |
269 // |lhs| and |rhs|, extracts the most significant 32 bits of the result, and | 277 // |lhs| and |rhs|, extracts the most significant 32 bits of the result, and |
270 // adds the accumulate value |acc|. | 278 // adds the accumulate value |acc|. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 // SignedSaturatedSub64(lhs, rhs) substracts |lhs| by |rhs|, | 330 // SignedSaturatedSub64(lhs, rhs) substracts |lhs| by |rhs|, |
323 // checks and returns the result. | 331 // checks and returns the result. |
324 int64_t SignedSaturatedSub64(int64_t lhs, int64_t rhs); | 332 int64_t SignedSaturatedSub64(int64_t lhs, int64_t rhs); |
325 | 333 |
326 | 334 |
327 } // namespace bits | 335 } // namespace bits |
328 } // namespace base | 336 } // namespace base |
329 } // namespace v8 | 337 } // namespace v8 |
330 | 338 |
331 #endif // V8_BASE_BITS_H_ | 339 #endif // V8_BASE_BITS_H_ |
OLD | NEW |