Back to index

glibc  2.9
e_log2f.c
Go to the documentation of this file.
00001 /* e_logf.c -- float version of e_log.c.
00002  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
00003  * adapted for log2 by Ulrich Drepper <drepper@cygnus.com>
00004  */
00005 
00006 /*
00007  * ====================================================
00008  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00009  *
00010  * Developed at SunPro, a Sun Microsystems, Inc. business.
00011  * Permission to use, copy, modify, and distribute this
00012  * software is freely granted, provided that this notice
00013  * is preserved.
00014  * ====================================================
00015  */
00016 
00017 
00018 #include "math.h"
00019 #include "math_private.h"
00020 
00021 #ifdef __STDC__
00022 static const float
00023 #else
00024 static float
00025 #endif
00026 ln2 = 0.69314718055994530942,
00027 two25 =    3.355443200e+07, /* 0x4c000000 */
00028 Lg1 = 6.6666668653e-01,     /* 3F2AAAAB */
00029 Lg2 = 4.0000000596e-01,     /* 3ECCCCCD */
00030 Lg3 = 2.8571429849e-01, /* 3E924925 */
00031 Lg4 = 2.2222198546e-01, /* 3E638E29 */
00032 Lg5 = 1.8183572590e-01, /* 3E3A3325 */
00033 Lg6 = 1.5313838422e-01, /* 3E1CD04F */
00034 Lg7 = 1.4798198640e-01; /* 3E178897 */
00035 
00036 #ifdef __STDC__
00037 static const float zero   =  0.0;
00038 #else
00039 static float zero   =  0.0;
00040 #endif
00041 
00042 #ifdef __STDC__
00043        float __ieee754_log2f(float x)
00044 #else
00045        float __ieee754_log2f(x)
00046        float x;
00047 #endif
00048 {
00049        float hfsq,f,s,z,R,w,t1,t2,dk;
00050        int32_t k,ix,i,j;
00051 
00052        GET_FLOAT_WORD(ix,x);
00053 
00054        k=0;
00055        if (ix < 0x00800000) {                    /* x < 2**-126  */
00056            if ((ix&0x7fffffff)==0)
00057               return -two25/(x-x);        /* log(+-0)=-inf */
00058            if (ix<0) return (x-x)/(x-x);  /* log(-#) = NaN */
00059            k -= 25; x *= two25; /* subnormal number, scale up x */
00060            GET_FLOAT_WORD(ix,x);
00061        }
00062        if (ix >= 0x7f800000) return x+x;
00063        k += (ix>>23)-127;
00064        ix &= 0x007fffff;
00065        i = (ix+(0x95f64<<3))&0x800000;
00066        SET_FLOAT_WORD(x,ix|(i^0x3f800000));      /* normalize x or x/2 */
00067        k += (i>>23);
00068        dk = (float)k;
00069        f = x-(float)1.0;
00070        if((0x007fffff&(15+ix))<16) {      /* |f| < 2**-20 */
00071            if(f==zero) return dk;
00072            R = f*f*((float)0.5-(float)0.33333333333333333*f);
00073            return dk-(R-f)/ln2;
00074        }
00075        s = f/((float)2.0+f);
00076        z = s*s;
00077        i = ix-(0x6147a<<3);
00078        w = z*z;
00079        j = (0x6b851<<3)-ix;
00080        t1= w*(Lg2+w*(Lg4+w*Lg6));
00081        t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7)));
00082        i |= j;
00083        R = t2+t1;
00084        if(i>0) {
00085            hfsq=(float)0.5*f*f;
00086            return dk-((hfsq-(s*(hfsq+R)))-f)/ln2;
00087        } else {
00088            return dk-((s*(f-R))-f)/ln2;
00089        }
00090 }