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

Unified Diff: runtime/lib/math_patch.dart

Issue 21966003: Adapt Random class to be able to run in javascript integer compatibility mode. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: runtime/lib/math_patch.dart
===================================================================
--- runtime/lib/math_patch.dart (revision 25742)
+++ runtime/lib/math_patch.dart (working copy)
@@ -46,14 +46,12 @@
if (seed == null) {
seed = _Random._nextSeed();
}
- do {
- seed = (seed + 0x5A17) & _Random._MASK_64;
- } while (seed == 0);
// Crank a couple of times to distribute the seed bits a bit further.
- return new _Random._internal(seed).._nextState()
- .._nextState()
- .._nextState()
- .._nextState();
+ return new _Random().._setupSeed(seed)
+ .._nextState()
+ .._nextState()
+ .._nextState()
+ .._nextState();
}
}
@@ -64,20 +62,28 @@
static const kSTATE_LO = 0;
static const kSTATE_HI = 1;
- _Random._internal(state) {
- _state[kSTATE_LO] = state & _MASK_32;
- _state[kSTATE_HI] = state >> 32;
- }
+ // Implements:
+ // do {
+ // seed = (seed + 0x5A17) & _Random._MASK_64;
+ // } while (seed == 0);
+ // _state[kSTATE_LO] = seed & _MASK_32;
+ // _state[kSTATE_HI] = seed >> 32;
+ // This is a native to prevent 64-bit operations in Dart, which
+ // fail with --throw_on_javascript_int_overflow.
+ void _setupSeed(int seed) native "Random_setupSeed";
// The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32.
// http://en.wikipedia.org/wiki/Multiply-with-carry
// The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1.
- void _nextState() {
- var state = ((_A * (_state[kSTATE_LO])) + _state[kSTATE_HI]) & _MASK_64;
- _state[kSTATE_LO] = state & _MASK_32;
- _state[kSTATE_HI] = state >> 32;
- }
+ // Implements:
+ // var state = ((_A * (_state[kSTATE_LO])) + _state[kSTATE_HI]) & _MASK_64;
+ // _state[kSTATE_LO] = state & _MASK_32;
+ // _state[kSTATE_HI] = state >> 32;
+ // This is a native to prevent 64-bit operations in Dart, which
+ // fail with --throw_on_javascript_int_overflow.
+ void _nextState() native "Random_nextState";
+
int nextInt(int max) {
// TODO(srdjan): Remove the 'limit' check once optimizing comparison of
// Smi-s with Mint constants.
@@ -103,7 +109,7 @@
}
double nextDouble() {
- return ((nextInt(1 << 26) << 27) + nextInt(1 << 27)) / _POW2_53_D;
+ return ((nextInt(1 << 26) * _POW2_27_D) + nextInt(1 << 27)) / _POW2_53_D;
}
bool nextBool() {
@@ -115,6 +121,7 @@
static const _MASK_64 = (1 << 64) - 1;
static const _POW2_32 = 1 << 32;
static const _POW2_53_D = 1.0 * (1 << 53);
+ static const _POW2_27_D = 1.0 * (1 << 27);
static const _A = 0xffffda61;

Powered by Google App Engine
This is Rietveld 408576698