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

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

Issue 68723002: Implement Math.random() purely in JavaScript. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Comments only Created 7 years, 1 month 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/full-codegen-x64.cc ('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 3713 matching lines...) Expand 10 before | Expand all | Expand 10 after
3724 MathPowStub stub(MathPowStub::INTEGER); 3724 MathPowStub stub(MathPowStub::INTEGER);
3725 __ CallStub(&stub); 3725 __ CallStub(&stub);
3726 } else { 3726 } else {
3727 ASSERT(exponent_type.IsDouble()); 3727 ASSERT(exponent_type.IsDouble());
3728 MathPowStub stub(MathPowStub::DOUBLE); 3728 MathPowStub stub(MathPowStub::DOUBLE);
3729 __ CallStub(&stub); 3729 __ CallStub(&stub);
3730 } 3730 }
3731 } 3731 }
3732 3732
3733 3733
3734 void LCodeGen::DoRandom(LRandom* instr) {
3735 // Assert that register size is twice the size of each seed.
3736 static const int kSeedSize = sizeof(uint32_t);
3737 STATIC_ASSERT(kPointerSize == 2 * kSeedSize);
3738
3739 // Load native context
3740 Register global_object = ToRegister(instr->global_object());
3741 Register native_context = global_object;
3742 __ movq(native_context, FieldOperand(
3743 global_object, GlobalObject::kNativeContextOffset));
3744
3745 // Load state (FixedArray of the native context's random seeds)
3746 static const int kRandomSeedOffset =
3747 FixedArray::kHeaderSize + Context::RANDOM_SEED_INDEX * kPointerSize;
3748 Register state = native_context;
3749 __ movq(state, FieldOperand(native_context, kRandomSeedOffset));
3750
3751 // Load state[0].
3752 Register state0 = ToRegister(instr->scratch());
3753 __ movl(state0, FieldOperand(state, ByteArray::kHeaderSize));
3754 // Load state[1].
3755 Register state1 = ToRegister(instr->scratch2());
3756 __ movl(state1, FieldOperand(state, ByteArray::kHeaderSize + kSeedSize));
3757
3758 // state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16)
3759 Register scratch3 = ToRegister(instr->scratch3());
3760 __ movzxwl(scratch3, state0);
3761 __ imull(scratch3, scratch3, Immediate(18273));
3762 __ shrl(state0, Immediate(16));
3763 __ addl(state0, scratch3);
3764 // Save state[0].
3765 __ movl(FieldOperand(state, ByteArray::kHeaderSize), state0);
3766
3767 // state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16)
3768 __ movzxwl(scratch3, state1);
3769 __ imull(scratch3, scratch3, Immediate(36969));
3770 __ shrl(state1, Immediate(16));
3771 __ addl(state1, scratch3);
3772 // Save state[1].
3773 __ movl(FieldOperand(state, ByteArray::kHeaderSize + kSeedSize), state1);
3774
3775 // Random bit pattern = (state[0] << 14) + (state[1] & 0x3FFFF)
3776 Register random = state0;
3777 __ shll(random, Immediate(14));
3778 __ andl(state1, Immediate(0x3FFFF));
3779 __ addl(random, state1);
3780
3781 // Convert 32 random bits in rax to 0.(32 random bits) in a double
3782 // by computing:
3783 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
3784 XMMRegister result = ToDoubleRegister(instr->result());
3785 XMMRegister scratch4 = double_scratch0();
3786 __ movq(scratch3, V8_INT64_C(0x4130000000000000)); // 1.0 x 2^20 as double
3787 __ movq(scratch4, scratch3);
3788 __ movd(result, random);
3789 __ xorps(result, scratch4);
3790 __ subsd(result, scratch4);
3791 }
3792
3793
3794 void LCodeGen::DoMathExp(LMathExp* instr) { 3734 void LCodeGen::DoMathExp(LMathExp* instr) {
3795 XMMRegister input = ToDoubleRegister(instr->value()); 3735 XMMRegister input = ToDoubleRegister(instr->value());
3796 XMMRegister result = ToDoubleRegister(instr->result()); 3736 XMMRegister result = ToDoubleRegister(instr->result());
3797 XMMRegister temp0 = double_scratch0(); 3737 XMMRegister temp0 = double_scratch0();
3798 Register temp1 = ToRegister(instr->temp1()); 3738 Register temp1 = ToRegister(instr->temp1());
3799 Register temp2 = ToRegister(instr->temp2()); 3739 Register temp2 = ToRegister(instr->temp2());
3800 3740
3801 MathExpGenerator::EmitMathExp(masm(), input, result, temp0, temp1, temp2); 3741 MathExpGenerator::EmitMathExp(masm(), input, result, temp0, temp1, temp2);
3802 } 3742 }
3803 3743
(...skipping 1888 matching lines...) Expand 10 before | Expand all | Expand 10 after
5692 FixedArray::kHeaderSize - kPointerSize)); 5632 FixedArray::kHeaderSize - kPointerSize));
5693 __ bind(&done); 5633 __ bind(&done);
5694 } 5634 }
5695 5635
5696 5636
5697 #undef __ 5637 #undef __
5698 5638
5699 } } // namespace v8::internal 5639 } } // namespace v8::internal
5700 5640
5701 #endif // V8_TARGET_ARCH_X64 5641 #endif // V8_TARGET_ARCH_X64
OLDNEW
« no previous file with comments | « src/x64/full-codegen-x64.cc ('k') | src/x64/lithium-x64.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698