Back to index

lightning-sunbird  0.9+nobinonly
e_exp.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Sun Microsystems, Inc.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /* @(#)e_exp.c 1.3 95/01/18 */
00041 /*
00042  * ====================================================
00043  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00044  *
00045  * Developed at SunSoft, a Sun Microsystems, Inc. business.
00046  * Permission to use, copy, modify, and distribute this
00047  * software is freely granted, provided that this notice 
00048  * is preserved.
00049  * ====================================================
00050  */
00051 
00052 /* __ieee754_exp(x)
00053  * Returns the exponential of x.
00054  *
00055  * Method
00056  *   1. Argument reduction:
00057  *      Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
00058  *     Given x, find r and integer k such that
00059  *
00060  *               x = k*ln2 + r,  |r| <= 0.5*ln2.  
00061  *
00062  *      Here r will be represented as r = hi-lo for better 
00063  *     accuracy.
00064  *
00065  *   2. Approximation of exp(r) by a special rational function on
00066  *     the interval [0,0.34658]:
00067  *     Write
00068  *         R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
00069  *      We use a special Reme algorithm on [0,0.34658] to generate 
00070  *     a polynomial of degree 5 to approximate R. The maximum error 
00071  *     of this polynomial approximation is bounded by 2**-59. In
00072  *     other words,
00073  *         R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
00074  *     (where z=r*r, and the values of P1 to P5 are listed below)
00075  *     and
00076  *         |                  5          |     -59
00077  *         | 2.0+P1*z+...+P5*z   -  R(z) | <= 2 
00078  *         |                             |
00079  *     The computation of exp(r) thus becomes
00080  *                             2*r
00081  *            exp(r) = 1 + -------
00082  *                          R - r
00083  *                                 r*R1(r)       
00084  *                   = 1 + r + ----------- (for better accuracy)
00085  *                              2 - R1(r)
00086  *     where
00087  *                            2       4             10
00088  *            R1(r) = r - (P1*r  + P2*r  + ... + P5*r   ).
00089  *     
00090  *   3. Scale back to obtain exp(x):
00091  *     From step 1, we have
00092  *        exp(x) = 2^k * exp(r)
00093  *
00094  * Special cases:
00095  *     exp(INF) is INF, exp(NaN) is NaN;
00096  *     exp(-INF) is 0, and
00097  *     for finite argument, only exp(0)=1 is exact.
00098  *
00099  * Accuracy:
00100  *     according to an error analysis, the error is always less than
00101  *     1 ulp (unit in the last place).
00102  *
00103  * Misc. info.
00104  *     For IEEE double 
00105  *         if x >  7.09782712893383973096e+02 then exp(x) overflow
00106  *         if x < -7.45133219101941108420e+02 then exp(x) underflow
00107  *
00108  * Constants:
00109  * The hexadecimal values are the intended ones for the following 
00110  * constants. The decimal values may be used, provided that the 
00111  * compiler will convert from decimal to binary accurately enough
00112  * to produce the hexadecimal values shown.
00113  */
00114 
00115 #include "fdlibm.h"
00116 
00117 #ifdef __STDC__
00118 static const double
00119 #else
00120 static double
00121 #endif
00122 one    = 1.0,
00123 halF[2]       = {0.5,-0.5,},
00124 really_big    = 1.0e+300,
00125 twom1000= 9.33263618503218878990e-302,     /* 2**-1000=0x01700000,0*/
00126 o_threshold=  7.09782712893383973096e+02,  /* 0x40862E42, 0xFEFA39EF */
00127 u_threshold= -7.45133219101941108420e+02,  /* 0xc0874910, 0xD52D3051 */
00128 ln2HI[2]   ={ 6.93147180369123816490e-01,  /* 0x3fe62e42, 0xfee00000 */
00129             -6.93147180369123816490e-01,},/* 0xbfe62e42, 0xfee00000 */
00130 ln2LO[2]   ={ 1.90821492927058770002e-10,  /* 0x3dea39ef, 0x35793c76 */
00131             -1.90821492927058770002e-10,},/* 0xbdea39ef, 0x35793c76 */
00132 invln2 =  1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */
00133 P1   =  1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */
00134 P2   = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */
00135 P3   =  6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */
00136 P4   = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */
00137 P5   =  4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */
00138 
00139 
00140 #ifdef __STDC__
00141        double __ieee754_exp(double x)     /* default IEEE double exp */
00142 #else
00143        double __ieee754_exp(x)     /* default IEEE double exp */
00144        double x;
00145 #endif
00146 {
00147         fd_twoints u;
00148        double y,hi,lo,c,t;
00149        int k, xsb;
00150        unsigned hx;
00151 
00152         u.d = x;
00153        hx  = __HI(u);       /* high word of x */
00154        xsb = (hx>>31)&1;           /* sign bit of x */
00155        hx &= 0x7fffffff;           /* high word of |x| */
00156 
00157     /* filter out non-finite argument */
00158        if(hx >= 0x40862E42) {                    /* if |x|>=709.78... */
00159             if(hx>=0x7ff00000) {
00160                 u.d = x;
00161               if(((hx&0xfffff)|__LO(u))!=0)
00162                    return x+x;            /* NaN */
00163               else return (xsb==0)? x:0.0;       /* exp(+-inf)={inf,0} */
00164            }
00165            if(x > o_threshold) return really_big*really_big; /* overflow */
00166            if(x < u_threshold) return twom1000*twom1000; /* underflow */
00167        }
00168 
00169     /* argument reduction */
00170        if(hx > 0x3fd62e42) {              /* if  |x| > 0.5 ln2 */ 
00171            if(hx < 0x3FF0A2B2) {   /* and |x| < 1.5 ln2 */
00172               hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;
00173            } else {
00174               k  = (int)(invln2*x+halF[xsb]);
00175               t  = k;
00176               hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */
00177               lo = t*ln2LO[0];
00178            }
00179            x  = hi - lo;
00180        } 
00181        else if(hx < 0x3e300000)  { /* when |x|<2**-28 */
00182            if(really_big+x>one) return one+x;/* trigger inexact */
00183        }
00184        else k = 0;
00185 
00186     /* x is now in primary range */
00187        t  = x*x;
00188        c  = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
00189        if(k==0)      return one-((x*c)/(c-2.0)-x); 
00190        else          y = one-((lo-(x*c)/(2.0-c))-hi);
00191        if(k >= -1021) {
00192             u.d = y;
00193            __HI(u) += (k<<20);     /* add k to y's exponent */
00194             y = u.d;
00195            return y;
00196        } else {
00197             u.d = y;
00198            __HI(u) += ((k+1000)<<20);/* add k to y's exponent */
00199             y = u.d;
00200            return y*twom1000;
00201        }
00202 }