Untangle RNG from v8 core
This will allow for using the RNG from platform files without depending on isolates. BUG=none R=bmeurer@chromium.org LOG=n Review URL: https://codereview.chromium.org/333053002 git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@21879 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
parent
8e9bb93986
commit
005410e8ea
@ -109,4 +109,12 @@ template <int> class StaticAssertionHelper { };
|
||||
template <typename T>
|
||||
inline void USE(T) { }
|
||||
|
||||
|
||||
#define IS_POWER_OF_TWO(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
|
||||
|
||||
// The following macro works on both 32 and 64-bit platforms.
|
||||
// Usage: instead of writing 0x1234567890123456
|
||||
// write V8_2PART_UINT64_C(0x12345678,90123456);
|
||||
#define V8_2PART_UINT64_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
|
||||
|
||||
#endif // V8_BASE_MACROS_H_
|
||||
|
@ -101,11 +101,6 @@ typedef byte* Address;
|
||||
# define V8_PTR_PREFIX ""
|
||||
#endif
|
||||
|
||||
// The following macro works on both 32 and 64-bit platforms.
|
||||
// Usage: instead of writing 0x1234567890123456
|
||||
// write V8_2PART_UINT64_C(0x12345678,90123456);
|
||||
#define V8_2PART_UINT64_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
|
||||
|
||||
#define V8PRIxPTR V8_PTR_PREFIX "x"
|
||||
#define V8PRIdPTR V8_PTR_PREFIX "d"
|
||||
#define V8PRIuPTR V8_PTR_PREFIX "u"
|
||||
|
@ -32,7 +32,11 @@ bool Isolate::DebuggerHasBreakPoints() {
|
||||
|
||||
RandomNumberGenerator* Isolate::random_number_generator() {
|
||||
if (random_number_generator_ == NULL) {
|
||||
random_number_generator_ = new RandomNumberGenerator;
|
||||
if (FLAG_random_seed != 0) {
|
||||
random_number_generator_ = new RandomNumberGenerator(FLAG_random_seed);
|
||||
} else {
|
||||
random_number_generator_ = new RandomNumberGenerator();
|
||||
}
|
||||
}
|
||||
return random_number_generator_;
|
||||
}
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "src/allocation.h"
|
||||
#include "src/base/macros.h"
|
||||
#include "src/checks.h"
|
||||
#include "src/globals.h"
|
||||
#include "src/list.h"
|
||||
@ -22,8 +23,6 @@ namespace internal {
|
||||
// ----------------------------------------------------------------------------
|
||||
// General helper functions
|
||||
|
||||
#define IS_POWER_OF_TWO(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
|
||||
|
||||
// Returns true iff x is a power of 2. Cannot be used with the maximally
|
||||
// negative value of the type T (the -1 overflows).
|
||||
template <typename T>
|
||||
|
5
src/utils/DEPS
Normal file
5
src/utils/DEPS
Normal file
@ -0,0 +1,5 @@
|
||||
include_rules = [
|
||||
"-src",
|
||||
"+src/base",
|
||||
"+src/platform",
|
||||
]
|
@ -7,10 +7,11 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "src/flags.h"
|
||||
#include <new>
|
||||
|
||||
#include "src/base/macros.h"
|
||||
#include "src/platform/mutex.h"
|
||||
#include "src/platform/time.h"
|
||||
#include "src/utils.h"
|
||||
|
||||
namespace v8 {
|
||||
namespace internal {
|
||||
@ -27,12 +28,6 @@ void RandomNumberGenerator::SetEntropySource(EntropySource source) {
|
||||
|
||||
|
||||
RandomNumberGenerator::RandomNumberGenerator() {
|
||||
// Check --random-seed flag first.
|
||||
if (FLAG_random_seed != 0) {
|
||||
SetSeed(FLAG_random_seed);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if embedder supplied an entropy source.
|
||||
{ LockGuard<Mutex> lock_guard(entropy_mutex.Pointer());
|
||||
if (entropy_source != NULL) {
|
||||
@ -87,7 +82,7 @@ int RandomNumberGenerator::NextInt(int max) {
|
||||
ASSERT_LE(0, max);
|
||||
|
||||
// Fast path if max is a power of 2.
|
||||
if (IsPowerOf2(max)) {
|
||||
if (IS_POWER_OF_TWO(max)) {
|
||||
return static_cast<int>((max * static_cast<int64_t>(Next(31))) >> 31);
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
#ifndef V8_UTILS_RANDOM_NUMBER_GENERATOR_H_
|
||||
#define V8_UTILS_RANDOM_NUMBER_GENERATOR_H_
|
||||
|
||||
#include "src/globals.h"
|
||||
#include "src/base/macros.h"
|
||||
|
||||
namespace v8 {
|
||||
namespace internal {
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include "src/v8.h"
|
||||
|
||||
#include "src/utils/random-number-generator.h"
|
||||
#include "src/isolate-inl.h"
|
||||
#include "test/cctest/cctest.h"
|
||||
|
||||
using namespace v8::internal;
|
||||
@ -77,7 +78,9 @@ TEST(NextDoubleRange) {
|
||||
TEST(RandomSeedFlagIsUsed) {
|
||||
for (unsigned n = 0; n < ARRAY_SIZE(kRandomSeeds); ++n) {
|
||||
FLAG_random_seed = kRandomSeeds[n];
|
||||
RandomNumberGenerator rng1;
|
||||
v8::Isolate* i = v8::Isolate::New();
|
||||
RandomNumberGenerator& rng1 =
|
||||
*reinterpret_cast<Isolate*>(i)->random_number_generator();
|
||||
RandomNumberGenerator rng2(kRandomSeeds[n]);
|
||||
for (int k = 1; k <= kMaxRuns; ++k) {
|
||||
int64_t i1, i2;
|
||||
@ -88,5 +91,6 @@ TEST(RandomSeedFlagIsUsed) {
|
||||
CHECK_EQ(rng2.NextInt(k), rng1.NextInt(k));
|
||||
CHECK_EQ(rng2.NextDouble(), rng1.NextDouble());
|
||||
}
|
||||
i->Dispose();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user