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

Side by Side Diff: src/runtime/runtime-maths.cc

Issue 1464303002: Implement xorshift128+ for Math.random. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: add missing include Created 5 years 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
« no previous file with comments | « src/runtime/runtime.h ('k') | test/cctest/test-serialize.cc » ('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 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/runtime/runtime-utils.h" 5 #include "src/runtime/runtime-utils.h"
6 6
7 #include "src/arguments.h" 7 #include "src/arguments.h"
8 #include "src/assembler.h" 8 #include "src/assembler.h"
9 #include "src/base/utils/random-number-generator.h" 9 #include "src/base/utils/random-number-generator.h"
10 #include "src/bootstrapper.h"
10 #include "src/codegen.h" 11 #include "src/codegen.h"
11 #include "src/third_party/fdlibm/fdlibm.h" 12 #include "src/third_party/fdlibm/fdlibm.h"
12 13
13 namespace v8 { 14 namespace v8 {
14 namespace internal { 15 namespace internal {
15 16
16 #define RUNTIME_UNARY_MATH(Name, name) \ 17 #define RUNTIME_UNARY_MATH(Name, name) \
17 RUNTIME_FUNCTION(Runtime_Math##Name) { \ 18 RUNTIME_FUNCTION(Runtime_Math##Name) { \
18 HandleScope scope(isolate); \ 19 HandleScope scope(isolate); \
19 DCHECK(args.length() == 1); \ 20 DCHECK(args.length() == 1); \
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 RUNTIME_FUNCTION(Runtime_IsMinusZero) { 241 RUNTIME_FUNCTION(Runtime_IsMinusZero) {
241 SealHandleScope shs(isolate); 242 SealHandleScope shs(isolate);
242 DCHECK(args.length() == 1); 243 DCHECK(args.length() == 1);
243 CONVERT_ARG_CHECKED(Object, obj, 0); 244 CONVERT_ARG_CHECKED(Object, obj, 0);
244 if (!obj->IsHeapNumber()) return isolate->heap()->false_value(); 245 if (!obj->IsHeapNumber()) return isolate->heap()->false_value();
245 HeapNumber* number = HeapNumber::cast(obj); 246 HeapNumber* number = HeapNumber::cast(obj);
246 return isolate->heap()->ToBoolean(IsMinusZero(number->value())); 247 return isolate->heap()->ToBoolean(IsMinusZero(number->value()));
247 } 248 }
248 249
249 250
250 RUNTIME_FUNCTION(Runtime_InitializeRNG) { 251 RUNTIME_FUNCTION(Runtime_GenerateRandomNumbers) {
251 HandleScope scope(isolate); 252 HandleScope scope(isolate);
252 DCHECK(args.length() == 0); 253 DCHECK(args.length() == 1);
253 static const int kSize = 4; 254 // Random numbers in the snapshot are not really that random.
254 Handle<FixedArray> array = isolate->factory()->NewFixedArray(kSize); 255 DCHECK(!isolate->bootstrapper()->IsActive());
255 uint16_t seeds[kSize]; 256 static const int kState0Offset = 0;
256 do { 257 static const int kState1Offset = 1;
257 isolate->random_number_generator()->NextBytes(seeds, 258 static const int kRandomBatchSize = 64;
258 kSize * sizeof(*seeds)); 259 CONVERT_ARG_HANDLE_CHECKED(Object, maybe_typed_array, 0);
259 } while (!(seeds[0] && seeds[1] && seeds[2] && seeds[3])); 260 Handle<JSTypedArray> typed_array;
260 for (int i = 0; i < kSize; i++) array->set(i, Smi::FromInt(seeds[i])); 261 // Allocate typed array if it does not yet exist.
261 return *isolate->factory()->NewJSArrayWithElements(array); 262 if (maybe_typed_array->IsJSTypedArray()) {
263 typed_array = Handle<JSTypedArray>::cast(maybe_typed_array);
264 } else {
265 static const int kByteLength = kRandomBatchSize * kDoubleSize;
266 Handle<JSArrayBuffer> buffer =
267 isolate->factory()->NewJSArrayBuffer(SharedFlag::kNotShared, TENURED);
268 JSArrayBuffer::SetupAllocatingData(buffer, isolate, kByteLength, true,
269 SharedFlag::kNotShared);
270 typed_array = isolate->factory()->NewJSTypedArray(
271 kExternalFloat64Array, buffer, 0, kRandomBatchSize);
272 }
273
274 DisallowHeapAllocation no_gc;
275 double* array =
276 reinterpret_cast<double*>(typed_array->GetBuffer()->backing_store());
277 // Fetch existing state.
278 uint64_t state0 = double_to_uint64(array[kState0Offset]);
279 uint64_t state1 = double_to_uint64(array[kState1Offset]);
280 // Initialize state if not yet initialized.
281 while (state0 == 0 || state1 == 0) {
282 isolate->random_number_generator()->NextBytes(&state0, sizeof(state0));
283 isolate->random_number_generator()->NextBytes(&state1, sizeof(state1));
284 }
285 // Create random numbers.
286 for (int i = kState1Offset + 1; i < kRandomBatchSize; i++) {
287 // Generate random numbers using xorshift128+.
288 base::RandomNumberGenerator::XorShift128(&state0, &state1);
289 array[i] = base::RandomNumberGenerator::ToDouble(state0, state1);
290 }
291 // Persist current state.
292 array[kState0Offset] = uint64_to_double(state0);
293 array[kState1Offset] = uint64_to_double(state1);
294 return *typed_array;
262 } 295 }
263 } // namespace internal 296 } // namespace internal
264 } // namespace v8 297 } // namespace v8
OLDNEW
« no previous file with comments | « src/runtime/runtime.h ('k') | test/cctest/test-serialize.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698