OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 FLAG_random_seed = static_cast<int>(kRandomSeeds[n]); | 44 FLAG_random_seed = static_cast<int>(kRandomSeeds[n]); |
45 v8::Isolate::CreateParams create_params; | 45 v8::Isolate::CreateParams create_params; |
46 create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); | 46 create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); |
47 v8::Isolate* i = v8::Isolate::New(create_params); | 47 v8::Isolate* i = v8::Isolate::New(create_params); |
48 v8::base::RandomNumberGenerator& rng = | 48 v8::base::RandomNumberGenerator& rng = |
49 *reinterpret_cast<Isolate*>(i)->random_number_generator(); | 49 *reinterpret_cast<Isolate*>(i)->random_number_generator(); |
50 CHECK_EQ(kRandomSeeds[n], rng.initial_seed()); | 50 CHECK_EQ(kRandomSeeds[n], rng.initial_seed()); |
51 i->Dispose(); | 51 i->Dispose(); |
52 } | 52 } |
53 } | 53 } |
54 | |
55 | |
56 // Chi squared for getting m 0s out of n bits. | |
57 double ChiSquared(int m, int n) { | |
58 double ys_minus_np1 = (m - n / 2.0); | |
59 double chi_squared_1 = ys_minus_np1 * ys_minus_np1 * 2.0 / n; | |
60 double ys_minus_np2 = ((n - m) - n / 2.0); | |
61 double chi_squared_2 = ys_minus_np2 * ys_minus_np2 * 2.0 / n; | |
62 return chi_squared_1 + chi_squared_2; | |
63 } | |
64 | |
65 | |
66 // Test for correlations between recent bits from the PRNG, or bits that are | |
67 // biased. | |
68 void RandomBitCorrelation(int random_bit) { | |
69 FLAG_random_seed = 31415926; | |
70 LocalContext env; | |
71 v8::Isolate* isolate = env->GetIsolate(); | |
72 Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate); | |
73 v8::base::RandomNumberGenerator* rng = i_isolate->random_number_generator(); | |
74 #ifdef DEBUG | |
75 const int kHistory = 2; | |
76 const int kRepeats = 1000; | |
77 #else | |
78 const int kHistory = 8; | |
79 const int kRepeats = 10000; | |
80 #endif | |
81 uint32_t history[kHistory]; | |
82 // The predictor bit is either constant 0 or 1, or one of the bits from the | |
83 // history. | |
84 for (int predictor_bit = -2; predictor_bit < 32; predictor_bit++) { | |
85 // The predicted bit is one of the bits from the PRNG. | |
86 for (int ago = 0; ago < kHistory; ago++) { | |
87 // We don't want to check whether each bit predicts itself. | |
88 if (ago == 0 && predictor_bit == random_bit) continue; | |
89 | |
90 // Enter the new random value into the history | |
91 for (int i = ago; i >= 0; i--) { | |
92 history[i] = bit_cast<uint32_t>(rng->NextInt()); | |
93 } | |
94 | |
95 // Find out how many of the bits are the same as the prediction bit. | |
96 int m = 0; | |
97 for (int i = 0; i < kRepeats; i++) { | |
98 v8::HandleScope scope(isolate); | |
99 uint32_t random = bit_cast<uint32_t>(rng->NextInt()); | |
100 for (int j = ago - 1; j >= 0; j--) history[j + 1] = history[j]; | |
101 history[0] = random; | |
102 | |
103 int predicted; | |
104 if (predictor_bit >= 0) { | |
105 predicted = (history[ago] >> predictor_bit) & 1; | |
106 } else { | |
107 predicted = predictor_bit == -2 ? 0 : 1; | |
108 } | |
109 int bit = (random >> random_bit) & 1; | |
110 if (bit == predicted) m++; | |
111 } | |
112 | |
113 // Chi squared analysis for k = 2 (2, states: same/not-same) and one | |
114 // degree of freedom (k - 1). | |
115 double chi_squared = ChiSquared(m, kRepeats); | |
116 if (chi_squared > 24) { | |
117 int percent = static_cast<int>(m * 100.0 / kRepeats); | |
118 if (predictor_bit < 0) { | |
119 PrintF("Bit %d is %d %d%% of the time\n", random_bit, | |
120 predictor_bit == -2 ? 0 : 1, percent); | |
121 } else { | |
122 PrintF("Bit %d is the same as bit %d %d ago %d%% of the time\n", | |
123 random_bit, predictor_bit, ago, percent); | |
124 } | |
125 } | |
126 | |
127 // For 1 degree of freedom this corresponds to 1 in a million. We are | |
128 // running ~8000 tests, so that would be surprising. | |
129 CHECK(chi_squared <= 24); | |
130 | |
131 // If the predictor bit is a fixed 0 or 1 then it makes no sense to | |
132 // repeat the test with a different age. | |
133 if (predictor_bit < 0) break; | |
134 } | |
135 } | |
136 } | |
137 | |
138 | |
139 #define TEST_RANDOM_BIT(BIT) \ | |
140 TEST(RandomBitCorrelations##BIT) { RandomBitCorrelation(BIT); } | |
141 | |
142 TEST_RANDOM_BIT(0) | |
143 TEST_RANDOM_BIT(1) | |
144 TEST_RANDOM_BIT(2) | |
145 TEST_RANDOM_BIT(3) | |
146 TEST_RANDOM_BIT(4) | |
147 TEST_RANDOM_BIT(5) | |
148 TEST_RANDOM_BIT(6) | |
149 TEST_RANDOM_BIT(7) | |
150 TEST_RANDOM_BIT(8) | |
151 TEST_RANDOM_BIT(9) | |
152 TEST_RANDOM_BIT(10) | |
153 TEST_RANDOM_BIT(11) | |
154 TEST_RANDOM_BIT(12) | |
155 TEST_RANDOM_BIT(13) | |
156 TEST_RANDOM_BIT(14) | |
157 TEST_RANDOM_BIT(15) | |
158 TEST_RANDOM_BIT(16) | |
159 TEST_RANDOM_BIT(17) | |
160 TEST_RANDOM_BIT(18) | |
161 TEST_RANDOM_BIT(19) | |
162 TEST_RANDOM_BIT(20) | |
163 TEST_RANDOM_BIT(21) | |
164 TEST_RANDOM_BIT(22) | |
165 TEST_RANDOM_BIT(23) | |
166 TEST_RANDOM_BIT(24) | |
167 TEST_RANDOM_BIT(25) | |
168 TEST_RANDOM_BIT(26) | |
169 TEST_RANDOM_BIT(27) | |
170 TEST_RANDOM_BIT(28) | |
171 TEST_RANDOM_BIT(29) | |
172 TEST_RANDOM_BIT(30) | |
173 TEST_RANDOM_BIT(31) | |
174 | |
175 #undef TEST_RANDOM_BIT | |
OLD | NEW |