xref: /minix3/lib/libm/src/e_log.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /* @(#)e_log.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_log.c,v 1.12 2002/05/26 22:01:51 wiz Exp $");
16*2fe8fb19SBen Gras #endif
17*2fe8fb19SBen Gras 
18*2fe8fb19SBen Gras /* __ieee754_log(x)
19*2fe8fb19SBen Gras  * Return the logrithm of x
20*2fe8fb19SBen Gras  *
21*2fe8fb19SBen Gras  * Method :
22*2fe8fb19SBen Gras  *   1. Argument Reduction: find k and f such that
23*2fe8fb19SBen Gras  *			x = 2^k * (1+f),
24*2fe8fb19SBen Gras  *	   where  sqrt(2)/2 < 1+f < sqrt(2) .
25*2fe8fb19SBen Gras  *
26*2fe8fb19SBen Gras  *   2. Approximation of log(1+f).
27*2fe8fb19SBen Gras  *	Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
28*2fe8fb19SBen Gras  *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
29*2fe8fb19SBen Gras  *	     	 = 2s + s*R
30*2fe8fb19SBen Gras  *      We use a special Reme algorithm on [0,0.1716] to generate
31*2fe8fb19SBen Gras  * 	a polynomial of degree 14 to approximate R The maximum error
32*2fe8fb19SBen Gras  *	of this polynomial approximation is bounded by 2**-58.45. In
33*2fe8fb19SBen Gras  *	other words,
34*2fe8fb19SBen Gras  *		        2      4      6      8      10      12      14
35*2fe8fb19SBen Gras  *	    R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s  +Lg6*s  +Lg7*s
36*2fe8fb19SBen Gras  *  	(the values of Lg1 to Lg7 are listed in the program)
37*2fe8fb19SBen Gras  *	and
38*2fe8fb19SBen Gras  *	    |      2          14          |     -58.45
39*2fe8fb19SBen Gras  *	    | Lg1*s +...+Lg7*s    -  R(z) | <= 2
40*2fe8fb19SBen Gras  *	    |                             |
41*2fe8fb19SBen Gras  *	Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
42*2fe8fb19SBen Gras  *	In order to guarantee error in log below 1ulp, we compute log
43*2fe8fb19SBen Gras  *	by
44*2fe8fb19SBen Gras  *		log(1+f) = f - s*(f - R)	(if f is not too large)
45*2fe8fb19SBen Gras  *		log(1+f) = f - (hfsq - s*(hfsq+R)).	(better accuracy)
46*2fe8fb19SBen Gras  *
47*2fe8fb19SBen Gras  *	3. Finally,  log(x) = k*ln2 + log(1+f).
48*2fe8fb19SBen Gras  *			    = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
49*2fe8fb19SBen Gras  *	   Here ln2 is split into two floating point number:
50*2fe8fb19SBen Gras  *			ln2_hi + ln2_lo,
51*2fe8fb19SBen Gras  *	   where n*ln2_hi is always exact for |n| < 2000.
52*2fe8fb19SBen Gras  *
53*2fe8fb19SBen Gras  * Special cases:
54*2fe8fb19SBen Gras  *	log(x) is NaN with signal if x < 0 (including -INF) ;
55*2fe8fb19SBen Gras  *	log(+INF) is +INF; log(0) is -INF with signal;
56*2fe8fb19SBen Gras  *	log(NaN) is that NaN with no signal.
57*2fe8fb19SBen Gras  *
58*2fe8fb19SBen Gras  * Accuracy:
59*2fe8fb19SBen Gras  *	according to an error analysis, the error is always less than
60*2fe8fb19SBen Gras  *	1 ulp (unit in the last place).
61*2fe8fb19SBen Gras  *
62*2fe8fb19SBen Gras  * Constants:
63*2fe8fb19SBen Gras  * The hexadecimal values are the intended ones for the following
64*2fe8fb19SBen Gras  * constants. The decimal values may be used, provided that the
65*2fe8fb19SBen Gras  * compiler will convert from decimal to binary accurately enough
66*2fe8fb19SBen Gras  * to produce the hexadecimal values shown.
67*2fe8fb19SBen Gras  */
68*2fe8fb19SBen Gras 
69*2fe8fb19SBen Gras #include "math.h"
70*2fe8fb19SBen Gras #include "math_private.h"
71*2fe8fb19SBen Gras 
72*2fe8fb19SBen Gras static const double
73*2fe8fb19SBen Gras ln2_hi  =  6.93147180369123816490e-01,	/* 3fe62e42 fee00000 */
74*2fe8fb19SBen Gras ln2_lo  =  1.90821492927058770002e-10,	/* 3dea39ef 35793c76 */
75*2fe8fb19SBen Gras two54   =  1.80143985094819840000e+16,  /* 43500000 00000000 */
76*2fe8fb19SBen Gras Lg1 = 6.666666666666735130e-01,  /* 3FE55555 55555593 */
77*2fe8fb19SBen Gras Lg2 = 3.999999999940941908e-01,  /* 3FD99999 9997FA04 */
78*2fe8fb19SBen Gras Lg3 = 2.857142874366239149e-01,  /* 3FD24924 94229359 */
79*2fe8fb19SBen Gras Lg4 = 2.222219843214978396e-01,  /* 3FCC71C5 1D8E78AF */
80*2fe8fb19SBen Gras Lg5 = 1.818357216161805012e-01,  /* 3FC74664 96CB03DE */
81*2fe8fb19SBen Gras Lg6 = 1.531383769920937332e-01,  /* 3FC39A09 D078C69F */
82*2fe8fb19SBen Gras Lg7 = 1.479819860511658591e-01;  /* 3FC2F112 DF3E5244 */
83*2fe8fb19SBen Gras 
84*2fe8fb19SBen Gras static const double zero   =  0.0;
85*2fe8fb19SBen Gras 
86*2fe8fb19SBen Gras double
__ieee754_log(double x)87*2fe8fb19SBen Gras __ieee754_log(double x)
88*2fe8fb19SBen Gras {
89*2fe8fb19SBen Gras 	double hfsq,f,s,z,R,w,t1,t2,dk;
90*2fe8fb19SBen Gras 	int32_t k,hx,i,j;
91*2fe8fb19SBen Gras 	u_int32_t lx;
92*2fe8fb19SBen Gras 
93*2fe8fb19SBen Gras 	EXTRACT_WORDS(hx,lx,x);
94*2fe8fb19SBen Gras 
95*2fe8fb19SBen Gras 	k=0;
96*2fe8fb19SBen Gras 	if (hx < 0x00100000) {			/* x < 2**-1022  */
97*2fe8fb19SBen Gras 	    if (((hx&0x7fffffff)|lx)==0)
98*2fe8fb19SBen Gras 		return -two54/zero;		/* log(+-0)=-inf */
99*2fe8fb19SBen Gras 	    if (hx<0) return (x-x)/zero;	/* log(-#) = NaN */
100*2fe8fb19SBen Gras 	    k -= 54; x *= two54; /* subnormal number, scale up x */
101*2fe8fb19SBen Gras 	    GET_HIGH_WORD(hx,x);
102*2fe8fb19SBen Gras 	}
103*2fe8fb19SBen Gras 	if (hx >= 0x7ff00000) return x+x;
104*2fe8fb19SBen Gras 	k += (hx>>20)-1023;
105*2fe8fb19SBen Gras 	hx &= 0x000fffff;
106*2fe8fb19SBen Gras 	i = (hx+0x95f64)&0x100000;
107*2fe8fb19SBen Gras 	SET_HIGH_WORD(x,hx|(i^0x3ff00000));	/* normalize x or x/2 */
108*2fe8fb19SBen Gras 	k += (i>>20);
109*2fe8fb19SBen Gras 	f = x-1.0;
110*2fe8fb19SBen Gras 	if((0x000fffff&(2+hx))<3) {	/* |f| < 2**-20 */
111*2fe8fb19SBen Gras 	    if(f==zero) { if(k==0) return zero;  else {dk=(double)k;
112*2fe8fb19SBen Gras 				   return dk*ln2_hi+dk*ln2_lo;}
113*2fe8fb19SBen Gras 	    }
114*2fe8fb19SBen Gras 	    R = f*f*(0.5-0.33333333333333333*f);
115*2fe8fb19SBen Gras 	    if(k==0) return f-R; else {dk=(double)k;
116*2fe8fb19SBen Gras 	    	     return dk*ln2_hi-((R-dk*ln2_lo)-f);}
117*2fe8fb19SBen Gras 	}
118*2fe8fb19SBen Gras  	s = f/(2.0+f);
119*2fe8fb19SBen Gras 	dk = (double)k;
120*2fe8fb19SBen Gras 	z = s*s;
121*2fe8fb19SBen Gras 	i = hx-0x6147a;
122*2fe8fb19SBen Gras 	w = z*z;
123*2fe8fb19SBen Gras 	j = 0x6b851-hx;
124*2fe8fb19SBen Gras 	t1= w*(Lg2+w*(Lg4+w*Lg6));
125*2fe8fb19SBen Gras 	t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7)));
126*2fe8fb19SBen Gras 	i |= j;
127*2fe8fb19SBen Gras 	R = t2+t1;
128*2fe8fb19SBen Gras 	if(i>0) {
129*2fe8fb19SBen Gras 	    hfsq=0.5*f*f;
130*2fe8fb19SBen Gras 	    if(k==0) return f-(hfsq-s*(hfsq+R)); else
131*2fe8fb19SBen Gras 		     return dk*ln2_hi-((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f);
132*2fe8fb19SBen Gras 	} else {
133*2fe8fb19SBen Gras 	    if(k==0) return f-s*(f-R); else
134*2fe8fb19SBen Gras 		     return dk*ln2_hi-((s*(f-R)-dk*ln2_lo)-f);
135*2fe8fb19SBen Gras 	}
136*2fe8fb19SBen Gras }
137