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

Side by Side Diff: src/ia32/lithium-codegen-ia32.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/ia32/lithium-codegen-ia32.h ('k') | src/ia32/lithium-ia32.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 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 4108 matching lines...) Expand 10 before | Expand all | Expand 10 after
4119 __ CallStub(&stub); 4119 __ CallStub(&stub);
4120 } else { 4120 } else {
4121 ASSERT(exponent_type.IsDouble()); 4121 ASSERT(exponent_type.IsDouble());
4122 MathPowStub stub(MathPowStub::DOUBLE); 4122 MathPowStub stub(MathPowStub::DOUBLE);
4123 __ CallStub(&stub); 4123 __ CallStub(&stub);
4124 } 4124 }
4125 } 4125 }
4126 4126
4127 4127
4128 void LCodeGen::DoRandom(LRandom* instr) { 4128 void LCodeGen::DoRandom(LRandom* instr) {
4129 class DeferredDoRandom V8_FINAL : public LDeferredCode { 4129 CpuFeatureScope scope(masm(), SSE2);
4130 public:
4131 DeferredDoRandom(LCodeGen* codegen,
4132 LRandom* instr,
4133 const X87Stack& x87_stack)
4134 : LDeferredCode(codegen, x87_stack), instr_(instr) { }
4135 virtual void Generate() V8_OVERRIDE { codegen()->DoDeferredRandom(instr_); }
4136 virtual LInstruction* instr() V8_OVERRIDE { return instr_; }
4137 private:
4138 LRandom* instr_;
4139 };
4140 4130
4141 DeferredDoRandom* deferred =
4142 new(zone()) DeferredDoRandom(this, instr, x87_stack_);
4143
4144 CpuFeatureScope scope(masm(), SSE2);
4145 // Having marked this instruction as a call we can use any
4146 // registers.
4147 ASSERT(ToDoubleRegister(instr->result()).is(xmm1));
4148 ASSERT(ToRegister(instr->global_object()).is(eax));
4149 // Assert that the register size is indeed the size of each seed. 4131 // Assert that the register size is indeed the size of each seed.
4150 static const int kSeedSize = sizeof(uint32_t); 4132 static const int kSeedSize = sizeof(uint32_t);
4151 STATIC_ASSERT(kPointerSize == kSeedSize); 4133 STATIC_ASSERT(kPointerSize == kSeedSize);
4152 4134
4153 __ mov(eax, FieldOperand(eax, GlobalObject::kNativeContextOffset)); 4135 // Load native context
4136 Register global_object = ToRegister(instr->global_object());
4137 Register native_context = global_object;
4138 __ mov(native_context, FieldOperand(
4139 global_object, GlobalObject::kNativeContextOffset));
4140
4141 // Load state (FixedArray of the native context's random seeds)
4154 static const int kRandomSeedOffset = 4142 static const int kRandomSeedOffset =
4155 FixedArray::kHeaderSize + Context::RANDOM_SEED_INDEX * kPointerSize; 4143 FixedArray::kHeaderSize + Context::RANDOM_SEED_INDEX * kPointerSize;
4156 __ mov(ebx, FieldOperand(eax, kRandomSeedOffset)); 4144 Register state = native_context;
4157 // ebx: FixedArray of the native context's random seeds 4145 __ mov(state, FieldOperand(native_context, kRandomSeedOffset));
4158 4146
4159 // Load state[0]. 4147 // Load state[0].
4160 __ mov(ecx, FieldOperand(ebx, ByteArray::kHeaderSize)); 4148 Register state0 = ToRegister(instr->scratch());
4161 // If state[0] == 0, call runtime to initialize seeds. 4149 __ mov(state0, FieldOperand(state, ByteArray::kHeaderSize));
4162 __ test(ecx, ecx);
4163 __ j(zero, deferred->entry());
4164 // Load state[1]. 4150 // Load state[1].
4165 __ mov(eax, FieldOperand(ebx, ByteArray::kHeaderSize + kSeedSize)); 4151 Register state1 = ToRegister(instr->scratch2());
4166 // ecx: state[0] 4152 __ mov(state1, FieldOperand(state, ByteArray::kHeaderSize + kSeedSize));
4167 // eax: state[1]
4168 4153
4169 // state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16) 4154 // state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16)
4170 __ movzx_w(edx, ecx); 4155 Register scratch3 = ToRegister(instr->scratch3());
4171 __ imul(edx, edx, 18273); 4156 __ movzx_w(scratch3, state0);
4172 __ shr(ecx, 16); 4157 __ imul(scratch3, scratch3, 18273);
4173 __ add(ecx, edx); 4158 __ shr(state0, 16);
4159 __ add(state0, scratch3);
4174 // Save state[0]. 4160 // Save state[0].
4175 __ mov(FieldOperand(ebx, ByteArray::kHeaderSize), ecx); 4161 __ mov(FieldOperand(state, ByteArray::kHeaderSize), state0);
4176 4162
4177 // state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16) 4163 // state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16)
4178 __ movzx_w(edx, eax); 4164 __ movzx_w(scratch3, state1);
4179 __ imul(edx, edx, 36969); 4165 __ imul(scratch3, scratch3, 36969);
4180 __ shr(eax, 16); 4166 __ shr(state1, 16);
4181 __ add(eax, edx); 4167 __ add(state1, scratch3);
4182 // Save state[1]. 4168 // Save state[1].
4183 __ mov(FieldOperand(ebx, ByteArray::kHeaderSize + kSeedSize), eax); 4169 __ mov(FieldOperand(state, ByteArray::kHeaderSize + kSeedSize), state1);
4184 4170
4185 // Random bit pattern = (state[0] << 14) + (state[1] & 0x3FFFF) 4171 // Random bit pattern = (state[0] << 14) + (state[1] & 0x3FFFF)
4186 __ shl(ecx, 14); 4172 Register random = state0;
4187 __ and_(eax, Immediate(0x3FFFF)); 4173 __ shl(random, 14);
4188 __ add(eax, ecx); 4174 __ and_(state1, Immediate(0x3FFFF));
4175 __ add(random, state1);
4189 4176
4190 __ bind(deferred->exit()); 4177 // Convert 32 random bits in random to 0.(32 random bits) in a double
4191 // Convert 32 random bits in eax to 0.(32 random bits) in a double
4192 // by computing: 4178 // by computing:
4193 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). 4179 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
4194 __ mov(ebx, Immediate(0x49800000)); // 1.0 x 2^20 as single. 4180 XMMRegister result = ToDoubleRegister(instr->result());
4195 __ movd(xmm2, ebx); 4181 // We use xmm0 as fixed scratch register here.
4196 __ movd(xmm1, eax); 4182 XMMRegister scratch4 = xmm0;
4197 __ cvtss2sd(xmm2, xmm2); 4183 __ mov(scratch3, Immediate(0x49800000)); // 1.0 x 2^20 as single.
4198 __ xorps(xmm1, xmm2); 4184 __ movd(scratch4, scratch3);
4199 __ subsd(xmm1, xmm2); 4185 __ movd(result, random);
4186 __ cvtss2sd(scratch4, scratch4);
4187 __ xorps(result, scratch4);
4188 __ subsd(result, scratch4);
4200 } 4189 }
4201 4190
4202 4191
4203 void LCodeGen::DoDeferredRandom(LRandom* instr) {
4204 __ PrepareCallCFunction(1, ebx);
4205 __ mov(Operand(esp, 0), eax);
4206 __ CallCFunction(ExternalReference::random_uint32_function(isolate()), 1);
4207 // Return value is in eax.
4208 }
4209
4210
4211 void LCodeGen::DoMathLog(LMathLog* instr) { 4192 void LCodeGen::DoMathLog(LMathLog* instr) {
4212 CpuFeatureScope scope(masm(), SSE2); 4193 CpuFeatureScope scope(masm(), SSE2);
4213 ASSERT(instr->value()->Equals(instr->result())); 4194 ASSERT(instr->value()->Equals(instr->result()));
4214 XMMRegister input_reg = ToDoubleRegister(instr->value()); 4195 XMMRegister input_reg = ToDoubleRegister(instr->value());
4215 Label positive, done, zero; 4196 Label positive, done, zero;
4216 __ xorps(xmm0, xmm0); 4197 __ xorps(xmm0, xmm0);
4217 __ ucomisd(input_reg, xmm0); 4198 __ ucomisd(input_reg, xmm0);
4218 __ j(above, &positive, Label::kNear); 4199 __ j(above, &positive, Label::kNear);
4219 __ j(equal, &zero, Label::kNear); 4200 __ j(equal, &zero, Label::kNear);
4220 ExternalReference nan = 4201 ExternalReference nan =
(...skipping 2152 matching lines...) Expand 10 before | Expand all | Expand 10 after
6373 FixedArray::kHeaderSize - kPointerSize)); 6354 FixedArray::kHeaderSize - kPointerSize));
6374 __ bind(&done); 6355 __ bind(&done);
6375 } 6356 }
6376 6357
6377 6358
6378 #undef __ 6359 #undef __
6379 6360
6380 } } // namespace v8::internal 6361 } } // namespace v8::internal
6381 6362
6382 #endif // V8_TARGET_ARCH_IA32 6363 #endif // V8_TARGET_ARCH_IA32
OLDNEW
« no previous file with comments | « src/ia32/lithium-codegen-ia32.h ('k') | src/ia32/lithium-ia32.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698