1*24594Szliu /*
2*24594Szliu  * Copyright (c) 1985 Regents of the University of California.
3*24594Szliu  *
4*24594Szliu  * Use and reproduction of this software are granted  in  accordance  with
5*24594Szliu  * the terms and conditions specified in  the  Berkeley  Software  License
6*24594Szliu  * Agreement (in particular, this entails acknowledgement of the programs'
7*24594Szliu  * source, and inclusion of this notice) with the additional understanding
8*24594Szliu  * that  all  recipients  should regard themselves as participants  in  an
9*24594Szliu  * ongoing  research  project and hence should  feel  obligated  to report
10*24594Szliu  * their  experiences (good or bad) with these elementary function  codes,
11*24594Szliu  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
12*24594Szliu  */
13*24594Szliu 
14*24594Szliu #ifndef lint
15*24594Szliu static char sccsid[] = "@(#)exp__E.c	1.1 (ELEFUNT) 09/06/85";
16*24594Szliu #endif not lint
17*24594Szliu 
18*24594Szliu /* exp__E(x,c)
19*24594Szliu  * ASSUMPTION: c << x  SO THAT  fl(x+c)=x.
20*24594Szliu  * (c is the correction term for x)
21*24594Szliu  * exp__E RETURNS
22*24594Szliu  *
23*24594Szliu  *			 /  exp(x+c) - 1 - x ,  1E-19 < |x| < .3465736
24*24594Szliu  *       exp__E(x,c) = 	|
25*24594Szliu  *			 \  0 ,  |x| < 1E-19.
26*24594Szliu  *
27*24594Szliu  * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS)
28*24594Szliu  * KERNEL FUNCTION OF EXP, EXPM1, POW FUNCTIONS
29*24594Szliu  * CODED IN C BY K.C. NG, 1/31/85;
30*24594Szliu  * REVISED BY K.C. NG on 3/16/85, 4/16/85.
31*24594Szliu  *
32*24594Szliu  * Required system supported function:
33*24594Szliu  *	copysign(x,y)
34*24594Szliu  *
35*24594Szliu  * Method:
36*24594Szliu  *	1. Rational approximation. Let r=x+c.
37*24594Szliu  *	   Based on
38*24594Szliu  *                                   2 * sinh(r/2)
39*24594Szliu  *                exp(r) - 1 =   ----------------------   ,
40*24594Szliu  *                               cosh(r/2) - sinh(r/2)
41*24594Szliu  *	   exp__E(r) is computed using
42*24594Szliu  *                   x*x            (x/2)*W - ( Q - ( 2*P  + x*P ) )
43*24594Szliu  *                   --- + (c + x*[---------------------------------- + c ])
44*24594Szliu  *                    2                          1 - W
45*24594Szliu  * 	   where  P := p1*x^2 + p2*x^4,
46*24594Szliu  *	          Q := q1*x^2 + q2*x^4 (for 56 bits precision, add q3*x^6)
47*24594Szliu  *	          W := x/2-(Q-x*P),
48*24594Szliu  *
49*24594Szliu  *	   (See the listing below for the values of p1,p2,q1,q2,q3. The poly-
50*24594Szliu  *	    nomials P and Q may be regarded as the approximations to sinh
51*24594Szliu  *	    and cosh :
52*24594Szliu  *		sinh(r/2) =  r/2 + r * P  ,  cosh(r/2) =  1 + Q . )
53*24594Szliu  *
54*24594Szliu  *         The coefficients were obtained by a special Remez algorithm.
55*24594Szliu  *
56*24594Szliu  * Approximation error:
57*24594Szliu  *
58*24594Szliu  *   |	exp(x) - 1			   |        2**(-57),  (IEEE double)
59*24594Szliu  *   | ------------  -  (exp__E(x,0)+x)/x  |  <=
60*24594Szliu  *   |	     x			           |	    2**(-69).  (VAX D)
61*24594Szliu  *
62*24594Szliu  * Constants:
63*24594Szliu  * The hexadecimal values are the intended ones for the following constants.
64*24594Szliu  * The decimal values may be used, provided that the compiler will convert
65*24594Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
66*24594Szliu  * shown.
67*24594Szliu  */
68*24594Szliu 
69*24594Szliu #ifdef VAX	/* VAX D format */
70*24594Szliu /* static double */
71*24594Szliu /* p1     =  1.5150724356786683059E-2    , Hex  2^ -6   *  .F83ABE67E1066A */
72*24594Szliu /* p2     =  6.3112487873718332688E-5    , Hex  2^-13   *  .845B4248CD0173 */
73*24594Szliu /* q1     =  1.1363478204690669916E-1    , Hex  2^ -3   *  .E8B95A44A2EC45 */
74*24594Szliu /* q2     =  1.2624568129896839182E-3    , Hex  2^ -9   *  .A5790572E4F5E7 */
75*24594Szliu /* q3     =  1.5021856115869022674E-6    ; Hex  2^-19   *  .C99EB4604AC395 */
76*24594Szliu static long        p1x[] = { 0x3abe3d78, 0x066a67e1};
77*24594Szliu static long        p2x[] = { 0x5b423984, 0x017348cd};
78*24594Szliu static long        q1x[] = { 0xb95a3ee8, 0xec4544a2};
79*24594Szliu static long        q2x[] = { 0x79053ba5, 0xf5e772e4};
80*24594Szliu static long        q3x[] = { 0x9eb436c9, 0xc395604a};
81*24594Szliu #define       p1    (*(double*)p1x)
82*24594Szliu #define       p2    (*(double*)p2x)
83*24594Szliu #define       q1    (*(double*)q1x)
84*24594Szliu #define       q2    (*(double*)q2x)
85*24594Szliu #define       q3    (*(double*)q3x)
86*24594Szliu #else	/* IEEE double */
87*24594Szliu static double
88*24594Szliu p1     =  1.3887401997267371720E-2    , /*Hex  2^ -7   *  1.C70FF8B3CC2CF */
89*24594Szliu p2     =  3.3044019718331897649E-5    , /*Hex  2^-15   *  1.15317DF4526C4 */
90*24594Szliu q1     =  1.1110813732786649355E-1    , /*Hex  2^ -4   *  1.C719538248597 */
91*24594Szliu q2     =  9.9176615021572857300E-4    ; /*Hex  2^-10   *  1.03FC4CB8C98E8 */
92*24594Szliu #endif
93*24594Szliu 
94*24594Szliu double exp__E(x,c)
95*24594Szliu double x,c;
96*24594Szliu {
97*24594Szliu 	double static zero=0.0, one=1.0, half=1.0/2.0, small=1.0E-19;
98*24594Szliu 	double copysign(),z,p,q,xp,xh,w;
99*24594Szliu 	if(copysign(x,one)>small) {
100*24594Szliu            z = x*x  ;
101*24594Szliu 	   p = z*( p1 +z* p2 );
102*24594Szliu #ifdef VAX
103*24594Szliu            q = z*( q1 +z*( q2 +z* q3 ));
104*24594Szliu #else	/* IEEE double */
105*24594Szliu            q = z*( q1 +z*  q2 );
106*24594Szliu #endif
107*24594Szliu            xp= x*p     ;
108*24594Szliu 	   xh= x*half  ;
109*24594Szliu            w = xh-(q-xp)  ;
110*24594Szliu 	   p = p+p;
111*24594Szliu 	   c += x*((xh*w-(q-(p+xp)))/(one-w)+c);
112*24594Szliu 	   return(z*half+c);
113*24594Szliu 	}
114*24594Szliu 	/* end of |x| > small */
115*24594Szliu 
116*24594Szliu 	else {
117*24594Szliu 	    if(x!=zero) one+small;	/* raise the inexact flag */
118*24594Szliu 	    return(copysign(zero,x));
119*24594Szliu 	}
120*24594Szliu }
121