| 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]. |
| 11 @patch | 11 @patch num pow(num x, num exponent) { |
| 12 num pow(num x, num exponent) { | |
| 13 if ((x is int) && (exponent is int) && (exponent >= 0)) { | 12 if ((x is int) && (exponent is int) && (exponent >= 0)) { |
| 14 return _intPow(x, exponent); | 13 return _intPow(x, exponent); |
| 15 } | 14 } |
| 16 return _doublePow(x.toDouble(), exponent.toDouble()); | 15 return _doublePow(x.toDouble(), exponent.toDouble()); |
| 17 } | 16 } |
| 18 | 17 |
| 19 double _doublePow(double base, double exponent) { | 18 double _doublePow(double base, double exponent) { |
| 20 if (exponent == 0.0) { | 19 if (exponent == 0.0) { |
| 21 return 1.0; // ECMA-262 15.8.2.13 | 20 return 1.0; // ECMA-262 15.8.2.13 |
| 22 } | 21 } |
| 23 // Speed up simple cases. | 22 // Speed up simple cases. |
| 24 if (exponent == 1.0) return base; | 23 if (exponent == 1.0) return base; |
| 25 if (exponent == 2.0) return base * base; | 24 if (exponent == 2.0) return base * base; |
| 26 if (exponent == 3.0) return base * base * base; | 25 if (exponent == 3.0) return base * base * base; |
| 27 | 26 |
| 28 if (base == 1.0) return 1.0; | 27 if (base == 1.0) return 1.0; |
| 29 | 28 |
| 30 if (base.isNaN || exponent.isNaN) { | 29 if (base.isNaN || exponent.isNaN) { |
| 31 return double.NAN; | 30 return double.NAN; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 50 } | 49 } |
| 51 exponent >>= 1; | 50 exponent >>= 1; |
| 52 // Skip unnecessary operation (can overflow to Mint or Bigint). | 51 // Skip unnecessary operation (can overflow to Mint or Bigint). |
| 53 if (exponent != 0) { | 52 if (exponent != 0) { |
| 54 base *= base; | 53 base *= base; |
| 55 } | 54 } |
| 56 } | 55 } |
| 57 return result; | 56 return result; |
| 58 } | 57 } |
| 59 | 58 |
| 60 @patch | 59 @patch double atan2(num a, num b) => _atan2(a.toDouble(), b.toDouble()); |
| 61 double atan2(num a, num b) => _atan2(a.toDouble(), b.toDouble()); | 60 @patch double sin(num x) => _sin(x.toDouble()); |
| 62 @patch | 61 @patch double cos(num x) => _cos(x.toDouble()); |
| 63 double sin(num x) => _sin(x.toDouble()); | 62 @patch double tan(num x) => _tan(x.toDouble()); |
| 64 @patch | 63 @patch double acos(num x) => _acos(x.toDouble()); |
| 65 double cos(num x) => _cos(x.toDouble()); | 64 @patch double asin(num x) => _asin(x.toDouble()); |
| 66 @patch | 65 @patch double atan(num x) => _atan(x.toDouble()); |
| 67 double tan(num x) => _tan(x.toDouble()); | 66 @patch double sqrt(num x) => _sqrt(x.toDouble()); |
| 68 @patch | 67 @patch double exp(num x) => _exp(x.toDouble()); |
| 69 double acos(num x) => _acos(x.toDouble()); | 68 @patch double log(num x) => _log(x.toDouble()); |
| 70 @patch | |
| 71 double asin(num x) => _asin(x.toDouble()); | |
| 72 @patch | |
| 73 double atan(num x) => _atan(x.toDouble()); | |
| 74 @patch | |
| 75 double sqrt(num x) => _sqrt(x.toDouble()); | |
| 76 @patch | |
| 77 double exp(num x) => _exp(x.toDouble()); | |
| 78 @patch | |
| 79 double log(num x) => _log(x.toDouble()); | |
| 80 | 69 |
| 81 double _atan2(double a, double b) native "Math_atan2"; | 70 double _atan2(double a, double b) native "Math_atan2"; |
| 82 double _sin(double x) native "Math_sin"; | 71 double _sin(double x) native "Math_sin"; |
| 83 double _cos(double x) native "Math_cos"; | 72 double _cos(double x) native "Math_cos"; |
| 84 double _tan(double x) native "Math_tan"; | 73 double _tan(double x) native "Math_tan"; |
| 85 double _acos(double x) native "Math_acos"; | 74 double _acos(double x) native "Math_acos"; |
| 86 double _asin(double x) native "Math_asin"; | 75 double _asin(double x) native "Math_asin"; |
| 87 double _atan(double x) native "Math_atan"; | 76 double _atan(double x) native "Math_atan"; |
| 88 double _sqrt(double x) native "Math_sqrt"; | 77 double _sqrt(double x) native "Math_sqrt"; |
| 89 double _exp(double x) native "Math_exp"; | 78 double _exp(double x) native "Math_exp"; |
| 90 double _log(double x) native "Math_log"; | 79 double _log(double x) native "Math_log"; |
| 91 | 80 |
| 81 |
| 92 // TODO(iposva): Handle patch methods within a patch class correctly. | 82 // TODO(iposva): Handle patch methods within a patch class correctly. |
| 93 @patch | 83 @patch class Random { |
| 94 class Random { | 84 |
| 95 @patch | 85 @patch factory Random([int seed]) { |
| 96 factory Random([int seed]) { | |
| 97 var state = _Random._setupSeed((seed == null) ? _Random._nextSeed() : seed); | 86 var state = _Random._setupSeed((seed == null) ? _Random._nextSeed() : seed); |
| 98 // Crank a couple of times to distribute the seed bits a bit further. | 87 // Crank a couple of times to distribute the seed bits a bit further. |
| 99 return new _Random._withState(state) | 88 return new _Random._withState(state).._nextState() |
| 100 .._nextState() | 89 .._nextState() |
| 101 .._nextState() | 90 .._nextState() |
| 102 .._nextState() | 91 .._nextState(); |
| 103 .._nextState(); | |
| 104 } | 92 } |
| 105 | 93 |
| 106 @patch | 94 @patch factory Random.secure() { |
| 107 factory Random.secure() { | |
| 108 return new _SecureRandom(); | 95 return new _SecureRandom(); |
| 109 } | 96 } |
| 110 } | 97 } |
| 111 | 98 |
| 99 |
| 112 class _Random implements Random { | 100 class _Random implements Random { |
| 113 // Internal state of the random number generator. | 101 // Internal state of the random number generator. |
| 114 final Uint32List _state; | 102 final Uint32List _state; |
| 115 static const _kSTATE_LO = 0; | 103 static const _kSTATE_LO = 0; |
| 116 static const _kSTATE_HI = 1; // Unused in Dart code. | 104 static const _kSTATE_HI = 1; // Unused in Dart code. |
| 117 | 105 |
| 118 _Random._withState(this._state); | 106 _Random._withState(this._state); |
| 119 | 107 |
| 120 // The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32. | 108 // The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32. |
| 121 // http://en.wikipedia.org/wiki/Multiply-with-carry | 109 // http://en.wikipedia.org/wiki/Multiply-with-carry |
| 122 // 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. |
| 123 | 111 |
| 124 // Implements: | 112 // Implements: |
| 125 // var state = | 113 // var state = |
| 126 // ((_A * (_state[_kSTATE_LO])) + _state[_kSTATE_HI]) & ((1 << 64) - 1); | 114 // ((_A * (_state[_kSTATE_LO])) + _state[_kSTATE_HI]) & ((1 << 64) - 1); |
| 127 // _state[_kSTATE_LO] = state & ((1 << 32) - 1); | 115 // _state[_kSTATE_LO] = state & ((1 << 32) - 1); |
| 128 // _state[_kSTATE_HI] = state >> 32; | 116 // _state[_kSTATE_HI] = state >> 32; |
| 129 // 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 |
| 130 // fail with --throw_on_javascript_int_overflow. | 118 // fail with --throw_on_javascript_int_overflow. |
| 131 // TODO(regis): Implement in Dart and remove Random_nextState in math.cc. | 119 // TODO(regis): Implement in Dart and remove Random_nextState in math.cc. |
| 132 void _nextState() native "Random_nextState"; | 120 void _nextState() native "Random_nextState"; |
| 133 | 121 |
| 134 int nextInt(int max) { | 122 int nextInt(int max) { |
| 135 const limit = 0x3FFFFFFF; | 123 const limit = 0x3FFFFFFF; |
| 136 if ((max <= 0) || ((max > limit) && (max > _POW2_32))) { | 124 if ((max <= 0) || ((max > limit) && (max > _POW2_32))) { |
| 137 throw new RangeError.range( | 125 throw new RangeError.range(max, 1, _POW2_32, "max", |
| 138 max, 1, _POW2_32, "max", "Must be positive and <= 2^32"); | 126 "Must be positive and <= 2^32"); |
| 139 } | 127 } |
| 140 if ((max & -max) == max) { | 128 if ((max & -max) == max) { |
| 141 // Fast case for powers of two. | 129 // Fast case for powers of two. |
| 142 _nextState(); | 130 _nextState(); |
| 143 return _state[_kSTATE_LO] & (max - 1); | 131 return _state[_kSTATE_LO] & (max - 1); |
| 144 } | 132 } |
| 145 | 133 |
| 146 var rnd32; | 134 var rnd32; |
| 147 var result; | 135 var result; |
| 148 do { | 136 do { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 178 // Get a seed from the VM's random number provider. | 166 // Get a seed from the VM's random number provider. |
| 179 static Uint32List _initialSeed() native "Random_initialSeed"; | 167 static Uint32List _initialSeed() native "Random_initialSeed"; |
| 180 | 168 |
| 181 static int _nextSeed() { | 169 static int _nextSeed() { |
| 182 // Trigger the PRNG once to change the internal state. | 170 // Trigger the PRNG once to change the internal state. |
| 183 _prng._nextState(); | 171 _prng._nextState(); |
| 184 return _prng._state[_kSTATE_LO]; | 172 return _prng._state[_kSTATE_LO]; |
| 185 } | 173 } |
| 186 } | 174 } |
| 187 | 175 |
| 176 |
| 188 class _SecureRandom implements Random { | 177 class _SecureRandom implements Random { |
| 189 _SecureRandom() { | 178 _SecureRandom() { |
| 190 // Throw early in constructor if entropy source is not hooked up. | 179 // Throw early in constructor if entropy source is not hooked up. |
| 191 _getBytes(1); | 180 _getBytes(1); |
| 192 } | 181 } |
| 193 | 182 |
| 194 // Return count bytes of entropy as a positive integer; count <= 8. | 183 // Return count bytes of entropy as a positive integer; count <= 8. |
| 195 static int _getBytes(int count) native "SecureRandom_getBytes"; | 184 static int _getBytes(int count) native "SecureRandom_getBytes"; |
| 196 | 185 |
| 197 int nextInt(int max) { | 186 int nextInt(int max) { |
| 198 RangeError.checkValueInInterval( | 187 RangeError.checkValueInInterval( |
| 199 max, 1, _POW2_32, "max", "Must be positive and <= 2^32"); | 188 max, 1, _POW2_32, "max", "Must be positive and <= 2^32"); |
| 200 final byteCount = ((max - 1).bitLength + 7) >> 3; | 189 final byteCount = ((max - 1).bitLength + 7) >> 3; |
| 201 if (byteCount == 0) { | 190 if (byteCount == 0) { |
| 202 return 0; // Not random if max == 1. | 191 return 0; // Not random if max == 1. |
| 203 } | 192 } |
| 204 var rnd; | 193 var rnd; |
| 205 var result; | 194 var result; |
| 206 do { | 195 do { |
| 207 rnd = _getBytes(byteCount); | 196 rnd = _getBytes(byteCount); |
| 208 result = rnd % max; | 197 result = rnd % max; |
| 209 } while ((rnd - result + max) > (1 << (byteCount << 3))); | 198 } while ((rnd - result + max) > (1 << (byteCount << 3))); |
| 210 return result; | 199 return result; |
| 211 } | 200 } |
| 212 | 201 |
| 213 double nextDouble() { | 202 double nextDouble() { |
| 214 return (_getBytes(7) >> 3) / _POW2_53_D; | 203 return (_getBytes(7) >> 3) / _POW2_53_D; |
| 215 } | 204 } |
| 216 | 205 |
| 217 bool nextBool() { | 206 bool nextBool() { |
| 218 return _getBytes(1).isEven; | 207 return _getBytes(1).isEven; |
| 219 } | 208 } |
| 220 | 209 |
| 221 // Constants used by the algorithm. | 210 // Constants used by the algorithm. |
| 222 static const _POW2_32 = 1 << 32; | 211 static const _POW2_32 = 1 << 32; |
| 223 static const _POW2_53_D = 1.0 * (1 << 53); | 212 static const _POW2_53_D = 1.0 * (1 << 53); |
| 224 } | 213 } |
| 214 |
| OLD | NEW |