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

Side by Side Diff: runtime/lib/math_patch.dart

Issue 199263005: - Avoid exposing the 64-bit state of the PRNG to Dart code. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 9 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import "dart:typed_data"; 5 import "dart:typed_data";
6 6
7 // A VM patch of the dart:math library. 7 // A VM patch of the dart:math library.
8 8
9 // If [x] is an [int] and [exponent] is a non-negative [int], the result is 9 // If [x] is an [int] and [exponent] is a non-negative [int], the result is
10 // an [int], otherwise the result is a [double]. 10 // an [int], otherwise the result is a [double].
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 double _atan(double x) native "Math_atan"; 64 double _atan(double x) native "Math_atan";
65 double _sqrt(double x) native "Math_sqrt"; 65 double _sqrt(double x) native "Math_sqrt";
66 double _exp(double x) native "Math_exp"; 66 double _exp(double x) native "Math_exp";
67 double _log(double x) native "Math_log"; 67 double _log(double x) native "Math_log";
68 68
69 69
70 // TODO(iposva): Handle patch methods within a patch class correctly. 70 // TODO(iposva): Handle patch methods within a patch class correctly.
71 patch class Random { 71 patch class Random {
72 72
73 /*patch*/ factory Random([int seed]) { 73 /*patch*/ factory Random([int seed]) {
74 if (seed == null) { 74 var state = _Random._setupSeed((seed == null) ? _Random._nextSeed() : seed);
75 seed = _Random._nextSeed();
76 }
77 // Crank a couple of times to distribute the seed bits a bit further. 75 // Crank a couple of times to distribute the seed bits a bit further.
78 return new _Random().._setupSeed(seed) 76 return new _Random._withState(state).._nextState()
79 .._nextState() 77 .._nextState()
80 .._nextState() 78 .._nextState()
81 .._nextState() 79 .._nextState();
82 .._nextState();
83 } 80 }
84 } 81 }
85 82
86 83
87 class _Random implements Random { 84 class _Random implements Random {
88 // Internal state of the random number generator. 85 // Internal state of the random number generator.
89 final _state = new Uint32List(2); 86 final _state;
90 static const kSTATE_LO = 0; 87 static const kSTATE_LO = 0;
91 static const kSTATE_HI = 1; 88 static const kSTATE_HI = 1;
92 89
93 // Implements: 90 _Random._withState(Uint32List this._state);
94 // do {
95 // seed = (seed + 0x5A17) & _Random._MASK_64;
96 // } while (seed == 0);
97 // _state[kSTATE_LO] = seed & _MASK_32;
98 // _state[kSTATE_HI] = seed >> 32;
99 // This is a native to prevent 64-bit operations in Dart, which
100 // fail with --throw_on_javascript_int_overflow.
101 void _setupSeed(int seed) native "Random_setupSeed";
102 91
103 // The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32. 92 // The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32.
104 // http://en.wikipedia.org/wiki/Multiply-with-carry 93 // http://en.wikipedia.org/wiki/Multiply-with-carry
105 // The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1. 94 // The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1.
106 95
107 // Implements: 96 // Implements:
108 // var state = ((_A * (_state[kSTATE_LO])) + _state[kSTATE_HI]) & _MASK_64; 97 // var state = ((_A * (_state[kSTATE_LO])) + _state[kSTATE_HI]) & _MASK_64;
109 // _state[kSTATE_LO] = state & _MASK_32; 98 // _state[kSTATE_LO] = state & _MASK_32;
110 // _state[kSTATE_HI] = state >> 32; 99 // _state[kSTATE_HI] = state >> 32;
111 // This is a native to prevent 64-bit operations in Dart, which 100 // This is a native to prevent 64-bit operations in Dart, which
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 // Constants used by the algorithm or masking. 136 // Constants used by the algorithm or masking.
148 static const _MASK_32 = (1 << 32) - 1; 137 static const _MASK_32 = (1 << 32) - 1;
149 static const _MASK_64 = (1 << 64) - 1; 138 static const _MASK_64 = (1 << 64) - 1;
150 static const _POW2_32 = 1 << 32; 139 static const _POW2_32 = 1 << 32;
151 static const _POW2_53_D = 1.0 * (1 << 53); 140 static const _POW2_53_D = 1.0 * (1 << 53);
152 static const _POW2_27_D = 1.0 * (1 << 27); 141 static const _POW2_27_D = 1.0 * (1 << 27);
153 142
154 static const _A = 0xffffda61; 143 static const _A = 0xffffda61;
155 144
156 // Use a singleton Random object to get a new seed if no seed was passed. 145 // Use a singleton Random object to get a new seed if no seed was passed.
157 static var _prng = new Random(_initialSeed()); 146 static var _prng = new _Random._withState(_initialSeed());
158 147
159 static int _initialSeed() native "Random_initialSeed"; 148 // This is a native to prevent 64-bit operations in Dart, which
149 // fail with --throw_on_javascript_int_overflow.
150 static Uint32List _setupSeed(int seed) native "Random_setupSeed";
151 // Get a seed from the VM's random number provider.
152 static Uint32List _initialSeed() native "Random_initialSeed";
160 153
161 static int _nextSeed() { 154 static int _nextSeed() {
162 // Trigger the PRNG once to change the internal state. 155 // Trigger the PRNG once to change the internal state.
163 _prng._nextState(); 156 _prng._nextState();
164 return _prng._state[kSTATE_LO]; 157 return _prng._state[kSTATE_LO];
165 } 158 }
166 } 159 }
OLDNEW
« runtime/lib/math.cc ('K') | « runtime/lib/math.cc ('k') | runtime/vm/bootstrap_natives.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698