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: #ifndef jsmath_h michael@0: #define jsmath_h michael@0: michael@0: #include "mozilla/MemoryReporting.h" michael@0: michael@0: #include "NamespaceImports.h" michael@0: michael@0: #ifndef M_PI michael@0: # define M_PI 3.14159265358979323846 michael@0: #endif michael@0: #ifndef M_E michael@0: # define M_E 2.7182818284590452354 michael@0: #endif michael@0: #ifndef M_LOG2E michael@0: # define M_LOG2E 1.4426950408889634074 michael@0: #endif michael@0: #ifndef M_LOG10E michael@0: # define M_LOG10E 0.43429448190325182765 michael@0: #endif michael@0: #ifndef M_LN2 michael@0: # define M_LN2 0.69314718055994530942 michael@0: #endif michael@0: #ifndef M_LN10 michael@0: # define M_LN10 2.30258509299404568402 michael@0: #endif michael@0: #ifndef M_SQRT2 michael@0: # define M_SQRT2 1.41421356237309504880 michael@0: #endif michael@0: #ifndef M_SQRT1_2 michael@0: # define M_SQRT1_2 0.70710678118654752440 michael@0: #endif michael@0: michael@0: namespace js { michael@0: michael@0: typedef double (*UnaryFunType)(double); michael@0: michael@0: class MathCache michael@0: { michael@0: static const unsigned SizeLog2 = 12; michael@0: static const unsigned Size = 1 << SizeLog2; michael@0: struct Entry { double in; UnaryFunType f; double out; }; michael@0: Entry table[Size]; michael@0: michael@0: public: michael@0: MathCache(); michael@0: michael@0: unsigned hash(double x) { michael@0: union { double d; struct { uint32_t one, two; } s; } u = { x }; michael@0: uint32_t hash32 = u.s.one ^ u.s.two; michael@0: uint16_t hash16 = uint16_t(hash32 ^ (hash32 >> 16)); michael@0: return (hash16 & (Size - 1)) ^ (hash16 >> (16 - SizeLog2)); michael@0: } michael@0: michael@0: /* michael@0: * N.B. lookup uses double-equality. This is only safe if hash() maps +0 michael@0: * and -0 to different table entries, which is asserted in MathCache(). michael@0: */ michael@0: double lookup(UnaryFunType f, double x) { michael@0: unsigned index = hash(x); michael@0: Entry &e = table[index]; michael@0: if (e.in == x && e.f == f) michael@0: return e.out; michael@0: e.in = x; michael@0: e.f = f; michael@0: return e.out = f(x); michael@0: } michael@0: michael@0: size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf); michael@0: }; michael@0: michael@0: } /* namespace js */ michael@0: michael@0: /* michael@0: * JS math functions. michael@0: */ michael@0: michael@0: extern JSObject * michael@0: js_InitMathClass(JSContext *cx, js::HandleObject obj); michael@0: michael@0: extern double michael@0: math_random_no_outparam(JSContext *cx); michael@0: michael@0: extern bool michael@0: js_math_random(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: js_math_abs(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: js_math_max(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: js_math_min(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: js_math_sqrt(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: js_math_pow(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: namespace js { michael@0: michael@0: extern bool michael@0: math_imul(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: RoundFloat32(JSContext *cx, Handle v, float *out); michael@0: michael@0: extern bool michael@0: math_fround(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: math_log(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern double michael@0: math_log_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_log_uncached(double x); michael@0: michael@0: extern bool michael@0: math_sin(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern double michael@0: math_sin_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_sin_uncached(double x); michael@0: michael@0: extern bool michael@0: math_cos(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern double michael@0: math_cos_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_cos_uncached(double x); michael@0: michael@0: extern bool michael@0: math_exp(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern double michael@0: math_exp_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_exp_uncached(double x); michael@0: michael@0: extern bool michael@0: math_tan(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern double michael@0: math_tan_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_tan_uncached(double x); michael@0: michael@0: extern bool michael@0: math_log10(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: math_log2(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: math_log1p(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: math_expm1(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: math_cosh(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: math_sinh(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: math_tanh(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: math_acosh(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: math_asinh(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: math_atanh(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern double michael@0: ecmaHypot(double x, double y); michael@0: michael@0: extern bool michael@0: math_hypot(JSContext *cx, unsigned argc, Value *vp); michael@0: michael@0: extern bool michael@0: math_trunc(JSContext *cx, unsigned argc, Value *vp); michael@0: michael@0: extern bool michael@0: math_sign(JSContext *cx, unsigned argc, Value *vp); michael@0: michael@0: extern bool michael@0: math_cbrt(JSContext *cx, unsigned argc, Value *vp); michael@0: michael@0: extern bool michael@0: math_asin(JSContext *cx, unsigned argc, Value *vp); michael@0: michael@0: extern bool michael@0: math_acos(JSContext *cx, unsigned argc, Value *vp); michael@0: michael@0: extern bool michael@0: math_atan(JSContext *cx, unsigned argc, Value *vp); michael@0: michael@0: extern bool michael@0: math_atan2(JSContext *cx, unsigned argc, Value *vp); michael@0: michael@0: extern double michael@0: ecmaAtan2(double x, double y); michael@0: michael@0: extern double michael@0: math_atan_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_atan_uncached(double x); michael@0: michael@0: extern bool michael@0: math_atan(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern double michael@0: math_asin_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_asin_uncached(double x); michael@0: michael@0: extern bool michael@0: math_asin(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern double michael@0: math_acos_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_acos_uncached(double x); michael@0: michael@0: extern bool michael@0: math_acos(JSContext *cx, unsigned argc, js::Value *vp); michael@0: michael@0: extern bool michael@0: math_ceil(JSContext *cx, unsigned argc, Value *vp); michael@0: michael@0: extern double michael@0: math_ceil_impl(double x); michael@0: michael@0: extern bool michael@0: math_clz32(JSContext *cx, unsigned argc, Value *vp); michael@0: michael@0: extern bool michael@0: math_floor(JSContext *cx, unsigned argc, Value *vp); michael@0: michael@0: extern double michael@0: math_floor_impl(double x); michael@0: michael@0: extern bool michael@0: math_round(JSContext *cx, unsigned argc, Value *vp); michael@0: michael@0: extern double michael@0: math_round_impl(double x); michael@0: michael@0: extern float michael@0: math_roundf_impl(float x); michael@0: michael@0: extern double michael@0: powi(double x, int y); michael@0: michael@0: extern double michael@0: ecmaPow(double x, double y); michael@0: michael@0: extern bool michael@0: math_imul(JSContext *cx, unsigned argc, Value *vp); michael@0: michael@0: extern double michael@0: math_log10_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_log10_uncached(double x); michael@0: michael@0: extern double michael@0: math_log2_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_log2_uncached(double x); michael@0: michael@0: extern double michael@0: math_log1p_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_log1p_uncached(double x); michael@0: michael@0: extern double michael@0: math_expm1_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_expm1_uncached(double x); michael@0: michael@0: extern double michael@0: math_cosh_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_cosh_uncached(double x); michael@0: michael@0: extern double michael@0: math_sinh_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_sinh_uncached(double x); michael@0: michael@0: extern double michael@0: math_tanh_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_tanh_uncached(double x); michael@0: michael@0: extern double michael@0: math_acosh_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_acosh_uncached(double x); michael@0: michael@0: extern double michael@0: math_asinh_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_asinh_uncached(double x); michael@0: michael@0: extern double michael@0: math_atanh_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_atanh_uncached(double x); michael@0: michael@0: extern double michael@0: math_trunc_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_trunc_uncached(double x); michael@0: michael@0: extern double michael@0: math_sign_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_sign_uncached(double x); michael@0: michael@0: extern double michael@0: math_cbrt_impl(MathCache *cache, double x); michael@0: michael@0: extern double michael@0: math_cbrt_uncached(double x); michael@0: michael@0: } /* namespace js */ michael@0: michael@0: #endif /* jsmath_h */