xref: /minix3/lib/libm/src/e_logf.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /* e_logf.c -- float version of e_log.c.
2*2fe8fb19SBen Gras  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3*2fe8fb19SBen Gras  */
4*2fe8fb19SBen Gras 
5*2fe8fb19SBen Gras /*
6*2fe8fb19SBen Gras  * ====================================================
7*2fe8fb19SBen Gras  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8*2fe8fb19SBen Gras  *
9*2fe8fb19SBen Gras  * Developed at SunPro, a Sun Microsystems, Inc. business.
10*2fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this
11*2fe8fb19SBen Gras  * software is freely granted, provided that this notice
12*2fe8fb19SBen Gras  * is preserved.
13*2fe8fb19SBen Gras  * ====================================================
14*2fe8fb19SBen Gras  */
15*2fe8fb19SBen Gras 
16*2fe8fb19SBen Gras #include <sys/cdefs.h>
17*2fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
18*2fe8fb19SBen Gras __RCSID("$NetBSD: e_logf.c,v 1.8 2002/05/26 22:01:51 wiz Exp $");
19*2fe8fb19SBen Gras #endif
20*2fe8fb19SBen Gras 
21*2fe8fb19SBen Gras #include "math.h"
22*2fe8fb19SBen Gras #include "math_private.h"
23*2fe8fb19SBen Gras 
24*2fe8fb19SBen Gras static const float
25*2fe8fb19SBen Gras ln2_hi =   6.9313812256e-01,	/* 0x3f317180 */
26*2fe8fb19SBen Gras ln2_lo =   9.0580006145e-06,	/* 0x3717f7d1 */
27*2fe8fb19SBen Gras two25 =    3.355443200e+07,	/* 0x4c000000 */
28*2fe8fb19SBen Gras Lg1 = 6.6666668653e-01,	/* 3F2AAAAB */
29*2fe8fb19SBen Gras Lg2 = 4.0000000596e-01,	/* 3ECCCCCD */
30*2fe8fb19SBen Gras Lg3 = 2.8571429849e-01, /* 3E924925 */
31*2fe8fb19SBen Gras Lg4 = 2.2222198546e-01, /* 3E638E29 */
32*2fe8fb19SBen Gras Lg5 = 1.8183572590e-01, /* 3E3A3325 */
33*2fe8fb19SBen Gras Lg6 = 1.5313838422e-01, /* 3E1CD04F */
34*2fe8fb19SBen Gras Lg7 = 1.4798198640e-01; /* 3E178897 */
35*2fe8fb19SBen Gras 
36*2fe8fb19SBen Gras static const float zero   =  0.0;
37*2fe8fb19SBen Gras 
38*2fe8fb19SBen Gras float
__ieee754_logf(float x)39*2fe8fb19SBen Gras __ieee754_logf(float x)
40*2fe8fb19SBen Gras {
41*2fe8fb19SBen Gras 	float hfsq,f,s,z,R,w,t1,t2,dk;
42*2fe8fb19SBen Gras 	int32_t k,ix,i,j;
43*2fe8fb19SBen Gras 
44*2fe8fb19SBen Gras 	GET_FLOAT_WORD(ix,x);
45*2fe8fb19SBen Gras 
46*2fe8fb19SBen Gras 	k=0;
47*2fe8fb19SBen Gras 	if (ix < 0x00800000) {			/* x < 2**-126  */
48*2fe8fb19SBen Gras 	    if ((ix&0x7fffffff)==0)
49*2fe8fb19SBen Gras 		return -two25/zero;		/* log(+-0)=-inf */
50*2fe8fb19SBen Gras 	    if (ix<0) return (x-x)/zero;	/* log(-#) = NaN */
51*2fe8fb19SBen Gras 	    k -= 25; x *= two25; /* subnormal number, scale up x */
52*2fe8fb19SBen Gras 	    GET_FLOAT_WORD(ix,x);
53*2fe8fb19SBen Gras 	}
54*2fe8fb19SBen Gras 	if (ix >= 0x7f800000) return x+x;
55*2fe8fb19SBen Gras 	k += (ix>>23)-127;
56*2fe8fb19SBen Gras 	ix &= 0x007fffff;
57*2fe8fb19SBen Gras 	i = (ix+(0x95f64<<3))&0x800000;
58*2fe8fb19SBen Gras 	SET_FLOAT_WORD(x,ix|(i^0x3f800000));	/* normalize x or x/2 */
59*2fe8fb19SBen Gras 	k += (i>>23);
60*2fe8fb19SBen Gras 	f = x-(float)1.0;
61*2fe8fb19SBen Gras 	if((0x007fffff&(15+ix))<16) {	/* |f| < 2**-20 */
62*2fe8fb19SBen Gras 	    if(f==zero) { if(k==0) return zero;  else {dk=(float)k;
63*2fe8fb19SBen Gras 				   return dk*ln2_hi+dk*ln2_lo;}
64*2fe8fb19SBen Gras 	    }
65*2fe8fb19SBen Gras 	    R = f*f*((float)0.5-(float)0.33333333333333333*f);
66*2fe8fb19SBen Gras 	    if(k==0) return f-R; else {dk=(float)k;
67*2fe8fb19SBen Gras 	    	     return dk*ln2_hi-((R-dk*ln2_lo)-f);}
68*2fe8fb19SBen Gras 	}
69*2fe8fb19SBen Gras  	s = f/((float)2.0+f);
70*2fe8fb19SBen Gras 	dk = (float)k;
71*2fe8fb19SBen Gras 	z = s*s;
72*2fe8fb19SBen Gras 	i = ix-(0x6147a<<3);
73*2fe8fb19SBen Gras 	w = z*z;
74*2fe8fb19SBen Gras 	j = (0x6b851<<3)-ix;
75*2fe8fb19SBen Gras 	t1= w*(Lg2+w*(Lg4+w*Lg6));
76*2fe8fb19SBen Gras 	t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7)));
77*2fe8fb19SBen Gras 	i |= j;
78*2fe8fb19SBen Gras 	R = t2+t1;
79*2fe8fb19SBen Gras 	if(i>0) {
80*2fe8fb19SBen Gras 	    hfsq=(float)0.5*f*f;
81*2fe8fb19SBen Gras 	    if(k==0) return f-(hfsq-s*(hfsq+R)); else
82*2fe8fb19SBen Gras 		     return dk*ln2_hi-((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f);
83*2fe8fb19SBen Gras 	} else {
84*2fe8fb19SBen Gras 	    if(k==0) return f-s*(f-R); else
85*2fe8fb19SBen Gras 		     return dk*ln2_hi-((s*(f-R)-dk*ln2_lo)-f);
86*2fe8fb19SBen Gras 	}
87*2fe8fb19SBen Gras }
88