Index: src/ia32/lithium-codegen-ia32.cc |
diff --git a/src/ia32/lithium-codegen-ia32.cc b/src/ia32/lithium-codegen-ia32.cc |
index 6ddc024258940f2776c0b400105a6dd9b56dddfe..36454a2efbee2b90988f5e505a3e4c075c12ee55 100644 |
--- a/src/ia32/lithium-codegen-ia32.cc |
+++ b/src/ia32/lithium-codegen-ia32.cc |
@@ -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. |
+ const int kSeedSize = sizeof(uint32_t); |
Jakob Kummerow
2012/03/06 10:49:30
static?
|
+ 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); |
+ const int kRandomSeedOffset = |
Jakob Kummerow
2012/03/06 10:49:30
static?
|
+ 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()); |