1*138486a3Suwe /* $NetBSD: n_exp__E.c,v 1.7 2008/04/29 15:10:02 uwe Exp $ */
225d43745Sragge /*
325d43745Sragge * Copyright (c) 1985, 1993
425d43745Sragge * The Regents of the University of California. All rights reserved.
525d43745Sragge *
625d43745Sragge * Redistribution and use in source and binary forms, with or without
725d43745Sragge * modification, are permitted provided that the following conditions
825d43745Sragge * are met:
925d43745Sragge * 1. Redistributions of source code must retain the above copyright
1025d43745Sragge * notice, this list of conditions and the following disclaimer.
1125d43745Sragge * 2. Redistributions in binary form must reproduce the above copyright
1225d43745Sragge * notice, this list of conditions and the following disclaimer in the
1325d43745Sragge * documentation and/or other materials provided with the distribution.
14eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
1525d43745Sragge * may be used to endorse or promote products derived from this software
1625d43745Sragge * without specific prior written permission.
1725d43745Sragge *
1825d43745Sragge * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1925d43745Sragge * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2025d43745Sragge * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2125d43745Sragge * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2225d43745Sragge * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2325d43745Sragge * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2425d43745Sragge * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2525d43745Sragge * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2625d43745Sragge * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2725d43745Sragge * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2825d43745Sragge * SUCH DAMAGE.
2925d43745Sragge */
3025d43745Sragge
3125d43745Sragge #ifndef lint
3204195f0eSragge #if 0
3325d43745Sragge static char sccsid[] = "@(#)exp__E.c 8.1 (Berkeley) 6/4/93";
3404195f0eSragge #endif
3525d43745Sragge #endif /* not lint */
3625d43745Sragge
3725d43745Sragge /* exp__E(x,c)
3825d43745Sragge * ASSUMPTION: c << x SO THAT fl(x+c)=x.
3925d43745Sragge * (c is the correction term for x)
4025d43745Sragge * exp__E RETURNS
4125d43745Sragge *
4225d43745Sragge * / exp(x+c) - 1 - x , 1E-19 < |x| < .3465736
4325d43745Sragge * exp__E(x,c) = |
4425d43745Sragge * \ 0 , |x| < 1E-19.
4525d43745Sragge *
4625d43745Sragge * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS)
4725d43745Sragge * KERNEL FUNCTION OF EXP, EXPM1, POW FUNCTIONS
4825d43745Sragge * CODED IN C BY K.C. NG, 1/31/85;
4925d43745Sragge * REVISED BY K.C. NG on 3/16/85, 4/16/85.
5025d43745Sragge *
5125d43745Sragge * Required system supported function:
5225d43745Sragge * copysign(x,y)
5325d43745Sragge *
5425d43745Sragge * Method:
5525d43745Sragge * 1. Rational approximation. Let r=x+c.
5625d43745Sragge * Based on
5725d43745Sragge * 2 * sinh(r/2)
5825d43745Sragge * exp(r) - 1 = ---------------------- ,
5925d43745Sragge * cosh(r/2) - sinh(r/2)
6025d43745Sragge * exp__E(r) is computed using
6125d43745Sragge * x*x (x/2)*W - ( Q - ( 2*P + x*P ) )
6225d43745Sragge * --- + (c + x*[---------------------------------- + c ])
6325d43745Sragge * 2 1 - W
6425d43745Sragge * where P := p1*x^2 + p2*x^4,
6525d43745Sragge * Q := q1*x^2 + q2*x^4 (for 56 bits precision, add q3*x^6)
6625d43745Sragge * W := x/2-(Q-x*P),
6725d43745Sragge *
6825d43745Sragge * (See the listing below for the values of p1,p2,q1,q2,q3. The poly-
6925d43745Sragge * nomials P and Q may be regarded as the approximations to sinh
7025d43745Sragge * and cosh :
7125d43745Sragge * sinh(r/2) = r/2 + r * P , cosh(r/2) = 1 + Q . )
7225d43745Sragge *
7325d43745Sragge * The coefficients were obtained by a special Remez algorithm.
7425d43745Sragge *
7525d43745Sragge * Approximation error:
7625d43745Sragge *
7725d43745Sragge * | exp(x) - 1 | 2**(-57), (IEEE double)
7825d43745Sragge * | ------------ - (exp__E(x,0)+x)/x | <=
7925d43745Sragge * | x | 2**(-69). (VAX D)
8025d43745Sragge *
8125d43745Sragge * Constants:
8225d43745Sragge * The hexadecimal values are the intended ones for the following constants.
8325d43745Sragge * The decimal values may be used, provided that the compiler will convert
8425d43745Sragge * from decimal to binary accurately enough to produce the hexadecimal values
8525d43745Sragge * shown.
8625d43745Sragge */
8725d43745Sragge
88f1009ecdSmatt #define _LIBM_STATIC
8925d43745Sragge #include "mathimpl.h"
9025d43745Sragge
9125d43745Sragge vc(p1, 1.5150724356786683059E-2 ,3abe,3d78,066a,67e1, -6, .F83ABE67E1066A)
9225d43745Sragge vc(p2, 6.3112487873718332688E-5 ,5b42,3984,0173,48cd, -13, .845B4248CD0173)
9325d43745Sragge vc(q1, 1.1363478204690669916E-1 ,b95a,3ee8,ec45,44a2, -3, .E8B95A44A2EC45)
9425d43745Sragge vc(q2, 1.2624568129896839182E-3 ,7905,3ba5,f5e7,72e4, -9, .A5790572E4F5E7)
9525d43745Sragge vc(q3, 1.5021856115869022674E-6 ,9eb4,36c9,c395,604a, -19, .C99EB4604AC395)
9625d43745Sragge
9725d43745Sragge ic(p1, 1.3887401997267371720E-2, -7, 1.C70FF8B3CC2CF)
9825d43745Sragge ic(p2, 3.3044019718331897649E-5, -15, 1.15317DF4526C4)
9925d43745Sragge ic(q1, 1.1110813732786649355E-1, -4, 1.C719538248597)
10025d43745Sragge ic(q2, 9.9176615021572857300E-4, -10, 1.03FC4CB8C98E8)
10125d43745Sragge
10225d43745Sragge #ifdef vccast
10325d43745Sragge #define p1 vccast(p1)
10425d43745Sragge #define p2 vccast(p2)
10525d43745Sragge #define q1 vccast(q1)
10625d43745Sragge #define q2 vccast(q2)
10725d43745Sragge #define q3 vccast(q3)
10825d43745Sragge #endif
10925d43745Sragge
110f1009ecdSmatt double
__exp__E(double x,double c)111f1009ecdSmatt __exp__E(double x, double c)
11225d43745Sragge {
113*138486a3Suwe static const double zero=0.0, one=1.0, half=1.0/2.0, small=1.0E-19;
11425d43745Sragge double z,p,q,xp,xh,w;
11525d43745Sragge if(copysign(x,one)>small) {
11625d43745Sragge z = x*x ;
11725d43745Sragge p = z*( p1 +z* p2 );
118c34b7ca1Smatt #if defined(__vax__)||defined(tahoe)
11925d43745Sragge q = z*( q1 +z*( q2 +z* q3 ));
120c34b7ca1Smatt #else /* defined(__vax__)||defined(tahoe) */
12125d43745Sragge q = z*( q1 +z* q2 );
122c34b7ca1Smatt #endif /* defined(__vax__)||defined(tahoe) */
12325d43745Sragge xp= x*p ;
12425d43745Sragge xh= x*half ;
12525d43745Sragge w = xh-(q-xp) ;
12625d43745Sragge p = p+p;
12725d43745Sragge c += x*((xh*w-(q-(p+xp)))/(one-w)+c);
12825d43745Sragge return(z*half+c);
12925d43745Sragge }
13025d43745Sragge /* end of |x| > small */
13125d43745Sragge
13225d43745Sragge else {
13304195f0eSragge if(x!=zero) w=one+small; /* raise the inexact flag ??? -ragge */
13425d43745Sragge return(copysign(zero,x));
13525d43745Sragge }
13625d43745Sragge }
137