OLD | NEW |
---|---|
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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 // http://en.wikipedia.org/wiki/Multiply-with-carry | 109 // http://en.wikipedia.org/wiki/Multiply-with-carry |
110 // The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1. | 110 // The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1. |
111 | 111 |
112 // Implements: | 112 // Implements: |
113 // var state = | 113 // var state = |
114 // ((_A * (_state[_kSTATE_LO])) + _state[_kSTATE_HI]) & ((1 << 64) - 1); | 114 // ((_A * (_state[_kSTATE_LO])) + _state[_kSTATE_HI]) & ((1 << 64) - 1); |
115 // _state[_kSTATE_LO] = state & ((1 << 32) - 1); | 115 // _state[_kSTATE_LO] = state & ((1 << 32) - 1); |
116 // _state[_kSTATE_HI] = state >> 32; | 116 // _state[_kSTATE_HI] = state >> 32; |
117 // This is a native to prevent 64-bit operations in Dart, which | 117 // This is a native to prevent 64-bit operations in Dart, which |
118 // fail with --throw_on_javascript_int_overflow. | 118 // fail with --throw_on_javascript_int_overflow. |
119 // TODO(regis): Implement in Dart and remove Random_nextState in math.cc. | |
119 void _nextState() native "Random_nextState"; | 120 void _nextState() native "Random_nextState"; |
120 | 121 |
121 int nextInt(int max) { | 122 int nextInt(int max) { |
122 const limit = 0x3FFFFFFF; | 123 const limit = 0x3FFFFFFF; |
123 if ((max <= 0) || ((max > limit) && (max > _POW2_32))) { | 124 if ((max <= 0) || ((max > limit) && (max > _POW2_32))) { |
124 throw new RangeError.range(max, 1, _POW2_32, "max", | 125 throw new RangeError.range(max, 1, _POW2_32, "max", |
125 "Must be positive and <= 2^32"); | 126 "Must be positive and <= 2^32"); |
126 } | 127 } |
127 if ((max & -max) == max) { | 128 if ((max & -max) == max) { |
128 // Fast case for powers of two. | 129 // Fast case for powers of two. |
(...skipping 22 matching lines...) Expand all Loading... | |
151 // Constants used by the algorithm. | 152 // Constants used by the algorithm. |
152 static const _POW2_32 = 1 << 32; | 153 static const _POW2_32 = 1 << 32; |
153 static const _POW2_53_D = 1.0 * (1 << 53); | 154 static const _POW2_53_D = 1.0 * (1 << 53); |
154 static const _POW2_27_D = 1.0 * (1 << 27); | 155 static const _POW2_27_D = 1.0 * (1 << 27); |
155 | 156 |
156 static const _A = 0xffffda61; | 157 static const _A = 0xffffda61; |
157 | 158 |
158 // Use a singleton Random object to get a new seed if no seed was passed. | 159 // Use a singleton Random object to get a new seed if no seed was passed. |
159 static var _prng = new _Random._withState(_initialSeed()); | 160 static var _prng = new _Random._withState(_initialSeed()); |
160 | 161 |
162 | |
srdjan
2016/02/10 23:35:20
One line too much
regis
2016/02/10 23:42:40
Done.
| |
161 // This is a native to prevent 64-bit operations in Dart, which | 163 // This is a native to prevent 64-bit operations in Dart, which |
162 // fail with --throw_on_javascript_int_overflow. | 164 // fail with --throw_on_javascript_int_overflow. |
165 // TODO(regis): Implement here in Dart and remove native in math.cc. | |
163 static Uint32List _setupSeed(int seed) native "Random_setupSeed"; | 166 static Uint32List _setupSeed(int seed) native "Random_setupSeed"; |
164 // Get a seed from the VM's random number provider. | 167 // Get a seed from the VM's random number provider. |
165 static Uint32List _initialSeed() native "Random_initialSeed"; | 168 static Uint32List _initialSeed() native "Random_initialSeed"; |
166 | 169 |
167 static int _nextSeed() { | 170 static int _nextSeed() { |
168 // Trigger the PRNG once to change the internal state. | 171 // Trigger the PRNG once to change the internal state. |
169 _prng._nextState(); | 172 _prng._nextState(); |
170 return _prng._state[_kSTATE_LO]; | 173 return _prng._state[_kSTATE_LO]; |
171 } | 174 } |
172 } | 175 } |
(...skipping 30 matching lines...) Expand all Loading... | |
203 | 206 |
204 bool nextBool() { | 207 bool nextBool() { |
205 return _getBytes(1).isEven; | 208 return _getBytes(1).isEven; |
206 } | 209 } |
207 | 210 |
208 // Constants used by the algorithm. | 211 // Constants used by the algorithm. |
209 static const _POW2_32 = 1 << 32; | 212 static const _POW2_32 = 1 << 32; |
210 static const _POW2_53_D = 1.0 * (1 << 53); | 213 static const _POW2_53_D = 1.0 * (1 << 53); |
211 } | 214 } |
212 | 215 |
OLD | NEW |