Back to index

glibc  2.9
s_log1p.c
Go to the documentation of this file.
00001 /* @(#)s_log1p.c 5.1 93/09/24 */
00002 /*
00003  * ====================================================
00004  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00005  *
00006  * Developed at SunPro, a Sun Microsystems, Inc. business.
00007  * Permission to use, copy, modify, and distribute this
00008  * software is freely granted, provided that this notice
00009  * is preserved.
00010  * ====================================================
00011  */
00012 /* Modified by Naohiko Shimizu/Tokai University, Japan 1997/08/25,
00013    for performance improvement on pipelined processors.
00014 */
00015 
00016 #if defined(LIBM_SCCS) && !defined(lint)
00017 static char rcsid[] = "$NetBSD: s_log1p.c,v 1.8 1995/05/10 20:47:46 jtc Exp $";
00018 #endif
00019 
00020 /* double log1p(double x)
00021  *
00022  * Method :
00023  *   1. Argument Reduction: find k and f such that
00024  *                   1+x = 2^k * (1+f),
00025  *        where  sqrt(2)/2 < 1+f < sqrt(2) .
00026  *
00027  *      Note. If k=0, then f=x is exact. However, if k!=0, then f
00028  *     may not be representable exactly. In that case, a correction
00029  *     term is need. Let u=1+x rounded. Let c = (1+x)-u, then
00030  *     log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
00031  *     and add back the correction term c/u.
00032  *     (Note: when x > 2**53, one can simply return log(x))
00033  *
00034  *   2. Approximation of log1p(f).
00035  *     Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
00036  *             = 2s + 2/3 s**3 + 2/5 s**5 + .....,
00037  *             = 2s + s*R
00038  *      We use a special Reme algorithm on [0,0.1716] to generate
00039  *     a polynomial of degree 14 to approximate R The maximum error
00040  *     of this polynomial approximation is bounded by 2**-58.45. In
00041  *     other words,
00042  *                    2      4      6      8      10      12      14
00043  *         R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s  +Lp6*s  +Lp7*s
00044  *     (the values of Lp1 to Lp7 are listed in the program)
00045  *     and
00046  *         |      2          14          |     -58.45
00047  *         | Lp1*s +...+Lp7*s    -  R(z) | <= 2
00048  *         |                             |
00049  *     Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
00050  *     In order to guarantee error in log below 1ulp, we compute log
00051  *     by
00052  *            log1p(f) = f - (hfsq - s*(hfsq+R)).
00053  *
00054  *     3. Finally, log1p(x) = k*ln2 + log1p(f).
00055  *                        = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
00056  *        Here ln2 is split into two floating point number:
00057  *                   ln2_hi + ln2_lo,
00058  *        where n*ln2_hi is always exact for |n| < 2000.
00059  *
00060  * Special cases:
00061  *     log1p(x) is NaN with signal if x < -1 (including -INF) ;
00062  *     log1p(+INF) is +INF; log1p(-1) is -INF with signal;
00063  *     log1p(NaN) is that NaN with no signal.
00064  *
00065  * Accuracy:
00066  *     according to an error analysis, the error is always less than
00067  *     1 ulp (unit in the last place).
00068  *
00069  * Constants:
00070  * The hexadecimal values are the intended ones for the following
00071  * constants. The decimal values may be used, provided that the
00072  * compiler will convert from decimal to binary accurately enough
00073  * to produce the hexadecimal values shown.
00074  *
00075  * Note: Assuming log() return accurate answer, the following
00076  *      algorithm can be used to compute log1p(x) to within a few ULP:
00077  *
00078  *            u = 1+x;
00079  *            if(u==1.0) return x ; else
00080  *                      return log(u)*(x/(u-1.0));
00081  *
00082  *      See HP-15C Advanced Functions Handbook, p.193.
00083  */
00084 
00085 #include "math.h"
00086 #include "math_private.h"
00087 
00088 #ifdef __STDC__
00089 static const double
00090 #else
00091 static double
00092 #endif
00093 ln2_hi  =  6.93147180369123816490e-01,    /* 3fe62e42 fee00000 */
00094 ln2_lo  =  1.90821492927058770002e-10,    /* 3dea39ef 35793c76 */
00095 two54   =  1.80143985094819840000e+16,  /* 43500000 00000000 */
00096 Lp[] = {0.0, 6.666666666666735130e-01,  /* 3FE55555 55555593 */
00097  3.999999999940941908e-01,  /* 3FD99999 9997FA04 */
00098  2.857142874366239149e-01,  /* 3FD24924 94229359 */
00099  2.222219843214978396e-01,  /* 3FCC71C5 1D8E78AF */
00100  1.818357216161805012e-01,  /* 3FC74664 96CB03DE */
00101  1.531383769920937332e-01,  /* 3FC39A09 D078C69F */
00102  1.479819860511658591e-01};  /* 3FC2F112 DF3E5244 */
00103 
00104 #ifdef __STDC__
00105 static const double zero = 0.0;
00106 #else
00107 static double zero = 0.0;
00108 #endif
00109 
00110 #ifdef __STDC__
00111        double __log1p(double x)
00112 #else
00113        double __log1p(x)
00114        double x;
00115 #endif
00116 {
00117        double hfsq,f,c,s,z,R,u,z2,z4,z6,R1,R2,R3,R4;
00118        int32_t k,hx,hu,ax;
00119 
00120        GET_HIGH_WORD(hx,x);
00121        ax = hx&0x7fffffff;
00122 
00123        k = 1;
00124        if (hx < 0x3FDA827A) {                    /* x < 0.41422  */
00125            if(ax>=0x3ff00000) {           /* x <= -1.0 */
00126               if(x==-1.0) return -two54/(x-x);/* log1p(-1)=+inf */
00127               else return (x-x)/(x-x);    /* log1p(x<-1)=NaN */
00128            }
00129            if(ax<0x3e200000) {                   /* |x| < 2**-29 */
00130               if(two54+x>zero                    /* raise inexact */
00131                    &&ax<0x3c900000)              /* |x| < 2**-54 */
00132                   return x;
00133               else
00134                   return x - x*x*0.5;
00135            }
00136            if(hx>0||hx<=((int32_t)0xbfd2bec3)) {
00137               k=0;f=x;hu=1;}       /* -0.2929<x<0.41422 */
00138        }
00139        if (hx >= 0x7ff00000) return x+x;
00140        if(k!=0) {
00141            if(hx<0x43400000) {
00142               u  = 1.0+x;
00143               GET_HIGH_WORD(hu,u);
00144                k  = (hu>>20)-1023;
00145                c  = (k>0)? 1.0-(u-x):x-(u-1.0);/* correction term */
00146               c /= u;
00147            } else {
00148               u  = x;
00149               GET_HIGH_WORD(hu,u);
00150                k  = (hu>>20)-1023;
00151               c  = 0;
00152            }
00153            hu &= 0x000fffff;
00154            if(hu<0x6a09e) {
00155                SET_HIGH_WORD(u,hu|0x3ff00000);   /* normalize u */
00156            } else {
00157                k += 1;
00158               SET_HIGH_WORD(u,hu|0x3fe00000);    /* normalize u/2 */
00159                hu = (0x00100000-hu)>>2;
00160            }
00161            f = u-1.0;
00162        }
00163        hfsq=0.5*f*f;
00164        if(hu==0) {   /* |f| < 2**-20 */
00165            if(f==zero) {
00166              if(k==0) return zero;
00167                      else {c += k*ln2_lo; return k*ln2_hi+c;}
00168            }
00169            R = hfsq*(1.0-0.66666666666666666*f);
00170            if(k==0) return f-R; else
00171                    return k*ln2_hi-((R-(k*ln2_lo+c))-f);
00172        }
00173        s = f/(2.0+f);
00174        z = s*s;
00175 #ifdef DO_NOT_USE_THIS
00176        R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7))))));
00177 #else
00178        R1 = z*Lp[1]; z2=z*z;
00179        R2 = Lp[2]+z*Lp[3]; z4=z2*z2;
00180        R3 = Lp[4]+z*Lp[5]; z6=z4*z2;
00181        R4 = Lp[6]+z*Lp[7];
00182        R = R1 + z2*R2 + z4*R3 + z6*R4;
00183 #endif
00184        if(k==0) return f-(hfsq-s*(hfsq+R)); else
00185                return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f);
00186 }
00187 weak_alias (__log1p, log1p)
00188 #ifdef NO_LONG_DOUBLE
00189 strong_alias (__log1p, __log1pl)
00190 weak_alias (__log1p, log1pl)
00191 #endif