xref: /haiku/src/system/libroot/posix/musl/math/expm1.c (revision f504f61099b010fbfa94b1cc63d2e9072c7f7185)
1*f504f610SAugustin Cavalier /* origin: FreeBSD /usr/src/lib/msun/src/s_expm1.c */
2*f504f610SAugustin Cavalier /*
3*f504f610SAugustin Cavalier  * ====================================================
4*f504f610SAugustin Cavalier  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*f504f610SAugustin Cavalier  *
6*f504f610SAugustin Cavalier  * Developed at SunPro, a Sun Microsystems, Inc. business.
7*f504f610SAugustin Cavalier  * Permission to use, copy, modify, and distribute this
8*f504f610SAugustin Cavalier  * software is freely granted, provided that this notice
9*f504f610SAugustin Cavalier  * is preserved.
10*f504f610SAugustin Cavalier  * ====================================================
11*f504f610SAugustin Cavalier  */
12*f504f610SAugustin Cavalier /* expm1(x)
13*f504f610SAugustin Cavalier  * Returns exp(x)-1, the exponential of x minus 1.
14*f504f610SAugustin Cavalier  *
15*f504f610SAugustin Cavalier  * Method
16*f504f610SAugustin Cavalier  *   1. Argument reduction:
17*f504f610SAugustin Cavalier  *      Given x, find r and integer k such that
18*f504f610SAugustin Cavalier  *
19*f504f610SAugustin Cavalier  *               x = k*ln2 + r,  |r| <= 0.5*ln2 ~ 0.34658
20*f504f610SAugustin Cavalier  *
21*f504f610SAugustin Cavalier  *      Here a correction term c will be computed to compensate
22*f504f610SAugustin Cavalier  *      the error in r when rounded to a floating-point number.
23*f504f610SAugustin Cavalier  *
24*f504f610SAugustin Cavalier  *   2. Approximating expm1(r) by a special rational function on
25*f504f610SAugustin Cavalier  *      the interval [0,0.34658]:
26*f504f610SAugustin Cavalier  *      Since
27*f504f610SAugustin Cavalier  *          r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
28*f504f610SAugustin Cavalier  *      we define R1(r*r) by
29*f504f610SAugustin Cavalier  *          r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
30*f504f610SAugustin Cavalier  *      That is,
31*f504f610SAugustin Cavalier  *          R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
32*f504f610SAugustin Cavalier  *                   = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
33*f504f610SAugustin Cavalier  *                   = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
34*f504f610SAugustin Cavalier  *      We use a special Remez algorithm on [0,0.347] to generate
35*f504f610SAugustin Cavalier  *      a polynomial of degree 5 in r*r to approximate R1. The
36*f504f610SAugustin Cavalier  *      maximum error of this polynomial approximation is bounded
37*f504f610SAugustin Cavalier  *      by 2**-61. In other words,
38*f504f610SAugustin Cavalier  *          R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
39*f504f610SAugustin Cavalier  *      where   Q1  =  -1.6666666666666567384E-2,
40*f504f610SAugustin Cavalier  *              Q2  =   3.9682539681370365873E-4,
41*f504f610SAugustin Cavalier  *              Q3  =  -9.9206344733435987357E-6,
42*f504f610SAugustin Cavalier  *              Q4  =   2.5051361420808517002E-7,
43*f504f610SAugustin Cavalier  *              Q5  =  -6.2843505682382617102E-9;
44*f504f610SAugustin Cavalier  *              z   =  r*r,
45*f504f610SAugustin Cavalier  *      with error bounded by
46*f504f610SAugustin Cavalier  *          |                  5           |     -61
47*f504f610SAugustin Cavalier  *          | 1.0+Q1*z+...+Q5*z   -  R1(z) | <= 2
48*f504f610SAugustin Cavalier  *          |                              |
49*f504f610SAugustin Cavalier  *
50*f504f610SAugustin Cavalier  *      expm1(r) = exp(r)-1 is then computed by the following
51*f504f610SAugustin Cavalier  *      specific way which minimize the accumulation rounding error:
52*f504f610SAugustin Cavalier  *                             2     3
53*f504f610SAugustin Cavalier  *                            r     r    [ 3 - (R1 + R1*r/2)  ]
54*f504f610SAugustin Cavalier  *            expm1(r) = r + --- + --- * [--------------------]
55*f504f610SAugustin Cavalier  *                            2     2    [ 6 - r*(3 - R1*r/2) ]
56*f504f610SAugustin Cavalier  *
57*f504f610SAugustin Cavalier  *      To compensate the error in the argument reduction, we use
58*f504f610SAugustin Cavalier  *              expm1(r+c) = expm1(r) + c + expm1(r)*c
59*f504f610SAugustin Cavalier  *                         ~ expm1(r) + c + r*c
60*f504f610SAugustin Cavalier  *      Thus c+r*c will be added in as the correction terms for
61*f504f610SAugustin Cavalier  *      expm1(r+c). Now rearrange the term to avoid optimization
62*f504f610SAugustin Cavalier  *      screw up:
63*f504f610SAugustin Cavalier  *                      (      2                                    2 )
64*f504f610SAugustin Cavalier  *                      ({  ( r    [ R1 -  (3 - R1*r/2) ]  )  }    r  )
65*f504f610SAugustin Cavalier  *       expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
66*f504f610SAugustin Cavalier  *                      ({  ( 2    [ 6 - r*(3 - R1*r/2) ]  )  }    2  )
67*f504f610SAugustin Cavalier  *                      (                                             )
68*f504f610SAugustin Cavalier  *
69*f504f610SAugustin Cavalier  *                 = r - E
70*f504f610SAugustin Cavalier  *   3. Scale back to obtain expm1(x):
71*f504f610SAugustin Cavalier  *      From step 1, we have
72*f504f610SAugustin Cavalier  *         expm1(x) = either 2^k*[expm1(r)+1] - 1
73*f504f610SAugustin Cavalier  *                  = or     2^k*[expm1(r) + (1-2^-k)]
74*f504f610SAugustin Cavalier  *   4. Implementation notes:
75*f504f610SAugustin Cavalier  *      (A). To save one multiplication, we scale the coefficient Qi
76*f504f610SAugustin Cavalier  *           to Qi*2^i, and replace z by (x^2)/2.
77*f504f610SAugustin Cavalier  *      (B). To achieve maximum accuracy, we compute expm1(x) by
78*f504f610SAugustin Cavalier  *        (i)   if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
79*f504f610SAugustin Cavalier  *        (ii)  if k=0, return r-E
80*f504f610SAugustin Cavalier  *        (iii) if k=-1, return 0.5*(r-E)-0.5
81*f504f610SAugustin Cavalier  *        (iv)  if k=1 if r < -0.25, return 2*((r+0.5)- E)
82*f504f610SAugustin Cavalier  *                     else          return  1.0+2.0*(r-E);
83*f504f610SAugustin Cavalier  *        (v)   if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
84*f504f610SAugustin Cavalier  *        (vi)  if k <= 20, return 2^k((1-2^-k)-(E-r)), else
85*f504f610SAugustin Cavalier  *        (vii) return 2^k(1-((E+2^-k)-r))
86*f504f610SAugustin Cavalier  *
87*f504f610SAugustin Cavalier  * Special cases:
88*f504f610SAugustin Cavalier  *      expm1(INF) is INF, expm1(NaN) is NaN;
89*f504f610SAugustin Cavalier  *      expm1(-INF) is -1, and
90*f504f610SAugustin Cavalier  *      for finite argument, only expm1(0)=0 is exact.
91*f504f610SAugustin Cavalier  *
92*f504f610SAugustin Cavalier  * Accuracy:
93*f504f610SAugustin Cavalier  *      according to an error analysis, the error is always less than
94*f504f610SAugustin Cavalier  *      1 ulp (unit in the last place).
95*f504f610SAugustin Cavalier  *
96*f504f610SAugustin Cavalier  * Misc. info.
97*f504f610SAugustin Cavalier  *      For IEEE double
98*f504f610SAugustin Cavalier  *          if x >  7.09782712893383973096e+02 then expm1(x) overflow
99*f504f610SAugustin Cavalier  *
100*f504f610SAugustin Cavalier  * Constants:
101*f504f610SAugustin Cavalier  * The hexadecimal values are the intended ones for the following
102*f504f610SAugustin Cavalier  * constants. The decimal values may be used, provided that the
103*f504f610SAugustin Cavalier  * compiler will convert from decimal to binary accurately enough
104*f504f610SAugustin Cavalier  * to produce the hexadecimal values shown.
105*f504f610SAugustin Cavalier  */
106*f504f610SAugustin Cavalier 
107*f504f610SAugustin Cavalier #include "libm.h"
108*f504f610SAugustin Cavalier 
109*f504f610SAugustin Cavalier static const double
110*f504f610SAugustin Cavalier o_threshold = 7.09782712893383973096e+02, /* 0x40862E42, 0xFEFA39EF */
111*f504f610SAugustin Cavalier ln2_hi      = 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */
112*f504f610SAugustin Cavalier ln2_lo      = 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */
113*f504f610SAugustin Cavalier invln2      = 1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */
114*f504f610SAugustin Cavalier /* Scaled Q's: Qn_here = 2**n * Qn_above, for R(2*z) where z = hxs = x*x/2: */
115*f504f610SAugustin Cavalier Q1 = -3.33333333333331316428e-02, /* BFA11111 111110F4 */
116*f504f610SAugustin Cavalier Q2 =  1.58730158725481460165e-03, /* 3F5A01A0 19FE5585 */
117*f504f610SAugustin Cavalier Q3 = -7.93650757867487942473e-05, /* BF14CE19 9EAADBB7 */
118*f504f610SAugustin Cavalier Q4 =  4.00821782732936239552e-06, /* 3ED0CFCA 86E65239 */
119*f504f610SAugustin Cavalier Q5 = -2.01099218183624371326e-07; /* BE8AFDB7 6E09C32D */
120*f504f610SAugustin Cavalier 
expm1(double x)121*f504f610SAugustin Cavalier double expm1(double x)
122*f504f610SAugustin Cavalier {
123*f504f610SAugustin Cavalier 	double_t y,hi,lo,c,t,e,hxs,hfx,r1,twopk;
124*f504f610SAugustin Cavalier 	union {double f; uint64_t i;} u = {x};
125*f504f610SAugustin Cavalier 	uint32_t hx = u.i>>32 & 0x7fffffff;
126*f504f610SAugustin Cavalier 	int k, sign = u.i>>63;
127*f504f610SAugustin Cavalier 
128*f504f610SAugustin Cavalier 	/* filter out huge and non-finite argument */
129*f504f610SAugustin Cavalier 	if (hx >= 0x4043687A) {  /* if |x|>=56*ln2 */
130*f504f610SAugustin Cavalier 		if (isnan(x))
131*f504f610SAugustin Cavalier 			return x;
132*f504f610SAugustin Cavalier 		if (sign)
133*f504f610SAugustin Cavalier 			return -1;
134*f504f610SAugustin Cavalier 		if (x > o_threshold) {
135*f504f610SAugustin Cavalier 			x *= 0x1p1023;
136*f504f610SAugustin Cavalier 			return x;
137*f504f610SAugustin Cavalier 		}
138*f504f610SAugustin Cavalier 	}
139*f504f610SAugustin Cavalier 
140*f504f610SAugustin Cavalier 	/* argument reduction */
141*f504f610SAugustin Cavalier 	if (hx > 0x3fd62e42) {  /* if  |x| > 0.5 ln2 */
142*f504f610SAugustin Cavalier 		if (hx < 0x3FF0A2B2) {  /* and |x| < 1.5 ln2 */
143*f504f610SAugustin Cavalier 			if (!sign) {
144*f504f610SAugustin Cavalier 				hi = x - ln2_hi;
145*f504f610SAugustin Cavalier 				lo = ln2_lo;
146*f504f610SAugustin Cavalier 				k =  1;
147*f504f610SAugustin Cavalier 			} else {
148*f504f610SAugustin Cavalier 				hi = x + ln2_hi;
149*f504f610SAugustin Cavalier 				lo = -ln2_lo;
150*f504f610SAugustin Cavalier 				k = -1;
151*f504f610SAugustin Cavalier 			}
152*f504f610SAugustin Cavalier 		} else {
153*f504f610SAugustin Cavalier 			k  = invln2*x + (sign ? -0.5 : 0.5);
154*f504f610SAugustin Cavalier 			t  = k;
155*f504f610SAugustin Cavalier 			hi = x - t*ln2_hi;  /* t*ln2_hi is exact here */
156*f504f610SAugustin Cavalier 			lo = t*ln2_lo;
157*f504f610SAugustin Cavalier 		}
158*f504f610SAugustin Cavalier 		x = hi-lo;
159*f504f610SAugustin Cavalier 		c = (hi-x)-lo;
160*f504f610SAugustin Cavalier 	} else if (hx < 0x3c900000) {  /* |x| < 2**-54, return x */
161*f504f610SAugustin Cavalier 		if (hx < 0x00100000)
162*f504f610SAugustin Cavalier 			FORCE_EVAL((float)x);
163*f504f610SAugustin Cavalier 		return x;
164*f504f610SAugustin Cavalier 	} else
165*f504f610SAugustin Cavalier 		k = 0;
166*f504f610SAugustin Cavalier 
167*f504f610SAugustin Cavalier 	/* x is now in primary range */
168*f504f610SAugustin Cavalier 	hfx = 0.5*x;
169*f504f610SAugustin Cavalier 	hxs = x*hfx;
170*f504f610SAugustin Cavalier 	r1 = 1.0+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5))));
171*f504f610SAugustin Cavalier 	t  = 3.0-r1*hfx;
172*f504f610SAugustin Cavalier 	e  = hxs*((r1-t)/(6.0 - x*t));
173*f504f610SAugustin Cavalier 	if (k == 0)   /* c is 0 */
174*f504f610SAugustin Cavalier 		return x - (x*e-hxs);
175*f504f610SAugustin Cavalier 	e  = x*(e-c) - c;
176*f504f610SAugustin Cavalier 	e -= hxs;
177*f504f610SAugustin Cavalier 	/* exp(x) ~ 2^k (x_reduced - e + 1) */
178*f504f610SAugustin Cavalier 	if (k == -1)
179*f504f610SAugustin Cavalier 		return 0.5*(x-e) - 0.5;
180*f504f610SAugustin Cavalier 	if (k == 1) {
181*f504f610SAugustin Cavalier 		if (x < -0.25)
182*f504f610SAugustin Cavalier 			return -2.0*(e-(x+0.5));
183*f504f610SAugustin Cavalier 		return 1.0+2.0*(x-e);
184*f504f610SAugustin Cavalier 	}
185*f504f610SAugustin Cavalier 	u.i = (uint64_t)(0x3ff + k)<<52;  /* 2^k */
186*f504f610SAugustin Cavalier 	twopk = u.f;
187*f504f610SAugustin Cavalier 	if (k < 0 || k > 56) {  /* suffice to return exp(x)-1 */
188*f504f610SAugustin Cavalier 		y = x - e + 1.0;
189*f504f610SAugustin Cavalier 		if (k == 1024)
190*f504f610SAugustin Cavalier 			y = y*2.0*0x1p1023;
191*f504f610SAugustin Cavalier 		else
192*f504f610SAugustin Cavalier 			y = y*twopk;
193*f504f610SAugustin Cavalier 		return y - 1.0;
194*f504f610SAugustin Cavalier 	}
195*f504f610SAugustin Cavalier 	u.i = (uint64_t)(0x3ff - k)<<52;  /* 2^-k */
196*f504f610SAugustin Cavalier 	if (k < 20)
197*f504f610SAugustin Cavalier 		y = (x-e+(1-u.f))*twopk;
198*f504f610SAugustin Cavalier 	else
199*f504f610SAugustin Cavalier 		y = (x-(e+u.f)+1)*twopk;
200*f504f610SAugustin Cavalier 	return y;
201*f504f610SAugustin Cavalier }
202