2013-04-16 13:47:10 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-19 17:59:46 -07:00
|
|
|
#ifndef jsmath_h
|
|
|
|
#define jsmath_h
|
2010-10-06 12:13:20 -07:00
|
|
|
|
2013-06-23 04:21:01 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
|
|
|
|
2013-03-15 02:29:02 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
|
2010-10-06 12:13:20 -07:00
|
|
|
namespace js {
|
|
|
|
|
|
|
|
typedef double (*UnaryFunType)(double);
|
|
|
|
|
|
|
|
class MathCache
|
|
|
|
{
|
|
|
|
static const unsigned SizeLog2 = 12;
|
|
|
|
static const unsigned Size = 1 << SizeLog2;
|
|
|
|
struct Entry { double in; UnaryFunType f; double out; };
|
|
|
|
Entry table[Size];
|
|
|
|
|
|
|
|
public:
|
|
|
|
MathCache();
|
|
|
|
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned hash(double x) {
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
union { double d; struct { uint32_t one, two; } s; } u = { x };
|
|
|
|
uint32_t hash32 = u.s.one ^ u.s.two;
|
|
|
|
uint16_t hash16 = uint16_t(hash32 ^ (hash32 >> 16));
|
2010-10-06 12:13:20 -07:00
|
|
|
return (hash16 & (Size - 1)) ^ (hash16 >> (16 - SizeLog2));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* N.B. lookup uses double-equality. This is only safe if hash() maps +0
|
|
|
|
* and -0 to different table entries, which is asserted in MathCache().
|
|
|
|
*/
|
|
|
|
double lookup(UnaryFunType f, double x) {
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned index = hash(x);
|
2010-10-06 12:13:20 -07:00
|
|
|
Entry &e = table[index];
|
|
|
|
if (e.in == x && e.f == f)
|
|
|
|
return e.out;
|
|
|
|
e.in = x;
|
|
|
|
e.f = f;
|
|
|
|
return (e.out = f(x));
|
|
|
|
}
|
2012-05-15 19:30:28 -07:00
|
|
|
|
2013-06-23 04:21:01 -07:00
|
|
|
size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf);
|
2010-10-06 12:13:20 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace js */
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* JS math functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern JSObject *
|
2012-09-20 22:17:49 -07:00
|
|
|
js_InitMathClass(JSContext *cx, js::HandleObject obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-10-05 12:05:21 -07:00
|
|
|
extern double
|
|
|
|
math_random_no_outparam(JSContext *cx);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2012-10-05 12:05:21 -07:00
|
|
|
js_math_random(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2012-02-28 15:11:11 -08:00
|
|
|
js_math_abs(JSContext *cx, unsigned argc, js::Value *vp);
|
2010-10-26 20:21:39 -07:00
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2012-02-28 15:11:11 -08:00
|
|
|
js_math_ceil(JSContext *cx, unsigned argc, js::Value *vp);
|
2009-08-19 15:31:10 -07:00
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2012-02-28 15:11:11 -08:00
|
|
|
js_math_floor(JSContext *cx, unsigned argc, js::Value *vp);
|
2009-08-19 15:31:10 -07:00
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2012-02-28 15:11:11 -08:00
|
|
|
js_math_max(JSContext *cx, unsigned argc, js::Value *vp);
|
2009-08-19 15:31:10 -07:00
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2012-02-28 15:11:11 -08:00
|
|
|
js_math_min(JSContext *cx, unsigned argc, js::Value *vp);
|
2009-08-19 15:31:10 -07:00
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2012-02-28 15:11:11 -08:00
|
|
|
js_math_round(JSContext *cx, unsigned argc, js::Value *vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2012-02-28 15:11:11 -08:00
|
|
|
js_math_sqrt(JSContext *cx, unsigned argc, js::Value *vp);
|
2011-03-21 22:55:27 -07:00
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2012-02-28 15:11:11 -08:00
|
|
|
js_math_pow(JSContext *cx, unsigned argc, js::Value *vp);
|
2011-03-21 22:55:27 -07:00
|
|
|
|
2012-02-24 14:19:52 -08:00
|
|
|
extern double
|
|
|
|
js_math_ceil_impl(double x);
|
2010-10-18 15:10:52 -07:00
|
|
|
|
2012-02-24 14:19:52 -08:00
|
|
|
extern double
|
|
|
|
js_math_floor_impl(double x);
|
2010-10-18 15:10:52 -07:00
|
|
|
|
2012-06-15 04:27:04 -07:00
|
|
|
namespace js {
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2012-12-14 10:28:14 -08:00
|
|
|
math_imul(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2012-06-15 04:27:04 -07:00
|
|
|
math_log(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
|
|
|
extern double
|
|
|
|
math_log_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_log_uncached(double x);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2012-06-15 04:27:04 -07:00
|
|
|
math_sin(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
|
|
|
extern double
|
|
|
|
math_sin_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_sin_uncached(double x);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2012-06-15 04:27:04 -07:00
|
|
|
math_cos(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
|
|
|
extern double
|
|
|
|
math_cos_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_cos_uncached(double x);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-03-03 15:56:58 -08:00
|
|
|
math_exp(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
|
|
|
extern double
|
|
|
|
math_exp_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_exp_uncached(double x);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2012-06-15 04:27:04 -07:00
|
|
|
math_tan(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
|
|
|
extern double
|
|
|
|
math_tan_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_tan_uncached(double x);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-07-15 08:03:14 -07:00
|
|
|
math_log10(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-07-15 08:03:14 -07:00
|
|
|
math_log2(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-07-15 08:03:14 -07:00
|
|
|
math_log1p(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-07-15 08:03:14 -07:00
|
|
|
math_expm1(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-07-15 08:03:14 -07:00
|
|
|
math_cosh(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-07-15 08:03:14 -07:00
|
|
|
math_sinh(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-07-15 08:03:14 -07:00
|
|
|
math_tanh(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-07-15 08:03:14 -07:00
|
|
|
math_acosh(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-07-15 08:03:14 -07:00
|
|
|
math_asinh(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-07-15 08:03:14 -07:00
|
|
|
math_atanh(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
2013-08-21 13:08:51 -07:00
|
|
|
// Math.hypot is disabled pending the resolution of spec issues (bug 896264).
|
|
|
|
#if 0
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-07-15 08:03:14 -07:00
|
|
|
math_hypot(JSContext *cx, unsigned argc, Value *vp);
|
2013-08-21 13:08:51 -07:00
|
|
|
#endif
|
2013-07-15 08:03:14 -07:00
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-07-15 08:03:14 -07:00
|
|
|
math_trunc(JSContext *cx, unsigned argc, Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-07-15 08:03:14 -07:00
|
|
|
math_sign(JSContext *cx, unsigned argc, Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-07-15 08:03:14 -07:00
|
|
|
math_cbrt(JSContext *cx, unsigned argc, Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-03-15 02:29:02 -07:00
|
|
|
math_asin(JSContext *cx, unsigned argc, Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-03-15 02:29:02 -07:00
|
|
|
math_acos(JSContext *cx, unsigned argc, Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-03-15 02:29:02 -07:00
|
|
|
math_atan(JSContext *cx, unsigned argc, Value *vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-03-15 02:29:02 -07:00
|
|
|
math_atan2(JSContext *cx, unsigned argc, Value *vp);
|
|
|
|
|
|
|
|
extern double
|
|
|
|
ecmaAtan2(double x, double y);
|
|
|
|
|
2013-03-03 15:56:58 -08:00
|
|
|
extern double
|
|
|
|
math_atan_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_atan_uncached(double x);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-03-03 15:56:58 -08:00
|
|
|
math_atan(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
|
|
|
extern double
|
|
|
|
math_asin_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_asin_uncached(double x);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-03-03 15:56:58 -08:00
|
|
|
math_asin(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
|
|
|
extern double
|
|
|
|
math_acos_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_acos_uncached(double x);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-03-03 15:56:58 -08:00
|
|
|
math_acos(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
2012-07-31 20:04:42 -07:00
|
|
|
extern double
|
|
|
|
powi(double x, int y);
|
|
|
|
|
|
|
|
extern double
|
|
|
|
ecmaPow(double x, double y);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
extern bool
|
2013-03-15 02:29:02 -07:00
|
|
|
math_imul(JSContext *cx, unsigned argc, Value *vp);
|
|
|
|
|
2013-07-15 08:03:14 -07:00
|
|
|
extern double
|
|
|
|
math_log10_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_log10_uncached(double x);
|
|
|
|
|
2013-07-15 08:03:14 -07:00
|
|
|
extern double
|
|
|
|
math_log2_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_log2_uncached(double x);
|
|
|
|
|
2013-07-15 08:03:14 -07:00
|
|
|
extern double
|
|
|
|
math_log1p_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_log1p_uncached(double x);
|
|
|
|
|
2013-07-15 08:03:14 -07:00
|
|
|
extern double
|
|
|
|
math_expm1_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_expm1_uncached(double x);
|
|
|
|
|
2013-07-15 08:03:14 -07:00
|
|
|
extern double
|
|
|
|
math_cosh_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_cosh_uncached(double x);
|
|
|
|
|
2013-07-15 08:03:14 -07:00
|
|
|
extern double
|
|
|
|
math_sinh_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_sinh_uncached(double x);
|
|
|
|
|
2013-07-15 08:03:14 -07:00
|
|
|
extern double
|
|
|
|
math_tanh_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_tanh_uncached(double x);
|
|
|
|
|
2013-07-15 08:03:14 -07:00
|
|
|
extern double
|
|
|
|
math_acosh_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_acosh_uncached(double x);
|
|
|
|
|
2013-07-15 08:03:14 -07:00
|
|
|
extern double
|
|
|
|
math_asinh_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_asinh_uncached(double x);
|
|
|
|
|
2013-07-15 08:03:14 -07:00
|
|
|
extern double
|
|
|
|
math_atanh_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_atanh_uncached(double x);
|
|
|
|
|
2013-08-21 13:08:51 -07:00
|
|
|
// Math.hypot is disabled pending the resolution of spec issues (bug 896264).
|
|
|
|
#if 0
|
2013-07-15 08:03:14 -07:00
|
|
|
extern double
|
|
|
|
math_hypot_impl(double x, double y);
|
2013-08-22 07:33:35 -07:00
|
|
|
|
|
|
|
extern double
|
|
|
|
math_hypot_uncached(double x, double y);
|
2013-08-21 13:08:51 -07:00
|
|
|
#endif
|
2013-07-15 08:03:14 -07:00
|
|
|
|
|
|
|
extern double
|
|
|
|
math_trunc_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_trunc_uncached(double x);
|
|
|
|
|
2013-07-15 08:03:14 -07:00
|
|
|
extern double
|
|
|
|
math_sign_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_sign_uncached(double x);
|
|
|
|
|
2013-07-15 08:03:14 -07:00
|
|
|
extern double
|
|
|
|
math_cbrt_impl(MathCache *cache, double x);
|
|
|
|
|
2013-08-22 07:33:35 -07:00
|
|
|
extern double
|
|
|
|
math_cbrt_uncached(double x);
|
|
|
|
|
2012-06-15 04:27:04 -07:00
|
|
|
} /* namespace js */
|
|
|
|
|
2013-06-19 17:59:46 -07:00
|
|
|
#endif /* jsmath_h */
|