xref: /minix/lib/libm/src/e_log2f.c (revision 2fe8fb19)
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_log2f.c,v 1.1 2005/07/21 12:55:58 christos 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 = 0.6931471805599452862268,
26*2fe8fb19SBen Gras two25 =    3.355443200e+07,	/* 0x4c000000 */
27*2fe8fb19SBen Gras Lg1 = 6.6666668653e-01,	/* 3F2AAAAB */
28*2fe8fb19SBen Gras Lg2 = 4.0000000596e-01,	/* 3ECCCCCD */
29*2fe8fb19SBen Gras Lg3 = 2.8571429849e-01, /* 3E924925 */
30*2fe8fb19SBen Gras Lg4 = 2.2222198546e-01, /* 3E638E29 */
31*2fe8fb19SBen Gras Lg5 = 1.8183572590e-01, /* 3E3A3325 */
32*2fe8fb19SBen Gras Lg6 = 1.5313838422e-01, /* 3E1CD04F */
33*2fe8fb19SBen Gras Lg7 = 1.4798198640e-01; /* 3E178897 */
34*2fe8fb19SBen Gras 
35*2fe8fb19SBen Gras static const float zero   =  0.0;
36*2fe8fb19SBen Gras 
37*2fe8fb19SBen Gras float
__ieee754_log2f(float x)38*2fe8fb19SBen Gras __ieee754_log2f(float x)
39*2fe8fb19SBen Gras {
40*2fe8fb19SBen Gras 	float hfsq,f,s,z,R,w,t1,t2,dk;
41*2fe8fb19SBen Gras 	int32_t k,ix,i,j;
42*2fe8fb19SBen Gras 
43*2fe8fb19SBen Gras 	GET_FLOAT_WORD(ix,x);
44*2fe8fb19SBen Gras 
45*2fe8fb19SBen Gras 	k=0;
46*2fe8fb19SBen Gras 	if (ix < 0x00800000) {			/* x < 2**-126  */
47*2fe8fb19SBen Gras 	    if ((ix&0x7fffffff)==0)
48*2fe8fb19SBen Gras 		return -two25/zero;		/* log(+-0)=-inf */
49*2fe8fb19SBen Gras 	    if (ix<0) return (x-x)/zero;	/* log(-#) = NaN */
50*2fe8fb19SBen Gras 	    k -= 25; x *= two25; /* subnormal number, scale up x */
51*2fe8fb19SBen Gras 	    GET_FLOAT_WORD(ix,x);
52*2fe8fb19SBen Gras 	}
53*2fe8fb19SBen Gras 	if (ix >= 0x7f800000) return x+x;
54*2fe8fb19SBen Gras 	k += (ix>>23)-127;
55*2fe8fb19SBen Gras 	ix &= 0x007fffff;
56*2fe8fb19SBen Gras 	i = (ix+(0x95f64<<3))&0x800000;
57*2fe8fb19SBen Gras 	SET_FLOAT_WORD(x,ix|(i^0x3f800000));	/* normalize x or x/2 */
58*2fe8fb19SBen Gras 	k += (i>>23);
59*2fe8fb19SBen Gras 	dk = (float)k;
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)
63*2fe8fb19SBen Gras 		    return (dk);
64*2fe8fb19SBen Gras 	    R = f*f*((float)0.5-(float)0.33333333333333333*f);
65*2fe8fb19SBen Gras 	    return (dk-(R-f)/ln2);
66*2fe8fb19SBen Gras 	}
67*2fe8fb19SBen Gras  	s = f/((float)2.0+f);
68*2fe8fb19SBen Gras 	z = s*s;
69*2fe8fb19SBen Gras 	i = ix-(0x6147a<<3);
70*2fe8fb19SBen Gras 	w = z*z;
71*2fe8fb19SBen Gras 	j = (0x6b851<<3)-ix;
72*2fe8fb19SBen Gras 	t1= w*(Lg2+w*(Lg4+w*Lg6));
73*2fe8fb19SBen Gras 	t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7)));
74*2fe8fb19SBen Gras 	i |= j;
75*2fe8fb19SBen Gras 	R = t2+t1;
76*2fe8fb19SBen Gras 	if(i>0) {
77*2fe8fb19SBen Gras 	    hfsq=(float)0.5*f*f;
78*2fe8fb19SBen Gras 	    return (dk-(hfsq-s*(hfsq+R)-f)/ln2);
79*2fe8fb19SBen Gras 	} else
80*2fe8fb19SBen Gras 		return (dk-((s*(f-R))-f)/ln2);
81*2fe8fb19SBen Gras }
82