Index: src/base/bits.h |
diff --git a/src/base/bits.h b/src/base/bits.h |
index f271df9067d736e8ac512a9d13b15b04840291d3..e6a733a45dbb24ce4c95a30a817b2de2b4ee2354 100644 |
--- a/src/base/bits.h |
+++ b/src/base/bits.h |
@@ -6,6 +6,7 @@ |
#define V8_BASE_BITS_H_ |
#include "include/v8stdint.h" |
+#include "src/base/macros.h" |
#if V8_CC_MSVC |
#include <intrin.h> |
#endif |
@@ -114,6 +115,34 @@ inline uint64_t RotateRight64(uint64_t value, uint64_t shift) { |
return (value >> shift) | (value << (64 - shift)); |
} |
+ |
+// SignedAddOverflow32(lhs,rhs,val) performs a signed summation of |lhs| and |
+// |rhs| and stores the result into the variable pointed to by |val| and |
+// returns true if the signed summation resulted in an overflow. |
+inline bool SignedAddOverflow32(int32_t lhs, int32_t rhs, int32_t* val) { |
+#if V8_HAS_BUILTIN_SADD_OVERFLOW |
+ return __builtin_sadd_overflow(lhs, rhs, val); |
+#else |
+ uint32_t res = static_cast<uint32_t>(lhs) + static_cast<uint32_t>(rhs); |
+ *val = bit_cast<int32_t>(res); |
+ return ((res ^ lhs) & (res ^ rhs) & (1U << 31)) != 0; |
+#endif |
+} |
+ |
+ |
+// SignedSubOverflow32(lhs,rhs,val) performs a signed subtraction of |lhs| and |
+// |rhs| and stores the result into the variable pointed to by |val| and |
+// returns true if the signed subtraction resulted in an overflow. |
+inline bool SignedSubOverflow32(int32_t lhs, int32_t rhs, int32_t* val) { |
+#if V8_HAS_BUILTIN_SSUB_OVERFLOW |
+ return __builtin_ssub_overflow(lhs, rhs, val); |
+#else |
+ uint32_t res = static_cast<uint32_t>(lhs) - static_cast<uint32_t>(rhs); |
+ *val = bit_cast<int32_t>(res); |
+ return ((res ^ lhs) & (res ^ ~rhs) & (1U << 31)) != 0; |
+#endif |
+} |
+ |
} // namespace bits |
} // namespace base |
} // namespace v8 |