The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / deps / v8 / src / math.js @ f230a1cf
History | View | Annotate | Download (8.33 KB)
1 |
// Copyright 2012 the V8 project authors. All rights reserved.
|
---|---|
2 |
// Redistribution and use in source and binary forms, with or without
|
3 |
// modification, are permitted provided that the following conditions are
|
4 |
// met:
|
5 |
//
|
6 |
// * Redistributions of source code must retain the above copyright
|
7 |
// notice, this list of conditions and the following disclaimer.
|
8 |
// * Redistributions in binary form must reproduce the above
|
9 |
// copyright notice, this list of conditions and the following
|
10 |
// disclaimer in the documentation and/or other materials provided
|
11 |
// with the distribution.
|
12 |
// * Neither the name of Google Inc. nor the names of its
|
13 |
// contributors may be used to endorse or promote products derived
|
14 |
// from this software without specific prior written permission.
|
15 |
//
|
16 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
17 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
18 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
19 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
20 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
23 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
24 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
26 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27 |
|
28 |
// This file relies on the fact that the following declarations have been made
|
29 |
// in runtime.js:
|
30 |
// var $Object = global.Object;
|
31 |
|
32 |
// Keep reference to original values of some global properties. This
|
33 |
// has the added benefit that the code in this file is isolated from
|
34 |
// changes to these properties.
|
35 |
var $floor = MathFloor; |
36 |
var $abs = MathAbs; |
37 |
|
38 |
// Instance class name can only be set on functions. That is the only
|
39 |
// purpose for MathConstructor.
|
40 |
function MathConstructor() {} |
41 |
var $Math = new MathConstructor(); |
42 |
|
43 |
// -------------------------------------------------------------------
|
44 |
|
45 |
// ECMA 262 - 15.8.2.1
|
46 |
function MathAbs(x) { |
47 |
if (%_IsSmi(x)) return x >= 0 ? x : -x; |
48 |
x = TO_NUMBER_INLINE(x); |
49 |
if (x === 0) return 0; // To handle -0. |
50 |
return x > 0 ? x : -x; |
51 |
} |
52 |
|
53 |
// ECMA 262 - 15.8.2.2
|
54 |
function MathAcos(x) { |
55 |
return %Math_acos(TO_NUMBER_INLINE(x));
|
56 |
} |
57 |
|
58 |
// ECMA 262 - 15.8.2.3
|
59 |
function MathAsin(x) { |
60 |
return %Math_asin(TO_NUMBER_INLINE(x));
|
61 |
} |
62 |
|
63 |
// ECMA 262 - 15.8.2.4
|
64 |
function MathAtan(x) { |
65 |
return %Math_atan(TO_NUMBER_INLINE(x));
|
66 |
} |
67 |
|
68 |
// ECMA 262 - 15.8.2.5
|
69 |
// The naming of y and x matches the spec, as does the order in which
|
70 |
// ToNumber (valueOf) is called.
|
71 |
function MathAtan2(y, x) { |
72 |
return %Math_atan2(TO_NUMBER_INLINE(y), TO_NUMBER_INLINE(x));
|
73 |
} |
74 |
|
75 |
// ECMA 262 - 15.8.2.6
|
76 |
function MathCeil(x) { |
77 |
return %Math_ceil(TO_NUMBER_INLINE(x));
|
78 |
} |
79 |
|
80 |
// ECMA 262 - 15.8.2.7
|
81 |
function MathCos(x) { |
82 |
return %_MathCos(TO_NUMBER_INLINE(x));
|
83 |
} |
84 |
|
85 |
// ECMA 262 - 15.8.2.8
|
86 |
function MathExp(x) { |
87 |
return %Math_exp(TO_NUMBER_INLINE(x));
|
88 |
} |
89 |
|
90 |
// ECMA 262 - 15.8.2.9
|
91 |
function MathFloor(x) { |
92 |
x = TO_NUMBER_INLINE(x); |
93 |
// It's more common to call this with a positive number that's out
|
94 |
// of range than negative numbers; check the upper bound first.
|
95 |
if (x < 0x80000000 && x > 0) { |
96 |
// Numbers in the range [0, 2^31) can be floored by converting
|
97 |
// them to an unsigned 32-bit value using the shift operator.
|
98 |
// We avoid doing so for -0, because the result of Math.floor(-0)
|
99 |
// has to be -0, which wouldn't be the case with the shift.
|
100 |
return TO_UINT32(x);
|
101 |
} else {
|
102 |
return %Math_floor(x);
|
103 |
} |
104 |
} |
105 |
|
106 |
// ECMA 262 - 15.8.2.10
|
107 |
function MathLog(x) { |
108 |
return %_MathLog(TO_NUMBER_INLINE(x));
|
109 |
} |
110 |
|
111 |
// ECMA 262 - 15.8.2.11
|
112 |
function MathMax(arg1, arg2) { // length == 2 |
113 |
var length = %_ArgumentsLength();
|
114 |
if (length == 2) { |
115 |
arg1 = TO_NUMBER_INLINE(arg1); |
116 |
arg2 = TO_NUMBER_INLINE(arg2); |
117 |
if (arg2 > arg1) return arg2; |
118 |
if (arg1 > arg2) return arg1; |
119 |
if (arg1 == arg2) {
|
120 |
// Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be
|
121 |
// a Smi or a heap number.
|
122 |
return (arg1 == 0 && !%_IsSmi(arg1) && 1 / arg1 < 0) ? arg2 : arg1; |
123 |
} |
124 |
// All comparisons failed, one of the arguments must be NaN.
|
125 |
return NAN;
|
126 |
} |
127 |
var r = -INFINITY;
|
128 |
for (var i = 0; i < length; i++) { |
129 |
var n = %_Arguments(i);
|
130 |
if (!IS_NUMBER(n)) n = NonNumberToNumber(n);
|
131 |
// Make sure +0 is considered greater than -0. -0 is never a Smi, +0 can be
|
132 |
// a Smi or heap number.
|
133 |
if (NUMBER_IS_NAN(n) || n > r ||
|
134 |
(r == 0 && n == 0 && !%_IsSmi(r) && 1 / r < 0)) { |
135 |
r = n; |
136 |
} |
137 |
} |
138 |
return r;
|
139 |
} |
140 |
|
141 |
// ECMA 262 - 15.8.2.12
|
142 |
function MathMin(arg1, arg2) { // length == 2 |
143 |
var length = %_ArgumentsLength();
|
144 |
if (length == 2) { |
145 |
arg1 = TO_NUMBER_INLINE(arg1); |
146 |
arg2 = TO_NUMBER_INLINE(arg2); |
147 |
if (arg2 > arg1) return arg1; |
148 |
if (arg1 > arg2) return arg2; |
149 |
if (arg1 == arg2) {
|
150 |
// Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be
|
151 |
// a Smi or a heap number.
|
152 |
return (arg1 == 0 && !%_IsSmi(arg1) && 1 / arg1 < 0) ? arg1 : arg2; |
153 |
} |
154 |
// All comparisons failed, one of the arguments must be NaN.
|
155 |
return NAN;
|
156 |
} |
157 |
var r = INFINITY;
|
158 |
for (var i = 0; i < length; i++) { |
159 |
var n = %_Arguments(i);
|
160 |
if (!IS_NUMBER(n)) n = NonNumberToNumber(n);
|
161 |
// Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be a
|
162 |
// Smi or a heap number.
|
163 |
if (NUMBER_IS_NAN(n) || n < r ||
|
164 |
(r == 0 && n == 0 && !%_IsSmi(n) && 1 / n < 0)) { |
165 |
r = n; |
166 |
} |
167 |
} |
168 |
return r;
|
169 |
} |
170 |
|
171 |
// ECMA 262 - 15.8.2.13
|
172 |
function MathPow(x, y) { |
173 |
return %_MathPow(TO_NUMBER_INLINE(x), TO_NUMBER_INLINE(y));
|
174 |
} |
175 |
|
176 |
// ECMA 262 - 15.8.2.14
|
177 |
function MathRandom() { |
178 |
return %_RandomHeapNumber();
|
179 |
} |
180 |
|
181 |
// ECMA 262 - 15.8.2.15
|
182 |
function MathRound(x) { |
183 |
return %RoundNumber(TO_NUMBER_INLINE(x));
|
184 |
} |
185 |
|
186 |
// ECMA 262 - 15.8.2.16
|
187 |
function MathSin(x) { |
188 |
return %_MathSin(TO_NUMBER_INLINE(x));
|
189 |
} |
190 |
|
191 |
// ECMA 262 - 15.8.2.17
|
192 |
function MathSqrt(x) { |
193 |
return %_MathSqrt(TO_NUMBER_INLINE(x));
|
194 |
} |
195 |
|
196 |
// ECMA 262 - 15.8.2.18
|
197 |
function MathTan(x) { |
198 |
return %_MathTan(TO_NUMBER_INLINE(x));
|
199 |
} |
200 |
|
201 |
// Non-standard extension.
|
202 |
function MathImul(x, y) { |
203 |
return %NumberImul(TO_NUMBER_INLINE(x), TO_NUMBER_INLINE(y));
|
204 |
} |
205 |
|
206 |
|
207 |
// -------------------------------------------------------------------
|
208 |
|
209 |
function SetUpMath() { |
210 |
%CheckIsBootstrapping(); |
211 |
|
212 |
%SetPrototype($Math, $Object.prototype); |
213 |
%SetProperty(global, "Math", $Math, DONT_ENUM); |
214 |
%FunctionSetInstanceClassName(MathConstructor, 'Math');
|
215 |
|
216 |
// Set up math constants.
|
217 |
// ECMA-262, section 15.8.1.1.
|
218 |
%OptimizeObjectForAddingMultipleProperties($Math, 8); |
219 |
%SetProperty($Math,
|
220 |
"E",
|
221 |
2.7182818284590452354,
|
222 |
DONT_ENUM | DONT_DELETE | READ_ONLY); |
223 |
// ECMA-262, section 15.8.1.2.
|
224 |
%SetProperty($Math,
|
225 |
"LN10",
|
226 |
2.302585092994046,
|
227 |
DONT_ENUM | DONT_DELETE | READ_ONLY); |
228 |
// ECMA-262, section 15.8.1.3.
|
229 |
%SetProperty($Math,
|
230 |
"LN2",
|
231 |
0.6931471805599453,
|
232 |
DONT_ENUM | DONT_DELETE | READ_ONLY); |
233 |
// ECMA-262, section 15.8.1.4.
|
234 |
%SetProperty($Math,
|
235 |
"LOG2E",
|
236 |
1.4426950408889634,
|
237 |
DONT_ENUM | DONT_DELETE | READ_ONLY); |
238 |
%SetProperty($Math,
|
239 |
"LOG10E",
|
240 |
0.4342944819032518,
|
241 |
DONT_ENUM | DONT_DELETE | READ_ONLY); |
242 |
%SetProperty($Math,
|
243 |
"PI",
|
244 |
3.1415926535897932,
|
245 |
DONT_ENUM | DONT_DELETE | READ_ONLY); |
246 |
%SetProperty($Math,
|
247 |
"SQRT1_2",
|
248 |
0.7071067811865476,
|
249 |
DONT_ENUM | DONT_DELETE | READ_ONLY); |
250 |
%SetProperty($Math,
|
251 |
"SQRT2",
|
252 |
1.4142135623730951,
|
253 |
DONT_ENUM | DONT_DELETE | READ_ONLY); |
254 |
%ToFastProperties($Math);
|
255 |
|
256 |
// Set up non-enumerable functions of the Math object and
|
257 |
// set their names.
|
258 |
InstallFunctions($Math, DONT_ENUM, $Array( |
259 |
"random", MathRandom,
|
260 |
"abs", MathAbs,
|
261 |
"acos", MathAcos,
|
262 |
"asin", MathAsin,
|
263 |
"atan", MathAtan,
|
264 |
"ceil", MathCeil,
|
265 |
"cos", MathCos,
|
266 |
"exp", MathExp,
|
267 |
"floor", MathFloor,
|
268 |
"log", MathLog,
|
269 |
"round", MathRound,
|
270 |
"sin", MathSin,
|
271 |
"sqrt", MathSqrt,
|
272 |
"tan", MathTan,
|
273 |
"atan2", MathAtan2,
|
274 |
"pow", MathPow,
|
275 |
"max", MathMax,
|
276 |
"min", MathMin,
|
277 |
"imul", MathImul
|
278 |
)); |
279 |
} |
280 |
|
281 |
SetUpMath(); |