xref: /titanic_51/usr/src/lib/libm/common/C/expm1.c (revision ddc0e0b53c661f6e439e3b7072b3ef353eadb4af)
125c28e83SPiotr Jasiukajtis /*
225c28e83SPiotr Jasiukajtis  * CDDL HEADER START
325c28e83SPiotr Jasiukajtis  *
425c28e83SPiotr Jasiukajtis  * The contents of this file are subject to the terms of the
525c28e83SPiotr Jasiukajtis  * Common Development and Distribution License (the "License").
625c28e83SPiotr Jasiukajtis  * You may not use this file except in compliance with the License.
725c28e83SPiotr Jasiukajtis  *
825c28e83SPiotr Jasiukajtis  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
925c28e83SPiotr Jasiukajtis  * or http://www.opensolaris.org/os/licensing.
1025c28e83SPiotr Jasiukajtis  * See the License for the specific language governing permissions
1125c28e83SPiotr Jasiukajtis  * and limitations under the License.
1225c28e83SPiotr Jasiukajtis  *
1325c28e83SPiotr Jasiukajtis  * When distributing Covered Code, include this CDDL HEADER in each
1425c28e83SPiotr Jasiukajtis  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1525c28e83SPiotr Jasiukajtis  * If applicable, add the following below this CDDL HEADER, with the
1625c28e83SPiotr Jasiukajtis  * fields enclosed by brackets "[]" replaced with your own identifying
1725c28e83SPiotr Jasiukajtis  * information: Portions Copyright [yyyy] [name of copyright owner]
1825c28e83SPiotr Jasiukajtis  *
1925c28e83SPiotr Jasiukajtis  * CDDL HEADER END
2025c28e83SPiotr Jasiukajtis  */
2125c28e83SPiotr Jasiukajtis 
2225c28e83SPiotr Jasiukajtis /*
2325c28e83SPiotr Jasiukajtis  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
2425c28e83SPiotr Jasiukajtis  */
2525c28e83SPiotr Jasiukajtis /*
2625c28e83SPiotr Jasiukajtis  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2725c28e83SPiotr Jasiukajtis  * Use is subject to license terms.
2825c28e83SPiotr Jasiukajtis  */
2925c28e83SPiotr Jasiukajtis 
30*ddc0e0b5SRichard Lowe #pragma weak __expm1 = expm1
3125c28e83SPiotr Jasiukajtis 
3225c28e83SPiotr Jasiukajtis /* INDENT OFF */
3325c28e83SPiotr Jasiukajtis /*
3425c28e83SPiotr Jasiukajtis  * expm1(x)
3525c28e83SPiotr Jasiukajtis  * Returns exp(x)-1, the exponential of x minus 1.
3625c28e83SPiotr Jasiukajtis  *
3725c28e83SPiotr Jasiukajtis  * Method
3825c28e83SPiotr Jasiukajtis  *   1. Arugment reduction:
3925c28e83SPiotr Jasiukajtis  *	Given x, find r and integer k such that
4025c28e83SPiotr Jasiukajtis  *
4125c28e83SPiotr Jasiukajtis  *               x = k*ln2 + r,  |r| <= 0.5*ln2 ~ 0.34658
4225c28e83SPiotr Jasiukajtis  *
4325c28e83SPiotr Jasiukajtis  *      Here a correction term c will be computed to compensate
4425c28e83SPiotr Jasiukajtis  *	the error in r when rounded to a floating-point number.
4525c28e83SPiotr Jasiukajtis  *
4625c28e83SPiotr Jasiukajtis  *   2. Approximating expm1(r) by a special rational function on
4725c28e83SPiotr Jasiukajtis  *	the interval [0,0.34658]:
4825c28e83SPiotr Jasiukajtis  *	Since
4925c28e83SPiotr Jasiukajtis  *	    r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
5025c28e83SPiotr Jasiukajtis  *	we define R1(r*r) by
5125c28e83SPiotr Jasiukajtis  *	    r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
5225c28e83SPiotr Jasiukajtis  *	That is,
5325c28e83SPiotr Jasiukajtis  *	    R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
5425c28e83SPiotr Jasiukajtis  *		     = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
5525c28e83SPiotr Jasiukajtis  *		     = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
5625c28e83SPiotr Jasiukajtis  *      We use a special Reme algorithm on [0,0.347] to generate
5725c28e83SPiotr Jasiukajtis  * 	a polynomial of degree 5 in r*r to approximate R1. The
5825c28e83SPiotr Jasiukajtis  *	maximum error of this polynomial approximation is bounded
5925c28e83SPiotr Jasiukajtis  *	by 2**-61. In other words,
6025c28e83SPiotr Jasiukajtis  *	    R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
6125c28e83SPiotr Jasiukajtis  *	where 	Q1  =  -1.6666666666666567384E-2,
6225c28e83SPiotr Jasiukajtis  * 		Q2  =   3.9682539681370365873E-4,
6325c28e83SPiotr Jasiukajtis  * 		Q3  =  -9.9206344733435987357E-6,
6425c28e83SPiotr Jasiukajtis  * 		Q4  =   2.5051361420808517002E-7,
6525c28e83SPiotr Jasiukajtis  * 		Q5  =  -6.2843505682382617102E-9;
6625c28e83SPiotr Jasiukajtis  *  	(where z=r*r, and the values of Q1 to Q5 are listed below)
6725c28e83SPiotr Jasiukajtis  *	with error bounded by
6825c28e83SPiotr Jasiukajtis  *	    |                  5           |     -61
6925c28e83SPiotr Jasiukajtis  *	    | 1.0+Q1*z+...+Q5*z   -  R1(z) | <= 2
7025c28e83SPiotr Jasiukajtis  *	    |                              |
7125c28e83SPiotr Jasiukajtis  *
7225c28e83SPiotr Jasiukajtis  *	expm1(r) = exp(r)-1 is then computed by the following
7325c28e83SPiotr Jasiukajtis  * 	specific way which minimize the accumulation rounding error:
7425c28e83SPiotr Jasiukajtis  *			       2     3
7525c28e83SPiotr Jasiukajtis  *			      r     r    [ 3 - (R1 + R1*r/2)  ]
7625c28e83SPiotr Jasiukajtis  *	      expm1(r) = r + --- + --- * [--------------------]
7725c28e83SPiotr Jasiukajtis  *		              2     2    [ 6 - r*(3 - R1*r/2) ]
7825c28e83SPiotr Jasiukajtis  *
7925c28e83SPiotr Jasiukajtis  *	To compensate the error in the argument reduction, we use
8025c28e83SPiotr Jasiukajtis  *		expm1(r+c) = expm1(r) + c + expm1(r)*c
8125c28e83SPiotr Jasiukajtis  *			   ~ expm1(r) + c + r*c
8225c28e83SPiotr Jasiukajtis  *	Thus c+r*c will be added in as the correction terms for
8325c28e83SPiotr Jasiukajtis  *	expm1(r+c). Now rearrange the term to avoid optimization
8425c28e83SPiotr Jasiukajtis  * 	screw up:
8525c28e83SPiotr Jasiukajtis  *		        (      2                                    2 )
8625c28e83SPiotr Jasiukajtis  *		        ({  ( r    [ R1 -  (3 - R1*r/2) ]  )  }    r  )
8725c28e83SPiotr Jasiukajtis  *	 expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
8825c28e83SPiotr Jasiukajtis  *	                ({  ( 2    [ 6 - r*(3 - R1*r/2) ]  )  }    2  )
8925c28e83SPiotr Jasiukajtis  *                      (                                             )
9025c28e83SPiotr Jasiukajtis  *
9125c28e83SPiotr Jasiukajtis  *		   = r - E
9225c28e83SPiotr Jasiukajtis  *   3. Scale back to obtain expm1(x):
9325c28e83SPiotr Jasiukajtis  *	From step 1, we have
9425c28e83SPiotr Jasiukajtis  *	   expm1(x) = either 2^k*[expm1(r)+1] - 1
9525c28e83SPiotr Jasiukajtis  *		    = or     2^k*[expm1(r) + (1-2^-k)]
9625c28e83SPiotr Jasiukajtis  *   4. Implementation notes:
9725c28e83SPiotr Jasiukajtis  *	(A). To save one multiplication, we scale the coefficient Qi
9825c28e83SPiotr Jasiukajtis  *	     to Qi*2^i, and replace z by (x^2)/2.
9925c28e83SPiotr Jasiukajtis  *	(B). To achieve maximum accuracy, we compute expm1(x) by
10025c28e83SPiotr Jasiukajtis  *	  (i)   if x < -56*ln2, return -1.0, (raise inexact if x != inf)
10125c28e83SPiotr Jasiukajtis  *	  (ii)  if k=0, return r-E
10225c28e83SPiotr Jasiukajtis  *	  (iii) if k=-1, return 0.5*(r-E)-0.5
10325c28e83SPiotr Jasiukajtis  *        (iv)	if k=1 if r < -0.25, return 2*((r+0.5)- E)
10425c28e83SPiotr Jasiukajtis  *					else	     return  1.0+2.0*(r-E);
10525c28e83SPiotr Jasiukajtis  *	  (v)   if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
10625c28e83SPiotr Jasiukajtis  *	  (vi)  if k <= 20, return 2^k((1-2^-k)-(E-r)), else
10725c28e83SPiotr Jasiukajtis  *	  (vii) return 2^k(1-((E+2^-k)-r))
10825c28e83SPiotr Jasiukajtis  *
10925c28e83SPiotr Jasiukajtis  * Special cases:
11025c28e83SPiotr Jasiukajtis  *	expm1(INF) is INF, expm1(NaN) is NaN;
11125c28e83SPiotr Jasiukajtis  *	expm1(-INF) is -1, and
11225c28e83SPiotr Jasiukajtis  *	for finite argument, only expm1(0)=0 is exact.
11325c28e83SPiotr Jasiukajtis  *
11425c28e83SPiotr Jasiukajtis  * Accuracy:
11525c28e83SPiotr Jasiukajtis  *	according to an error analysis, the error is always less than
11625c28e83SPiotr Jasiukajtis  *	1 ulp (unit in the last place).
11725c28e83SPiotr Jasiukajtis  *
11825c28e83SPiotr Jasiukajtis  * Misc. info.
11925c28e83SPiotr Jasiukajtis  *	For IEEE double
12025c28e83SPiotr Jasiukajtis  *	    if x >  7.09782712893383973096e+02 then expm1(x) overflow
12125c28e83SPiotr Jasiukajtis  *
12225c28e83SPiotr Jasiukajtis  * Constants:
12325c28e83SPiotr Jasiukajtis  * The hexadecimal values are the intended ones for the following
12425c28e83SPiotr Jasiukajtis  * constants. The decimal values may be used, provided that the
12525c28e83SPiotr Jasiukajtis  * compiler will convert from decimal to binary accurately enough
12625c28e83SPiotr Jasiukajtis  * to produce the hexadecimal values shown.
12725c28e83SPiotr Jasiukajtis  */
12825c28e83SPiotr Jasiukajtis /* INDENT ON */
12925c28e83SPiotr Jasiukajtis 
13025c28e83SPiotr Jasiukajtis #include "libm_macros.h"
13125c28e83SPiotr Jasiukajtis #include <math.h>
13225c28e83SPiotr Jasiukajtis 
13325c28e83SPiotr Jasiukajtis static const double xxx[] = {
13425c28e83SPiotr Jasiukajtis /* one */		 1.0,
13525c28e83SPiotr Jasiukajtis /* huge */		 1.0e+300,
13625c28e83SPiotr Jasiukajtis /* tiny */		 1.0e-300,
13725c28e83SPiotr Jasiukajtis /* o_threshold */	 7.09782712893383973096e+02,	/* 40862E42 FEFA39EF */
13825c28e83SPiotr Jasiukajtis /* ln2_hi */		 6.93147180369123816490e-01,	/* 3FE62E42 FEE00000 */
13925c28e83SPiotr Jasiukajtis /* ln2_lo */		 1.90821492927058770002e-10,	/* 3DEA39EF 35793C76 */
14025c28e83SPiotr Jasiukajtis /* invln2 */		 1.44269504088896338700e+00,	/* 3FF71547 652B82FE */
14125c28e83SPiotr Jasiukajtis /* scaled coefficients related to expm1 */
14225c28e83SPiotr Jasiukajtis /* Q1 */		-3.33333333333331316428e-02,	/* BFA11111 111110F4 */
14325c28e83SPiotr Jasiukajtis /* Q2 */		 1.58730158725481460165e-03,	/* 3F5A01A0 19FE5585 */
14425c28e83SPiotr Jasiukajtis /* Q3 */		-7.93650757867487942473e-05,	/* BF14CE19 9EAADBB7 */
14525c28e83SPiotr Jasiukajtis /* Q4 */		 4.00821782732936239552e-06,	/* 3ED0CFCA 86E65239 */
14625c28e83SPiotr Jasiukajtis /* Q5 */		-2.01099218183624371326e-07	/* BE8AFDB7 6E09C32D */
14725c28e83SPiotr Jasiukajtis };
14825c28e83SPiotr Jasiukajtis #define	one		xxx[0]
14925c28e83SPiotr Jasiukajtis #define	huge		xxx[1]
15025c28e83SPiotr Jasiukajtis #define	tiny		xxx[2]
15125c28e83SPiotr Jasiukajtis #define	o_threshold	xxx[3]
15225c28e83SPiotr Jasiukajtis #define	ln2_hi		xxx[4]
15325c28e83SPiotr Jasiukajtis #define	ln2_lo		xxx[5]
15425c28e83SPiotr Jasiukajtis #define	invln2		xxx[6]
15525c28e83SPiotr Jasiukajtis #define	Q1		xxx[7]
15625c28e83SPiotr Jasiukajtis #define	Q2		xxx[8]
15725c28e83SPiotr Jasiukajtis #define	Q3		xxx[9]
15825c28e83SPiotr Jasiukajtis #define	Q4		xxx[10]
15925c28e83SPiotr Jasiukajtis #define	Q5		xxx[11]
16025c28e83SPiotr Jasiukajtis 
16125c28e83SPiotr Jasiukajtis double
16225c28e83SPiotr Jasiukajtis expm1(double x) {
16325c28e83SPiotr Jasiukajtis 	double y, hi, lo, c = 0.0L, t, e, hxs, hfx, r1;
16425c28e83SPiotr Jasiukajtis 	int k, xsb;
16525c28e83SPiotr Jasiukajtis 	unsigned hx;
16625c28e83SPiotr Jasiukajtis 
16725c28e83SPiotr Jasiukajtis 	hx = ((unsigned *) &x)[HIWORD];		/* high word of x */
16825c28e83SPiotr Jasiukajtis 	xsb = hx & 0x80000000;			/* sign bit of x */
16925c28e83SPiotr Jasiukajtis 	if (xsb == 0)
17025c28e83SPiotr Jasiukajtis 		y = x;
17125c28e83SPiotr Jasiukajtis 	else
17225c28e83SPiotr Jasiukajtis 		y = -x;				/* y = |x| */
17325c28e83SPiotr Jasiukajtis 	hx &= 0x7fffffff;			/* high word of |x| */
17425c28e83SPiotr Jasiukajtis 
17525c28e83SPiotr Jasiukajtis 	/* filter out huge and non-finite argument */
17625c28e83SPiotr Jasiukajtis 	/* for example exp(38)-1 is approximately 3.1855932e+16 */
17725c28e83SPiotr Jasiukajtis 	if (hx >= 0x4043687A) {
17825c28e83SPiotr Jasiukajtis 		/* if |x|>=56*ln2 (~38.8162...) */
17925c28e83SPiotr Jasiukajtis 		if (hx >= 0x40862E42) {		/* if |x|>=709.78... -> inf */
18025c28e83SPiotr Jasiukajtis 			if (hx >= 0x7ff00000) {
18125c28e83SPiotr Jasiukajtis 				if (((hx & 0xfffff) | ((int *) &x)[LOWORD])
18225c28e83SPiotr Jasiukajtis 					!= 0)
18325c28e83SPiotr Jasiukajtis 					return (x * x);	/* + -> * for Cheetah */
18425c28e83SPiotr Jasiukajtis 				else
18525c28e83SPiotr Jasiukajtis 					/* exp(+-inf)={inf,-1} */
18625c28e83SPiotr Jasiukajtis 					return (xsb == 0 ? x : -1.0);
18725c28e83SPiotr Jasiukajtis 			}
18825c28e83SPiotr Jasiukajtis 			if (x > o_threshold)
18925c28e83SPiotr Jasiukajtis 				return (huge * huge);	/* overflow */
19025c28e83SPiotr Jasiukajtis 		}
19125c28e83SPiotr Jasiukajtis 		if (xsb != 0) {		/* x < -56*ln2, return -1.0 w/inexact */
19225c28e83SPiotr Jasiukajtis 			if (x + tiny < 0.0)		/* raise inexact */
19325c28e83SPiotr Jasiukajtis 				return (tiny - one);	/* return -1 */
19425c28e83SPiotr Jasiukajtis 		}
19525c28e83SPiotr Jasiukajtis 	}
19625c28e83SPiotr Jasiukajtis 
19725c28e83SPiotr Jasiukajtis 	/* argument reduction */
19825c28e83SPiotr Jasiukajtis 	if (hx > 0x3fd62e42) {			/* if  |x| > 0.5 ln2 */
19925c28e83SPiotr Jasiukajtis 		if (hx < 0x3FF0A2B2) {		/* and |x| < 1.5 ln2 */
20025c28e83SPiotr Jasiukajtis 			if (xsb == 0) {		/* positive number */
20125c28e83SPiotr Jasiukajtis 				hi = x - ln2_hi;
20225c28e83SPiotr Jasiukajtis 				lo = ln2_lo;
20325c28e83SPiotr Jasiukajtis 				k = 1;
20425c28e83SPiotr Jasiukajtis 			} else {
20525c28e83SPiotr Jasiukajtis 				/* negative number */
20625c28e83SPiotr Jasiukajtis 				hi = x + ln2_hi;
20725c28e83SPiotr Jasiukajtis 				lo = -ln2_lo;
20825c28e83SPiotr Jasiukajtis 				k = -1;
20925c28e83SPiotr Jasiukajtis 			}
21025c28e83SPiotr Jasiukajtis 		} else {
21125c28e83SPiotr Jasiukajtis 			/* |x| > 1.5 ln2 */
21225c28e83SPiotr Jasiukajtis 			k = (int) (invln2 * x + (xsb == 0 ? 0.5 : -0.5));
21325c28e83SPiotr Jasiukajtis 			t = k;
21425c28e83SPiotr Jasiukajtis 			hi = x - t * ln2_hi;	/* t*ln2_hi is exact here */
21525c28e83SPiotr Jasiukajtis 			lo = t * ln2_lo;
21625c28e83SPiotr Jasiukajtis 		}
21725c28e83SPiotr Jasiukajtis 		x = hi - lo;
21825c28e83SPiotr Jasiukajtis 		c = (hi - x) - lo; /* still at |x| > 0.5 ln2 */
21925c28e83SPiotr Jasiukajtis 	} else if (hx < 0x3c900000) {
22025c28e83SPiotr Jasiukajtis 		/* when |x|<2**-54, return x */
22125c28e83SPiotr Jasiukajtis 		t = huge + x;		/* return x w/inexact when x != 0 */
22225c28e83SPiotr Jasiukajtis 		return (x - (t - (huge + x)));
22325c28e83SPiotr Jasiukajtis 	} else
22425c28e83SPiotr Jasiukajtis 		/* |x| <= 0.5 ln2 */
22525c28e83SPiotr Jasiukajtis 		k = 0;
22625c28e83SPiotr Jasiukajtis 
22725c28e83SPiotr Jasiukajtis 	/* x is now in primary range */
22825c28e83SPiotr Jasiukajtis 	hfx = 0.5 * x;
22925c28e83SPiotr Jasiukajtis 	hxs = x * hfx;
23025c28e83SPiotr Jasiukajtis 	r1 = one + hxs * (Q1 + hxs * (Q2 + hxs * (Q3 + hxs * (Q4 + hxs * Q5))));
23125c28e83SPiotr Jasiukajtis 	t = 3.0 - r1 * hfx;
23225c28e83SPiotr Jasiukajtis 	e = hxs * ((r1 - t) / (6.0 - x * t));
23325c28e83SPiotr Jasiukajtis 	if (k == 0) /* |x| <= 0.5 ln2 */
23425c28e83SPiotr Jasiukajtis 		return (x - (x * e - hxs));
23525c28e83SPiotr Jasiukajtis 	else {		/* |x| > 0.5 ln2 */
23625c28e83SPiotr Jasiukajtis 		e = (x * (e - c) - c);
23725c28e83SPiotr Jasiukajtis 		e -= hxs;
23825c28e83SPiotr Jasiukajtis 		if (k == -1)
23925c28e83SPiotr Jasiukajtis 			return (0.5 * (x - e) - 0.5);
24025c28e83SPiotr Jasiukajtis 		if (k == 1) {
24125c28e83SPiotr Jasiukajtis 			if (x < -0.25)
24225c28e83SPiotr Jasiukajtis 				return (-2.0 * (e - (x + 0.5)));
24325c28e83SPiotr Jasiukajtis 			else
24425c28e83SPiotr Jasiukajtis 				return (one + 2.0 * (x - e));
24525c28e83SPiotr Jasiukajtis 		}
24625c28e83SPiotr Jasiukajtis 		if (k <= -2 || k > 56) {	/* suffice to return exp(x)-1 */
24725c28e83SPiotr Jasiukajtis 			y = one - (e - x);
24825c28e83SPiotr Jasiukajtis 			((int *) &y)[HIWORD] += k << 20;
24925c28e83SPiotr Jasiukajtis 			return (y - one);
25025c28e83SPiotr Jasiukajtis 		}
25125c28e83SPiotr Jasiukajtis 		t = one;
25225c28e83SPiotr Jasiukajtis 		if (k < 20) {
25325c28e83SPiotr Jasiukajtis 			((int *) &t)[HIWORD] = 0x3ff00000 - (0x200000 >> k);
25425c28e83SPiotr Jasiukajtis 							/* t = 1 - 2^-k */
25525c28e83SPiotr Jasiukajtis 			y = t - (e - x);
25625c28e83SPiotr Jasiukajtis 			((int *) &y)[HIWORD] += k << 20;
25725c28e83SPiotr Jasiukajtis 		} else {
25825c28e83SPiotr Jasiukajtis 			((int *) &t)[HIWORD] = (0x3ff - k) << 20; /* 2^-k */
25925c28e83SPiotr Jasiukajtis 			y = x - (e + t);
26025c28e83SPiotr Jasiukajtis 			y += one;
26125c28e83SPiotr Jasiukajtis 			((int *) &y)[HIWORD] += k << 20;
26225c28e83SPiotr Jasiukajtis 		}
26325c28e83SPiotr Jasiukajtis 	}
26425c28e83SPiotr Jasiukajtis 	return (y);
26525c28e83SPiotr Jasiukajtis }
266