xref: /minix3/lib/libm/src/e_log10.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /* @(#)e_log10.c 5.1 93/09/24 */
2*2fe8fb19SBen Gras /*
3*2fe8fb19SBen Gras  * ====================================================
4*2fe8fb19SBen Gras  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*2fe8fb19SBen Gras  *
6*2fe8fb19SBen Gras  * Developed at SunPro, a Sun Microsystems, Inc. business.
7*2fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this
8*2fe8fb19SBen Gras  * software is freely granted, provided that this notice
9*2fe8fb19SBen Gras  * is preserved.
10*2fe8fb19SBen Gras  * ====================================================
11*2fe8fb19SBen Gras  */
12*2fe8fb19SBen Gras 
13*2fe8fb19SBen Gras #include <sys/cdefs.h>
14*2fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
15*2fe8fb19SBen Gras __RCSID("$NetBSD: e_log10.c,v 1.12 2002/05/26 22:01:51 wiz Exp $");
16*2fe8fb19SBen Gras #endif
17*2fe8fb19SBen Gras 
18*2fe8fb19SBen Gras /* __ieee754_log10(x)
19*2fe8fb19SBen Gras  * Return the base 10 logarithm of x
20*2fe8fb19SBen Gras  *
21*2fe8fb19SBen Gras  * Method :
22*2fe8fb19SBen Gras  *	Let log10_2hi = leading 40 bits of log10(2) and
23*2fe8fb19SBen Gras  *	    log10_2lo = log10(2) - log10_2hi,
24*2fe8fb19SBen Gras  *	    ivln10   = 1/log(10) rounded.
25*2fe8fb19SBen Gras  *	Then
26*2fe8fb19SBen Gras  *		n = ilogb(x),
27*2fe8fb19SBen Gras  *		if(n<0)  n = n+1;
28*2fe8fb19SBen Gras  *		x = scalbn(x,-n);
29*2fe8fb19SBen Gras  *		log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))
30*2fe8fb19SBen Gras  *
31*2fe8fb19SBen Gras  * Note 1:
32*2fe8fb19SBen Gras  *	To guarantee log10(10**n)=n, where 10**n is normal, the rounding
33*2fe8fb19SBen Gras  *	mode must set to Round-to-Nearest.
34*2fe8fb19SBen Gras  * Note 2:
35*2fe8fb19SBen Gras  *	[1/log(10)] rounded to 53 bits has error  .198   ulps;
36*2fe8fb19SBen Gras  *	log10 is monotonic at all binary break points.
37*2fe8fb19SBen Gras  *
38*2fe8fb19SBen Gras  * Special cases:
39*2fe8fb19SBen Gras  *	log10(x) is NaN with signal if x < 0;
40*2fe8fb19SBen Gras  *	log10(+INF) is +INF with no signal; log10(0) is -INF with signal;
41*2fe8fb19SBen Gras  *	log10(NaN) is that NaN with no signal;
42*2fe8fb19SBen Gras  *	log10(10**N) = N  for N=0,1,...,22.
43*2fe8fb19SBen Gras  *
44*2fe8fb19SBen Gras  * Constants:
45*2fe8fb19SBen Gras  * The hexadecimal values are the intended ones for the following constants.
46*2fe8fb19SBen Gras  * The decimal values may be used, provided that the compiler will convert
47*2fe8fb19SBen Gras  * from decimal to binary accurately enough to produce the hexadecimal values
48*2fe8fb19SBen Gras  * shown.
49*2fe8fb19SBen Gras  */
50*2fe8fb19SBen Gras 
51*2fe8fb19SBen Gras #include "math.h"
52*2fe8fb19SBen Gras #include "math_private.h"
53*2fe8fb19SBen Gras 
54*2fe8fb19SBen Gras static const double
55*2fe8fb19SBen Gras two54      =  1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
56*2fe8fb19SBen Gras ivln10     =  4.34294481903251816668e-01, /* 0x3FDBCB7B, 0x1526E50E */
57*2fe8fb19SBen Gras log10_2hi  =  3.01029995663611771306e-01, /* 0x3FD34413, 0x509F6000 */
58*2fe8fb19SBen Gras log10_2lo  =  3.69423907715893078616e-13; /* 0x3D59FEF3, 0x11F12B36 */
59*2fe8fb19SBen Gras 
60*2fe8fb19SBen Gras static const double zero   =  0.0;
61*2fe8fb19SBen Gras 
62*2fe8fb19SBen Gras double
__ieee754_log10(double x)63*2fe8fb19SBen Gras __ieee754_log10(double x)
64*2fe8fb19SBen Gras {
65*2fe8fb19SBen Gras 	double y,z;
66*2fe8fb19SBen Gras 	int32_t i,k,hx;
67*2fe8fb19SBen Gras 	u_int32_t lx;
68*2fe8fb19SBen Gras 
69*2fe8fb19SBen Gras 	EXTRACT_WORDS(hx,lx,x);
70*2fe8fb19SBen Gras 
71*2fe8fb19SBen Gras         k=0;
72*2fe8fb19SBen Gras         if (hx < 0x00100000) {                  /* x < 2**-1022  */
73*2fe8fb19SBen Gras             if (((hx&0x7fffffff)|lx)==0)
74*2fe8fb19SBen Gras                 return -two54/zero;             /* log(+-0)=-inf */
75*2fe8fb19SBen Gras             if (hx<0) return (x-x)/zero;        /* log(-#) = NaN */
76*2fe8fb19SBen Gras             k -= 54; x *= two54; /* subnormal number, scale up x */
77*2fe8fb19SBen Gras 	    GET_HIGH_WORD(hx,x);
78*2fe8fb19SBen Gras         }
79*2fe8fb19SBen Gras 	if (hx >= 0x7ff00000) return x+x;
80*2fe8fb19SBen Gras 	k += (hx>>20)-1023;
81*2fe8fb19SBen Gras 	i  = ((u_int32_t)k&0x80000000)>>31;
82*2fe8fb19SBen Gras         hx = (hx&0x000fffff)|((0x3ff-i)<<20);
83*2fe8fb19SBen Gras         y  = (double)(k+i);
84*2fe8fb19SBen Gras 	SET_HIGH_WORD(x,hx);
85*2fe8fb19SBen Gras 	z  = y*log10_2lo + ivln10*__ieee754_log(x);
86*2fe8fb19SBen Gras 	return  z+y*log10_2hi;
87*2fe8fb19SBen Gras }
88