Merge V8 5.2.361.47  DO NOT MERGE

https://chromium.googlesource.com/v8/v8/+/5.2.361.47

FPIIM-449

Change-Id: Ibec421b85a9b88cb3a432ada642e469fe7e78346
(cherry picked from commit bcf72ee8e3b26f1d0726869c7ddb3921c68b09a8)
diff --git a/src/base/bits.cc b/src/base/bits.cc
index 74d747f..9b949cc 100644
--- a/src/base/bits.cc
+++ b/src/base/bits.cc
@@ -7,6 +7,7 @@
 #include <limits>
 
 #include "src/base/logging.h"
+#include "src/base/safe_math.h"
 
 namespace v8 {
 namespace base {
@@ -48,6 +49,35 @@
   return lhs % rhs;
 }
 
+
+int64_t FromCheckedNumeric(const internal::CheckedNumeric<int64_t> value) {
+  if (value.IsValid())
+    return value.ValueUnsafe();
+
+  // We could return max/min but we don't really expose what the maximum delta
+  // is. Instead, return max/(-max), which is something that clients can reason
+  // about.
+  // TODO(rvargas) crbug.com/332611: don't use internal values.
+  int64_t limit = std::numeric_limits<int64_t>::max();
+  if (value.validity() == internal::RANGE_UNDERFLOW)
+    limit = -limit;
+  return value.ValueOrDefault(limit);
+}
+
+
+int64_t SignedSaturatedAdd64(int64_t lhs, int64_t rhs) {
+  internal::CheckedNumeric<int64_t> rv(lhs);
+  rv += rhs;
+  return FromCheckedNumeric(rv);
+}
+
+
+int64_t SignedSaturatedSub64(int64_t lhs, int64_t rhs) {
+  internal::CheckedNumeric<int64_t> rv(lhs);
+  rv -= rhs;
+  return FromCheckedNumeric(rv);
+}
+
 }  // namespace bits
 }  // namespace base
 }  // namespace v8