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 // TODO(srdjan): Remove the 'limit' check once optimizing comparison of | |
118 // Smi-s with Mint constants. | |
119 final limit = 0x3FFFFFFF; | 117 final limit = 0x3FFFFFFF; |
120 if (max <= 0 || ((max > limit) && (max > _POW2_32))) { | 118 if (max <= 0 || ((max > limit) && (max > _POW2_32))) { |
121 throw new ArgumentError("max must be positive and < 2^32:" | 119 throw new ArgumentError("max must be positive and < 2^32:" |
122 " $max"); | 120 " $max"); |
123 } | 121 } |
124 if ((max & -max) == max) { | 122 if ((max & -max) == max) { |
125 // Fast case for powers of two. | 123 // Fast case for powers of two. |
126 _nextState(); | 124 _nextState(); |
127 return _state[kSTATE_LO] & (max - 1); | 125 return _state[kSTATE_LO] & (max - 1); |
128 } | 126 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 static Uint32List _setupSeed(int seed) native "Random_setupSeed"; | 160 static Uint32List _setupSeed(int seed) native "Random_setupSeed"; |
163 // Get a seed from the VM's random number provider. | 161 // Get a seed from the VM's random number provider. |
164 static Uint32List _initialSeed() native "Random_initialSeed"; | 162 static Uint32List _initialSeed() native "Random_initialSeed"; |
165 | 163 |
166 static int _nextSeed() { | 164 static int _nextSeed() { |
167 // Trigger the PRNG once to change the internal state. | 165 // Trigger the PRNG once to change the internal state. |
168 _prng._nextState(); | 166 _prng._nextState(); |
169 return _prng._state[kSTATE_LO]; | 167 return _prng._state[kSTATE_LO]; |
170 } | 168 } |
171 } | 169 } |
OLD | NEW |