2009-06-10 18:29:44 -07:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
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
|
|
|
|
|
|
|
#ifndef jsmath_h___
|
|
|
|
#define jsmath_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
|
|
|
|
|
|
|
size_t sizeOfIncludingThis(JSMallocSizeOfFun mallocSizeOf);
|
2010-10-06 12:13:20 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace js */
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* JS math functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern JSObject *
|
|
|
|
js_InitMathClass(JSContext *cx, JSObject *obj);
|
|
|
|
|
2008-07-30 15:51:44 -07:00
|
|
|
extern void
|
2010-03-18 08:27:26 -07:00
|
|
|
js_InitRandom(JSContext *cx);
|
2008-07-30 15:51:44 -07:00
|
|
|
|
2010-10-26 20:21:39 -07:00
|
|
|
extern JSBool
|
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
|
|
|
|
2009-08-19 15:31:10 -07:00
|
|
|
extern JSBool
|
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
|
|
|
|
|
|
|
extern JSBool
|
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
|
|
|
|
|
|
|
extern JSBool
|
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
|
|
|
|
|
|
|
extern JSBool
|
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
|
|
|
|
|
|
|
extern JSBool
|
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
|
|
|
|
2011-03-21 22:55:27 -07:00
|
|
|
extern JSBool
|
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
|
|
|
|
|
|
|
extern JSBool
|
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 {
|
|
|
|
|
|
|
|
extern JSBool
|
|
|
|
math_log(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
|
|
|
extern double
|
|
|
|
math_log_impl(MathCache *cache, double x);
|
|
|
|
|
|
|
|
extern JSBool
|
|
|
|
math_sin(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
|
|
|
extern double
|
|
|
|
math_sin_impl(MathCache *cache, double x);
|
|
|
|
|
|
|
|
extern JSBool
|
|
|
|
math_cos(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
|
|
|
extern double
|
|
|
|
math_cos_impl(MathCache *cache, double x);
|
|
|
|
|
|
|
|
extern JSBool
|
|
|
|
math_tan(JSContext *cx, unsigned argc, js::Value *vp);
|
|
|
|
|
|
|
|
extern double
|
|
|
|
math_tan_impl(MathCache *cache, double x);
|
|
|
|
|
2012-07-31 20:04:42 -07:00
|
|
|
extern double
|
|
|
|
powi(double x, int y);
|
|
|
|
|
|
|
|
extern double
|
|
|
|
ecmaPow(double x, double y);
|
|
|
|
|
2012-06-15 04:27:04 -07:00
|
|
|
} /* namespace js */
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif /* jsmath_h___ */
|