1 // The following is adapted from fdlibm (http://www.netlib.org/fdlibm),
3 // ====================================================
4 // Copyright (C) 1993-2004 by Sun Microsystems, Inc. All rights reserved.
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
10 // ====================================================
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.
16 // The following is a straightforward translation of fdlibm routines
17 // by Raymond Toy (rtoy@google.com).
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 // rempio2result is used as a container for return values of %RemPiO2. It is
24 // initialized to a two-element Float64Array during genesis.
31 const INVPIO2 = kMath[0];
32 const PIO2_1 = kMath[1];
33 const PIO2_1T = kMath[2];
34 const PIO2_2 = kMath[3];
35 const PIO2_2T = kMath[4];
36 const PIO2_3 = kMath[5];
37 const PIO2_3T = kMath[6];
38 const PIO4 = kMath[32];
39 const PIO4LO = kMath[33];
41 // Compute k and r such that x - k*pi/2 = r where |r| < pi/4. For
42 // precision, r is returned as two values y0 and y1 such that r = y0 + y1
43 // to more than double precision.
47 var hx = %_DoubleHi(X);
48 var ix = hx & 0x7fffffff;
50 if (ix < 0x4002d97c) {
51 // |X| ~< 3*pi/4, special case with n = +/- 1
54 if (ix != 0x3ff921fb) {
55 // 33+53 bit pi is good enough
57 y1 = (z - y0) - PIO2_1T;
59 // near pi/2, use 33+33+53 bit pi
62 y1 = (z - y0) - PIO2_2T;
68 if (ix != 0x3ff921fb) {
69 // 33+53 bit pi is good enough
71 y1 = (z - y0) + PIO2_1T;
73 // near pi/2, use 33+33+53 bit pi
76 y1 = (z - y0) + PIO2_2T;
80 } else if (ix <= 0x413921fb) {
81 // |X| ~<= 2^19*(pi/2), medium size
83 n = (t * INVPIO2 + 0.5) | 0;
84 var r = t - n * PIO2_1;
86 // First round good to 85 bit
88 if (ix - (%_DoubleHi(y0) & 0x7ff00000) > 0x1000000) {
89 // 2nd iteration needed, good to 118
93 w = n * PIO2_2T - ((t - r) - w);
95 if (ix - (%_DoubleHi(y0) & 0x7ff00000) > 0x3100000) {
96 // 3rd iteration needed. 151 bits accuracy
100 w = n * PIO2_3T - ((t - r) - w);
111 // Need to do full Payne-Hanek reduction here.
112 n = %RemPiO2(X, rempio2result);
113 y0 = rempio2result[0];
114 y1 = rempio2result[1];
119 // __kernel_sin(X, Y, IY)
120 // kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854
121 // Input X is assumed to be bounded by ~pi/4 in magnitude.
122 // Input Y is the tail of X so that x = X + Y.
125 // 1. Since ieee_sin(-x) = -ieee_sin(x), we need only to consider positive x.
126 // 2. ieee_sin(x) is approximated by a polynomial of degree 13 on
129 // sin(x) ~ x + S1*x + ... + S6*x
132 // |ieee_sin(x) 2 4 6 8 10 12 | -58
133 // |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2
136 // 3. ieee_sin(X+Y) = ieee_sin(X) + sin'(X')*Y
137 // ~ ieee_sin(X) + (1-X*X/2)*Y
138 // For better accuracy, let
140 // r = X *(S2+X *(S3+X *(S4+X *(S5+X *S6))))
142 // sin(x) = X + (S1*X + (X *(r-Y/2)+Y))
148 macro RETURN_KERNELSIN(X, Y, SIGN)
151 var r = KSIN(1) + z * (KSIN(2) + z * (KSIN(3) +
152 z * (KSIN(4) + z * KSIN(5))));
153 return (X - ((z * (0.5 * Y - v * r) - Y) - v * KSIN(0))) SIGN;
156 // __kernel_cos(X, Y)
157 // kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
158 // Input X is assumed to be bounded by ~pi/4 in magnitude.
159 // Input Y is the tail of X so that x = X + Y.
162 // 1. Since ieee_cos(-x) = ieee_cos(x), we need only to consider positive x.
163 // 2. ieee_cos(x) is approximated by a polynomial of degree 14 on
166 // cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x
167 // where the remez error is
169 // | 2 4 6 8 10 12 14 | -58
170 // |ieee_cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x +C6*x )| <= 2
174 // 3. let r = C1*x +C2*x +C3*x +C4*x +C5*x +C6*x , then
175 // ieee_cos(x) = 1 - x*x/2 + r
176 // since ieee_cos(X+Y) ~ ieee_cos(X) - ieee_sin(X)*Y
177 // ~ ieee_cos(X) - X*Y,
178 // a correction term is necessary in ieee_cos(x) and hence
179 // cos(X+Y) = 1 - (X*X/2 - (r - X*Y))
180 // For better accuracy when x > 0.3, let qx = |x|/4 with
181 // the last 32 bits mask off, and if x > 0.78125, let qx = 0.28125.
183 // cos(X+Y) = (1-qx) - ((X*X/2-qx) - (r-X*Y)).
184 // Note that 1-qx and (X*X/2-qx) is EXACT here, and the
185 // magnitude of the latter is at least a quarter of X*X/2,
186 // thus, reducing the rounding error in the subtraction.
192 macro RETURN_KERNELCOS(X, Y, SIGN)
193 var ix = %_DoubleHi(X) & 0x7fffffff;
195 var r = z * (KCOS(0) + z * (KCOS(1) + z * (KCOS(2)+
196 z * (KCOS(3) + z * (KCOS(4) + z * KCOS(5))))));
197 if (ix < 0x3fd33333) { // |x| ~< 0.3
198 return (1 - (0.5 * z - (z * r - X * Y))) SIGN;
201 if (ix > 0x3fe90000) { // |x| > 0.78125
204 qx = %_ConstructDouble(%_DoubleHi(0.25 * X), 0);
206 var hz = 0.5 * z - qx;
207 return (1 - qx - (hz - (z * r - X * Y))) SIGN;
212 // kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854
213 // Input x is assumed to be bounded by ~pi/4 in magnitude.
214 // Input y is the tail of x.
215 // Input k indicates whether ieee_tan (if k = 1) or -1/tan (if k = -1)
219 // 1. Since ieee_tan(-x) = -ieee_tan(x), we need only to consider positive x.
220 // 2. if x < 2^-28 (hx<0x3e300000 0), return x with inexact if x!=0.
221 // 3. ieee_tan(x) is approximated by a odd polynomial of degree 27 on
224 // tan(x) ~ x + T1*x + ... + T13*x
227 // |ieee_tan(x) 2 4 26 | -59.2
228 // |----- - (1+T1*x +T2*x +.... +T13*x )| <= 2
231 // Note: ieee_tan(x+y) = ieee_tan(x) + tan'(x)*y
232 // ~ ieee_tan(x) + (1+x*x)*y
233 // Therefore, for better accuracy in computing ieee_tan(x+y), let
235 // r = x *(T2+x *(T3+x *(...+x *(T12+x *T13))))
238 // tan(x+y) = x + (T1*x + (x *(r+y)+y))
240 // 4. For x in [0.67434,pi/4], let y = pi/4 - x, then
241 // tan(x) = ieee_tan(pi/4-y) = (1-ieee_tan(y))/(1+ieee_tan(y))
242 // = 1 - 2*(ieee_tan(y) - (ieee_tan(y)^2)/(1+ieee_tan(y)))
244 // Set returnTan to 1 for tan; -1 for cot. Anything else is illegal
245 // and will cause incorrect results.
251 function KernelTan(x, y, returnTan) {
254 var hx = %_DoubleHi(x);
255 var ix = hx & 0x7fffffff;
257 if (ix < 0x3e300000) { // |x| < 2^-28
258 if (((ix | %_DoubleLo(x)) | (returnTan + 1)) == 0) {
259 // x == 0 && returnTan = -1
260 return 1 / MathAbs(x);
262 if (returnTan == 1) {
265 // Compute -1/(x + y) carefully
267 var z = %_ConstructDouble(%_DoubleHi(w), 0);
270 var t = %_ConstructDouble(%_DoubleHi(a), 0);
272 return t + a * (s + t * v);
276 if (ix >= 0x3fe59428) { // |x| > .6744
289 // Break x^5 * (T1 + x^2*T2 + ...) into
290 // x^5 * (T1 + x^4*T3 + ... + x^20*T11) +
291 // x^5 * (x^2 * (T2 + x^4*T4 + ... + x^22*T12))
292 var r = KTAN(1) + w * (KTAN(3) + w * (KTAN(5) +
293 w * (KTAN(7) + w * (KTAN(9) + w * KTAN(11)))));
294 var v = z * (KTAN(2) + w * (KTAN(4) + w * (KTAN(6) +
295 w * (KTAN(8) + w * (KTAN(10) + w * KTAN(12))))));
297 r = y + z * (s * (r + v) + y);
300 if (ix >= 0x3fe59428) {
301 return (1 - ((hx >> 30) & 2)) *
302 (returnTan - 2.0 * (x - (w * w / (w + returnTan) - r)));
304 if (returnTan == 1) {
307 z = %_ConstructDouble(%_DoubleHi(w), 0);
310 var t = %_ConstructDouble(%_DoubleHi(a), 0);
312 return t + a * (s + t * v);
316 function MathSinSlow(x) {
318 var sign = 1 - (n & 2);
320 RETURN_KERNELCOS(y0, y1, * sign);
322 RETURN_KERNELSIN(y0, y1, * sign);
326 function MathCosSlow(x) {
329 var sign = (n & 2) - 1;
330 RETURN_KERNELSIN(y0, y1, * sign);
332 var sign = 1 - (n & 2);
333 RETURN_KERNELCOS(y0, y1, * sign);
337 // ECMA 262 - 15.8.2.16
338 function MathSin(x) {
339 x = x * 1; // Convert to number.
340 if ((%_DoubleHi(x) & 0x7fffffff) <= 0x3fe921fb) {
341 // |x| < pi/4, approximately. No reduction needed.
342 RETURN_KERNELSIN(x, 0, /* empty */);
344 return MathSinSlow(x);
347 // ECMA 262 - 15.8.2.7
348 function MathCos(x) {
349 x = x * 1; // Convert to number.
350 if ((%_DoubleHi(x) & 0x7fffffff) <= 0x3fe921fb) {
351 // |x| < pi/4, approximately. No reduction needed.
352 RETURN_KERNELCOS(x, 0, /* empty */);
354 return MathCosSlow(x);
357 // ECMA 262 - 15.8.2.18
358 function MathTan(x) {
359 x = x * 1; // Convert to number.
360 if ((%_DoubleHi(x) & 0x7fffffff) <= 0x3fe921fb) {
361 // |x| < pi/4, approximately. No reduction needed.
362 return KernelTan(x, 0, 1);
365 return KernelTan(y0, y1, (n & 1) ? -1 : 1);
368 // ES6 draft 09-27-13, section 20.2.2.20.
372 // 1. Argument Reduction: find k and f such that
373 // 1+x = 2^k * (1+f),
374 // where sqrt(2)/2 < 1+f < sqrt(2) .
376 // Note. If k=0, then f=x is exact. However, if k!=0, then f
377 // may not be representable exactly. In that case, a correction
378 // term is need. Let u=1+x rounded. Let c = (1+x)-u, then
379 // log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
380 // and add back the correction term c/u.
381 // (Note: when x > 2**53, one can simply return log(x))
383 // 2. Approximation of log1p(f).
384 // Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
385 // = 2s + 2/3 s**3 + 2/5 s**5 + .....,
387 // We use a special Reme algorithm on [0,0.1716] to generate
388 // a polynomial of degree 14 to approximate R The maximum error
389 // of this polynomial approximation is bounded by 2**-58.45. In
392 // R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s
393 // (the values of Lp1 to Lp7 are listed in the program)
396 // | Lp1*s +...+Lp7*s - R(z) | <= 2
398 // Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
399 // In order to guarantee error in log below 1ulp, we compute log
401 // log1p(f) = f - (hfsq - s*(hfsq+R)).
403 // 3. Finally, log1p(x) = k*ln2 + log1p(f).
404 // = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
405 // Here ln2 is split into two floating point number:
407 // where n*ln2_hi is always exact for |n| < 2000.
410 // log1p(x) is NaN with signal if x < -1 (including -INF) ;
411 // log1p(+INF) is +INF; log1p(-1) is -INF with signal;
412 // log1p(NaN) is that NaN with no signal.
415 // according to an error analysis, the error is always less than
416 // 1 ulp (unit in the last place).
419 // Constants are found in fdlibm.cc. We assume the C++ compiler to convert
420 // from decimal to binary accurately enough to produce the intended values.
422 // Note: Assuming log() return accurate answer, the following
423 // algorithm can be used to compute log1p(x) to within a few ULP:
426 // if (u==1.0) return x ; else
427 // return log(u)*(x/(u-1.0));
429 // See HP-15C Advanced Functions Handbook, p.193.
431 const LN2_HI = kMath[34];
432 const LN2_LO = kMath[35];
433 const TWO_THIRD = kMath[36];
438 const TWO54 = 18014398509481984;
440 function MathLog1p(x) {
441 x = x * 1; // Convert to number.
442 var hx = %_DoubleHi(x);
443 var ax = hx & 0x7fffffff;
450 if (hx < 0x3fda827a) {
452 if (ax >= 0x3ff00000) { // |x| >= 1
454 return -INFINITY; // log1p(-1) = -inf
456 return NAN; // log1p(x<-1) = NaN
458 } else if (ax < 0x3c900000) {
459 // For |x| < 2^-54 we can return x.
461 } else if (ax < 0x3e200000) {
462 // For |x| < 2^-29 we can use a simple two-term Taylor series.
463 return x - x * x * 0.5;
466 if ((hx > 0) || (hx <= -0x402D413D)) { // (int) 0xbfd2bec3 = -0x402d413d
467 // -.2929 < x < 0.41422
472 // Handle Infinity and NAN
473 if (hx >= 0x7ff00000) return x;
476 if (hx < 0x43400000) {
480 k = (hu >> 20) - 1023;
481 c = (k > 0) ? 1 - (u - x) : x - (u - 1);
485 k = (hu >> 20) - 1023;
489 u = %_ConstructDouble(hu | 0x3ff00000, %_DoubleLo(u)); // Normalize u.
492 u = %_ConstructDouble(hu | 0x3fe00000, %_DoubleLo(u)); // Normalize u/2.
493 hu = (0x00100000 - hu) >> 2;
498 var hfsq = 0.5 * f * f;
505 return k * LN2_HI + (c + k * LN2_LO);
508 var R = hfsq * (1 - TWO_THIRD * f);
512 return k * LN2_HI - ((R - (k * LN2_LO + c)) - f);
518 var R = z * (KLOG1P(0) + z * (KLOG1P(1) + z *
519 (KLOG1P(2) + z * (KLOG1P(3) + z *
520 (KLOG1P(4) + z * (KLOG1P(5) + z * KLOG1P(6)))))));
522 return f - (hfsq - s * (hfsq + R));
524 return k * LN2_HI - ((hfsq - (s * (hfsq + R) + (k * LN2_LO + c))) - f);
528 // ES6 draft 09-27-13, section 20.2.2.14.
530 // Returns exp(x)-1, the exponential of x minus 1.
533 // 1. Argument reduction:
534 // Given x, find r and integer k such that
536 // x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658
538 // Here a correction term c will be computed to compensate
539 // the error in r when rounded to a floating-point number.
541 // 2. Approximating expm1(r) by a special rational function on
542 // the interval [0,0.34658]:
544 // r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
545 // we define R1(r*r) by
546 // r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
548 // R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
549 // = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
550 // = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
551 // We use a special Remes algorithm on [0,0.347] to generate
552 // a polynomial of degree 5 in r*r to approximate R1. The
553 // maximum error of this polynomial approximation is bounded
554 // by 2**-61. In other words,
555 // R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
556 // where Q1 = -1.6666666666666567384E-2,
557 // Q2 = 3.9682539681370365873E-4,
558 // Q3 = -9.9206344733435987357E-6,
559 // Q4 = 2.5051361420808517002E-7,
560 // Q5 = -6.2843505682382617102E-9;
561 // (where z=r*r, and the values of Q1 to Q5 are listed below)
562 // with error bounded by
564 // | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2
567 // expm1(r) = exp(r)-1 is then computed by the following
568 // specific way which minimize the accumulation rounding error:
570 // r r [ 3 - (R1 + R1*r/2) ]
571 // expm1(r) = r + --- + --- * [--------------------]
572 // 2 2 [ 6 - r*(3 - R1*r/2) ]
574 // To compensate the error in the argument reduction, we use
575 // expm1(r+c) = expm1(r) + c + expm1(r)*c
576 // ~ expm1(r) + c + r*c
577 // Thus c+r*c will be added in as the correction terms for
578 // expm1(r+c). Now rearrange the term to avoid optimization
581 // ({ ( r [ R1 - (3 - R1*r/2) ] ) } r )
582 // expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
583 // ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 )
587 // 3. Scale back to obtain expm1(x):
588 // From step 1, we have
589 // expm1(x) = either 2^k*[expm1(r)+1] - 1
590 // = or 2^k*[expm1(r) + (1-2^-k)]
591 // 4. Implementation notes:
592 // (A). To save one multiplication, we scale the coefficient Qi
593 // to Qi*2^i, and replace z by (x^2)/2.
594 // (B). To achieve maximum accuracy, we compute expm1(x) by
595 // (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
596 // (ii) if k=0, return r-E
597 // (iii) if k=-1, return 0.5*(r-E)-0.5
598 // (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E)
599 // else return 1.0+2.0*(r-E);
600 // (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
601 // (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else
602 // (vii) return 2^k(1-((E+2^-k)-r))
605 // expm1(INF) is INF, expm1(NaN) is NaN;
606 // expm1(-INF) is -1, and
607 // for finite argument, only expm1(0)=0 is exact.
610 // according to an error analysis, the error is always less than
611 // 1 ulp (unit in the last place).
615 // if x > 7.09782712893383973096e+02 then expm1(x) overflow
617 const KEXPM1_OVERFLOW = kMath[44];
618 const INVLN2 = kMath[45];
623 function MathExpm1(x) {
624 x = x * 1; // Convert to number.
632 var hx = %_DoubleHi(x);
633 var xsb = hx & 0x80000000; // Sign bit of x
634 var y = (xsb === 0) ? x : -x; // y = |x|
635 hx &= 0x7fffffff; // High word of |x|
637 // Filter out huge and non-finite argument
638 if (hx >= 0x4043687a) { // if |x| ~=> 56 * ln2
639 if (hx >= 0x40862e42) { // if |x| >= 709.78
640 if (hx >= 0x7ff00000) {
641 // expm1(inf) = inf; expm1(-inf) = -1; expm1(nan) = nan;
642 return (x === -INFINITY) ? -1 : x;
644 if (x > KEXPM1_OVERFLOW) return INFINITY; // Overflow
646 if (xsb != 0) return -1; // x < -56 * ln2, return -1.
649 // Argument reduction
650 if (hx > 0x3fd62e42) { // if |x| > 0.5 * ln2
651 if (hx < 0x3ff0a2b2) { // and |x| < 1.5 * ln2
662 k = (INVLN2 * x + ((xsb === 0) ? 0.5 : -0.5)) | 0;
664 // t * ln2_hi is exact here.
670 } else if (hx < 0x3c900000) {
671 // When |x| < 2^-54, we can return x.
678 // x is now in primary range
681 var r1 = 1 + hxs * (KEXPM1(0) + hxs * (KEXPM1(1) + hxs *
682 (KEXPM1(2) + hxs * (KEXPM1(3) + hxs * KEXPM1(4)))));
684 var e = hxs * ((r1 - t) / (6 - x * t));
685 if (k === 0) { // c is 0
686 return x - (x*e - hxs);
688 e = (x * (e - c) - c);
690 if (k === -1) return 0.5 * (x - e) - 0.5;
692 if (x < -0.25) return -2 * (e - (x + 0.5));
693 return 1 + 2 * (x - e);
696 if (k <= -2 || k > 56) {
697 // suffice to return exp(x) + 1
699 // Add k to y's exponent
700 y = %_ConstructDouble(%_DoubleHi(y) + (k << 20), %_DoubleLo(y));
705 t = %_ConstructDouble(0x3ff00000 - (0x200000 >> k), 0);
707 // Add k to y's exponent
708 y = %_ConstructDouble(%_DoubleHi(y) + (k << 20), %_DoubleLo(y));
711 t = %_ConstructDouble((0x3ff - k) << 20, 0);
714 // Add k to y's exponent
715 y = %_ConstructDouble(%_DoubleHi(y) + (k << 20), %_DoubleLo(y));
722 // ES6 draft 09-27-13, section 20.2.2.30.
725 // mathematically sinh(x) if defined to be (exp(x)-exp(-x))/2
726 // 1. Replace x by |x| (sinh(-x) = -sinh(x)).
729 // 0 <= x <= 22 : sinh(x) := --------------, E=expm1(x)
732 // 22 <= x <= lnovft : sinh(x) := exp(x)/2
733 // lnovft <= x <= ln2ovft: sinh(x) := exp(x/2)/2 * exp(x/2)
734 // ln2ovft < x : sinh(x) := x*shuge (overflow)
737 // sinh(x) is |x| if x is +Infinity, -Infinity, or NaN.
738 // only sinh(0)=0 is exact for finite x.
740 const KSINH_OVERFLOW = kMath[51];
741 const TWO_M28 = 3.725290298461914e-9; // 2^-28, empty lower half
742 const LOG_MAXD = 709.7822265625; // 0x40862e42 00000000, empty lower half
744 function MathSinh(x) {
745 x = x * 1; // Convert to number.
746 var h = (x < 0) ? -0.5 : 0.5;
747 // |x| in [0, 22]. return sign(x)*0.5*(E+E/(E+1))
750 // For |x| < 2^-28, sinh(x) = x
751 if (ax < TWO_M28) return x;
752 var t = MathExpm1(ax);
753 if (ax < 1) return h * (2 * t - t * t / (t + 1));
754 return h * (t + t / (t + 1));
756 // |x| in [22, log(maxdouble)], return 0.5 * exp(|x|)
757 if (ax < LOG_MAXD) return h * MathExp(ax);
758 // |x| in [log(maxdouble), overflowthreshold]
759 // overflowthreshold = 710.4758600739426
760 if (ax <= KSINH_OVERFLOW) {
761 var w = MathExp(0.5 * ax);
765 // |x| > overflowthreshold or is NaN.
766 // Return Infinity of the appropriate sign or NaN.
771 // ES6 draft 09-27-13, section 20.2.2.12.
774 // mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2
775 // 1. Replace x by |x| (cosh(x) = cosh(-x)).
778 // 0 <= x <= ln2/2 : cosh(x) := 1 + -------------------
782 // ln2/2 <= x <= 22 : cosh(x) := -------------------
784 // 22 <= x <= lnovft : cosh(x) := exp(x)/2
785 // lnovft <= x <= ln2ovft: cosh(x) := exp(x/2)/2 * exp(x/2)
786 // ln2ovft < x : cosh(x) := huge*huge (overflow)
789 // cosh(x) is |x| if x is +INF, -INF, or NaN.
790 // only cosh(0)=1 is exact for finite x.
792 const KCOSH_OVERFLOW = kMath[51];
794 function MathCosh(x) {
795 x = x * 1; // Convert to number.
796 var ix = %_DoubleHi(x) & 0x7fffffff;
797 // |x| in [0,0.5*log2], return 1+expm1(|x|)^2/(2*exp(|x|))
798 if (ix < 0x3fd62e43) {
799 var t = MathExpm1(MathAbs(x));
801 // For |x| < 2^-55, cosh(x) = 1
802 if (ix < 0x3c800000) return w;
803 return 1 + (t * t) / (w + w);
805 // |x| in [0.5*log2, 22], return (exp(|x|)+1/exp(|x|)/2
806 if (ix < 0x40360000) {
807 var t = MathExp(MathAbs(x));
808 return 0.5 * t + 0.5 / t;
810 // |x| in [22, log(maxdouble)], return half*exp(|x|)
811 if (ix < 0x40862e42) return 0.5 * MathExp(MathAbs(x));
812 // |x| in [log(maxdouble), overflowthreshold]
813 if (MathAbs(x) <= KCOSH_OVERFLOW) {
814 var w = MathExp(0.5 * MathAbs(x));
818 if (NUMBER_IS_NAN(x)) return x;
819 // |x| > overflowthreshold.
823 // ES6 draft 09-27-13, section 20.2.2.21.
824 // Return the base 10 logarithm of x
827 // Let log10_2hi = leading 40 bits of log10(2) and
828 // log10_2lo = log10(2) - log10_2hi,
829 // ivln10 = 1/log(10) rounded.
834 // log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))
837 // To guarantee log10(10**n)=n, where 10**n is normal, the rounding
838 // mode must set to Round-to-Nearest.
840 // [1/log(10)] rounded to 53 bits has error .198 ulps;
841 // log10 is monotonic at all binary break points.
844 // log10(x) is NaN if x < 0;
845 // log10(+INF) is +INF; log10(0) is -INF;
846 // log10(NaN) is that NaN;
847 // log10(10**N) = N for N=0,1,...,22.
850 const IVLN10 = kMath[52];
851 const LOG10_2HI = kMath[53];
852 const LOG10_2LO = kMath[54];
854 function MathLog10(x) {
855 x = x * 1; // Convert to number.
856 var hx = %_DoubleHi(x);
857 var lx = %_DoubleLo(x);
860 if (hx < 0x00100000) {
862 // log10(+/- 0) = -Infinity.
863 if (((hx & 0x7fffffff) | lx) === 0) return -INFINITY;
864 // log10 of negative number is NaN.
865 if (hx < 0) return NAN;
866 // Subnormal number. Scale up x.
874 if (hx >= 0x7ff00000) return x;
876 k += (hx >> 20) - 1023;
877 i = (k & 0x80000000) >> 31;
878 hx = (hx & 0x000fffff) | ((0x3ff - i) << 20);
880 x = %_ConstructDouble(hx, lx);
882 z = y * LOG10_2LO + IVLN10 * MathLog(x);
883 return z + y * LOG10_2HI;
887 // ES6 draft 09-27-13, section 20.2.2.22.
888 // Return the base 2 logarithm of x
890 // fdlibm does not have an explicit log2 function, but fdlibm's pow
891 // function does implement an accurate log2 function as part of the
892 // pow implementation. This extracts the core parts of that as a
893 // separate log2 function.
896 // Compute log2(x) in two pieces:
898 // where w1 has 53-24 = 29 bits of trailing zeroes.
900 const DP_H = kMath[64];
901 const DP_L = kMath[65];
903 // Polynomial coefficients for (3/2)*(log2(x) - 2*s - 2/3*s^3)
908 // cp = 2/(3*ln(2)). Note that cp_h + cp_l is cp, but with more accuracy.
909 const CP = kMath[61];
910 const CP_H = kMath[62];
911 const CP_L = kMath[63];
913 const TWO53 = 9007199254740992;
915 function MathLog2(x) {
916 x = x * 1; // Convert to number.
918 var hx = %_DoubleHi(x);
919 var lx = %_DoubleLo(x);
920 var ix = hx & 0x7fffffff;
922 // Handle special cases.
923 // log2(+/- 0) = -Infinity
924 if ((ix | lx) == 0) return -INFINITY;
926 // log(x) = NaN, if x < 0
927 if (hx < 0) return NAN;
929 // log2(Infinity) = Infinity, log2(NaN) = NaN
930 if (ix >= 0x7ff00000) return x;
934 // Take care of subnormal number.
935 if (ix < 0x00100000) {
941 n += (ix >> 20) - 0x3ff;
942 var j = ix & 0x000fffff;
944 // Determine interval.
945 ix = j | 0x3ff00000; // normalize ix.
950 if (j > 0x3988e) { // |x| > sqrt(3/2)
951 if (j < 0xbb67a) { // |x| < sqrt(3)
961 ax = %_ConstructDouble(ix, %_DoubleLo(ax));
963 // Compute ss = s_h + s_l = (x - 1)/(x+1) or (x - 1.5)/(x + 1.5)
965 var v = 1 / (ax + bp);
967 var s_h = %_ConstructDouble(%_DoubleHi(ss), 0);
969 // t_h = ax + bp[k] High
970 var t_h = %_ConstructDouble(%_DoubleHi(ax + bp), 0)
971 var t_l = ax - (t_h - bp);
972 var s_l = v * ((u - s_h * t_h) - s_h * t_l);
976 var r = s2 * s2 * (KLOG2(0) + s2 * (KLOG2(1) + s2 * (KLOG2(2) + s2 * (
977 KLOG2(3) + s2 * (KLOG2(4) + s2 * KLOG2(5))))));
978 r += s_l * (s_h + ss);
980 t_h = %_ConstructDouble(%_DoubleHi(3.0 + s2 + r), 0);
981 t_l = r - ((t_h - 3.0) - s2);
982 // u + v = ss * (1 + ...)
984 v = s_l * t_h + t_l * ss;
986 // 2 / (3 * log(2)) * (ss + ...)
987 p_h = %_ConstructDouble(%_DoubleHi(u + v), 0);
990 z_l = CP_L * p_h + p_l * CP + dp_l;
992 // log2(ax) = (ss + ...) * 2 / (3 * log(2)) = n + dp_h + z_h + z_l
994 var t1 = %_ConstructDouble(%_DoubleHi(((z_h + z_l) + dp_h) + t), 0);
995 var t2 = z_l - (((t1 - t) - dp_h) - z_h);
997 // t1 + t2 = log2(ax), sum up because we do not care about extra precision.