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

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

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

Powered by Google App Engine
This is Rietveld 408576698