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
15*24706Selefunt static char sccsid[] =
16*24706Selefunt "@(#)exp__E.c	1.2 (Berkeley) 8/21/85; 1.2 (ucb.elefunt) 09/11/85";
1724594Szliu #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 
7024594Szliu #ifdef VAX	/* VAX D format */
7124594Szliu /* static double */
7224594Szliu /* p1     =  1.5150724356786683059E-2    , Hex  2^ -6   *  .F83ABE67E1066A */
7324594Szliu /* p2     =  6.3112487873718332688E-5    , Hex  2^-13   *  .845B4248CD0173 */
7424594Szliu /* q1     =  1.1363478204690669916E-1    , Hex  2^ -3   *  .E8B95A44A2EC45 */
7524594Szliu /* q2     =  1.2624568129896839182E-3    , Hex  2^ -9   *  .A5790572E4F5E7 */
7624594Szliu /* q3     =  1.5021856115869022674E-6    ; Hex  2^-19   *  .C99EB4604AC395 */
7724594Szliu static long        p1x[] = { 0x3abe3d78, 0x066a67e1};
7824594Szliu static long        p2x[] = { 0x5b423984, 0x017348cd};
7924594Szliu static long        q1x[] = { 0xb95a3ee8, 0xec4544a2};
8024594Szliu static long        q2x[] = { 0x79053ba5, 0xf5e772e4};
8124594Szliu static long        q3x[] = { 0x9eb436c9, 0xc395604a};
8224594Szliu #define       p1    (*(double*)p1x)
8324594Szliu #define       p2    (*(double*)p2x)
8424594Szliu #define       q1    (*(double*)q1x)
8524594Szliu #define       q2    (*(double*)q2x)
8624594Szliu #define       q3    (*(double*)q3x)
8724594Szliu #else	/* IEEE double */
8824594Szliu static double
8924594Szliu p1     =  1.3887401997267371720E-2    , /*Hex  2^ -7   *  1.C70FF8B3CC2CF */
9024594Szliu p2     =  3.3044019718331897649E-5    , /*Hex  2^-15   *  1.15317DF4526C4 */
9124594Szliu q1     =  1.1110813732786649355E-1    , /*Hex  2^ -4   *  1.C719538248597 */
9224594Szliu q2     =  9.9176615021572857300E-4    ; /*Hex  2^-10   *  1.03FC4CB8C98E8 */
9324594Szliu #endif
9424594Szliu 
9524594Szliu double exp__E(x,c)
9624594Szliu double x,c;
9724594Szliu {
9824594Szliu 	double static zero=0.0, one=1.0, half=1.0/2.0, small=1.0E-19;
9924594Szliu 	double copysign(),z,p,q,xp,xh,w;
10024594Szliu 	if(copysign(x,one)>small) {
10124594Szliu            z = x*x  ;
10224594Szliu 	   p = z*( p1 +z* p2 );
10324594Szliu #ifdef VAX
10424594Szliu            q = z*( q1 +z*( q2 +z* q3 ));
10524594Szliu #else	/* IEEE double */
10624594Szliu            q = z*( q1 +z*  q2 );
10724594Szliu #endif
10824594Szliu            xp= x*p     ;
10924594Szliu 	   xh= x*half  ;
11024594Szliu            w = xh-(q-xp)  ;
11124594Szliu 	   p = p+p;
11224594Szliu 	   c += x*((xh*w-(q-(p+xp)))/(one-w)+c);
11324594Szliu 	   return(z*half+c);
11424594Szliu 	}
11524594Szliu 	/* end of |x| > small */
11624594Szliu 
11724594Szliu 	else {
11824594Szliu 	    if(x!=zero) one+small;	/* raise the inexact flag */
11924594Szliu 	    return(copysign(zero,x));
12024594Szliu 	}
12124594Szliu }
122