xref: /netbsd-src/lib/libm/src/s_expm1.c (revision fa47e9d8508da57b045de69d4b36e96567ab2e58)
113618394Sjtc /* @(#)s_expm1.c 5.1 93/09/24 */
213618394Sjtc /*
313618394Sjtc  * ====================================================
413618394Sjtc  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
513618394Sjtc  *
613618394Sjtc  * Developed at SunPro, a Sun Microsystems, Inc. business.
713618394Sjtc  * Permission to use, copy, modify, and distribute this
813618394Sjtc  * software is freely granted, provided that this notice
913618394Sjtc  * is preserved.
1013618394Sjtc  * ====================================================
1113618394Sjtc  */
1213618394Sjtc 
1361187201Slukem #include <sys/cdefs.h>
14d1f06e0bSjtc #if defined(LIBM_SCCS) && !defined(lint)
15*fa47e9d8Smaya __RCSID("$NetBSD: s_expm1.c,v 1.13 2017/02/09 22:11:09 maya Exp $");
16bc3f7bf6Sjtc #endif
17bc3f7bf6Sjtc 
1813618394Sjtc /* expm1(x)
1913618394Sjtc  * Returns exp(x)-1, the exponential of x minus 1.
2013618394Sjtc  *
2113618394Sjtc  * Method
2213618394Sjtc  *   1. Argument reduction:
2313618394Sjtc  *	Given x, find r and integer k such that
2413618394Sjtc  *
2513618394Sjtc  *               x = k*ln2 + r,  |r| <= 0.5*ln2 ~ 0.34658
2613618394Sjtc  *
2713618394Sjtc  *      Here a correction term c will be computed to compensate
2813618394Sjtc  *	the error in r when rounded to a floating-point number.
2913618394Sjtc  *
3013618394Sjtc  *   2. Approximating expm1(r) by a special rational function on
3113618394Sjtc  *	the interval [0,0.34658]:
3213618394Sjtc  *	Since
3313618394Sjtc  *	    r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
3413618394Sjtc  *	we define R1(r*r) by
3513618394Sjtc  *	    r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
3613618394Sjtc  *	That is,
3713618394Sjtc  *	    R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
3813618394Sjtc  *		     = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
3913618394Sjtc  *		     = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
4013618394Sjtc  *      We use a special Reme algorithm on [0,0.347] to generate
4113618394Sjtc  * 	a polynomial of degree 5 in r*r to approximate R1. The
4213618394Sjtc  *	maximum error of this polynomial approximation is bounded
4313618394Sjtc  *	by 2**-61. In other words,
4413618394Sjtc  *	    R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
4513618394Sjtc  *	where 	Q1  =  -1.6666666666666567384E-2,
4613618394Sjtc  * 		Q2  =   3.9682539681370365873E-4,
4713618394Sjtc  * 		Q3  =  -9.9206344733435987357E-6,
4813618394Sjtc  * 		Q4  =   2.5051361420808517002E-7,
4913618394Sjtc  * 		Q5  =  -6.2843505682382617102E-9;
5013618394Sjtc  *  	(where z=r*r, and the values of Q1 to Q5 are listed below)
5113618394Sjtc  *	with error bounded by
5213618394Sjtc  *	    |                  5           |     -61
5313618394Sjtc  *	    | 1.0+Q1*z+...+Q5*z   -  R1(z) | <= 2
5413618394Sjtc  *	    |                              |
5513618394Sjtc  *
5613618394Sjtc  *	expm1(r) = exp(r)-1 is then computed by the following
5713618394Sjtc  * 	specific way which minimize the accumulation rounding error:
5813618394Sjtc  *			       2     3
5913618394Sjtc  *			      r     r    [ 3 - (R1 + R1*r/2)  ]
6013618394Sjtc  *	      expm1(r) = r + --- + --- * [--------------------]
6113618394Sjtc  *		              2     2    [ 6 - r*(3 - R1*r/2) ]
6213618394Sjtc  *
6313618394Sjtc  *	To compensate the error in the argument reduction, we use
6413618394Sjtc  *		expm1(r+c) = expm1(r) + c + expm1(r)*c
6513618394Sjtc  *			   ~ expm1(r) + c + r*c
6613618394Sjtc  *	Thus c+r*c will be added in as the correction terms for
6713618394Sjtc  *	expm1(r+c). Now rearrange the term to avoid optimization
6813618394Sjtc  * 	screw up:
6913618394Sjtc  *		        (      2                                    2 )
7013618394Sjtc  *		        ({  ( r    [ R1 -  (3 - R1*r/2) ]  )  }    r  )
7113618394Sjtc  *	 expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
7213618394Sjtc  *	                ({  ( 2    [ 6 - r*(3 - R1*r/2) ]  )  }    2  )
7313618394Sjtc  *                      (                                             )
7413618394Sjtc  *
7513618394Sjtc  *		   = r - E
7613618394Sjtc  *   3. Scale back to obtain expm1(x):
7713618394Sjtc  *	From step 1, we have
7813618394Sjtc  *	   expm1(x) = either 2^k*[expm1(r)+1] - 1
7913618394Sjtc  *		    = or     2^k*[expm1(r) + (1-2^-k)]
8013618394Sjtc  *   4. Implementation notes:
8113618394Sjtc  *	(A). To save one multiplication, we scale the coefficient Qi
8213618394Sjtc  *	     to Qi*2^i, and replace z by (x^2)/2.
8313618394Sjtc  *	(B). To achieve maximum accuracy, we compute expm1(x) by
8413618394Sjtc  *	  (i)   if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
8513618394Sjtc  *	  (ii)  if k=0, return r-E
8613618394Sjtc  *	  (iii) if k=-1, return 0.5*(r-E)-0.5
8713618394Sjtc  *        (iv)	if k=1 if r < -0.25, return 2*((r+0.5)- E)
8813618394Sjtc  *	       	       else	     return  1.0+2.0*(r-E);
8913618394Sjtc  *	  (v)   if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
9013618394Sjtc  *	  (vi)  if k <= 20, return 2^k((1-2^-k)-(E-r)), else
9113618394Sjtc  *	  (vii) return 2^k(1-((E+2^-k)-r))
9213618394Sjtc  *
9313618394Sjtc  * Special cases:
9413618394Sjtc  *	expm1(INF) is INF, expm1(NaN) is NaN;
9513618394Sjtc  *	expm1(-INF) is -1, and
9613618394Sjtc  *	for finite argument, only expm1(0)=0 is exact.
9713618394Sjtc  *
9813618394Sjtc  * Accuracy:
9913618394Sjtc  *	according to an error analysis, the error is always less than
10013618394Sjtc  *	1 ulp (unit in the last place).
10113618394Sjtc  *
10213618394Sjtc  * Misc. info.
10313618394Sjtc  *	For IEEE double
10413618394Sjtc  *	    if x >  7.09782712893383973096e+02 then expm1(x) overflow
10513618394Sjtc  *
10613618394Sjtc  * Constants:
10713618394Sjtc  * The hexadecimal values are the intended ones for the following
10813618394Sjtc  * constants. The decimal values may be used, provided that the
10913618394Sjtc  * compiler will convert from decimal to binary accurately enough
11013618394Sjtc  * to produce the hexadecimal values shown.
11113618394Sjtc  */
11213618394Sjtc 
1138346e333Sjtc #include "math.h"
1148346e333Sjtc #include "math_private.h"
11513618394Sjtc 
11613618394Sjtc static const double
11713618394Sjtc one		= 1.0,
11813618394Sjtc huge		= 1.0e+300,
11913618394Sjtc tiny		= 1.0e-300,
12013618394Sjtc o_threshold	= 7.09782712893383973096e+02,/* 0x40862E42, 0xFEFA39EF */
12113618394Sjtc ln2_hi		= 6.93147180369123816490e-01,/* 0x3fe62e42, 0xfee00000 */
12213618394Sjtc ln2_lo		= 1.90821492927058770002e-10,/* 0x3dea39ef, 0x35793c76 */
12313618394Sjtc invln2		= 1.44269504088896338700e+00,/* 0x3ff71547, 0x652b82fe */
12413618394Sjtc 	/* scaled coefficients related to expm1 */
12513618394Sjtc Q1  =  -3.33333333333331316428e-02, /* BFA11111 111110F4 */
12613618394Sjtc Q2  =   1.58730158725481460165e-03, /* 3F5A01A0 19FE5585 */
12713618394Sjtc Q3  =  -7.93650757867487942473e-05, /* BF14CE19 9EAADBB7 */
12813618394Sjtc Q4  =   4.00821782732936239552e-06, /* 3ED0CFCA 86E65239 */
12913618394Sjtc Q5  =  -2.01099218183624371326e-07; /* BE8AFDB7 6E09C32D */
13013618394Sjtc 
131aa30599eSwiz double
expm1(double x)132aa30599eSwiz expm1(double x)
13313618394Sjtc {
13413618394Sjtc 	double y,hi,lo,c,t,e,hxs,hfx,r1;
135b0c9d092Sjtc 	int32_t k,xsb;
136b0c9d092Sjtc 	u_int32_t hx;
13713618394Sjtc 
13861187201Slukem 	c = 0;
1398346e333Sjtc 	GET_HIGH_WORD(hx,x);
14013618394Sjtc 	xsb = hx&0x80000000;		/* sign bit of x */
141*fa47e9d8Smaya #ifdef DEAD_CODE
14213618394Sjtc 	if(xsb==0) y=x; else y= -x;	/* y = |x| */
143*fa47e9d8Smaya #endif
14413618394Sjtc 	hx &= 0x7fffffff;		/* high word of |x| */
14513618394Sjtc 
14613618394Sjtc     /* filter out huge and non-finite argument */
14713618394Sjtc 	if(hx >= 0x4043687A) {			/* if |x|>=56*ln2 */
14813618394Sjtc 	    if(hx >= 0x40862E42) {		/* if |x|>=709.78... */
14913618394Sjtc                 if(hx>=0x7ff00000) {
150b0c9d092Sjtc 		    u_int32_t low;
1518346e333Sjtc 		    GET_LOW_WORD(low,x);
1528346e333Sjtc 		    if(((hx&0xfffff)|low)!=0)
15313618394Sjtc 		         return x+x; 	 /* NaN */
15413618394Sjtc 		    else return (xsb==0)? x:-1.0;/* exp(+-inf)={inf,-1} */
15513618394Sjtc 	        }
15613618394Sjtc 	        if(x > o_threshold) return huge*huge; /* overflow */
15713618394Sjtc 	    }
15813618394Sjtc 	    if(xsb!=0) { /* x < -56*ln2, return -1.0 with inexact */
15913618394Sjtc 		if(x+tiny<0.0)		/* raise inexact */
16013618394Sjtc 		return tiny-one;	/* return -1 */
16113618394Sjtc 	    }
16213618394Sjtc 	}
16313618394Sjtc 
16413618394Sjtc     /* argument reduction */
16513618394Sjtc 	if(hx > 0x3fd62e42) {		/* if  |x| > 0.5 ln2 */
16613618394Sjtc 	    if(hx < 0x3FF0A2B2) {	/* and |x| < 1.5 ln2 */
16713618394Sjtc 		if(xsb==0)
16813618394Sjtc 		    {hi = x - ln2_hi; lo =  ln2_lo;  k =  1;}
16913618394Sjtc 		else
17013618394Sjtc 		    {hi = x + ln2_hi; lo = -ln2_lo;  k = -1;}
17113618394Sjtc 	    } else {
17213618394Sjtc 		k  = invln2*x+((xsb==0)?0.5:-0.5);
17313618394Sjtc 		t  = k;
17413618394Sjtc 		hi = x - t*ln2_hi;	/* t*ln2_hi is exact here */
17513618394Sjtc 		lo = t*ln2_lo;
17613618394Sjtc 	    }
17713618394Sjtc 	    x  = hi - lo;
17813618394Sjtc 	    c  = (hi-x)-lo;
17913618394Sjtc 	}
18013618394Sjtc 	else if(hx < 0x3c900000) {  	/* when |x|<2**-54, return x */
18113618394Sjtc 	    t = huge+x;	/* return x with inexact flags when x!=0 */
18213618394Sjtc 	    return x - (t-(huge+x));
18313618394Sjtc 	}
18413618394Sjtc 	else k = 0;
18513618394Sjtc 
18613618394Sjtc     /* x is now in primary range */
18713618394Sjtc 	hfx = 0.5*x;
18813618394Sjtc 	hxs = x*hfx;
18913618394Sjtc 	r1 = one+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5))));
19013618394Sjtc 	t  = 3.0-r1*hfx;
19113618394Sjtc 	e  = hxs*((r1-t)/(6.0 - x*t));
19213618394Sjtc 	if(k==0) return x - (x*e-hxs);		/* c is 0 */
19313618394Sjtc 	else {
19413618394Sjtc 	    e  = (x*(e-c)-c);
19513618394Sjtc 	    e -= hxs;
19613618394Sjtc 	    if(k== -1) return 0.5*(x-e)-0.5;
19708c09aefSthorpej 	    if(k==1)  {
19813618394Sjtc 	       	if(x < -0.25) return -2.0*(e-(x+0.5));
19913618394Sjtc 	       	else 	      return  one+2.0*(x-e);
20008c09aefSthorpej 	    }
20113618394Sjtc 	    if (k <= -2 || k>56) {   /* suffice to return exp(x)-1 */
202b0c9d092Sjtc 	        u_int32_t high;
20313618394Sjtc 	        y = one-(e-x);
2048346e333Sjtc 		GET_HIGH_WORD(high,y);
2058346e333Sjtc 		SET_HIGH_WORD(y,high+(k<<20));	/* add k to y's exponent */
20613618394Sjtc 	        return y-one;
20713618394Sjtc 	    }
20813618394Sjtc 	    t = one;
20913618394Sjtc 	    if(k<20) {
210b0c9d092Sjtc 	        u_int32_t high;
2118346e333Sjtc 	        SET_HIGH_WORD(t,0x3ff00000 - (0x200000>>k));  /* t=1-2^-k */
21213618394Sjtc 	       	y = t-(e-x);
2138346e333Sjtc 		GET_HIGH_WORD(high,y);
2148346e333Sjtc 		SET_HIGH_WORD(y,high+(k<<20));	/* add k to y's exponent */
21513618394Sjtc 	   } else {
216b0c9d092Sjtc 	        u_int32_t high;
2178346e333Sjtc 		SET_HIGH_WORD(t,((0x3ff-k)<<20));	/* 2^-k */
21813618394Sjtc 	       	y = x-(e+t);
21913618394Sjtc 	       	y += one;
2208346e333Sjtc 		GET_HIGH_WORD(high,y);
2218346e333Sjtc 		SET_HIGH_WORD(y,high+(k<<20));	/* add k to y's exponent */
22213618394Sjtc 	    }
22313618394Sjtc 	}
22413618394Sjtc 	return y;
22513618394Sjtc }
226