e_log10.c revision 176451
1141296Sdas
2141296Sdas/* @(#)e_log10.c 1.3 95/01/18 */
32116Sjkh/*
42116Sjkh * ====================================================
52116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
62116Sjkh *
7141296Sdas * Developed at SunSoft, a Sun Microsystems, Inc. business.
82116Sjkh * Permission to use, copy, modify, and distribute this
9141296Sdas * software is freely granted, provided that this notice
102116Sjkh * is preserved.
112116Sjkh * ====================================================
122116Sjkh */
132116Sjkh
14176451Sdas#include <sys/cdefs.h>
15176451Sdas__FBSDID("$FreeBSD: head/lib/msun/src/e_log10.c 176451 2008-02-22 02:30:36Z das $");
162116Sjkh
172116Sjkh/* __ieee754_log10(x)
182116Sjkh * Return the base 10 logarithm of x
19141296Sdas *
202116Sjkh * Method :
212116Sjkh *	Let log10_2hi = leading 40 bits of log10(2) and
222116Sjkh *	    log10_2lo = log10(2) - log10_2hi,
232116Sjkh *	    ivln10   = 1/log(10) rounded.
242116Sjkh *	Then
25141296Sdas *		n = ilogb(x),
262116Sjkh *		if(n<0)  n = n+1;
272116Sjkh *		x = scalbn(x,-n);
282116Sjkh *		log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))
292116Sjkh *
302116Sjkh * Note 1:
31141296Sdas *	To guarantee log10(10**n)=n, where 10**n is normal, the rounding
322116Sjkh *	mode must set to Round-to-Nearest.
332116Sjkh * Note 2:
342116Sjkh *	[1/log(10)] rounded to 53 bits has error  .198   ulps;
352116Sjkh *	log10 is monotonic at all binary break points.
362116Sjkh *
372116Sjkh * Special cases:
38141296Sdas *	log10(x) is NaN with signal if x < 0;
392116Sjkh *	log10(+INF) is +INF with no signal; log10(0) is -INF with signal;
402116Sjkh *	log10(NaN) is that NaN with no signal;
412116Sjkh *	log10(10**N) = N  for N=0,1,...,22.
422116Sjkh *
432116Sjkh * Constants:
442116Sjkh * The hexadecimal values are the intended ones for the following constants.
452116Sjkh * The decimal values may be used, provided that the compiler will convert
462116Sjkh * from decimal to binary accurately enough to produce the hexadecimal values
472116Sjkh * shown.
482116Sjkh */
492116Sjkh
502116Sjkh#include "math.h"
512116Sjkh#include "math_private.h"
522116Sjkh
532116Sjkhstatic const double
542116Sjkhtwo54      =  1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
552116Sjkhivln10     =  4.34294481903251816668e-01, /* 0x3FDBCB7B, 0x1526E50E */
562116Sjkhlog10_2hi  =  3.01029995663611771306e-01, /* 0x3FD34413, 0x509F6000 */
572116Sjkhlog10_2lo  =  3.69423907715893078616e-13; /* 0x3D59FEF3, 0x11F12B36 */
582116Sjkh
592116Sjkhstatic const double zero   =  0.0;
602116Sjkh
6197413Salfreddouble
62117912Speter__ieee754_log10(double x)
632116Sjkh{
642116Sjkh	double y,z;
652116Sjkh	int32_t i,k,hx;
662116Sjkh	u_int32_t lx;
672116Sjkh
682116Sjkh	EXTRACT_WORDS(hx,lx,x);
692116Sjkh
702116Sjkh        k=0;
712116Sjkh        if (hx < 0x00100000) {                  /* x < 2**-1022  */
722116Sjkh            if (((hx&0x7fffffff)|lx)==0)
732116Sjkh                return -two54/zero;             /* log(+-0)=-inf */
742116Sjkh            if (hx<0) return (x-x)/zero;        /* log(-#) = NaN */
752116Sjkh            k -= 54; x *= two54; /* subnormal number, scale up x */
762116Sjkh	    GET_HIGH_WORD(hx,x);
772116Sjkh        }
782116Sjkh	if (hx >= 0x7ff00000) return x+x;
792116Sjkh	k += (hx>>20)-1023;
802116Sjkh	i  = ((u_int32_t)k&0x80000000)>>31;
812116Sjkh        hx = (hx&0x000fffff)|((0x3ff-i)<<20);
822116Sjkh        y  = (double)(k+i);
832116Sjkh	SET_HIGH_WORD(x,hx);
842116Sjkh	z  = y*log10_2lo + ivln10*__ieee754_log(x);
852116Sjkh	return  z+y*log10_2hi;
862116Sjkh}
87