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 the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * All recipients should regard themselves as participants in an ongoing
18  * research project and hence should feel obligated to report their
19  * experiences (good or bad) with these elementary function codes, using
20  * the sendbug(8) program, to the authors.
21  */
22 
23 #ifndef lint
24 static char sccsid[] = "@(#)exp__E.c	5.3 (Berkeley) 06/30/88";
25 #endif /* not lint */
26 
27 /* exp__E(x,c)
28  * ASSUMPTION: c << x  SO THAT  fl(x+c)=x.
29  * (c is the correction term for x)
30  * exp__E RETURNS
31  *
32  *			 /  exp(x+c) - 1 - x ,  1E-19 < |x| < .3465736
33  *       exp__E(x,c) = 	|
34  *			 \  0 ,  |x| < 1E-19.
35  *
36  * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS)
37  * KERNEL FUNCTION OF EXP, EXPM1, POW FUNCTIONS
38  * CODED IN C BY K.C. NG, 1/31/85;
39  * REVISED BY K.C. NG on 3/16/85, 4/16/85.
40  *
41  * Required system supported function:
42  *	copysign(x,y)
43  *
44  * Method:
45  *	1. Rational approximation. Let r=x+c.
46  *	   Based on
47  *                                   2 * sinh(r/2)
48  *                exp(r) - 1 =   ----------------------   ,
49  *                               cosh(r/2) - sinh(r/2)
50  *	   exp__E(r) is computed using
51  *                   x*x            (x/2)*W - ( Q - ( 2*P  + x*P ) )
52  *                   --- + (c + x*[---------------------------------- + c ])
53  *                    2                          1 - W
54  * 	   where  P := p1*x^2 + p2*x^4,
55  *	          Q := q1*x^2 + q2*x^4 (for 56 bits precision, add q3*x^6)
56  *	          W := x/2-(Q-x*P),
57  *
58  *	   (See the listing below for the values of p1,p2,q1,q2,q3. The poly-
59  *	    nomials P and Q may be regarded as the approximations to sinh
60  *	    and cosh :
61  *		sinh(r/2) =  r/2 + r * P  ,  cosh(r/2) =  1 + Q . )
62  *
63  *         The coefficients were obtained by a special Remez algorithm.
64  *
65  * Approximation error:
66  *
67  *   |	exp(x) - 1			   |        2**(-57),  (IEEE double)
68  *   | ------------  -  (exp__E(x,0)+x)/x  |  <=
69  *   |	     x			           |	    2**(-69).  (VAX D)
70  *
71  * Constants:
72  * The hexadecimal values are the intended ones for the following constants.
73  * The decimal values may be used, provided that the compiler will convert
74  * from decimal to binary accurately enough to produce the hexadecimal values
75  * shown.
76  */
77 
78 #if defined(vax)||defined(tahoe)	/* VAX D format */
79 #ifdef vax
80 #define _0x(A,B)	0x/**/A/**/B
81 #else	/* vax */
82 #define _0x(A,B)	0x/**/B/**/A
83 #endif	/* vax */
84 /* static double */
85 /* p1     =  1.5150724356786683059E-2    , Hex  2^ -6   *  .F83ABE67E1066A */
86 /* p2     =  6.3112487873718332688E-5    , Hex  2^-13   *  .845B4248CD0173 */
87 /* q1     =  1.1363478204690669916E-1    , Hex  2^ -3   *  .E8B95A44A2EC45 */
88 /* q2     =  1.2624568129896839182E-3    , Hex  2^ -9   *  .A5790572E4F5E7 */
89 /* q3     =  1.5021856115869022674E-6    ; Hex  2^-19   *  .C99EB4604AC395 */
90 static long        p1x[] = { _0x(3abe,3d78), _0x(066a,67e1)};
91 static long        p2x[] = { _0x(5b42,3984), _0x(0173,48cd)};
92 static long        q1x[] = { _0x(b95a,3ee8), _0x(ec45,44a2)};
93 static long        q2x[] = { _0x(7905,3ba5), _0x(f5e7,72e4)};
94 static long        q3x[] = { _0x(9eb4,36c9), _0x(c395,604a)};
95 #define       p1    (*(double*)p1x)
96 #define       p2    (*(double*)p2x)
97 #define       q1    (*(double*)q1x)
98 #define       q2    (*(double*)q2x)
99 #define       q3    (*(double*)q3x)
100 #else	/* defined(vax)||defined(tahoe)	*/
101 static double
102 p1     =  1.3887401997267371720E-2    , /*Hex  2^ -7   *  1.C70FF8B3CC2CF */
103 p2     =  3.3044019718331897649E-5    , /*Hex  2^-15   *  1.15317DF4526C4 */
104 q1     =  1.1110813732786649355E-1    , /*Hex  2^ -4   *  1.C719538248597 */
105 q2     =  9.9176615021572857300E-4    ; /*Hex  2^-10   *  1.03FC4CB8C98E8 */
106 #endif	/* defined(vax)||defined(tahoe)	*/
107 
108 double exp__E(x,c)
109 double x,c;
110 {
111 	static double zero=0.0, one=1.0, half=1.0/2.0, small=1.0E-19;
112 	double copysign(),z,p,q,xp,xh,w;
113 	if(copysign(x,one)>small) {
114            z = x*x  ;
115 	   p = z*( p1 +z* p2 );
116 #if defined(vax)||defined(tahoe)
117            q = z*( q1 +z*( q2 +z* q3 ));
118 #else	/* defined(vax)||defined(tahoe) */
119            q = z*( q1 +z*  q2 );
120 #endif	/* defined(vax)||defined(tahoe) */
121            xp= x*p     ;
122 	   xh= x*half  ;
123            w = xh-(q-xp)  ;
124 	   p = p+p;
125 	   c += x*((xh*w-(q-(p+xp)))/(one-w)+c);
126 	   return(z*half+c);
127 	}
128 	/* end of |x| > small */
129 
130 	else {
131 	    if(x!=zero) one+small;	/* raise the inexact flag */
132 	    return(copysign(zero,x));
133 	}
134 }
135