Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(294)

Side by Side Diff: src/x64/lithium-codegen-x64.cc

Issue 23478031: Improve code generation for the HRandom instruction. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed comment. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/x64/lithium-codegen-x64.h ('k') | src/x64/lithium-x64.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 3649 matching lines...) Expand 10 before | Expand all | Expand 10 after
3660 __ CallStub(&stub); 3660 __ CallStub(&stub);
3661 } else { 3661 } else {
3662 ASSERT(exponent_type.IsDouble()); 3662 ASSERT(exponent_type.IsDouble());
3663 MathPowStub stub(MathPowStub::DOUBLE); 3663 MathPowStub stub(MathPowStub::DOUBLE);
3664 __ CallStub(&stub); 3664 __ CallStub(&stub);
3665 } 3665 }
3666 } 3666 }
3667 3667
3668 3668
3669 void LCodeGen::DoRandom(LRandom* instr) { 3669 void LCodeGen::DoRandom(LRandom* instr) {
3670 class DeferredDoRandom V8_FINAL : public LDeferredCode { 3670 // Assert that register size is twice the size of each seed.
3671 public:
3672 DeferredDoRandom(LCodeGen* codegen, LRandom* instr)
3673 : LDeferredCode(codegen), instr_(instr) { }
3674 virtual void Generate() V8_OVERRIDE { codegen()->DoDeferredRandom(instr_); }
3675 virtual LInstruction* instr() V8_OVERRIDE { return instr_; }
3676 private:
3677 LRandom* instr_;
3678 };
3679
3680 DeferredDoRandom* deferred = new(zone()) DeferredDoRandom(this, instr);
3681
3682 // Having marked this instruction as a call we can use any
3683 // registers.
3684 ASSERT(ToDoubleRegister(instr->result()).is(xmm1));
3685
3686 // Choose the right register for the first argument depending on
3687 // calling convention.
3688 #ifdef _WIN64
3689 ASSERT(ToRegister(instr->global_object()).is(rcx));
3690 Register global_object = rcx;
3691 #else
3692 ASSERT(ToRegister(instr->global_object()).is(rdi));
3693 Register global_object = rdi;
3694 #endif
3695
3696 static const int kSeedSize = sizeof(uint32_t); 3671 static const int kSeedSize = sizeof(uint32_t);
3697 STATIC_ASSERT(kPointerSize == 2 * kSeedSize); 3672 STATIC_ASSERT(kPointerSize == 2 * kSeedSize);
3698 3673
3699 __ movq(global_object, 3674 // Load native context
3700 FieldOperand(global_object, GlobalObject::kNativeContextOffset)); 3675 Register global_object = ToRegister(instr->global_object());
3676 Register native_context = global_object;
3677 __ movq(native_context, FieldOperand(
3678 global_object, GlobalObject::kNativeContextOffset));
3679
3680 // Load state (FixedArray of the native context's random seeds)
3701 static const int kRandomSeedOffset = 3681 static const int kRandomSeedOffset =
3702 FixedArray::kHeaderSize + Context::RANDOM_SEED_INDEX * kPointerSize; 3682 FixedArray::kHeaderSize + Context::RANDOM_SEED_INDEX * kPointerSize;
3703 __ movq(rbx, FieldOperand(global_object, kRandomSeedOffset)); 3683 Register state = native_context;
3704 // rbx: FixedArray of the native context's random seeds 3684 __ movq(state, FieldOperand(native_context, kRandomSeedOffset));
3705 3685
3706 // Load state[0]. 3686 // Load state[0].
3707 __ movl(rax, FieldOperand(rbx, ByteArray::kHeaderSize)); 3687 Register state0 = ToRegister(instr->scratch());
3708 // If state[0] == 0, call runtime to initialize seeds. 3688 __ movl(state0, FieldOperand(state, ByteArray::kHeaderSize));
3709 __ testl(rax, rax);
3710 __ j(zero, deferred->entry());
3711 // Load state[1]. 3689 // Load state[1].
3712 __ movl(rcx, FieldOperand(rbx, ByteArray::kHeaderSize + kSeedSize)); 3690 Register state1 = ToRegister(instr->scratch2());
3691 __ movl(state1, FieldOperand(state, ByteArray::kHeaderSize + kSeedSize));
3713 3692
3714 // state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16) 3693 // state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16)
3715 // Only operate on the lower 32 bit of rax. 3694 Register scratch3 = ToRegister(instr->scratch3());
3716 __ movzxwl(rdx, rax); 3695 __ movzxwl(scratch3, state0);
3717 __ imull(rdx, rdx, Immediate(18273)); 3696 __ imull(scratch3, scratch3, Immediate(18273));
3718 __ shrl(rax, Immediate(16)); 3697 __ shrl(state0, Immediate(16));
3719 __ addl(rax, rdx); 3698 __ addl(state0, scratch3);
3720 // Save state[0]. 3699 // Save state[0].
3721 __ movl(FieldOperand(rbx, ByteArray::kHeaderSize), rax); 3700 __ movl(FieldOperand(state, ByteArray::kHeaderSize), state0);
3722 3701
3723 // state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16) 3702 // state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16)
3724 __ movzxwl(rdx, rcx); 3703 __ movzxwl(scratch3, state1);
3725 __ imull(rdx, rdx, Immediate(36969)); 3704 __ imull(scratch3, scratch3, Immediate(36969));
3726 __ shrl(rcx, Immediate(16)); 3705 __ shrl(state1, Immediate(16));
3727 __ addl(rcx, rdx); 3706 __ addl(state1, scratch3);
3728 // Save state[1]. 3707 // Save state[1].
3729 __ movl(FieldOperand(rbx, ByteArray::kHeaderSize + kSeedSize), rcx); 3708 __ movl(FieldOperand(state, ByteArray::kHeaderSize + kSeedSize), state1);
3730 3709
3731 // Random bit pattern = (state[0] << 14) + (state[1] & 0x3FFFF) 3710 // Random bit pattern = (state[0] << 14) + (state[1] & 0x3FFFF)
3732 __ shll(rax, Immediate(14)); 3711 Register random = state0;
3733 __ andl(rcx, Immediate(0x3FFFF)); 3712 __ shll(random, Immediate(14));
3734 __ addl(rax, rcx); 3713 __ andl(state1, Immediate(0x3FFFF));
3714 __ addl(random, state1);
3735 3715
3736 __ bind(deferred->exit());
3737 // Convert 32 random bits in rax to 0.(32 random bits) in a double 3716 // Convert 32 random bits in rax to 0.(32 random bits) in a double
3738 // by computing: 3717 // by computing:
3739 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). 3718 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
3740 __ movq(rcx, V8_INT64_C(0x4130000000000000), 3719 XMMRegister result = ToDoubleRegister(instr->result());
3720 // We use xmm0 as fixed scratch register here.
3721 XMMRegister scratch4 = xmm0;
3722 __ movq(scratch3, V8_INT64_C(0x4130000000000000),
3741 RelocInfo::NONE64); // 1.0 x 2^20 as double 3723 RelocInfo::NONE64); // 1.0 x 2^20 as double
3742 __ movq(xmm2, rcx); 3724 __ movq(scratch4, scratch3);
3743 __ movd(xmm1, rax); 3725 __ movd(result, random);
3744 __ xorps(xmm1, xmm2); 3726 __ xorps(result, scratch4);
3745 __ subsd(xmm1, xmm2); 3727 __ subsd(result, scratch4);
3746 } 3728 }
3747 3729
3748 3730
3749 void LCodeGen::DoDeferredRandom(LRandom* instr) {
3750 __ PrepareCallCFunction(1);
3751 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
3752 __ movq(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
3753 // Return value is in rax.
3754 }
3755
3756
3757 void LCodeGen::DoMathExp(LMathExp* instr) { 3731 void LCodeGen::DoMathExp(LMathExp* instr) {
3758 XMMRegister input = ToDoubleRegister(instr->value()); 3732 XMMRegister input = ToDoubleRegister(instr->value());
3759 XMMRegister result = ToDoubleRegister(instr->result()); 3733 XMMRegister result = ToDoubleRegister(instr->result());
3760 Register temp1 = ToRegister(instr->temp1()); 3734 Register temp1 = ToRegister(instr->temp1());
3761 Register temp2 = ToRegister(instr->temp2()); 3735 Register temp2 = ToRegister(instr->temp2());
3762 3736
3763 MathExpGenerator::EmitMathExp(masm(), input, result, xmm0, temp1, temp2); 3737 MathExpGenerator::EmitMathExp(masm(), input, result, xmm0, temp1, temp2);
3764 } 3738 }
3765 3739
3766 3740
(...skipping 1752 matching lines...) Expand 10 before | Expand all | Expand 10 after
5519 FixedArray::kHeaderSize - kPointerSize)); 5493 FixedArray::kHeaderSize - kPointerSize));
5520 __ bind(&done); 5494 __ bind(&done);
5521 } 5495 }
5522 5496
5523 5497
5524 #undef __ 5498 #undef __
5525 5499
5526 } } // namespace v8::internal 5500 } } // namespace v8::internal
5527 5501
5528 #endif // V8_TARGET_ARCH_X64 5502 #endif // V8_TARGET_ARCH_X64
OLDNEW
« no previous file with comments | « src/x64/lithium-codegen-x64.h ('k') | src/x64/lithium-x64.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698