Convert log10() to use __kernel_log(), which is more accurate and simpler.

This commit is contained in:
David Schultz 2011-03-07 03:11:27 +00:00
parent f3732b5aaa
commit acda0929b2
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=219360

View File

@ -14,45 +14,18 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/* __ieee754_log10(x)
* Return the base 10 logarithm of x
*
* Method :
* Let log10_2hi = leading 40 bits of log10(2) and
* log10_2lo = log10(2) - log10_2hi,
* ivln10 = 1/log(10) rounded.
* Then
* n = ilogb(x),
* if(n<0) n = n+1;
* x = scalbn(x,-n);
* log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))
*
* Note 1:
* To guarantee log10(10**n)=n, where 10**n is normal, the rounding
* mode must set to Round-to-Nearest.
* Note 2:
* [1/log(10)] rounded to 53 bits has error .198 ulps;
* log10 is monotonic at all binary break points.
*
* Special cases:
* log10(x) is NaN with signal if x < 0;
* log10(+INF) is +INF with no signal; log10(0) is -INF with signal;
* log10(NaN) is that NaN with no signal;
* log10(10**N) = N for N=0,1,...,22.
*
* Constants:
* The hexadecimal values are the intended ones for the following constants.
* The decimal values may be used, provided that the compiler will convert
* from decimal to binary accurately enough to produce the hexadecimal values
* shown.
/*
* Return the base 10 logarithm of x. See k_log.c for details on the algorithm.
*/
#include "math.h"
#include "math_private.h"
#include "k_log.h"
static const double
two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
ivln10 = 4.34294481903251816668e-01, /* 0x3FDBCB7B, 0x1526E50E */
ivln10hi = 4.34294481878168880939e-01, /* 0x3fdbcb7b, 0x15200000 */
ivln10lo = 2.50829467116452752298e-11, /* 0x3dbb9438, 0xca9aadd5 */
log10_2hi = 3.01029995663611771306e-01, /* 0x3FD34413, 0x509F6000 */
log10_2lo = 3.69423907715893078616e-13; /* 0x3D59FEF3, 0x11F12B36 */
@ -61,7 +34,7 @@ static const double zero = 0.0;
double
__ieee754_log10(double x)
{
double y,z;
double f,hi,lo,y,z;
int32_t i,k,hx;
u_int32_t lx;
@ -77,10 +50,15 @@ __ieee754_log10(double x)
}
if (hx >= 0x7ff00000) return x+x;
k += (hx>>20)-1023;
i = ((u_int32_t)k&0x80000000)>>31;
hx = (hx&0x000fffff)|((0x3ff-i)<<20);
y = (double)(k+i);
SET_HIGH_WORD(x,hx);
z = y*log10_2lo + ivln10*__ieee754_log(x);
hx &= 0x000fffff;
i = (hx+0x95f64)&0x100000;
SET_HIGH_WORD(x,hx|(i^0x3ff00000)); /* normalize x or x/2 */
k += (i>>20);
y = (double)k;
f = __kernel_log(x);
hi = x = x - 1;
SET_LOW_WORD(hi,0);
lo = x - hi;
z = y*log10_2lo + (x+f)*ivln10lo + (lo+f)*ivln10hi + hi*ivln10hi;
return z+y*log10_2hi;
}