Back to index

lightning-sunbird  0.9+nobinonly
e_log.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_log.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_log(x)
00053  * Return the logrithm of x
00054  *
00055  * Method :                  
00056  *   1. Argument Reduction: find k and f such that 
00057  *                   x = 2^k * (1+f), 
00058  *        where  sqrt(2)/2 < 1+f < sqrt(2) .
00059  *
00060  *   2. Approximation of log(1+f).
00061  *     Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
00062  *             = 2s + 2/3 s**3 + 2/5 s**5 + .....,
00063  *             = 2s + s*R
00064  *      We use a special Reme algorithm on [0,0.1716] to generate 
00065  *     a polynomial of degree 14 to approximate R The maximum error 
00066  *     of this polynomial approximation is bounded by 2**-58.45. In
00067  *     other words,
00068  *                    2      4      6      8      10      12      14
00069  *         R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s  +Lg6*s  +Lg7*s
00070  *     (the values of Lg1 to Lg7 are listed in the program)
00071  *     and
00072  *         |      2          14          |     -58.45
00073  *         | Lg1*s +...+Lg7*s    -  R(z) | <= 2 
00074  *         |                             |
00075  *     Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
00076  *     In order to guarantee error in log below 1ulp, we compute log
00077  *     by
00078  *            log(1+f) = f - s*(f - R)    (if f is not too large)
00079  *            log(1+f) = f - (hfsq - s*(hfsq+R)).       (better accuracy)
00080  *     
00081  *     3. Finally,  log(x) = k*ln2 + log(1+f).  
00082  *                       = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
00083  *        Here ln2 is split into two floating point number: 
00084  *                   ln2_hi + ln2_lo,
00085  *        where n*ln2_hi is always exact for |n| < 2000.
00086  *
00087  * Special cases:
00088  *     log(x) is NaN with signal if x < 0 (including -INF) ; 
00089  *     log(+INF) is +INF; log(0) is -INF with signal;
00090  *     log(NaN) is that NaN with no signal.
00091  *
00092  * Accuracy:
00093  *     according to an error analysis, the error is always less than
00094  *     1 ulp (unit in the last place).
00095  *
00096  * Constants:
00097  * The hexadecimal values are the intended ones for the following 
00098  * constants. The decimal values may be used, provided that the 
00099  * compiler will convert from decimal to binary accurately enough 
00100  * to produce the hexadecimal values shown.
00101  */
00102 
00103 #include "fdlibm.h"
00104 
00105 #ifdef __STDC__
00106 static const double
00107 #else
00108 static double
00109 #endif
00110 ln2_hi  =  6.93147180369123816490e-01,    /* 3fe62e42 fee00000 */
00111 ln2_lo  =  1.90821492927058770002e-10,    /* 3dea39ef 35793c76 */
00112 two54   =  1.80143985094819840000e+16,  /* 43500000 00000000 */
00113 Lg1 = 6.666666666666735130e-01,  /* 3FE55555 55555593 */
00114 Lg2 = 3.999999999940941908e-01,  /* 3FD99999 9997FA04 */
00115 Lg3 = 2.857142874366239149e-01,  /* 3FD24924 94229359 */
00116 Lg4 = 2.222219843214978396e-01,  /* 3FCC71C5 1D8E78AF */
00117 Lg5 = 1.818357216161805012e-01,  /* 3FC74664 96CB03DE */
00118 Lg6 = 1.531383769920937332e-01,  /* 3FC39A09 D078C69F */
00119 Lg7 = 1.479819860511658591e-01;  /* 3FC2F112 DF3E5244 */
00120 
00121 static double zero   =  0.0;
00122 
00123 #ifdef __STDC__
00124        double __ieee754_log(double x)
00125 #else
00126        double __ieee754_log(x)
00127        double x;
00128 #endif
00129 {
00130         fd_twoints u;
00131        double hfsq,f,s,z,R,w,t1,t2,dk;
00132        int k,hx,i,j;
00133        unsigned lx;
00134 
00135         u.d = x;
00136        hx = __HI(u);        /* high word of x */
00137        lx = __LO(u);        /* low  word of x */
00138 
00139        k=0;
00140        if (hx < 0x00100000) {                    /* x < 2**-1022  */
00141            if (((hx&0x7fffffff)|lx)==0) 
00142               return -two54/zero;         /* log(+-0)=-inf */
00143            if (hx<0) return (x-x)/zero;   /* log(-#) = NaN */
00144            k -= 54; x *= two54; /* subnormal number, scale up x */
00145             u.d = x;
00146            hx = __HI(u);           /* high word of x */
00147        } 
00148        if (hx >= 0x7ff00000) return x+x;
00149        k += (hx>>20)-1023;
00150        hx &= 0x000fffff;
00151        i = (hx+0x95f64)&0x100000;
00152         u.d = x;
00153        __HI(u) = hx|(i^0x3ff00000);       /* normalize x or x/2 */
00154         x = u.d;
00155        k += (i>>20);
00156        f = x-1.0;
00157        if((0x000fffff&(2+hx))<3) { /* |f| < 2**-20 */
00158            if(f==zero) { 
00159                 if(k==0) return zero; else {dk=(double)k;
00160                                             return dk*ln2_hi+dk*ln2_lo;} 
00161             }
00162            R = f*f*(0.5-0.33333333333333333*f);
00163            if(k==0) return f-R; else {dk=(double)k;
00164                    return dk*ln2_hi-((R-dk*ln2_lo)-f);}
00165        }
00166        s = f/(2.0+f); 
00167        dk = (double)k;
00168        z = s*s;
00169        i = hx-0x6147a;
00170        w = z*z;
00171        j = 0x6b851-hx;
00172        t1= w*(Lg2+w*(Lg4+w*Lg6)); 
00173        t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7))); 
00174        i |= j;
00175        R = t2+t1;
00176        if(i>0) {
00177            hfsq=0.5*f*f;
00178            if(k==0) return f-(hfsq-s*(hfsq+R)); else
00179                    return dk*ln2_hi-((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f);
00180        } else {
00181            if(k==0) return f-s*(f-R); else
00182                    return dk*ln2_hi-((s*(f-R)-dk*ln2_lo)-f);
00183        }
00184 }