xref: /original-bsd/lib/libm/common_source/exp.c (revision f1656be1)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  *
12  * All recipients should regard themselves as participants in an ongoing
13  * research project and hence should feel obligated to report their
14  * experiences (good or bad) with these elementary function codes, using
15  * the sendbug(8) program, to the authors.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)exp.c	5.2 (Berkeley) 04/29/88";
20 #endif /* not lint */
21 
22 /* EXP(X)
23  * RETURN THE EXPONENTIAL OF X
24  * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS)
25  * CODED IN C BY K.C. NG, 1/19/85;
26  * REVISED BY K.C. NG on 2/6/85, 2/15/85, 3/7/85, 3/24/85, 4/16/85, 6/14/86.
27  *
28  * Required system supported functions:
29  *	scalb(x,n)
30  *	copysign(x,y)
31  *	finite(x)
32  *
33  * Method:
34  *	1. Argument Reduction: given the input x, find r and integer k such
35  *	   that
36  *	                   x = k*ln2 + r,  |r| <= 0.5*ln2 .
37  *	   r will be represented as r := z+c for better accuracy.
38  *
39  *	2. Compute exp(r) by
40  *
41  *		exp(r) = 1 + r + r*R1/(2-R1),
42  *	   where
43  *		R1 = x - x^2*(p1+x^2*(p2+x^2*(p3+x^2*(p4+p5*x^2)))).
44  *
45  *	3. exp(x) = 2^k * exp(r) .
46  *
47  * Special cases:
48  *	exp(INF) is INF, exp(NaN) is NaN;
49  *	exp(-INF)=  0;
50  *	for finite argument, only exp(0)=1 is exact.
51  *
52  * Accuracy:
53  *	exp(x) returns the exponential of x nearly rounded. In a test run
54  *	with 1,156,000 random arguments on a VAX, the maximum observed
55  *	error was 0.869 ulps (units in the last place).
56  *
57  * Constants:
58  * The hexadecimal values are the intended ones for the following constants.
59  * The decimal values may be used, provided that the compiler will convert
60  * from decimal to binary accurately enough to produce the hexadecimal values
61  * shown.
62  */
63 
64 #if defined(vax)||defined(tahoe)	/* VAX D format */
65 #ifdef vax
66 #define _0x(A,B)	0x/**/A/**/B
67 #else	/* vax */
68 #define _0x(A,B)	0x/**/B/**/A
69 #endif	/* vax */
70 /* static double */
71 /* ln2hi  =  6.9314718055829871446E-1    , Hex  2^  0   *  .B17217F7D00000 */
72 /* ln2lo  =  1.6465949582897081279E-12   , Hex  2^-39   *  .E7BCD5E4F1D9CC */
73 /* lnhuge =  9.4961163736712506989E1     , Hex  2^  7   *  .BDEC1DA73E9010 */
74 /* lntiny = -9.5654310917272452386E1     , Hex  2^  7   * -.BF4F01D72E33AF */
75 /* invln2 =  1.4426950408889634148E0     ; Hex  2^  1   *  .B8AA3B295C17F1 */
76 /* p1     =  1.6666666666666602251E-1    , Hex  2^-2    *  .AAAAAAAAAAA9F1 */
77 /* p2     = -2.7777777777015591216E-3    , Hex  2^-8    * -.B60B60B5F5EC94 */
78 /* p3     =  6.6137563214379341918E-5    , Hex  2^-13   *  .8AB355792EF15F */
79 /* p4     = -1.6533902205465250480E-6    , Hex  2^-19   * -.DDEA0E2E935F84 */
80 /* p5     =  4.1381367970572387085E-8    , Hex  2^-24   *  .B1BB4B95F52683 */
81 static long     ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)};
82 static long     ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)};
83 static long    lnhugex[] = { _0x(ec1d,43bd), _0x(9010,a73e)};
84 static long    lntinyx[] = { _0x(4f01,c3bf), _0x(33af,d72e)};
85 static long    invln2x[] = { _0x(aa3b,40b8), _0x(17f1,295c)};
86 static long        p1x[] = { _0x(aaaa,3f2a), _0x(a9f1,aaaa)};
87 static long        p2x[] = { _0x(0b60,bc36), _0x(ec94,b5f5)};
88 static long        p3x[] = { _0x(b355,398a), _0x(f15f,792e)};
89 static long        p4x[] = { _0x(ea0e,b6dd), _0x(5f84,2e93)};
90 static long        p5x[] = { _0x(bb4b,3431), _0x(2683,95f5)};
91 #define    ln2hi    (*(double*)ln2hix)
92 #define    ln2lo    (*(double*)ln2lox)
93 #define   lnhuge    (*(double*)lnhugex)
94 #define   lntiny    (*(double*)lntinyx)
95 #define   invln2    (*(double*)invln2x)
96 #define       p1    (*(double*)p1x)
97 #define       p2    (*(double*)p2x)
98 #define       p3    (*(double*)p3x)
99 #define       p4    (*(double*)p4x)
100 #define       p5    (*(double*)p5x)
101 
102 #else	/* defined(vax)||defined(tahoe)	*/
103 static double
104 p1     =  1.6666666666666601904E-1    , /*Hex  2^-3    *  1.555555555553E */
105 p2     = -2.7777777777015593384E-3    , /*Hex  2^-9    * -1.6C16C16BEBD93 */
106 p3     =  6.6137563214379343612E-5    , /*Hex  2^-14   *  1.1566AAF25DE2C */
107 p4     = -1.6533902205465251539E-6    , /*Hex  2^-20   * -1.BBD41C5D26BF1 */
108 p5     =  4.1381367970572384604E-8    , /*Hex  2^-25   *  1.6376972BEA4D0 */
109 ln2hi  =  6.9314718036912381649E-1    , /*Hex  2^ -1   *  1.62E42FEE00000 */
110 ln2lo  =  1.9082149292705877000E-10   , /*Hex  2^-33   *  1.A39EF35793C76 */
111 lnhuge =  7.1602103751842355450E2     , /*Hex  2^  9   *  1.6602B15B7ECF2 */
112 lntiny = -7.5137154372698068983E2     , /*Hex  2^  9   * -1.77AF8EBEAE354 */
113 invln2 =  1.4426950408889633870E0     ; /*Hex  2^  0   *  1.71547652B82FE */
114 #endif	/* defined(vax)||defined(tahoe)	*/
115 
116 double exp(x)
117 double x;
118 {
119 	double scalb(), copysign(), z,hi,lo,c;
120 	int k,finite();
121 
122 #if !defined(vax)&&!defined(tahoe)
123 	if(x!=x) return(x);	/* x is NaN */
124 #endif	/* !defined(vax)&&!defined(tahoe) */
125 	if( x <= lnhuge ) {
126 		if( x >= lntiny ) {
127 
128 		    /* argument reduction : x --> x - k*ln2 */
129 
130 			k=invln2*x+copysign(0.5,x);	/* k=NINT(x/ln2) */
131 
132 		    /* express x-k*ln2 as hi-lo and let x=hi-lo rounded */
133 
134 			hi=x-k*ln2hi;
135 			x=hi-(lo=k*ln2lo);
136 
137 		    /* return 2^k*[1+x+x*c/(2+c)]  */
138 			z=x*x;
139 			c= x - z*(p1+z*(p2+z*(p3+z*(p4+z*p5))));
140 			return  scalb(1.0+(hi-(lo-(x*c)/(2.0-c))),k);
141 
142 		}
143 		/* end of x > lntiny */
144 
145 		else
146 		     /* exp(-big#) underflows to zero */
147 		     if(finite(x))  return(scalb(1.0,-5000));
148 
149 		     /* exp(-INF) is zero */
150 		     else return(0.0);
151 	}
152 	/* end of x < lnhuge */
153 
154 	else
155 	/* exp(INF) is INF, exp(+big#) overflows to INF */
156 	    return( finite(x) ?  scalb(1.0,5000)  : x);
157 }
158