xref: /freebsd/lib/msun/src/s_expm1.c (revision 0dd5a5603e7a33d976f8e6015620bbc79839c609)
13a8617a8SJordan K. Hubbard /*
23a8617a8SJordan K. Hubbard  * ====================================================
33a8617a8SJordan K. Hubbard  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
43a8617a8SJordan K. Hubbard  *
53a8617a8SJordan K. Hubbard  * Developed at SunPro, a Sun Microsystems, Inc. business.
63a8617a8SJordan K. Hubbard  * Permission to use, copy, modify, and distribute this
73a8617a8SJordan K. Hubbard  * software is freely granted, provided that this notice
83a8617a8SJordan K. Hubbard  * is preserved.
93a8617a8SJordan K. Hubbard  * ====================================================
103a8617a8SJordan K. Hubbard  */
113a8617a8SJordan K. Hubbard 
123a8617a8SJordan K. Hubbard /* expm1(x)
133a8617a8SJordan K. Hubbard  * Returns exp(x)-1, the exponential of x minus 1.
143a8617a8SJordan K. Hubbard  *
153a8617a8SJordan K. Hubbard  * Method
163a8617a8SJordan K. Hubbard  *   1. Argument reduction:
173a8617a8SJordan K. Hubbard  *	Given x, find r and integer k such that
183a8617a8SJordan K. Hubbard  *
193a8617a8SJordan K. Hubbard  *               x = k*ln2 + r,  |r| <= 0.5*ln2 ~ 0.34658
203a8617a8SJordan K. Hubbard  *
213a8617a8SJordan K. Hubbard  *      Here a correction term c will be computed to compensate
223a8617a8SJordan K. Hubbard  *	the error in r when rounded to a floating-point number.
233a8617a8SJordan K. Hubbard  *
243a8617a8SJordan K. Hubbard  *   2. Approximating expm1(r) by a special rational function on
253a8617a8SJordan K. Hubbard  *	the interval [0,0.34658]:
263a8617a8SJordan K. Hubbard  *	Since
273a8617a8SJordan K. Hubbard  *	    r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
283a8617a8SJordan K. Hubbard  *	we define R1(r*r) by
293a8617a8SJordan K. Hubbard  *	    r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
303a8617a8SJordan K. Hubbard  *	That is,
313a8617a8SJordan K. Hubbard  *	    R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
323a8617a8SJordan K. Hubbard  *		     = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
333a8617a8SJordan K. Hubbard  *		     = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
343a8617a8SJordan K. Hubbard  *      We use a special Reme algorithm on [0,0.347] to generate
353a8617a8SJordan K. Hubbard  * 	a polynomial of degree 5 in r*r to approximate R1. The
363a8617a8SJordan K. Hubbard  *	maximum error of this polynomial approximation is bounded
373a8617a8SJordan K. Hubbard  *	by 2**-61. In other words,
383a8617a8SJordan K. Hubbard  *	    R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
393a8617a8SJordan K. Hubbard  *	where 	Q1  =  -1.6666666666666567384E-2,
403a8617a8SJordan K. Hubbard  * 		Q2  =   3.9682539681370365873E-4,
413a8617a8SJordan K. Hubbard  * 		Q3  =  -9.9206344733435987357E-6,
423a8617a8SJordan K. Hubbard  * 		Q4  =   2.5051361420808517002E-7,
433a8617a8SJordan K. Hubbard  * 		Q5  =  -6.2843505682382617102E-9;
446d656800SBruce Evans  *		z   =  r*r,
453a8617a8SJordan K. Hubbard  *	with error bounded by
463a8617a8SJordan K. Hubbard  *	    |                  5           |     -61
473a8617a8SJordan K. Hubbard  *	    | 1.0+Q1*z+...+Q5*z   -  R1(z) | <= 2
483a8617a8SJordan K. Hubbard  *	    |                              |
493a8617a8SJordan K. Hubbard  *
503a8617a8SJordan K. Hubbard  *	expm1(r) = exp(r)-1 is then computed by the following
513a8617a8SJordan K. Hubbard  * 	specific way which minimize the accumulation rounding error:
523a8617a8SJordan K. Hubbard  *			       2     3
533a8617a8SJordan K. Hubbard  *			      r     r    [ 3 - (R1 + R1*r/2)  ]
543a8617a8SJordan K. Hubbard  *	      expm1(r) = r + --- + --- * [--------------------]
553a8617a8SJordan K. Hubbard  *		              2     2    [ 6 - r*(3 - R1*r/2) ]
563a8617a8SJordan K. Hubbard  *
573a8617a8SJordan K. Hubbard  *	To compensate the error in the argument reduction, we use
583a8617a8SJordan K. Hubbard  *		expm1(r+c) = expm1(r) + c + expm1(r)*c
593a8617a8SJordan K. Hubbard  *			   ~ expm1(r) + c + r*c
603a8617a8SJordan K. Hubbard  *	Thus c+r*c will be added in as the correction terms for
613a8617a8SJordan K. Hubbard  *	expm1(r+c). Now rearrange the term to avoid optimization
623a8617a8SJordan K. Hubbard  * 	screw up:
633a8617a8SJordan K. Hubbard  *		        (      2                                    2 )
643a8617a8SJordan K. Hubbard  *		        ({  ( r    [ R1 -  (3 - R1*r/2) ]  )  }    r  )
653a8617a8SJordan K. Hubbard  *	 expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
663a8617a8SJordan K. Hubbard  *	                ({  ( 2    [ 6 - r*(3 - R1*r/2) ]  )  }    2  )
673a8617a8SJordan K. Hubbard  *                      (                                             )
683a8617a8SJordan K. Hubbard  *
693a8617a8SJordan K. Hubbard  *		   = r - E
703a8617a8SJordan K. Hubbard  *   3. Scale back to obtain expm1(x):
713a8617a8SJordan K. Hubbard  *	From step 1, we have
723a8617a8SJordan K. Hubbard  *	   expm1(x) = either 2^k*[expm1(r)+1] - 1
733a8617a8SJordan K. Hubbard  *		    = or     2^k*[expm1(r) + (1-2^-k)]
743a8617a8SJordan K. Hubbard  *   4. Implementation notes:
753a8617a8SJordan K. Hubbard  *	(A). To save one multiplication, we scale the coefficient Qi
763a8617a8SJordan K. Hubbard  *	     to Qi*2^i, and replace z by (x^2)/2.
773a8617a8SJordan K. Hubbard  *	(B). To achieve maximum accuracy, we compute expm1(x) by
783a8617a8SJordan K. Hubbard  *	  (i)   if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
793a8617a8SJordan K. Hubbard  *	  (ii)  if k=0, return r-E
803a8617a8SJordan K. Hubbard  *	  (iii) if k=-1, return 0.5*(r-E)-0.5
813a8617a8SJordan K. Hubbard  *        (iv)	if k=1 if r < -0.25, return 2*((r+0.5)- E)
823a8617a8SJordan K. Hubbard  *	       	       else	     return  1.0+2.0*(r-E);
833a8617a8SJordan K. Hubbard  *	  (v)   if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
843a8617a8SJordan K. Hubbard  *	  (vi)  if k <= 20, return 2^k((1-2^-k)-(E-r)), else
853a8617a8SJordan K. Hubbard  *	  (vii) return 2^k(1-((E+2^-k)-r))
863a8617a8SJordan K. Hubbard  *
873a8617a8SJordan K. Hubbard  * Special cases:
883a8617a8SJordan K. Hubbard  *	expm1(INF) is INF, expm1(NaN) is NaN;
893a8617a8SJordan K. Hubbard  *	expm1(-INF) is -1, and
903a8617a8SJordan K. Hubbard  *	for finite argument, only expm1(0)=0 is exact.
913a8617a8SJordan K. Hubbard  *
923a8617a8SJordan K. Hubbard  * Accuracy:
933a8617a8SJordan K. Hubbard  *	according to an error analysis, the error is always less than
943a8617a8SJordan K. Hubbard  *	1 ulp (unit in the last place).
953a8617a8SJordan K. Hubbard  *
963a8617a8SJordan K. Hubbard  * Misc. info.
973a8617a8SJordan K. Hubbard  *	For IEEE double
983a8617a8SJordan K. Hubbard  *	    if x >  7.09782712893383973096e+02 then expm1(x) overflow
993a8617a8SJordan K. Hubbard  *
1003a8617a8SJordan K. Hubbard  * Constants:
1013a8617a8SJordan K. Hubbard  * The hexadecimal values are the intended ones for the following
1023a8617a8SJordan K. Hubbard  * constants. The decimal values may be used, provided that the
1033a8617a8SJordan K. Hubbard  * compiler will convert from decimal to binary accurately enough
1043a8617a8SJordan K. Hubbard  * to produce the hexadecimal values shown.
1053a8617a8SJordan K. Hubbard  */
1063a8617a8SJordan K. Hubbard 
107f2ea2b9dSDavid Schultz #include <float.h>
108f2ea2b9dSDavid Schultz 
1093a8617a8SJordan K. Hubbard #include "math.h"
1103a8617a8SJordan K. Hubbard #include "math_private.h"
1113a8617a8SJordan K. Hubbard 
1123a8617a8SJordan K. Hubbard static const double
1133a8617a8SJordan K. Hubbard one		= 1.0,
1143a8617a8SJordan K. Hubbard tiny		= 1.0e-300,
1153a8617a8SJordan K. Hubbard o_threshold	= 7.09782712893383973096e+02,/* 0x40862E42, 0xFEFA39EF */
1163a8617a8SJordan K. Hubbard ln2_hi		= 6.93147180369123816490e-01,/* 0x3fe62e42, 0xfee00000 */
1173a8617a8SJordan K. Hubbard ln2_lo		= 1.90821492927058770002e-10,/* 0x3dea39ef, 0x35793c76 */
1183a8617a8SJordan K. Hubbard invln2		= 1.44269504088896338700e+00,/* 0x3ff71547, 0x652b82fe */
1196d656800SBruce Evans /* Scaled Q's: Qn_here = 2**n * Qn_above, for R(2*z) where z = hxs = x*x/2: */
1203a8617a8SJordan K. Hubbard Q1  =  -3.33333333333331316428e-02, /* BFA11111 111110F4 */
1213a8617a8SJordan K. Hubbard Q2  =   1.58730158725481460165e-03, /* 3F5A01A0 19FE5585 */
1223a8617a8SJordan K. Hubbard Q3  =  -7.93650757867487942473e-05, /* BF14CE19 9EAADBB7 */
1233a8617a8SJordan K. Hubbard Q4  =   4.00821782732936239552e-06, /* 3ED0CFCA 86E65239 */
1243a8617a8SJordan K. Hubbard Q5  =  -2.01099218183624371326e-07; /* BE8AFDB7 6E09C32D */
1253a8617a8SJordan K. Hubbard 
1267dbbb6ddSDavid Schultz static volatile double huge = 1.0e+300;
1277dbbb6ddSDavid Schultz 
12859b19ff1SAlfred Perlstein double
expm1(double x)12959b19ff1SAlfred Perlstein expm1(double x)
1303a8617a8SJordan K. Hubbard {
131a00672cfSBruce Evans 	double y,hi,lo,c,t,e,hxs,hfx,r1,twopk;
1323a8617a8SJordan K. Hubbard 	int32_t k,xsb;
1333a8617a8SJordan K. Hubbard 	u_int32_t hx;
1343a8617a8SJordan K. Hubbard 
1353a8617a8SJordan K. Hubbard 	GET_HIGH_WORD(hx,x);
1363a8617a8SJordan K. Hubbard 	xsb = hx&0x80000000;		/* sign bit of x */
1373a8617a8SJordan K. Hubbard 	hx &= 0x7fffffff;		/* high word of |x| */
1383a8617a8SJordan K. Hubbard 
1393a8617a8SJordan K. Hubbard     /* filter out huge and non-finite argument */
1403a8617a8SJordan K. Hubbard 	if(hx >= 0x4043687A) {			/* if |x|>=56*ln2 */
1413a8617a8SJordan K. Hubbard 	    if(hx >= 0x40862E42) {		/* if |x|>=709.78... */
1423a8617a8SJordan K. Hubbard                 if(hx>=0x7ff00000) {
1433a8617a8SJordan K. Hubbard 		    u_int32_t low;
1443a8617a8SJordan K. Hubbard 		    GET_LOW_WORD(low,x);
1453a8617a8SJordan K. Hubbard 		    if(((hx&0xfffff)|low)!=0)
1463a8617a8SJordan K. Hubbard 		         return x+x; 	 /* NaN */
1473a8617a8SJordan K. Hubbard 		    else return (xsb==0)? x:-1.0;/* exp(+-inf)={inf,-1} */
1483a8617a8SJordan K. Hubbard 	        }
1493a8617a8SJordan K. Hubbard 	        if(x > o_threshold) return huge*huge; /* overflow */
1503a8617a8SJordan K. Hubbard 	    }
1513a8617a8SJordan K. Hubbard 	    if(xsb!=0) { /* x < -56*ln2, return -1.0 with inexact */
1523a8617a8SJordan K. Hubbard 		if(x+tiny<0.0)		/* raise inexact */
1533a8617a8SJordan K. Hubbard 		return tiny-one;	/* return -1 */
1543a8617a8SJordan K. Hubbard 	    }
1553a8617a8SJordan K. Hubbard 	}
1563a8617a8SJordan K. Hubbard 
1573a8617a8SJordan K. Hubbard     /* argument reduction */
1583a8617a8SJordan K. Hubbard 	if(hx > 0x3fd62e42) {		/* if  |x| > 0.5 ln2 */
1593a8617a8SJordan K. Hubbard 	    if(hx < 0x3FF0A2B2) {	/* and |x| < 1.5 ln2 */
1603a8617a8SJordan K. Hubbard 		if(xsb==0)
1613a8617a8SJordan K. Hubbard 		    {hi = x - ln2_hi; lo =  ln2_lo;  k =  1;}
1623a8617a8SJordan K. Hubbard 		else
1633a8617a8SJordan K. Hubbard 		    {hi = x + ln2_hi; lo = -ln2_lo;  k = -1;}
1643a8617a8SJordan K. Hubbard 	    } else {
1653a8617a8SJordan K. Hubbard 		k  = invln2*x+((xsb==0)?0.5:-0.5);
1663a8617a8SJordan K. Hubbard 		t  = k;
1673a8617a8SJordan K. Hubbard 		hi = x - t*ln2_hi;	/* t*ln2_hi is exact here */
1683a8617a8SJordan K. Hubbard 		lo = t*ln2_lo;
1693a8617a8SJordan K. Hubbard 	    }
170f2ea2b9dSDavid Schultz 	    STRICT_ASSIGN(double, x, hi - lo);
1713a8617a8SJordan K. Hubbard 	    c  = (hi-x)-lo;
1723a8617a8SJordan K. Hubbard 	}
1733a8617a8SJordan K. Hubbard 	else if(hx < 0x3c900000) {  	/* when |x|<2**-54, return x */
1743a8617a8SJordan K. Hubbard 	    t = huge+x;	/* return x with inexact flags when x!=0 */
1753a8617a8SJordan K. Hubbard 	    return x - (t-(huge+x));
1763a8617a8SJordan K. Hubbard 	}
1773a8617a8SJordan K. Hubbard 	else k = 0;
1783a8617a8SJordan K. Hubbard 
1793a8617a8SJordan K. Hubbard     /* x is now in primary range */
1803a8617a8SJordan K. Hubbard 	hfx = 0.5*x;
1813a8617a8SJordan K. Hubbard 	hxs = x*hfx;
1823a8617a8SJordan K. Hubbard 	r1 = one+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5))));
1833a8617a8SJordan K. Hubbard 	t  = 3.0-r1*hfx;
1843a8617a8SJordan K. Hubbard 	e  = hxs*((r1-t)/(6.0 - x*t));
1853a8617a8SJordan K. Hubbard 	if(k==0) return x - (x*e-hxs);		/* c is 0 */
1863a8617a8SJordan K. Hubbard 	else {
187*5763a8cfSDimitry Andric 	    INSERT_WORDS(twopk,((u_int32_t)(0x3ff+k))<<20,0);	/* 2^k */
1883a8617a8SJordan K. Hubbard 	    e  = (x*(e-c)-c);
1893a8617a8SJordan K. Hubbard 	    e -= hxs;
1903a8617a8SJordan K. Hubbard 	    if(k== -1) return 0.5*(x-e)-0.5;
191ee0730e6SDavid Schultz 	    if(k==1) {
1923a8617a8SJordan K. Hubbard 	       	if(x < -0.25) return -2.0*(e-(x+0.5));
1933a8617a8SJordan K. Hubbard 	       	else 	      return  one+2.0*(x-e);
194ee0730e6SDavid Schultz 	    }
1953a8617a8SJordan K. Hubbard 	    if (k <= -2 || k>56) {   /* suffice to return exp(x)-1 */
1963a8617a8SJordan K. Hubbard 	        y = one-(e-x);
197a00672cfSBruce Evans 		if (k == 1024) y = y*2.0*0x1p1023;
198a00672cfSBruce Evans 		else y = y*twopk;
1993a8617a8SJordan K. Hubbard 	        return y-one;
2003a8617a8SJordan K. Hubbard 	    }
2013a8617a8SJordan K. Hubbard 	    t = one;
2023a8617a8SJordan K. Hubbard 	    if(k<20) {
2033a8617a8SJordan K. Hubbard 	        SET_HIGH_WORD(t,0x3ff00000 - (0x200000>>k));  /* t=1-2^-k */
2043a8617a8SJordan K. Hubbard 	       	y = t-(e-x);
205a00672cfSBruce Evans 		y = y*twopk;
2063a8617a8SJordan K. Hubbard 	   } else {
2073a8617a8SJordan K. Hubbard 		SET_HIGH_WORD(t,((0x3ff-k)<<20));	/* 2^-k */
2083a8617a8SJordan K. Hubbard 	       	y = x-(e+t);
2093a8617a8SJordan K. Hubbard 	       	y += one;
210a00672cfSBruce Evans 		y = y*twopk;
2113a8617a8SJordan K. Hubbard 	    }
2123a8617a8SJordan K. Hubbard 	}
2133a8617a8SJordan K. Hubbard 	return y;
2143a8617a8SJordan K. Hubbard }
2153ffff4baSSteve Kargl 
2163ffff4baSSteve Kargl #if (LDBL_MANT_DIG == 53)
2173ffff4baSSteve Kargl __weak_reference(expm1, expm1l);
2183ffff4baSSteve Kargl #endif
219