LCOV - code coverage report
Current view: top level - src/runtime - runtime-maths.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 20 23 87.0 %
Date: 2017-10-20 Functions: 1 2 50.0 %

          Line data    Source code
       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
       3             : // found in the LICENSE file.
       4             : 
       5             : #include "src/runtime/runtime-utils.h"
       6             : 
       7             : #include "src/arguments.h"
       8             : #include "src/assembler.h"
       9             : #include "src/base/utils/random-number-generator.h"
      10             : #include "src/bootstrapper.h"
      11             : #include "src/counters.h"
      12             : #include "src/double.h"
      13             : #include "src/objects-inl.h"
      14             : 
      15             : namespace v8 {
      16             : namespace internal {
      17             : 
      18      136216 : RUNTIME_FUNCTION(Runtime_GenerateRandomNumbers) {
      19       68108 :   HandleScope scope(isolate);
      20             :   DCHECK_EQ(0, args.length());
      21             : 
      22       68108 :   Handle<Context> native_context = isolate->native_context();
      23             :   DCHECK_EQ(0, native_context->math_random_index()->value());
      24             : 
      25             :   static const int kCacheSize = 64;
      26             :   static const int kState0Offset = kCacheSize - 1;
      27             :   static const int kState1Offset = kState0Offset - 1;
      28             :   // The index is decremented before used to access the cache.
      29             :   static const int kInitialIndex = kState1Offset;
      30             : 
      31             :   Handle<FixedDoubleArray> cache;
      32       68108 :   uint64_t state0 = 0;
      33       68108 :   uint64_t state1 = 0;
      34      136216 :   if (native_context->math_random_cache()->IsFixedDoubleArray()) {
      35             :     cache = Handle<FixedDoubleArray>(
      36       67937 :         FixedDoubleArray::cast(native_context->math_random_cache()), isolate);
      37       67937 :     state0 = double_to_uint64(cache->get_scalar(kState0Offset));
      38       67937 :     state1 = double_to_uint64(cache->get_scalar(kState1Offset));
      39             :   } else {
      40             :     cache = Handle<FixedDoubleArray>::cast(
      41         171 :         isolate->factory()->NewFixedDoubleArray(kCacheSize, TENURED));
      42         171 :     native_context->set_math_random_cache(*cache);
      43             :     // Initialize state if not yet initialized. If a fixed random seed was
      44             :     // requested, use it to reset our state the first time a script asks for
      45             :     // random numbers in this context. This ensures the script sees a consistent
      46             :     // sequence.
      47         171 :     if (FLAG_random_seed != 0) {
      48         171 :       state0 = FLAG_random_seed;
      49         171 :       state1 = FLAG_random_seed;
      50             :     } else {
      51           0 :       while (state0 == 0 || state1 == 0) {
      52           0 :         isolate->random_number_generator()->NextBytes(&state0, sizeof(state0));
      53           0 :         isolate->random_number_generator()->NextBytes(&state1, sizeof(state1));
      54             :       }
      55             :     }
      56             :   }
      57             : 
      58             :   DisallowHeapAllocation no_gc;
      59             :   FixedDoubleArray* raw_cache = *cache;
      60             :   // Create random numbers.
      61     4222696 :   for (int i = 0; i < kInitialIndex; i++) {
      62             :     // Generate random numbers using xorshift128+.
      63     4222696 :     base::RandomNumberGenerator::XorShift128(&state0, &state1);
      64     4222696 :     raw_cache->set(i, base::RandomNumberGenerator::ToDouble(state0, state1));
      65             :   }
      66             : 
      67             :   // Persist current state.
      68       68108 :   raw_cache->set(kState0Offset, uint64_to_double(state0));
      69       68108 :   raw_cache->set(kState1Offset, uint64_to_double(state1));
      70       68108 :   return Smi::FromInt(kInitialIndex);
      71             : }
      72             : }  // namespace internal
      73             : }  // namespace v8

Generated by: LCOV version 1.10