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