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

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

Issue 243923002: Revert unused optimization in math.pow and implement same code in Dart as is done in the inlined ve… (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/vm/intermediate_language.h » ('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 num pow(num x, num exponent) {
12 if ((x is int) && (exponent is int) && (exponent >= 0)) { 12 if ((x is int) && (exponent is int) && (exponent >= 0)) {
13 return _intPow(x, exponent); 13 return _intPow(x, exponent);
14 } 14 }
15 return _doublePow(x.toDouble(), exponent.toDouble()); 15 return _doublePow(x.toDouble(), exponent.toDouble());
16 } 16 }
17 17
18 double _doublePow(double base, double exponent) { 18 double _doublePow(double base, double exponent) {
19 if (exponent == 0.0) { 19 if (exponent == 0.0) {
20 return 1.0; // ECMA-262 15.8.2.13 20 return 1.0; // ECMA-262 15.8.2.13
21 } 21 }
22 if (base == 1.0) return 1.0; 22 if (base == 1.0) return 1.0;
23 if (exponent == 1.0) return base; 23
24 if (exponent == 2.0) return base * base;
25 if (exponent == 3.0) return base * base * base;
26
27 if (base.isNaN || exponent.isNaN) { 24 if (base.isNaN || exponent.isNaN) {
28 return double.NAN; 25 return double.NAN;
29 } 26 }
30 return _pow(base, exponent); 27 if ((base != -double.INFINITY) && (exponent == 0.5)) {
28 if (base == 0.0) {
29 return 0.0;
30 }
31 return sqrt(base);
32 }
33 return _pow(base.toDouble(), exponent.toDouble());
31 } 34 }
32 35
33 double _pow(double base, double exponent) native "Math_doublePow"; 36 double _pow(double base, double exponent) native "Math_doublePow";
34 37
35 int _intPow(int base, int exponent) { 38 int _intPow(int base, int exponent) {
36 // Exponentiation by squaring. 39 // Exponentiation by squaring.
37 int result = 1; 40 int result = 1;
38 while (exponent != 0) { 41 while (exponent != 0) {
39 if ((exponent & 1) == 1) { 42 if ((exponent & 1) == 1) {
40 result *= base; 43 result *= base;
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 static Uint32List _setupSeed(int seed) native "Random_setupSeed"; 157 static Uint32List _setupSeed(int seed) native "Random_setupSeed";
155 // Get a seed from the VM's random number provider. 158 // Get a seed from the VM's random number provider.
156 static Uint32List _initialSeed() native "Random_initialSeed"; 159 static Uint32List _initialSeed() native "Random_initialSeed";
157 160
158 static int _nextSeed() { 161 static int _nextSeed() {
159 // Trigger the PRNG once to change the internal state. 162 // Trigger the PRNG once to change the internal state.
160 _prng._nextState(); 163 _prng._nextState();
161 return _prng._state[kSTATE_LO]; 164 return _prng._state[kSTATE_LO];
162 } 165 }
163 } 166 }
OLDNEW
« no previous file with comments | « no previous file | runtime/vm/intermediate_language.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698