Chromium Code Reviews

Side by Side Diff: src/mips/lithium-codegen-mips.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.
Jump to:
View unified diff | | Annotate | Revision Log
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 3788 matching lines...)
3799 MathPowStub stub(MathPowStub::INTEGER); 3799 MathPowStub stub(MathPowStub::INTEGER);
3800 __ CallStub(&stub); 3800 __ CallStub(&stub);
3801 } else { 3801 } else {
3802 ASSERT(exponent_type.IsDouble()); 3802 ASSERT(exponent_type.IsDouble());
3803 MathPowStub stub(MathPowStub::DOUBLE); 3803 MathPowStub stub(MathPowStub::DOUBLE);
3804 __ CallStub(&stub); 3804 __ CallStub(&stub);
3805 } 3805 }
3806 } 3806 }
3807 3807
3808 3808
3809 void LCodeGen::DoRandom(LRandom* instr) {
3810 // Assert that the register size is indeed the size of each seed.
3811 static const int kSeedSize = sizeof(uint32_t);
3812 STATIC_ASSERT(kPointerSize == kSeedSize);
3813
3814 // Load native context.
3815 Register global_object = ToRegister(instr->global_object());
3816 Register native_context = global_object;
3817 __ lw(native_context, FieldMemOperand(
3818 global_object, GlobalObject::kNativeContextOffset));
3819
3820 // Load state (FixedArray of the native context's random seeds).
3821 static const int kRandomSeedOffset =
3822 FixedArray::kHeaderSize + Context::RANDOM_SEED_INDEX * kPointerSize;
3823 Register state = native_context;
3824 __ lw(state, FieldMemOperand(native_context, kRandomSeedOffset));
3825
3826 // Load state[0].
3827 Register state0 = ToRegister(instr->scratch());
3828 __ lw(state0, FieldMemOperand(state, ByteArray::kHeaderSize));
3829 // Load state[1].
3830 Register state1 = ToRegister(instr->scratch2());
3831 __ lw(state1, FieldMemOperand(state, ByteArray::kHeaderSize + kSeedSize));
3832
3833 // state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16)
3834 Register scratch3 = ToRegister(instr->scratch3());
3835 Register scratch4 = scratch0();
3836 __ And(scratch3, state0, Operand(0xFFFF));
3837 __ li(scratch4, Operand(18273));
3838 __ Mul(scratch3, scratch3, scratch4);
3839 __ srl(state0, state0, 16);
3840 __ Addu(state0, scratch3, state0);
3841 // Save state[0].
3842 __ sw(state0, FieldMemOperand(state, ByteArray::kHeaderSize));
3843
3844 // state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16)
3845 __ And(scratch3, state1, Operand(0xFFFF));
3846 __ li(scratch4, Operand(36969));
3847 __ Mul(scratch3, scratch3, scratch4);
3848 __ srl(state1, state1, 16),
3849 __ Addu(state1, scratch3, state1);
3850 // Save state[1].
3851 __ sw(state1, FieldMemOperand(state, ByteArray::kHeaderSize + kSeedSize));
3852
3853 // Random bit pattern = (state[0] << 14) + (state[1] & 0x3FFFF)
3854 Register random = scratch4;
3855 __ And(random, state1, Operand(0x3FFFF));
3856 __ sll(state0, state0, 14);
3857 __ Addu(random, random, state0);
3858
3859 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
3860 __ li(scratch3, Operand(0x41300000));
3861 // Move 0x41300000xxxxxxxx (x = random bits in v0) to FPU.
3862 DoubleRegister result = ToDoubleRegister(instr->result());
3863 __ Move(result, random, scratch3);
3864 // Move 0x4130000000000000 to FPU.
3865 DoubleRegister scratch5 = double_scratch0();
3866 __ Move(scratch5, zero_reg, scratch3);
3867 __ sub_d(result, result, scratch5);
3868 }
3869
3870
3871 void LCodeGen::DoMathExp(LMathExp* instr) { 3809 void LCodeGen::DoMathExp(LMathExp* instr) {
3872 DoubleRegister input = ToDoubleRegister(instr->value()); 3810 DoubleRegister input = ToDoubleRegister(instr->value());
3873 DoubleRegister result = ToDoubleRegister(instr->result()); 3811 DoubleRegister result = ToDoubleRegister(instr->result());
3874 DoubleRegister double_scratch1 = ToDoubleRegister(instr->double_temp()); 3812 DoubleRegister double_scratch1 = ToDoubleRegister(instr->double_temp());
3875 DoubleRegister double_scratch2 = double_scratch0(); 3813 DoubleRegister double_scratch2 = double_scratch0();
3876 Register temp1 = ToRegister(instr->temp1()); 3814 Register temp1 = ToRegister(instr->temp1());
3877 Register temp2 = ToRegister(instr->temp2()); 3815 Register temp2 = ToRegister(instr->temp2());
3878 3816
3879 MathExpGenerator::EmitMathExp( 3817 MathExpGenerator::EmitMathExp(
3880 masm(), input, result, double_scratch1, double_scratch2, 3818 masm(), input, result, double_scratch1, double_scratch2,
(...skipping 1965 matching lines...)
5846 __ Subu(scratch, result, scratch); 5784 __ Subu(scratch, result, scratch);
5847 __ lw(result, FieldMemOperand(scratch, 5785 __ lw(result, FieldMemOperand(scratch,
5848 FixedArray::kHeaderSize - kPointerSize)); 5786 FixedArray::kHeaderSize - kPointerSize));
5849 __ bind(&done); 5787 __ bind(&done);
5850 } 5788 }
5851 5789
5852 5790
5853 #undef __ 5791 #undef __
5854 5792
5855 } } // namespace v8::internal 5793 } } // namespace v8::internal
OLDNEW
« src/math.js ('K') | « src/mips/full-codegen-mips.cc ('k') | src/mips/lithium-mips.h » ('j') | no next file with comments »

Powered by Google App Engine