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"
59 extern jsdouble js_NaN
;
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 JSClass js_MathClass
= {
100 JSCLASS_HAS_CACHED_PROTO(JSProto_Math
),
101 JS_PropertyStub
, JS_PropertyStub
, JS_PropertyStub
, JS_PropertyStub
,
102 JS_EnumerateStub
, JS_ResolveStub
, JS_ConvertStub
, NULL
,
103 JSCLASS_NO_OPTIONAL_MEMBERS
107 math_abs(JSContext
*cx
, uintN argc
, jsval
*vp
)
112 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
115 x
= js_ValueToNumber(cx
, &vp
[2]);
116 if (JSVAL_IS_NULL(vp
[2]))
119 return js_NewNumberInRootedValue(cx
, z
, vp
);
123 math_acos(JSContext
*cx
, uintN argc
, jsval
*vp
)
128 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
131 x
= js_ValueToNumber(cx
, &vp
[2]);
132 if (JSVAL_IS_NULL(vp
[2]))
134 #if defined(SOLARIS) && defined(__GNUC__)
135 if (x
< -1 || 1 < x
) {
136 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
141 return js_NewNumberInRootedValue(cx
, z
, vp
);
145 math_asin(JSContext
*cx
, uintN argc
, jsval
*vp
)
150 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
153 x
= js_ValueToNumber(cx
, &vp
[2]);
154 if (JSVAL_IS_NULL(vp
[2]))
156 #if defined(SOLARIS) && defined(__GNUC__)
157 if (x
< -1 || 1 < x
) {
158 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
163 return js_NewNumberInRootedValue(cx
, z
, vp
);
167 math_atan(JSContext
*cx
, uintN argc
, jsval
*vp
)
172 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
175 x
= js_ValueToNumber(cx
, &vp
[2]);
176 if (JSVAL_IS_NULL(vp
[2]))
179 return js_NewNumberInRootedValue(cx
, z
, vp
);
182 static inline jsdouble JS_FASTCALL
183 math_atan2_kernel(jsdouble x
, jsdouble y
)
185 #if defined(_MSC_VER)
187 * MSVC's atan2 does not yield the result demanded by ECMA when both x
188 * and y are infinite.
189 * - The result is a multiple of pi/4.
190 * - The sign of x determines the sign of the result.
191 * - The sign of y determines the multiplicator, 1 or 3.
193 if (JSDOUBLE_IS_INFINITE(x
) && JSDOUBLE_IS_INFINITE(y
)) {
194 jsdouble z
= js_copysign(M_PI
/ 4, x
);
201 #if defined(SOLARIS) && defined(__GNUC__)
203 if (JSDOUBLE_IS_NEGZERO(y
))
204 return js_copysign(M_PI
, x
);
213 math_atan2(JSContext
*cx
, uintN argc
, jsval
*vp
)
218 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
221 x
= js_ValueToNumber(cx
, &vp
[2]);
222 if (JSVAL_IS_NULL(vp
[2]))
224 y
= js_ValueToNumber(cx
, &vp
[3]);
225 if (JSVAL_IS_NULL(vp
[3]))
227 return js_NewNumberInRootedValue(cx
, math_atan2_kernel (x
, y
), vp
);
230 static inline jsdouble JS_FASTCALL
231 math_ceil_kernel(jsdouble x
)
234 if (x
< 0 && x
> -1.0)
235 return js_copysign(0, -1);
241 math_ceil(JSContext
*cx
, uintN argc
, jsval
*vp
)
246 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
249 x
= js_ValueToNumber(cx
, &vp
[2]);
250 if (JSVAL_IS_NULL(vp
[2]))
252 z
= math_ceil_kernel(x
);
253 return js_NewNumberInRootedValue(cx
, z
, vp
);
257 math_cos(JSContext
*cx
, uintN argc
, jsval
*vp
)
262 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
265 x
= js_ValueToNumber(cx
, &vp
[2]);
266 if (JSVAL_IS_NULL(vp
[2]))
269 return js_NewNumberInRootedValue(cx
, z
, vp
);
273 math_exp(JSContext
*cx
, uintN argc
, jsval
*vp
)
278 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
281 x
= js_ValueToNumber(cx
, &vp
[2]);
282 if (JSVAL_IS_NULL(vp
[2]))
285 if (!JSDOUBLE_IS_NaN(x
)) {
286 if (x
== *cx
->runtime
->jsPositiveInfinity
) {
287 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsPositiveInfinity
);
290 if (x
== *cx
->runtime
->jsNegativeInfinity
) {
297 return js_NewNumberInRootedValue(cx
, z
, vp
);
301 math_floor(JSContext
*cx
, uintN argc
, jsval
*vp
)
306 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
309 x
= js_ValueToNumber(cx
, &vp
[2]);
310 if (JSVAL_IS_NULL(vp
[2]))
313 return js_NewNumberInRootedValue(cx
, z
, vp
);
317 math_log(JSContext
*cx
, uintN argc
, jsval
*vp
)
322 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
325 x
= js_ValueToNumber(cx
, &vp
[2]);
326 if (JSVAL_IS_NULL(vp
[2]))
328 #if defined(SOLARIS) && defined(__GNUC__)
330 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
335 return js_NewNumberInRootedValue(cx
, z
, vp
);
339 math_max(JSContext
*cx
, uintN argc
, jsval
*vp
)
341 jsdouble x
, z
= *cx
->runtime
->jsNegativeInfinity
;
346 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNegativeInfinity
);
350 for (i
= 0; i
< argc
; i
++) {
351 x
= js_ValueToNumber(cx
, &argv
[i
]);
352 if (JSVAL_IS_NULL(argv
[i
]))
354 if (JSDOUBLE_IS_NaN(x
)) {
355 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
358 if (x
== 0 && x
== z
) {
359 if (js_copysign(1.0, z
) == -1)
365 return js_NewNumberInRootedValue(cx
, z
, vp
);
369 math_min(JSContext
*cx
, uintN argc
, jsval
*vp
)
371 jsdouble x
, z
= *cx
->runtime
->jsPositiveInfinity
;
376 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsPositiveInfinity
);
380 for (i
= 0; i
< argc
; i
++) {
381 x
= js_ValueToNumber(cx
, &argv
[i
]);
382 if (JSVAL_IS_NULL(argv
[i
]))
384 if (JSDOUBLE_IS_NaN(x
)) {
385 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
388 if (x
== 0 && x
== z
) {
389 if (js_copysign(1.0, x
) == -1)
395 return js_NewNumberInRootedValue(cx
, z
, vp
);
399 math_pow(JSContext
*cx
, uintN argc
, jsval
*vp
)
404 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
407 x
= js_ValueToNumber(cx
, &vp
[2]);
408 if (JSVAL_IS_NULL(vp
[2]))
410 y
= js_ValueToNumber(cx
, &vp
[3]);
411 if (JSVAL_IS_NULL(vp
[3]))
414 * Because C99 and ECMA specify different behavior for pow(),
415 * we need to wrap the libm call to make it ECMA compliant.
417 if (!JSDOUBLE_IS_FINITE(y
) && (x
== 1.0 || x
== -1.0)) {
418 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
421 /* pow(x, +-0) is always 1, even for x = NaN. */
427 return js_NewNumberInRootedValue(cx
, z
, vp
);
431 * Math.random() support, lifted from java.util.Random.java.
434 random_setSeed(JSRuntime
*rt
, int64 seed
)
439 JSLL_DIV(seed
, seed
, tmp
);
440 JSLL_XOR(tmp
, seed
, rt
->rngMultiplier
);
441 JSLL_AND(rt
->rngSeed
, tmp
, rt
->rngMask
);
445 js_random_init(JSRuntime
*rt
)
449 /* Do at most once. */
450 if (rt
->rngInitialized
)
452 rt
->rngInitialized
= JS_TRUE
;
454 /* rt->rngMultiplier = 0x5DEECE66DL */
455 JSLL_ISHL(tmp
, 0x5, 32);
456 JSLL_UI2L(tmp2
, 0xDEECE66DL
);
457 JSLL_OR(rt
->rngMultiplier
, tmp
, tmp2
);
459 /* rt->rngAddend = 0xBL */
460 JSLL_I2L(rt
->rngAddend
, 0xBL
);
462 /* rt->rngMask = (1L << 48) - 1 */
464 JSLL_SHL(tmp2
, tmp
, 48);
465 JSLL_SUB(rt
->rngMask
, tmp2
, tmp
);
467 /* rt->rngDscale = (jsdouble)(1L << 53) */
468 JSLL_SHL(tmp2
, tmp
, 53);
469 JSLL_L2D(rt
->rngDscale
, tmp2
);
471 /* Finally, set the seed from current time. */
472 random_setSeed(rt
, PRMJ_Now());
476 random_next(JSRuntime
*rt
, int bits
)
481 JSLL_MUL(nextseed
, rt
->rngSeed
, rt
->rngMultiplier
);
482 JSLL_ADD(nextseed
, nextseed
, rt
->rngAddend
);
483 JSLL_AND(nextseed
, nextseed
, rt
->rngMask
);
484 rt
->rngSeed
= nextseed
;
485 JSLL_USHR(tmp
, nextseed
, 48 - bits
);
486 JSLL_L2I(retval
, tmp
);
491 js_random_nextDouble(JSRuntime
*rt
)
496 JSLL_ISHL(tmp
, random_next(rt
, 26), 27);
497 JSLL_UI2L(tmp2
, random_next(rt
, 27));
498 JSLL_ADD(tmp
, tmp
, tmp2
);
500 return d
/ rt
->rngDscale
;
504 math_random(JSContext
*cx
, uintN argc
, jsval
*vp
)
512 z
= js_random_nextDouble(rt
);
513 JS_UNLOCK_RUNTIME(rt
);
514 return js_NewNumberInRootedValue(cx
, z
, vp
);
517 #if defined _WIN32 && !defined WINCE && _MSC_VER < 1400
518 /* Try to work around apparent _copysign bustage in VC6 and VC7. */
520 js_copysign(double x
, double y
)
526 xu
.s
.hi
&= ~JSDOUBLE_HI32_SIGNBIT
;
527 xu
.s
.hi
|= yu
.s
.hi
& JSDOUBLE_HI32_SIGNBIT
;
533 math_round(JSContext
*cx
, uintN argc
, jsval
*vp
)
538 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
541 x
= js_ValueToNumber(cx
, &vp
[2]);
542 if (JSVAL_IS_NULL(vp
[2]))
544 z
= js_copysign(floor(x
+ 0.5), x
);
545 return js_NewNumberInRootedValue(cx
, z
, vp
);
549 math_sin(JSContext
*cx
, uintN argc
, jsval
*vp
)
554 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
557 x
= js_ValueToNumber(cx
, &vp
[2]);
558 if (JSVAL_IS_NULL(vp
[2]))
561 return js_NewNumberInRootedValue(cx
, z
, vp
);
565 math_sqrt(JSContext
*cx
, uintN argc
, jsval
*vp
)
570 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
573 x
= js_ValueToNumber(cx
, &vp
[2]);
574 if (JSVAL_IS_NULL(vp
[2]))
577 return js_NewNumberInRootedValue(cx
, z
, vp
);
581 math_tan(JSContext
*cx
, uintN argc
, jsval
*vp
)
586 *vp
= DOUBLE_TO_JSVAL(cx
->runtime
->jsNaN
);
589 x
= js_ValueToNumber(cx
, &vp
[2]);
590 if (JSVAL_IS_NULL(vp
[2]))
593 return js_NewNumberInRootedValue(cx
, z
, vp
);
598 math_toSource(JSContext
*cx
, uintN argc
, jsval
*vp
)
600 *vp
= ATOM_KEY(CLASS_ATOM(cx
, Math
));
607 #define MATH_BUILTIN_1(name) MATH_BUILTIN_CFUN_1(name, name)
608 #define MATH_BUILTIN_CFUN_1(name, cfun) \
609 static jsdouble FASTCALL math_##name##_tn(jsdouble d) { return cfun(d); } \
610 JS_DEFINE_TRCINFO_1(math_##name, \
611 (1, (static, DOUBLE, math_##name##_tn, DOUBLE, 1, 1)))
613 MATH_BUILTIN_CFUN_1(abs
, fabs
)
618 MATH_BUILTIN_1(floor
)
621 static jsdouble FASTCALL
622 math_acos_tn(jsdouble d
)
624 #if defined(SOLARIS) && defined(__GNUC__)
625 if (d
< -1 || 1 < d
) {
632 static jsdouble FASTCALL
633 math_asin_tn(jsdouble d
)
635 #if defined(SOLARIS) && defined(__GNUC__)
636 if (d
< -1 || 1 < d
) {
645 static jsdouble FASTCALL
646 math_exp_tn(JSContext
*cx
, jsdouble d
)
648 if (!JSDOUBLE_IS_NaN(d
)) {
649 if (d
== *cx
->runtime
->jsPositiveInfinity
) {
650 return *cx
->runtime
->jsPositiveInfinity
;
652 if (d
== *cx
->runtime
->jsNegativeInfinity
) {
659 JS_DEFINE_TRCINFO_1(math_exp
,
660 (2, (static, DOUBLE
, math_exp_tn
, CONTEXT
, DOUBLE
, 1, 1)))
668 static jsdouble FASTCALL
669 math_log_tn(jsdouble d
)
671 #if defined(SOLARIS) && defined(__GNUC__)
678 static jsdouble FASTCALL
679 math_max_tn(jsdouble d
, jsdouble p
)
681 if (JSDOUBLE_IS_NaN(d
) || JSDOUBLE_IS_NaN(p
))
684 if (p
== 0 && p
== d
) {
685 // Max prefers 0.0 to -0.0.
686 if (js_copysign(1.0, d
) == -1)
690 return (p
> d
) ? p
: d
;
693 static jsdouble FASTCALL
694 math_min_tn(jsdouble d
, jsdouble p
)
696 if (JSDOUBLE_IS_NaN(d
) || JSDOUBLE_IS_NaN(p
))
699 if (p
== 0 && p
== d
) {
700 // Min prefers -0.0 to 0.0.
701 if (js_copysign (1.0, p
) == -1)
705 return (p
< d
) ? p
: d
;
708 static jsdouble FASTCALL
709 math_pow_tn(jsdouble d
, jsdouble p
)
711 if (!JSDOUBLE_IS_FINITE(p
) && (d
== 1.0 || d
== -1.0))
718 static jsdouble FASTCALL
719 math_random_tn(JSRuntime
* rt
)
723 jsdouble z
= js_random_nextDouble(rt
);
724 JS_UNLOCK_RUNTIME(rt
);
728 static jsdouble FASTCALL
729 math_round_tn(jsdouble x
)
731 return js_copysign(floor(x
+ 0.5), x
);
734 static jsdouble FASTCALL
735 math_ceil_tn(jsdouble x
)
737 return math_ceil_kernel(x
);
740 JS_DEFINE_TRCINFO_1(math_acos
,
741 (1, (static, DOUBLE
, math_acos_tn
, DOUBLE
, 1, 1)))
742 JS_DEFINE_TRCINFO_1(math_asin
,
743 (1, (static, DOUBLE
, math_asin_tn
, DOUBLE
, 1, 1)))
744 JS_DEFINE_TRCINFO_1(math_atan2
,
745 (2, (static, DOUBLE
, math_atan2_kernel
, DOUBLE
, DOUBLE
, 1, 1)))
746 JS_DEFINE_TRCINFO_1(math_log
,
747 (1, (static, DOUBLE
, math_log_tn
, DOUBLE
, 1, 1)))
748 JS_DEFINE_TRCINFO_1(math_max
,
749 (2, (static, DOUBLE
, math_max_tn
, DOUBLE
, DOUBLE
, 1, 1)))
750 JS_DEFINE_TRCINFO_1(math_min
,
751 (2, (static, DOUBLE
, math_min_tn
, DOUBLE
, DOUBLE
, 1, 1)))
752 JS_DEFINE_TRCINFO_1(math_pow
,
753 (2, (static, DOUBLE
, math_pow_tn
, DOUBLE
, DOUBLE
, 1, 1)))
754 JS_DEFINE_TRCINFO_1(math_random
,
755 (1, (static, DOUBLE
, math_random_tn
, RUNTIME
, 0, 0)))
756 JS_DEFINE_TRCINFO_1(math_round
,
757 (1, (static, DOUBLE
, math_round_tn
, DOUBLE
, 1, 1)))
758 JS_DEFINE_TRCINFO_1(math_ceil
,
759 (1, (static, DOUBLE
, math_ceil_tn
, DOUBLE
, 1, 1)))
761 #endif /* JS_TRACER */
763 static JSFunctionSpec math_static_methods
[] = {
765 JS_FN(js_toSource_str
, math_toSource
, 0, 0),
767 JS_TN("abs", math_abs
, 1, 0, math_abs_trcinfo
),
768 JS_TN("acos", math_acos
, 1, 0, math_acos_trcinfo
),
769 JS_TN("asin", math_asin
, 1, 0, math_asin_trcinfo
),
770 JS_TN("atan", math_atan
, 1, 0, math_atan_trcinfo
),
771 JS_TN("atan2", math_atan2
, 2, 0, math_atan2_trcinfo
),
772 JS_TN("ceil", math_ceil
, 1, 0, math_ceil_trcinfo
),
773 JS_TN("cos", math_cos
, 1, 0, math_cos_trcinfo
),
774 JS_TN("exp", math_exp
, 1, 0, math_exp_trcinfo
),
775 JS_TN("floor", math_floor
, 1, 0, math_floor_trcinfo
),
776 JS_TN("log", math_log
, 1, 0, math_log_trcinfo
),
777 JS_TN("max", math_max
, 2, 0, math_max_trcinfo
),
778 JS_TN("min", math_min
, 2, 0, math_min_trcinfo
),
779 JS_TN("pow", math_pow
, 2, 0, math_pow_trcinfo
),
780 JS_TN("random", math_random
, 0, 0, math_random_trcinfo
),
781 JS_TN("round", math_round
, 1, 0, math_round_trcinfo
),
782 JS_TN("sin", math_sin
, 1, 0, math_sin_trcinfo
),
783 JS_TN("sqrt", math_sqrt
, 1, 0, math_sqrt_trcinfo
),
784 JS_TN("tan", math_tan
, 1, 0, math_tan_trcinfo
),
789 js_InitMathClass(JSContext
*cx
, JSObject
*obj
)
793 Math
= JS_NewObject(cx
, &js_MathClass
, NULL
, obj
);
796 if (!JS_DefineProperty(cx
, obj
, js_Math_str
, OBJECT_TO_JSVAL(Math
),
797 JS_PropertyStub
, JS_PropertyStub
, 0)) {
801 if (!JS_DefineFunctions(cx
, Math
, math_static_methods
))
803 if (!JS_DefineConstDoubles(cx
, Math
, math_constants
))