134126Sbostic /*
224594Szliu  * Copyright (c) 1985 Regents of the University of California.
334126Sbostic  * All rights reserved.
434126Sbostic  *
534126Sbostic  * Redistribution and use in source and binary forms are permitted
6*34931Sbostic  * provided that the above copyright notice and this paragraph are
7*34931Sbostic  * duplicated in all such forms and that any documentation,
8*34931Sbostic  * advertising materials, and other materials related to such
9*34931Sbostic  * distribution and use acknowledge that the software was developed
10*34931Sbostic  * by the University of California, Berkeley.  The name of the
11*34931Sbostic  * University may not be used to endorse or promote products derived
12*34931Sbostic  * from this software without specific prior written permission.
13*34931Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34931Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34931Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1634126Sbostic  *
1734126Sbostic  * All recipients should regard themselves as participants in an ongoing
1834126Sbostic  * research project and hence should feel obligated to report their
1934126Sbostic  * experiences (good or bad) with these elementary function codes, using
2034126Sbostic  * the sendbug(8) program, to the authors.
2124594Szliu  */
2224594Szliu 
2324594Szliu #ifndef lint
24*34931Sbostic static char sccsid[] = "@(#)exp__E.c	5.3 (Berkeley) 06/30/88";
2534126Sbostic #endif /* not lint */
2624594Szliu 
2724594Szliu /* exp__E(x,c)
2824594Szliu  * ASSUMPTION: c << x  SO THAT  fl(x+c)=x.
2924594Szliu  * (c is the correction term for x)
3024594Szliu  * exp__E RETURNS
3124594Szliu  *
3224594Szliu  *			 /  exp(x+c) - 1 - x ,  1E-19 < |x| < .3465736
3324594Szliu  *       exp__E(x,c) = 	|
3424594Szliu  *			 \  0 ,  |x| < 1E-19.
3524594Szliu  *
3624594Szliu  * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS)
3724594Szliu  * KERNEL FUNCTION OF EXP, EXPM1, POW FUNCTIONS
3824594Szliu  * CODED IN C BY K.C. NG, 1/31/85;
3924594Szliu  * REVISED BY K.C. NG on 3/16/85, 4/16/85.
4024594Szliu  *
4124594Szliu  * Required system supported function:
4224594Szliu  *	copysign(x,y)
4324594Szliu  *
4424594Szliu  * Method:
4524594Szliu  *	1. Rational approximation. Let r=x+c.
4624594Szliu  *	   Based on
4724594Szliu  *                                   2 * sinh(r/2)
4824594Szliu  *                exp(r) - 1 =   ----------------------   ,
4924594Szliu  *                               cosh(r/2) - sinh(r/2)
5024594Szliu  *	   exp__E(r) is computed using
5124594Szliu  *                   x*x            (x/2)*W - ( Q - ( 2*P  + x*P ) )
5224594Szliu  *                   --- + (c + x*[---------------------------------- + c ])
5324594Szliu  *                    2                          1 - W
5424594Szliu  * 	   where  P := p1*x^2 + p2*x^4,
5524594Szliu  *	          Q := q1*x^2 + q2*x^4 (for 56 bits precision, add q3*x^6)
5624594Szliu  *	          W := x/2-(Q-x*P),
5724594Szliu  *
5824594Szliu  *	   (See the listing below for the values of p1,p2,q1,q2,q3. The poly-
5924594Szliu  *	    nomials P and Q may be regarded as the approximations to sinh
6024594Szliu  *	    and cosh :
6124594Szliu  *		sinh(r/2) =  r/2 + r * P  ,  cosh(r/2) =  1 + Q . )
6224594Szliu  *
6324594Szliu  *         The coefficients were obtained by a special Remez algorithm.
6424594Szliu  *
6524594Szliu  * Approximation error:
6624594Szliu  *
6724594Szliu  *   |	exp(x) - 1			   |        2**(-57),  (IEEE double)
6824594Szliu  *   | ------------  -  (exp__E(x,0)+x)/x  |  <=
6924594Szliu  *   |	     x			           |	    2**(-69).  (VAX D)
7024594Szliu  *
7124594Szliu  * Constants:
7224594Szliu  * The hexadecimal values are the intended ones for the following constants.
7324594Szliu  * The decimal values may be used, provided that the compiler will convert
7424594Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
7524594Szliu  * shown.
7624594Szliu  */
7724594Szliu 
7831853Szliu #if defined(vax)||defined(tahoe)	/* VAX D format */
7931853Szliu #ifdef vax
8031812Szliu #define _0x(A,B)	0x/**/A/**/B
8131853Szliu #else	/* vax */
8231812Szliu #define _0x(A,B)	0x/**/B/**/A
8331853Szliu #endif	/* vax */
8424594Szliu /* static double */
8524594Szliu /* p1     =  1.5150724356786683059E-2    , Hex  2^ -6   *  .F83ABE67E1066A */
8624594Szliu /* p2     =  6.3112487873718332688E-5    , Hex  2^-13   *  .845B4248CD0173 */
8724594Szliu /* q1     =  1.1363478204690669916E-1    , Hex  2^ -3   *  .E8B95A44A2EC45 */
8824594Szliu /* q2     =  1.2624568129896839182E-3    , Hex  2^ -9   *  .A5790572E4F5E7 */
8924594Szliu /* q3     =  1.5021856115869022674E-6    ; Hex  2^-19   *  .C99EB4604AC395 */
9031812Szliu static long        p1x[] = { _0x(3abe,3d78), _0x(066a,67e1)};
9131812Szliu static long        p2x[] = { _0x(5b42,3984), _0x(0173,48cd)};
9231812Szliu static long        q1x[] = { _0x(b95a,3ee8), _0x(ec45,44a2)};
9331812Szliu static long        q2x[] = { _0x(7905,3ba5), _0x(f5e7,72e4)};
9431812Szliu static long        q3x[] = { _0x(9eb4,36c9), _0x(c395,604a)};
9524594Szliu #define       p1    (*(double*)p1x)
9624594Szliu #define       p2    (*(double*)p2x)
9724594Szliu #define       q1    (*(double*)q1x)
9824594Szliu #define       q2    (*(double*)q2x)
9924594Szliu #define       q3    (*(double*)q3x)
10031853Szliu #else	/* defined(vax)||defined(tahoe)	*/
10124594Szliu static double
10224594Szliu p1     =  1.3887401997267371720E-2    , /*Hex  2^ -7   *  1.C70FF8B3CC2CF */
10324594Szliu p2     =  3.3044019718331897649E-5    , /*Hex  2^-15   *  1.15317DF4526C4 */
10424594Szliu q1     =  1.1110813732786649355E-1    , /*Hex  2^ -4   *  1.C719538248597 */
10524594Szliu q2     =  9.9176615021572857300E-4    ; /*Hex  2^-10   *  1.03FC4CB8C98E8 */
10631853Szliu #endif	/* defined(vax)||defined(tahoe)	*/
10724594Szliu 
10824594Szliu double exp__E(x,c)
10924594Szliu double x,c;
11024594Szliu {
11126893Selefunt 	static double zero=0.0, one=1.0, half=1.0/2.0, small=1.0E-19;
11224594Szliu 	double copysign(),z,p,q,xp,xh,w;
11324594Szliu 	if(copysign(x,one)>small) {
11424594Szliu            z = x*x  ;
11524594Szliu 	   p = z*( p1 +z* p2 );
11631853Szliu #if defined(vax)||defined(tahoe)
11724594Szliu            q = z*( q1 +z*( q2 +z* q3 ));
11831853Szliu #else	/* defined(vax)||defined(tahoe) */
11924594Szliu            q = z*( q1 +z*  q2 );
12031853Szliu #endif	/* defined(vax)||defined(tahoe) */
12124594Szliu            xp= x*p     ;
12224594Szliu 	   xh= x*half  ;
12324594Szliu            w = xh-(q-xp)  ;
12424594Szliu 	   p = p+p;
12524594Szliu 	   c += x*((xh*w-(q-(p+xp)))/(one-w)+c);
12624594Szliu 	   return(z*half+c);
12724594Szliu 	}
12824594Szliu 	/* end of |x| > small */
12924594Szliu 
13024594Szliu 	else {
13124594Szliu 	    if(x!=zero) one+small;	/* raise the inexact flag */
13224594Szliu 	    return(copysign(zero,x));
13324594Szliu 	}
13424594Szliu }
135