1*34126Sbostic /*
224594Szliu  * Copyright (c) 1985 Regents of the University of California.
3*34126Sbostic  * All rights reserved.
4*34126Sbostic  *
5*34126Sbostic  * Redistribution and use in source and binary forms are permitted
6*34126Sbostic  * provided that this notice is preserved and that due credit is given
7*34126Sbostic  * to the University of California at Berkeley. The name of the University
8*34126Sbostic  * may not be used to endorse or promote products derived from this
9*34126Sbostic  * software without specific prior written permission. This software
10*34126Sbostic  * is provided ``as is'' without express or implied warranty.
11*34126Sbostic  *
12*34126Sbostic  * All recipients should regard themselves as participants in an ongoing
13*34126Sbostic  * research project and hence should feel obligated to report their
14*34126Sbostic  * experiences (good or bad) with these elementary function codes, using
15*34126Sbostic  * the sendbug(8) program, to the authors.
1624594Szliu  */
1724594Szliu 
1824594Szliu #ifndef lint
19*34126Sbostic static char sccsid[] = "@(#)exp__E.c	5.2 (Berkeley) 04/29/88";
20*34126Sbostic #endif /* not lint */
2124594Szliu 
2224594Szliu /* exp__E(x,c)
2324594Szliu  * ASSUMPTION: c << x  SO THAT  fl(x+c)=x.
2424594Szliu  * (c is the correction term for x)
2524594Szliu  * exp__E RETURNS
2624594Szliu  *
2724594Szliu  *			 /  exp(x+c) - 1 - x ,  1E-19 < |x| < .3465736
2824594Szliu  *       exp__E(x,c) = 	|
2924594Szliu  *			 \  0 ,  |x| < 1E-19.
3024594Szliu  *
3124594Szliu  * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS)
3224594Szliu  * KERNEL FUNCTION OF EXP, EXPM1, POW FUNCTIONS
3324594Szliu  * CODED IN C BY K.C. NG, 1/31/85;
3424594Szliu  * REVISED BY K.C. NG on 3/16/85, 4/16/85.
3524594Szliu  *
3624594Szliu  * Required system supported function:
3724594Szliu  *	copysign(x,y)
3824594Szliu  *
3924594Szliu  * Method:
4024594Szliu  *	1. Rational approximation. Let r=x+c.
4124594Szliu  *	   Based on
4224594Szliu  *                                   2 * sinh(r/2)
4324594Szliu  *                exp(r) - 1 =   ----------------------   ,
4424594Szliu  *                               cosh(r/2) - sinh(r/2)
4524594Szliu  *	   exp__E(r) is computed using
4624594Szliu  *                   x*x            (x/2)*W - ( Q - ( 2*P  + x*P ) )
4724594Szliu  *                   --- + (c + x*[---------------------------------- + c ])
4824594Szliu  *                    2                          1 - W
4924594Szliu  * 	   where  P := p1*x^2 + p2*x^4,
5024594Szliu  *	          Q := q1*x^2 + q2*x^4 (for 56 bits precision, add q3*x^6)
5124594Szliu  *	          W := x/2-(Q-x*P),
5224594Szliu  *
5324594Szliu  *	   (See the listing below for the values of p1,p2,q1,q2,q3. The poly-
5424594Szliu  *	    nomials P and Q may be regarded as the approximations to sinh
5524594Szliu  *	    and cosh :
5624594Szliu  *		sinh(r/2) =  r/2 + r * P  ,  cosh(r/2) =  1 + Q . )
5724594Szliu  *
5824594Szliu  *         The coefficients were obtained by a special Remez algorithm.
5924594Szliu  *
6024594Szliu  * Approximation error:
6124594Szliu  *
6224594Szliu  *   |	exp(x) - 1			   |        2**(-57),  (IEEE double)
6324594Szliu  *   | ------------  -  (exp__E(x,0)+x)/x  |  <=
6424594Szliu  *   |	     x			           |	    2**(-69).  (VAX D)
6524594Szliu  *
6624594Szliu  * Constants:
6724594Szliu  * The hexadecimal values are the intended ones for the following constants.
6824594Szliu  * The decimal values may be used, provided that the compiler will convert
6924594Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
7024594Szliu  * shown.
7124594Szliu  */
7224594Szliu 
7331853Szliu #if defined(vax)||defined(tahoe)	/* VAX D format */
7431853Szliu #ifdef vax
7531812Szliu #define _0x(A,B)	0x/**/A/**/B
7631853Szliu #else	/* vax */
7731812Szliu #define _0x(A,B)	0x/**/B/**/A
7831853Szliu #endif	/* vax */
7924594Szliu /* static double */
8024594Szliu /* p1     =  1.5150724356786683059E-2    , Hex  2^ -6   *  .F83ABE67E1066A */
8124594Szliu /* p2     =  6.3112487873718332688E-5    , Hex  2^-13   *  .845B4248CD0173 */
8224594Szliu /* q1     =  1.1363478204690669916E-1    , Hex  2^ -3   *  .E8B95A44A2EC45 */
8324594Szliu /* q2     =  1.2624568129896839182E-3    , Hex  2^ -9   *  .A5790572E4F5E7 */
8424594Szliu /* q3     =  1.5021856115869022674E-6    ; Hex  2^-19   *  .C99EB4604AC395 */
8531812Szliu static long        p1x[] = { _0x(3abe,3d78), _0x(066a,67e1)};
8631812Szliu static long        p2x[] = { _0x(5b42,3984), _0x(0173,48cd)};
8731812Szliu static long        q1x[] = { _0x(b95a,3ee8), _0x(ec45,44a2)};
8831812Szliu static long        q2x[] = { _0x(7905,3ba5), _0x(f5e7,72e4)};
8931812Szliu static long        q3x[] = { _0x(9eb4,36c9), _0x(c395,604a)};
9024594Szliu #define       p1    (*(double*)p1x)
9124594Szliu #define       p2    (*(double*)p2x)
9224594Szliu #define       q1    (*(double*)q1x)
9324594Szliu #define       q2    (*(double*)q2x)
9424594Szliu #define       q3    (*(double*)q3x)
9531853Szliu #else	/* defined(vax)||defined(tahoe)	*/
9624594Szliu static double
9724594Szliu p1     =  1.3887401997267371720E-2    , /*Hex  2^ -7   *  1.C70FF8B3CC2CF */
9824594Szliu p2     =  3.3044019718331897649E-5    , /*Hex  2^-15   *  1.15317DF4526C4 */
9924594Szliu q1     =  1.1110813732786649355E-1    , /*Hex  2^ -4   *  1.C719538248597 */
10024594Szliu q2     =  9.9176615021572857300E-4    ; /*Hex  2^-10   *  1.03FC4CB8C98E8 */
10131853Szliu #endif	/* defined(vax)||defined(tahoe)	*/
10224594Szliu 
10324594Szliu double exp__E(x,c)
10424594Szliu double x,c;
10524594Szliu {
10626893Selefunt 	static double zero=0.0, one=1.0, half=1.0/2.0, small=1.0E-19;
10724594Szliu 	double copysign(),z,p,q,xp,xh,w;
10824594Szliu 	if(copysign(x,one)>small) {
10924594Szliu            z = x*x  ;
11024594Szliu 	   p = z*( p1 +z* p2 );
11131853Szliu #if defined(vax)||defined(tahoe)
11224594Szliu            q = z*( q1 +z*( q2 +z* q3 ));
11331853Szliu #else	/* defined(vax)||defined(tahoe) */
11424594Szliu            q = z*( q1 +z*  q2 );
11531853Szliu #endif	/* defined(vax)||defined(tahoe) */
11624594Szliu            xp= x*p     ;
11724594Szliu 	   xh= x*half  ;
11824594Szliu            w = xh-(q-xp)  ;
11924594Szliu 	   p = p+p;
12024594Szliu 	   c += x*((xh*w-(q-(p+xp)))/(one-w)+c);
12124594Szliu 	   return(z*half+c);
12224594Szliu 	}
12324594Szliu 	/* end of |x| > small */
12424594Szliu 
12524594Szliu 	else {
12624594Szliu 	    if(x!=zero) one+small;	/* raise the inexact flag */
12724594Szliu 	    return(copysign(zero,x));
12824594Szliu 	}
12924594Szliu }
130