880446990536b62a633fced0ba3b446c9801b396
[platform/upstream/nodejs.git] / deps / v8 / src / third_party / fdlibm / fdlibm.js
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 // rempio2result is used as a container for return values of %RemPiO2. It is
24 // initialized to a two-element Float64Array during genesis.
25
26 "use strict";
27
28 var kMath;
29 var rempio2result;
30
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];
40
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.
44
45 macro REMPIO2(X)
46   var n, y0, y1;
47   var hx = %_DoubleHi(X);
48   var ix = hx & 0x7fffffff;
49
50   if (ix < 0x4002d97c) {
51     // |X| ~< 3*pi/4, special case with n = +/- 1
52     if (hx > 0) {
53       var z = X - PIO2_1;
54       if (ix != 0x3ff921fb) {
55         // 33+53 bit pi is good enough
56         y0 = z - PIO2_1T;
57         y1 = (z - y0) - PIO2_1T;
58       } else {
59         // near pi/2, use 33+33+53 bit pi
60         z -= PIO2_2;
61         y0 = z - PIO2_2T;
62         y1 = (z - y0) - PIO2_2T;
63       }
64       n = 1;
65     } else {
66       // Negative X
67       var z = X + PIO2_1;
68       if (ix != 0x3ff921fb) {
69         // 33+53 bit pi is good enough
70         y0 = z + PIO2_1T;
71         y1 = (z - y0) + PIO2_1T;
72       } else {
73         // near pi/2, use 33+33+53 bit pi
74         z += PIO2_2;
75         y0 = z + PIO2_2T;
76         y1 = (z - y0) + PIO2_2T;
77       }
78       n = -1;
79     }
80   } else if (ix <= 0x413921fb) {
81     // |X| ~<= 2^19*(pi/2), medium size
82     var t = MathAbs(X);
83     n = (t * INVPIO2 + 0.5) | 0;
84     var r = t - n * PIO2_1;
85     var w = n * PIO2_1T;
86     // First round good to 85 bit
87     y0 = r - w;
88     if (ix - (%_DoubleHi(y0) & 0x7ff00000) > 0x1000000) {
89       // 2nd iteration needed, good to 118
90       t = r;
91       w = n * PIO2_2;
92       r = t - w;
93       w = n * PIO2_2T - ((t - r) - w);
94       y0 = r - w;
95       if (ix - (%_DoubleHi(y0) & 0x7ff00000) > 0x3100000) {
96         // 3rd iteration needed. 151 bits accuracy
97         t = r;
98         w = n * PIO2_3;
99         r = t - w;
100         w = n * PIO2_3T - ((t - r) - w);
101         y0 = r - w;
102       }
103     }
104     y1 = (r - y0) - w;
105     if (hx < 0) {
106       n = -n;
107       y0 = -y0;
108       y1 = -y1;
109     }
110   } else {
111     // Need to do full Payne-Hanek reduction here.
112     n = %RemPiO2(X, rempio2result);
113     y0 = rempio2result[0];
114     y1 = rempio2result[1];
115   }
116 endmacro
117
118
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.
123 //
124 // Algorithm
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
127 //     [0,pi/4]
128 //                           3            13
129 //          sin(x) ~ x + S1*x + ... + S6*x
130 //     where
131 //
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
134 //    |  x                                               |
135 //
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
139 //               3      2      2      2      2
140 //          r = X *(S2+X *(S3+X *(S4+X *(S5+X *S6))))
141 //     then                   3    2
142 //          sin(x) = X + (S1*X + (X *(r-Y/2)+Y))
143 //
144 macro KSIN(x)
145 kMath[7+x]
146 endmacro
147
148 macro RETURN_KERNELSIN(X, Y, SIGN)
149   var z = X * X;
150   var v = z * X;
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;
154 endmacro
155
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.
160 //
161 // Algorithm
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
164 //     [0,pi/4]
165 //                                   4            14
166 //          cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x
167 //     where the remez error is
168 //
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
171 //  |                                                           |
172 //
173 //                 4     6     8     10    12     14
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.
182 //     Then
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.
187 //
188 macro KCOS(x)
189 kMath[13+x]
190 endmacro
191
192 macro RETURN_KERNELCOS(X, Y, SIGN)
193   var ix = %_DoubleHi(X) & 0x7fffffff;
194   var z = X * X;
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;
199   } else {
200     var qx;
201     if (ix > 0x3fe90000) {  // |x| > 0.78125
202       qx = 0.28125;
203     } else {
204       qx = %_ConstructDouble(%_DoubleHi(0.25 * X), 0);
205     }
206     var hz = 0.5 * z - qx;
207     return (1 - qx - (hz - (z * r - X * Y))) SIGN;
208   }
209 endmacro
210
211
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)
216 // is returned.
217 //
218 // Algorithm
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
222 //     [0,0.67434]
223 //                           3             27
224 //          tan(x) ~ x + T1*x + ... + T13*x
225 //     where
226 //
227 //     |ieee_tan(x)    2     4            26   |     -59.2
228 //     |----- - (1+T1*x +T2*x +.... +T13*x    )| <= 2
229 //     |  x                                    |
230 //
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
234 //               3      2      2       2       2
235 //          r = x *(T2+x *(T3+x *(...+x *(T12+x *T13))))
236 //     then
237 //                              3    2
238 //          tan(x+y) = x + (T1*x + (x *(r+y)+y))
239 //
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)))
243 //
244 // Set returnTan to 1 for tan; -1 for cot.  Anything else is illegal
245 // and will cause incorrect results.
246 //
247 macro KTAN(x)
248 kMath[19+x]
249 endmacro
250
251 function KernelTan(x, y, returnTan) {
252   var z;
253   var w;
254   var hx = %_DoubleHi(x);
255   var ix = hx & 0x7fffffff;
256
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);
261     } else {
262       if (returnTan == 1) {
263         return x;
264       } else {
265         // Compute -1/(x + y) carefully
266         var w = x + y;
267         var z = %_ConstructDouble(%_DoubleHi(w), 0);
268         var v = y - (z - x);
269         var a = -1 / w;
270         var t = %_ConstructDouble(%_DoubleHi(a), 0);
271         var s = 1 + t * z;
272         return t + a * (s + t * v);
273       }
274     }
275   }
276   if (ix >= 0x3fe59428) {  // |x| > .6744
277     if (x < 0) {
278       x = -x;
279       y = -y;
280     }
281     z = PIO4 - x;
282     w = PIO4LO - y;
283     x = z + w;
284     y = 0;
285   }
286   z = x * x;
287   w = z * z;
288
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))))));
296   var s = z * x;
297   r = y + z * (s * (r + v) + y);
298   r = r + KTAN(0) * s;
299   w = x + r;
300   if (ix >= 0x3fe59428) {
301     return (1 - ((hx >> 30) & 2)) *
302       (returnTan - 2.0 * (x - (w * w / (w + returnTan) - r)));
303   }
304   if (returnTan == 1) {
305     return w;
306   } else {
307     z = %_ConstructDouble(%_DoubleHi(w), 0);
308     v = r - (z - x);
309     var a = -1 / w;
310     var t = %_ConstructDouble(%_DoubleHi(a), 0);
311     s = 1 + t * z;
312     return t + a * (s + t * v);
313   }
314 }
315
316 function MathSinSlow(x) {
317   REMPIO2(x);
318   var sign = 1 - (n & 2);
319   if (n & 1) {
320     RETURN_KERNELCOS(y0, y1, * sign);
321   } else {
322     RETURN_KERNELSIN(y0, y1, * sign);
323   }
324 }
325
326 function MathCosSlow(x) {
327   REMPIO2(x);
328   if (n & 1) {
329     var sign = (n & 2) - 1;
330     RETURN_KERNELSIN(y0, y1, * sign);
331   } else {
332     var sign = 1 - (n & 2);
333     RETURN_KERNELCOS(y0, y1, * sign);
334   }
335 }
336
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 */);
343   }
344   return MathSinSlow(x);
345 }
346
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 */);
353   }
354   return MathCosSlow(x);
355 }
356
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);
363   }
364   REMPIO2(x);
365   return KernelTan(y0, y1, (n & 1) ? -1 : 1);
366 }
367
368 // ES6 draft 09-27-13, section 20.2.2.20.
369 // Math.log1p
370 //
371 // Method :                  
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) .
375 //
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))
382 //
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 + .....,
386 //            = 2s + s*R
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
390 //      other words,
391 //                      2      4      6      8      10      12      14
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)
394 //      and
395 //          |      2          14          |     -58.45
396 //          | Lp1*s +...+Lp7*s    -  R(z) | <= 2 
397 //          |                             |
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
400 //      by
401 //              log1p(f) = f - (hfsq - s*(hfsq+R)).
402 //
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: 
406 //                      ln2_hi + ln2_lo,
407 //         where n*ln2_hi is always exact for |n| < 2000.
408 //
409 // Special cases:
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.
413 //
414 // Accuracy:
415 //      according to an error analysis, the error is always less than
416 //      1 ulp (unit in the last place).
417 //
418 // Constants:
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.
421 //
422 // Note: Assuming log() return accurate answer, the following
423 //       algorithm can be used to compute log1p(x) to within a few ULP:
424 //
425 //              u = 1+x;
426 //              if (u==1.0) return x ; else
427 //                          return log(u)*(x/(u-1.0));
428 //
429 //       See HP-15C Advanced Functions Handbook, p.193.
430 //
431 const LN2_HI    = kMath[34];
432 const LN2_LO    = kMath[35];
433 const TWO_THIRD = kMath[36];
434 macro KLOG1P(x)
435 (kMath[37+x])
436 endmacro
437 // 2^54
438 const TWO54 = 18014398509481984;
439
440 function MathLog1p(x) {
441   x = x * 1;  // Convert to number.
442   var hx = %_DoubleHi(x);
443   var ax = hx & 0x7fffffff;
444   var k = 1;
445   var f = x;
446   var hu = 1;
447   var c = 0;
448   var u = x;
449
450   if (hx < 0x3fda827a) {
451     // x < 0.41422
452     if (ax >= 0x3ff00000) {  // |x| >= 1
453       if (x === -1) {
454         return -INFINITY;  // log1p(-1) = -inf
455       } else {
456         return NAN;  // log1p(x<-1) = NaN
457       }
458     } else if (ax < 0x3c900000)  {
459       // For |x| < 2^-54 we can return x.
460       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;
464     }
465
466     if ((hx > 0) || (hx <= -0x402D413D)) {  // (int) 0xbfd2bec3 = -0x402d413d
467       // -.2929 < x < 0.41422
468       k = 0;
469     }
470   }
471
472   // Handle Infinity and NAN
473   if (hx >= 0x7ff00000) return x;
474
475   if (k !== 0) {
476     if (hx < 0x43400000) {
477       // x < 2^53
478       u = 1 + x;
479       hu = %_DoubleHi(u);
480       k = (hu >> 20) - 1023;
481       c = (k > 0) ? 1 - (u - x) : x - (u - 1);
482       c = c / u;
483     } else {
484       hu = %_DoubleHi(u);
485       k = (hu >> 20) - 1023;
486     }
487     hu = hu & 0xfffff;
488     if (hu < 0x6a09e) {
489       u = %_ConstructDouble(hu | 0x3ff00000, %_DoubleLo(u));  // Normalize u.
490     } else {
491       ++k;
492       u = %_ConstructDouble(hu | 0x3fe00000, %_DoubleLo(u));  // Normalize u/2.
493       hu = (0x00100000 - hu) >> 2;
494     }
495     f = u - 1;
496   }
497
498   var hfsq = 0.5 * f * f;
499   if (hu === 0) {
500     // |f| < 2^-20;
501     if (f === 0) {
502       if (k === 0) {
503         return 0.0;
504       } else {
505         return k * LN2_HI + (c + k * LN2_LO);
506       }
507     }
508     var R = hfsq * (1 - TWO_THIRD * f);
509     if (k === 0) {
510       return f - R;
511     } else {
512       return k * LN2_HI - ((R - (k * LN2_LO + c)) - f);
513     }
514   }
515
516   var s = f / (2 + f); 
517   var z = s * s;
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)))))));
521   if (k === 0) {
522     return f - (hfsq - s * (hfsq + R));
523   } else {
524     return k * LN2_HI - ((hfsq - (s * (hfsq + R) + (k * LN2_LO + c))) - f);
525   }
526 }
527
528 // ES6 draft 09-27-13, section 20.2.2.14.
529 // Math.expm1
530 // Returns exp(x)-1, the exponential of x minus 1.
531 //
532 // Method
533 //   1. Argument reduction:
534 //      Given x, find r and integer k such that
535 //
536 //               x = k*ln2 + r,  |r| <= 0.5*ln2 ~ 0.34658  
537 //
538 //      Here a correction term c will be computed to compensate 
539 //      the error in r when rounded to a floating-point number.
540 //
541 //   2. Approximating expm1(r) by a special rational function on
542 //      the interval [0,0.34658]:
543 //      Since
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)
547 //      That is,
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
563 //          |                  5           |     -61
564 //          | 1.0+Q1*z+...+Q5*z   -  R1(z) | <= 2 
565 //          |                              |
566 //
567 //      expm1(r) = exp(r)-1 is then computed by the following 
568 //      specific way which minimize the accumulation rounding error: 
569 //                             2     3
570 //                            r     r    [ 3 - (R1 + R1*r/2)  ]
571 //            expm1(r) = r + --- + --- * [--------------------]
572 //                            2     2    [ 6 - r*(3 - R1*r/2) ]
573 //
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 
579 //      screw up:
580 //                      (      2                                    2 )
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  )
584 //                      (                                             )
585 //
586 //                 = r - E
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)) 
603 //
604 // Special cases:
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.
608 //
609 // Accuracy:
610 //      according to an error analysis, the error is always less than
611 //      1 ulp (unit in the last place).
612 //
613 // Misc. info.
614 //      For IEEE double 
615 //          if x > 7.09782712893383973096e+02 then expm1(x) overflow
616 //
617 const KEXPM1_OVERFLOW = kMath[44];
618 const INVLN2          = kMath[45];
619 macro KEXPM1(x)
620 (kMath[46+x])
621 endmacro
622
623 function MathExpm1(x) {
624   x = x * 1;  // Convert to number.
625   var y;
626   var hi;
627   var lo;
628   var k;
629   var t;
630   var c;
631     
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|
636
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;
643       }
644       if (x > KEXPM1_OVERFLOW) return INFINITY;  // Overflow
645     }
646     if (xsb != 0) return -1;  // x < -56 * ln2, return -1.
647   }
648
649   // Argument reduction
650   if (hx > 0x3fd62e42) {    // if |x| > 0.5 * ln2
651     if (hx < 0x3ff0a2b2) {  // and |x| < 1.5 * ln2
652       if (xsb === 0) {
653         hi = x - LN2_HI;
654         lo = LN2_LO;
655         k = 1;
656       } else {
657         hi = x + LN2_HI;
658         lo = -LN2_LO;
659         k = -1;
660       }
661     } else {
662       k = (INVLN2 * x + ((xsb === 0) ? 0.5 : -0.5)) | 0;
663       t = k;
664       // t * ln2_hi is exact here.
665       hi = x - t * LN2_HI;
666       lo = t * LN2_LO;
667     }
668     x = hi - lo;
669     c = (hi - x) - lo;
670   } else if (hx < 0x3c900000)   {
671     // When |x| < 2^-54, we can return x.
672     return x;
673   } else {
674     // Fall through.
675     k = 0;
676   }
677
678   // x is now in primary range
679   var hfx = 0.5 * x;
680   var hxs = x * hfx;
681   var r1 = 1 + hxs * (KEXPM1(0) + hxs * (KEXPM1(1) + hxs *
682                      (KEXPM1(2) + hxs * (KEXPM1(3) + hxs * KEXPM1(4)))));
683   t = 3 - r1 * hfx;
684   var e = hxs * ((r1 - t) / (6 - x * t));
685   if (k === 0) {  // c is 0
686     return x - (x*e - hxs);
687   } else {
688     e = (x * (e - c) - c);
689     e -= hxs;
690     if (k === -1) return 0.5 * (x - e) - 0.5;
691     if (k === 1) {
692       if (x < -0.25) return -2 * (e - (x + 0.5));
693       return 1 + 2 * (x - e);
694     }
695
696     if (k <= -2 || k > 56) {
697       // suffice to return exp(x) + 1
698       y = 1 - (e - x);
699       // Add k to y's exponent
700       y = %_ConstructDouble(%_DoubleHi(y) + (k << 20), %_DoubleLo(y));
701       return y - 1;
702     }
703     if (k < 20) {
704       // t = 1 - 2^k
705       t = %_ConstructDouble(0x3ff00000 - (0x200000 >> k), 0);
706       y = t - (e - x);
707       // Add k to y's exponent
708       y = %_ConstructDouble(%_DoubleHi(y) + (k << 20), %_DoubleLo(y));
709     } else {
710       // t = 2^-k
711       t = %_ConstructDouble((0x3ff - k) << 20, 0);
712       y = x - (e + t);
713       y += 1;
714       // Add k to y's exponent
715       y = %_ConstructDouble(%_DoubleHi(y) + (k << 20), %_DoubleLo(y));
716     }
717   }
718   return y;
719 }
720
721
722 // ES6 draft 09-27-13, section 20.2.2.30.
723 // Math.sinh
724 // Method :
725 // mathematically sinh(x) if defined to be (exp(x)-exp(-x))/2
726 //      1. Replace x by |x| (sinh(-x) = -sinh(x)).
727 //      2.
728 //                                                  E + E/(E+1)
729 //          0        <= x <= 22     :  sinh(x) := --------------, E=expm1(x)
730 //                                                      2
731 //
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)
735 //
736 // Special cases:
737 //      sinh(x) is |x| if x is +Infinity, -Infinity, or NaN.
738 //      only sinh(0)=0 is exact for finite x.
739 //
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
743
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))
748   var ax = MathAbs(x);
749   if (ax < 22) {
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));
755   }
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);
762     var t = h * w;
763     return t * w;
764   }
765   // |x| > overflowthreshold or is NaN.
766   // Return Infinity of the appropriate sign or NaN.
767   return x * INFINITY;
768 }
769
770
771 // ES6 draft 09-27-13, section 20.2.2.12.
772 // Math.cosh
773 // Method : 
774 // mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2
775 //      1. Replace x by |x| (cosh(x) = cosh(-x)). 
776 //      2.
777 //                                                      [ exp(x) - 1 ]^2 
778 //          0        <= x <= ln2/2  :  cosh(x) := 1 + -------------------
779 //                                                         2*exp(x)
780 //
781 //                                                 exp(x) + 1/exp(x)
782 //          ln2/2    <= x <= 22     :  cosh(x) := -------------------
783 //                                                        2
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)
787 //
788 // Special cases:
789 //      cosh(x) is |x| if x is +INF, -INF, or NaN.
790 //      only cosh(0)=1 is exact for finite x.
791 //
792 const KCOSH_OVERFLOW = kMath[51];
793
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));
800     var w = 1 + t;
801     // For |x| < 2^-55, cosh(x) = 1
802     if (ix < 0x3c800000) return w;
803     return 1 + (t * t) / (w + w);
804   }
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;
809   }
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));
815     var t = 0.5 * w;
816     return t * w;
817   }
818   if (NUMBER_IS_NAN(x)) return x;
819   // |x| > overflowthreshold.
820   return INFINITY;
821 }
822
823 // ES6 draft 09-27-13, section 20.2.2.21.
824 // Return the base 10 logarithm of x
825 //
826 // Method :
827 //      Let log10_2hi = leading 40 bits of log10(2) and
828 //          log10_2lo = log10(2) - log10_2hi,
829 //          ivln10   = 1/log(10) rounded.
830 //      Then
831 //              n = ilogb(x),
832 //              if(n<0)  n = n+1;
833 //              x = scalbn(x,-n);
834 //              log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))
835 //
836 // Note 1:
837 //      To guarantee log10(10**n)=n, where 10**n is normal, the rounding
838 //      mode must set to Round-to-Nearest.
839 // Note 2:
840 //      [1/log(10)] rounded to 53 bits has error .198 ulps;
841 //      log10 is monotonic at all binary break points.
842 //
843 // Special cases:
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.
848 //
849
850 const IVLN10 = kMath[52];
851 const LOG10_2HI = kMath[53];
852 const LOG10_2LO = kMath[54];
853
854 function MathLog10(x) {
855   x = x * 1;  // Convert to number.
856   var hx = %_DoubleHi(x);
857   var lx = %_DoubleLo(x);
858   var k = 0;
859
860   if (hx < 0x00100000) {
861     // x < 2^-1022
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.
867     k -= 54;
868     x *= TWO54;
869     hx = %_DoubleHi(x);
870     lx = %_DoubleLo(x);
871   }
872
873   // Infinity or NaN.
874   if (hx >= 0x7ff00000) return x;
875
876   k += (hx >> 20) - 1023;
877   i = (k & 0x80000000) >> 31;
878   hx = (hx & 0x000fffff) | ((0x3ff - i) << 20);
879   y = k + i;
880   x = %_ConstructDouble(hx, lx);
881
882   z = y * LOG10_2LO + IVLN10 * MathLog(x);
883   return z + y * LOG10_2HI;
884 }
885
886
887 // ES6 draft 09-27-13, section 20.2.2.22.
888 // Return the base 2 logarithm of x
889 //
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.
894
895 // Method:
896 // Compute log2(x) in two pieces:
897 // log2(x) = w1 + w2
898 // where w1 has 53-24 = 29 bits of trailing zeroes.
899
900 const DP_H = kMath[64];
901 const DP_L = kMath[65];
902
903 // Polynomial coefficients for (3/2)*(log2(x) - 2*s - 2/3*s^3)
904 macro KLOG2(x)
905 (kMath[55+x])
906 endmacro
907
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];
912 // 2^53
913 const TWO53 = 9007199254740992; 
914
915 function MathLog2(x) {
916   x = x * 1;  // Convert to number.
917   var ax = MathAbs(x);
918   var hx = %_DoubleHi(x);
919   var lx = %_DoubleLo(x);
920   var ix = hx & 0x7fffffff;
921
922   // Handle special cases.
923   // log2(+/- 0) = -Infinity
924   if ((ix | lx) == 0) return -INFINITY;
925
926   // log(x) = NaN, if x < 0
927   if (hx < 0) return NAN;
928
929   // log2(Infinity) = Infinity, log2(NaN) = NaN
930   if (ix >= 0x7ff00000) return x;
931
932   var n = 0;
933
934   // Take care of subnormal number.
935   if (ix < 0x00100000) {
936     ax *= TWO53;
937     n -= 53;
938     ix = %_DoubleHi(ax);
939   }
940
941   n += (ix >> 20) - 0x3ff;
942   var j = ix & 0x000fffff;
943
944   // Determine interval.
945   ix = j | 0x3ff00000;  // normalize ix.
946
947   var bp = 1;
948   var dp_h = 0;
949   var dp_l = 0;
950   if (j > 0x3988e) {  // |x| > sqrt(3/2)
951     if (j < 0xbb67a) {  // |x| < sqrt(3)
952       bp = 1.5;
953       dp_h = DP_H;
954       dp_l = DP_L;
955     } else {
956       n += 1;
957       ix -= 0x00100000;
958     }
959   }
960  
961   ax = %_ConstructDouble(ix, %_DoubleLo(ax));
962
963   // Compute ss = s_h + s_l = (x - 1)/(x+1) or (x - 1.5)/(x + 1.5)
964   var u = ax - bp;
965   var v = 1 / (ax + bp);
966   var ss = u * v;
967   var s_h = %_ConstructDouble(%_DoubleHi(ss), 0);
968
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);
973
974   // Compute log2(ax)
975   var s2 = ss * ss;
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);
979   s2  = s_h * s_h;
980   t_h = %_ConstructDouble(%_DoubleHi(3.0 + s2 + r), 0);
981   t_l = r - ((t_h - 3.0) - s2);
982   // u + v = ss * (1 + ...)
983   u = s_h * t_h;
984   v = s_l * t_h + t_l * ss;
985
986   // 2 / (3 * log(2)) * (ss + ...)
987   p_h = %_ConstructDouble(%_DoubleHi(u + v), 0);
988   p_l = v - (p_h - u);
989   z_h = CP_H * p_h;
990   z_l = CP_L * p_h + p_l * CP + dp_l;
991
992   // log2(ax) = (ss + ...) * 2 / (3 * log(2)) = n + dp_h + z_h + z_l
993   var t = n;
994   var t1 = %_ConstructDouble(%_DoubleHi(((z_h + z_l) + dp_h) + t), 0);
995   var t2 = z_l - (((t1 - t) - dp_h) - z_h);
996
997   // t1 + t2 = log2(ax), sum up because we do not care about extra precision.
998   return t1 + t2;
999 }