Back to index

lightning-sunbird  0.9+nobinonly
s_log1p.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 /* @(#)s_log1p.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 /* double log1p(double x)
00053  *
00054  * Method :                  
00055  *   1. Argument Reduction: find k and f such that 
00056  *                   1+x = 2^k * (1+f), 
00057  *        where  sqrt(2)/2 < 1+f < sqrt(2) .
00058  *
00059  *      Note. If k=0, then f=x is exact. However, if k!=0, then f
00060  *     may not be representable exactly. In that case, a correction
00061  *     term is need. Let u=1+x rounded. Let c = (1+x)-u, then
00062  *     log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
00063  *     and add back the correction term c/u.
00064  *     (Note: when x > 2**53, one can simply return log(x))
00065  *
00066  *   2. Approximation of log1p(f).
00067  *     Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
00068  *             = 2s + 2/3 s**3 + 2/5 s**5 + .....,
00069  *             = 2s + s*R
00070  *      We use a special Reme algorithm on [0,0.1716] to generate 
00071  *     a polynomial of degree 14 to approximate R The maximum error 
00072  *     of this polynomial approximation is bounded by 2**-58.45. In
00073  *     other words,
00074  *                    2      4      6      8      10      12      14
00075  *         R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s  +Lp6*s  +Lp7*s
00076  *     (the values of Lp1 to Lp7 are listed in the program)
00077  *     and
00078  *         |      2          14          |     -58.45
00079  *         | Lp1*s +...+Lp7*s    -  R(z) | <= 2 
00080  *         |                             |
00081  *     Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
00082  *     In order to guarantee error in log below 1ulp, we compute log
00083  *     by
00084  *            log1p(f) = f - (hfsq - s*(hfsq+R)).
00085  *     
00086  *     3. Finally, log1p(x) = k*ln2 + log1p(f).  
00087  *                        = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
00088  *        Here ln2 is split into two floating point number: 
00089  *                   ln2_hi + ln2_lo,
00090  *        where n*ln2_hi is always exact for |n| < 2000.
00091  *
00092  * Special cases:
00093  *     log1p(x) is NaN with signal if x < -1 (including -INF) ; 
00094  *     log1p(+INF) is +INF; log1p(-1) is -INF with signal;
00095  *     log1p(NaN) is that NaN with no signal.
00096  *
00097  * Accuracy:
00098  *     according to an error analysis, the error is always less than
00099  *     1 ulp (unit in the last place).
00100  *
00101  * Constants:
00102  * The hexadecimal values are the intended ones for the following 
00103  * constants. The decimal values may be used, provided that the 
00104  * compiler will convert from decimal to binary accurately enough 
00105  * to produce the hexadecimal values shown.
00106  *
00107  * Note: Assuming log() return accurate answer, the following
00108  *      algorithm can be used to compute log1p(x) to within a few ULP:
00109  *     
00110  *            u = 1+x;
00111  *            if(u==1.0) return x ; else
00112  *                      return log(u)*(x/(u-1.0));
00113  *
00114  *      See HP-15C Advanced Functions Handbook, p.193.
00115  */
00116 
00117 #include "fdlibm.h"
00118 
00119 #ifdef __STDC__
00120 static const double
00121 #else
00122 static double
00123 #endif
00124 ln2_hi  =  6.93147180369123816490e-01,    /* 3fe62e42 fee00000 */
00125 ln2_lo  =  1.90821492927058770002e-10,    /* 3dea39ef 35793c76 */
00126 two54   =  1.80143985094819840000e+16,  /* 43500000 00000000 */
00127 Lp1 = 6.666666666666735130e-01,  /* 3FE55555 55555593 */
00128 Lp2 = 3.999999999940941908e-01,  /* 3FD99999 9997FA04 */
00129 Lp3 = 2.857142874366239149e-01,  /* 3FD24924 94229359 */
00130 Lp4 = 2.222219843214978396e-01,  /* 3FCC71C5 1D8E78AF */
00131 Lp5 = 1.818357216161805012e-01,  /* 3FC74664 96CB03DE */
00132 Lp6 = 1.531383769920937332e-01,  /* 3FC39A09 D078C69F */
00133 Lp7 = 1.479819860511658591e-01;  /* 3FC2F112 DF3E5244 */
00134 
00135 static double zero = 0.0;
00136 
00137 #ifdef __STDC__
00138        double fd_log1p(double x)
00139 #else
00140        double fd_log1p(x)
00141        double x;
00142 #endif
00143 {
00144        double hfsq,f,c,s,z,R,u;
00145        int k,hx,hu,ax;
00146         fd_twoints un;
00147 
00148         un.d = x;
00149        hx = __HI(un);              /* high word of x */
00150        ax = hx&0x7fffffff;
00151 
00152        k = 1;
00153        if (hx < 0x3FDA827A) {                    /* x < 0.41422  */
00154            if(ax>=0x3ff00000) {           /* x <= -1.0 */
00155               if(x==-1.0) return -two54/zero; /* log1p(-1)=+inf */
00156               else return (x-x)/(x-x);    /* log1p(x<-1)=NaN */
00157            }
00158            if(ax<0x3e200000) {                   /* |x| < 2**-29 */
00159               if(two54+x>zero                    /* raise inexact */
00160                    &&ax<0x3c900000)              /* |x| < 2**-54 */
00161                   return x;
00162               else
00163                   return x - x*x*0.5;
00164            }
00165            if(hx>0||hx<=((int)0xbfd2bec3)) {
00166               k=0;f=x;hu=1;}       /* -0.2929<x<0.41422 */
00167        } 
00168        if (hx >= 0x7ff00000) return x+x;
00169        if(k!=0) {
00170            if(hx<0x43400000) {
00171               u  = 1.0+x; 
00172                 un.d = u;
00173                hu = __HI(un);             /* high word of u */
00174                k  = (hu>>20)-1023;
00175                c  = (k>0)? 1.0-(u-x):x-(u-1.0);/* correction term */
00176               c /= u;
00177            } else {
00178               u  = x;
00179                 un.d = u;
00180                hu = __HI(un);             /* high word of u */
00181                k  = (hu>>20)-1023;
00182               c  = 0;
00183            }
00184            hu &= 0x000fffff;
00185            if(hu<0x6a09e) {
00186                 un.d = u;
00187                __HI(un) = hu|0x3ff00000;  /* normalize u */
00188                 u = un.d;
00189            } else {
00190                k += 1; 
00191                 un.d = u;
00192                __HI(un) = hu|0x3fe00000;  /* normalize u/2 */
00193                 u = un.d;
00194                hu = (0x00100000-hu)>>2;
00195            }
00196            f = u-1.0;
00197        }
00198        hfsq=0.5*f*f;
00199        if(hu==0) {   /* |f| < 2**-20 */
00200            if(f==zero) if(k==0) return zero;  
00201                      else {c += k*ln2_lo; return k*ln2_hi+c;}
00202            R = hfsq*(1.0-0.66666666666666666*f);
00203            if(k==0) return f-R; else
00204                    return k*ln2_hi-((R-(k*ln2_lo+c))-f);
00205        }
00206        s = f/(2.0+f); 
00207        z = s*s;
00208        R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7))))));
00209        if(k==0) return f-(hfsq-s*(hfsq+R)); else
00210                return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f);
00211 }