Inline Math.random in crankshaft (ia32).
authoryangguo@chromium.org <yangguo@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Tue, 6 Mar 2012 11:31:51 +0000 (11:31 +0000)
committeryangguo@chromium.org <yangguo@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Tue, 6 Mar 2012 11:31:51 +0000 (11:31 +0000)
BUG=
TEST=cctest/test-random.cc

Review URL: https://chromiumcodereview.appspot.com/9615008

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@10939 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/ia32/lithium-codegen-ia32.cc
src/ia32/lithium-codegen-ia32.h
test/cctest/SConscript
test/cctest/cctest.gyp
test/cctest/test-random.cc [new file with mode: 0644]

index 6ddc024258940f2776c0b400105a6dd9b56dddfe..ab2a95963627d3b3e066756c26bd2f421b7a5461 100644 (file)
@@ -3050,16 +3050,62 @@ void LCodeGen::DoPower(LPower* instr) {
 
 
 void LCodeGen::DoRandom(LRandom* instr) {
+  class DeferredDoRandom: public LDeferredCode {
+   public:
+    DeferredDoRandom(LCodeGen* codegen, LRandom* instr)
+        : LDeferredCode(codegen), instr_(instr) { }
+    virtual void Generate() { codegen()->DoDeferredRandom(instr_); }
+    virtual LInstruction* instr() { return instr_; }
+   private:
+    LRandom* instr_;
+  };
+
+  DeferredDoRandom* deferred = new DeferredDoRandom(this, instr);
+
   // Having marked this instruction as a call we can use any
   // registers.
   ASSERT(ToDoubleRegister(instr->result()).is(xmm1));
   ASSERT(ToRegister(instr->InputAt(0)).is(eax));
+  // Assert that the register size is indeed the size of each seed.
+  static const int kSeedSize = sizeof(uint32_t);
+  STATIC_ASSERT(kPointerSize == kSeedSize);
 
-  __ PrepareCallCFunction(1, ebx);
   __ mov(eax, FieldOperand(eax, GlobalObject::kGlobalContextOffset));
-  __ mov(Operand(esp, 0), eax);
-  __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
+  static const int kRandomSeedOffset =
+      FixedArray::kHeaderSize + Context::RANDOM_SEED_INDEX * kPointerSize;
+  __ mov(ebx, FieldOperand(eax, kRandomSeedOffset));
+  // ebx: FixedArray of the global context's random seeds
+
+  // Load state[0].
+  __ mov(ecx, FieldOperand(ebx, ByteArray::kHeaderSize));
+  // If state[0] == 0, call runtime to initialize seeds.
+  __ test(ecx, ecx);
+  __ j(zero, deferred->entry());
+
+  // state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16)
+  __ movzx_w(edx, ecx);
+  __ imul(edx, edx, 18273);
+  __ shr(ecx, 16);
+  __ add(ecx, edx);
+  __ mov(FieldOperand(ebx, ByteArray::kHeaderSize), ecx);
+
+  // Load state[1].
+  // state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16);
+  __ mov(eax, FieldOperand(ebx, ByteArray::kHeaderSize + kSeedSize));
+  __ movzx_w(edx, eax);
+  __ imul(edx, edx, 36969);
+  __ shr(eax, 16);
+  __ add(eax, edx);
+  __ mov(FieldOperand(ebx, ByteArray::kHeaderSize + kSeedSize), eax);
+
+  // ecx: state[0]
+  // eax: state[1]
+  // Random bit pattern = (state[0] << 14) + (state[1] & 0x3FFFF)
+  __ shl(ecx, 14);
+  __ and_(eax, Immediate(0x3FFFF));
+  __ add(eax, ecx);
 
+  __ bind(deferred->exit());
   // Convert 32 random bits in eax to 0.(32 random bits) in a double
   // by computing:
   // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
@@ -3072,6 +3118,14 @@ void LCodeGen::DoRandom(LRandom* instr) {
 }
 
 
+void LCodeGen::DoDeferredRandom(LRandom* instr) {
+  __ PrepareCallCFunction(1, ebx);
+  __ mov(Operand(esp, 0), eax);
+  __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
+  // Return value is in eax.
+}
+
+
 void LCodeGen::DoMathLog(LUnaryMathOperation* instr) {
   ASSERT(instr->value()->Equals(instr->result()));
   XMMRegister input_reg = ToDoubleRegister(instr->value());
index baf203630381725a6defb832e35e29c249ec3e29..481a2ae7a5adf3b30bb269db62b1a3d5dc13ca6d 100644 (file)
@@ -105,6 +105,7 @@ class LCodeGen BASE_EMBEDDED {
   void DoDeferredTaggedToI(LTaggedToI* instr);
   void DoDeferredMathAbsTaggedHeapNumber(LUnaryMathOperation* instr);
   void DoDeferredStackCheck(LStackCheck* instr);
+  void DoDeferredRandom(LRandom* instr);
   void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
   void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
   void DoDeferredAllocateObject(LAllocateObject* instr);
index edb859e9cbc64da3e8bf0b23bdab7c143a4eaced..e512a33935ec83a2e6b3b0a4ad173dc17eafa51d 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright 2008 the V8 project authors. All rights reserved.
+# Copyright 2012 the V8 project authors. All rights reserved.
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions are
 # met:
@@ -86,6 +86,7 @@ SOURCES = {
     'test-parsing.cc',
     'test-platform-tls.cc',
     'test-profile-generator.cc',
+    'test-random.cc',
     'test-regexp.cc',
     'test-reloc-info.cc',
     'test-serialize.cc',
index 3b8f4f653d759b842c08f2f56c86613471e3d0a1..d8a8fc45f6a5c245531643eedb678df10d734fc2 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright 2011 the V8 project authors. All rights reserved.
+# Copyright 2012 the V8 project authors. All rights reserved.
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions are
 # met:
@@ -81,6 +81,7 @@
         'test-parsing.cc',
         'test-platform-tls.cc',
         'test-profile-generator.cc',
+        'test-random.cc',
         'test-regexp.cc',
         'test-reloc-info.cc',
         'test-serialize.cc',
diff --git a/test/cctest/test-random.cc b/test/cctest/test-random.cc
new file mode 100644 (file)
index 0000000..477daf1
--- /dev/null
@@ -0,0 +1,109 @@
+// Copyright 2012 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+//       notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+//       copyright notice, this list of conditions and the following
+//       disclaimer in the documentation and/or other materials provided
+//       with the distribution.
+//     * Neither the name of Google Inc. nor the names of its
+//       contributors may be used to endorse or promote products derived
+//       from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+#include "v8.h"
+
+#include "cctest.h"
+#include "compiler.h"
+#include "execution.h"
+#include "isolate.h"
+
+
+using namespace v8::internal;
+
+static v8::Persistent<v8::Context> env;
+
+
+void SetSeeds(Handle<ByteArray> seeds, uint32_t state0, uint32_t state1) {
+  for (int i = 0; i < 4; i++) {
+    seeds->set(i, static_cast<byte>(state0 >> (i * kBitsPerByte)));
+    seeds->set(i + 4, static_cast<byte>(state1 >> (i * kBitsPerByte)));
+  }
+}
+
+
+void TestSeeds(Handle<JSFunction> fun,
+               Handle<Context> context,
+               uint32_t state0,
+               uint32_t state1) {
+  bool has_pending_exception;
+  Handle<JSObject> global(context->global());
+  Handle<ByteArray> seeds(context->random_seed());
+
+  SetSeeds(seeds, state0, state1);
+  Handle<Object> value =
+      Execution::Call(fun, global, 0, NULL, &has_pending_exception);
+  CHECK(value->IsHeapNumber());
+  CHECK(fun->IsOptimized());
+  double crankshaft_value = HeapNumber::cast(*value)->value();
+
+  SetSeeds(seeds, state0, state1);
+  V8::FillHeapNumberWithRandom(*value, *context);
+  double runtime_value = HeapNumber::cast(*value)->value();
+  CHECK_EQ(runtime_value, crankshaft_value);
+}
+
+
+TEST(CrankshaftRandom) {
+  if (!FLAG_crankshaft) return;
+
+  if (env.IsEmpty()) env = v8::Context::New();
+  v8::HandleScope scope;
+  env->Enter();
+
+  Handle<Context> context(Isolate::Current()->context());
+  Handle<JSObject> global(context->global());
+  Handle<ByteArray> seeds(context->random_seed());
+  bool has_pending_exception;
+
+  CompileRun("function f() { return Math.random(); }");
+
+  Object* symbol = FACTORY->LookupAsciiSymbol("f")->ToObjectChecked();
+  MaybeObject* fun_object =
+      context->global()->GetProperty(String::cast(symbol));
+  Handle<JSFunction> fun(JSFunction::cast(fun_object->ToObjectChecked()));
+
+  // Optimize function.
+  Execution::Call(fun, global, 0, NULL, &has_pending_exception);
+  Execution::Call(fun, global, 0, NULL, &has_pending_exception);
+  fun->MarkForLazyRecompilation();
+
+  // Test with some random values.
+  TestSeeds(fun, context, 0xC0C0AFFE, 0x31415926);
+  TestSeeds(fun, context, 0x01020304, 0xFFFFFFFF);
+  TestSeeds(fun, context, 0x00000001, 0x00000000);
+
+  // Test that we bail out to runtime when seeds are uninitialized (zeros).
+  SetSeeds(seeds, 0, 0);
+  Handle<Object> value =
+      Execution::Call(fun, global, 0, NULL, &has_pending_exception);
+  CHECK(value->IsHeapNumber());
+  CHECK(fun->IsOptimized());
+  double crankshaft_value = HeapNumber::cast(*value)->value();
+  CHECK_NE(0.0, crankshaft_value);
+}