Upgrade V8 to version 4.9.385.28

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

FPIIM-449

Change-Id: I4b2e74289d4bf3667f2f3dc8aa2e541f63e26eb4
diff --git a/src/runtime/runtime-maths.cc b/src/runtime/runtime-maths.cc
index 6397ad1..427d2b8 100644
--- a/src/runtime/runtime-maths.cc
+++ b/src/runtime/runtime-maths.cc
@@ -2,15 +2,15 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "src/v8.h"
+#include "src/runtime/runtime-utils.h"
 
 #include "src/arguments.h"
 #include "src/assembler.h"
+#include "src/base/utils/random-number-generator.h"
+#include "src/bootstrapper.h"
 #include "src/codegen.h"
-#include "src/runtime/runtime-utils.h"
 #include "src/third_party/fdlibm/fdlibm.h"
 
-
 namespace v8 {
 namespace internal {
 
@@ -34,9 +34,10 @@
   HandleScope scope(isolate);
   DCHECK(args.length() == 1);
   CONVERT_DOUBLE_ARG_CHECKED(x, 0);
-  uint64_t integer = double_to_uint64(x);
-  integer = (integer >> 32) & 0xFFFFFFFFu;
-  return *isolate->factory()->NewNumber(static_cast<int32_t>(integer));
+  uint64_t unsigned64 = double_to_uint64(x);
+  uint32_t unsigned32 = static_cast<uint32_t>(unsigned64 >> 32);
+  int32_t signed32 = bit_cast<int32_t, uint32_t>(unsigned32);
+  return *isolate->factory()->NewNumber(signed32);
 }
 
 
@@ -44,8 +45,10 @@
   HandleScope scope(isolate);
   DCHECK(args.length() == 1);
   CONVERT_DOUBLE_ARG_CHECKED(x, 0);
-  return *isolate->factory()->NewNumber(
-      static_cast<int32_t>(double_to_uint64(x) & 0xFFFFFFFFu));
+  uint64_t unsigned64 = double_to_uint64(x);
+  uint32_t unsigned32 = static_cast<uint32_t>(unsigned64);
+  int32_t signed32 = bit_cast<int32_t, uint32_t>(unsigned32);
+  return *isolate->factory()->NewNumber(signed32);
 }
 
 
@@ -60,19 +63,15 @@
 
 
 RUNTIME_FUNCTION(Runtime_RemPiO2) {
-  HandleScope handle_scope(isolate);
-  DCHECK(args.length() == 1);
+  SealHandleScope shs(isolate);
+  DisallowHeapAllocation no_gc;
+  DCHECK(args.length() == 2);
   CONVERT_DOUBLE_ARG_CHECKED(x, 0);
-  Factory* factory = isolate->factory();
-  double y[2] = {0.0, 0.0};
-  int n = fdlibm::rempio2(x, y);
-  Handle<FixedArray> array = factory->NewFixedArray(3);
-  Handle<HeapNumber> y0 = factory->NewHeapNumber(y[0]);
-  Handle<HeapNumber> y1 = factory->NewHeapNumber(y[1]);
-  array->set(0, Smi::FromInt(n));
-  array->set(1, *y0);
-  array->set(2, *y1);
-  return *factory->NewJSArrayWithElements(array);
+  CONVERT_ARG_CHECKED(JSTypedArray, result, 1);
+  RUNTIME_ASSERT(result->byte_length() == Smi::FromInt(2 * sizeof(double)));
+  FixedFloat64Array* array = FixedFloat64Array::cast(result->elements());
+  double* y = static_cast<double*>(array->DataPtr());
+  return Smi::FromInt(fdlibm::rempio2(x, y));
 }
 
 
@@ -108,12 +107,23 @@
   isolate->counters()->math_exp()->Increment();
 
   CONVERT_DOUBLE_ARG_CHECKED(x, 0);
-  lazily_initialize_fast_exp();
-  return *isolate->factory()->NewNumber(fast_exp(x));
+  lazily_initialize_fast_exp(isolate);
+  return *isolate->factory()->NewNumber(fast_exp(x, isolate));
 }
 
 
