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__E.c	5.2 (Berkeley) 04/29/88";
20 #endif /* not lint */
21 
22 /* exp__E(x,c)
23  * ASSUMPTION: c << x  SO THAT  fl(x+c)=x.
24  * (c is the correction term for x)
25  * exp__E RETURNS
26  *
27  *			 /  exp(x+c) - 1 - x ,  1E-19 < |x| < .3465736
28  *       exp__E(x,c) = 	|
29  *			 \  0 ,  |x| < 1E-19.
30  *
31  * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS)
32  * KERNEL FUNCTION OF EXP, EXPM1, POW FUNCTIONS
33  * CODED IN C BY K.C. NG, 1/31/85;
34  * REVISED BY K.C. NG on 3/16/85, 4/16/85.
35  *
36  * Required system supported function:
37  *	copysign(x,y)
38  *
39  * Method:
40  *	1. Rational approximation. Let r=x+c.
41  *	   Based on
42  *                                   2 * sinh(r/2)
43  *                exp(r) - 1 =   ----------------------   ,
44  *                               cosh(r/2) - sinh(r/2)
45  *	   exp__E(r) is computed using
46  *                   x*x            (x/2)*W - ( Q - ( 2*P  + x*P ) )
47  *                   --- + (c + x*[---------------------------------- + c ])
48  *                    2                          1 - W
49  * 	   where  P := p1*x^2 + p2*x^4,
50  *	          Q := q1*x^2 + q2*x^4 (for 56 bits precision, add q3*x^6)
51  *	          W := x/2-(Q-x*P),
52  *
53  *	   (See the listing below for the values of p1,p2,q1,q2,q3. The poly-
54  *	    nomials P and Q may be regarded as the approximations to sinh
55  *	    and cosh :
56  *		sinh(r/2) =  r/2 + r * P  ,  cosh(r/2) =  1 + Q . )
57  *
58  *         The coefficients were obtained by a special Remez algorithm.
59  *
60  * Approximation error:
61  *
62  *   |	exp(x) - 1			   |        2**(-57),  (IEEE double)
63  *   | ------------  -  (exp__E(x,0)+x)/x  |  <=
64  *   |	     x			           |	    2**(-69).  (VAX D)
65  *
66  * Constants:
67  * The hexadecimal values are the intended ones for the following constants.
68  * The decimal values may be used, provided that the compiler will convert
69  * from decimal to binary accurately enough to produce the hexadecimal values
70  * shown.
71  */
72 
73 #if defined(vax)||defined(tahoe)	/* VAX D format */
74 #ifdef vax
75 #define _0x(A,B)	0x/**/A/**/B
76 #else	/* vax */
77 #define _0x(A,B)	0x/**/B/**/A
78 #endif	/* vax */
79 /* static double */
80 /* p1     =  1.5150724356786683059E-2    , Hex  2^ -6   *  .F83ABE67E1066A */
81 /* p2     =  6.3112487873718332688E-5    , Hex  2^-13   *  .845B4248CD0173 */
82 /* q1     =  1.1363478204690669916E-1    , Hex  2^ -3   *  .E8B95A44A2EC45 */
83 /* q2     =  1.2624568129896839182E-3    , Hex  2^ -9   *  .A5790572E4F5E7 */
84 /* q3     =  1.5021856115869022674E-6    ; Hex  2^-19   *  .C99EB4604AC395 */
85 static long        p1x[] = { _0x(3abe,3d78), _0x(066a,67e1)};
86 static long        p2x[] = { _0x(5b42,3984), _0x(0173,48cd)};
87 static long        q1x[] = { _0x(b95a,3ee8), _0x(ec45,44a2)};
88 static long        q2x[] = { _0x(7905,3ba5), _0x(f5e7,72e4)};
89 static long        q3x[] = { _0x(9eb4,36c9), _0x(c395,604a)};
90 #define       p1    (*(double*)p1x)
91 #define       p2    (*(double*)p2x)
92 #define       q1    (*(double*)q1x)
93 #define       q2    (*(double*)q2x)
94 #define       q3    (*(double*)q3x)
95 #else	/* defined(vax)||defined(tahoe)	*/
96 static double
97 p1     =  1.3887401997267371720E-2    , /*Hex  2^ -7   *  1.C70FF8B3CC2CF */
98 p2     =  3.3044019718331897649E-5    , /*Hex  2^-15   *  1.15317DF4526C4 */
99 q1     =  1.1110813732786649355E-1    , /*Hex  2^ -4   *  1.C719538248597 */
100 q2     =  9.9176615021572857300E-4    ; /*Hex  2^-10   *  1.03FC4CB8C98E8 */
101 #endif	/* defined(vax)||defined(tahoe)	*/
102 
103 double exp__E(x,c)
104 double x,c;
105 {
106 	static double zero=0.0, one=1.0, half=1.0/2.0, small=1.0E-19;
107 	double copysign(),z,p,q,xp,xh,w;
108 	if(copysign(x,one)>small) {
109            z = x*x  ;
110 	   p = z*( p1 +z* p2 );
111 #if defined(vax)||defined(tahoe)
112            q = z*( q1 +z*( q2 +z* q3 ));
113 #else	/* defined(vax)||defined(tahoe) */
114            q = z*( q1 +z*  q2 );
115 #endif	/* defined(vax)||defined(tahoe) */
116            xp= x*p     ;
117 	   xh= x*half  ;
118            w = xh-(q-xp)  ;
119 	   p = p+p;
120 	   c += x*((xh*w-(q-(p+xp)))/(one-w)+c);
121 	   return(z*half+c);
122 	}
123 	/* end of |x| > small */
124 
125 	else {
126 	    if(x!=zero) one+small;	/* raise the inexact flag */
127 	    return(copysign(zero,x));
128 	}
129 }
130