src/libm/e_log10.c
changeset 11852 c3fc99758ad2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/libm/e_log10.c	Wed Jan 17 11:53:09 2018 -0500
     1.3 @@ -0,0 +1,106 @@
     1.4 +/*
     1.5 + * ====================================================
     1.6 + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
     1.7 + *
     1.8 + * Developed at SunPro, a Sun Microsystems, Inc. business.
     1.9 + * Permission to use, copy, modify, and distribute this
    1.10 + * software is freely granted, provided that this notice
    1.11 + * is preserved.
    1.12 + * ====================================================
    1.13 + */
    1.14 +
    1.15 +#if defined(_MSC_VER)           /* Handle Microsoft VC++ compiler specifics. */
    1.16 +/* C4723: potential divide by zero. */
    1.17 +#pragma warning ( disable : 4723 )
    1.18 +#endif
    1.19 +
    1.20 +/* __ieee754_log10(x)
    1.21 + * Return the base 10 logarithm of x
    1.22 + *
    1.23 + * Method :
    1.24 + *	Let log10_2hi = leading 40 bits of log10(2) and
    1.25 + *	    log10_2lo = log10(2) - log10_2hi,
    1.26 + *	    ivln10   = 1/log(10) rounded.
    1.27 + *	Then
    1.28 + *		n = ilogb(x),
    1.29 + *		if(n<0)  n = n+1;
    1.30 + *		x = scalbn(x,-n);
    1.31 + *		log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))
    1.32 + *
    1.33 + * Note 1:
    1.34 + *	To guarantee log10(10**n)=n, where 10**n is normal, the rounding
    1.35 + *	mode must set to Round-to-Nearest.
    1.36 + * Note 2:
    1.37 + *	[1/log(10)] rounded to 53 bits has error  .198   ulps;
    1.38 + *	log10 is monotonic at all binary break points.
    1.39 + *
    1.40 + * Special cases:
    1.41 + *	log10(x) is NaN with signal if x < 0;
    1.42 + *	log10(+INF) is +INF with no signal; log10(0) is -INF with signal;
    1.43 + *	log10(NaN) is that NaN with no signal;
    1.44 + *	log10(10**N) = N  for N=0,1,...,22.
    1.45 + *
    1.46 + * Constants:
    1.47 + * The hexadecimal values are the intended ones for the following constants.
    1.48 + * The decimal values may be used, provided that the compiler will convert
    1.49 + * from decimal to binary accurately enough to produce the hexadecimal values
    1.50 + * shown.
    1.51 + */
    1.52 +
    1.53 +#include "math_libm.h"
    1.54 +#include "math_private.h"
    1.55 +
    1.56 +static const double
    1.57 +two54      =  1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
    1.58 +ivln10     =  4.34294481903251816668e-01, /* 0x3FDBCB7B, 0x1526E50E */
    1.59 +log10_2hi  =  3.01029995663611771306e-01, /* 0x3FD34413, 0x509F6000 */
    1.60 +log10_2lo  =  3.69423907715893078616e-13; /* 0x3D59FEF3, 0x11F12B36 */
    1.61 +
    1.62 +static const double zero = 0.0;
    1.63 +
    1.64 +double attribute_hidden __ieee754_log10(double x)
    1.65 +{
    1.66 +	double y,z;
    1.67 +	int32_t i,k,hx;
    1.68 +	u_int32_t lx;
    1.69 +
    1.70 +	EXTRACT_WORDS(hx,lx,x);
    1.71 +
    1.72 +        k=0;
    1.73 +        if (hx < 0x00100000) {                  /* x < 2**-1022  */
    1.74 +            if (((hx&0x7fffffff)|lx)==0)
    1.75 +                return -two54/zero;             /* log(+-0)=-inf */
    1.76 +            if (hx<0) return (x-x)/zero;        /* log(-#) = NaN */
    1.77 +            k -= 54; x *= two54; /* subnormal number, scale up x */
    1.78 +	    GET_HIGH_WORD(hx,x);
    1.79 +        }
    1.80 +	if (hx >= 0x7ff00000) return x+x;
    1.81 +	k += (hx>>20)-1023;
    1.82 +	i  = ((u_int32_t)k&0x80000000)>>31;
    1.83 +        hx = (hx&0x000fffff)|((0x3ff-i)<<20);
    1.84 +        y  = (double)(k+i);
    1.85 +	SET_HIGH_WORD(x,hx);
    1.86 +	z  = y*log10_2lo + ivln10*__ieee754_log(x);
    1.87 +	return  z+y*log10_2hi;
    1.88 +}
    1.89 +
    1.90 +/*
    1.91 + * wrapper log10(X)
    1.92 + */
    1.93 +#ifndef _IEEE_LIBM
    1.94 +double log10(double x)
    1.95 +{
    1.96 +	double z = __ieee754_log10(x);
    1.97 +	if (_LIB_VERSION == _IEEE_ || isnan(x))
    1.98 +		return z;
    1.99 +	if (x <= 0.0) {
   1.100 +		if(x == 0.0)
   1.101 +			return __kernel_standard(x, x, 18); /* log10(0) */
   1.102 +		return __kernel_standard(x, x, 19); /* log10(x<0) */
   1.103 +	}
   1.104 +	return z;
   1.105 +}
   1.106 +#else
   1.107 +strong_alias(__ieee754_log10, log10)
   1.108 +#endif
   1.109 +libm_hidden_def(log10)