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