xref: /minix3/lib/libm/src/b_exp.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: b_exp.c,v 1.1 2012/05/05 17:54:14 christos Exp $	*/
2*84d9c625SLionel Sambuc 
3*84d9c625SLionel Sambuc /*
4*84d9c625SLionel Sambuc  * Copyright (c) 1985, 1993
5*84d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
6*84d9c625SLionel Sambuc  *
7*84d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*84d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions
9*84d9c625SLionel Sambuc  * are met:
10*84d9c625SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*84d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*84d9c625SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*84d9c625SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
16*84d9c625SLionel Sambuc  *    must display the following acknowledgement:
17*84d9c625SLionel Sambuc  *	This product includes software developed by the University of
18*84d9c625SLionel Sambuc  *	California, Berkeley and its contributors.
19*84d9c625SLionel Sambuc  * 4. Neither the name of the University nor the names of its contributors
20*84d9c625SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21*84d9c625SLionel Sambuc  *    without specific prior written permission.
22*84d9c625SLionel Sambuc  *
23*84d9c625SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*84d9c625SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*84d9c625SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*84d9c625SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*84d9c625SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*84d9c625SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*84d9c625SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*84d9c625SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*84d9c625SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*84d9c625SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*84d9c625SLionel Sambuc  * SUCH DAMAGE.
34*84d9c625SLionel Sambuc  */
35*84d9c625SLionel Sambuc 
36*84d9c625SLionel Sambuc /* @(#)exp.c	8.1 (Berkeley) 6/4/93 */
37*84d9c625SLionel Sambuc #include <sys/cdefs.h>
38*84d9c625SLionel Sambuc #if 0
39*84d9c625SLionel Sambuc __FBSDID("$FreeBSD: release/9.0.0/lib/msun/bsdsrc/b_exp.c 176449 2008-02-22 02:26:51Z das $");
40*84d9c625SLionel Sambuc #else
41*84d9c625SLionel Sambuc __RCSID("$NetBSD: b_exp.c,v 1.1 2012/05/05 17:54:14 christos Exp $");
42*84d9c625SLionel Sambuc #endif
43*84d9c625SLionel Sambuc 
44*84d9c625SLionel Sambuc 
45*84d9c625SLionel Sambuc /* EXP(X)
46*84d9c625SLionel Sambuc  * RETURN THE EXPONENTIAL OF X
47*84d9c625SLionel Sambuc  * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS)
48*84d9c625SLionel Sambuc  * CODED IN C BY K.C. NG, 1/19/85;
49*84d9c625SLionel Sambuc  * REVISED BY K.C. NG on 2/6/85, 2/15/85, 3/7/85, 3/24/85, 4/16/85, 6/14/86.
50*84d9c625SLionel Sambuc  *
51*84d9c625SLionel Sambuc  * Required system supported functions:
52*84d9c625SLionel Sambuc  *	scalb(x,n)
53*84d9c625SLionel Sambuc  *	copysign(x,y)
54*84d9c625SLionel Sambuc  *	finite(x)
55*84d9c625SLionel Sambuc  *
56*84d9c625SLionel Sambuc  * Method:
57*84d9c625SLionel Sambuc  *	1. Argument Reduction: given the input x, find r and integer k such
58*84d9c625SLionel Sambuc  *	   that
59*84d9c625SLionel Sambuc  *	                   x = k*ln2 + r,  |r| <= 0.5*ln2 .
60*84d9c625SLionel Sambuc  *	   r will be represented as r := z+c for better accuracy.
61*84d9c625SLionel Sambuc  *
62*84d9c625SLionel Sambuc  *	2. Compute exp(r) by
63*84d9c625SLionel Sambuc  *
64*84d9c625SLionel Sambuc  *		exp(r) = 1 + r + r*R1/(2-R1),
65*84d9c625SLionel Sambuc  *	   where
66*84d9c625SLionel Sambuc  *		R1 = x - x^2*(p1+x^2*(p2+x^2*(p3+x^2*(p4+p5*x^2)))).
67*84d9c625SLionel Sambuc  *
68*84d9c625SLionel Sambuc  *	3. exp(x) = 2^k * exp(r) .
69*84d9c625SLionel Sambuc  *
70*84d9c625SLionel Sambuc  * Special cases:
71*84d9c625SLionel Sambuc  *	exp(INF) is INF, exp(NaN) is NaN;
72*84d9c625SLionel Sambuc  *	exp(-INF)=  0;
73*84d9c625SLionel Sambuc  *	for finite argument, only exp(0)=1 is exact.
74*84d9c625SLionel Sambuc  *
75*84d9c625SLionel Sambuc  * Accuracy:
76*84d9c625SLionel Sambuc  *	exp(x) returns the exponential of x nearly rounded. In a test run
77*84d9c625SLionel Sambuc  *	with 1,156,000 random arguments on a VAX, the maximum observed
78*84d9c625SLionel Sambuc  *	error was 0.869 ulps (units in the last place).
79*84d9c625SLionel Sambuc  */
80*84d9c625SLionel Sambuc 
81*84d9c625SLionel Sambuc #include "math.h"
82*84d9c625SLionel Sambuc #include "math_private.h"
83*84d9c625SLionel Sambuc 
84*84d9c625SLionel Sambuc static const double p1 = 0x1.555555555553ep-3;
85*84d9c625SLionel Sambuc static const double p2 = -0x1.6c16c16bebd93p-9;
86*84d9c625SLionel Sambuc static const double p3 = 0x1.1566aaf25de2cp-14;
87*84d9c625SLionel Sambuc static const double p4 = -0x1.bbd41c5d26bf1p-20;
88*84d9c625SLionel Sambuc static const double p5 = 0x1.6376972bea4d0p-25;
89*84d9c625SLionel Sambuc static const double ln2hi = 0x1.62e42fee00000p-1;
90*84d9c625SLionel Sambuc static const double ln2lo = 0x1.a39ef35793c76p-33;
91*84d9c625SLionel Sambuc static const double lnhuge = 0x1.6602b15b7ecf2p9;
92*84d9c625SLionel Sambuc static const double lntiny = -0x1.77af8ebeae354p9;
93*84d9c625SLionel Sambuc static const double invln2 = 0x1.71547652b82fep0;
94*84d9c625SLionel Sambuc 
95*84d9c625SLionel Sambuc /* returns exp(r = x + c) for |c| < |x| with no overlap.  */
96*84d9c625SLionel Sambuc 
97*84d9c625SLionel Sambuc double
__exp__D(double x,double c)98*84d9c625SLionel Sambuc __exp__D(double x, double c)
99*84d9c625SLionel Sambuc {
100*84d9c625SLionel Sambuc 	double  z,hi,lo;
101*84d9c625SLionel Sambuc 	int k;
102*84d9c625SLionel Sambuc 
103*84d9c625SLionel Sambuc 	if (x != x)	/* x is NaN */
104*84d9c625SLionel Sambuc 		return(x);
105*84d9c625SLionel Sambuc 	if ( x <= lnhuge ) {
106*84d9c625SLionel Sambuc 		if ( x >= lntiny ) {
107*84d9c625SLionel Sambuc 
108*84d9c625SLionel Sambuc 		    /* argument reduction : x --> x - k*ln2 */
109*84d9c625SLionel Sambuc 			z = invln2*x;
110*84d9c625SLionel Sambuc 			k = z + copysign(.5, x);
111*84d9c625SLionel Sambuc 
112*84d9c625SLionel Sambuc 		    /* express (x+c)-k*ln2 as hi-lo and let x=hi-lo rounded */
113*84d9c625SLionel Sambuc 
114*84d9c625SLionel Sambuc 			hi=(x-k*ln2hi);			/* Exact. */
115*84d9c625SLionel Sambuc 			x= hi - (lo = k*ln2lo-c);
116*84d9c625SLionel Sambuc 		    /* return 2^k*[1+x+x*c/(2+c)]  */
117*84d9c625SLionel Sambuc 			z=x*x;
118*84d9c625SLionel Sambuc 			c= x - z*(p1+z*(p2+z*(p3+z*(p4+z*p5))));
119*84d9c625SLionel Sambuc 			c = (x*c)/(2.0-c);
120*84d9c625SLionel Sambuc 
121*84d9c625SLionel Sambuc 			return  scalb(1.+(hi-(lo - c)), k);
122*84d9c625SLionel Sambuc 		}
123*84d9c625SLionel Sambuc 		/* end of x > lntiny */
124*84d9c625SLionel Sambuc 
125*84d9c625SLionel Sambuc 		else
126*84d9c625SLionel Sambuc 		     /* exp(-big#) underflows to zero */
127*84d9c625SLionel Sambuc 		     if(finite(x))  return(scalb(1.0,-5000));
128*84d9c625SLionel Sambuc 
129*84d9c625SLionel Sambuc 		     /* exp(-INF) is zero */
130*84d9c625SLionel Sambuc 		     else return(0.0);
131*84d9c625SLionel Sambuc 	}
132*84d9c625SLionel Sambuc 	/* end of x < lnhuge */
133*84d9c625SLionel Sambuc 
134*84d9c625SLionel Sambuc 	else
135*84d9c625SLionel Sambuc 	/* exp(INF) is INF, exp(+big#) overflows to INF */
136*84d9c625SLionel Sambuc 	    return( finite(x) ?  scalb(1.0,5000)  : x);
137*84d9c625SLionel Sambuc }
138