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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 107 |
108 // Implements: | 108 // Implements: |
109 // var state = ((_A * (_state[kSTATE_LO])) + _state[kSTATE_HI]) & _MASK_64; | 109 // var state = ((_A * (_state[kSTATE_LO])) + _state[kSTATE_HI]) & _MASK_64; |
110 // _state[kSTATE_LO] = state & _MASK_32; | 110 // _state[kSTATE_LO] = state & _MASK_32; |
111 // _state[kSTATE_HI] = state >> 32; | 111 // _state[kSTATE_HI] = state >> 32; |
112 // This is a native to prevent 64-bit operations in Dart, which | 112 // This is a native to prevent 64-bit operations in Dart, which |
113 // fail with --throw_on_javascript_int_overflow. | 113 // fail with --throw_on_javascript_int_overflow. |
114 void _nextState() native "Random_nextState"; | 114 void _nextState() native "Random_nextState"; |
115 | 115 |
116 int nextInt(int max) { | 116 int nextInt(int max) { |
117 final limit = 0x3FFFFFFF; | 117 const limit = 0x3FFFFFFF; |
118 if (max <= 0 || ((max > limit) && (max > _POW2_32))) { | 118 if (max <= 0 || ((max > limit) && (max > _POW2_32))) { |
119 throw new ArgumentError("max must be positive and < 2^32:" | 119 throw new ArgumentError("max must be positive and < 2^32:" |
120 " $max"); | 120 " $max"); |
121 } | 121 } |
122 if ((max & -max) == max) { | 122 if ((max & -max) == max) { |
123 // Fast case for powers of two. | 123 // Fast case for powers of two. |
124 _nextState(); | 124 _nextState(); |
125 return _state[kSTATE_LO] & (max - 1); | 125 return _state[kSTATE_LO] & (max - 1); |
126 } | 126 } |
127 | 127 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 static Uint32List _setupSeed(int seed) native "Random_setupSeed"; | 160 static Uint32List _setupSeed(int seed) native "Random_setupSeed"; |
161 // Get a seed from the VM's random number provider. | 161 // Get a seed from the VM's random number provider. |
162 static Uint32List _initialSeed() native "Random_initialSeed"; | 162 static Uint32List _initialSeed() native "Random_initialSeed"; |
163 | 163 |
164 static int _nextSeed() { | 164 static int _nextSeed() { |
165 // Trigger the PRNG once to change the internal state. | 165 // Trigger the PRNG once to change the internal state. |
166 _prng._nextState(); | 166 _prng._nextState(); |
167 return _prng._state[kSTATE_LO]; | 167 return _prng._state[kSTATE_LO]; |
168 } | 168 } |
169 } | 169 } |
OLD | NEW |