michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * JS math package. michael@0: */ michael@0: michael@0: #include "jsmath.h" michael@0: michael@0: #include "mozilla/Constants.h" michael@0: #include "mozilla/FloatingPoint.h" michael@0: #include "mozilla/MathAlgorithms.h" michael@0: #include "mozilla/MemoryReporting.h" michael@0: michael@0: #include // for std::max michael@0: #include michael@0: michael@0: #ifdef XP_UNIX michael@0: # include michael@0: #endif michael@0: michael@0: #include "jsapi.h" michael@0: #include "jsatom.h" michael@0: #include "jscntxt.h" michael@0: #include "jscompartment.h" michael@0: #include "jslibmath.h" michael@0: #include "jstypes.h" michael@0: #include "prmjtime.h" michael@0: michael@0: #include "jsobjinlines.h" michael@0: michael@0: using namespace js; michael@0: michael@0: using mozilla::Abs; michael@0: using mozilla::NumberEqualsInt32; michael@0: using mozilla::NumberIsInt32; michael@0: using mozilla::ExponentComponent; michael@0: using mozilla::FloatingPoint; michael@0: using mozilla::IsFinite; michael@0: using mozilla::IsInfinite; michael@0: using mozilla::IsNaN; michael@0: using mozilla::IsNegative; michael@0: using mozilla::IsNegativeZero; michael@0: using mozilla::PositiveInfinity; michael@0: using mozilla::NegativeInfinity; michael@0: using JS::ToNumber; michael@0: using JS::GenericNaN; michael@0: michael@0: static const JSConstDoubleSpec math_constants[] = { michael@0: {M_E, "E", 0, {0,0,0}}, michael@0: {M_LOG2E, "LOG2E", 0, {0,0,0}}, michael@0: {M_LOG10E, "LOG10E", 0, {0,0,0}}, michael@0: {M_LN2, "LN2", 0, {0,0,0}}, michael@0: {M_LN10, "LN10", 0, {0,0,0}}, michael@0: {M_PI, "PI", 0, {0,0,0}}, michael@0: {M_SQRT2, "SQRT2", 0, {0,0,0}}, michael@0: {M_SQRT1_2, "SQRT1_2", 0, {0,0,0}}, michael@0: {0,0,0,{0,0,0}} michael@0: }; michael@0: michael@0: MathCache::MathCache() { michael@0: memset(table, 0, sizeof(table)); michael@0: michael@0: /* See comments in lookup(). */ michael@0: JS_ASSERT(IsNegativeZero(-0.0)); michael@0: JS_ASSERT(!IsNegativeZero(+0.0)); michael@0: JS_ASSERT(hash(-0.0) != hash(+0.0)); michael@0: } michael@0: michael@0: size_t michael@0: MathCache::sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) michael@0: { michael@0: return mallocSizeOf(this); michael@0: } michael@0: michael@0: const Class js::MathClass = { michael@0: js_Math_str, michael@0: JSCLASS_HAS_CACHED_PROTO(JSProto_Math), michael@0: JS_PropertyStub, /* addProperty */ michael@0: JS_DeletePropertyStub, /* delProperty */ michael@0: JS_PropertyStub, /* getProperty */ michael@0: JS_StrictPropertyStub, /* setProperty */ michael@0: JS_EnumerateStub, michael@0: JS_ResolveStub, michael@0: JS_ConvertStub michael@0: }; michael@0: michael@0: bool michael@0: js_math_abs(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: if (args.length() == 0) { michael@0: args.rval().setNaN(); michael@0: return true; michael@0: } michael@0: michael@0: double x; michael@0: if (!ToNumber(cx, args[0], &x)) michael@0: return false; michael@0: michael@0: double z = Abs(x); michael@0: args.rval().setNumber(z); michael@0: return true; michael@0: } michael@0: michael@0: #if defined(SOLARIS) && defined(__GNUC__) michael@0: #define ACOS_IF_OUT_OF_RANGE(x) if (x < -1 || 1 < x) return GenericNaN(); michael@0: #else michael@0: #define ACOS_IF_OUT_OF_RANGE(x) michael@0: #endif michael@0: michael@0: double michael@0: js::math_acos_impl(MathCache *cache, double x) michael@0: { michael@0: ACOS_IF_OUT_OF_RANGE(x); michael@0: return cache->lookup(acos, x); michael@0: } michael@0: michael@0: double michael@0: js::math_acos_uncached(double x) michael@0: { michael@0: ACOS_IF_OUT_OF_RANGE(x); michael@0: return acos(x); michael@0: } michael@0: michael@0: #undef ACOS_IF_OUT_OF_RANGE michael@0: michael@0: bool michael@0: js::math_acos(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: if (args.length() == 0) { michael@0: args.rval().setNaN(); michael@0: return true; michael@0: } michael@0: michael@0: double x; michael@0: if (!ToNumber(cx, args[0], &x)) michael@0: return false; michael@0: michael@0: MathCache *mathCache = cx->runtime()->getMathCache(cx); michael@0: if (!mathCache) michael@0: return false; michael@0: michael@0: double z = math_acos_impl(mathCache, x); michael@0: args.rval().setDouble(z); michael@0: return true; michael@0: } michael@0: michael@0: #if defined(SOLARIS) && defined(__GNUC__) michael@0: #define ASIN_IF_OUT_OF_RANGE(x) if (x < -1 || 1 < x) return GenericNaN(); michael@0: #else michael@0: #define ASIN_IF_OUT_OF_RANGE(x) michael@0: #endif michael@0: michael@0: double michael@0: js::math_asin_impl(MathCache *cache, double x) michael@0: { michael@0: ASIN_IF_OUT_OF_RANGE(x); michael@0: return cache->lookup(asin, x); michael@0: } michael@0: michael@0: double michael@0: js::math_asin_uncached(double x) michael@0: { michael@0: ASIN_IF_OUT_OF_RANGE(x); michael@0: return asin(x); michael@0: } michael@0: michael@0: #undef ASIN_IF_OUT_OF_RANGE michael@0: michael@0: bool michael@0: js::math_asin(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: if (args.length() == 0) { michael@0: args.rval().setNaN(); michael@0: return true; michael@0: } michael@0: michael@0: double x; michael@0: if (!ToNumber(cx, args[0], &x)) michael@0: return false; michael@0: michael@0: MathCache *mathCache = cx->runtime()->getMathCache(cx); michael@0: if (!mathCache) michael@0: return false; michael@0: michael@0: double z = math_asin_impl(mathCache, x); michael@0: args.rval().setDouble(z); michael@0: return true; michael@0: } michael@0: michael@0: double michael@0: js::math_atan_impl(MathCache *cache, double x) michael@0: { michael@0: return cache->lookup(atan, x); michael@0: } michael@0: michael@0: double michael@0: js::math_atan_uncached(double x) michael@0: { michael@0: return atan(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_atan(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: if (args.length() == 0) { michael@0: args.rval().setNaN(); michael@0: return true; michael@0: } michael@0: michael@0: double x; michael@0: if (!ToNumber(cx, args[0], &x)) michael@0: return false; michael@0: michael@0: MathCache *mathCache = cx->runtime()->getMathCache(cx); michael@0: if (!mathCache) michael@0: return false; michael@0: michael@0: double z = math_atan_impl(mathCache, x); michael@0: args.rval().setDouble(z); michael@0: return true; michael@0: } michael@0: michael@0: double michael@0: js::ecmaAtan2(double y, double x) michael@0: { michael@0: #if defined(_MSC_VER) michael@0: /* michael@0: * MSVC's atan2 does not yield the result demanded by ECMA when both x michael@0: * and y are infinite. michael@0: * - The result is a multiple of pi/4. michael@0: * - The sign of y determines the sign of the result. michael@0: * - The sign of x determines the multiplicator, 1 or 3. michael@0: */ michael@0: if (IsInfinite(y) && IsInfinite(x)) { michael@0: double z = js_copysign(M_PI / 4, y); michael@0: if (x < 0) michael@0: z *= 3; michael@0: return z; michael@0: } michael@0: #endif michael@0: michael@0: #if defined(SOLARIS) && defined(__GNUC__) michael@0: if (y == 0) { michael@0: if (IsNegativeZero(x)) michael@0: return js_copysign(M_PI, y); michael@0: if (x == 0) michael@0: return y; michael@0: } michael@0: #endif michael@0: return atan2(y, x); michael@0: } michael@0: michael@0: bool michael@0: js::math_atan2(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: double y; michael@0: if (!ToNumber(cx, args.get(0), &y)) michael@0: return false; michael@0: michael@0: double x; michael@0: if (!ToNumber(cx, args.get(1), &x)) michael@0: return false; michael@0: michael@0: double z = ecmaAtan2(y, x); michael@0: args.rval().setDouble(z); michael@0: return true; michael@0: } michael@0: michael@0: double michael@0: js::math_ceil_impl(double x) michael@0: { michael@0: #ifdef __APPLE__ michael@0: if (x < 0 && x > -1.0) michael@0: return js_copysign(0, -1); michael@0: #endif michael@0: return ceil(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_ceil(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: if (args.length() == 0) { michael@0: args.rval().setNaN(); michael@0: return true; michael@0: } michael@0: michael@0: double x; michael@0: if (!ToNumber(cx, args[0], &x)) michael@0: return false; michael@0: michael@0: double z = math_ceil_impl(x); michael@0: args.rval().setNumber(z); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: js::math_clz32(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: if (args.length() == 0) { michael@0: args.rval().setInt32(32); michael@0: return true; michael@0: } michael@0: michael@0: uint32_t n; michael@0: if (!ToUint32(cx, args[0], &n)) michael@0: return false; michael@0: michael@0: if (n == 0) { michael@0: args.rval().setInt32(32); michael@0: return true; michael@0: } michael@0: michael@0: args.rval().setInt32(mozilla::CountLeadingZeroes32(n)); michael@0: return true; michael@0: } michael@0: michael@0: double michael@0: js::math_cos_impl(MathCache *cache, double x) michael@0: { michael@0: return cache->lookup(cos, x); michael@0: } michael@0: michael@0: double michael@0: js::math_cos_uncached(double x) michael@0: { michael@0: return cos(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_cos(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: if (args.length() == 0) { michael@0: args.rval().setNaN(); michael@0: return true; michael@0: } michael@0: michael@0: double x; michael@0: if (!ToNumber(cx, args[0], &x)) michael@0: return false; michael@0: michael@0: MathCache *mathCache = cx->runtime()->getMathCache(cx); michael@0: if (!mathCache) michael@0: return false; michael@0: michael@0: double z = math_cos_impl(mathCache, x); michael@0: args.rval().setDouble(z); michael@0: return true; michael@0: } michael@0: michael@0: #ifdef _WIN32 michael@0: #define EXP_IF_OUT_OF_RANGE(x) \ michael@0: if (!IsNaN(x)) { \ michael@0: if (x == PositiveInfinity()) \ michael@0: return PositiveInfinity(); \ michael@0: if (x == NegativeInfinity()) \ michael@0: return 0.0; \ michael@0: } michael@0: #else michael@0: #define EXP_IF_OUT_OF_RANGE(x) michael@0: #endif michael@0: michael@0: double michael@0: js::math_exp_impl(MathCache *cache, double x) michael@0: { michael@0: EXP_IF_OUT_OF_RANGE(x); michael@0: return cache->lookup(exp, x); michael@0: } michael@0: michael@0: double michael@0: js::math_exp_uncached(double x) michael@0: { michael@0: EXP_IF_OUT_OF_RANGE(x); michael@0: return exp(x); michael@0: } michael@0: michael@0: #undef EXP_IF_OUT_OF_RANGE michael@0: michael@0: bool michael@0: js::math_exp(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: if (args.length() == 0) { michael@0: args.rval().setNaN(); michael@0: return true; michael@0: } michael@0: michael@0: double x; michael@0: if (!ToNumber(cx, args[0], &x)) michael@0: return false; michael@0: michael@0: MathCache *mathCache = cx->runtime()->getMathCache(cx); michael@0: if (!mathCache) michael@0: return false; michael@0: michael@0: double z = math_exp_impl(mathCache, x); michael@0: args.rval().setNumber(z); michael@0: return true; michael@0: } michael@0: michael@0: double michael@0: js::math_floor_impl(double x) michael@0: { michael@0: return floor(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_floor(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: if (args.length() == 0) { michael@0: args.rval().setNaN(); michael@0: return true; michael@0: } michael@0: michael@0: double x; michael@0: if (!ToNumber(cx, args[0], &x)) michael@0: return false; michael@0: michael@0: double z = math_floor_impl(x); michael@0: args.rval().setNumber(z); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: js::math_imul(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: uint32_t a = 0, b = 0; michael@0: if (args.hasDefined(0) && !ToUint32(cx, args[0], &a)) michael@0: return false; michael@0: if (args.hasDefined(1) && !ToUint32(cx, args[1], &b)) michael@0: return false; michael@0: michael@0: uint32_t product = a * b; michael@0: args.rval().setInt32(product > INT32_MAX michael@0: ? int32_t(INT32_MIN + (product - INT32_MAX - 1)) michael@0: : int32_t(product)); michael@0: return true; michael@0: } michael@0: michael@0: // Implements Math.fround (20.2.2.16) up to step 3 michael@0: bool michael@0: js::RoundFloat32(JSContext *cx, Handle v, float *out) michael@0: { michael@0: double d; michael@0: bool success = ToNumber(cx, v, &d); michael@0: *out = static_cast(d); michael@0: return success; michael@0: } michael@0: michael@0: bool michael@0: js::math_fround(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: if (args.length() == 0) { michael@0: args.rval().setNaN(); michael@0: return true; michael@0: } michael@0: michael@0: float f; michael@0: if (!RoundFloat32(cx, args[0], &f)) michael@0: return false; michael@0: michael@0: args.rval().setDouble(static_cast(f)); michael@0: return true; michael@0: } michael@0: michael@0: #if defined(SOLARIS) && defined(__GNUC__) michael@0: #define LOG_IF_OUT_OF_RANGE(x) if (x < 0) return GenericNaN(); michael@0: #else michael@0: #define LOG_IF_OUT_OF_RANGE(x) michael@0: #endif michael@0: michael@0: double michael@0: js::math_log_impl(MathCache *cache, double x) michael@0: { michael@0: LOG_IF_OUT_OF_RANGE(x); michael@0: return cache->lookup(log, x); michael@0: } michael@0: michael@0: double michael@0: js::math_log_uncached(double x) michael@0: { michael@0: LOG_IF_OUT_OF_RANGE(x); michael@0: return log(x); michael@0: } michael@0: michael@0: #undef LOG_IF_OUT_OF_RANGE michael@0: michael@0: bool michael@0: js::math_log(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: if (args.length() == 0) { michael@0: args.rval().setNaN(); michael@0: return true; michael@0: } michael@0: michael@0: double x; michael@0: if (!ToNumber(cx, args[0], &x)) michael@0: return false; michael@0: michael@0: MathCache *mathCache = cx->runtime()->getMathCache(cx); michael@0: if (!mathCache) michael@0: return false; michael@0: michael@0: double z = math_log_impl(mathCache, x); michael@0: args.rval().setNumber(z); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: js_math_max(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: double maxval = NegativeInfinity(); michael@0: for (unsigned i = 0; i < args.length(); i++) { michael@0: double x; michael@0: if (!ToNumber(cx, args[i], &x)) michael@0: return false; michael@0: // Math.max(num, NaN) => NaN, Math.max(-0, +0) => +0 michael@0: if (x > maxval || IsNaN(x) || (x == maxval && IsNegative(maxval))) michael@0: maxval = x; michael@0: } michael@0: args.rval().setNumber(maxval); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: js_math_min(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: double minval = PositiveInfinity(); michael@0: for (unsigned i = 0; i < args.length(); i++) { michael@0: double x; michael@0: if (!ToNumber(cx, args[i], &x)) michael@0: return false; michael@0: // Math.min(num, NaN) => NaN, Math.min(-0, +0) => -0 michael@0: if (x < minval || IsNaN(x) || (x == minval && IsNegativeZero(x))) michael@0: minval = x; michael@0: } michael@0: args.rval().setNumber(minval); michael@0: return true; michael@0: } michael@0: michael@0: // Disable PGO for Math.pow() and related functions (see bug 791214). michael@0: #if defined(_MSC_VER) michael@0: # pragma optimize("g", off) michael@0: #endif michael@0: double michael@0: js::powi(double x, int y) michael@0: { michael@0: unsigned n = (y < 0) ? -y : y; michael@0: double m = x; michael@0: double p = 1; michael@0: while (true) { michael@0: if ((n & 1) != 0) p *= m; michael@0: n >>= 1; michael@0: if (n == 0) { michael@0: if (y < 0) { michael@0: // Unfortunately, we have to be careful when p has reached michael@0: // infinity in the computation, because sometimes the higher michael@0: // internal precision in the pow() implementation would have michael@0: // given us a finite p. This happens very rarely. michael@0: michael@0: double result = 1.0 / p; michael@0: return (result == 0 && IsInfinite(p)) michael@0: ? pow(x, static_cast(y)) // Avoid pow(double, int). michael@0: : result; michael@0: } michael@0: michael@0: return p; michael@0: } michael@0: m *= m; michael@0: } michael@0: } michael@0: #if defined(_MSC_VER) michael@0: # pragma optimize("", on) michael@0: #endif michael@0: michael@0: // Disable PGO for Math.pow() and related functions (see bug 791214). michael@0: #if defined(_MSC_VER) michael@0: # pragma optimize("g", off) michael@0: #endif michael@0: double michael@0: js::ecmaPow(double x, double y) michael@0: { michael@0: /* michael@0: * Use powi if the exponent is an integer-valued double. We don't have to michael@0: * check for NaN since a comparison with NaN is always false. michael@0: */ michael@0: int32_t yi; michael@0: if (NumberEqualsInt32(y, &yi)) michael@0: return powi(x, yi); michael@0: michael@0: /* michael@0: * Because C99 and ECMA specify different behavior for pow(), michael@0: * we need to wrap the libm call to make it ECMA compliant. michael@0: */ michael@0: if (!IsFinite(y) && (x == 1.0 || x == -1.0)) michael@0: return GenericNaN(); michael@0: michael@0: /* pow(x, +-0) is always 1, even for x = NaN (MSVC gets this wrong). */ michael@0: if (y == 0) michael@0: return 1; michael@0: michael@0: /* michael@0: * Special case for square roots. Note that pow(x, 0.5) != sqrt(x) michael@0: * when x = -0.0, so we have to guard for this. michael@0: */ michael@0: if (IsFinite(x) && x != 0.0) { michael@0: if (y == 0.5) michael@0: return sqrt(x); michael@0: if (y == -0.5) michael@0: return 1.0 / sqrt(x); michael@0: } michael@0: return pow(x, y); michael@0: } michael@0: #if defined(_MSC_VER) michael@0: # pragma optimize("", on) michael@0: #endif michael@0: michael@0: // Disable PGO for Math.pow() and related functions (see bug 791214). michael@0: #if defined(_MSC_VER) michael@0: # pragma optimize("g", off) michael@0: #endif michael@0: bool michael@0: js_math_pow(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: double x; michael@0: if (!ToNumber(cx, args.get(0), &x)) michael@0: return false; michael@0: michael@0: double y; michael@0: if (!ToNumber(cx, args.get(1), &y)) michael@0: return false; michael@0: michael@0: double z = ecmaPow(x, y); michael@0: args.rval().setNumber(z); michael@0: return true; michael@0: } michael@0: #if defined(_MSC_VER) michael@0: # pragma optimize("", on) michael@0: #endif michael@0: michael@0: static uint64_t michael@0: random_generateSeed() michael@0: { michael@0: union { michael@0: uint8_t u8[8]; michael@0: uint32_t u32[2]; michael@0: uint64_t u64; michael@0: } seed; michael@0: seed.u64 = 0; michael@0: michael@0: #if defined(XP_WIN) michael@0: /* michael@0: * Our PRNG only uses 48 bits, so calling rand_s() twice to get 64 bits is michael@0: * probably overkill. michael@0: */ michael@0: rand_s(&seed.u32[0]); michael@0: #elif defined(XP_UNIX) michael@0: /* michael@0: * In the unlikely event we can't read /dev/urandom, there's not much we can michael@0: * do, so just mix in the fd error code and the current time. michael@0: */ michael@0: int fd = open("/dev/urandom", O_RDONLY); michael@0: MOZ_ASSERT(fd >= 0, "Can't open /dev/urandom"); michael@0: if (fd >= 0) { michael@0: read(fd, seed.u8, mozilla::ArrayLength(seed.u8)); michael@0: close(fd); michael@0: } michael@0: seed.u32[0] ^= fd; michael@0: #else michael@0: # error "Platform needs to implement random_generateSeed()" michael@0: #endif michael@0: michael@0: seed.u32[1] ^= PRMJ_Now(); michael@0: return seed.u64; michael@0: } michael@0: michael@0: static const uint64_t RNG_MULTIPLIER = 0x5DEECE66DLL; michael@0: static const uint64_t RNG_ADDEND = 0xBLL; michael@0: static const uint64_t RNG_MASK = (1LL << 48) - 1; michael@0: static const double RNG_DSCALE = double(1LL << 53); michael@0: michael@0: /* michael@0: * Math.random() support, lifted from java.util.Random.java. michael@0: */ michael@0: static void michael@0: random_initState(uint64_t *rngState) michael@0: { michael@0: /* Our PRNG only uses 48 bits, so squeeze our entropy into those bits. */ michael@0: uint64_t seed = random_generateSeed(); michael@0: seed ^= (seed >> 16); michael@0: *rngState = (seed ^ RNG_MULTIPLIER) & RNG_MASK; michael@0: } michael@0: michael@0: uint64_t michael@0: random_next(uint64_t *rngState, int bits) michael@0: { michael@0: MOZ_ASSERT((*rngState & 0xffff000000000000ULL) == 0, "Bad rngState"); michael@0: MOZ_ASSERT(bits > 0 && bits <= 48, "bits is out of range"); michael@0: michael@0: if (*rngState == 0) { michael@0: random_initState(rngState); michael@0: } michael@0: michael@0: uint64_t nextstate = *rngState * RNG_MULTIPLIER; michael@0: nextstate += RNG_ADDEND; michael@0: nextstate &= RNG_MASK; michael@0: *rngState = nextstate; michael@0: return nextstate >> (48 - bits); michael@0: } michael@0: michael@0: static inline double michael@0: random_nextDouble(JSContext *cx) michael@0: { michael@0: uint64_t *rng = &cx->compartment()->rngState; michael@0: return double((random_next(rng, 26) << 27) + random_next(rng, 27)) / RNG_DSCALE; michael@0: } michael@0: michael@0: double michael@0: math_random_no_outparam(JSContext *cx) michael@0: { michael@0: /* Calculate random without memory traffic, for use in the JITs. */ michael@0: return random_nextDouble(cx); michael@0: } michael@0: michael@0: bool michael@0: js_math_random(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: double z = random_nextDouble(cx); michael@0: args.rval().setDouble(z); michael@0: return true; michael@0: } michael@0: michael@0: double michael@0: js::math_round_impl(double x) michael@0: { michael@0: int32_t ignored; michael@0: if (NumberIsInt32(x, &ignored)) michael@0: return x; michael@0: michael@0: /* Some numbers are so big that adding 0.5 would give the wrong number. */ michael@0: if (ExponentComponent(x) >= int_fast16_t(FloatingPoint::ExponentShift)) michael@0: return x; michael@0: michael@0: return js_copysign(floor(x + 0.5), x); michael@0: } michael@0: michael@0: float michael@0: js::math_roundf_impl(float x) michael@0: { michael@0: int32_t ignored; michael@0: if (NumberIsInt32(x, &ignored)) michael@0: return x; michael@0: michael@0: /* Some numbers are so big that adding 0.5 would give the wrong number. */ michael@0: if (ExponentComponent(x) >= int_fast16_t(FloatingPoint::ExponentShift)) michael@0: return x; michael@0: michael@0: return js_copysign(floorf(x + 0.5f), x); michael@0: } michael@0: michael@0: bool /* ES5 15.8.2.15. */ michael@0: js::math_round(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: if (args.length() == 0) { michael@0: args.rval().setNaN(); michael@0: return true; michael@0: } michael@0: michael@0: double x; michael@0: if (!ToNumber(cx, args[0], &x)) michael@0: return false; michael@0: michael@0: double z = math_round_impl(x); michael@0: args.rval().setNumber(z); michael@0: return true; michael@0: } michael@0: michael@0: double michael@0: js::math_sin_impl(MathCache *cache, double x) michael@0: { michael@0: return cache->lookup(sin, x); michael@0: } michael@0: michael@0: double michael@0: js::math_sin_uncached(double x) michael@0: { michael@0: return sin(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_sin(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: if (args.length() == 0) { michael@0: args.rval().setNaN(); michael@0: return true; michael@0: } michael@0: michael@0: double x; michael@0: if (!ToNumber(cx, args[0], &x)) michael@0: return false; michael@0: michael@0: MathCache *mathCache = cx->runtime()->getMathCache(cx); michael@0: if (!mathCache) michael@0: return false; michael@0: michael@0: double z = math_sin_impl(mathCache, x); michael@0: args.rval().setDouble(z); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: js_math_sqrt(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: if (args.length() == 0) { michael@0: args.rval().setNaN(); michael@0: return true; michael@0: } michael@0: michael@0: double x; michael@0: if (!ToNumber(cx, args[0], &x)) michael@0: return false; michael@0: michael@0: MathCache *mathCache = cx->runtime()->getMathCache(cx); michael@0: if (!mathCache) michael@0: return false; michael@0: michael@0: double z = mathCache->lookup(sqrt, x); michael@0: args.rval().setDouble(z); michael@0: return true; michael@0: } michael@0: michael@0: double michael@0: js::math_tan_impl(MathCache *cache, double x) michael@0: { michael@0: return cache->lookup(tan, x); michael@0: } michael@0: michael@0: double michael@0: js::math_tan_uncached(double x) michael@0: { michael@0: return tan(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_tan(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: if (args.length() == 0) { michael@0: args.rval().setNaN(); michael@0: return true; michael@0: } michael@0: michael@0: double x; michael@0: if (!ToNumber(cx, args[0], &x)) michael@0: return false; michael@0: michael@0: MathCache *mathCache = cx->runtime()->getMathCache(cx); michael@0: if (!mathCache) michael@0: return false; michael@0: michael@0: double z = math_tan_impl(mathCache, x); michael@0: args.rval().setDouble(z); michael@0: return true; michael@0: } michael@0: michael@0: michael@0: typedef double (*UnaryMathFunctionType)(MathCache *cache, double); michael@0: michael@0: template michael@0: static bool math_function(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: if (args.length() == 0) { michael@0: args.rval().setNumber(GenericNaN()); michael@0: return true; michael@0: } michael@0: michael@0: double x; michael@0: if (!ToNumber(cx, args[0], &x)) michael@0: return false; michael@0: michael@0: MathCache *mathCache = cx->runtime()->getMathCache(cx); michael@0: if (!mathCache) michael@0: return false; michael@0: double z = F(mathCache, x); michael@0: args.rval().setNumber(z); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: michael@0: double michael@0: js::math_log10_impl(MathCache *cache, double x) michael@0: { michael@0: return cache->lookup(log10, x); michael@0: } michael@0: michael@0: double michael@0: js::math_log10_uncached(double x) michael@0: { michael@0: return log10(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_log10(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: return math_function(cx, argc, vp); michael@0: } michael@0: michael@0: #if !HAVE_LOG2 michael@0: double log2(double x) michael@0: { michael@0: return log(x) / M_LN2; michael@0: } michael@0: #endif michael@0: michael@0: double michael@0: js::math_log2_impl(MathCache *cache, double x) michael@0: { michael@0: return cache->lookup(log2, x); michael@0: } michael@0: michael@0: double michael@0: js::math_log2_uncached(double x) michael@0: { michael@0: return log2(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_log2(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: return math_function(cx, argc, vp); michael@0: } michael@0: michael@0: #if !HAVE_LOG1P michael@0: double log1p(double x) michael@0: { michael@0: if (fabs(x) < 1e-4) { michael@0: /* michael@0: * Use Taylor approx. log(1 + x) = x - x^2 / 2 + x^3 / 3 - x^4 / 4 with error x^5 / 5 michael@0: * Since |x| < 10^-4, |x|^5 < 10^-20, relative error less than 10^-16 michael@0: */ michael@0: double z = -(x * x * x * x) / 4 + (x * x * x) / 3 - (x * x) / 2 + x; michael@0: return z; michael@0: } else { michael@0: /* For other large enough values of x use direct computation */ michael@0: return log(1.0 + x); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef __APPLE__ michael@0: // Ensure that log1p(-0) is -0. michael@0: #define LOG1P_IF_OUT_OF_RANGE(x) if (x == 0) return x; michael@0: #else michael@0: #define LOG1P_IF_OUT_OF_RANGE(x) michael@0: #endif michael@0: michael@0: double michael@0: js::math_log1p_impl(MathCache *cache, double x) michael@0: { michael@0: LOG1P_IF_OUT_OF_RANGE(x); michael@0: return cache->lookup(log1p, x); michael@0: } michael@0: michael@0: double michael@0: js::math_log1p_uncached(double x) michael@0: { michael@0: LOG1P_IF_OUT_OF_RANGE(x); michael@0: return log1p(x); michael@0: } michael@0: michael@0: #undef LOG1P_IF_OUT_OF_RANGE michael@0: michael@0: bool michael@0: js::math_log1p(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: return math_function(cx, argc, vp); michael@0: } michael@0: michael@0: #if !HAVE_EXPM1 michael@0: double expm1(double x) michael@0: { michael@0: /* Special handling for -0 */ michael@0: if (x == 0.0) michael@0: return x; michael@0: michael@0: if (fabs(x) < 1e-5) { michael@0: /* michael@0: * Use Taylor approx. exp(x) - 1 = x + x^2 / 2 + x^3 / 6 with error x^4 / 24 michael@0: * Since |x| < 10^-5, |x|^4 < 10^-20, relative error less than 10^-15 michael@0: */ michael@0: double z = (x * x * x) / 6 + (x * x) / 2 + x; michael@0: return z; michael@0: } else { michael@0: /* For other large enough values of x use direct computation */ michael@0: return exp(x) - 1.0; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: double michael@0: js::math_expm1_impl(MathCache *cache, double x) michael@0: { michael@0: return cache->lookup(expm1, x); michael@0: } michael@0: michael@0: double michael@0: js::math_expm1_uncached(double x) michael@0: { michael@0: return expm1(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_expm1(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: return math_function(cx, argc, vp); michael@0: } michael@0: michael@0: #if !HAVE_SQRT1PM1 michael@0: /* This algorithm computes sqrt(1+x)-1 for small x */ michael@0: double sqrt1pm1(double x) michael@0: { michael@0: if (fabs(x) > 0.75) michael@0: return sqrt(1 + x) - 1; michael@0: michael@0: return expm1(log1p(x) / 2); michael@0: } michael@0: #endif michael@0: michael@0: michael@0: double michael@0: js::math_cosh_impl(MathCache *cache, double x) michael@0: { michael@0: return cache->lookup(cosh, x); michael@0: } michael@0: michael@0: double michael@0: js::math_cosh_uncached(double x) michael@0: { michael@0: return cosh(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_cosh(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: return math_function(cx, argc, vp); michael@0: } michael@0: michael@0: double michael@0: js::math_sinh_impl(MathCache *cache, double x) michael@0: { michael@0: return cache->lookup(sinh, x); michael@0: } michael@0: michael@0: double michael@0: js::math_sinh_uncached(double x) michael@0: { michael@0: return sinh(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_sinh(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: return math_function(cx, argc, vp); michael@0: } michael@0: michael@0: double michael@0: js::math_tanh_impl(MathCache *cache, double x) michael@0: { michael@0: return cache->lookup(tanh, x); michael@0: } michael@0: michael@0: double michael@0: js::math_tanh_uncached(double x) michael@0: { michael@0: return tanh(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_tanh(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: return math_function(cx, argc, vp); michael@0: } michael@0: michael@0: #if !HAVE_ACOSH michael@0: double acosh(double x) michael@0: { michael@0: const double SQUARE_ROOT_EPSILON = sqrt(std::numeric_limits::epsilon()); michael@0: michael@0: if ((x - 1) >= SQUARE_ROOT_EPSILON) { michael@0: if (x > 1 / SQUARE_ROOT_EPSILON) { michael@0: /* michael@0: * http://functions.wolfram.com/ElementaryFunctions/ArcCosh/06/01/06/01/0001/ michael@0: * approximation by laurent series in 1/x at 0+ order from -1 to 0 michael@0: */ michael@0: return log(x) + M_LN2; michael@0: } else if (x < 1.5) { michael@0: // This is just a rearrangement of the standard form below michael@0: // devised to minimize loss of precision when x ~ 1: michael@0: double y = x - 1; michael@0: return log1p(y + sqrt(y * y + 2 * y)); michael@0: } else { michael@0: // http://functions.wolfram.com/ElementaryFunctions/ArcCosh/02/ michael@0: return log(x + sqrt(x * x - 1)); michael@0: } michael@0: } else { michael@0: // see http://functions.wolfram.com/ElementaryFunctions/ArcCosh/06/01/04/01/0001/ michael@0: double y = x - 1; michael@0: // approximation by taylor series in y at 0 up to order 2. michael@0: // If x is less than 1, sqrt(2 * y) is NaN and the result is NaN. michael@0: return sqrt(2 * y) * (1 - y / 12 + 3 * y * y / 160); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: double michael@0: js::math_acosh_impl(MathCache *cache, double x) michael@0: { michael@0: return cache->lookup(acosh, x); michael@0: } michael@0: michael@0: double michael@0: js::math_acosh_uncached(double x) michael@0: { michael@0: return acosh(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_acosh(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: return math_function(cx, argc, vp); michael@0: } michael@0: michael@0: #if !HAVE_ASINH michael@0: // Bug 899712 - gcc incorrectly rewrites -asinh(-x) to asinh(x) when overriding michael@0: // asinh. michael@0: static double my_asinh(double x) michael@0: { michael@0: const double SQUARE_ROOT_EPSILON = sqrt(std::numeric_limits::epsilon()); michael@0: const double FOURTH_ROOT_EPSILON = sqrt(SQUARE_ROOT_EPSILON); michael@0: michael@0: if (x >= FOURTH_ROOT_EPSILON) { michael@0: if (x > 1 / SQUARE_ROOT_EPSILON) michael@0: // http://functions.wolfram.com/ElementaryFunctions/ArcSinh/06/01/06/01/0001/ michael@0: // approximation by laurent series in 1/x at 0+ order from -1 to 1 michael@0: return M_LN2 + log(x) + 1 / (4 * x * x); michael@0: else if (x < 0.5) michael@0: return log1p(x + sqrt1pm1(x * x)); michael@0: else michael@0: return log(x + sqrt(x * x + 1)); michael@0: } else if (x <= -FOURTH_ROOT_EPSILON) { michael@0: return -my_asinh(-x); michael@0: } else { michael@0: // http://functions.wolfram.com/ElementaryFunctions/ArcSinh/06/01/03/01/0001/ michael@0: // approximation by taylor series in x at 0 up to order 2 michael@0: double result = x; michael@0: michael@0: if (fabs(x) >= SQUARE_ROOT_EPSILON) { michael@0: double x3 = x * x * x; michael@0: // approximation by taylor series in x at 0 up to order 4 michael@0: result -= x3 / 6; michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: double michael@0: js::math_asinh_impl(MathCache *cache, double x) michael@0: { michael@0: #ifdef HAVE_ASINH michael@0: return cache->lookup(asinh, x); michael@0: #else michael@0: return cache->lookup(my_asinh, x); michael@0: #endif michael@0: } michael@0: michael@0: double michael@0: js::math_asinh_uncached(double x) michael@0: { michael@0: #ifdef HAVE_ASINH michael@0: return asinh(x); michael@0: #else michael@0: return my_asinh(x); michael@0: #endif michael@0: } michael@0: michael@0: bool michael@0: js::math_asinh(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: return math_function(cx, argc, vp); michael@0: } michael@0: michael@0: #if !HAVE_ATANH michael@0: double atanh(double x) michael@0: { michael@0: const double EPSILON = std::numeric_limits::epsilon(); michael@0: const double SQUARE_ROOT_EPSILON = sqrt(EPSILON); michael@0: const double FOURTH_ROOT_EPSILON = sqrt(SQUARE_ROOT_EPSILON); michael@0: michael@0: if (fabs(x) >= FOURTH_ROOT_EPSILON) { michael@0: // http://functions.wolfram.com/ElementaryFunctions/ArcTanh/02/ michael@0: if (fabs(x) < 0.5) michael@0: return (log1p(x) - log1p(-x)) / 2; michael@0: michael@0: return log((1 + x) / (1 - x)) / 2; michael@0: } else { michael@0: // http://functions.wolfram.com/ElementaryFunctions/ArcTanh/06/01/03/01/ michael@0: // approximation by taylor series in x at 0 up to order 2 michael@0: double result = x; michael@0: michael@0: if (fabs(x) >= SQUARE_ROOT_EPSILON) { michael@0: double x3 = x * x * x; michael@0: result += x3 / 3; michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: double michael@0: js::math_atanh_impl(MathCache *cache, double x) michael@0: { michael@0: return cache->lookup(atanh, x); michael@0: } michael@0: michael@0: double michael@0: js::math_atanh_uncached(double x) michael@0: { michael@0: return atanh(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_atanh(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: return math_function(cx, argc, vp); michael@0: } michael@0: michael@0: /* Consistency wrapper for platform deviations in hypot() */ michael@0: double michael@0: js::ecmaHypot(double x, double y) michael@0: { michael@0: #ifdef XP_WIN michael@0: /* michael@0: * Workaround MS hypot bug, where hypot(Infinity, NaN or Math.MIN_VALUE) michael@0: * is NaN, not Infinity. michael@0: */ michael@0: if (mozilla::IsInfinite(x) || mozilla::IsInfinite(y)) { michael@0: return mozilla::PositiveInfinity(); michael@0: } michael@0: #endif michael@0: return hypot(x, y); michael@0: } michael@0: michael@0: bool michael@0: js::math_hypot(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: michael@0: // IonMonkey calls the system hypot function directly if two arguments are michael@0: // given. Do that here as well to get the same results. michael@0: if (args.length() == 2) { michael@0: double x, y; michael@0: if (!ToNumber(cx, args[0], &x)) michael@0: return false; michael@0: if (!ToNumber(cx, args[1], &y)) michael@0: return false; michael@0: michael@0: double result = ecmaHypot(x, y); michael@0: args.rval().setNumber(result); michael@0: return true; michael@0: } michael@0: michael@0: bool isInfinite = false; michael@0: bool isNaN = false; michael@0: michael@0: double scale = 0; michael@0: double sumsq = 1; michael@0: michael@0: for (unsigned i = 0; i < args.length(); i++) { michael@0: double x; michael@0: if (!ToNumber(cx, args[i], &x)) michael@0: return false; michael@0: michael@0: isInfinite |= mozilla::IsInfinite(x); michael@0: isNaN |= mozilla::IsNaN(x); michael@0: michael@0: double xabs = mozilla::Abs(x); michael@0: michael@0: if (scale < xabs) { michael@0: sumsq = 1 + sumsq * (scale / xabs) * (scale / xabs); michael@0: scale = xabs; michael@0: } else if (scale != 0) { michael@0: sumsq += (xabs / scale) * (xabs / scale); michael@0: } michael@0: } michael@0: michael@0: double result = isInfinite ? PositiveInfinity() : michael@0: isNaN ? GenericNaN() : michael@0: scale * sqrt(sumsq); michael@0: args.rval().setNumber(result); michael@0: return true; michael@0: } michael@0: michael@0: #if !HAVE_TRUNC michael@0: double trunc(double x) michael@0: { michael@0: return x > 0 ? floor(x) : ceil(x); michael@0: } michael@0: #endif michael@0: michael@0: double michael@0: js::math_trunc_impl(MathCache *cache, double x) michael@0: { michael@0: return cache->lookup(trunc, x); michael@0: } michael@0: michael@0: double michael@0: js::math_trunc_uncached(double x) michael@0: { michael@0: return trunc(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_trunc(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: return math_function(cx, argc, vp); michael@0: } michael@0: michael@0: static double sign(double x) michael@0: { michael@0: if (mozilla::IsNaN(x)) michael@0: return GenericNaN(); michael@0: michael@0: return x == 0 ? x : x < 0 ? -1 : 1; michael@0: } michael@0: michael@0: double michael@0: js::math_sign_impl(MathCache *cache, double x) michael@0: { michael@0: return cache->lookup(sign, x); michael@0: } michael@0: michael@0: double michael@0: js::math_sign_uncached(double x) michael@0: { michael@0: return sign(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_sign(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: return math_function(cx, argc, vp); michael@0: } michael@0: michael@0: #if !HAVE_CBRT michael@0: double cbrt(double x) michael@0: { michael@0: if (x > 0) { michael@0: return pow(x, 1.0 / 3.0); michael@0: } else if (x == 0) { michael@0: return x; michael@0: } else { michael@0: return -pow(-x, 1.0 / 3.0); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: double michael@0: js::math_cbrt_impl(MathCache *cache, double x) michael@0: { michael@0: return cache->lookup(cbrt, x); michael@0: } michael@0: michael@0: double michael@0: js::math_cbrt_uncached(double x) michael@0: { michael@0: return cbrt(x); michael@0: } michael@0: michael@0: bool michael@0: js::math_cbrt(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: return math_function(cx, argc, vp); michael@0: } michael@0: michael@0: #if JS_HAS_TOSOURCE michael@0: static bool michael@0: math_toSource(JSContext *cx, unsigned argc, Value *vp) michael@0: { michael@0: CallArgs args = CallArgsFromVp(argc, vp); michael@0: args.rval().setString(cx->names().Math); michael@0: return true; michael@0: } michael@0: #endif michael@0: michael@0: static const JSFunctionSpec math_static_methods[] = { michael@0: #if JS_HAS_TOSOURCE michael@0: JS_FN(js_toSource_str, math_toSource, 0, 0), michael@0: #endif michael@0: JS_FN("abs", js_math_abs, 1, 0), michael@0: JS_FN("acos", math_acos, 1, 0), michael@0: JS_FN("asin", math_asin, 1, 0), michael@0: JS_FN("atan", math_atan, 1, 0), michael@0: JS_FN("atan2", math_atan2, 2, 0), michael@0: JS_FN("ceil", math_ceil, 1, 0), michael@0: JS_FN("clz32", math_clz32, 1, 0), michael@0: JS_FN("cos", math_cos, 1, 0), michael@0: JS_FN("exp", math_exp, 1, 0), michael@0: JS_FN("floor", math_floor, 1, 0), michael@0: JS_FN("imul", math_imul, 2, 0), michael@0: JS_FN("fround", math_fround, 1, 0), michael@0: JS_FN("log", math_log, 1, 0), michael@0: JS_FN("max", js_math_max, 2, 0), michael@0: JS_FN("min", js_math_min, 2, 0), michael@0: JS_FN("pow", js_math_pow, 2, 0), michael@0: JS_FN("random", js_math_random, 0, 0), michael@0: JS_FN("round", math_round, 1, 0), michael@0: JS_FN("sin", math_sin, 1, 0), michael@0: JS_FN("sqrt", js_math_sqrt, 1, 0), michael@0: JS_FN("tan", math_tan, 1, 0), michael@0: JS_FN("log10", math_log10, 1, 0), michael@0: JS_FN("log2", math_log2, 1, 0), michael@0: JS_FN("log1p", math_log1p, 1, 0), michael@0: JS_FN("expm1", math_expm1, 1, 0), michael@0: JS_FN("cosh", math_cosh, 1, 0), michael@0: JS_FN("sinh", math_sinh, 1, 0), michael@0: JS_FN("tanh", math_tanh, 1, 0), michael@0: JS_FN("acosh", math_acosh, 1, 0), michael@0: JS_FN("asinh", math_asinh, 1, 0), michael@0: JS_FN("atanh", math_atanh, 1, 0), michael@0: JS_FN("hypot", math_hypot, 2, 0), michael@0: JS_FN("trunc", math_trunc, 1, 0), michael@0: JS_FN("sign", math_sign, 1, 0), michael@0: JS_FN("cbrt", math_cbrt, 1, 0), michael@0: JS_FS_END michael@0: }; michael@0: michael@0: JSObject * michael@0: js_InitMathClass(JSContext *cx, HandleObject obj) michael@0: { michael@0: RootedObject proto(cx, obj->as().getOrCreateObjectPrototype(cx)); michael@0: if (!proto) michael@0: return nullptr; michael@0: RootedObject Math(cx, NewObjectWithGivenProto(cx, &MathClass, proto, obj, SingletonObject)); michael@0: if (!Math) michael@0: return nullptr; michael@0: michael@0: if (!JS_DefineProperty(cx, obj, js_Math_str, Math, 0, michael@0: JS_PropertyStub, JS_StrictPropertyStub)) michael@0: { michael@0: return nullptr; michael@0: } michael@0: michael@0: if (!JS_DefineFunctions(cx, Math, math_static_methods)) michael@0: return nullptr; michael@0: if (!JS_DefineConstDoubles(cx, Math, math_constants)) michael@0: return nullptr; michael@0: michael@0: obj->as().setConstructor(JSProto_Math, ObjectValue(*Math)); michael@0: michael@0: return Math; michael@0: }