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

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: 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
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 3674 matching lines...) Expand 10 before | Expand all | Expand 10 after
3685 MathPowStub stub(MathPowStub::INTEGER); 3685 MathPowStub stub(MathPowStub::INTEGER);
3686 __ CallStub(&stub); 3686 __ CallStub(&stub);
3687 } else { 3687 } else {
3688 ASSERT(exponent_type.IsDouble()); 3688 ASSERT(exponent_type.IsDouble());
3689 MathPowStub stub(MathPowStub::DOUBLE); 3689 MathPowStub stub(MathPowStub::DOUBLE);
3690 __ CallStub(&stub); 3690 __ CallStub(&stub);
3691 } 3691 }
3692 } 3692 }
3693 3693
3694 3694
3695 void LCodeGen::DoRandom(LRandom* instr) {
3696 // Assert that register size is twice the size of each seed.
3697 static const int kSeedSize = sizeof(uint32_t);
3698 STATIC_ASSERT(kPointerSize == 2 * kSeedSize);
3699
3700 // Load native context
3701 Register global_object = ToRegister(instr->global_object());
3702 Register native_context = global_object;
3703 __ movq(native_context, FieldOperand(
3704 global_object, GlobalObject::kNativeContextOffset));
3705
3706 // Load state (FixedArray of the native context's random seeds)
3707 static const int kRandomSeedOffset =
3708 FixedArray::kHeaderSize + Context::RANDOM_SEED_INDEX * kPointerSize;
3709 Register state = native_context;
3710 __ movq(state, FieldOperand(native_context, kRandomSeedOffset));
3711
3712 // Load state[0].
3713 Register state0 = ToRegister(instr->scratch());
3714 __ movl(state0, FieldOperand(state, ByteArray::kHeaderSize));
3715 // Load state[1].
3716 Register state1 = ToRegister(instr->scratch2());
3717 __ movl(state1, FieldOperand(state, ByteArray::kHeaderSize + kSeedSize));
3718
3719 // state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16)
3720 Register scratch3 = ToRegister(instr->scratch3());
3721 __ movzxwl(scratch3, state0);
3722 __ imull(scratch3, scratch3, Immediate(18273));
3723 __ shrl(state0, Immediate(16));
3724 __ addl(state0, scratch3);
3725 // Save state[0].
3726 __ movl(FieldOperand(state, ByteArray::kHeaderSize), state0);
3727
3728 // state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16)
3729 __ movzxwl(scratch3, state1);
3730 __ imull(scratch3, scratch3, Immediate(36969));
3731 __ shrl(state1, Immediate(16));
3732 __ addl(state1, scratch3);
3733 // Save state[1].
3734 __ movl(FieldOperand(state, ByteArray::kHeaderSize + kSeedSize), state1);
3735
3736 // Random bit pattern = (state[0] << 14) + (state[1] & 0x3FFFF)
3737 Register random = state0;
3738 __ shll(random, Immediate(14));
3739 __ andl(state1, Immediate(0x3FFFF));
3740 __ addl(random, state1);
3741
3742 // Convert 32 random bits in rax to 0.(32 random bits) in a double
3743 // by computing:
3744 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
3745 XMMRegister result = ToDoubleRegister(instr->result());
3746 XMMRegister scratch4 = double_scratch0();
3747 __ movq(scratch3, V8_INT64_C(0x4130000000000000)); // 1.0 x 2^20 as double
3748 __ movq(scratch4, scratch3);
3749 __ movd(result, random);
3750 __ xorps(result, scratch4);
3751 __ subsd(result, scratch4);
3752 }
3753
3754
3755 void LCodeGen::DoMathExp(LMathExp* instr) { 3695 void LCodeGen::DoMathExp(LMathExp* instr) {
3756 XMMRegister input = ToDoubleRegister(instr->value()); 3696 XMMRegister input = ToDoubleRegister(instr->value());
3757 XMMRegister result = ToDoubleRegister(instr->result()); 3697 XMMRegister result = ToDoubleRegister(instr->result());
3758 XMMRegister temp0 = double_scratch0(); 3698 XMMRegister temp0 = double_scratch0();
3759 Register temp1 = ToRegister(instr->temp1()); 3699 Register temp1 = ToRegister(instr->temp1());
3760 Register temp2 = ToRegister(instr->temp2()); 3700 Register temp2 = ToRegister(instr->temp2());
3761 3701
3762 MathExpGenerator::EmitMathExp(masm(), input, result, temp0, temp1, temp2); 3702 MathExpGenerator::EmitMathExp(masm(), input, result, temp0, temp1, temp2);
3763 } 3703 }
3764 3704
(...skipping 1869 matching lines...) Expand 10 before | Expand all | Expand 10 after
5634 FixedArray::kHeaderSize - kPointerSize)); 5574 FixedArray::kHeaderSize - kPointerSize));
5635 __ bind(&done); 5575 __ bind(&done);
5636 } 5576 }
5637 5577
5638 5578
5639 #undef __ 5579 #undef __
5640 5580
5641 } } // namespace v8::internal 5581 } } // namespace v8::internal
5642 5582
5643 #endif // V8_TARGET_ARCH_X64 5583 #endif // V8_TARGET_ARCH_X64
OLDNEW
« src/math.js ('K') | « 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