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

Side by Side Diff: src/js/math.js

Issue 2068743002: [builtins] Unify Atanh, Cbrt and Expm1 as exports from flibm. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fixed type warning. Created 4 years, 6 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 | « src/external-reference-table.cc ('k') | src/objects.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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 (function(global, utils) { 5 (function(global, utils) {
6 "use strict"; 6 "use strict";
7 7
8 %CheckIsBootstrapping(); 8 %CheckIsBootstrapping();
9 9
10 // ------------------------------------------------------------------- 10 // -------------------------------------------------------------------
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 81
82 // ES6 draft 09-27-13, section 20.2.2.3. 82 // ES6 draft 09-27-13, section 20.2.2.3.
83 function MathAcosh(x) { 83 function MathAcosh(x) {
84 x = TO_NUMBER(x); 84 x = TO_NUMBER(x);
85 if (x < 1) return NaN; 85 if (x < 1) return NaN;
86 // Idempotent for NaN and +Infinity. 86 // Idempotent for NaN and +Infinity.
87 if (!NUMBER_IS_FINITE(x)) return x; 87 if (!NUMBER_IS_FINITE(x)) return x;
88 return %math_log(x + %math_sqrt(x + 1) * %math_sqrt(x - 1)); 88 return %math_log(x + %math_sqrt(x + 1) * %math_sqrt(x - 1));
89 } 89 }
90 90
91 // ES6 draft 09-27-13, section 20.2.2.7.
92 function MathAtanh(x) {
93 x = TO_NUMBER(x);
94 // Idempotent for +/-0.
95 if (x === 0) return x;
96 // Returns NaN for NaN and +/- Infinity.
97 if (!NUMBER_IS_FINITE(x)) return NaN;
98 return 0.5 * %math_log((1 + x) / (1 - x));
99 }
100
101 // ES6 draft 09-27-13, section 20.2.2.17. 91 // ES6 draft 09-27-13, section 20.2.2.17.
102 function MathHypot(x, y) { // Function length is 2. 92 function MathHypot(x, y) { // Function length is 2.
103 // We may want to introduce fast paths for two arguments and when 93 // We may want to introduce fast paths for two arguments and when
104 // normalization to avoid overflow is not necessary. For now, we 94 // normalization to avoid overflow is not necessary. For now, we
105 // simply assume the general case. 95 // simply assume the general case.
106 var length = arguments.length; 96 var length = arguments.length;
107 var max = 0; 97 var max = 0;
108 for (var i = 0; i < length; i++) { 98 for (var i = 0; i < length; i++) {
109 var n = MathAbs(arguments[i]); 99 var n = MathAbs(arguments[i]);
110 if (n > max) max = n; 100 if (n > max) max = n;
111 arguments[i] = n; 101 arguments[i] = n;
112 } 102 }
113 if (max === INFINITY) return INFINITY; 103 if (max === INFINITY) return INFINITY;
114 104
115 // Kahan summation to avoid rounding errors. 105 // Kahan summation to avoid rounding errors.
116 // Normalize the numbers to the largest one to avoid overflow. 106 // Normalize the numbers to the largest one to avoid overflow.
117 if (max === 0) max = 1; 107 if (max === 0) max = 1;
118 var sum = 0; 108 var sum = 0;
119 var compensation = 0; 109 var compensation = 0;
120 for (var i = 0; i < length; i++) { 110 for (var i = 0; i < length; i++) {
121 var n = arguments[i] / max; 111 var n = arguments[i] / max;
122 var summand = n * n - compensation; 112 var summand = n * n - compensation;
123 var preliminary = sum + summand; 113 var preliminary = sum + summand;
124 compensation = (preliminary - sum) - summand; 114 compensation = (preliminary - sum) - summand;
125 sum = preliminary; 115 sum = preliminary;
126 } 116 }
127 return %math_sqrt(sum) * max; 117 return %math_sqrt(sum) * max;
128 } 118 }
129 119
130 // ES6 draft 09-27-13, section 20.2.2.9.
131 // Cube root approximation, refer to: http://metamerist.com/cbrt/cbrt.htm
132 // Using initial approximation adapted from Kahan's cbrt and 4 iterations
133 // of Newton's method.
134 function MathCbrt(x) {
135 x = TO_NUMBER(x);
136 if (x == 0 || !NUMBER_IS_FINITE(x)) return x;
137 return x >= 0 ? CubeRoot(x) : -CubeRoot(-x);
138 }
139
140 macro NEWTON_ITERATION_CBRT(x, approx)
141 (1.0 / 3.0) * (x / (approx * approx) + 2 * approx);
142 endmacro
143
144 function CubeRoot(x) {
145 var approx_hi = %math_floor(%_DoubleHi(x) / 3) + 0x2A9F7893;
146 var approx = %_ConstructDouble(approx_hi | 0, 0);
147 approx = NEWTON_ITERATION_CBRT(x, approx);
148 approx = NEWTON_ITERATION_CBRT(x, approx);
149 approx = NEWTON_ITERATION_CBRT(x, approx);
150 return NEWTON_ITERATION_CBRT(x, approx);
151 }
152
153 // ------------------------------------------------------------------- 120 // -------------------------------------------------------------------
154 121
155 %InstallToContext([ 122 %InstallToContext([
156 "math_pow", MathPowJS, 123 "math_pow", MathPowJS,
157 ]); 124 ]);
158 125
159 %AddNamedProperty(GlobalMath, toStringTagSymbol, "Math", READ_ONLY | DONT_ENUM); 126 %AddNamedProperty(GlobalMath, toStringTagSymbol, "Math", READ_ONLY | DONT_ENUM);
160 127
161 // Set up math constants. 128 // Set up math constants.
162 utils.InstallConstants(GlobalMath, [ 129 utils.InstallConstants(GlobalMath, [
(...skipping 13 matching lines...) Expand all
176 143
177 // Set up non-enumerable functions of the Math object and 144 // Set up non-enumerable functions of the Math object and
178 // set their names. 145 // set their names.
179 utils.InstallFunctions(GlobalMath, DONT_ENUM, [ 146 utils.InstallFunctions(GlobalMath, DONT_ENUM, [
180 "random", MathRandom, 147 "random", MathRandom,
181 "abs", MathAbs, 148 "abs", MathAbs,
182 "pow", MathPowJS, 149 "pow", MathPowJS,
183 "sign", MathSign, 150 "sign", MathSign,
184 "asinh", MathAsinh, 151 "asinh", MathAsinh,
185 "acosh", MathAcosh, 152 "acosh", MathAcosh,
186 "atanh", MathAtanh,
187 "hypot", MathHypot, 153 "hypot", MathHypot,
188 "cbrt", MathCbrt
189 ]); 154 ]);
190 155
191 %SetForceInlineFlag(MathAbs); 156 %SetForceInlineFlag(MathAbs);
192 %SetForceInlineFlag(MathRandom); 157 %SetForceInlineFlag(MathRandom);
193 %SetForceInlineFlag(MathSign); 158 %SetForceInlineFlag(MathSign);
194 159
195 // ------------------------------------------------------------------- 160 // -------------------------------------------------------------------
196 // Exports 161 // Exports
197 162
198 utils.Export(function(to) { 163 utils.Export(function(to) {
199 to.MathAbs = MathAbs; 164 to.MathAbs = MathAbs;
200 to.IntRandom = MathRandomRaw; 165 to.IntRandom = MathRandomRaw;
201 }); 166 });
202 167
203 }) 168 })
OLDNEW
« no previous file with comments | « src/external-reference-table.cc ('k') | src/objects.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698