js/src/jsmath.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef jsmath_h
michael@0 8 #define jsmath_h
michael@0 9
michael@0 10 #include "mozilla/MemoryReporting.h"
michael@0 11
michael@0 12 #include "NamespaceImports.h"
michael@0 13
michael@0 14 #ifndef M_PI
michael@0 15 # define M_PI 3.14159265358979323846
michael@0 16 #endif
michael@0 17 #ifndef M_E
michael@0 18 # define M_E 2.7182818284590452354
michael@0 19 #endif
michael@0 20 #ifndef M_LOG2E
michael@0 21 # define M_LOG2E 1.4426950408889634074
michael@0 22 #endif
michael@0 23 #ifndef M_LOG10E
michael@0 24 # define M_LOG10E 0.43429448190325182765
michael@0 25 #endif
michael@0 26 #ifndef M_LN2
michael@0 27 # define M_LN2 0.69314718055994530942
michael@0 28 #endif
michael@0 29 #ifndef M_LN10
michael@0 30 # define M_LN10 2.30258509299404568402
michael@0 31 #endif
michael@0 32 #ifndef M_SQRT2
michael@0 33 # define M_SQRT2 1.41421356237309504880
michael@0 34 #endif
michael@0 35 #ifndef M_SQRT1_2
michael@0 36 # define M_SQRT1_2 0.70710678118654752440
michael@0 37 #endif
michael@0 38
michael@0 39 namespace js {
michael@0 40
michael@0 41 typedef double (*UnaryFunType)(double);
michael@0 42
michael@0 43 class MathCache
michael@0 44 {
michael@0 45 static const unsigned SizeLog2 = 12;
michael@0 46 static const unsigned Size = 1 << SizeLog2;
michael@0 47 struct Entry { double in; UnaryFunType f; double out; };
michael@0 48 Entry table[Size];
michael@0 49
michael@0 50 public:
michael@0 51 MathCache();
michael@0 52
michael@0 53 unsigned hash(double x) {
michael@0 54 union { double d; struct { uint32_t one, two; } s; } u = { x };
michael@0 55 uint32_t hash32 = u.s.one ^ u.s.two;
michael@0 56 uint16_t hash16 = uint16_t(hash32 ^ (hash32 >> 16));
michael@0 57 return (hash16 & (Size - 1)) ^ (hash16 >> (16 - SizeLog2));
michael@0 58 }
michael@0 59
michael@0 60 /*
michael@0 61 * N.B. lookup uses double-equality. This is only safe if hash() maps +0
michael@0 62 * and -0 to different table entries, which is asserted in MathCache().
michael@0 63 */
michael@0 64 double lookup(UnaryFunType f, double x) {
michael@0 65 unsigned index = hash(x);
michael@0 66 Entry &e = table[index];
michael@0 67 if (e.in == x && e.f == f)
michael@0 68 return e.out;
michael@0 69 e.in = x;
michael@0 70 e.f = f;
michael@0 71 return e.out = f(x);
michael@0 72 }
michael@0 73
michael@0 74 size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf);
michael@0 75 };
michael@0 76
michael@0 77 } /* namespace js */
michael@0 78
michael@0 79 /*
michael@0 80 * JS math functions.
michael@0 81 */
michael@0 82
michael@0 83 extern JSObject *
michael@0 84 js_InitMathClass(JSContext *cx, js::HandleObject obj);
michael@0 85
michael@0 86 extern double
michael@0 87 math_random_no_outparam(JSContext *cx);
michael@0 88
michael@0 89 extern bool
michael@0 90 js_math_random(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 91
michael@0 92 extern bool
michael@0 93 js_math_abs(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 94
michael@0 95 extern bool
michael@0 96 js_math_max(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 97
michael@0 98 extern bool
michael@0 99 js_math_min(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 100
michael@0 101 extern bool
michael@0 102 js_math_sqrt(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 103
michael@0 104 extern bool
michael@0 105 js_math_pow(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 106
michael@0 107 namespace js {
michael@0 108
michael@0 109 extern bool
michael@0 110 math_imul(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 111
michael@0 112 extern bool
michael@0 113 RoundFloat32(JSContext *cx, Handle<Value> v, float *out);
michael@0 114
michael@0 115 extern bool
michael@0 116 math_fround(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 117
michael@0 118 extern bool
michael@0 119 math_log(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 120
michael@0 121 extern double
michael@0 122 math_log_impl(MathCache *cache, double x);
michael@0 123
michael@0 124 extern double
michael@0 125 math_log_uncached(double x);
michael@0 126
michael@0 127 extern bool
michael@0 128 math_sin(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 129
michael@0 130 extern double
michael@0 131 math_sin_impl(MathCache *cache, double x);
michael@0 132
michael@0 133 extern double
michael@0 134 math_sin_uncached(double x);
michael@0 135
michael@0 136 extern bool
michael@0 137 math_cos(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 138
michael@0 139 extern double
michael@0 140 math_cos_impl(MathCache *cache, double x);
michael@0 141
michael@0 142 extern double
michael@0 143 math_cos_uncached(double x);
michael@0 144
michael@0 145 extern bool
michael@0 146 math_exp(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 147
michael@0 148 extern double
michael@0 149 math_exp_impl(MathCache *cache, double x);
michael@0 150
michael@0 151 extern double
michael@0 152 math_exp_uncached(double x);
michael@0 153
michael@0 154 extern bool
michael@0 155 math_tan(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 156
michael@0 157 extern double
michael@0 158 math_tan_impl(MathCache *cache, double x);
michael@0 159
michael@0 160 extern double
michael@0 161 math_tan_uncached(double x);
michael@0 162
michael@0 163 extern bool
michael@0 164 math_log10(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 165
michael@0 166 extern bool
michael@0 167 math_log2(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 168
michael@0 169 extern bool
michael@0 170 math_log1p(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 171
michael@0 172 extern bool
michael@0 173 math_expm1(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 174
michael@0 175 extern bool
michael@0 176 math_cosh(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 177
michael@0 178 extern bool
michael@0 179 math_sinh(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 180
michael@0 181 extern bool
michael@0 182 math_tanh(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 183
michael@0 184 extern bool
michael@0 185 math_acosh(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 186
michael@0 187 extern bool
michael@0 188 math_asinh(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 189
michael@0 190 extern bool
michael@0 191 math_atanh(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 192
michael@0 193 extern double
michael@0 194 ecmaHypot(double x, double y);
michael@0 195
michael@0 196 extern bool
michael@0 197 math_hypot(JSContext *cx, unsigned argc, Value *vp);
michael@0 198
michael@0 199 extern bool
michael@0 200 math_trunc(JSContext *cx, unsigned argc, Value *vp);
michael@0 201
michael@0 202 extern bool
michael@0 203 math_sign(JSContext *cx, unsigned argc, Value *vp);
michael@0 204
michael@0 205 extern bool
michael@0 206 math_cbrt(JSContext *cx, unsigned argc, Value *vp);
michael@0 207
michael@0 208 extern bool
michael@0 209 math_asin(JSContext *cx, unsigned argc, Value *vp);
michael@0 210
michael@0 211 extern bool
michael@0 212 math_acos(JSContext *cx, unsigned argc, Value *vp);
michael@0 213
michael@0 214 extern bool
michael@0 215 math_atan(JSContext *cx, unsigned argc, Value *vp);
michael@0 216
michael@0 217 extern bool
michael@0 218 math_atan2(JSContext *cx, unsigned argc, Value *vp);
michael@0 219
michael@0 220 extern double
michael@0 221 ecmaAtan2(double x, double y);
michael@0 222
michael@0 223 extern double
michael@0 224 math_atan_impl(MathCache *cache, double x);
michael@0 225
michael@0 226 extern double
michael@0 227 math_atan_uncached(double x);
michael@0 228
michael@0 229 extern bool
michael@0 230 math_atan(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 231
michael@0 232 extern double
michael@0 233 math_asin_impl(MathCache *cache, double x);
michael@0 234
michael@0 235 extern double
michael@0 236 math_asin_uncached(double x);
michael@0 237
michael@0 238 extern bool
michael@0 239 math_asin(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 240
michael@0 241 extern double
michael@0 242 math_acos_impl(MathCache *cache, double x);
michael@0 243
michael@0 244 extern double
michael@0 245 math_acos_uncached(double x);
michael@0 246
michael@0 247 extern bool
michael@0 248 math_acos(JSContext *cx, unsigned argc, js::Value *vp);
michael@0 249
michael@0 250 extern bool
michael@0 251 math_ceil(JSContext *cx, unsigned argc, Value *vp);
michael@0 252
michael@0 253 extern double
michael@0 254 math_ceil_impl(double x);
michael@0 255
michael@0 256 extern bool
michael@0 257 math_clz32(JSContext *cx, unsigned argc, Value *vp);
michael@0 258
michael@0 259 extern bool
michael@0 260 math_floor(JSContext *cx, unsigned argc, Value *vp);
michael@0 261
michael@0 262 extern double
michael@0 263 math_floor_impl(double x);
michael@0 264
michael@0 265 extern bool
michael@0 266 math_round(JSContext *cx, unsigned argc, Value *vp);
michael@0 267
michael@0 268 extern double
michael@0 269 math_round_impl(double x);
michael@0 270
michael@0 271 extern float
michael@0 272 math_roundf_impl(float x);
michael@0 273
michael@0 274 extern double
michael@0 275 powi(double x, int y);
michael@0 276
michael@0 277 extern double
michael@0 278 ecmaPow(double x, double y);
michael@0 279
michael@0 280 extern bool
michael@0 281 math_imul(JSContext *cx, unsigned argc, Value *vp);
michael@0 282
michael@0 283 extern double
michael@0 284 math_log10_impl(MathCache *cache, double x);
michael@0 285
michael@0 286 extern double
michael@0 287 math_log10_uncached(double x);
michael@0 288
michael@0 289 extern double
michael@0 290 math_log2_impl(MathCache *cache, double x);
michael@0 291
michael@0 292 extern double
michael@0 293 math_log2_uncached(double x);
michael@0 294
michael@0 295 extern double
michael@0 296 math_log1p_impl(MathCache *cache, double x);
michael@0 297
michael@0 298 extern double
michael@0 299 math_log1p_uncached(double x);
michael@0 300
michael@0 301 extern double
michael@0 302 math_expm1_impl(MathCache *cache, double x);
michael@0 303
michael@0 304 extern double
michael@0 305 math_expm1_uncached(double x);
michael@0 306
michael@0 307 extern double
michael@0 308 math_cosh_impl(MathCache *cache, double x);
michael@0 309
michael@0 310 extern double
michael@0 311 math_cosh_uncached(double x);
michael@0 312
michael@0 313 extern double
michael@0 314 math_sinh_impl(MathCache *cache, double x);
michael@0 315
michael@0 316 extern double
michael@0 317 math_sinh_uncached(double x);
michael@0 318
michael@0 319 extern double
michael@0 320 math_tanh_impl(MathCache *cache, double x);
michael@0 321
michael@0 322 extern double
michael@0 323 math_tanh_uncached(double x);
michael@0 324
michael@0 325 extern double
michael@0 326 math_acosh_impl(MathCache *cache, double x);
michael@0 327
michael@0 328 extern double
michael@0 329 math_acosh_uncached(double x);
michael@0 330
michael@0 331 extern double
michael@0 332 math_asinh_impl(MathCache *cache, double x);
michael@0 333
michael@0 334 extern double
michael@0 335 math_asinh_uncached(double x);
michael@0 336
michael@0 337 extern double
michael@0 338 math_atanh_impl(MathCache *cache, double x);
michael@0 339
michael@0 340 extern double
michael@0 341 math_atanh_uncached(double x);
michael@0 342
michael@0 343 extern double
michael@0 344 math_trunc_impl(MathCache *cache, double x);
michael@0 345
michael@0 346 extern double
michael@0 347 math_trunc_uncached(double x);
michael@0 348
michael@0 349 extern double
michael@0 350 math_sign_impl(MathCache *cache, double x);
michael@0 351
michael@0 352 extern double
michael@0 353 math_sign_uncached(double x);
michael@0 354
michael@0 355 extern double
michael@0 356 math_cbrt_impl(MathCache *cache, double x);
michael@0 357
michael@0 358 extern double
michael@0 359 math_cbrt_uncached(double x);
michael@0 360
michael@0 361 } /* namespace js */
michael@0 362
michael@0 363 #endif /* jsmath_h */

mercurial