src/libm/e_log.c
changeset 2756 a98604b691c8
child 3162 dc1eb82ffdaa
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/libm/e_log.c	Mon Sep 15 06:33:23 2008 +0000
     1.3 @@ -0,0 +1,166 @@
     1.4 +/* @(#)e_log.c 5.1 93/09/24 */
     1.5 +/*
     1.6 + * ====================================================
     1.7 + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
     1.8 + *
     1.9 + * Developed at SunPro, a Sun Microsystems, Inc. business.
    1.10 + * Permission to use, copy, modify, and distribute this
    1.11 + * software is freely granted, provided that this notice
    1.12 + * is preserved.
    1.13 + * ====================================================
    1.14 + */
    1.15 +
    1.16 +#if defined(LIBM_SCCS) && !defined(lint)
    1.17 +static char rcsid[] = "$NetBSD: e_log.c,v 1.8 1995/05/10 20:45:49 jtc Exp $";
    1.18 +#endif
    1.19 +
    1.20 +/* __ieee754_log(x)
    1.21 + * Return the logrithm of x
    1.22 + *
    1.23 + * Method :
    1.24 + *   1. Argument Reduction: find k and f such that
    1.25 + *			x = 2^k * (1+f),
    1.26 + *	   where  sqrt(2)/2 < 1+f < sqrt(2) .
    1.27 + *
    1.28 + *   2. Approximation of log(1+f).
    1.29 + *	Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
    1.30 + *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
    1.31 + *	     	 = 2s + s*R
    1.32 + *      We use a special Reme algorithm on [0,0.1716] to generate
    1.33 + * 	a polynomial of degree 14 to approximate R The maximum error
    1.34 + *	of this polynomial approximation is bounded by 2**-58.45. In
    1.35 + *	other words,
    1.36 + *		        2      4      6      8      10      12      14
    1.37 + *	    R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s  +Lg6*s  +Lg7*s
    1.38 + *  	(the values of Lg1 to Lg7 are listed in the program)
    1.39 + *	and
    1.40 + *	    |      2          14          |     -58.45
    1.41 + *	    | Lg1*s +...+Lg7*s    -  R(z) | <= 2
    1.42 + *	    |                             |
    1.43 + *	Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
    1.44 + *	In order to guarantee error in log below 1ulp, we compute log
    1.45 + *	by
    1.46 + *		log(1+f) = f - s*(f - R)	(if f is not too large)
    1.47 + *		log(1+f) = f - (hfsq - s*(hfsq+R)).	(better accuracy)
    1.48 + *
    1.49 + *	3. Finally,  log(x) = k*ln2 + log(1+f).
    1.50 + *			    = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
    1.51 + *	   Here ln2 is split into two floating point number:
    1.52 + *			ln2_hi + ln2_lo,
    1.53 + *	   where n*ln2_hi is always exact for |n| < 2000.
    1.54 + *
    1.55 + * Special cases:
    1.56 + *	log(x) is NaN with signal if x < 0 (including -INF) ;
    1.57 + *	log(+INF) is +INF; log(0) is -INF with signal;
    1.58 + *	log(NaN) is that NaN with no signal.
    1.59 + *
    1.60 + * Accuracy:
    1.61 + *	according to an error analysis, the error is always less than
    1.62 + *	1 ulp (unit in the last place).
    1.63 + *
    1.64 + * Constants:
    1.65 + * The hexadecimal values are the intended ones for the following
    1.66 + * constants. The decimal values may be used, provided that the
    1.67 + * compiler will convert from decimal to binary accurately enough
    1.68 + * to produce the hexadecimal values shown.
    1.69 + */
    1.70 +
    1.71 +#include "math.h"
    1.72 +#include "math_private.h"
    1.73 +
    1.74 +#ifdef __STDC__
    1.75 +static const double
    1.76 +#else
    1.77 +static double
    1.78 +#endif
    1.79 +  ln2_hi = 6.93147180369123816490e-01,  /* 3fe62e42 fee00000 */
    1.80 +    ln2_lo = 1.90821492927058770002e-10,        /* 3dea39ef 35793c76 */
    1.81 +    two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */
    1.82 +    Lg1 = 6.666666666666735130e-01,     /* 3FE55555 55555593 */
    1.83 +    Lg2 = 3.999999999940941908e-01,     /* 3FD99999 9997FA04 */
    1.84 +    Lg3 = 2.857142874366239149e-01,     /* 3FD24924 94229359 */
    1.85 +    Lg4 = 2.222219843214978396e-01,     /* 3FCC71C5 1D8E78AF */
    1.86 +    Lg5 = 1.818357216161805012e-01,     /* 3FC74664 96CB03DE */
    1.87 +    Lg6 = 1.531383769920937332e-01,     /* 3FC39A09 D078C69F */
    1.88 +    Lg7 = 1.479819860511658591e-01;     /* 3FC2F112 DF3E5244 */
    1.89 +
    1.90 +#ifdef __STDC__
    1.91 +static const double zero = 0.0;
    1.92 +#else
    1.93 +static double zero = 0.0;
    1.94 +#endif
    1.95 +
    1.96 +#ifdef __STDC__
    1.97 +double attribute_hidden
    1.98 +__ieee754_log(double x)
    1.99 +#else
   1.100 +double attribute_hidden
   1.101 +__ieee754_log(x)
   1.102 +     double x;
   1.103 +#endif
   1.104 +{
   1.105 +    double hfsq, f, s, z, R, w, t1, t2, dk;
   1.106 +    int32_t k, hx, i, j;
   1.107 +    u_int32_t lx;
   1.108 +
   1.109 +    EXTRACT_WORDS(hx, lx, x);
   1.110 +
   1.111 +    k = 0;
   1.112 +    if (hx < 0x00100000) {      /* x < 2**-1022  */
   1.113 +        if (((hx & 0x7fffffff) | lx) == 0)
   1.114 +            return -two54 / zero;       /* log(+-0)=-inf */
   1.115 +        if (hx < 0)
   1.116 +            return (x - x) / zero;      /* log(-#) = NaN */
   1.117 +        k -= 54;
   1.118 +        x *= two54;             /* subnormal number, scale up x */
   1.119 +        GET_HIGH_WORD(hx, x);
   1.120 +    }
   1.121 +    if (hx >= 0x7ff00000)
   1.122 +        return x + x;
   1.123 +    k += (hx >> 20) - 1023;
   1.124 +    hx &= 0x000fffff;
   1.125 +    i = (hx + 0x95f64) & 0x100000;
   1.126 +    SET_HIGH_WORD(x, hx | (i ^ 0x3ff00000));    /* normalize x or x/2 */
   1.127 +    k += (i >> 20);
   1.128 +    f = x - 1.0;
   1.129 +    if ((0x000fffff & (2 + hx)) < 3) {  /* |f| < 2**-20 */
   1.130 +        if (f == zero) {
   1.131 +            if (k == 0)
   1.132 +                return zero;
   1.133 +            else {
   1.134 +                dk = (double) k;
   1.135 +                return dk * ln2_hi + dk * ln2_lo;
   1.136 +            }
   1.137 +        }
   1.138 +        R = f * f * (0.5 - 0.33333333333333333 * f);
   1.139 +        if (k == 0)
   1.140 +            return f - R;
   1.141 +        else {
   1.142 +            dk = (double) k;
   1.143 +            return dk * ln2_hi - ((R - dk * ln2_lo) - f);
   1.144 +        }
   1.145 +    }
   1.146 +    s = f / (2.0 + f);
   1.147 +    dk = (double) k;
   1.148 +    z = s * s;
   1.149 +    i = hx - 0x6147a;
   1.150 +    w = z * z;
   1.151 +    j = 0x6b851 - hx;
   1.152 +    t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
   1.153 +    t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
   1.154 +    i |= j;
   1.155 +    R = t2 + t1;
   1.156 +    if (i > 0) {
   1.157 +        hfsq = 0.5 * f * f;
   1.158 +        if (k == 0)
   1.159 +            return f - (hfsq - s * (hfsq + R));
   1.160 +        else
   1.161 +            return dk * ln2_hi - ((hfsq - (s * (hfsq + R) + dk * ln2_lo)) -
   1.162 +                                  f);
   1.163 +    } else {
   1.164 +        if (k == 0)
   1.165 +            return f - s * (f - R);
   1.166 +        else
   1.167 +            return dk * ln2_hi - ((s * (f - R) - dk * ln2_lo) - f);
   1.168 +    }
   1.169 +}