124594Szliu /*
224594Szliu  * Copyright (c) 1985 Regents of the University of California.
324594Szliu  *
424594Szliu  * Use and reproduction of this software are granted  in  accordance  with
524594Szliu  * the terms and conditions specified in  the  Berkeley  Software  License
624594Szliu  * Agreement (in particular, this entails acknowledgement of the programs'
724594Szliu  * source, and inclusion of this notice) with the additional understanding
824594Szliu  * that  all  recipients  should regard themselves as participants  in  an
924594Szliu  * ongoing  research  project and hence should  feel  obligated  to report
1024594Szliu  * their  experiences (good or bad) with these elementary function  codes,
1124594Szliu  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
1224594Szliu  */
1324594Szliu 
1424594Szliu #ifndef lint
1524706Selefunt static char sccsid[] =
16*31853Szliu "@(#)exp__E.c	1.2 (Berkeley) 8/21/85; 1.6 (ucb.elefunt) 07/13/87";
17*31853Szliu #endif	/* not lint */
1824594Szliu 
1924594Szliu /* exp__E(x,c)
2024594Szliu  * ASSUMPTION: c << x  SO THAT  fl(x+c)=x.
2124594Szliu  * (c is the correction term for x)
2224594Szliu  * exp__E RETURNS
2324594Szliu  *
2424594Szliu  *			 /  exp(x+c) - 1 - x ,  1E-19 < |x| < .3465736
2524594Szliu  *       exp__E(x,c) = 	|
2624594Szliu  *			 \  0 ,  |x| < 1E-19.
2724594Szliu  *
2824594Szliu  * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS)
2924594Szliu  * KERNEL FUNCTION OF EXP, EXPM1, POW FUNCTIONS
3024594Szliu  * CODED IN C BY K.C. NG, 1/31/85;
3124594Szliu  * REVISED BY K.C. NG on 3/16/85, 4/16/85.
3224594Szliu  *
3324594Szliu  * Required system supported function:
3424594Szliu  *	copysign(x,y)
3524594Szliu  *
3624594Szliu  * Method:
3724594Szliu  *	1. Rational approximation. Let r=x+c.
3824594Szliu  *	   Based on
3924594Szliu  *                                   2 * sinh(r/2)
4024594Szliu  *                exp(r) - 1 =   ----------------------   ,
4124594Szliu  *                               cosh(r/2) - sinh(r/2)
4224594Szliu  *	   exp__E(r) is computed using
4324594Szliu  *                   x*x            (x/2)*W - ( Q - ( 2*P  + x*P ) )
4424594Szliu  *                   --- + (c + x*[---------------------------------- + c ])
4524594Szliu  *                    2                          1 - W
4624594Szliu  * 	   where  P := p1*x^2 + p2*x^4,
4724594Szliu  *	          Q := q1*x^2 + q2*x^4 (for 56 bits precision, add q3*x^6)
4824594Szliu  *	          W := x/2-(Q-x*P),
4924594Szliu  *
5024594Szliu  *	   (See the listing below for the values of p1,p2,q1,q2,q3. The poly-
5124594Szliu  *	    nomials P and Q may be regarded as the approximations to sinh
5224594Szliu  *	    and cosh :
5324594Szliu  *		sinh(r/2) =  r/2 + r * P  ,  cosh(r/2) =  1 + Q . )
5424594Szliu  *
5524594Szliu  *         The coefficients were obtained by a special Remez algorithm.
5624594Szliu  *
5724594Szliu  * Approximation error:
5824594Szliu  *
5924594Szliu  *   |	exp(x) - 1			   |        2**(-57),  (IEEE double)
6024594Szliu  *   | ------------  -  (exp__E(x,0)+x)/x  |  <=
6124594Szliu  *   |	     x			           |	    2**(-69).  (VAX D)
6224594Szliu  *
6324594Szliu  * Constants:
6424594Szliu  * The hexadecimal values are the intended ones for the following constants.
6524594Szliu  * The decimal values may be used, provided that the compiler will convert
6624594Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
6724594Szliu  * shown.
6824594Szliu  */
6924594Szliu 
70*31853Szliu #if defined(vax)||defined(tahoe)	/* VAX D format */
71*31853Szliu #ifdef vax
7231812Szliu #define _0x(A,B)	0x/**/A/**/B
73*31853Szliu #else	/* vax */
7431812Szliu #define _0x(A,B)	0x/**/B/**/A
75*31853Szliu #endif	/* vax */
7624594Szliu /* static double */
7724594Szliu /* p1     =  1.5150724356786683059E-2    , Hex  2^ -6   *  .F83ABE67E1066A */
7824594Szliu /* p2     =  6.3112487873718332688E-5    , Hex  2^-13   *  .845B4248CD0173 */
7924594Szliu /* q1     =  1.1363478204690669916E-1    , Hex  2^ -3   *  .E8B95A44A2EC45 */
8024594Szliu /* q2     =  1.2624568129896839182E-3    , Hex  2^ -9   *  .A5790572E4F5E7 */
8124594Szliu /* q3     =  1.5021856115869022674E-6    ; Hex  2^-19   *  .C99EB4604AC395 */
8231812Szliu static long        p1x[] = { _0x(3abe,3d78), _0x(066a,67e1)};
8331812Szliu static long        p2x[] = { _0x(5b42,3984), _0x(0173,48cd)};
8431812Szliu static long        q1x[] = { _0x(b95a,3ee8), _0x(ec45,44a2)};
8531812Szliu static long        q2x[] = { _0x(7905,3ba5), _0x(f5e7,72e4)};
8631812Szliu static long        q3x[] = { _0x(9eb4,36c9), _0x(c395,604a)};
8724594Szliu #define       p1    (*(double*)p1x)
8824594Szliu #define       p2    (*(double*)p2x)
8924594Szliu #define       q1    (*(double*)q1x)
9024594Szliu #define       q2    (*(double*)q2x)
9124594Szliu #define       q3    (*(double*)q3x)
92*31853Szliu #else	/* defined(vax)||defined(tahoe)	*/
9324594Szliu static double
9424594Szliu p1     =  1.3887401997267371720E-2    , /*Hex  2^ -7   *  1.C70FF8B3CC2CF */
9524594Szliu p2     =  3.3044019718331897649E-5    , /*Hex  2^-15   *  1.15317DF4526C4 */
9624594Szliu q1     =  1.1110813732786649355E-1    , /*Hex  2^ -4   *  1.C719538248597 */
9724594Szliu q2     =  9.9176615021572857300E-4    ; /*Hex  2^-10   *  1.03FC4CB8C98E8 */
98*31853Szliu #endif	/* defined(vax)||defined(tahoe)	*/
9924594Szliu 
10024594Szliu double exp__E(x,c)
10124594Szliu double x,c;
10224594Szliu {
10326893Selefunt 	static double zero=0.0, one=1.0, half=1.0/2.0, small=1.0E-19;
10424594Szliu 	double copysign(),z,p,q,xp,xh,w;
10524594Szliu 	if(copysign(x,one)>small) {
10624594Szliu            z = x*x  ;
10724594Szliu 	   p = z*( p1 +z* p2 );
108*31853Szliu #if defined(vax)||defined(tahoe)
10924594Szliu            q = z*( q1 +z*( q2 +z* q3 ));
110*31853Szliu #else	/* defined(vax)||defined(tahoe) */
11124594Szliu            q = z*( q1 +z*  q2 );
112*31853Szliu #endif	/* defined(vax)||defined(tahoe) */
11324594Szliu            xp= x*p     ;
11424594Szliu 	   xh= x*half  ;
11524594Szliu            w = xh-(q-xp)  ;
11624594Szliu 	   p = p+p;
11724594Szliu 	   c += x*((xh*w-(q-(p+xp)))/(one-w)+c);
11824594Szliu 	   return(z*half+c);
11924594Szliu 	}
12024594Szliu 	/* end of |x| > small */
12124594Szliu 
12224594Szliu 	else {
12324594Szliu 	    if(x!=zero) one+small;	/* raise the inexact flag */
12424594Szliu 	    return(copysign(zero,x));
12524594Szliu 	}
12624594Szliu }
127