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.
33 %CheckIsBootstrapping();
35 var GlobalMath = global.Math;
36 var GlobalArray = global.Array;
38 //-------------------------------------------------------------------
40 const INVPIO2 = kMath[0];
41 const PIO2_1 = kMath[1];
42 const PIO2_1T = kMath[2];
43 const PIO2_2 = kMath[3];
44 const PIO2_2T = kMath[4];
45 const PIO2_3 = kMath[5];
46 const PIO2_3T = kMath[6];
47 const PIO4 = kMath[32];
48 const PIO4LO = kMath[33];
50 // Compute k and r such that x - k*pi/2 = r where |r| < pi/4. For
51 // precision, r is returned as two values y0 and y1 such that r = y0 + y1
52 // to more than double precision.
56 var hx = %_DoubleHi(X);
57 var ix = hx & 0x7fffffff;
59 if (ix < 0x4002d97c) {
60 // |X| ~< 3*pi/4, special case with n = +/- 1
63 if (ix != 0x3ff921fb) {
64 // 33+53 bit pi is good enough
66 y1 = (z - y0) - PIO2_1T;
68 // near pi/2, use 33+33+53 bit pi
71 y1 = (z - y0) - PIO2_2T;
77 if (ix != 0x3ff921fb) {
78 // 33+53 bit pi is good enough
80 y1 = (z - y0) + PIO2_1T;
82 // near pi/2, use 33+33+53 bit pi
85 y1 = (z - y0) + PIO2_2T;
89 } else if (ix <= 0x413921fb) {
90 // |X| ~<= 2^19*(pi/2), medium size
92 n = (t * INVPIO2 + 0.5) | 0;
93 var r = t - n * PIO2_1;
95 // First round good to 85 bit
97 if (ix - (%_DoubleHi(y0) & 0x7ff00000) > 0x1000000) {
98 // 2nd iteration needed, good to 118
102 w = n * PIO2_2T - ((t - r) - w);
104 if (ix - (%_DoubleHi(y0) & 0x7ff00000) > 0x3100000) {
105 // 3rd iteration needed. 151 bits accuracy
109 w = n * PIO2_3T - ((t - r) - w);
120 // Need to do full Payne-Hanek reduction here.
121 n = %RemPiO2(X, rempio2result);
122 y0 = rempio2result[0];
123 y1 = rempio2result[1];
128 // __kernel_sin(X, Y, IY)
129 // kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854
130 // Input X is assumed to be bounded by ~pi/4 in magnitude.
131 // Input Y is the tail of X so that x = X + Y.
134 // 1. Since ieee_sin(-x) = -ieee_sin(x), we need only to consider positive x.
135 // 2. ieee_sin(x) is approximated by a polynomial of degree 13 on
138 // sin(x) ~ x + S1*x + ... + S6*x
141 // |ieee_sin(x) 2 4 6 8 10 12 | -58
142 // |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2
145 // 3. ieee_sin(X+Y) = ieee_sin(X) + sin'(X')*Y
146 // ~ ieee_sin(X) + (1-X*X/2)*Y
147 // For better accuracy, let
149 // r = X *(S2+X *(S3+X *(S4+X *(S5+X *S6))))
151 // sin(x) = X + (S1*X + (X *(r-Y/2)+Y))
153 const S1 = -1.66666666666666324348e-01;
154 const S2 = 8.33333333332248946124e-03;
155 const S3 = -1.98412698298579493134e-04;
156 const S4 = 2.75573137070700676789e-06;
157 const S5 = -2.50507602534068634195e-08;
158 const S6 = 1.58969099521155010221e-10;
160 macro RETURN_KERNELSIN(X, Y, SIGN)
163 var r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6)));
164 return (X - ((z * (0.5 * Y - v * r) - Y) - v * S1)) SIGN;
167 // __kernel_cos(X, Y)
168 // kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
169 // Input X is assumed to be bounded by ~pi/4 in magnitude.
170 // Input Y is the tail of X so that x = X + Y.
173 // 1. Since ieee_cos(-x) = ieee_cos(x), we need only to consider positive x.
174 // 2. ieee_cos(x) is approximated by a polynomial of degree 14 on
177 // cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x
178 // where the remez error is
180 // | 2 4 6 8 10 12 14 | -58
181 // |ieee_cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x +C6*x )| <= 2
185 // 3. let r = C1*x +C2*x +C3*x +C4*x +C5*x +C6*x , then
186 // ieee_cos(x) = 1 - x*x/2 + r
187 // since ieee_cos(X+Y) ~ ieee_cos(X) - ieee_sin(X)*Y
188 // ~ ieee_cos(X) - X*Y,
189 // a correction term is necessary in ieee_cos(x) and hence
190 // cos(X+Y) = 1 - (X*X/2 - (r - X*Y))
191 // For better accuracy when x > 0.3, let qx = |x|/4 with
192 // the last 32 bits mask off, and if x > 0.78125, let qx = 0.28125.
194 // cos(X+Y) = (1-qx) - ((X*X/2-qx) - (r-X*Y)).
195 // Note that 1-qx and (X*X/2-qx) is EXACT here, and the
196 // magnitude of the latter is at least a quarter of X*X/2,
197 // thus, reducing the rounding error in the subtraction.
199 const C1 = 4.16666666666666019037e-02;
200 const C2 = -1.38888888888741095749e-03;
201 const C3 = 2.48015872894767294178e-05;
202 const C4 = -2.75573143513906633035e-07;
203 const C5 = 2.08757232129817482790e-09;
204 const C6 = -1.13596475577881948265e-11;
206 macro RETURN_KERNELCOS(X, Y, SIGN)
207 var ix = %_DoubleHi(X) & 0x7fffffff;
209 var r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * C6)))));
210 if (ix < 0x3fd33333) { // |x| ~< 0.3
211 return (1 - (0.5 * z - (z * r - X * Y))) SIGN;
214 if (ix > 0x3fe90000) { // |x| > 0.78125
217 qx = %_ConstructDouble(%_DoubleHi(0.25 * X), 0);
219 var hz = 0.5 * z - qx;
220 return (1 - qx - (hz - (z * r - X * Y))) SIGN;
225 // kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854
226 // Input x is assumed to be bounded by ~pi/4 in magnitude.
227 // Input y is the tail of x.
228 // Input k indicates whether ieee_tan (if k = 1) or -1/tan (if k = -1)
232 // 1. Since ieee_tan(-x) = -ieee_tan(x), we need only to consider positive x.
233 // 2. if x < 2^-28 (hx<0x3e300000 0), return x with inexact if x!=0.
234 // 3. ieee_tan(x) is approximated by a odd polynomial of degree 27 on
237 // tan(x) ~ x + T1*x + ... + T13*x
240 // |ieee_tan(x) 2 4 26 | -59.2
241 // |----- - (1+T1*x +T2*x +.... +T13*x )| <= 2
244 // Note: ieee_tan(x+y) = ieee_tan(x) + tan'(x)*y
245 // ~ ieee_tan(x) + (1+x*x)*y
246 // Therefore, for better accuracy in computing ieee_tan(x+y), let
248 // r = x *(T2+x *(T3+x *(...+x *(T12+x *T13))))
251 // tan(x+y) = x + (T1*x + (x *(r+y)+y))
253 // 4. For x in [0.67434,pi/4], let y = pi/4 - x, then
254 // tan(x) = ieee_tan(pi/4-y) = (1-ieee_tan(y))/(1+ieee_tan(y))
255 // = 1 - 2*(ieee_tan(y) - (ieee_tan(y)^2)/(1+ieee_tan(y)))
257 // Set returnTan to 1 for tan; -1 for cot. Anything else is illegal
258 // and will cause incorrect results.
264 function KernelTan(x, y, returnTan) {
267 var hx = %_DoubleHi(x);
268 var ix = hx & 0x7fffffff;
270 if (ix < 0x3e300000) { // |x| < 2^-28
271 if (((ix | %_DoubleLo(x)) | (returnTan + 1)) == 0) {
272 // x == 0 && returnTan = -1
275 if (returnTan == 1) {
278 // Compute -1/(x + y) carefully
280 var z = %_ConstructDouble(%_DoubleHi(w), 0);
283 var t = %_ConstructDouble(%_DoubleHi(a), 0);
285 return t + a * (s + t * v);
289 if (ix >= 0x3fe59428) { // |x| > .6744
302 // Break x^5 * (T1 + x^2*T2 + ...) into
303 // x^5 * (T1 + x^4*T3 + ... + x^20*T11) +
304 // x^5 * (x^2 * (T2 + x^4*T4 + ... + x^22*T12))
305 var r = KTAN(1) + w * (KTAN(3) + w * (KTAN(5) +
306 w * (KTAN(7) + w * (KTAN(9) + w * KTAN(11)))));
307 var v = z * (KTAN(2) + w * (KTAN(4) + w * (KTAN(6) +
308 w * (KTAN(8) + w * (KTAN(10) + w * KTAN(12))))));
310 r = y + z * (s * (r + v) + y);
313 if (ix >= 0x3fe59428) {
314 return (1 - ((hx >> 30) & 2)) *
315 (returnTan - 2.0 * (x - (w * w / (w + returnTan) - r)));
317 if (returnTan == 1) {
320 z = %_ConstructDouble(%_DoubleHi(w), 0);
323 var t = %_ConstructDouble(%_DoubleHi(a), 0);
325 return t + a * (s + t * v);
329 function MathSinSlow(x) {
331 var sign = 1 - (n & 2);
333 RETURN_KERNELCOS(y0, y1, * sign);
335 RETURN_KERNELSIN(y0, y1, * sign);
339 function MathCosSlow(x) {
342 var sign = (n & 2) - 1;
343 RETURN_KERNELSIN(y0, y1, * sign);
345 var sign = 1 - (n & 2);
346 RETURN_KERNELCOS(y0, y1, * sign);
350 // ECMA 262 - 15.8.2.16
351 function MathSin(x) {
352 x = +x; // Convert to number.
353 if ((%_DoubleHi(x) & 0x7fffffff) <= 0x3fe921fb) {
354 // |x| < pi/4, approximately. No reduction needed.
355 RETURN_KERNELSIN(x, 0, /* empty */);
357 return +MathSinSlow(x);
360 // ECMA 262 - 15.8.2.7
361 function MathCos(x) {
362 x = +x; // Convert to number.
363 if ((%_DoubleHi(x) & 0x7fffffff) <= 0x3fe921fb) {
364 // |x| < pi/4, approximately. No reduction needed.
365 RETURN_KERNELCOS(x, 0, /* empty */);
367 return +MathCosSlow(x);
370 // ECMA 262 - 15.8.2.18
371 function MathTan(x) {
372 x = x * 1; // Convert to number.
373 if ((%_DoubleHi(x) & 0x7fffffff) <= 0x3fe921fb) {
374 // |x| < pi/4, approximately. No reduction needed.
375 return KernelTan(x, 0, 1);
378 return KernelTan(y0, y1, (n & 1) ? -1 : 1);
381 // ES6 draft 09-27-13, section 20.2.2.20.
385 // 1. Argument Reduction: find k and f such that
386 // 1+x = 2^k * (1+f),
387 // where sqrt(2)/2 < 1+f < sqrt(2) .
389 // Note. If k=0, then f=x is exact. However, if k!=0, then f
390 // may not be representable exactly. In that case, a correction
391 // term is need. Let u=1+x rounded. Let c = (1+x)-u, then
392 // log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
393 // and add back the correction term c/u.
394 // (Note: when x > 2**53, one can simply return log(x))
396 // 2. Approximation of log1p(f).
397 // Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
398 // = 2s + 2/3 s**3 + 2/5 s**5 + .....,
400 // We use a special Reme algorithm on [0,0.1716] to generate
401 // a polynomial of degree 14 to approximate R The maximum error
402 // of this polynomial approximation is bounded by 2**-58.45. In
405 // R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s
406 // (the values of Lp1 to Lp7 are listed in the program)
409 // | Lp1*s +...+Lp7*s - R(z) | <= 2
411 // Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
412 // In order to guarantee error in log below 1ulp, we compute log
414 // log1p(f) = f - (hfsq - s*(hfsq+R)).
416 // 3. Finally, log1p(x) = k*ln2 + log1p(f).
417 // = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
418 // Here ln2 is split into two floating point number:
420 // where n*ln2_hi is always exact for |n| < 2000.
423 // log1p(x) is NaN with signal if x < -1 (including -INF) ;
424 // log1p(+INF) is +INF; log1p(-1) is -INF with signal;
425 // log1p(NaN) is that NaN with no signal.
428 // according to an error analysis, the error is always less than
429 // 1 ulp (unit in the last place).
432 // Constants are found in fdlibm.cc. We assume the C++ compiler to convert
433 // from decimal to binary accurately enough to produce the intended values.
435 // Note: Assuming log() return accurate answer, the following
436 // algorithm can be used to compute log1p(x) to within a few ULP:
439 // if (u==1.0) return x ; else
440 // return log(u)*(x/(u-1.0));
442 // See HP-15C Advanced Functions Handbook, p.193.
444 const LN2_HI = kMath[34];
445 const LN2_LO = kMath[35];
446 const TWO_THIRD = kMath[36];
451 const TWO54 = 18014398509481984;
453 function MathLog1p(x) {
454 x = x * 1; // Convert to number.
455 var hx = %_DoubleHi(x);
456 var ax = hx & 0x7fffffff;
463 if (hx < 0x3fda827a) {
465 if (ax >= 0x3ff00000) { // |x| >= 1
467 return -INFINITY; // log1p(-1) = -inf
469 return NAN; // log1p(x<-1) = NaN
471 } else if (ax < 0x3c900000) {
472 // For |x| < 2^-54 we can return x.
474 } else if (ax < 0x3e200000) {
475 // For |x| < 2^-29 we can use a simple two-term Taylor series.
476 return x - x * x * 0.5;
479 if ((hx > 0) || (hx <= -0x402D413D)) { // (int) 0xbfd2bec3 = -0x402d413d
480 // -.2929 < x < 0.41422
485 // Handle Infinity and NAN
486 if (hx >= 0x7ff00000) return x;
489 if (hx < 0x43400000) {
493 k = (hu >> 20) - 1023;
494 c = (k > 0) ? 1 - (u - x) : x - (u - 1);
498 k = (hu >> 20) - 1023;
502 u = %_ConstructDouble(hu | 0x3ff00000, %_DoubleLo(u)); // Normalize u.
505 u = %_ConstructDouble(hu | 0x3fe00000, %_DoubleLo(u)); // Normalize u/2.
506 hu = (0x00100000 - hu) >> 2;
511 var hfsq = 0.5 * f * f;
518 return k * LN2_HI + (c + k * LN2_LO);
521 var R = hfsq * (1 - TWO_THIRD * f);
525 return k * LN2_HI - ((R - (k * LN2_LO + c)) - f);
531 var R = z * (KLOG1P(0) + z * (KLOG1P(1) + z *
532 (KLOG1P(2) + z * (KLOG1P(3) + z *
533 (KLOG1P(4) + z * (KLOG1P(5) + z * KLOG1P(6)))))));
535 return f - (hfsq - s * (hfsq + R));
537 return k * LN2_HI - ((hfsq - (s * (hfsq + R) + (k * LN2_LO + c))) - f);
541 // ES6 draft 09-27-13, section 20.2.2.14.
543 // Returns exp(x)-1, the exponential of x minus 1.
546 // 1. Argument reduction:
547 // Given x, find r and integer k such that
549 // x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658
551 // Here a correction term c will be computed to compensate
552 // the error in r when rounded to a floating-point number.
554 // 2. Approximating expm1(r) by a special rational function on
555 // the interval [0,0.34658]:
557 // r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
558 // we define R1(r*r) by
559 // r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
561 // R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
562 // = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
563 // = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
564 // We use a special Remes algorithm on [0,0.347] to generate
565 // a polynomial of degree 5 in r*r to approximate R1. The
566 // maximum error of this polynomial approximation is bounded
567 // by 2**-61. In other words,
568 // R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
569 // where Q1 = -1.6666666666666567384E-2,
570 // Q2 = 3.9682539681370365873E-4,
571 // Q3 = -9.9206344733435987357E-6,
572 // Q4 = 2.5051361420808517002E-7,
573 // Q5 = -6.2843505682382617102E-9;
574 // (where z=r*r, and the values of Q1 to Q5 are listed below)
575 // with error bounded by
577 // | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2
580 // expm1(r) = exp(r)-1 is then computed by the following
581 // specific way which minimize the accumulation rounding error:
583 // r r [ 3 - (R1 + R1*r/2) ]
584 // expm1(r) = r + --- + --- * [--------------------]
585 // 2 2 [ 6 - r*(3 - R1*r/2) ]
587 // To compensate the error in the argument reduction, we use
588 // expm1(r+c) = expm1(r) + c + expm1(r)*c
589 // ~ expm1(r) + c + r*c
590 // Thus c+r*c will be added in as the correction terms for
591 // expm1(r+c). Now rearrange the term to avoid optimization
594 // ({ ( r [ R1 - (3 - R1*r/2) ] ) } r )
595 // expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
596 // ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 )
600 // 3. Scale back to obtain expm1(x):
601 // From step 1, we have
602 // expm1(x) = either 2^k*[expm1(r)+1] - 1
603 // = or 2^k*[expm1(r) + (1-2^-k)]
604 // 4. Implementation notes:
605 // (A). To save one multiplication, we scale the coefficient Qi
606 // to Qi*2^i, and replace z by (x^2)/2.
607 // (B). To achieve maximum accuracy, we compute expm1(x) by
608 // (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
609 // (ii) if k=0, return r-E
610 // (iii) if k=-1, return 0.5*(r-E)-0.5
611 // (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E)
612 // else return 1.0+2.0*(r-E);
613 // (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
614 // (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else
615 // (vii) return 2^k(1-((E+2^-k)-r))
618 // expm1(INF) is INF, expm1(NaN) is NaN;
619 // expm1(-INF) is -1, and
620 // for finite argument, only expm1(0)=0 is exact.
623 // according to an error analysis, the error is always less than
624 // 1 ulp (unit in the last place).
628 // if x > 7.09782712893383973096e+02 then expm1(x) overflow
630 const KEXPM1_OVERFLOW = kMath[44];
631 const INVLN2 = kMath[45];
636 function MathExpm1(x) {
637 x = x * 1; // Convert to number.
645 var hx = %_DoubleHi(x);
646 var xsb = hx & 0x80000000; // Sign bit of x
647 var y = (xsb === 0) ? x : -x; // y = |x|
648 hx &= 0x7fffffff; // High word of |x|
650 // Filter out huge and non-finite argument
651 if (hx >= 0x4043687a) { // if |x| ~=> 56 * ln2
652 if (hx >= 0x40862e42) { // if |x| >= 709.78
653 if (hx >= 0x7ff00000) {
654 // expm1(inf) = inf; expm1(-inf) = -1; expm1(nan) = nan;
655 return (x === -INFINITY) ? -1 : x;
657 if (x > KEXPM1_OVERFLOW) return INFINITY; // Overflow
659 if (xsb != 0) return -1; // x < -56 * ln2, return -1.
662 // Argument reduction
663 if (hx > 0x3fd62e42) { // if |x| > 0.5 * ln2
664 if (hx < 0x3ff0a2b2) { // and |x| < 1.5 * ln2
675 k = (INVLN2 * x + ((xsb === 0) ? 0.5 : -0.5)) | 0;
677 // t * ln2_hi is exact here.
683 } else if (hx < 0x3c900000) {
684 // When |x| < 2^-54, we can return x.
691 // x is now in primary range
694 var r1 = 1 + hxs * (KEXPM1(0) + hxs * (KEXPM1(1) + hxs *
695 (KEXPM1(2) + hxs * (KEXPM1(3) + hxs * KEXPM1(4)))));
697 var e = hxs * ((r1 - t) / (6 - x * t));
698 if (k === 0) { // c is 0
699 return x - (x*e - hxs);
701 e = (x * (e - c) - c);
703 if (k === -1) return 0.5 * (x - e) - 0.5;
705 if (x < -0.25) return -2 * (e - (x + 0.5));
706 return 1 + 2 * (x - e);
709 if (k <= -2 || k > 56) {
710 // suffice to return exp(x) + 1
712 // Add k to y's exponent
713 y = %_ConstructDouble(%_DoubleHi(y) + (k << 20), %_DoubleLo(y));
718 t = %_ConstructDouble(0x3ff00000 - (0x200000 >> k), 0);
720 // Add k to y's exponent
721 y = %_ConstructDouble(%_DoubleHi(y) + (k << 20), %_DoubleLo(y));
724 t = %_ConstructDouble((0x3ff - k) << 20, 0);
727 // Add k to y's exponent
728 y = %_ConstructDouble(%_DoubleHi(y) + (k << 20), %_DoubleLo(y));
735 // ES6 draft 09-27-13, section 20.2.2.30.
738 // mathematically sinh(x) if defined to be (exp(x)-exp(-x))/2
739 // 1. Replace x by |x| (sinh(-x) = -sinh(x)).
742 // 0 <= x <= 22 : sinh(x) := --------------, E=expm1(x)
745 // 22 <= x <= lnovft : sinh(x) := exp(x)/2
746 // lnovft <= x <= ln2ovft: sinh(x) := exp(x/2)/2 * exp(x/2)
747 // ln2ovft < x : sinh(x) := x*shuge (overflow)
750 // sinh(x) is |x| if x is +Infinity, -Infinity, or NaN.
751 // only sinh(0)=0 is exact for finite x.
753 const KSINH_OVERFLOW = kMath[51];
754 const TWO_M28 = 3.725290298461914e-9; // 2^-28, empty lower half
755 const LOG_MAXD = 709.7822265625; // 0x40862e42 00000000, empty lower half
757 function MathSinh(x) {
758 x = x * 1; // Convert to number.
759 var h = (x < 0) ? -0.5 : 0.5;
760 // |x| in [0, 22]. return sign(x)*0.5*(E+E/(E+1))
763 // For |x| < 2^-28, sinh(x) = x
764 if (ax < TWO_M28) return x;
765 var t = MathExpm1(ax);
766 if (ax < 1) return h * (2 * t - t * t / (t + 1));
767 return h * (t + t / (t + 1));
769 // |x| in [22, log(maxdouble)], return 0.5 * exp(|x|)
770 if (ax < LOG_MAXD) return h * $exp(ax);
771 // |x| in [log(maxdouble), overflowthreshold]
772 // overflowthreshold = 710.4758600739426
773 if (ax <= KSINH_OVERFLOW) {
774 var w = $exp(0.5 * ax);
778 // |x| > overflowthreshold or is NaN.
779 // Return Infinity of the appropriate sign or NaN.
784 // ES6 draft 09-27-13, section 20.2.2.12.
787 // mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2
788 // 1. Replace x by |x| (cosh(x) = cosh(-x)).
791 // 0 <= x <= ln2/2 : cosh(x) := 1 + -------------------
795 // ln2/2 <= x <= 22 : cosh(x) := -------------------
797 // 22 <= x <= lnovft : cosh(x) := exp(x)/2
798 // lnovft <= x <= ln2ovft: cosh(x) := exp(x/2)/2 * exp(x/2)
799 // ln2ovft < x : cosh(x) := huge*huge (overflow)
802 // cosh(x) is |x| if x is +INF, -INF, or NaN.
803 // only cosh(0)=1 is exact for finite x.
805 const KCOSH_OVERFLOW = kMath[51];
807 function MathCosh(x) {
808 x = x * 1; // Convert to number.
809 var ix = %_DoubleHi(x) & 0x7fffffff;
810 // |x| in [0,0.5*log2], return 1+expm1(|x|)^2/(2*exp(|x|))
811 if (ix < 0x3fd62e43) {
812 var t = MathExpm1($abs(x));
814 // For |x| < 2^-55, cosh(x) = 1
815 if (ix < 0x3c800000) return w;
816 return 1 + (t * t) / (w + w);
818 // |x| in [0.5*log2, 22], return (exp(|x|)+1/exp(|x|)/2
819 if (ix < 0x40360000) {
820 var t = $exp($abs(x));
821 return 0.5 * t + 0.5 / t;
823 // |x| in [22, log(maxdouble)], return half*exp(|x|)
824 if (ix < 0x40862e42) return 0.5 * $exp($abs(x));
825 // |x| in [log(maxdouble), overflowthreshold]
826 if ($abs(x) <= KCOSH_OVERFLOW) {
827 var w = $exp(0.5 * $abs(x));
831 if (NUMBER_IS_NAN(x)) return x;
832 // |x| > overflowthreshold.
836 // ES6 draft 09-27-13, section 20.2.2.21.
837 // Return the base 10 logarithm of x
840 // Let log10_2hi = leading 40 bits of log10(2) and
841 // log10_2lo = log10(2) - log10_2hi,
842 // ivln10 = 1/log(10) rounded.
847 // log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))
850 // To guarantee log10(10**n)=n, where 10**n is normal, the rounding
851 // mode must set to Round-to-Nearest.
853 // [1/log(10)] rounded to 53 bits has error .198 ulps;
854 // log10 is monotonic at all binary break points.
857 // log10(x) is NaN if x < 0;
858 // log10(+INF) is +INF; log10(0) is -INF;
859 // log10(NaN) is that NaN;
860 // log10(10**N) = N for N=0,1,...,22.
863 const IVLN10 = kMath[52];
864 const LOG10_2HI = kMath[53];
865 const LOG10_2LO = kMath[54];
867 function MathLog10(x) {
868 x = x * 1; // Convert to number.
869 var hx = %_DoubleHi(x);
870 var lx = %_DoubleLo(x);
873 if (hx < 0x00100000) {
875 // log10(+/- 0) = -Infinity.
876 if (((hx & 0x7fffffff) | lx) === 0) return -INFINITY;
877 // log10 of negative number is NaN.
878 if (hx < 0) return NAN;
879 // Subnormal number. Scale up x.
887 if (hx >= 0x7ff00000) return x;
889 k += (hx >> 20) - 1023;
890 i = (k & 0x80000000) >> 31;
891 hx = (hx & 0x000fffff) | ((0x3ff - i) << 20);
893 x = %_ConstructDouble(hx, lx);
895 z = y * LOG10_2LO + IVLN10 * %_MathLogRT(x);
896 return z + y * LOG10_2HI;
900 // ES6 draft 09-27-13, section 20.2.2.22.
901 // Return the base 2 logarithm of x
903 // fdlibm does not have an explicit log2 function, but fdlibm's pow
904 // function does implement an accurate log2 function as part of the
905 // pow implementation. This extracts the core parts of that as a
906 // separate log2 function.
909 // Compute log2(x) in two pieces:
911 // where w1 has 53-24 = 29 bits of trailing zeroes.
913 const DP_H = kMath[64];
914 const DP_L = kMath[65];
916 // Polynomial coefficients for (3/2)*(log2(x) - 2*s - 2/3*s^3)
921 // cp = 2/(3*ln(2)). Note that cp_h + cp_l is cp, but with more accuracy.
922 const CP = kMath[61];
923 const CP_H = kMath[62];
924 const CP_L = kMath[63];
926 const TWO53 = 9007199254740992;
928 function MathLog2(x) {
929 x = x * 1; // Convert to number.
931 var hx = %_DoubleHi(x);
932 var lx = %_DoubleLo(x);
933 var ix = hx & 0x7fffffff;
935 // Handle special cases.
936 // log2(+/- 0) = -Infinity
937 if ((ix | lx) == 0) return -INFINITY;
939 // log(x) = NaN, if x < 0
940 if (hx < 0) return NAN;
942 // log2(Infinity) = Infinity, log2(NaN) = NaN
943 if (ix >= 0x7ff00000) return x;
947 // Take care of subnormal number.
948 if (ix < 0x00100000) {
954 n += (ix >> 20) - 0x3ff;
955 var j = ix & 0x000fffff;
957 // Determine interval.
958 ix = j | 0x3ff00000; // normalize ix.
963 if (j > 0x3988e) { // |x| > sqrt(3/2)
964 if (j < 0xbb67a) { // |x| < sqrt(3)
974 ax = %_ConstructDouble(ix, %_DoubleLo(ax));
976 // Compute ss = s_h + s_l = (x - 1)/(x+1) or (x - 1.5)/(x + 1.5)
978 var v = 1 / (ax + bp);
980 var s_h = %_ConstructDouble(%_DoubleHi(ss), 0);
982 // t_h = ax + bp[k] High
983 var t_h = %_ConstructDouble(%_DoubleHi(ax + bp), 0)
984 var t_l = ax - (t_h - bp);
985 var s_l = v * ((u - s_h * t_h) - s_h * t_l);
989 var r = s2 * s2 * (KLOG2(0) + s2 * (KLOG2(1) + s2 * (KLOG2(2) + s2 * (
990 KLOG2(3) + s2 * (KLOG2(4) + s2 * KLOG2(5))))));
991 r += s_l * (s_h + ss);
993 t_h = %_ConstructDouble(%_DoubleHi(3.0 + s2 + r), 0);
994 t_l = r - ((t_h - 3.0) - s2);
995 // u + v = ss * (1 + ...)
997 v = s_l * t_h + t_l * ss;
999 // 2 / (3 * log(2)) * (ss + ...)
1000 p_h = %_ConstructDouble(%_DoubleHi(u + v), 0);
1001 p_l = v - (p_h - u);
1003 z_l = CP_L * p_h + p_l * CP + dp_l;
1005 // log2(ax) = (ss + ...) * 2 / (3 * log(2)) = n + dp_h + z_h + z_l
1007 var t1 = %_ConstructDouble(%_DoubleHi(((z_h + z_l) + dp_h) + t), 0);
1008 var t2 = z_l - (((t1 - t) - dp_h) - z_h);
1010 // t1 + t2 = log2(ax), sum up because we do not care about extra precision.
1014 //-------------------------------------------------------------------
1016 InstallFunctions(GlobalMath, DONT_ENUM, GlobalArray(
1028 %SetInlineBuiltinFlag(MathSin);
1029 %SetInlineBuiltinFlag(MathCos);