1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsmath.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,363 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef jsmath_h 1.11 +#define jsmath_h 1.12 + 1.13 +#include "mozilla/MemoryReporting.h" 1.14 + 1.15 +#include "NamespaceImports.h" 1.16 + 1.17 +#ifndef M_PI 1.18 +# define M_PI 3.14159265358979323846 1.19 +#endif 1.20 +#ifndef M_E 1.21 +# define M_E 2.7182818284590452354 1.22 +#endif 1.23 +#ifndef M_LOG2E 1.24 +# define M_LOG2E 1.4426950408889634074 1.25 +#endif 1.26 +#ifndef M_LOG10E 1.27 +# define M_LOG10E 0.43429448190325182765 1.28 +#endif 1.29 +#ifndef M_LN2 1.30 +# define M_LN2 0.69314718055994530942 1.31 +#endif 1.32 +#ifndef M_LN10 1.33 +# define M_LN10 2.30258509299404568402 1.34 +#endif 1.35 +#ifndef M_SQRT2 1.36 +# define M_SQRT2 1.41421356237309504880 1.37 +#endif 1.38 +#ifndef M_SQRT1_2 1.39 +# define M_SQRT1_2 0.70710678118654752440 1.40 +#endif 1.41 + 1.42 +namespace js { 1.43 + 1.44 +typedef double (*UnaryFunType)(double); 1.45 + 1.46 +class MathCache 1.47 +{ 1.48 + static const unsigned SizeLog2 = 12; 1.49 + static const unsigned Size = 1 << SizeLog2; 1.50 + struct Entry { double in; UnaryFunType f; double out; }; 1.51 + Entry table[Size]; 1.52 + 1.53 + public: 1.54 + MathCache(); 1.55 + 1.56 + unsigned hash(double x) { 1.57 + union { double d; struct { uint32_t one, two; } s; } u = { x }; 1.58 + uint32_t hash32 = u.s.one ^ u.s.two; 1.59 + uint16_t hash16 = uint16_t(hash32 ^ (hash32 >> 16)); 1.60 + return (hash16 & (Size - 1)) ^ (hash16 >> (16 - SizeLog2)); 1.61 + } 1.62 + 1.63 + /* 1.64 + * N.B. lookup uses double-equality. This is only safe if hash() maps +0 1.65 + * and -0 to different table entries, which is asserted in MathCache(). 1.66 + */ 1.67 + double lookup(UnaryFunType f, double x) { 1.68 + unsigned index = hash(x); 1.69 + Entry &e = table[index]; 1.70 + if (e.in == x && e.f == f) 1.71 + return e.out; 1.72 + e.in = x; 1.73 + e.f = f; 1.74 + return e.out = f(x); 1.75 + } 1.76 + 1.77 + size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf); 1.78 +}; 1.79 + 1.80 +} /* namespace js */ 1.81 + 1.82 +/* 1.83 + * JS math functions. 1.84 + */ 1.85 + 1.86 +extern JSObject * 1.87 +js_InitMathClass(JSContext *cx, js::HandleObject obj); 1.88 + 1.89 +extern double 1.90 +math_random_no_outparam(JSContext *cx); 1.91 + 1.92 +extern bool 1.93 +js_math_random(JSContext *cx, unsigned argc, js::Value *vp); 1.94 + 1.95 +extern bool 1.96 +js_math_abs(JSContext *cx, unsigned argc, js::Value *vp); 1.97 + 1.98 +extern bool 1.99 +js_math_max(JSContext *cx, unsigned argc, js::Value *vp); 1.100 + 1.101 +extern bool 1.102 +js_math_min(JSContext *cx, unsigned argc, js::Value *vp); 1.103 + 1.104 +extern bool 1.105 +js_math_sqrt(JSContext *cx, unsigned argc, js::Value *vp); 1.106 + 1.107 +extern bool 1.108 +js_math_pow(JSContext *cx, unsigned argc, js::Value *vp); 1.109 + 1.110 +namespace js { 1.111 + 1.112 +extern bool 1.113 +math_imul(JSContext *cx, unsigned argc, js::Value *vp); 1.114 + 1.115 +extern bool 1.116 +RoundFloat32(JSContext *cx, Handle<Value> v, float *out); 1.117 + 1.118 +extern bool 1.119 +math_fround(JSContext *cx, unsigned argc, js::Value *vp); 1.120 + 1.121 +extern bool 1.122 +math_log(JSContext *cx, unsigned argc, js::Value *vp); 1.123 + 1.124 +extern double 1.125 +math_log_impl(MathCache *cache, double x); 1.126 + 1.127 +extern double 1.128 +math_log_uncached(double x); 1.129 + 1.130 +extern bool 1.131 +math_sin(JSContext *cx, unsigned argc, js::Value *vp); 1.132 + 1.133 +extern double 1.134 +math_sin_impl(MathCache *cache, double x); 1.135 + 1.136 +extern double 1.137 +math_sin_uncached(double x); 1.138 + 1.139 +extern bool 1.140 +math_cos(JSContext *cx, unsigned argc, js::Value *vp); 1.141 + 1.142 +extern double 1.143 +math_cos_impl(MathCache *cache, double x); 1.144 + 1.145 +extern double 1.146 +math_cos_uncached(double x); 1.147 + 1.148 +extern bool 1.149 +math_exp(JSContext *cx, unsigned argc, js::Value *vp); 1.150 + 1.151 +extern double 1.152 +math_exp_impl(MathCache *cache, double x); 1.153 + 1.154 +extern double 1.155 +math_exp_uncached(double x); 1.156 + 1.157 +extern bool 1.158 +math_tan(JSContext *cx, unsigned argc, js::Value *vp); 1.159 + 1.160 +extern double 1.161 +math_tan_impl(MathCache *cache, double x); 1.162 + 1.163 +extern double 1.164 +math_tan_uncached(double x); 1.165 + 1.166 +extern bool 1.167 +math_log10(JSContext *cx, unsigned argc, js::Value *vp); 1.168 + 1.169 +extern bool 1.170 +math_log2(JSContext *cx, unsigned argc, js::Value *vp); 1.171 + 1.172 +extern bool 1.173 +math_log1p(JSContext *cx, unsigned argc, js::Value *vp); 1.174 + 1.175 +extern bool 1.176 +math_expm1(JSContext *cx, unsigned argc, js::Value *vp); 1.177 + 1.178 +extern bool 1.179 +math_cosh(JSContext *cx, unsigned argc, js::Value *vp); 1.180 + 1.181 +extern bool 1.182 +math_sinh(JSContext *cx, unsigned argc, js::Value *vp); 1.183 + 1.184 +extern bool 1.185 +math_tanh(JSContext *cx, unsigned argc, js::Value *vp); 1.186 + 1.187 +extern bool 1.188 +math_acosh(JSContext *cx, unsigned argc, js::Value *vp); 1.189 + 1.190 +extern bool 1.191 +math_asinh(JSContext *cx, unsigned argc, js::Value *vp); 1.192 + 1.193 +extern bool 1.194 +math_atanh(JSContext *cx, unsigned argc, js::Value *vp); 1.195 + 1.196 +extern double 1.197 +ecmaHypot(double x, double y); 1.198 + 1.199 +extern bool 1.200 +math_hypot(JSContext *cx, unsigned argc, Value *vp); 1.201 + 1.202 +extern bool 1.203 +math_trunc(JSContext *cx, unsigned argc, Value *vp); 1.204 + 1.205 +extern bool 1.206 +math_sign(JSContext *cx, unsigned argc, Value *vp); 1.207 + 1.208 +extern bool 1.209 +math_cbrt(JSContext *cx, unsigned argc, Value *vp); 1.210 + 1.211 +extern bool 1.212 +math_asin(JSContext *cx, unsigned argc, Value *vp); 1.213 + 1.214 +extern bool 1.215 +math_acos(JSContext *cx, unsigned argc, Value *vp); 1.216 + 1.217 +extern bool 1.218 +math_atan(JSContext *cx, unsigned argc, Value *vp); 1.219 + 1.220 +extern bool 1.221 +math_atan2(JSContext *cx, unsigned argc, Value *vp); 1.222 + 1.223 +extern double 1.224 +ecmaAtan2(double x, double y); 1.225 + 1.226 +extern double 1.227 +math_atan_impl(MathCache *cache, double x); 1.228 + 1.229 +extern double 1.230 +math_atan_uncached(double x); 1.231 + 1.232 +extern bool 1.233 +math_atan(JSContext *cx, unsigned argc, js::Value *vp); 1.234 + 1.235 +extern double 1.236 +math_asin_impl(MathCache *cache, double x); 1.237 + 1.238 +extern double 1.239 +math_asin_uncached(double x); 1.240 + 1.241 +extern bool 1.242 +math_asin(JSContext *cx, unsigned argc, js::Value *vp); 1.243 + 1.244 +extern double 1.245 +math_acos_impl(MathCache *cache, double x); 1.246 + 1.247 +extern double 1.248 +math_acos_uncached(double x); 1.249 + 1.250 +extern bool 1.251 +math_acos(JSContext *cx, unsigned argc, js::Value *vp); 1.252 + 1.253 +extern bool 1.254 +math_ceil(JSContext *cx, unsigned argc, Value *vp); 1.255 + 1.256 +extern double 1.257 +math_ceil_impl(double x); 1.258 + 1.259 +extern bool 1.260 +math_clz32(JSContext *cx, unsigned argc, Value *vp); 1.261 + 1.262 +extern bool 1.263 +math_floor(JSContext *cx, unsigned argc, Value *vp); 1.264 + 1.265 +extern double 1.266 +math_floor_impl(double x); 1.267 + 1.268 +extern bool 1.269 +math_round(JSContext *cx, unsigned argc, Value *vp); 1.270 + 1.271 +extern double 1.272 +math_round_impl(double x); 1.273 + 1.274 +extern float 1.275 +math_roundf_impl(float x); 1.276 + 1.277 +extern double 1.278 +powi(double x, int y); 1.279 + 1.280 +extern double 1.281 +ecmaPow(double x, double y); 1.282 + 1.283 +extern bool 1.284 +math_imul(JSContext *cx, unsigned argc, Value *vp); 1.285 + 1.286 +extern double 1.287 +math_log10_impl(MathCache *cache, double x); 1.288 + 1.289 +extern double 1.290 +math_log10_uncached(double x); 1.291 + 1.292 +extern double 1.293 +math_log2_impl(MathCache *cache, double x); 1.294 + 1.295 +extern double 1.296 +math_log2_uncached(double x); 1.297 + 1.298 +extern double 1.299 +math_log1p_impl(MathCache *cache, double x); 1.300 + 1.301 +extern double 1.302 +math_log1p_uncached(double x); 1.303 + 1.304 +extern double 1.305 +math_expm1_impl(MathCache *cache, double x); 1.306 + 1.307 +extern double 1.308 +math_expm1_uncached(double x); 1.309 + 1.310 +extern double 1.311 +math_cosh_impl(MathCache *cache, double x); 1.312 + 1.313 +extern double 1.314 +math_cosh_uncached(double x); 1.315 + 1.316 +extern double 1.317 +math_sinh_impl(MathCache *cache, double x); 1.318 + 1.319 +extern double 1.320 +math_sinh_uncached(double x); 1.321 + 1.322 +extern double 1.323 +math_tanh_impl(MathCache *cache, double x); 1.324 + 1.325 +extern double 1.326 +math_tanh_uncached(double x); 1.327 + 1.328 +extern double 1.329 +math_acosh_impl(MathCache *cache, double x); 1.330 + 1.331 +extern double 1.332 +math_acosh_uncached(double x); 1.333 + 1.334 +extern double 1.335 +math_asinh_impl(MathCache *cache, double x); 1.336 + 1.337 +extern double 1.338 +math_asinh_uncached(double x); 1.339 + 1.340 +extern double 1.341 +math_atanh_impl(MathCache *cache, double x); 1.342 + 1.343 +extern double 1.344 +math_atanh_uncached(double x); 1.345 + 1.346 +extern double 1.347 +math_trunc_impl(MathCache *cache, double x); 1.348 + 1.349 +extern double 1.350 +math_trunc_uncached(double x); 1.351 + 1.352 +extern double 1.353 +math_sign_impl(MathCache *cache, double x); 1.354 + 1.355 +extern double 1.356 +math_sign_uncached(double x); 1.357 + 1.358 +extern double 1.359 +math_cbrt_impl(MathCache *cache, double x); 1.360 + 1.361 +extern double 1.362 +math_cbrt_uncached(double x); 1.363 + 1.364 +} /* namespace js */ 1.365 + 1.366 +#endif /* jsmath_h */