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