1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla Communicator client code, released
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
50 #include "jsbuiltins.h"
52 #include "jsversion.h"
56 #include "jslibmath.h"
57 #include "jscompartment.h"
62 #define M_E 2.7182818284590452354
65 #define M_LOG2E 1.4426950408889634074
68 #define M_LOG10E 0.43429448190325182765
71 #define M_LN2 0.69314718055994530942
74 #define M_LN10 2.30258509299404568402
77 #define M_PI 3.14159265358979323846
80 #define M_SQRT2 1.41421356237309504880
83 #define M_SQRT1_2 0.70710678118654752440
86 static JSConstDoubleSpec math_constants[] = {
87 {M_E, "E", 0, {0,0,0}},
88 {M_LOG2E, "LOG2E", 0, {0,0,0}},
89 {M_LOG10E, "LOG10E", 0, {0,0,0}},
90 {M_LN2, "LN2", 0, {0,0,0}},
91 {M_LN10, "LN10", 0, {0,0,0}},
92 {M_PI, "PI", 0, {0,0,0}},
93 {M_SQRT2, "SQRT2", 0, {0,0,0}},
94 {M_SQRT1_2, "SQRT1_2", 0, {0,0,0}},
98 MathCache::MathCache() {
99 memset(table, 0, sizeof(table));
101 /* See comments in lookup(). */
102 JS_ASSERT(JSDOUBLE_IS_NEGZERO(-0.0));
103 JS_ASSERT(!JSDOUBLE_IS_NEGZERO(+0.0));
104 JS_ASSERT(hash(-0.0) != hash(+0.0));
107 Class js_MathClass = {
109 JSCLASS_HAS_CACHED_PROTO(JSProto_Math),
110 PropertyStub, /* addProperty */
111 PropertyStub, /* delProperty */
112 PropertyStub, /* getProperty */
113 StrictPropertyStub, /* setProperty */
120 js_math_abs(JSContext *cx, uintN argc, Value *vp)
125 vp->setDouble(js_NaN);
128 if (!ValueToNumber(cx, vp[2], &x))
136 math_acos(JSContext *cx, uintN argc, Value *vp)
141 vp->setDouble(js_NaN);
144 if (!ValueToNumber(cx, vp[2], &x))
146 #if defined(SOLARIS) && defined(__GNUC__)
147 if (x < -1 || 1 < x) {
148 vp->setDouble(js_NaN);
152 MathCache *mathCache = GetMathCache(cx);
155 z = mathCache->lookup(acos, x);
161 math_asin(JSContext *cx, uintN argc, Value *vp)
166 vp->setDouble(js_NaN);
169 if (!ValueToNumber(cx, vp[2], &x))
171 #if defined(SOLARIS) && defined(__GNUC__)
172 if (x < -1 || 1 < x) {
173 vp->setDouble(js_NaN);
177 MathCache *mathCache = GetMathCache(cx);
180 z = mathCache->lookup(asin, x);
186 math_atan(JSContext *cx, uintN argc, Value *vp)
191 vp->setDouble(js_NaN);
194 if (!ValueToNumber(cx, vp[2], &x))
196 MathCache *mathCache = GetMathCache(cx);
199 z = mathCache->lookup(atan, x);
204 static inline jsdouble JS_FASTCALL
205 math_atan2_kernel(jsdouble x, jsdouble y)
207 #if defined(_MSC_VER)
209 * MSVC's atan2 does not yield the result demanded by ECMA when both x
210 * and y are infinite.
211 * - The result is a multiple of pi/4.
212 * - The sign of x determines the sign of the result.
213 * - The sign of y determines the multiplicator, 1 or 3.
215 if (JSDOUBLE_IS_INFINITE(x) && JSDOUBLE_IS_INFINITE(y)) {
216 jsdouble z = js_copysign(M_PI / 4, x);
223 #if defined(SOLARIS) && defined(__GNUC__)
225 if (JSDOUBLE_IS_NEGZERO(y))
226 return js_copysign(M_PI, x);
235 math_atan2(JSContext *cx, uintN argc, Value *vp)
240 vp->setDouble(js_NaN);
243 if (!ValueToNumber(cx, vp[2], &x))
245 if (!ValueToNumber(cx, vp[3], &y))
247 z = math_atan2_kernel(x, y);
253 js_math_ceil_impl(jsdouble x)
256 if (x < 0 && x > -1.0)
257 return js_copysign(0, -1);
263 js_math_ceil(JSContext *cx, uintN argc, Value *vp)
268 vp->setDouble(js_NaN);
271 if (!ValueToNumber(cx, vp[2], &x))
273 z = js_math_ceil_impl(x);
279 math_cos(JSContext *cx, uintN argc, Value *vp)
284 vp->setDouble(js_NaN);
287 if (!ValueToNumber(cx, vp[2], &x))
289 MathCache *mathCache = GetMathCache(cx);
292 z = mathCache->lookup(cos, x);
298 math_exp_body(double d)
301 if (!JSDOUBLE_IS_NaN(d)) {
302 if (d == js_PositiveInfinity)
303 return js_PositiveInfinity;
304 if (d == js_NegativeInfinity)
312 math_exp(JSContext *cx, uintN argc, Value *vp)
317 vp->setDouble(js_NaN);
320 if (!ValueToNumber(cx, vp[2], &x))
322 MathCache *mathCache = GetMathCache(cx);
325 z = mathCache->lookup(math_exp_body, x);
331 js_math_floor_impl(jsdouble x)
337 js_math_floor(JSContext *cx, uintN argc, Value *vp)
342 vp->setDouble(js_NaN);
345 if (!ValueToNumber(cx, vp[2], &x))
347 z = js_math_floor_impl(x);
353 math_log(JSContext *cx, uintN argc, Value *vp)
358 vp->setDouble(js_NaN);
361 if (!ValueToNumber(cx, vp[2], &x))
363 #if defined(SOLARIS) && defined(__GNUC__)
365 vp->setDouble(js_NaN);
369 MathCache *mathCache = GetMathCache(cx);
372 z = mathCache->lookup(log, x);
378 js_math_max(JSContext *cx, uintN argc, Value *vp)
380 jsdouble x, z = js_NegativeInfinity;
385 vp->setDouble(js_NegativeInfinity);
389 for (i = 0; i < argc; i++) {
390 if (!ValueToNumber(cx, argv[i], &x))
392 if (JSDOUBLE_IS_NaN(x)) {
393 vp->setDouble(js_NaN);
396 if (x == 0 && x == z) {
397 if (js_copysign(1.0, z) == -1)
408 js_math_min(JSContext *cx, uintN argc, Value *vp)
410 jsdouble x, z = js_PositiveInfinity;
415 vp->setDouble(js_PositiveInfinity);
419 for (i = 0; i < argc; i++) {
420 if (!ValueToNumber(cx, argv[i], &x))
422 if (JSDOUBLE_IS_NaN(x)) {
423 vp->setDouble(js_NaN);
426 if (x == 0 && x == z) {
427 if (js_copysign(1.0, x) == -1)
438 powi(jsdouble x, jsint y)
440 jsuint n = (y < 0) ? -y : y;
444 if ((n & 1) != 0) p *= m;
448 // Unfortunately, we have to be careful when p has reached
449 // infinity in the computation, because sometimes the higher
450 // internal precision in the pow() implementation would have
451 // given us a finite p. This happens very rarely.
453 jsdouble result = 1.0 / p;
454 return (result == 0 && JSDOUBLE_IS_INFINITE(p))
455 ? pow(x, static_cast<jsdouble>(y)) // Avoid pow(double, int).
466 math_pow(JSContext *cx, uintN argc, Value *vp)
471 vp->setDouble(js_NaN);
474 if (!ValueToNumber(cx, vp[2], &x))
476 if (!ValueToNumber(cx, vp[3], &y))
479 * Special case for square roots. Note that pow(x, 0.5) != sqrt(x)
480 * when x = -0.0, so we have to guard for this.
482 if (JSDOUBLE_IS_FINITE(x) && x != 0.0) {
484 vp->setNumber(sqrt(x));
488 vp->setNumber(1.0/sqrt(x));
493 * Because C99 and ECMA specify different behavior for pow(),
494 * we need to wrap the libm call to make it ECMA compliant.
496 if (!JSDOUBLE_IS_FINITE(y) && (x == 1.0 || x == -1.0)) {
497 vp->setDouble(js_NaN);
500 /* pow(x, +-0) is always 1, even for x = NaN. */
507 z = powi(x, vp[3].toInt32());
515 static const int64 RNG_MULTIPLIER = 0x5DEECE66DLL;
516 static const int64 RNG_ADDEND = 0xBLL;
517 static const int64 RNG_MASK = (1LL << 48) - 1;
518 static const jsdouble RNG_DSCALE = jsdouble(1LL << 53);
521 * Math.random() support, lifted from java.util.Random.java.
524 random_setSeed(JSContext *cx, int64 seed)
526 cx->rngSeed = (seed ^ RNG_MULTIPLIER) & RNG_MASK;
530 js_InitRandom(JSContext *cx)
533 * Set the seed from current time. Since we have a RNG per context and we often bring
534 * up several contexts at the same time, we xor in some additional values, namely
535 * the context and its successor. We don't just use the context because it might be
536 * possible to reverse engineer the context pointer if one guesses the time right.
539 (PRMJ_Now() / 1000) ^
541 int64(cx->link.next));
545 random_next(JSContext *cx, int bits)
547 uint64 nextseed = cx->rngSeed * RNG_MULTIPLIER;
548 nextseed += RNG_ADDEND;
549 nextseed &= RNG_MASK;
550 cx->rngSeed = nextseed;
551 return nextseed >> (48 - bits);
554 static inline jsdouble
555 random_nextDouble(JSContext *cx)
557 return jsdouble((random_next(cx, 26) << 27) + random_next(cx, 27)) / RNG_DSCALE;
561 math_random(JSContext *cx, uintN argc, Value *vp)
563 jsdouble z = random_nextDouble(cx);
568 #if defined _WIN32 && !defined WINCE && _MSC_VER < 1400
569 /* Try to work around apparent _copysign bustage in VC7.x. */
571 js_copysign(double x, double y)
577 xu.s.hi &= ~JSDOUBLE_HI32_SIGNBIT;
578 xu.s.hi |= yu.s.hi & JSDOUBLE_HI32_SIGNBIT;
584 js_math_round_impl(jsdouble x)
586 return js_copysign(floor(x + 0.5), x);
590 js_math_round(JSContext *cx, uintN argc, Value *vp)
595 vp->setDouble(js_NaN);
598 if (!ValueToNumber(cx, vp[2], &x))
600 z = js_copysign(floor(x + 0.5), x);
606 math_sin(JSContext *cx, uintN argc, Value *vp)
611 vp->setDouble(js_NaN);
614 if (!ValueToNumber(cx, vp[2], &x))
616 MathCache *mathCache = GetMathCache(cx);
619 z = mathCache->lookup(sin, x);
625 math_sqrt(JSContext *cx, uintN argc, Value *vp)
630 vp->setDouble(js_NaN);
633 if (!ValueToNumber(cx, vp[2], &x))
635 MathCache *mathCache = GetMathCache(cx);
638 z = mathCache->lookup(sqrt, x);
644 math_tan(JSContext *cx, uintN argc, Value *vp)
649 vp->setDouble(js_NaN);
652 if (!ValueToNumber(cx, vp[2], &x))
654 MathCache *mathCache = GetMathCache(cx);
657 z = mathCache->lookup(tan, x);
664 math_toSource(JSContext *cx, uintN argc, Value *vp)
666 vp->setString(ATOM_TO_STRING(CLASS_ATOM(cx, Math)));
673 #define MATH_BUILTIN_1(name, cfun) \
674 static jsdouble FASTCALL name##_tn(MathCache *cache, jsdouble d) { \
675 return cache->lookup(cfun, d); \
677 JS_DEFINE_TRCINFO_1(name, \
678 (2, (static, DOUBLE, name##_tn, MATHCACHE, DOUBLE, 1, nanojit::ACCSET_NONE)))
680 MATH_BUILTIN_1(js_math_abs, fabs)
681 MATH_BUILTIN_1(math_atan, atan)
682 MATH_BUILTIN_1(math_sin, sin)
683 MATH_BUILTIN_1(math_cos, cos)
684 MATH_BUILTIN_1(math_sqrt, sqrt)
685 MATH_BUILTIN_1(math_tan, tan)
687 static jsdouble FASTCALL
688 math_acos_tn(MathCache *cache, jsdouble d)
690 #if defined(SOLARIS) && defined(__GNUC__)
691 if (d < -1 || 1 < d) {
695 return cache->lookup(acos, d);
698 static jsdouble FASTCALL
699 math_asin_tn(MathCache *cache, jsdouble d)
701 #if defined(SOLARIS) && defined(__GNUC__)
702 if (d < -1 || 1 < d) {
706 return cache->lookup(asin, d);
709 static jsdouble FASTCALL
710 math_exp_tn(MathCache *cache, jsdouble d)
712 return cache->lookup(math_exp_body, d);
715 JS_DEFINE_TRCINFO_1(math_exp,
716 (2, (static, DOUBLE, math_exp_tn, MATHCACHE, DOUBLE, 1, nanojit::ACCSET_NONE)))
718 static jsdouble FASTCALL
719 math_log_tn(MathCache *cache, jsdouble d)
721 #if defined(SOLARIS) && defined(__GNUC__)
725 return cache->lookup(log, d);
728 static jsdouble FASTCALL
729 math_max_tn(jsdouble d, jsdouble p)
731 if (JSDOUBLE_IS_NaN(d) || JSDOUBLE_IS_NaN(p))
734 if (p == 0 && p == d) {
735 // Max prefers 0.0 to -0.0.
736 if (js_copysign(1.0, d) == -1)
740 return (p > d) ? p : d;
743 static jsdouble FASTCALL
744 math_min_tn(jsdouble d, jsdouble p)
746 if (JSDOUBLE_IS_NaN(d) || JSDOUBLE_IS_NaN(p))
749 if (p == 0 && p == d) {
750 // Min prefers -0.0 to 0.0.
751 if (js_copysign (1.0, p) == -1)
755 return (p < d) ? p : d;
758 static jsdouble FASTCALL
759 math_pow_tn(jsdouble d, jsdouble p)
762 * Special case for square roots. Note that pow(x, 0.5) != sqrt(x)
763 * when x = -0.0, so we have to guard for this.
765 if (JSDOUBLE_IS_FINITE(d) && d != 0.0) {
772 if (!JSDOUBLE_IS_FINITE(p) && (d == 1.0 || d == -1.0))
777 if (JSDOUBLE_IS_INT32(p, &i))
783 static jsdouble FASTCALL
784 math_random_tn(JSContext *cx)
786 return random_nextDouble(cx);
789 static jsdouble FASTCALL
790 math_round_tn(jsdouble x)
792 return js_math_round_impl(x);
795 static jsdouble FASTCALL
796 math_ceil_tn(jsdouble x)
798 return js_math_ceil_impl(x);
801 static jsdouble FASTCALL
802 math_floor_tn(jsdouble x)
804 return js_math_floor_impl(x);
807 JS_DEFINE_TRCINFO_1(math_acos,
808 (2, (static, DOUBLE, math_acos_tn, MATHCACHE, DOUBLE, 1, nanojit::ACCSET_NONE)))
809 JS_DEFINE_TRCINFO_1(math_asin,
810 (2, (static, DOUBLE, math_asin_tn, MATHCACHE, DOUBLE, 1, nanojit::ACCSET_NONE)))
811 JS_DEFINE_TRCINFO_1(math_atan2,
812 (2, (static, DOUBLE, math_atan2_kernel, DOUBLE, DOUBLE, 1, nanojit::ACCSET_NONE)))
813 JS_DEFINE_TRCINFO_1(js_math_floor,
814 (1, (static, DOUBLE, math_floor_tn, DOUBLE, 1, nanojit::ACCSET_NONE)))
815 JS_DEFINE_TRCINFO_1(math_log,
816 (2, (static, DOUBLE, math_log_tn, MATHCACHE, DOUBLE, 1, nanojit::ACCSET_NONE)))
817 JS_DEFINE_TRCINFO_1(js_math_max,
818 (2, (static, DOUBLE, math_max_tn, DOUBLE, DOUBLE, 1, nanojit::ACCSET_NONE)))
819 JS_DEFINE_TRCINFO_1(js_math_min,
820 (2, (static, DOUBLE, math_min_tn, DOUBLE, DOUBLE, 1, nanojit::ACCSET_NONE)))
821 JS_DEFINE_TRCINFO_1(math_pow,
822 (2, (static, DOUBLE, math_pow_tn, DOUBLE, DOUBLE, 1, nanojit::ACCSET_NONE)))
823 JS_DEFINE_TRCINFO_1(math_random,
824 (1, (static, DOUBLE, math_random_tn, CONTEXT, 0, nanojit::ACCSET_STORE_ANY)))
825 JS_DEFINE_TRCINFO_1(js_math_round,
826 (1, (static, DOUBLE, math_round_tn, DOUBLE, 1, nanojit::ACCSET_NONE)))
827 JS_DEFINE_TRCINFO_1(js_math_ceil,
828 (1, (static, DOUBLE, math_ceil_tn, DOUBLE, 1, nanojit::ACCSET_NONE)))
830 #endif /* JS_TRACER */
832 static JSFunctionSpec math_static_methods[] = {
834 JS_FN(js_toSource_str, math_toSource, 0, 0),
836 JS_TN("abs", js_math_abs, 1, 0, &js_math_abs_trcinfo),
837 JS_TN("acos", math_acos, 1, 0, &math_acos_trcinfo),
838 JS_TN("asin", math_asin, 1, 0, &math_asin_trcinfo),
839 JS_TN("atan", math_atan, 1, 0, &math_atan_trcinfo),
840 JS_TN("atan2", math_atan2, 2, 0, &math_atan2_trcinfo),
841 JS_TN("ceil", js_math_ceil, 1, 0, &js_math_ceil_trcinfo),
842 JS_TN("cos", math_cos, 1, 0, &math_cos_trcinfo),
843 JS_TN("exp", math_exp, 1, 0, &math_exp_trcinfo),
844 JS_TN("floor", js_math_floor, 1, 0, &js_math_floor_trcinfo),
845 JS_TN("log", math_log, 1, 0, &math_log_trcinfo),
846 JS_TN("max", js_math_max, 2, 0, &js_math_max_trcinfo),
847 JS_TN("min", js_math_min, 2, 0, &js_math_min_trcinfo),
848 JS_TN("pow", math_pow, 2, 0, &math_pow_trcinfo),
849 JS_TN("random", math_random, 0, 0, &math_random_trcinfo),
850 JS_TN("round", js_math_round, 1, 0, &js_math_round_trcinfo),
851 JS_TN("sin", math_sin, 1, 0, &math_sin_trcinfo),
852 JS_TN("sqrt", math_sqrt, 1, 0, &math_sqrt_trcinfo),
853 JS_TN("tan", math_tan, 1, 0, &math_tan_trcinfo),
858 js_IsMathFunction(JSNative native)
860 for (size_t i=0; math_static_methods[i].name != NULL; i++) {
861 if (native == math_static_methods[i].call)
868 js_InitMathClass(JSContext *cx, JSObject *obj)
872 Math = JS_NewObject(cx, Jsvalify(&js_MathClass), NULL, obj);
875 if (!JS_DefineProperty(cx, obj, js_Math_str, OBJECT_TO_JSVAL(Math),
876 JS_PropertyStub, JS_StrictPropertyStub, 0)) {
880 if (!JS_DefineFunctions(cx, Math, math_static_methods))
882 if (!JS_DefineConstDoubles(cx, Math, math_constants))