| OLD | NEW |
| (Empty) |
| 1 // The following is adapted from fdlibm (http://www.netlib.org/fdlibm), | |
| 2 // | |
| 3 // ==================================================== | |
| 4 // Copyright (C) 1993-2004 by Sun Microsystems, Inc. All rights reserved. | |
| 5 // | |
| 6 // Developed at SunSoft, a Sun Microsystems, Inc. business. | |
| 7 // Permission to use, copy, modify, and distribute this | |
| 8 // software is freely granted, provided that this notice | |
| 9 // is preserved. | |
| 10 // ==================================================== | |
| 11 // | |
| 12 // The original source code covered by the above license above has been | |
| 13 // modified significantly by Google Inc. | |
| 14 // Copyright 2014 the V8 project authors. All rights reserved. | |
| 15 // | |
| 16 // The following is a straightforward translation of fdlibm routines | |
| 17 // by Raymond Toy (rtoy@google.com). | |
| 18 | |
| 19 // Double constants that do not have empty lower 32 bits are found in fdlibm.cc | |
| 20 // and exposed through kMath as typed array. We assume the compiler to convert | |
| 21 // from decimal to binary accurately enough to produce the intended values. | |
| 22 // kMath is initialized to a Float64Array during genesis and not writable. | |
| 23 var kMath; | |
| 24 | |
| 25 const INVPIO2 = kMath[0]; | |
| 26 const PIO2_1 = kMath[1]; | |
| 27 const PIO2_1T = kMath[2]; | |
| 28 const PIO2_2 = kMath[3]; | |
| 29 const PIO2_2T = kMath[4]; | |
| 30 const PIO2_3 = kMath[5]; | |
| 31 const PIO2_3T = kMath[6]; | |
| 32 const PIO4 = kMath[32]; | |
| 33 const PIO4LO = kMath[33]; | |
| 34 | |
| 35 // Compute k and r such that x - k*pi/2 = r where |r| < pi/4. For | |
| 36 // precision, r is returned as two values y0 and y1 such that r = y0 + y1 | |
| 37 // to more than double precision. | |
| 38 macro REMPIO2(X) | |
| 39 var n, y0, y1; | |
| 40 var hx = %_DoubleHi(X); | |
| 41 var ix = hx & 0x7fffffff; | |
| 42 | |
| 43 if (ix < 0x4002d97c) { | |
| 44 // |X| ~< 3*pi/4, special case with n = +/- 1 | |
| 45 if (hx > 0) { | |
| 46 var z = X - PIO2_1; | |
| 47 if (ix != 0x3ff921fb) { | |
| 48 // 33+53 bit pi is good enough | |
| 49 y0 = z - PIO2_1T; | |
| 50 y1 = (z - y0) - PIO2_1T; | |
| 51 } else { | |
| 52 // near pi/2, use 33+33+53 bit pi | |
| 53 z -= PIO2_2; | |
| 54 y0 = z - PIO2_2T; | |
| 55 y1 = (z - y0) - PIO2_2T; | |
| 56 } | |
| 57 n = 1; | |
| 58 } else { | |
| 59 // Negative X | |
| 60 var z = X + PIO2_1; | |
| 61 if (ix != 0x3ff921fb) { | |
| 62 // 33+53 bit pi is good enough | |
| 63 y0 = z + PIO2_1T; | |
| 64 y1 = (z - y0) + PIO2_1T; | |
| 65 } else { | |
| 66 // near pi/2, use 33+33+53 bit pi | |
| 67 z += PIO2_2; | |
| 68 y0 = z + PIO2_2T; | |
| 69 y1 = (z - y0) + PIO2_2T; | |
| 70 } | |
| 71 n = -1; | |
| 72 } | |
| 73 } else if (ix <= 0x413921fb) { | |
| 74 // |X| ~<= 2^19*(pi/2), medium size | |
| 75 var t = MathAbs(X); | |
| 76 n = (t * INVPIO2 + 0.5) | 0; | |
| 77 var r = t - n * PIO2_1; | |
| 78 var w = n * PIO2_1T; | |
| 79 // First round good to 85 bit | |
| 80 y0 = r - w; | |
| 81 if (ix - (%_DoubleHi(y0) & 0x7ff00000) > 0x1000000) { | |
| 82 // 2nd iteration needed, good to 118 | |
| 83 t = r; | |
| 84 w = n * PIO2_2; | |
| 85 r = t - w; | |
| 86 w = n * PIO2_2T - ((t - r) - w); | |
| 87 y0 = r - w; | |
| 88 if (ix - (%_DoubleHi(y0) & 0x7ff00000) > 0x3100000) { | |
| 89 // 3rd iteration needed. 151 bits accuracy | |
| 90 t = r; | |
| 91 w = n * PIO2_3; | |
| 92 r = t - w; | |
| 93 w = n * PIO2_3T - ((t - r) - w); | |
| 94 y0 = r - w; | |
| 95 } | |
| 96 } | |
| 97 y1 = (r - y0) - w; | |
| 98 if (hx < 0) { | |
| 99 n = -n; | |
| 100 y0 = -y0; | |
| 101 y1 = -y1; | |
| 102 } | |
| 103 } else { | |
| 104 // Need to do full Payne-Hanek reduction here. | |
| 105 var r = %RemPiO2(X); | |
| 106 n = r[0]; | |
| 107 y0 = r[1]; | |
| 108 y1 = r[2]; | |
| 109 } | |
| 110 endmacro | |
| 111 | |
| 112 | |
| 113 // __kernel_sin(X, Y, IY) | |
| 114 // kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854 | |
| 115 // Input X is assumed to be bounded by ~pi/4 in magnitude. | |
| 116 // Input Y is the tail of X so that x = X + Y. | |
| 117 // | |
| 118 // Algorithm | |
| 119 // 1. Since ieee_sin(-x) = -ieee_sin(x), we need only to consider positive x. | |
| 120 // 2. ieee_sin(x) is approximated by a polynomial of degree 13 on | |
| 121 // [0,pi/4] | |
| 122 // 3 13 | |
| 123 // sin(x) ~ x + S1*x + ... + S6*x | |
| 124 // where | |
| 125 // | |
| 126 // |ieee_sin(x) 2 4 6 8 10 12 | -58 | |
| 127 // |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2 | |
| 128 // | x | | |
| 129 // | |
| 130 // 3. ieee_sin(X+Y) = ieee_sin(X) + sin'(X')*Y | |
| 131 // ~ ieee_sin(X) + (1-X*X/2)*Y | |
| 132 // For better accuracy, let | |
| 133 // 3 2 2 2 2 | |
| 134 // r = X *(S2+X *(S3+X *(S4+X *(S5+X *S6)))) | |
| 135 // then 3 2 | |
| 136 // sin(x) = X + (S1*X + (X *(r-Y/2)+Y)) | |
| 137 // | |
| 138 macro KSIN(x) | |
| 139 kMath[7+x] | |
| 140 endmacro | |
| 141 | |
| 142 macro RETURN_KERNELSIN(X, Y, SIGN) | |
| 143 var z = X * X; | |
| 144 var v = z * X; | |
| 145 var r = KSIN(1) + z * (KSIN(2) + z * (KSIN(3) + | |
| 146 z * (KSIN(4) + z * KSIN(5)))); | |
| 147 return (X - ((z * (0.5 * Y - v * r) - Y) - v * KSIN(0))) SIGN; | |
| 148 endmacro | |
| 149 | |
| 150 // __kernel_cos(X, Y) | |
| 151 // kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164 | |
| 152 // Input X is assumed to be bounded by ~pi/4 in magnitude. | |
| 153 // Input Y is the tail of X so that x = X + Y. | |
| 154 // | |
| 155 // Algorithm | |
| 156 // 1. Since ieee_cos(-x) = ieee_cos(x), we need only to consider positive x. | |
| 157 // 2. ieee_cos(x) is approximated by a polynomial of degree 14 on | |
| 158 // [0,pi/4] | |
| 159 // 4 14 | |
| 160 // cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x | |
| 161 // where the remez error is | |
| 162 // | |
| 163 // | 2 4 6 8 10 12 14 | -58 | |
| 164 // |ieee_cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x +C6*x )| <= 2 | |
| 165 // | | | |
| 166 // | |
| 167 // 4 6 8 10 12 14 | |
| 168 // 3. let r = C1*x +C2*x +C3*x +C4*x +C5*x +C6*x , then | |
| 169 // ieee_cos(x) = 1 - x*x/2 + r | |
| 170 // since ieee_cos(X+Y) ~ ieee_cos(X) - ieee_sin(X)*Y | |
| 171 // ~ ieee_cos(X) - X*Y, | |
| 172 // a correction term is necessary in ieee_cos(x) and hence | |
| 173 // cos(X+Y) = 1 - (X*X/2 - (r - X*Y)) | |
| 174 // For better accuracy when x > 0.3, let qx = |x|/4 with | |
| 175 // the last 32 bits mask off, and if x > 0.78125, let qx = 0.28125. | |
| 176 // Then | |
| 177 // cos(X+Y) = (1-qx) - ((X*X/2-qx) - (r-X*Y)). | |
| 178 // Note that 1-qx and (X*X/2-qx) is EXACT here, and the | |
| 179 // magnitude of the latter is at least a quarter of X*X/2, | |
| 180 // thus, reducing the rounding error in the subtraction. | |
| 181 // | |
| 182 macro KCOS(x) | |
| 183 kMath[13+x] | |
| 184 endmacro | |
| 185 | |
| 186 macro RETURN_KERNELCOS(X, Y, SIGN) | |
| 187 var ix = %_DoubleHi(X) & 0x7fffffff; | |
| 188 var z = X * X; | |
| 189 var r = z * (KCOS(0) + z * (KCOS(1) + z * (KCOS(2)+ | |
| 190 z * (KCOS(3) + z * (KCOS(4) + z * KCOS(5)))))); | |
| 191 if (ix < 0x3fd33333) { // |x| ~< 0.3 | |
| 192 return (1 - (0.5 * z - (z * r - X * Y))) SIGN; | |
| 193 } else { | |
| 194 var qx; | |
| 195 if (ix > 0x3fe90000) { // |x| > 0.78125 | |
| 196 qx = 0.28125; | |
| 197 } else { | |
| 198 qx = %_ConstructDouble(%_DoubleHi(0.25 * X), 0); | |
| 199 } | |
| 200 var hz = 0.5 * z - qx; | |
| 201 return (1 - qx - (hz - (z * r - X * Y))) SIGN; | |
| 202 } | |
| 203 endmacro | |
| 204 | |
| 205 | |
| 206 // kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854 | |
| 207 // Input x is assumed to be bounded by ~pi/4 in magnitude. | |
| 208 // Input y is the tail of x. | |
| 209 // Input k indicates whether ieee_tan (if k = 1) or -1/tan (if k = -1) | |
| 210 // is returned. | |
| 211 // | |
| 212 // Algorithm | |
| 213 // 1. Since ieee_tan(-x) = -ieee_tan(x), we need only to consider positive x. | |
| 214 // 2. if x < 2^-28 (hx<0x3e300000 0), return x with inexact if x!=0. | |
| 215 // 3. ieee_tan(x) is approximated by a odd polynomial of degree 27 on | |
| 216 // [0,0.67434] | |
| 217 // 3 27 | |
| 218 // tan(x) ~ x + T1*x + ... + T13*x | |
| 219 // where | |
| 220 // | |
| 221 // |ieee_tan(x) 2 4 26 | -59.2 | |
| 222 // |----- - (1+T1*x +T2*x +.... +T13*x )| <= 2 | |
| 223 // | x | | |
| 224 // | |
| 225 // Note: ieee_tan(x+y) = ieee_tan(x) + tan'(x)*y | |
| 226 // ~ ieee_tan(x) + (1+x*x)*y | |
| 227 // Therefore, for better accuracy in computing ieee_tan(x+y), let | |
| 228 // 3 2 2 2 2 | |
| 229 // r = x *(T2+x *(T3+x *(...+x *(T12+x *T13)))) | |
| 230 // then | |
| 231 // 3 2 | |
| 232 // tan(x+y) = x + (T1*x + (x *(r+y)+y)) | |
| 233 // | |
| 234 // 4. For x in [0.67434,pi/4], let y = pi/4 - x, then | |
| 235 // tan(x) = ieee_tan(pi/4-y) = (1-ieee_tan(y))/(1+ieee_tan(y)) | |
| 236 // = 1 - 2*(ieee_tan(y) - (ieee_tan(y)^2)/(1+ieee_tan(y))) | |
| 237 // | |
| 238 // Set returnTan to 1 for tan; -1 for cot. Anything else is illegal | |
| 239 // and will cause incorrect results. | |
| 240 // | |
| 241 macro KTAN(x) | |
| 242 kMath[19+x] | |
| 243 endmacro | |
| 244 | |
| 245 function KernelTan(x, y, returnTan) { | |
| 246 var z; | |
| 247 var w; | |
| 248 var hx = %_DoubleHi(x); | |
| 249 var ix = hx & 0x7fffffff; | |
| 250 | |
| 251 if (ix < 0x3e300000) { // |x| < 2^-28 | |
| 252 if (((ix | %_DoubleLo(x)) | (returnTan + 1)) == 0) { | |
| 253 // x == 0 && returnTan = -1 | |
| 254 return 1 / MathAbs(x); | |
| 255 } else { | |
| 256 if (returnTan == 1) { | |
| 257 return x; | |
| 258 } else { | |
| 259 // Compute -1/(x + y) carefully | |
| 260 var w = x + y; | |
| 261 var z = %_ConstructDouble(%_DoubleHi(w), 0); | |
| 262 var v = y - (z - x); | |
| 263 var a = -1 / w; | |
| 264 var t = %_ConstructDouble(%_DoubleHi(a), 0); | |
| 265 var s = 1 + t * z; | |
| 266 return t + a * (s + t * v); | |
| 267 } | |
| 268 } | |
| 269 } | |
| 270 if (ix >= 0x3fe59429) { // |x| > .6744 | |
| 271 if (x < 0) { | |
| 272 x = -x; | |
| 273 y = -y; | |
| 274 } | |
| 275 z = PIO4 - x; | |
| 276 w = PIO4LO - y; | |
| 277 x = z + w; | |
| 278 y = 0; | |
| 279 } | |
| 280 z = x * x; | |
| 281 w = z * z; | |
| 282 | |
| 283 // Break x^5 * (T1 + x^2*T2 + ...) into | |
| 284 // x^5 * (T1 + x^4*T3 + ... + x^20*T11) + | |
| 285 // x^5 * (x^2 * (T2 + x^4*T4 + ... + x^22*T12)) | |
| 286 var r = KTAN(1) + w * (KTAN(3) + w * (KTAN(5) + | |
| 287 w * (KTAN(7) + w * (KTAN(9) + w * KTAN(11))))); | |
| 288 var v = z * (KTAN(2) + w * (KTAN(4) + w * (KTAN(6) + | |
| 289 w * (KTAN(8) + w * (KTAN(10) + w * KTAN(12)))))); | |
| 290 var s = z * x; | |
| 291 r = y + z * (s * (r + v) + y); | |
| 292 r = r + KTAN(0) * s; | |
| 293 w = x + r; | |
| 294 if (ix >= 0x3fe59428) { | |
| 295 return (1 - ((hx >> 30) & 2)) * | |
| 296 (returnTan - 2.0 * (x - (w * w / (w + returnTan) - r))); | |
| 297 } | |
| 298 if (returnTan == 1) { | |
| 299 return w; | |
| 300 } else { | |
| 301 z = %_ConstructDouble(%_DoubleHi(w), 0); | |
| 302 v = r - (z - x); | |
| 303 var a = -1 / w; | |
| 304 var t = %_ConstructDouble(%_DoubleHi(a), 0); | |
| 305 s = 1 + t * z; | |
| 306 return t + a * (s + t * v); | |
| 307 } | |
| 308 } | |
| 309 | |
| 310 function MathSinSlow(x) { | |
| 311 REMPIO2(x); | |
| 312 var sign = 1 - (n & 2); | |
| 313 if (n & 1) { | |
| 314 RETURN_KERNELCOS(y0, y1, * sign); | |
| 315 } else { | |
| 316 RETURN_KERNELSIN(y0, y1, * sign); | |
| 317 } | |
| 318 } | |
| 319 | |
| 320 function MathCosSlow(x) { | |
| 321 REMPIO2(x); | |
| 322 if (n & 1) { | |
| 323 var sign = (n & 2) - 1; | |
| 324 RETURN_KERNELSIN(y0, y1, * sign); | |
| 325 } else { | |
| 326 var sign = 1 - (n & 2); | |
| 327 RETURN_KERNELCOS(y0, y1, * sign); | |
| 328 } | |
| 329 } | |
| 330 | |
| 331 // ECMA 262 - 15.8.2.16 | |
| 332 function MathSin(x) { | |
| 333 x = x * 1; // Convert to number. | |
| 334 if ((%_DoubleHi(x) & 0x7fffffff) <= 0x3fe921fb) { | |
| 335 // |x| < pi/4, approximately. No reduction needed. | |
| 336 RETURN_KERNELSIN(x, 0, /* empty */); | |
| 337 } | |
| 338 return MathSinSlow(x); | |
| 339 } | |
| 340 | |
| 341 // ECMA 262 - 15.8.2.7 | |
| 342 function MathCos(x) { | |
| 343 x = x * 1; // Convert to number. | |
| 344 if ((%_DoubleHi(x) & 0x7fffffff) <= 0x3fe921fb) { | |
| 345 // |x| < pi/4, approximately. No reduction needed. | |
| 346 RETURN_KERNELCOS(x, 0, /* empty */); | |
| 347 } | |
| 348 return MathCosSlow(x); | |
| 349 } | |
| 350 | |
| 351 // ECMA 262 - 15.8.2.18 | |
| 352 function MathTan(x) { | |
| 353 x = x * 1; // Convert to number. | |
| 354 if ((%_DoubleHi(x) & 0x7fffffff) <= 0x3fe921fb) { | |
| 355 // |x| < pi/4, approximately. No reduction needed. | |
| 356 return KernelTan(x, 0, 1); | |
| 357 } | |
| 358 REMPIO2(x); | |
| 359 return KernelTan(y0, y1, (n & 1) ? -1 : 1); | |
| 360 } | |
| 361 | |
| 362 // ES6 draft 09-27-13, section 20.2.2.20. | |
| 363 // Math.log1p | |
| 364 // | |
| 365 // Method : | |
| 366 // 1. Argument Reduction: find k and f such that | |
| 367 // 1+x = 2^k * (1+f), | |
| 368 // where sqrt(2)/2 < 1+f < sqrt(2) . | |
| 369 // | |
| 370 // Note. If k=0, then f=x is exact. However, if k!=0, then f | |
| 371 // may not be representable exactly. In that case, a correction | |
| 372 // term is need. Let u=1+x rounded. Let c = (1+x)-u, then | |
| 373 // log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u), | |
| 374 // and add back the correction term c/u. | |
| 375 // (Note: when x > 2**53, one can simply return log(x)) | |
| 376 // | |
| 377 // 2. Approximation of log1p(f). | |
| 378 // Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) | |
| 379 // = 2s + 2/3 s**3 + 2/5 s**5 + ....., | |
| 380 // = 2s + s*R | |
| 381 // We use a special Reme algorithm on [0,0.1716] to generate | |
| 382 // a polynomial of degree 14 to approximate R The maximum error | |
| 383 // of this polynomial approximation is bounded by 2**-58.45. In | |
| 384 // other words, | |
| 385 // 2 4 6 8 10 12 14 | |
| 386 // R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s | |
| 387 // (the values of Lp1 to Lp7 are listed in the program) | |
| 388 // and | |
| 389 // | 2 14 | -58.45 | |
| 390 // | Lp1*s +...+Lp7*s - R(z) | <= 2 | |
| 391 // | | | |
| 392 // Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. | |
| 393 // In order to guarantee error in log below 1ulp, we compute log | |
| 394 // by | |
| 395 // log1p(f) = f - (hfsq - s*(hfsq+R)). | |
| 396 // | |
| 397 // 3. Finally, log1p(x) = k*ln2 + log1p(f). | |
| 398 // = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) | |
| 399 // Here ln2 is split into two floating point number: | |
| 400 // ln2_hi + ln2_lo, | |
| 401 // where n*ln2_hi is always exact for |n| < 2000. | |
| 402 // | |
| 403 // Special cases: | |
| 404 // log1p(x) is NaN with signal if x < -1 (including -INF) ; | |
| 405 // log1p(+INF) is +INF; log1p(-1) is -INF with signal; | |
| 406 // log1p(NaN) is that NaN with no signal. | |
| 407 // | |
| 408 // Accuracy: | |
| 409 // according to an error analysis, the error is always less than | |
| 410 // 1 ulp (unit in the last place). | |
| 411 // | |
| 412 // Constants: | |
| 413 // Constants are found in fdlibm.cc. We assume the C++ compiler to convert | |
| 414 // from decimal to binary accurately enough to produce the intended values. | |
| 415 // | |
| 416 // Note: Assuming log() return accurate answer, the following | |
| 417 // algorithm can be used to compute log1p(x) to within a few ULP: | |
| 418 // | |
| 419 // u = 1+x; | |
| 420 // if (u==1.0) return x ; else | |
| 421 // return log(u)*(x/(u-1.0)); | |
| 422 // | |
| 423 // See HP-15C Advanced Functions Handbook, p.193. | |
| 424 // | |
| 425 const LN2_HI = kMath[34]; | |
| 426 const LN2_LO = kMath[35]; | |
| 427 const TWO54 = kMath[36]; | |
| 428 const TWO_THIRD = kMath[37]; | |
| 429 macro KLOG1P(x) | |
| 430 (kMath[38+x]) | |
| 431 endmacro | |
| 432 | |
| 433 function MathLog1p(x) { | |
| 434 x = x * 1; // Convert to number. | |
| 435 var hx = %_DoubleHi(x); | |
| 436 var ax = hx & 0x7fffffff; | |
| 437 var k = 1; | |
| 438 var f = x; | |
| 439 var hu = 1; | |
| 440 var c = 0; | |
| 441 var u = x; | |
| 442 | |
| 443 if (hx < 0x3fda827a) { | |
| 444 // x < 0.41422 | |
| 445 if (ax >= 0x3ff00000) { // |x| >= 1 | |
| 446 if (x === -1) { | |
| 447 return -INFINITY; // log1p(-1) = -inf | |
| 448 } else { | |
| 449 return NAN; // log1p(x<-1) = NaN | |
| 450 } | |
| 451 } else if (ax < 0x3c900000) { | |
| 452 // For |x| < 2^-54 we can return x. | |
| 453 return x; | |
| 454 } else if (ax < 0x3e200000) { | |
| 455 // For |x| < 2^-29 we can use a simple two-term Taylor series. | |
| 456 return x - x * x * 0.5; | |
| 457 } | |
| 458 | |
| 459 if ((hx > 0) || (hx <= -0x402D413D)) { // (int) 0xbfd2bec3 = -0x402d413d | |
| 460 // -.2929 < x < 0.41422 | |
| 461 k = 0; | |
| 462 } | |
| 463 } | |
| 464 | |
| 465 // Handle Infinity and NAN | |
| 466 if (hx >= 0x7ff00000) return x; | |
| 467 | |
| 468 if (k !== 0) { | |
| 469 if (hx < 0x43400000) { | |
| 470 // x < 2^53 | |
| 471 u = 1 + x; | |
| 472 hu = %_DoubleHi(u); | |
| 473 k = (hu >> 20) - 1023; | |
| 474 c = (k > 0) ? 1 - (u - x) : x - (u - 1); | |
| 475 c = c / u; | |
| 476 } else { | |
| 477 hu = %_DoubleHi(u); | |
| 478 k = (hu >> 20) - 1023; | |
| 479 } | |
| 480 hu = hu & 0xfffff; | |
| 481 if (hu < 0x6a09e) { | |
| 482 u = %_ConstructDouble(hu | 0x3ff00000, %_DoubleLo(u)); // Normalize u. | |
| 483 } else { | |
| 484 ++k; | |
| 485 u = %_ConstructDouble(hu | 0x3fe00000, %_DoubleLo(u)); // Normalize u/2. | |
| 486 hu = (0x00100000 - hu) >> 2; | |
| 487 } | |
| 488 f = u - 1; | |
| 489 } | |
| 490 | |
| 491 var hfsq = 0.5 * f * f; | |
| 492 if (hu === 0) { | |
| 493 // |f| < 2^-20; | |
| 494 if (f === 0) { | |
| 495 if (k === 0) { | |
| 496 return 0.0; | |
| 497 } else { | |
| 498 return k * LN2_HI + (c + k * LN2_LO); | |
| 499 } | |
| 500 } | |
| 501 var R = hfsq * (1 - TWO_THIRD * f); | |
| 502 if (k === 0) { | |
| 503 return f - R; | |
| 504 } else { | |
| 505 return k * LN2_HI - ((R - (k * LN2_LO + c)) - f); | |
| 506 } | |
| 507 } | |
| 508 | |
| 509 var s = f / (2 + f); | |
| 510 var z = s * s; | |
| 511 var R = z * (KLOG1P(0) + z * (KLOG1P(1) + z * | |
| 512 (KLOG1P(2) + z * (KLOG1P(3) + z * | |
| 513 (KLOG1P(4) + z * (KLOG1P(5) + z * KLOG1P(6))))))); | |
| 514 if (k === 0) { | |
| 515 return f - (hfsq - s * (hfsq + R)); | |
| 516 } else { | |
| 517 return k * LN2_HI - ((hfsq - (s * (hfsq + R) + (k * LN2_LO + c))) - f); | |
| 518 } | |
| 519 } | |
| 520 | |
| 521 // ES6 draft 09-27-13, section 20.2.2.14. | |
| 522 // Math.expm1 | |
| 523 // Returns exp(x)-1, the exponential of x minus 1. | |
| 524 // | |
| 525 // Method | |
| 526 // 1. Argument reduction: | |
| 527 // Given x, find r and integer k such that | |
| 528 // | |
| 529 // x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658 | |
| 530 // | |
| 531 // Here a correction term c will be computed to compensate | |
| 532 // the error in r when rounded to a floating-point number. | |
| 533 // | |
| 534 // 2. Approximating expm1(r) by a special rational function on | |
| 535 // the interval [0,0.34658]: | |
| 536 // Since | |
| 537 // r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ... | |
| 538 // we define R1(r*r) by | |
| 539 // r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r) | |
| 540 // That is, | |
| 541 // R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r) | |
| 542 // = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r)) | |
| 543 // = 1 - r^2/60 + r^4/2520 - r^6/100800 + ... | |
| 544 // We use a special Remes algorithm on [0,0.347] to generate | |
| 545 // a polynomial of degree 5 in r*r to approximate R1. The | |
| 546 // maximum error of this polynomial approximation is bounded | |
| 547 // by 2**-61. In other words, | |
| 548 // R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5 | |
| 549 // where Q1 = -1.6666666666666567384E-2, | |
| 550 // Q2 = 3.9682539681370365873E-4, | |
| 551 // Q3 = -9.9206344733435987357E-6, | |
| 552 // Q4 = 2.5051361420808517002E-7, | |
| 553 // Q5 = -6.2843505682382617102E-9; | |
| 554 // (where z=r*r, and the values of Q1 to Q5 are listed below) | |
| 555 // with error bounded by | |
| 556 // | 5 | -61 | |
| 557 // | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2 | |
| 558 // | | | |
| 559 // | |
| 560 // expm1(r) = exp(r)-1 is then computed by the following | |
| 561 // specific way which minimize the accumulation rounding error: | |
| 562 // 2 3 | |
| 563 // r r [ 3 - (R1 + R1*r/2) ] | |
| 564 // expm1(r) = r + --- + --- * [--------------------] | |
| 565 // 2 2 [ 6 - r*(3 - R1*r/2) ] | |
| 566 // | |
| 567 // To compensate the error in the argument reduction, we use | |
| 568 // expm1(r+c) = expm1(r) + c + expm1(r)*c | |
| 569 // ~ expm1(r) + c + r*c | |
| 570 // Thus c+r*c will be added in as the correction terms for | |
| 571 // expm1(r+c). Now rearrange the term to avoid optimization | |
| 572 // screw up: | |
| 573 // ( 2 2 ) | |
| 574 // ({ ( r [ R1 - (3 - R1*r/2) ] ) } r ) | |
| 575 // expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- ) | |
| 576 // ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 ) | |
| 577 // ( ) | |
| 578 // | |
| 579 // = r - E | |
| 580 // 3. Scale back to obtain expm1(x): | |
| 581 // From step 1, we have | |
| 582 // expm1(x) = either 2^k*[expm1(r)+1] - 1 | |
| 583 // = or 2^k*[expm1(r) + (1-2^-k)] | |
| 584 // 4. Implementation notes: | |
| 585 // (A). To save one multiplication, we scale the coefficient Qi | |
| 586 // to Qi*2^i, and replace z by (x^2)/2. | |
| 587 // (B). To achieve maximum accuracy, we compute expm1(x) by | |
| 588 // (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf) | |
| 589 // (ii) if k=0, return r-E | |
| 590 // (iii) if k=-1, return 0.5*(r-E)-0.5 | |
| 591 // (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E) | |
| 592 // else return 1.0+2.0*(r-E); | |
| 593 // (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1) | |
| 594 // (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else | |
| 595 // (vii) return 2^k(1-((E+2^-k)-r)) | |
| 596 // | |
| 597 // Special cases: | |
| 598 // expm1(INF) is INF, expm1(NaN) is NaN; | |
| 599 // expm1(-INF) is -1, and | |
| 600 // for finite argument, only expm1(0)=0 is exact. | |
| 601 // | |
| 602 // Accuracy: | |
| 603 // according to an error analysis, the error is always less than | |
| 604 // 1 ulp (unit in the last place). | |
| 605 // | |
| 606 // Misc. info. | |
| 607 // For IEEE double | |
| 608 // if x > 7.09782712893383973096e+02 then expm1(x) overflow | |
| 609 // | |
| 610 const KEXPM1_OVERFLOW = kMath[45]; | |
| 611 const INVLN2 = kMath[46]; | |
| 612 macro KEXPM1(x) | |
| 613 (kMath[47+x]) | |
| 614 endmacro | |
| 615 | |
| 616 function MathExpm1(x) { | |
| 617 x = x * 1; // Convert to number. | |
| 618 var y; | |
| 619 var hi; | |
| 620 var lo; | |
| 621 var k; | |
| 622 var t; | |
| 623 var c; | |
| 624 | |
| 625 var hx = %_DoubleHi(x); | |
| 626 var xsb = hx & 0x80000000; // Sign bit of x | |
| 627 var y = (xsb === 0) ? x : -x; // y = |x| | |
| 628 hx &= 0x7fffffff; // High word of |x| | |
| 629 | |
| 630 // Filter out huge and non-finite argument | |
| 631 if (hx >= 0x4043687a) { // if |x| ~=> 56 * ln2 | |
| 632 if (hx >= 0x40862e42) { // if |x| >= 709.78 | |
| 633 if (hx >= 0x7ff00000) { | |
| 634 // expm1(inf) = inf; expm1(-inf) = -1; expm1(nan) = nan; | |
| 635 return (x === -INFINITY) ? -1 : x; | |
| 636 } | |
| 637 if (x > KEXPM1_OVERFLOW) return INFINITY; // Overflow | |
| 638 } | |
| 639 if (xsb != 0) return -1; // x < -56 * ln2, return -1. | |
| 640 } | |
| 641 | |
| 642 // Argument reduction | |
| 643 if (hx > 0x3fd62e42) { // if |x| > 0.5 * ln2 | |
| 644 if (hx < 0x3ff0a2b2) { // and |x| < 1.5 * ln2 | |
| 645 if (xsb === 0) { | |
| 646 hi = x - LN2_HI; | |
| 647 lo = LN2_LO; | |
| 648 k = 1; | |
| 649 } else { | |
| 650 hi = x + LN2_HI; | |
| 651 lo = -LN2_LO; | |
| 652 k = -1; | |
| 653 } | |
| 654 } else { | |
| 655 k = (INVLN2 * x + ((xsb === 0) ? 0.5 : -0.5)) | 0; | |
| 656 t = k; | |
| 657 // t * ln2_hi is exact here. | |
| 658 hi = x - t * LN2_HI; | |
| 659 lo = t * LN2_LO; | |
| 660 } | |
| 661 x = hi - lo; | |
| 662 c = (hi - x) - lo; | |
| 663 } else if (hx < 0x3c900000) { | |
| 664 // When |x| < 2^-54, we can return x. | |
| 665 return x; | |
| 666 } else { | |
| 667 // Fall through. | |
| 668 k = 0; | |
| 669 } | |
| 670 | |
| 671 // x is now in primary range | |
| 672 var hfx = 0.5 * x; | |
| 673 var hxs = x * hfx; | |
| 674 var r1 = 1 + hxs * (KEXPM1(0) + hxs * (KEXPM1(1) + hxs * | |
| 675 (KEXPM1(2) + hxs * (KEXPM1(3) + hxs * KEXPM1(4))))); | |
| 676 t = 3 - r1 * hfx; | |
| 677 var e = hxs * ((r1 - t) / (6 - x * t)); | |
| 678 if (k === 0) { // c is 0 | |
| 679 return x - (x*e - hxs); | |
| 680 } else { | |
| 681 e = (x * (e - c) - c); | |
| 682 e -= hxs; | |
| 683 if (k === -1) return 0.5 * (x - e) - 0.5; | |
| 684 if (k === 1) { | |
| 685 if (x < -0.25) return -2 * (e - (x + 0.5)); | |
| 686 return 1 + 2 * (x - e); | |
| 687 } | |
| 688 | |
| 689 if (k <= -2 || k > 56) { | |
| 690 // suffice to return exp(x) + 1 | |
| 691 y = 1 - (e - x); | |
| 692 // Add k to y's exponent | |
| 693 y = %_ConstructDouble(%_DoubleHi(y) + (k << 20), %_DoubleLo(y)); | |
| 694 return y - 1; | |
| 695 } | |
| 696 if (k < 20) { | |
| 697 // t = 1 - 2^k | |
| 698 t = %_ConstructDouble(0x3ff00000 - (0x200000 >> k), 0); | |
| 699 y = t - (e - x); | |
| 700 // Add k to y's exponent | |
| 701 y = %_ConstructDouble(%_DoubleHi(y) + (k << 20), %_DoubleLo(y)); | |
| 702 } else { | |
| 703 // t = 2^-k | |
| 704 t = %_ConstructDouble((0x3ff - k) << 20, 0); | |
| 705 y = x - (e + t); | |
| 706 y += 1; | |
| 707 // Add k to y's exponent | |
| 708 y = %_ConstructDouble(%_DoubleHi(y) + (k << 20), %_DoubleLo(y)); | |
| 709 } | |
| 710 } | |
| 711 return y; | |
| 712 } | |
| 713 | |
| 714 | |
| 715 // ES6 draft 09-27-13, section 20.2.2.30. | |
| 716 // Math.sinh | |
| 717 // Method : | |
| 718 // mathematically sinh(x) if defined to be (exp(x)-exp(-x))/2 | |
| 719 // 1. Replace x by |x| (sinh(-x) = -sinh(x)). | |
| 720 // 2. | |
| 721 // E + E/(E+1) | |
| 722 // 0 <= x <= 22 : sinh(x) := --------------, E=expm1(x) | |
| 723 // 2 | |
| 724 // | |
| 725 // 22 <= x <= lnovft : sinh(x) := exp(x)/2 | |
| 726 // lnovft <= x <= ln2ovft: sinh(x) := exp(x/2)/2 * exp(x/2) | |
| 727 // ln2ovft < x : sinh(x) := x*shuge (overflow) | |
| 728 // | |
| 729 // Special cases: | |
| 730 // sinh(x) is |x| if x is +Infinity, -Infinity, or NaN. | |
| 731 // only sinh(0)=0 is exact for finite x. | |
| 732 // | |
| 733 const KSINH_OVERFLOW = kMath[52]; | |
| 734 const TWO_M28 = 3.725290298461914e-9; // 2^-28, empty lower half | |
| 735 const LOG_MAXD = 709.7822265625; // 0x40862e42 00000000, empty lower half | |
| 736 | |
| 737 function MathSinh(x) { | |
| 738 x = x * 1; // Convert to number. | |
| 739 var h = (x < 0) ? -0.5 : 0.5; | |
| 740 // |x| in [0, 22]. return sign(x)*0.5*(E+E/(E+1)) | |
| 741 var ax = MathAbs(x); | |
| 742 if (ax < 22) { | |
| 743 // For |x| < 2^-28, sinh(x) = x | |
| 744 if (ax < TWO_M28) return x; | |
| 745 var t = MathExpm1(ax); | |
| 746 if (ax < 1) return h * (2 * t - t * t / (t + 1)); | |
| 747 return h * (t + t / (t + 1)); | |
| 748 } | |
| 749 // |x| in [22, log(maxdouble)], return 0.5 * exp(|x|) | |
| 750 if (ax < LOG_MAXD) return h * MathExp(ax); | |
| 751 // |x| in [log(maxdouble), overflowthreshold] | |
| 752 // overflowthreshold = 710.4758600739426 | |
| 753 if (ax <= KSINH_OVERFLOW) { | |
| 754 var w = MathExp(0.5 * ax); | |
| 755 var t = h * w; | |
| 756 return t * w; | |
| 757 } | |
| 758 // |x| > overflowthreshold or is NaN. | |
| 759 // Return Infinity of the appropriate sign or NaN. | |
| 760 return x * INFINITY; | |
| 761 } | |
| 762 | |
| 763 | |
| 764 // ES6 draft 09-27-13, section 20.2.2.12. | |
| 765 // Math.cosh | |
| 766 // Method : | |
| 767 // mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2 | |
| 768 // 1. Replace x by |x| (cosh(x) = cosh(-x)). | |
| 769 // 2. | |
| 770 // [ exp(x) - 1 ]^2 | |
| 771 // 0 <= x <= ln2/2 : cosh(x) := 1 + ------------------- | |
| 772 // 2*exp(x) | |
| 773 // | |
| 774 // exp(x) + 1/exp(x) | |
| 775 // ln2/2 <= x <= 22 : cosh(x) := ------------------- | |
| 776 // 2 | |
| 777 // 22 <= x <= lnovft : cosh(x) := exp(x)/2 | |
| 778 // lnovft <= x <= ln2ovft: cosh(x) := exp(x/2)/2 * exp(x/2) | |
| 779 // ln2ovft < x : cosh(x) := huge*huge (overflow) | |
| 780 // | |
| 781 // Special cases: | |
| 782 // cosh(x) is |x| if x is +INF, -INF, or NaN. | |
| 783 // only cosh(0)=1 is exact for finite x. | |
| 784 // | |
| 785 const KCOSH_OVERFLOW = kMath[52]; | |
| 786 | |
| 787 function MathCosh(x) { | |
| 788 x = x * 1; // Convert to number. | |
| 789 var ix = %_DoubleHi(x) & 0x7fffffff; | |
| 790 // |x| in [0,0.5*log2], return 1+expm1(|x|)^2/(2*exp(|x|)) | |
| 791 if (ix < 0x3fd62e43) { | |
| 792 var t = MathExpm1(MathAbs(x)); | |
| 793 var w = 1 + t; | |
| 794 // For |x| < 2^-55, cosh(x) = 1 | |
| 795 if (ix < 0x3c800000) return w; | |
| 796 return 1 + (t * t) / (w + w); | |
| 797 } | |
| 798 // |x| in [0.5*log2, 22], return (exp(|x|)+1/exp(|x|)/2 | |
| 799 if (ix < 0x40360000) { | |
| 800 var t = MathExp(MathAbs(x)); | |
| 801 return 0.5 * t + 0.5 / t; | |
| 802 } | |
| 803 // |x| in [22, log(maxdouble)], return half*exp(|x|) | |
| 804 if (ix < 0x40862e42) return 0.5 * MathExp(MathAbs(x)); | |
| 805 // |x| in [log(maxdouble), overflowthreshold] | |
| 806 if (MathAbs(x) <= KCOSH_OVERFLOW) { | |
| 807 var w = MathExp(0.5 * MathAbs(x)); | |
| 808 var t = 0.5 * w; | |
| 809 return t * w; | |
| 810 } | |
| 811 if (NUMBER_IS_NAN(x)) return x; | |
| 812 // |x| > overflowthreshold. | |
| 813 return INFINITY; | |
| 814 } | |
| OLD | NEW |