1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci *
15425bb815Sopenharmony_ci * This file is based on work under the following copyright and permission
16425bb815Sopenharmony_ci * notice:
17425bb815Sopenharmony_ci *
18425bb815Sopenharmony_ci *     Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
19425bb815Sopenharmony_ci *
20425bb815Sopenharmony_ci *     Permission to use, copy, modify, and distribute this
21425bb815Sopenharmony_ci *     software is freely granted, provided that this notice
22425bb815Sopenharmony_ci *     is preserved.
23425bb815Sopenharmony_ci *
24425bb815Sopenharmony_ci *     @(#)s_expm1.c 5.1 93/09/24
25425bb815Sopenharmony_ci */
26425bb815Sopenharmony_ci
27425bb815Sopenharmony_ci#include "jerry-libm-internal.h"
28425bb815Sopenharmony_ci
29425bb815Sopenharmony_ci/* expm1(x)
30425bb815Sopenharmony_ci * Returns exp(x)-1, the exponential of x minus 1.
31425bb815Sopenharmony_ci *
32425bb815Sopenharmony_ci * Method
33425bb815Sopenharmony_ci *   1. Argument reduction:
34425bb815Sopenharmony_ci *  Given x, find r and integer k such that
35425bb815Sopenharmony_ci *
36425bb815Sopenharmony_ci *               x = k*ln2 + r,  |r| <= 0.5*ln2 ~ 0.34658
37425bb815Sopenharmony_ci *
38425bb815Sopenharmony_ci *      Here a correction term c will be computed to compensate
39425bb815Sopenharmony_ci *  the error in r when rounded to a floating-point number.
40425bb815Sopenharmony_ci *
41425bb815Sopenharmony_ci *   2. Approximating expm1(r) by a special rational function on
42425bb815Sopenharmony_ci *  the interval [0,0.34658]:
43425bb815Sopenharmony_ci *  Since
44425bb815Sopenharmony_ci *      r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
45425bb815Sopenharmony_ci *  we define R1(r*r) by
46425bb815Sopenharmony_ci *      r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
47425bb815Sopenharmony_ci *  That is,
48425bb815Sopenharmony_ci *      R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
49425bb815Sopenharmony_ci *         = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
50425bb815Sopenharmony_ci *         = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
51425bb815Sopenharmony_ci *      We use a special Reme algorithm on [0,0.347] to generate
52425bb815Sopenharmony_ci *   a polynomial of degree 5 in r*r to approximate R1. The
53425bb815Sopenharmony_ci *  maximum error of this polynomial approximation is bounded
54425bb815Sopenharmony_ci *  by 2**-61. In other words,
55425bb815Sopenharmony_ci *      R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
56425bb815Sopenharmony_ci *  where   Q1  =  -1.6666666666666567384E-2,
57425bb815Sopenharmony_ci *     Q2  =   3.9682539681370365873E-4,
58425bb815Sopenharmony_ci *     Q3  =  -9.9206344733435987357E-6,
59425bb815Sopenharmony_ci *     Q4  =   2.5051361420808517002E-7,
60425bb815Sopenharmony_ci *     Q5  =  -6.2843505682382617102E-9;
61425bb815Sopenharmony_ci *    z   =  r*r,
62425bb815Sopenharmony_ci *  with error bounded by
63425bb815Sopenharmony_ci *      |                  5           |     -61
64425bb815Sopenharmony_ci *      | 1.0+Q1*z+...+Q5*z   -  R1(z) | <= 2
65425bb815Sopenharmony_ci *      |                              |
66425bb815Sopenharmony_ci *
67425bb815Sopenharmony_ci *  expm1(r) = exp(r)-1 is then computed by the following
68425bb815Sopenharmony_ci *   specific way which minimize the accumulation rounding error:
69425bb815Sopenharmony_ci *                        2     3
70425bb815Sopenharmony_ci *                        r     r    [ 3 - (R1 + R1*r/2)  ]
71425bb815Sopenharmony_ci *        expm1(r) = r + --- + --- * [--------------------]
72425bb815Sopenharmony_ci *                        2     2    [ 6 - r*(3 - R1*r/2) ]
73425bb815Sopenharmony_ci *
74425bb815Sopenharmony_ci *  To compensate the error in the argument reduction, we use
75425bb815Sopenharmony_ci *    expm1(r+c) = expm1(r) + c + expm1(r)*c
76425bb815Sopenharmony_ci *         ~ expm1(r) + c + r*c
77425bb815Sopenharmony_ci *  Thus c+r*c will be added in as the correction terms for
78425bb815Sopenharmony_ci *  expm1(r+c). Now rearrange the term to avoid optimization
79425bb815Sopenharmony_ci *   screw up:
80425bb815Sopenharmony_ci *                  (      2                                    2 )
81425bb815Sopenharmony_ci *                  ({  ( r    [ R1 -  (3 - R1*r/2) ]  )  }    r  )
82425bb815Sopenharmony_ci *   expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
83425bb815Sopenharmony_ci *                  ({  ( 2    [ 6 - r*(3 - R1*r/2) ]  )  }    2  )
84425bb815Sopenharmony_ci *                  (                                             )
85425bb815Sopenharmony_ci *
86425bb815Sopenharmony_ci *       = r - E
87425bb815Sopenharmony_ci *   3. Scale back to obtain expm1(x):
88425bb815Sopenharmony_ci *  From step 1, we have
89425bb815Sopenharmony_ci *     expm1(x) = either 2^k*[expm1(r)+1] - 1
90425bb815Sopenharmony_ci *              = or     2^k*[expm1(r) + (1-2^-k)]
91425bb815Sopenharmony_ci *   4. Implementation notes:
92425bb815Sopenharmony_ci *  (A). To save one multiplication, we scale the coefficient Qi
93425bb815Sopenharmony_ci *       to Qi*2^i, and replace z by (x^2)/2.
94425bb815Sopenharmony_ci *  (B). To achieve maximum accuracy, we compute expm1(x) by
95425bb815Sopenharmony_ci *    (i)   if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
96425bb815Sopenharmony_ci *    (ii)  if k=0, return r-E
97425bb815Sopenharmony_ci *    (iii) if k=-1, return 0.5*(r-E)-0.5
98425bb815Sopenharmony_ci *    (iv)  if k=1 if r < -0.25, return 2*((r+0.5)- E)
99425bb815Sopenharmony_ci *                  else       return  1.0+2.0*(r-E);
100425bb815Sopenharmony_ci *    (v)   if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
101425bb815Sopenharmony_ci *    (vi)  if k <= 20, return 2^k((1-2^-k)-(E-r)), else
102425bb815Sopenharmony_ci *    (vii) return 2^k(1-((E+2^-k)-r))
103425bb815Sopenharmony_ci *
104425bb815Sopenharmony_ci * Special cases:
105425bb815Sopenharmony_ci *  expm1(INF) is INF, expm1(NaN) is NaN;
106425bb815Sopenharmony_ci *  expm1(-INF) is -1, and
107425bb815Sopenharmony_ci *  for finite argument, only expm1(0)=0 is exact.
108425bb815Sopenharmony_ci *
109425bb815Sopenharmony_ci * Accuracy:
110425bb815Sopenharmony_ci *  according to an error analysis, the error is always less than
111425bb815Sopenharmony_ci *  1 ulp (unit in the last place).
112425bb815Sopenharmony_ci *
113425bb815Sopenharmony_ci * Misc. info.
114425bb815Sopenharmony_ci *  For IEEE double
115425bb815Sopenharmony_ci *      if x >  7.09782712893383973096e+02 then expm1(x) overflow
116425bb815Sopenharmony_ci *
117425bb815Sopenharmony_ci * Constants:
118425bb815Sopenharmony_ci * The hexadecimal values are the intended ones for the following
119425bb815Sopenharmony_ci * constants. The decimal values may be used, provided that the
120425bb815Sopenharmony_ci * compiler will convert from decimal to binary accurately enough
121425bb815Sopenharmony_ci * to produce the hexadecimal values shown.
122425bb815Sopenharmony_ci */
123425bb815Sopenharmony_ci
124425bb815Sopenharmony_ci#define one 1.0
125425bb815Sopenharmony_ci#define huge 1.0e+300
126425bb815Sopenharmony_ci#define tiny 1.0e-300
127425bb815Sopenharmony_ci#define o_threshold 7.09782712893383973096e+02 /* 0x40862E42, 0xFEFA39EF */
128425bb815Sopenharmony_ci#define ln2_hi 6.93147180369123816490e-01      /* 0x3fe62e42, 0xfee00000 */
129425bb815Sopenharmony_ci#define ln2_lo 1.90821492927058770002e-10      /* 0x3dea39ef, 0x35793c76 */
130425bb815Sopenharmony_ci#define invln2 1.44269504088896338700e+00      /* 0x3ff71547, 0x652b82fe */
131425bb815Sopenharmony_ci
132425bb815Sopenharmony_ci/* Scaled Q's: Qn_here = 2**n * Qn_above, for R(2*z) where z = hxs = x*x/2: */
133425bb815Sopenharmony_ci#define Q1 -3.33333333333331316428e-02 /* BFA11111 111110F4 */
134425bb815Sopenharmony_ci#define Q2 1.58730158725481460165e-03  /* 3F5A01A0 19FE5585 */
135425bb815Sopenharmony_ci#define Q3 -7.93650757867487942473e-05 /* BF14CE19 9EAADBB7 */
136425bb815Sopenharmony_ci#define Q4 4.00821782732936239552e-06  /* 3ED0CFCA 86E65239 */
137425bb815Sopenharmony_ci#define Q5 -2.01099218183624371326e-07 /* BE8AFDB7 6E09C32D */
138425bb815Sopenharmony_ci
139425bb815Sopenharmony_cidouble
140425bb815Sopenharmony_ciexpm1 (double x)
141425bb815Sopenharmony_ci{
142425bb815Sopenharmony_ci  double y, hi, lo, c, e, hxs, hfx, r1;
143425bb815Sopenharmony_ci  double_accessor t, twopk;
144425bb815Sopenharmony_ci  int k, xsb;
145425bb815Sopenharmony_ci  unsigned int hx;
146425bb815Sopenharmony_ci
147425bb815Sopenharmony_ci  hx = __HI (x);
148425bb815Sopenharmony_ci  xsb = hx & 0x80000000; /* sign bit of x */
149425bb815Sopenharmony_ci  hx &= 0x7fffffff;      /* high word of |x| */
150425bb815Sopenharmony_ci
151425bb815Sopenharmony_ci  /* filter out huge and non-finite argument */
152425bb815Sopenharmony_ci  if (hx >= 0x4043687A)
153425bb815Sopenharmony_ci  {
154425bb815Sopenharmony_ci    /* if |x|>=56*ln2 */
155425bb815Sopenharmony_ci    if (hx >= 0x40862E42)
156425bb815Sopenharmony_ci    {
157425bb815Sopenharmony_ci      /* if |x|>=709.78... */
158425bb815Sopenharmony_ci      if (hx >= 0x7ff00000)
159425bb815Sopenharmony_ci      {
160425bb815Sopenharmony_ci        unsigned int low;
161425bb815Sopenharmony_ci        low = __LO (x);
162425bb815Sopenharmony_ci        if (((hx & 0xfffff) | low) != 0)
163425bb815Sopenharmony_ci        {
164425bb815Sopenharmony_ci          /* NaN */
165425bb815Sopenharmony_ci          return x + x;
166425bb815Sopenharmony_ci        }
167425bb815Sopenharmony_ci        else
168425bb815Sopenharmony_ci        {
169425bb815Sopenharmony_ci          /* exp(+-inf)-1={inf,-1} */
170425bb815Sopenharmony_ci          return (xsb == 0) ? x : -1.0;
171425bb815Sopenharmony_ci        }
172425bb815Sopenharmony_ci      }
173425bb815Sopenharmony_ci      if (x > o_threshold)
174425bb815Sopenharmony_ci      {
175425bb815Sopenharmony_ci        /* overflow */
176425bb815Sopenharmony_ci        return huge * huge;
177425bb815Sopenharmony_ci      }
178425bb815Sopenharmony_ci    }
179425bb815Sopenharmony_ci    if (xsb != 0)
180425bb815Sopenharmony_ci    {
181425bb815Sopenharmony_ci      /* x < -56*ln2, return -1.0 with inexact */
182425bb815Sopenharmony_ci      if (x + tiny < 0.0) /* raise inexact */
183425bb815Sopenharmony_ci      {
184425bb815Sopenharmony_ci        /* return -1 */
185425bb815Sopenharmony_ci        return tiny - one;
186425bb815Sopenharmony_ci      }
187425bb815Sopenharmony_ci    }
188425bb815Sopenharmony_ci  }
189425bb815Sopenharmony_ci
190425bb815Sopenharmony_ci  /* argument reduction */
191425bb815Sopenharmony_ci  if (hx > 0x3fd62e42)
192425bb815Sopenharmony_ci  {
193425bb815Sopenharmony_ci    /* if  |x| > 0.5 ln2 */
194425bb815Sopenharmony_ci    if (hx < 0x3FF0A2B2)
195425bb815Sopenharmony_ci    {
196425bb815Sopenharmony_ci      /* and |x| < 1.5 ln2 */
197425bb815Sopenharmony_ci      if (xsb == 0)
198425bb815Sopenharmony_ci      {
199425bb815Sopenharmony_ci        hi = x - ln2_hi;
200425bb815Sopenharmony_ci        lo = ln2_lo;
201425bb815Sopenharmony_ci        k = 1;
202425bb815Sopenharmony_ci      }
203425bb815Sopenharmony_ci      else
204425bb815Sopenharmony_ci      {
205425bb815Sopenharmony_ci        hi = x + ln2_hi;
206425bb815Sopenharmony_ci        lo = -ln2_lo;
207425bb815Sopenharmony_ci        k = -1;
208425bb815Sopenharmony_ci      }
209425bb815Sopenharmony_ci    }
210425bb815Sopenharmony_ci    else
211425bb815Sopenharmony_ci    {
212425bb815Sopenharmony_ci      k = (int) (invln2 * x + ((xsb == 0) ? 0.5 : -0.5));
213425bb815Sopenharmony_ci      t.dbl = k;
214425bb815Sopenharmony_ci      hi = x - t.dbl * ln2_hi; /* t*ln2_hi is exact here */
215425bb815Sopenharmony_ci      lo = t.dbl * ln2_lo;
216425bb815Sopenharmony_ci    }
217425bb815Sopenharmony_ci    x = hi - lo;
218425bb815Sopenharmony_ci    c = (hi - x) - lo;
219425bb815Sopenharmony_ci  }
220425bb815Sopenharmony_ci  else if (hx < 0x3c900000)
221425bb815Sopenharmony_ci  {
222425bb815Sopenharmony_ci    /* when |x|<2**-54, return x */
223425bb815Sopenharmony_ci    return x;
224425bb815Sopenharmony_ci  }
225425bb815Sopenharmony_ci  else
226425bb815Sopenharmony_ci  {
227425bb815Sopenharmony_ci    k = 0;
228425bb815Sopenharmony_ci  }
229425bb815Sopenharmony_ci
230425bb815Sopenharmony_ci  /* x is now in primary range */
231425bb815Sopenharmony_ci  hfx = 0.5 * x;
232425bb815Sopenharmony_ci  hxs = x * hfx;
233425bb815Sopenharmony_ci  r1 = one + hxs * (Q1 + hxs * (Q2 + hxs * (Q3 + hxs * (Q4 + hxs * Q5))));
234425bb815Sopenharmony_ci  t.dbl = 3.0 - r1 * hfx;
235425bb815Sopenharmony_ci  e = hxs * ((r1 - t.dbl) / (6.0 - x * t.dbl));
236425bb815Sopenharmony_ci  if (k == 0)
237425bb815Sopenharmony_ci  {
238425bb815Sopenharmony_ci    /* c is 0 */
239425bb815Sopenharmony_ci    return x - (x * e - hxs);
240425bb815Sopenharmony_ci  }
241425bb815Sopenharmony_ci  else
242425bb815Sopenharmony_ci  {
243425bb815Sopenharmony_ci    twopk.as_int.hi = 0x3ff00000 + ((unsigned int) k << 20); /* 2^k */
244425bb815Sopenharmony_ci    twopk.as_int.lo = 0;
245425bb815Sopenharmony_ci    e = (x * (e - c) - c);
246425bb815Sopenharmony_ci    e -= hxs;
247425bb815Sopenharmony_ci    if (k == -1)
248425bb815Sopenharmony_ci    {
249425bb815Sopenharmony_ci      return 0.5 * (x - e) - 0.5;
250425bb815Sopenharmony_ci    }
251425bb815Sopenharmony_ci    if (k == 1)
252425bb815Sopenharmony_ci    {
253425bb815Sopenharmony_ci      if (x < -0.25)
254425bb815Sopenharmony_ci      {
255425bb815Sopenharmony_ci        return -2.0 * (e - (x + 0.5));
256425bb815Sopenharmony_ci      }
257425bb815Sopenharmony_ci      else
258425bb815Sopenharmony_ci      {
259425bb815Sopenharmony_ci        return one + 2.0 * (x - e);
260425bb815Sopenharmony_ci      }
261425bb815Sopenharmony_ci    }
262425bb815Sopenharmony_ci    if ((k <= -2) || (k > 56))
263425bb815Sopenharmony_ci    {
264425bb815Sopenharmony_ci      /* suffice to return exp(x)-1 */
265425bb815Sopenharmony_ci      y = one - (e - x);
266425bb815Sopenharmony_ci      if (k == 1024)
267425bb815Sopenharmony_ci      {
268425bb815Sopenharmony_ci        y = y * 2.0 * 0x1p1023;
269425bb815Sopenharmony_ci      }
270425bb815Sopenharmony_ci      else
271425bb815Sopenharmony_ci      {
272425bb815Sopenharmony_ci        y = y * twopk.dbl;
273425bb815Sopenharmony_ci      }
274425bb815Sopenharmony_ci      return y - one;
275425bb815Sopenharmony_ci    }
276425bb815Sopenharmony_ci    t.dbl = one;
277425bb815Sopenharmony_ci    if (k < 20)
278425bb815Sopenharmony_ci    {
279425bb815Sopenharmony_ci      t.as_int.hi = (0x3ff00000 - (0x200000 >> k)); /* t=1-2^-k */
280425bb815Sopenharmony_ci      y = t.dbl - (e - x);
281425bb815Sopenharmony_ci      y = y * twopk.dbl;
282425bb815Sopenharmony_ci    }
283425bb815Sopenharmony_ci    else
284425bb815Sopenharmony_ci    {
285425bb815Sopenharmony_ci      t.as_int.hi = ((0x3ff - k) << 20); /* 2^-k */
286425bb815Sopenharmony_ci      y = x - (e + t.dbl);
287425bb815Sopenharmony_ci      y += one;
288425bb815Sopenharmony_ci      y = y * twopk.dbl;
289425bb815Sopenharmony_ci    }
290425bb815Sopenharmony_ci  }
291425bb815Sopenharmony_ci  return y;
292425bb815Sopenharmony_ci} /* expm1 */
293425bb815Sopenharmony_ci
294425bb815Sopenharmony_ci#undef one
295425bb815Sopenharmony_ci#undef huge
296425bb815Sopenharmony_ci#undef tiny
297425bb815Sopenharmony_ci#undef o_threshold
298425bb815Sopenharmony_ci#undef ln2_hi
299425bb815Sopenharmony_ci#undef ln2_lo
300425bb815Sopenharmony_ci#undef invln2
301425bb815Sopenharmony_ci#undef Q1
302425bb815Sopenharmony_ci#undef Q2
303425bb815Sopenharmony_ci#undef Q3
304425bb815Sopenharmony_ci#undef Q4
305425bb815Sopenharmony_ci#undef Q5
306