|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 /* |
|
8 * JS math package. |
|
9 */ |
|
10 |
|
11 #include "jsmath.h" |
|
12 |
|
13 #include "mozilla/Constants.h" |
|
14 #include "mozilla/FloatingPoint.h" |
|
15 #include "mozilla/MathAlgorithms.h" |
|
16 #include "mozilla/MemoryReporting.h" |
|
17 |
|
18 #include <algorithm> // for std::max |
|
19 #include <fcntl.h> |
|
20 |
|
21 #ifdef XP_UNIX |
|
22 # include <unistd.h> |
|
23 #endif |
|
24 |
|
25 #include "jsapi.h" |
|
26 #include "jsatom.h" |
|
27 #include "jscntxt.h" |
|
28 #include "jscompartment.h" |
|
29 #include "jslibmath.h" |
|
30 #include "jstypes.h" |
|
31 #include "prmjtime.h" |
|
32 |
|
33 #include "jsobjinlines.h" |
|
34 |
|
35 using namespace js; |
|
36 |
|
37 using mozilla::Abs; |
|
38 using mozilla::NumberEqualsInt32; |
|
39 using mozilla::NumberIsInt32; |
|
40 using mozilla::ExponentComponent; |
|
41 using mozilla::FloatingPoint; |
|
42 using mozilla::IsFinite; |
|
43 using mozilla::IsInfinite; |
|
44 using mozilla::IsNaN; |
|
45 using mozilla::IsNegative; |
|
46 using mozilla::IsNegativeZero; |
|
47 using mozilla::PositiveInfinity; |
|
48 using mozilla::NegativeInfinity; |
|
49 using JS::ToNumber; |
|
50 using JS::GenericNaN; |
|
51 |
|
52 static const JSConstDoubleSpec math_constants[] = { |
|
53 {M_E, "E", 0, {0,0,0}}, |
|
54 {M_LOG2E, "LOG2E", 0, {0,0,0}}, |
|
55 {M_LOG10E, "LOG10E", 0, {0,0,0}}, |
|
56 {M_LN2, "LN2", 0, {0,0,0}}, |
|
57 {M_LN10, "LN10", 0, {0,0,0}}, |
|
58 {M_PI, "PI", 0, {0,0,0}}, |
|
59 {M_SQRT2, "SQRT2", 0, {0,0,0}}, |
|
60 {M_SQRT1_2, "SQRT1_2", 0, {0,0,0}}, |
|
61 {0,0,0,{0,0,0}} |
|
62 }; |
|
63 |
|
64 MathCache::MathCache() { |
|
65 memset(table, 0, sizeof(table)); |
|
66 |
|
67 /* See comments in lookup(). */ |
|
68 JS_ASSERT(IsNegativeZero(-0.0)); |
|
69 JS_ASSERT(!IsNegativeZero(+0.0)); |
|
70 JS_ASSERT(hash(-0.0) != hash(+0.0)); |
|
71 } |
|
72 |
|
73 size_t |
|
74 MathCache::sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) |
|
75 { |
|
76 return mallocSizeOf(this); |
|
77 } |
|
78 |
|
79 const Class js::MathClass = { |
|
80 js_Math_str, |
|
81 JSCLASS_HAS_CACHED_PROTO(JSProto_Math), |
|
82 JS_PropertyStub, /* addProperty */ |
|
83 JS_DeletePropertyStub, /* delProperty */ |
|
84 JS_PropertyStub, /* getProperty */ |
|
85 JS_StrictPropertyStub, /* setProperty */ |
|
86 JS_EnumerateStub, |
|
87 JS_ResolveStub, |
|
88 JS_ConvertStub |
|
89 }; |
|
90 |
|
91 bool |
|
92 js_math_abs(JSContext *cx, unsigned argc, Value *vp) |
|
93 { |
|
94 CallArgs args = CallArgsFromVp(argc, vp); |
|
95 |
|
96 if (args.length() == 0) { |
|
97 args.rval().setNaN(); |
|
98 return true; |
|
99 } |
|
100 |
|
101 double x; |
|
102 if (!ToNumber(cx, args[0], &x)) |
|
103 return false; |
|
104 |
|
105 double z = Abs(x); |
|
106 args.rval().setNumber(z); |
|
107 return true; |
|
108 } |
|
109 |
|
110 #if defined(SOLARIS) && defined(__GNUC__) |
|
111 #define ACOS_IF_OUT_OF_RANGE(x) if (x < -1 || 1 < x) return GenericNaN(); |
|
112 #else |
|
113 #define ACOS_IF_OUT_OF_RANGE(x) |
|
114 #endif |
|
115 |
|
116 double |
|
117 js::math_acos_impl(MathCache *cache, double x) |
|
118 { |
|
119 ACOS_IF_OUT_OF_RANGE(x); |
|
120 return cache->lookup(acos, x); |
|
121 } |
|
122 |
|
123 double |
|
124 js::math_acos_uncached(double x) |
|
125 { |
|
126 ACOS_IF_OUT_OF_RANGE(x); |
|
127 return acos(x); |
|
128 } |
|
129 |
|
130 #undef ACOS_IF_OUT_OF_RANGE |
|
131 |
|
132 bool |
|
133 js::math_acos(JSContext *cx, unsigned argc, Value *vp) |
|
134 { |
|
135 CallArgs args = CallArgsFromVp(argc, vp); |
|
136 |
|
137 if (args.length() == 0) { |
|
138 args.rval().setNaN(); |
|
139 return true; |
|
140 } |
|
141 |
|
142 double x; |
|
143 if (!ToNumber(cx, args[0], &x)) |
|
144 return false; |
|
145 |
|
146 MathCache *mathCache = cx->runtime()->getMathCache(cx); |
|
147 if (!mathCache) |
|
148 return false; |
|
149 |
|
150 double z = math_acos_impl(mathCache, x); |
|
151 args.rval().setDouble(z); |
|
152 return true; |
|
153 } |
|
154 |
|
155 #if defined(SOLARIS) && defined(__GNUC__) |
|
156 #define ASIN_IF_OUT_OF_RANGE(x) if (x < -1 || 1 < x) return GenericNaN(); |
|
157 #else |
|
158 #define ASIN_IF_OUT_OF_RANGE(x) |
|
159 #endif |
|
160 |
|
161 double |
|
162 js::math_asin_impl(MathCache *cache, double x) |
|
163 { |
|
164 ASIN_IF_OUT_OF_RANGE(x); |
|
165 return cache->lookup(asin, x); |
|
166 } |
|
167 |
|
168 double |
|
169 js::math_asin_uncached(double x) |
|
170 { |
|
171 ASIN_IF_OUT_OF_RANGE(x); |
|
172 return asin(x); |
|
173 } |
|
174 |
|
175 #undef ASIN_IF_OUT_OF_RANGE |
|
176 |
|
177 bool |
|
178 js::math_asin(JSContext *cx, unsigned argc, Value *vp) |
|
179 { |
|
180 CallArgs args = CallArgsFromVp(argc, vp); |
|
181 |
|
182 if (args.length() == 0) { |
|
183 args.rval().setNaN(); |
|
184 return true; |
|
185 } |
|
186 |
|
187 double x; |
|
188 if (!ToNumber(cx, args[0], &x)) |
|
189 return false; |
|
190 |
|
191 MathCache *mathCache = cx->runtime()->getMathCache(cx); |
|
192 if (!mathCache) |
|
193 return false; |
|
194 |
|
195 double z = math_asin_impl(mathCache, x); |
|
196 args.rval().setDouble(z); |
|
197 return true; |
|
198 } |
|
199 |
|
200 double |
|
201 js::math_atan_impl(MathCache *cache, double x) |
|
202 { |
|
203 return cache->lookup(atan, x); |
|
204 } |
|
205 |
|
206 double |
|
207 js::math_atan_uncached(double x) |
|
208 { |
|
209 return atan(x); |
|
210 } |
|
211 |
|
212 bool |
|
213 js::math_atan(JSContext *cx, unsigned argc, Value *vp) |
|
214 { |
|
215 CallArgs args = CallArgsFromVp(argc, vp); |
|
216 |
|
217 if (args.length() == 0) { |
|
218 args.rval().setNaN(); |
|
219 return true; |
|
220 } |
|
221 |
|
222 double x; |
|
223 if (!ToNumber(cx, args[0], &x)) |
|
224 return false; |
|
225 |
|
226 MathCache *mathCache = cx->runtime()->getMathCache(cx); |
|
227 if (!mathCache) |
|
228 return false; |
|
229 |
|
230 double z = math_atan_impl(mathCache, x); |
|
231 args.rval().setDouble(z); |
|
232 return true; |
|
233 } |
|
234 |
|
235 double |
|
236 js::ecmaAtan2(double y, double x) |
|
237 { |
|
238 #if defined(_MSC_VER) |
|
239 /* |
|
240 * MSVC's atan2 does not yield the result demanded by ECMA when both x |
|
241 * and y are infinite. |
|
242 * - The result is a multiple of pi/4. |
|
243 * - The sign of y determines the sign of the result. |
|
244 * - The sign of x determines the multiplicator, 1 or 3. |
|
245 */ |
|
246 if (IsInfinite(y) && IsInfinite(x)) { |
|
247 double z = js_copysign(M_PI / 4, y); |
|
248 if (x < 0) |
|
249 z *= 3; |
|
250 return z; |
|
251 } |
|
252 #endif |
|
253 |
|
254 #if defined(SOLARIS) && defined(__GNUC__) |
|
255 if (y == 0) { |
|
256 if (IsNegativeZero(x)) |
|
257 return js_copysign(M_PI, y); |
|
258 if (x == 0) |
|
259 return y; |
|
260 } |
|
261 #endif |
|
262 return atan2(y, x); |
|
263 } |
|
264 |
|
265 bool |
|
266 js::math_atan2(JSContext *cx, unsigned argc, Value *vp) |
|
267 { |
|
268 CallArgs args = CallArgsFromVp(argc, vp); |
|
269 |
|
270 double y; |
|
271 if (!ToNumber(cx, args.get(0), &y)) |
|
272 return false; |
|
273 |
|
274 double x; |
|
275 if (!ToNumber(cx, args.get(1), &x)) |
|
276 return false; |
|
277 |
|
278 double z = ecmaAtan2(y, x); |
|
279 args.rval().setDouble(z); |
|
280 return true; |
|
281 } |
|
282 |
|
283 double |
|
284 js::math_ceil_impl(double x) |
|
285 { |
|
286 #ifdef __APPLE__ |
|
287 if (x < 0 && x > -1.0) |
|
288 return js_copysign(0, -1); |
|
289 #endif |
|
290 return ceil(x); |
|
291 } |
|
292 |
|
293 bool |
|
294 js::math_ceil(JSContext *cx, unsigned argc, Value *vp) |
|
295 { |
|
296 CallArgs args = CallArgsFromVp(argc, vp); |
|
297 |
|
298 if (args.length() == 0) { |
|
299 args.rval().setNaN(); |
|
300 return true; |
|
301 } |
|
302 |
|
303 double x; |
|
304 if (!ToNumber(cx, args[0], &x)) |
|
305 return false; |
|
306 |
|
307 double z = math_ceil_impl(x); |
|
308 args.rval().setNumber(z); |
|
309 return true; |
|
310 } |
|
311 |
|
312 bool |
|
313 js::math_clz32(JSContext *cx, unsigned argc, Value *vp) |
|
314 { |
|
315 CallArgs args = CallArgsFromVp(argc, vp); |
|
316 |
|
317 if (args.length() == 0) { |
|
318 args.rval().setInt32(32); |
|
319 return true; |
|
320 } |
|
321 |
|
322 uint32_t n; |
|
323 if (!ToUint32(cx, args[0], &n)) |
|
324 return false; |
|
325 |
|
326 if (n == 0) { |
|
327 args.rval().setInt32(32); |
|
328 return true; |
|
329 } |
|
330 |
|
331 args.rval().setInt32(mozilla::CountLeadingZeroes32(n)); |
|
332 return true; |
|
333 } |
|
334 |
|
335 double |
|
336 js::math_cos_impl(MathCache *cache, double x) |
|
337 { |
|
338 return cache->lookup(cos, x); |
|
339 } |
|
340 |
|
341 double |
|
342 js::math_cos_uncached(double x) |
|
343 { |
|
344 return cos(x); |
|
345 } |
|
346 |
|
347 bool |
|
348 js::math_cos(JSContext *cx, unsigned argc, Value *vp) |
|
349 { |
|
350 CallArgs args = CallArgsFromVp(argc, vp); |
|
351 |
|
352 if (args.length() == 0) { |
|
353 args.rval().setNaN(); |
|
354 return true; |
|
355 } |
|
356 |
|
357 double x; |
|
358 if (!ToNumber(cx, args[0], &x)) |
|
359 return false; |
|
360 |
|
361 MathCache *mathCache = cx->runtime()->getMathCache(cx); |
|
362 if (!mathCache) |
|
363 return false; |
|
364 |
|
365 double z = math_cos_impl(mathCache, x); |
|
366 args.rval().setDouble(z); |
|
367 return true; |
|
368 } |
|
369 |
|
370 #ifdef _WIN32 |
|
371 #define EXP_IF_OUT_OF_RANGE(x) \ |
|
372 if (!IsNaN(x)) { \ |
|
373 if (x == PositiveInfinity<double>()) \ |
|
374 return PositiveInfinity<double>(); \ |
|
375 if (x == NegativeInfinity<double>()) \ |
|
376 return 0.0; \ |
|
377 } |
|
378 #else |
|
379 #define EXP_IF_OUT_OF_RANGE(x) |
|
380 #endif |
|
381 |
|
382 double |
|
383 js::math_exp_impl(MathCache *cache, double x) |
|
384 { |
|
385 EXP_IF_OUT_OF_RANGE(x); |
|
386 return cache->lookup(exp, x); |
|
387 } |
|
388 |
|
389 double |
|
390 js::math_exp_uncached(double x) |
|
391 { |
|
392 EXP_IF_OUT_OF_RANGE(x); |
|
393 return exp(x); |
|
394 } |
|
395 |
|
396 #undef EXP_IF_OUT_OF_RANGE |
|
397 |
|
398 bool |
|
399 js::math_exp(JSContext *cx, unsigned argc, Value *vp) |
|
400 { |
|
401 CallArgs args = CallArgsFromVp(argc, vp); |
|
402 |
|
403 if (args.length() == 0) { |
|
404 args.rval().setNaN(); |
|
405 return true; |
|
406 } |
|
407 |
|
408 double x; |
|
409 if (!ToNumber(cx, args[0], &x)) |
|
410 return false; |
|
411 |
|
412 MathCache *mathCache = cx->runtime()->getMathCache(cx); |
|
413 if (!mathCache) |
|
414 return false; |
|
415 |
|
416 double z = math_exp_impl(mathCache, x); |
|
417 args.rval().setNumber(z); |
|
418 return true; |
|
419 } |
|
420 |
|
421 double |
|
422 js::math_floor_impl(double x) |
|
423 { |
|
424 return floor(x); |
|
425 } |
|
426 |
|
427 bool |
|
428 js::math_floor(JSContext *cx, unsigned argc, Value *vp) |
|
429 { |
|
430 CallArgs args = CallArgsFromVp(argc, vp); |
|
431 |
|
432 if (args.length() == 0) { |
|
433 args.rval().setNaN(); |
|
434 return true; |
|
435 } |
|
436 |
|
437 double x; |
|
438 if (!ToNumber(cx, args[0], &x)) |
|
439 return false; |
|
440 |
|
441 double z = math_floor_impl(x); |
|
442 args.rval().setNumber(z); |
|
443 return true; |
|
444 } |
|
445 |
|
446 bool |
|
447 js::math_imul(JSContext *cx, unsigned argc, Value *vp) |
|
448 { |
|
449 CallArgs args = CallArgsFromVp(argc, vp); |
|
450 |
|
451 uint32_t a = 0, b = 0; |
|
452 if (args.hasDefined(0) && !ToUint32(cx, args[0], &a)) |
|
453 return false; |
|
454 if (args.hasDefined(1) && !ToUint32(cx, args[1], &b)) |
|
455 return false; |
|
456 |
|
457 uint32_t product = a * b; |
|
458 args.rval().setInt32(product > INT32_MAX |
|
459 ? int32_t(INT32_MIN + (product - INT32_MAX - 1)) |
|
460 : int32_t(product)); |
|
461 return true; |
|
462 } |
|
463 |
|
464 // Implements Math.fround (20.2.2.16) up to step 3 |
|
465 bool |
|
466 js::RoundFloat32(JSContext *cx, Handle<Value> v, float *out) |
|
467 { |
|
468 double d; |
|
469 bool success = ToNumber(cx, v, &d); |
|
470 *out = static_cast<float>(d); |
|
471 return success; |
|
472 } |
|
473 |
|
474 bool |
|
475 js::math_fround(JSContext *cx, unsigned argc, Value *vp) |
|
476 { |
|
477 CallArgs args = CallArgsFromVp(argc, vp); |
|
478 |
|
479 if (args.length() == 0) { |
|
480 args.rval().setNaN(); |
|
481 return true; |
|
482 } |
|
483 |
|
484 float f; |
|
485 if (!RoundFloat32(cx, args[0], &f)) |
|
486 return false; |
|
487 |
|
488 args.rval().setDouble(static_cast<double>(f)); |
|
489 return true; |
|
490 } |
|
491 |
|
492 #if defined(SOLARIS) && defined(__GNUC__) |
|
493 #define LOG_IF_OUT_OF_RANGE(x) if (x < 0) return GenericNaN(); |
|
494 #else |
|
495 #define LOG_IF_OUT_OF_RANGE(x) |
|
496 #endif |
|
497 |
|
498 double |
|
499 js::math_log_impl(MathCache *cache, double x) |
|
500 { |
|
501 LOG_IF_OUT_OF_RANGE(x); |
|
502 return cache->lookup(log, x); |
|
503 } |
|
504 |
|
505 double |
|
506 js::math_log_uncached(double x) |
|
507 { |
|
508 LOG_IF_OUT_OF_RANGE(x); |
|
509 return log(x); |
|
510 } |
|
511 |
|
512 #undef LOG_IF_OUT_OF_RANGE |
|
513 |
|
514 bool |
|
515 js::math_log(JSContext *cx, unsigned argc, Value *vp) |
|
516 { |
|
517 CallArgs args = CallArgsFromVp(argc, vp); |
|
518 |
|
519 if (args.length() == 0) { |
|
520 args.rval().setNaN(); |
|
521 return true; |
|
522 } |
|
523 |
|
524 double x; |
|
525 if (!ToNumber(cx, args[0], &x)) |
|
526 return false; |
|
527 |
|
528 MathCache *mathCache = cx->runtime()->getMathCache(cx); |
|
529 if (!mathCache) |
|
530 return false; |
|
531 |
|
532 double z = math_log_impl(mathCache, x); |
|
533 args.rval().setNumber(z); |
|
534 return true; |
|
535 } |
|
536 |
|
537 bool |
|
538 js_math_max(JSContext *cx, unsigned argc, Value *vp) |
|
539 { |
|
540 CallArgs args = CallArgsFromVp(argc, vp); |
|
541 |
|
542 double maxval = NegativeInfinity<double>(); |
|
543 for (unsigned i = 0; i < args.length(); i++) { |
|
544 double x; |
|
545 if (!ToNumber(cx, args[i], &x)) |
|
546 return false; |
|
547 // Math.max(num, NaN) => NaN, Math.max(-0, +0) => +0 |
|
548 if (x > maxval || IsNaN(x) || (x == maxval && IsNegative(maxval))) |
|
549 maxval = x; |
|
550 } |
|
551 args.rval().setNumber(maxval); |
|
552 return true; |
|
553 } |
|
554 |
|
555 bool |
|
556 js_math_min(JSContext *cx, unsigned argc, Value *vp) |
|
557 { |
|
558 CallArgs args = CallArgsFromVp(argc, vp); |
|
559 |
|
560 double minval = PositiveInfinity<double>(); |
|
561 for (unsigned i = 0; i < args.length(); i++) { |
|
562 double x; |
|
563 if (!ToNumber(cx, args[i], &x)) |
|
564 return false; |
|
565 // Math.min(num, NaN) => NaN, Math.min(-0, +0) => -0 |
|
566 if (x < minval || IsNaN(x) || (x == minval && IsNegativeZero(x))) |
|
567 minval = x; |
|
568 } |
|
569 args.rval().setNumber(minval); |
|
570 return true; |
|
571 } |
|
572 |
|
573 // Disable PGO for Math.pow() and related functions (see bug 791214). |
|
574 #if defined(_MSC_VER) |
|
575 # pragma optimize("g", off) |
|
576 #endif |
|
577 double |
|
578 js::powi(double x, int y) |
|
579 { |
|
580 unsigned n = (y < 0) ? -y : y; |
|
581 double m = x; |
|
582 double p = 1; |
|
583 while (true) { |
|
584 if ((n & 1) != 0) p *= m; |
|
585 n >>= 1; |
|
586 if (n == 0) { |
|
587 if (y < 0) { |
|
588 // Unfortunately, we have to be careful when p has reached |
|
589 // infinity in the computation, because sometimes the higher |
|
590 // internal precision in the pow() implementation would have |
|
591 // given us a finite p. This happens very rarely. |
|
592 |
|
593 double result = 1.0 / p; |
|
594 return (result == 0 && IsInfinite(p)) |
|
595 ? pow(x, static_cast<double>(y)) // Avoid pow(double, int). |
|
596 : result; |
|
597 } |
|
598 |
|
599 return p; |
|
600 } |
|
601 m *= m; |
|
602 } |
|
603 } |
|
604 #if defined(_MSC_VER) |
|
605 # pragma optimize("", on) |
|
606 #endif |
|
607 |
|
608 // Disable PGO for Math.pow() and related functions (see bug 791214). |
|
609 #if defined(_MSC_VER) |
|
610 # pragma optimize("g", off) |
|
611 #endif |
|
612 double |
|
613 js::ecmaPow(double x, double y) |
|
614 { |
|
615 /* |
|
616 * Use powi if the exponent is an integer-valued double. We don't have to |
|
617 * check for NaN since a comparison with NaN is always false. |
|
618 */ |
|
619 int32_t yi; |
|
620 if (NumberEqualsInt32(y, &yi)) |
|
621 return powi(x, yi); |
|
622 |
|
623 /* |
|
624 * Because C99 and ECMA specify different behavior for pow(), |
|
625 * we need to wrap the libm call to make it ECMA compliant. |
|
626 */ |
|
627 if (!IsFinite(y) && (x == 1.0 || x == -1.0)) |
|
628 return GenericNaN(); |
|
629 |
|
630 /* pow(x, +-0) is always 1, even for x = NaN (MSVC gets this wrong). */ |
|
631 if (y == 0) |
|
632 return 1; |
|
633 |
|
634 /* |
|
635 * Special case for square roots. Note that pow(x, 0.5) != sqrt(x) |
|
636 * when x = -0.0, so we have to guard for this. |
|
637 */ |
|
638 if (IsFinite(x) && x != 0.0) { |
|
639 if (y == 0.5) |
|
640 return sqrt(x); |
|
641 if (y == -0.5) |
|
642 return 1.0 / sqrt(x); |
|
643 } |
|
644 return pow(x, y); |
|
645 } |
|
646 #if defined(_MSC_VER) |
|
647 # pragma optimize("", on) |
|
648 #endif |
|
649 |
|
650 // Disable PGO for Math.pow() and related functions (see bug 791214). |
|
651 #if defined(_MSC_VER) |
|
652 # pragma optimize("g", off) |
|
653 #endif |
|
654 bool |
|
655 js_math_pow(JSContext *cx, unsigned argc, Value *vp) |
|
656 { |
|
657 CallArgs args = CallArgsFromVp(argc, vp); |
|
658 |
|
659 double x; |
|
660 if (!ToNumber(cx, args.get(0), &x)) |
|
661 return false; |
|
662 |
|
663 double y; |
|
664 if (!ToNumber(cx, args.get(1), &y)) |
|
665 return false; |
|
666 |
|
667 double z = ecmaPow(x, y); |
|
668 args.rval().setNumber(z); |
|
669 return true; |
|
670 } |
|
671 #if defined(_MSC_VER) |
|
672 # pragma optimize("", on) |
|
673 #endif |
|
674 |
|
675 static uint64_t |
|
676 random_generateSeed() |
|
677 { |
|
678 union { |
|
679 uint8_t u8[8]; |
|
680 uint32_t u32[2]; |
|
681 uint64_t u64; |
|
682 } seed; |
|
683 seed.u64 = 0; |
|
684 |
|
685 #if defined(XP_WIN) |
|
686 /* |
|
687 * Our PRNG only uses 48 bits, so calling rand_s() twice to get 64 bits is |
|
688 * probably overkill. |
|
689 */ |
|
690 rand_s(&seed.u32[0]); |
|
691 #elif defined(XP_UNIX) |
|
692 /* |
|
693 * In the unlikely event we can't read /dev/urandom, there's not much we can |
|
694 * do, so just mix in the fd error code and the current time. |
|
695 */ |
|
696 int fd = open("/dev/urandom", O_RDONLY); |
|
697 MOZ_ASSERT(fd >= 0, "Can't open /dev/urandom"); |
|
698 if (fd >= 0) { |
|
699 read(fd, seed.u8, mozilla::ArrayLength(seed.u8)); |
|
700 close(fd); |
|
701 } |
|
702 seed.u32[0] ^= fd; |
|
703 #else |
|
704 # error "Platform needs to implement random_generateSeed()" |
|
705 #endif |
|
706 |
|
707 seed.u32[1] ^= PRMJ_Now(); |
|
708 return seed.u64; |
|
709 } |
|
710 |
|
711 static const uint64_t RNG_MULTIPLIER = 0x5DEECE66DLL; |
|
712 static const uint64_t RNG_ADDEND = 0xBLL; |
|
713 static const uint64_t RNG_MASK = (1LL << 48) - 1; |
|
714 static const double RNG_DSCALE = double(1LL << 53); |
|
715 |
|
716 /* |
|
717 * Math.random() support, lifted from java.util.Random.java. |
|
718 */ |
|
719 static void |
|
720 random_initState(uint64_t *rngState) |
|
721 { |
|
722 /* Our PRNG only uses 48 bits, so squeeze our entropy into those bits. */ |
|
723 uint64_t seed = random_generateSeed(); |
|
724 seed ^= (seed >> 16); |
|
725 *rngState = (seed ^ RNG_MULTIPLIER) & RNG_MASK; |
|
726 } |
|
727 |
|
728 uint64_t |
|
729 random_next(uint64_t *rngState, int bits) |
|
730 { |
|
731 MOZ_ASSERT((*rngState & 0xffff000000000000ULL) == 0, "Bad rngState"); |
|
732 MOZ_ASSERT(bits > 0 && bits <= 48, "bits is out of range"); |
|
733 |
|
734 if (*rngState == 0) { |
|
735 random_initState(rngState); |
|
736 } |
|
737 |
|
738 uint64_t nextstate = *rngState * RNG_MULTIPLIER; |
|
739 nextstate += RNG_ADDEND; |
|
740 nextstate &= RNG_MASK; |
|
741 *rngState = nextstate; |
|
742 return nextstate >> (48 - bits); |
|
743 } |
|
744 |
|
745 static inline double |
|
746 random_nextDouble(JSContext *cx) |
|
747 { |
|
748 uint64_t *rng = &cx->compartment()->rngState; |
|
749 return double((random_next(rng, 26) << 27) + random_next(rng, 27)) / RNG_DSCALE; |
|
750 } |
|
751 |
|
752 double |
|
753 math_random_no_outparam(JSContext *cx) |
|
754 { |
|
755 /* Calculate random without memory traffic, for use in the JITs. */ |
|
756 return random_nextDouble(cx); |
|
757 } |
|
758 |
|
759 bool |
|
760 js_math_random(JSContext *cx, unsigned argc, Value *vp) |
|
761 { |
|
762 CallArgs args = CallArgsFromVp(argc, vp); |
|
763 double z = random_nextDouble(cx); |
|
764 args.rval().setDouble(z); |
|
765 return true; |
|
766 } |
|
767 |
|
768 double |
|
769 js::math_round_impl(double x) |
|
770 { |
|
771 int32_t ignored; |
|
772 if (NumberIsInt32(x, &ignored)) |
|
773 return x; |
|
774 |
|
775 /* Some numbers are so big that adding 0.5 would give the wrong number. */ |
|
776 if (ExponentComponent(x) >= int_fast16_t(FloatingPoint<double>::ExponentShift)) |
|
777 return x; |
|
778 |
|
779 return js_copysign(floor(x + 0.5), x); |
|
780 } |
|
781 |
|
782 float |
|
783 js::math_roundf_impl(float x) |
|
784 { |
|
785 int32_t ignored; |
|
786 if (NumberIsInt32(x, &ignored)) |
|
787 return x; |
|
788 |
|
789 /* Some numbers are so big that adding 0.5 would give the wrong number. */ |
|
790 if (ExponentComponent(x) >= int_fast16_t(FloatingPoint<float>::ExponentShift)) |
|
791 return x; |
|
792 |
|
793 return js_copysign(floorf(x + 0.5f), x); |
|
794 } |
|
795 |
|
796 bool /* ES5 15.8.2.15. */ |
|
797 js::math_round(JSContext *cx, unsigned argc, Value *vp) |
|
798 { |
|
799 CallArgs args = CallArgsFromVp(argc, vp); |
|
800 |
|
801 if (args.length() == 0) { |
|
802 args.rval().setNaN(); |
|
803 return true; |
|
804 } |
|
805 |
|
806 double x; |
|
807 if (!ToNumber(cx, args[0], &x)) |
|
808 return false; |
|
809 |
|
810 double z = math_round_impl(x); |
|
811 args.rval().setNumber(z); |
|
812 return true; |
|
813 } |
|
814 |
|
815 double |
|
816 js::math_sin_impl(MathCache *cache, double x) |
|
817 { |
|
818 return cache->lookup(sin, x); |
|
819 } |
|
820 |
|
821 double |
|
822 js::math_sin_uncached(double x) |
|
823 { |
|
824 return sin(x); |
|
825 } |
|
826 |
|
827 bool |
|
828 js::math_sin(JSContext *cx, unsigned argc, Value *vp) |
|
829 { |
|
830 CallArgs args = CallArgsFromVp(argc, vp); |
|
831 |
|
832 if (args.length() == 0) { |
|
833 args.rval().setNaN(); |
|
834 return true; |
|
835 } |
|
836 |
|
837 double x; |
|
838 if (!ToNumber(cx, args[0], &x)) |
|
839 return false; |
|
840 |
|
841 MathCache *mathCache = cx->runtime()->getMathCache(cx); |
|
842 if (!mathCache) |
|
843 return false; |
|
844 |
|
845 double z = math_sin_impl(mathCache, x); |
|
846 args.rval().setDouble(z); |
|
847 return true; |
|
848 } |
|
849 |
|
850 bool |
|
851 js_math_sqrt(JSContext *cx, unsigned argc, Value *vp) |
|
852 { |
|
853 CallArgs args = CallArgsFromVp(argc, vp); |
|
854 |
|
855 if (args.length() == 0) { |
|
856 args.rval().setNaN(); |
|
857 return true; |
|
858 } |
|
859 |
|
860 double x; |
|
861 if (!ToNumber(cx, args[0], &x)) |
|
862 return false; |
|
863 |
|
864 MathCache *mathCache = cx->runtime()->getMathCache(cx); |
|
865 if (!mathCache) |
|
866 return false; |
|
867 |
|
868 double z = mathCache->lookup(sqrt, x); |
|
869 args.rval().setDouble(z); |
|
870 return true; |
|
871 } |
|
872 |
|
873 double |
|
874 js::math_tan_impl(MathCache *cache, double x) |
|
875 { |
|
876 return cache->lookup(tan, x); |
|
877 } |
|
878 |
|
879 double |
|
880 js::math_tan_uncached(double x) |
|
881 { |
|
882 return tan(x); |
|
883 } |
|
884 |
|
885 bool |
|
886 js::math_tan(JSContext *cx, unsigned argc, Value *vp) |
|
887 { |
|
888 CallArgs args = CallArgsFromVp(argc, vp); |
|
889 |
|
890 if (args.length() == 0) { |
|
891 args.rval().setNaN(); |
|
892 return true; |
|
893 } |
|
894 |
|
895 double x; |
|
896 if (!ToNumber(cx, args[0], &x)) |
|
897 return false; |
|
898 |
|
899 MathCache *mathCache = cx->runtime()->getMathCache(cx); |
|
900 if (!mathCache) |
|
901 return false; |
|
902 |
|
903 double z = math_tan_impl(mathCache, x); |
|
904 args.rval().setDouble(z); |
|
905 return true; |
|
906 } |
|
907 |
|
908 |
|
909 typedef double (*UnaryMathFunctionType)(MathCache *cache, double); |
|
910 |
|
911 template <UnaryMathFunctionType F> |
|
912 static bool math_function(JSContext *cx, unsigned argc, Value *vp) |
|
913 { |
|
914 CallArgs args = CallArgsFromVp(argc, vp); |
|
915 if (args.length() == 0) { |
|
916 args.rval().setNumber(GenericNaN()); |
|
917 return true; |
|
918 } |
|
919 |
|
920 double x; |
|
921 if (!ToNumber(cx, args[0], &x)) |
|
922 return false; |
|
923 |
|
924 MathCache *mathCache = cx->runtime()->getMathCache(cx); |
|
925 if (!mathCache) |
|
926 return false; |
|
927 double z = F(mathCache, x); |
|
928 args.rval().setNumber(z); |
|
929 |
|
930 return true; |
|
931 } |
|
932 |
|
933 |
|
934 |
|
935 double |
|
936 js::math_log10_impl(MathCache *cache, double x) |
|
937 { |
|
938 return cache->lookup(log10, x); |
|
939 } |
|
940 |
|
941 double |
|
942 js::math_log10_uncached(double x) |
|
943 { |
|
944 return log10(x); |
|
945 } |
|
946 |
|
947 bool |
|
948 js::math_log10(JSContext *cx, unsigned argc, Value *vp) |
|
949 { |
|
950 return math_function<math_log10_impl>(cx, argc, vp); |
|
951 } |
|
952 |
|
953 #if !HAVE_LOG2 |
|
954 double log2(double x) |
|
955 { |
|
956 return log(x) / M_LN2; |
|
957 } |
|
958 #endif |
|
959 |
|
960 double |
|
961 js::math_log2_impl(MathCache *cache, double x) |
|
962 { |
|
963 return cache->lookup(log2, x); |
|
964 } |
|
965 |
|
966 double |
|
967 js::math_log2_uncached(double x) |
|
968 { |
|
969 return log2(x); |
|
970 } |
|
971 |
|
972 bool |
|
973 js::math_log2(JSContext *cx, unsigned argc, Value *vp) |
|
974 { |
|
975 return math_function<math_log2_impl>(cx, argc, vp); |
|
976 } |
|
977 |
|
978 #if !HAVE_LOG1P |
|
979 double log1p(double x) |
|
980 { |
|
981 if (fabs(x) < 1e-4) { |
|
982 /* |
|
983 * Use Taylor approx. log(1 + x) = x - x^2 / 2 + x^3 / 3 - x^4 / 4 with error x^5 / 5 |
|
984 * Since |x| < 10^-4, |x|^5 < 10^-20, relative error less than 10^-16 |
|
985 */ |
|
986 double z = -(x * x * x * x) / 4 + (x * x * x) / 3 - (x * x) / 2 + x; |
|
987 return z; |
|
988 } else { |
|
989 /* For other large enough values of x use direct computation */ |
|
990 return log(1.0 + x); |
|
991 } |
|
992 } |
|
993 #endif |
|
994 |
|
995 #ifdef __APPLE__ |
|
996 // Ensure that log1p(-0) is -0. |
|
997 #define LOG1P_IF_OUT_OF_RANGE(x) if (x == 0) return x; |
|
998 #else |
|
999 #define LOG1P_IF_OUT_OF_RANGE(x) |
|
1000 #endif |
|
1001 |
|
1002 double |
|
1003 js::math_log1p_impl(MathCache *cache, double x) |
|
1004 { |
|
1005 LOG1P_IF_OUT_OF_RANGE(x); |
|
1006 return cache->lookup(log1p, x); |
|
1007 } |
|
1008 |
|
1009 double |
|
1010 js::math_log1p_uncached(double x) |
|
1011 { |
|
1012 LOG1P_IF_OUT_OF_RANGE(x); |
|
1013 return log1p(x); |
|
1014 } |
|
1015 |
|
1016 #undef LOG1P_IF_OUT_OF_RANGE |
|
1017 |
|
1018 bool |
|
1019 js::math_log1p(JSContext *cx, unsigned argc, Value *vp) |
|
1020 { |
|
1021 return math_function<math_log1p_impl>(cx, argc, vp); |
|
1022 } |
|
1023 |
|
1024 #if !HAVE_EXPM1 |
|
1025 double expm1(double x) |
|
1026 { |
|
1027 /* Special handling for -0 */ |
|
1028 if (x == 0.0) |
|
1029 return x; |
|
1030 |
|
1031 if (fabs(x) < 1e-5) { |
|
1032 /* |
|
1033 * Use Taylor approx. exp(x) - 1 = x + x^2 / 2 + x^3 / 6 with error x^4 / 24 |
|
1034 * Since |x| < 10^-5, |x|^4 < 10^-20, relative error less than 10^-15 |
|
1035 */ |
|
1036 double z = (x * x * x) / 6 + (x * x) / 2 + x; |
|
1037 return z; |
|
1038 } else { |
|
1039 /* For other large enough values of x use direct computation */ |
|
1040 return exp(x) - 1.0; |
|
1041 } |
|
1042 } |
|
1043 #endif |
|
1044 |
|
1045 double |
|
1046 js::math_expm1_impl(MathCache *cache, double x) |
|
1047 { |
|
1048 return cache->lookup(expm1, x); |
|
1049 } |
|
1050 |
|
1051 double |
|
1052 js::math_expm1_uncached(double x) |
|
1053 { |
|
1054 return expm1(x); |
|
1055 } |
|
1056 |
|
1057 bool |
|
1058 js::math_expm1(JSContext *cx, unsigned argc, Value *vp) |
|
1059 { |
|
1060 return math_function<math_expm1_impl>(cx, argc, vp); |
|
1061 } |
|
1062 |
|
1063 #if !HAVE_SQRT1PM1 |
|
1064 /* This algorithm computes sqrt(1+x)-1 for small x */ |
|
1065 double sqrt1pm1(double x) |
|
1066 { |
|
1067 if (fabs(x) > 0.75) |
|
1068 return sqrt(1 + x) - 1; |
|
1069 |
|
1070 return expm1(log1p(x) / 2); |
|
1071 } |
|
1072 #endif |
|
1073 |
|
1074 |
|
1075 double |
|
1076 js::math_cosh_impl(MathCache *cache, double x) |
|
1077 { |
|
1078 return cache->lookup(cosh, x); |
|
1079 } |
|
1080 |
|
1081 double |
|
1082 js::math_cosh_uncached(double x) |
|
1083 { |
|
1084 return cosh(x); |
|
1085 } |
|
1086 |
|
1087 bool |
|
1088 js::math_cosh(JSContext *cx, unsigned argc, Value *vp) |
|
1089 { |
|
1090 return math_function<math_cosh_impl>(cx, argc, vp); |
|
1091 } |
|
1092 |
|
1093 double |
|
1094 js::math_sinh_impl(MathCache *cache, double x) |
|
1095 { |
|
1096 return cache->lookup(sinh, x); |
|
1097 } |
|
1098 |
|
1099 double |
|
1100 js::math_sinh_uncached(double x) |
|
1101 { |
|
1102 return sinh(x); |
|
1103 } |
|
1104 |
|
1105 bool |
|
1106 js::math_sinh(JSContext *cx, unsigned argc, Value *vp) |
|
1107 { |
|
1108 return math_function<math_sinh_impl>(cx, argc, vp); |
|
1109 } |
|
1110 |
|
1111 double |
|
1112 js::math_tanh_impl(MathCache *cache, double x) |
|
1113 { |
|
1114 return cache->lookup(tanh, x); |
|
1115 } |
|
1116 |
|
1117 double |
|
1118 js::math_tanh_uncached(double x) |
|
1119 { |
|
1120 return tanh(x); |
|
1121 } |
|
1122 |
|
1123 bool |
|
1124 js::math_tanh(JSContext *cx, unsigned argc, Value *vp) |
|
1125 { |
|
1126 return math_function<math_tanh_impl>(cx, argc, vp); |
|
1127 } |
|
1128 |
|
1129 #if !HAVE_ACOSH |
|
1130 double acosh(double x) |
|
1131 { |
|
1132 const double SQUARE_ROOT_EPSILON = sqrt(std::numeric_limits<double>::epsilon()); |
|
1133 |
|
1134 if ((x - 1) >= SQUARE_ROOT_EPSILON) { |
|
1135 if (x > 1 / SQUARE_ROOT_EPSILON) { |
|
1136 /* |
|
1137 * http://functions.wolfram.com/ElementaryFunctions/ArcCosh/06/01/06/01/0001/ |
|
1138 * approximation by laurent series in 1/x at 0+ order from -1 to 0 |
|
1139 */ |
|
1140 return log(x) + M_LN2; |
|
1141 } else if (x < 1.5) { |
|
1142 // This is just a rearrangement of the standard form below |
|
1143 // devised to minimize loss of precision when x ~ 1: |
|
1144 double y = x - 1; |
|
1145 return log1p(y + sqrt(y * y + 2 * y)); |
|
1146 } else { |
|
1147 // http://functions.wolfram.com/ElementaryFunctions/ArcCosh/02/ |
|
1148 return log(x + sqrt(x * x - 1)); |
|
1149 } |
|
1150 } else { |
|
1151 // see http://functions.wolfram.com/ElementaryFunctions/ArcCosh/06/01/04/01/0001/ |
|
1152 double y = x - 1; |
|
1153 // approximation by taylor series in y at 0 up to order 2. |
|
1154 // If x is less than 1, sqrt(2 * y) is NaN and the result is NaN. |
|
1155 return sqrt(2 * y) * (1 - y / 12 + 3 * y * y / 160); |
|
1156 } |
|
1157 } |
|
1158 #endif |
|
1159 |
|
1160 double |
|
1161 js::math_acosh_impl(MathCache *cache, double x) |
|
1162 { |
|
1163 return cache->lookup(acosh, x); |
|
1164 } |
|
1165 |
|
1166 double |
|
1167 js::math_acosh_uncached(double x) |
|
1168 { |
|
1169 return acosh(x); |
|
1170 } |
|
1171 |
|
1172 bool |
|
1173 js::math_acosh(JSContext *cx, unsigned argc, Value *vp) |
|
1174 { |
|
1175 return math_function<math_acosh_impl>(cx, argc, vp); |
|
1176 } |
|
1177 |
|
1178 #if !HAVE_ASINH |
|
1179 // Bug 899712 - gcc incorrectly rewrites -asinh(-x) to asinh(x) when overriding |
|
1180 // asinh. |
|
1181 static double my_asinh(double x) |
|
1182 { |
|
1183 const double SQUARE_ROOT_EPSILON = sqrt(std::numeric_limits<double>::epsilon()); |
|
1184 const double FOURTH_ROOT_EPSILON = sqrt(SQUARE_ROOT_EPSILON); |
|
1185 |
|
1186 if (x >= FOURTH_ROOT_EPSILON) { |
|
1187 if (x > 1 / SQUARE_ROOT_EPSILON) |
|
1188 // http://functions.wolfram.com/ElementaryFunctions/ArcSinh/06/01/06/01/0001/ |
|
1189 // approximation by laurent series in 1/x at 0+ order from -1 to 1 |
|
1190 return M_LN2 + log(x) + 1 / (4 * x * x); |
|
1191 else if (x < 0.5) |
|
1192 return log1p(x + sqrt1pm1(x * x)); |
|
1193 else |
|
1194 return log(x + sqrt(x * x + 1)); |
|
1195 } else if (x <= -FOURTH_ROOT_EPSILON) { |
|
1196 return -my_asinh(-x); |
|
1197 } else { |
|
1198 // http://functions.wolfram.com/ElementaryFunctions/ArcSinh/06/01/03/01/0001/ |
|
1199 // approximation by taylor series in x at 0 up to order 2 |
|
1200 double result = x; |
|
1201 |
|
1202 if (fabs(x) >= SQUARE_ROOT_EPSILON) { |
|
1203 double x3 = x * x * x; |
|
1204 // approximation by taylor series in x at 0 up to order 4 |
|
1205 result -= x3 / 6; |
|
1206 } |
|
1207 |
|
1208 return result; |
|
1209 } |
|
1210 } |
|
1211 #endif |
|
1212 |
|
1213 double |
|
1214 js::math_asinh_impl(MathCache *cache, double x) |
|
1215 { |
|
1216 #ifdef HAVE_ASINH |
|
1217 return cache->lookup(asinh, x); |
|
1218 #else |
|
1219 return cache->lookup(my_asinh, x); |
|
1220 #endif |
|
1221 } |
|
1222 |
|
1223 double |
|
1224 js::math_asinh_uncached(double x) |
|
1225 { |
|
1226 #ifdef HAVE_ASINH |
|
1227 return asinh(x); |
|
1228 #else |
|
1229 return my_asinh(x); |
|
1230 #endif |
|
1231 } |
|
1232 |
|
1233 bool |
|
1234 js::math_asinh(JSContext *cx, unsigned argc, Value *vp) |
|
1235 { |
|
1236 return math_function<math_asinh_impl>(cx, argc, vp); |
|
1237 } |
|
1238 |
|
1239 #if !HAVE_ATANH |
|
1240 double atanh(double x) |
|
1241 { |
|
1242 const double EPSILON = std::numeric_limits<double>::epsilon(); |
|
1243 const double SQUARE_ROOT_EPSILON = sqrt(EPSILON); |
|
1244 const double FOURTH_ROOT_EPSILON = sqrt(SQUARE_ROOT_EPSILON); |
|
1245 |
|
1246 if (fabs(x) >= FOURTH_ROOT_EPSILON) { |
|
1247 // http://functions.wolfram.com/ElementaryFunctions/ArcTanh/02/ |
|
1248 if (fabs(x) < 0.5) |
|
1249 return (log1p(x) - log1p(-x)) / 2; |
|
1250 |
|
1251 return log((1 + x) / (1 - x)) / 2; |
|
1252 } else { |
|
1253 // http://functions.wolfram.com/ElementaryFunctions/ArcTanh/06/01/03/01/ |
|
1254 // approximation by taylor series in x at 0 up to order 2 |
|
1255 double result = x; |
|
1256 |
|
1257 if (fabs(x) >= SQUARE_ROOT_EPSILON) { |
|
1258 double x3 = x * x * x; |
|
1259 result += x3 / 3; |
|
1260 } |
|
1261 |
|
1262 return result; |
|
1263 } |
|
1264 } |
|
1265 #endif |
|
1266 |
|
1267 double |
|
1268 js::math_atanh_impl(MathCache *cache, double x) |
|
1269 { |
|
1270 return cache->lookup(atanh, x); |
|
1271 } |
|
1272 |
|
1273 double |
|
1274 js::math_atanh_uncached(double x) |
|
1275 { |
|
1276 return atanh(x); |
|
1277 } |
|
1278 |
|
1279 bool |
|
1280 js::math_atanh(JSContext *cx, unsigned argc, Value *vp) |
|
1281 { |
|
1282 return math_function<math_atanh_impl>(cx, argc, vp); |
|
1283 } |
|
1284 |
|
1285 /* Consistency wrapper for platform deviations in hypot() */ |
|
1286 double |
|
1287 js::ecmaHypot(double x, double y) |
|
1288 { |
|
1289 #ifdef XP_WIN |
|
1290 /* |
|
1291 * Workaround MS hypot bug, where hypot(Infinity, NaN or Math.MIN_VALUE) |
|
1292 * is NaN, not Infinity. |
|
1293 */ |
|
1294 if (mozilla::IsInfinite(x) || mozilla::IsInfinite(y)) { |
|
1295 return mozilla::PositiveInfinity<double>(); |
|
1296 } |
|
1297 #endif |
|
1298 return hypot(x, y); |
|
1299 } |
|
1300 |
|
1301 bool |
|
1302 js::math_hypot(JSContext *cx, unsigned argc, Value *vp) |
|
1303 { |
|
1304 CallArgs args = CallArgsFromVp(argc, vp); |
|
1305 |
|
1306 // IonMonkey calls the system hypot function directly if two arguments are |
|
1307 // given. Do that here as well to get the same results. |
|
1308 if (args.length() == 2) { |
|
1309 double x, y; |
|
1310 if (!ToNumber(cx, args[0], &x)) |
|
1311 return false; |
|
1312 if (!ToNumber(cx, args[1], &y)) |
|
1313 return false; |
|
1314 |
|
1315 double result = ecmaHypot(x, y); |
|
1316 args.rval().setNumber(result); |
|
1317 return true; |
|
1318 } |
|
1319 |
|
1320 bool isInfinite = false; |
|
1321 bool isNaN = false; |
|
1322 |
|
1323 double scale = 0; |
|
1324 double sumsq = 1; |
|
1325 |
|
1326 for (unsigned i = 0; i < args.length(); i++) { |
|
1327 double x; |
|
1328 if (!ToNumber(cx, args[i], &x)) |
|
1329 return false; |
|
1330 |
|
1331 isInfinite |= mozilla::IsInfinite(x); |
|
1332 isNaN |= mozilla::IsNaN(x); |
|
1333 |
|
1334 double xabs = mozilla::Abs(x); |
|
1335 |
|
1336 if (scale < xabs) { |
|
1337 sumsq = 1 + sumsq * (scale / xabs) * (scale / xabs); |
|
1338 scale = xabs; |
|
1339 } else if (scale != 0) { |
|
1340 sumsq += (xabs / scale) * (xabs / scale); |
|
1341 } |
|
1342 } |
|
1343 |
|
1344 double result = isInfinite ? PositiveInfinity<double>() : |
|
1345 isNaN ? GenericNaN() : |
|
1346 scale * sqrt(sumsq); |
|
1347 args.rval().setNumber(result); |
|
1348 return true; |
|
1349 } |
|
1350 |
|
1351 #if !HAVE_TRUNC |
|
1352 double trunc(double x) |
|
1353 { |
|
1354 return x > 0 ? floor(x) : ceil(x); |
|
1355 } |
|
1356 #endif |
|
1357 |
|
1358 double |
|
1359 js::math_trunc_impl(MathCache *cache, double x) |
|
1360 { |
|
1361 return cache->lookup(trunc, x); |
|
1362 } |
|
1363 |
|
1364 double |
|
1365 js::math_trunc_uncached(double x) |
|
1366 { |
|
1367 return trunc(x); |
|
1368 } |
|
1369 |
|
1370 bool |
|
1371 js::math_trunc(JSContext *cx, unsigned argc, Value *vp) |
|
1372 { |
|
1373 return math_function<math_trunc_impl>(cx, argc, vp); |
|
1374 } |
|
1375 |
|
1376 static double sign(double x) |
|
1377 { |
|
1378 if (mozilla::IsNaN(x)) |
|
1379 return GenericNaN(); |
|
1380 |
|
1381 return x == 0 ? x : x < 0 ? -1 : 1; |
|
1382 } |
|
1383 |
|
1384 double |
|
1385 js::math_sign_impl(MathCache *cache, double x) |
|
1386 { |
|
1387 return cache->lookup(sign, x); |
|
1388 } |
|
1389 |
|
1390 double |
|
1391 js::math_sign_uncached(double x) |
|
1392 { |
|
1393 return sign(x); |
|
1394 } |
|
1395 |
|
1396 bool |
|
1397 js::math_sign(JSContext *cx, unsigned argc, Value *vp) |
|
1398 { |
|
1399 return math_function<math_sign_impl>(cx, argc, vp); |
|
1400 } |
|
1401 |
|
1402 #if !HAVE_CBRT |
|
1403 double cbrt(double x) |
|
1404 { |
|
1405 if (x > 0) { |
|
1406 return pow(x, 1.0 / 3.0); |
|
1407 } else if (x == 0) { |
|
1408 return x; |
|
1409 } else { |
|
1410 return -pow(-x, 1.0 / 3.0); |
|
1411 } |
|
1412 } |
|
1413 #endif |
|
1414 |
|
1415 double |
|
1416 js::math_cbrt_impl(MathCache *cache, double x) |
|
1417 { |
|
1418 return cache->lookup(cbrt, x); |
|
1419 } |
|
1420 |
|
1421 double |
|
1422 js::math_cbrt_uncached(double x) |
|
1423 { |
|
1424 return cbrt(x); |
|
1425 } |
|
1426 |
|
1427 bool |
|
1428 js::math_cbrt(JSContext *cx, unsigned argc, Value *vp) |
|
1429 { |
|
1430 return math_function<math_cbrt_impl>(cx, argc, vp); |
|
1431 } |
|
1432 |
|
1433 #if JS_HAS_TOSOURCE |
|
1434 static bool |
|
1435 math_toSource(JSContext *cx, unsigned argc, Value *vp) |
|
1436 { |
|
1437 CallArgs args = CallArgsFromVp(argc, vp); |
|
1438 args.rval().setString(cx->names().Math); |
|
1439 return true; |
|
1440 } |
|
1441 #endif |
|
1442 |
|
1443 static const JSFunctionSpec math_static_methods[] = { |
|
1444 #if JS_HAS_TOSOURCE |
|
1445 JS_FN(js_toSource_str, math_toSource, 0, 0), |
|
1446 #endif |
|
1447 JS_FN("abs", js_math_abs, 1, 0), |
|
1448 JS_FN("acos", math_acos, 1, 0), |
|
1449 JS_FN("asin", math_asin, 1, 0), |
|
1450 JS_FN("atan", math_atan, 1, 0), |
|
1451 JS_FN("atan2", math_atan2, 2, 0), |
|
1452 JS_FN("ceil", math_ceil, 1, 0), |
|
1453 JS_FN("clz32", math_clz32, 1, 0), |
|
1454 JS_FN("cos", math_cos, 1, 0), |
|
1455 JS_FN("exp", math_exp, 1, 0), |
|
1456 JS_FN("floor", math_floor, 1, 0), |
|
1457 JS_FN("imul", math_imul, 2, 0), |
|
1458 JS_FN("fround", math_fround, 1, 0), |
|
1459 JS_FN("log", math_log, 1, 0), |
|
1460 JS_FN("max", js_math_max, 2, 0), |
|
1461 JS_FN("min", js_math_min, 2, 0), |
|
1462 JS_FN("pow", js_math_pow, 2, 0), |
|
1463 JS_FN("random", js_math_random, 0, 0), |
|
1464 JS_FN("round", math_round, 1, 0), |
|
1465 JS_FN("sin", math_sin, 1, 0), |
|
1466 JS_FN("sqrt", js_math_sqrt, 1, 0), |
|
1467 JS_FN("tan", math_tan, 1, 0), |
|
1468 JS_FN("log10", math_log10, 1, 0), |
|
1469 JS_FN("log2", math_log2, 1, 0), |
|
1470 JS_FN("log1p", math_log1p, 1, 0), |
|
1471 JS_FN("expm1", math_expm1, 1, 0), |
|
1472 JS_FN("cosh", math_cosh, 1, 0), |
|
1473 JS_FN("sinh", math_sinh, 1, 0), |
|
1474 JS_FN("tanh", math_tanh, 1, 0), |
|
1475 JS_FN("acosh", math_acosh, 1, 0), |
|
1476 JS_FN("asinh", math_asinh, 1, 0), |
|
1477 JS_FN("atanh", math_atanh, 1, 0), |
|
1478 JS_FN("hypot", math_hypot, 2, 0), |
|
1479 JS_FN("trunc", math_trunc, 1, 0), |
|
1480 JS_FN("sign", math_sign, 1, 0), |
|
1481 JS_FN("cbrt", math_cbrt, 1, 0), |
|
1482 JS_FS_END |
|
1483 }; |
|
1484 |
|
1485 JSObject * |
|
1486 js_InitMathClass(JSContext *cx, HandleObject obj) |
|
1487 { |
|
1488 RootedObject proto(cx, obj->as<GlobalObject>().getOrCreateObjectPrototype(cx)); |
|
1489 if (!proto) |
|
1490 return nullptr; |
|
1491 RootedObject Math(cx, NewObjectWithGivenProto(cx, &MathClass, proto, obj, SingletonObject)); |
|
1492 if (!Math) |
|
1493 return nullptr; |
|
1494 |
|
1495 if (!JS_DefineProperty(cx, obj, js_Math_str, Math, 0, |
|
1496 JS_PropertyStub, JS_StrictPropertyStub)) |
|
1497 { |
|
1498 return nullptr; |
|
1499 } |
|
1500 |
|
1501 if (!JS_DefineFunctions(cx, Math, math_static_methods)) |
|
1502 return nullptr; |
|
1503 if (!JS_DefineConstDoubles(cx, Math, math_constants)) |
|
1504 return nullptr; |
|
1505 |
|
1506 obj->as<GlobalObject>().setConstructor(JSProto_Math, ObjectValue(*Math)); |
|
1507 |
|
1508 return Math; |
|
1509 } |