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

Side by Side Diff: pkg/dev_compiler/tool/input_sdk/patch/math_patch.dart

Issue 2752163002: Format all dart dev compiler files (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
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 // Patch file for dart:math library. 5 // Patch file for dart:math library.
6 import 'dart:_foreign_helper' show JS; 6 import 'dart:_foreign_helper' show JS;
7 import 'dart:_js_helper' show patch, checkNum; 7 import 'dart:_js_helper' show patch, checkNum;
8 import 'dart:typed_data' show ByteData; 8 import 'dart:typed_data' show ByteData;
9 9
10 @patch 10 @patch
11 num/*=T*/ min/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b) 11 num/*=T*/ min/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b) =>
12 => JS('num', r'Math.min(#, #)', checkNum(a), checkNum(b)) as num/*=T*/; 12 JS('num', r'Math.min(#, #)', checkNum(a), checkNum(b)) as num/*=T*/;
13 13
14 @patch 14 @patch
15 num/*=T*/ max/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b) 15 num/*=T*/ max/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b) =>
16 => JS('num', r'Math.max(#, #)', checkNum(a), checkNum(b)) as num/*=T*/; 16 JS('num', r'Math.max(#, #)', checkNum(a), checkNum(b)) as num/*=T*/;
17 17
18 @patch 18 @patch
19 double sqrt(num x) 19 double sqrt(num x) => JS('num', r'Math.sqrt(#)', checkNum(x));
20 => JS('num', r'Math.sqrt(#)', checkNum(x));
21 20
22 @patch 21 @patch
23 double sin(num x) 22 double sin(num x) => JS('num', r'Math.sin(#)', checkNum(x));
24 => JS('num', r'Math.sin(#)', checkNum(x));
25 23
26 @patch 24 @patch
27 double cos(num x) 25 double cos(num x) => JS('num', r'Math.cos(#)', checkNum(x));
28 => JS('num', r'Math.cos(#)', checkNum(x));
29 26
30 @patch 27 @patch
31 double tan(num x) 28 double tan(num x) => JS('num', r'Math.tan(#)', checkNum(x));
32 => JS('num', r'Math.tan(#)', checkNum(x));
33 29
34 @patch 30 @patch
35 double acos(num x) 31 double acos(num x) => JS('num', r'Math.acos(#)', checkNum(x));
36 => JS('num', r'Math.acos(#)', checkNum(x));
37 32
38 @patch 33 @patch
39 double asin(num x) 34 double asin(num x) => JS('num', r'Math.asin(#)', checkNum(x));
40 => JS('num', r'Math.asin(#)', checkNum(x));
41 35
42 @patch 36 @patch
43 double atan(num x) 37 double atan(num x) => JS('num', r'Math.atan(#)', checkNum(x));
44 => JS('num', r'Math.atan(#)', checkNum(x));
45 38
46 @patch 39 @patch
47 double atan2(num a, num b) 40 double atan2(num a, num b) =>
48 => JS('num', r'Math.atan2(#, #)', checkNum(a), checkNum(b)); 41 JS('num', r'Math.atan2(#, #)', checkNum(a), checkNum(b));
49 42
50 @patch 43 @patch
51 double exp(num x) 44 double exp(num x) => JS('num', r'Math.exp(#)', checkNum(x));
52 => JS('num', r'Math.exp(#)', checkNum(x));
53 45
54 @patch 46 @patch
55 double log(num x) 47 double log(num x) => JS('num', r'Math.log(#)', checkNum(x));
56 => JS('num', r'Math.log(#)', checkNum(x));
57 48
58 @patch 49 @patch
59 num pow(num x, num exponent) { 50 num pow(num x, num exponent) {
60 checkNum(x); 51 checkNum(x);
61 checkNum(exponent); 52 checkNum(exponent);
62 return JS('num', r'Math.pow(#, #)', x, exponent); 53 return JS('num', r'Math.pow(#, #)', x, exponent);
63 } 54 }
64 55
65 const int _POW2_32 = 0x100000000; 56 const int _POW2_32 = 0x100000000;
66 57
(...skipping 25 matching lines...) Expand all
92 * the range from 0.0, inclusive, to 1.0, exclusive. 83 * the range from 0.0, inclusive, to 1.0, exclusive.
93 */ 84 */
94 double nextDouble() => JS("double", "Math.random()"); 85 double nextDouble() => JS("double", "Math.random()");
95 86
96 /** 87 /**
97 * Generates a random boolean value. 88 * Generates a random boolean value.
98 */ 89 */
99 bool nextBool() => JS("bool", "Math.random() < 0.5"); 90 bool nextBool() => JS("bool", "Math.random() < 0.5");
100 } 91 }
101 92
102
103 class _Random implements Random { 93 class _Random implements Random {
104 // Constants used by the algorithm or masking. 94 // Constants used by the algorithm or masking.
105 static const double _POW2_53_D = 1.0 * (0x20000000000000); 95 static const double _POW2_53_D = 1.0 * (0x20000000000000);
106 static const double _POW2_27_D = 1.0 * (1 << 27); 96 static const double _POW2_27_D = 1.0 * (1 << 27);
107 static const int _MASK32 = 0xFFFFFFFF; 97 static const int _MASK32 = 0xFFFFFFFF;
108 98
109 // State comprised of two unsigned 32 bit integers. 99 // State comprised of two unsigned 32 bit integers.
110 int _lo = 0; 100 int _lo = 0;
111 int _hi = 0; 101 int _hi = 0;
112 102
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 // http://en.wikipedia.org/wiki/Multiply-with-carry 185 // http://en.wikipedia.org/wiki/Multiply-with-carry
196 // The constant A (0xFFFFDA61) is selected from "Numerical Recipes 3rd 186 // The constant A (0xFFFFDA61) is selected from "Numerical Recipes 3rd
197 // Edition" p.348 B1. 187 // Edition" p.348 B1.
198 188
199 // Implements: 189 // Implements:
200 // var state = (A * _lo + _hi) & _MASK_64; 190 // var state = (A * _lo + _hi) & _MASK_64;
201 // _lo = state & _MASK_32; 191 // _lo = state & _MASK_32;
202 // _hi = state >> 32; 192 // _hi = state >> 32;
203 void _nextState() { 193 void _nextState() {
204 // Simulate (0xFFFFDA61 * lo + hi) without overflowing 53 bits. 194 // Simulate (0xFFFFDA61 * lo + hi) without overflowing 53 bits.
205 int tmpHi = 0xFFFF0000 * _lo; // At most 48 bits of significant result. 195 int tmpHi = 0xFFFF0000 * _lo; // At most 48 bits of significant result.
206 int tmpHiLo = tmpHi & _MASK32; // Get the lower 32 bits. 196 int tmpHiLo = tmpHi & _MASK32; // Get the lower 32 bits.
207 int tmpHiHi = tmpHi - tmpHiLo; // And just the upper 32 bits. 197 int tmpHiHi = tmpHi - tmpHiLo; // And just the upper 32 bits.
208 int tmpLo = 0xDA61 * _lo; 198 int tmpLo = 0xDA61 * _lo;
209 int tmpLoLo = tmpLo & _MASK32; 199 int tmpLoLo = tmpLo & _MASK32;
210 int tmpLoHi = tmpLo - tmpLoLo; 200 int tmpLoHi = tmpLo - tmpLoLo;
211 201
212 int newLo = tmpLoLo + tmpHiLo + _hi; 202 int newLo = tmpLoLo + tmpHiLo + _hi;
213 _lo = newLo & _MASK32; 203 _lo = newLo & _MASK32;
214 int newLoHi = newLo - _lo; 204 int newLoHi = newLo - _lo;
215 _hi = ((tmpLoHi + tmpHiHi + newLoHi) ~/ _POW2_32) & _MASK32; 205 _hi = ((tmpLoHi + tmpHiHi + newLoHi) ~/ _POW2_32) & _MASK32;
216 assert(_lo < _POW2_32); 206 assert(_lo < _POW2_32);
217 assert(_hi < _POW2_32); 207 assert(_hi < _POW2_32);
(...skipping 26 matching lines...) Expand all
244 int bits27 = _lo & ((1 << 27) - 1); 234 int bits27 = _lo & ((1 << 27) - 1);
245 return (bits26 * _POW2_27_D + bits27) / _POW2_53_D; 235 return (bits26 * _POW2_27_D + bits27) / _POW2_53_D;
246 } 236 }
247 237
248 bool nextBool() { 238 bool nextBool() {
249 _nextState(); 239 _nextState();
250 return (_lo & 1) == 0; 240 return (_lo & 1) == 0;
251 } 241 }
252 } 242 }
253 243
254
255 class _JSSecureRandom implements Random { 244 class _JSSecureRandom implements Random {
256 // Reused buffer with room enough for a double. 245 // Reused buffer with room enough for a double.
257 final _buffer = new ByteData(8); 246 final _buffer = new ByteData(8);
258 247
259 _JSSecureRandom() { 248 _JSSecureRandom() {
260 var crypto = JS("", "self.crypto"); 249 var crypto = JS("", "self.crypto");
261 if (crypto != null) { 250 if (crypto != null) {
262 var getRandomValues = JS("", "#.getRandomValues", crypto); 251 var getRandomValues = JS("", "#.getRandomValues", crypto);
263 if (getRandomValues != null) { 252 if (getRandomValues != null) {
264 return; 253 return;
265 } 254 }
266 } 255 }
267 throw new UnsupportedError( 256 throw new UnsupportedError(
268 "No source of cryptographically secure random numbers available."); 257 "No source of cryptographically secure random numbers available.");
269 } 258 }
270 259
271 /// Fill _buffer from [start] to `start + length` with random bytes. 260 /// Fill _buffer from [start] to `start + length` with random bytes.
272 void _getRandomBytes(int start, int length) { 261 void _getRandomBytes(int start, int length) {
273 JS("void", "crypto.getRandomValues(#)", 262 JS("void", "crypto.getRandomValues(#)",
274 _buffer.buffer.asUint8List(start, length)); 263 _buffer.buffer.asUint8List(start, length));
275 } 264 }
276 265
277 bool nextBool() { 266 bool nextBool() {
278 _getRandomBytes(0, 1); 267 _getRandomBytes(0, 1);
279 return _buffer.getUint8(0).isOdd; 268 return _buffer.getUint8(0).isOdd;
280 } 269 }
281 270
282 double nextDouble() { 271 double nextDouble() {
283 _getRandomBytes(1, 7); 272 _getRandomBytes(1, 7);
284 // Set top bits 12 of double to 0x3FF which is the exponent for numbers 273 // Set top bits 12 of double to 0x3FF which is the exponent for numbers
285 // between 1.0 and 2.0. 274 // between 1.0 and 2.0.
286 _buffer.setUint8(0, 0x3F); 275 _buffer.setUint8(0, 0x3F);
287 int highByte = _buffer.getUint8(1); 276 int highByte = _buffer.getUint8(1);
288 _buffer.setUint8(1, highByte | 0xF0); 277 _buffer.setUint8(1, highByte | 0xF0);
289 278
290 // Buffer now contains double in the range [1.0-2.0) 279 // Buffer now contains double in the range [1.0-2.0)
291 // with 52 bits of entropy (not 53). 280 // with 52 bits of entropy (not 53).
292 // To get 53 bits, we extract the 53rd bit from higthByte before 281 // To get 53 bits, we extract the 53rd bit from higthByte before
293 // overwriting it, and add that as a least significant bit. 282 // overwriting it, and add that as a least significant bit.
294 // The getFloat64 method is big-endian as default. 283 // The getFloat64 method is big-endian as default.
295 double result = _buffer.getFloat64(0) - 1.0; 284 double result = _buffer.getFloat64(0) - 1.0;
296 if (highByte & 0x10 != 0) { 285 if (highByte & 0x10 != 0) {
297 result += 1.1102230246251565e-16; // pow(2,-53). 286 result += 1.1102230246251565e-16; // pow(2,-53).
298 } 287 }
299 return result; 288 return result;
300 } 289 }
301 290
302 int nextInt(int max) { 291 int nextInt(int max) {
303 if (max <= 0 || max > _POW2_32) { 292 if (max <= 0 || max > _POW2_32) {
304 throw new RangeError("max must be in range 0 < max ≤ 2^32, was $max"); 293 throw new RangeError("max must be in range 0 < max ≤ 2^32, was $max");
305 } 294 }
306 int byteCount = 1; 295 int byteCount = 1;
307 if (max > 0xFF) { 296 if (max > 0xFF) {
(...skipping 21 matching lines...) Expand all
329 // last range of k*max .. 256**byteCount. 318 // last range of k*max .. 256**byteCount.
330 // TODO: Consider picking a higher byte count if the last range is a 319 // TODO: Consider picking a higher byte count if the last range is a
331 // significant portion of the entire range - a 50% chance of having 320 // significant portion of the entire range - a 50% chance of having
332 // to use two more bytes is no worse than always using one more. 321 // to use two more bytes is no worse than always using one more.
333 if (random - result + max < randomLimit) { 322 if (random - result + max < randomLimit) {
334 return result; 323 return result;
335 } 324 }
336 } 325 }
337 } 326 }
338 } 327 }
OLDNEW
« no previous file with comments | « pkg/dev_compiler/tool/input_sdk/patch/isolate_patch.dart ('k') | pkg/dev_compiler/tool/input_sdk/patch/mirrors_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698