Bumping manifests a=b2g-bump
[gecko.git] / js / src / jsmath.h
blob23cb24e8411e2a7891ff0edc9df86dfe4b52346a
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sts=4 et sw=4 tw=99:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef jsmath_h
8 #define jsmath_h
10 #include "mozilla/MemoryReporting.h"
12 #include "NamespaceImports.h"
14 #ifndef M_PI
15 # define M_PI 3.14159265358979323846
16 #endif
17 #ifndef M_E
18 # define M_E 2.7182818284590452354
19 #endif
20 #ifndef M_LOG2E
21 # define M_LOG2E 1.4426950408889634074
22 #endif
23 #ifndef M_LOG10E
24 # define M_LOG10E 0.43429448190325182765
25 #endif
26 #ifndef M_LN2
27 # define M_LN2 0.69314718055994530942
28 #endif
29 #ifndef M_LN10
30 # define M_LN10 2.30258509299404568402
31 #endif
32 #ifndef M_SQRT2
33 # define M_SQRT2 1.41421356237309504880
34 #endif
35 #ifndef M_SQRT1_2
36 # define M_SQRT1_2 0.70710678118654752440
37 #endif
39 namespace js {
41 typedef double (*UnaryFunType)(double);
43 class MathCache
45 public:
46 enum MathFuncId {
47 Zero,
48 Sin, Cos, Tan, Sinh, Cosh, Tanh, Asin, Acos, Atan, Asinh, Acosh, Atanh,
49 Sqrt, Log, Log10, Log2, Log1p, Exp, Expm1, Cbrt, Trunc, Sign
52 private:
53 static const unsigned SizeLog2 = 12;
54 static const unsigned Size = 1 << SizeLog2;
55 struct Entry { double in; MathFuncId id; double out; };
56 Entry table[Size];
58 public:
59 MathCache();
61 unsigned hash(double x, MathFuncId id) {
62 union { double d; struct { uint32_t one, two; } s; } u = { x };
63 uint32_t hash32 = u.s.one ^ u.s.two;
64 hash32 += uint32_t(id) << 8;
65 uint16_t hash16 = uint16_t(hash32 ^ (hash32 >> 16));
66 return (hash16 & (Size - 1)) ^ (hash16 >> (16 - SizeLog2));
70 * N.B. lookup uses double-equality. This is only safe if hash() maps +0
71 * and -0 to different table entries, which is asserted in MathCache().
73 double lookup(UnaryFunType f, double x, MathFuncId id) {
74 unsigned index = hash(x, id);
75 Entry& e = table[index];
76 if (e.in == x && e.id == id)
77 return e.out;
78 e.in = x;
79 e.id = id;
80 return e.out = f(x);
83 size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf);
86 } /* namespace js */
89 * JS math functions.
92 extern JSObject*
93 js_InitMathClass(JSContext* cx, js::HandleObject obj);
95 namespace js {
97 extern void
98 random_initState(uint64_t* rngState);
100 extern uint64_t
101 random_next(uint64_t* rngState, int bits);
103 static const double RNG_DSCALE = double(1LL << 53);
105 inline double
106 random_nextDouble(uint64_t* rng)
108 return double((random_next(rng, 26) << 27) + random_next(rng, 27)) / RNG_DSCALE;
111 extern double
112 math_random_no_outparam(JSContext* cx);
114 extern bool
115 math_random(JSContext* cx, unsigned argc, js::Value* vp);
117 extern bool
118 math_abs_handle(JSContext* cx, js::HandleValue v, js::MutableHandleValue r);
120 extern bool
121 math_abs(JSContext* cx, unsigned argc, js::Value* vp);
123 extern double
124 math_max_impl(double x, double y);
126 extern bool
127 math_max(JSContext* cx, unsigned argc, js::Value* vp);
129 extern double
130 math_min_impl(double x, double y);
132 extern bool
133 math_min(JSContext* cx, unsigned argc, js::Value* vp);
135 extern bool
136 math_sqrt(JSContext* cx, unsigned argc, js::Value* vp);
138 extern bool
139 math_pow_handle(JSContext* cx, js::HandleValue base, js::HandleValue power,
140 js::MutableHandleValue result);
142 extern bool
143 math_pow(JSContext* cx, unsigned argc, js::Value* vp);
145 extern bool
146 minmax_impl(JSContext* cx, bool max, js::HandleValue a, js::HandleValue b,
147 js::MutableHandleValue res);
149 extern bool
150 math_sqrt_handle(JSContext* cx, js::HandleValue number, js::MutableHandleValue result);
152 extern bool
153 math_imul(JSContext* cx, unsigned argc, js::Value* vp);
155 extern bool
156 RoundFloat32(JSContext* cx, HandleValue v, float* out);
158 extern bool
159 RoundFloat32(JSContext* cx, HandleValue arg, MutableHandleValue res);
161 extern bool
162 math_fround(JSContext* cx, unsigned argc, js::Value* vp);
164 extern bool
165 math_log(JSContext* cx, unsigned argc, js::Value* vp);
167 extern double
168 math_log_impl(MathCache* cache, double x);
170 extern double
171 math_log_uncached(double x);
173 extern bool
174 math_sin(JSContext* cx, unsigned argc, js::Value* vp);
176 extern double
177 math_sin_impl(MathCache* cache, double x);
179 extern double
180 math_sin_uncached(double x);
182 extern bool
183 math_sin_handle(JSContext* cx, HandleValue val, MutableHandleValue res);
185 extern bool
186 math_cos(JSContext* cx, unsigned argc, js::Value* vp);
188 extern double
189 math_cos_impl(MathCache* cache, double x);
191 extern double
192 math_cos_uncached(double x);
194 extern bool
195 math_exp(JSContext* cx, unsigned argc, js::Value* vp);
197 extern double
198 math_exp_impl(MathCache* cache, double x);
200 extern double
201 math_exp_uncached(double x);
203 extern bool
204 math_tan(JSContext* cx, unsigned argc, js::Value* vp);
206 extern double
207 math_tan_impl(MathCache* cache, double x);
209 extern double
210 math_tan_uncached(double x);
212 extern bool
213 math_log10(JSContext* cx, unsigned argc, js::Value* vp);
215 extern bool
216 math_log2(JSContext* cx, unsigned argc, js::Value* vp);
218 extern bool
219 math_log1p(JSContext* cx, unsigned argc, js::Value* vp);
221 extern bool
222 math_expm1(JSContext* cx, unsigned argc, js::Value* vp);
224 extern bool
225 math_cosh(JSContext* cx, unsigned argc, js::Value* vp);
227 extern bool
228 math_sinh(JSContext* cx, unsigned argc, js::Value* vp);
230 extern bool
231 math_tanh(JSContext* cx, unsigned argc, js::Value* vp);
233 extern bool
234 math_acosh(JSContext* cx, unsigned argc, js::Value* vp);
236 extern bool
237 math_asinh(JSContext* cx, unsigned argc, js::Value* vp);
239 extern bool
240 math_atanh(JSContext* cx, unsigned argc, js::Value* vp);
242 extern double
243 ecmaHypot(double x, double y);
245 extern bool
246 math_hypot(JSContext* cx, unsigned argc, Value* vp);
248 extern bool
249 math_hypot_handle(JSContext* cx, HandleValueArray args, MutableHandleValue res);
251 extern bool
252 math_trunc(JSContext* cx, unsigned argc, Value* vp);
254 extern bool
255 math_sign(JSContext* cx, unsigned argc, Value* vp);
257 extern bool
258 math_cbrt(JSContext* cx, unsigned argc, Value* vp);
260 extern bool
261 math_asin(JSContext* cx, unsigned argc, Value* vp);
263 extern bool
264 math_acos(JSContext* cx, unsigned argc, Value* vp);
266 extern bool
267 math_atan(JSContext* cx, unsigned argc, Value* vp);
269 extern bool
270 math_atan2_handle(JSContext* cx, HandleValue y, HandleValue x, MutableHandleValue res);
272 extern bool
273 math_atan2(JSContext* cx, unsigned argc, Value* vp);
275 extern double
276 ecmaAtan2(double x, double y);
278 extern double
279 math_atan_impl(MathCache* cache, double x);
281 extern double
282 math_atan_uncached(double x);
284 extern bool
285 math_atan(JSContext* cx, unsigned argc, js::Value* vp);
287 extern double
288 math_asin_impl(MathCache* cache, double x);
290 extern double
291 math_asin_uncached(double x);
293 extern bool
294 math_asin(JSContext* cx, unsigned argc, js::Value* vp);
296 extern double
297 math_acos_impl(MathCache* cache, double x);
299 extern double
300 math_acos_uncached(double x);
302 extern bool
303 math_acos(JSContext* cx, unsigned argc, js::Value* vp);
305 extern bool
306 math_ceil(JSContext* cx, unsigned argc, Value* vp);
308 extern double
309 math_ceil_impl(double x);
311 extern bool
312 math_clz32(JSContext* cx, unsigned argc, Value* vp);
314 extern bool
315 math_floor_handle(JSContext* cx, HandleValue v, MutableHandleValue r);
317 extern bool
318 math_floor(JSContext* cx, unsigned argc, Value* vp);
320 extern double
321 math_floor_impl(double x);
323 template<typename T>
324 extern T GetBiggestNumberLessThan(T x);
326 extern bool
327 math_round_handle(JSContext* cx, HandleValue arg, MutableHandleValue res);
329 extern bool
330 math_round(JSContext* cx, unsigned argc, Value* vp);
332 extern double
333 math_round_impl(double x);
335 extern float
336 math_roundf_impl(float x);
338 extern double
339 powi(double x, int y);
341 extern double
342 ecmaPow(double x, double y);
344 extern bool
345 math_imul(JSContext* cx, unsigned argc, Value* vp);
347 extern double
348 math_log10_impl(MathCache* cache, double x);
350 extern double
351 math_log10_uncached(double x);
353 extern double
354 math_log2_impl(MathCache* cache, double x);
356 extern double
357 math_log2_uncached(double x);
359 extern double
360 math_log1p_impl(MathCache* cache, double x);
362 extern double
363 math_log1p_uncached(double x);
365 extern double
366 math_expm1_impl(MathCache* cache, double x);
368 extern double
369 math_expm1_uncached(double x);
371 extern double
372 math_cosh_impl(MathCache* cache, double x);
374 extern double
375 math_cosh_uncached(double x);
377 extern double
378 math_sinh_impl(MathCache* cache, double x);
380 extern double
381 math_sinh_uncached(double x);
383 extern double
384 math_tanh_impl(MathCache* cache, double x);
386 extern double
387 math_tanh_uncached(double x);
389 extern double
390 math_acosh_impl(MathCache* cache, double x);
392 extern double
393 math_acosh_uncached(double x);
395 extern double
396 math_asinh_impl(MathCache* cache, double x);
398 extern double
399 math_asinh_uncached(double x);
401 extern double
402 math_atanh_impl(MathCache* cache, double x);
404 extern double
405 math_atanh_uncached(double x);
407 extern double
408 math_trunc_impl(MathCache* cache, double x);
410 extern double
411 math_trunc_uncached(double x);
413 extern double
414 math_sign_impl(MathCache* cache, double x);
416 extern double
417 math_sign_uncached(double x);
419 extern double
420 math_cbrt_impl(MathCache* cache, double x);
422 extern double
423 math_cbrt_uncached(double x);
425 } /* namespace js */
427 #endif /* jsmath_h */