-RUNTIME_FUNCTION(Runtime_MathFloorRT) {
+RUNTIME_FUNCTION(Runtime_MathClz32) {
+  HandleScope scope(isolate);
+  DCHECK(args.length() == 1);
+  isolate->counters()->math_clz32()->Increment();
+
+  CONVERT_NUMBER_CHECKED(uint32_t, x, Uint32, args[0]);
+  return *isolate->factory()->NewNumberFromUint(
+      base::bits::CountLeadingZeros32(x));
+}
+
+
+RUNTIME_FUNCTION(Runtime_MathFloor) {
   HandleScope scope(isolate);
   DCHECK(args.length() == 1);
   isolate->counters()->math_floor()->Increment();
@@ -125,7 +135,7 @@
 
 // Slow version of Math.pow.  We check for fast paths for special cases.
 // Used if VFP3 is not available.
-RUNTIME_FUNCTION(Runtime_MathPowSlow) {
+RUNTIME_FUNCTION(Runtime_MathPow) {
   HandleScope scope(isolate);
   DCHECK(args.length() == 2);
   isolate->counters()->math_pow()->Increment();
@@ -140,7 +150,7 @@
   }
 
   CONVERT_DOUBLE_ARG_CHECKED(y, 1);
-  double result = power_helper(x, y);
+  double result = power_helper(isolate, x, y);
   if (std::isnan(result)) return isolate->heap()->nan_value();
   return *isolate->factory()->NewNumber(result);
 }
@@ -208,13 +218,14 @@
 }
 
 
-RUNTIME_FUNCTION(Runtime_MathSqrtRT) {
+RUNTIME_FUNCTION(Runtime_MathSqrt) {
   HandleScope scope(isolate);
   DCHECK(args.length() == 1);
   isolate->counters()->math_sqrt()->Increment();
 
   CONVERT_DOUBLE_ARG_CHECKED(x, 0);
-  return *isolate->factory()->NewNumber(fast_sqrt(x));
+  lazily_initialize_fast_sqrt(isolate);
+  return *isolate->factory()->NewNumber(fast_sqrt(x, isolate));
 }
 
 
@@ -228,13 +239,7 @@
 }
 
 
-RUNTIME_FUNCTION(RuntimeReference_MathPow) {
-  SealHandleScope shs(isolate);
-  return __RT_impl_Runtime_MathPowSlow(args, isolate);
-}
-
-
-RUNTIME_FUNCTION(RuntimeReference_IsMinusZero) {
+RUNTIME_FUNCTION(Runtime_IsMinusZero) {
   SealHandleScope shs(isolate);
   DCHECK(args.length() == 1);
   CONVERT_ARG_CHECKED(Object, obj, 0);
@@ -242,5 +247,52 @@
   HeapNumber* number = HeapNumber::cast(obj);
   return isolate->heap()->ToBoolean(IsMinusZero(number->value()));
 }
+
+
+RUNTIME_FUNCTION(Runtime_GenerateRandomNumbers) {
+  HandleScope scope(isolate);
+  DCHECK(args.length() == 1);
+  // Random numbers in the snapshot are not really that random.
+  DCHECK(!isolate->bootstrapper()->IsActive());
+  static const int kState0Offset = 0;
+  static const int kState1Offset = 1;
+  static const int kRandomBatchSize = 64;
+  CONVERT_ARG_HANDLE_CHECKED(Object, maybe_typed_array, 0);
+  Handle<JSTypedArray> typed_array;
+  // Allocate typed array if it does not yet exist.
+  if (maybe_typed_array->IsJSTypedArray()) {
+    typed_array = Handle<JSTypedArray>::cast(maybe_typed_array);
+  } else {
+    static const int kByteLength = kRandomBatchSize * kDoubleSize;
+    Handle<JSArrayBuffer> buffer =
+        isolate->factory()->NewJSArrayBuffer(SharedFlag::kNotShared, TENURED);
+    JSArrayBuffer::SetupAllocatingData(buffer, isolate, kByteLength, true,
+                                       SharedFlag::kNotShared);
+    typed_array = isolate->factory()->NewJSTypedArray(
+        kExternalFloat64Array, buffer, 0, kRandomBatchSize);
+  }
+
+  DisallowHeapAllocation no_gc;
+  double* array =
+      reinterpret_cast<double*>(typed_array->GetBuffer()->backing_store());
+  // Fetch existing state.
+  uint64_t state0 = double_to_uint64(array[kState0Offset]);
+  uint64_t state1 = double_to_uint64(array[kState1Offset]);
+  // Initialize state if not yet initialized.
+  while (state0 == 0 || state1 == 0) {
+    isolate->random_number_generator()->NextBytes(&state0, sizeof(state0));
+    isolate->random_number_generator()->NextBytes(&state1, sizeof(state1));
+  }
+  // Create random numbers.
+  for (int i = kState1Offset + 1; i < kRandomBatchSize; i++) {
+    // Generate random numbers using xorshift128+.
+    base::RandomNumberGenerator::XorShift128(&state0, &state1);
+    array[i] = base::RandomNumberGenerator::ToDouble(state0, state1);
+  }
+  // Persist current state.
+  array[kState0Offset] = uint64_to_double(state0);
+  array[kState1Offset] = uint64_to_double(state1);
+  return *typed_array;
 }
-}  // namespace v8::internal
+}  // namespace internal
+}  // namespace v8