Home | History | Annotate | Line # | Download | only in noieee_src
      1  1.7     uwe /*      $NetBSD: n_exp__E.c,v 1.7 2008/04/29 15:10:02 uwe Exp $ */
      2  1.1   ragge /*
      3  1.1   ragge  * Copyright (c) 1985, 1993
      4  1.1   ragge  *	The Regents of the University of California.  All rights reserved.
      5  1.1   ragge  *
      6  1.1   ragge  * Redistribution and use in source and binary forms, with or without
      7  1.1   ragge  * modification, are permitted provided that the following conditions
      8  1.1   ragge  * are met:
      9  1.1   ragge  * 1. Redistributions of source code must retain the above copyright
     10  1.1   ragge  *    notice, this list of conditions and the following disclaimer.
     11  1.1   ragge  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1   ragge  *    notice, this list of conditions and the following disclaimer in the
     13  1.1   ragge  *    documentation and/or other materials provided with the distribution.
     14  1.6     agc  * 3. Neither the name of the University nor the names of its contributors
     15  1.1   ragge  *    may be used to endorse or promote products derived from this software
     16  1.1   ragge  *    without specific prior written permission.
     17  1.1   ragge  *
     18  1.1   ragge  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     19  1.1   ragge  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  1.1   ragge  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  1.1   ragge  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     22  1.1   ragge  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.1   ragge  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  1.1   ragge  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.1   ragge  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.1   ragge  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1   ragge  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1   ragge  * SUCH DAMAGE.
     29  1.1   ragge  */
     30  1.1   ragge 
     31  1.1   ragge #ifndef lint
     32  1.2   ragge #if 0
     33  1.1   ragge static char sccsid[] = "@(#)exp__E.c	8.1 (Berkeley) 6/4/93";
     34  1.2   ragge #endif
     35  1.1   ragge #endif /* not lint */
     36  1.1   ragge 
     37  1.1   ragge /* exp__E(x,c)
     38  1.1   ragge  * ASSUMPTION: c << x  SO THAT  fl(x+c)=x.
     39  1.1   ragge  * (c is the correction term for x)
     40  1.1   ragge  * exp__E RETURNS
     41  1.1   ragge  *
     42  1.1   ragge  *			 /  exp(x+c) - 1 - x ,  1E-19 < |x| < .3465736
     43  1.4  simonb  *       exp__E(x,c) = 	|
     44  1.1   ragge  *			 \  0 ,  |x| < 1E-19.
     45  1.1   ragge  *
     46  1.1   ragge  * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS)
     47  1.1   ragge  * KERNEL FUNCTION OF EXP, EXPM1, POW FUNCTIONS
     48  1.1   ragge  * CODED IN C BY K.C. NG, 1/31/85;
     49  1.1   ragge  * REVISED BY K.C. NG on 3/16/85, 4/16/85.
     50  1.1   ragge  *
     51  1.1   ragge  * Required system supported function:
     52  1.4  simonb  *	copysign(x,y)
     53  1.1   ragge  *
     54  1.1   ragge  * Method:
     55  1.1   ragge  *	1. Rational approximation. Let r=x+c.
     56  1.1   ragge  *	   Based on
     57  1.4  simonb  *                                   2 * sinh(r/2)
     58  1.1   ragge  *                exp(r) - 1 =   ----------------------   ,
     59  1.1   ragge  *                               cosh(r/2) - sinh(r/2)
     60  1.1   ragge  *	   exp__E(r) is computed using
     61  1.1   ragge  *                   x*x            (x/2)*W - ( Q - ( 2*P  + x*P ) )
     62  1.1   ragge  *                   --- + (c + x*[---------------------------------- + c ])
     63  1.1   ragge  *                    2                          1 - W
     64  1.1   ragge  * 	   where  P := p1*x^2 + p2*x^4,
     65  1.1   ragge  *	          Q := q1*x^2 + q2*x^4 (for 56 bits precision, add q3*x^6)
     66  1.1   ragge  *	          W := x/2-(Q-x*P),
     67  1.1   ragge  *
     68  1.1   ragge  *	   (See the listing below for the values of p1,p2,q1,q2,q3. The poly-
     69  1.1   ragge  *	    nomials P and Q may be regarded as the approximations to sinh
     70  1.1   ragge  *	    and cosh :
     71  1.1   ragge  *		sinh(r/2) =  r/2 + r * P  ,  cosh(r/2) =  1 + Q . )
     72  1.1   ragge  *
     73  1.1   ragge  *         The coefficients were obtained by a special Remez algorithm.
     74  1.1   ragge  *
     75  1.1   ragge  * Approximation error:
     76  1.1   ragge  *
     77  1.1   ragge  *   |	exp(x) - 1			   |        2**(-57),  (IEEE double)
     78  1.4  simonb  *   | ------------  -  (exp__E(x,0)+x)/x  |  <=
     79  1.1   ragge  *   |	     x			           |	    2**(-69).  (VAX D)
     80  1.1   ragge  *
     81  1.1   ragge  * Constants:
     82  1.1   ragge  * The hexadecimal values are the intended ones for the following constants.
     83  1.1   ragge  * The decimal values may be used, provided that the compiler will convert
     84  1.1   ragge  * from decimal to binary accurately enough to produce the hexadecimal values
     85  1.1   ragge  * shown.
     86  1.1   ragge  */
     87  1.1   ragge 
     88  1.5    matt #define _LIBM_STATIC
     89  1.1   ragge #include "mathimpl.h"
     90  1.1   ragge 
     91  1.1   ragge vc(p1, 1.5150724356786683059E-2 ,3abe,3d78,066a,67e1,  -6, .F83ABE67E1066A)
     92  1.1   ragge vc(p2, 6.3112487873718332688E-5 ,5b42,3984,0173,48cd, -13, .845B4248CD0173)
     93  1.1   ragge vc(q1, 1.1363478204690669916E-1 ,b95a,3ee8,ec45,44a2,  -3, .E8B95A44A2EC45)
     94  1.1   ragge vc(q2, 1.2624568129896839182E-3 ,7905,3ba5,f5e7,72e4,  -9, .A5790572E4F5E7)
     95  1.1   ragge vc(q3, 1.5021856115869022674E-6 ,9eb4,36c9,c395,604a, -19, .C99EB4604AC395)
     96  1.1   ragge 
     97  1.1   ragge ic(p1, 1.3887401997267371720E-2,  -7, 1.C70FF8B3CC2CF)
     98  1.1   ragge ic(p2, 3.3044019718331897649E-5, -15, 1.15317DF4526C4)
     99  1.1   ragge ic(q1, 1.1110813732786649355E-1,  -4, 1.C719538248597)
    100  1.1   ragge ic(q2, 9.9176615021572857300E-4, -10, 1.03FC4CB8C98E8)
    101  1.1   ragge 
    102  1.1   ragge #ifdef vccast
    103  1.1   ragge #define       p1    vccast(p1)
    104  1.1   ragge #define       p2    vccast(p2)
    105  1.1   ragge #define       q1    vccast(q1)
    106  1.1   ragge #define       q2    vccast(q2)
    107  1.1   ragge #define       q3    vccast(q3)
    108  1.1   ragge #endif
    109  1.1   ragge 
    110  1.5    matt double
    111  1.5    matt __exp__E(double x, double c)
    112  1.1   ragge {
    113  1.7     uwe 	static const double zero=0.0, one=1.0, half=1.0/2.0, small=1.0E-19;
    114  1.1   ragge 	double z,p,q,xp,xh,w;
    115  1.1   ragge 	if(copysign(x,one)>small) {
    116  1.1   ragge            z = x*x  ;
    117  1.1   ragge 	   p = z*( p1 +z* p2 );
    118  1.3    matt #if defined(__vax__)||defined(tahoe)
    119  1.1   ragge            q = z*( q1 +z*( q2 +z* q3 ));
    120  1.3    matt #else	/* defined(__vax__)||defined(tahoe) */
    121  1.1   ragge            q = z*( q1 +z*  q2 );
    122  1.3    matt #endif	/* defined(__vax__)||defined(tahoe) */
    123  1.4  simonb            xp= x*p     ;
    124  1.1   ragge 	   xh= x*half  ;
    125  1.1   ragge            w = xh-(q-xp)  ;
    126  1.1   ragge 	   p = p+p;
    127  1.1   ragge 	   c += x*((xh*w-(q-(p+xp)))/(one-w)+c);
    128  1.1   ragge 	   return(z*half+c);
    129  1.1   ragge 	}
    130  1.1   ragge 	/* end of |x| > small */
    131  1.1   ragge 
    132  1.1   ragge 	else {
    133  1.2   ragge 	    if(x!=zero) w=one+small;	/* raise the inexact flag ??? -ragge */
    134  1.1   ragge 	    return(copysign(zero,x));
    135  1.1   ragge 	}
    136  1.1   ragge }
    137