POK
/home/jaouen/pok_official/pok/trunk/libpok/libm/e_expf.c
00001 /*
00002  *                               POK header
00003  * 
00004  * The following file is a part of the POK project. Any modification should
00005  * made according to the POK licence. You CANNOT use this file or a part of
00006  * this file is this part of a file for your own project
00007  *
00008  * For more information on the POK licence, please see our LICENCE FILE
00009  *
00010  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
00011  *
00012  *                                      Copyright (c) 2007-2009 POK team 
00013  *
00014  * Created by julien on Fri Jan 30 14:41:34 2009 
00015  */
00016 
00017 /* e_expf.c -- float version of e_exp.c.
00018  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
00019  */
00020 
00021 /*
00022  * ====================================================
00023  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00024  *
00025  * Developed at SunPro, a Sun Microsystems, Inc. business.
00026  * Permission to use, copy, modify, and distribute this
00027  * software is freely granted, provided that this notice
00028  * is preserved.
00029  * ====================================================
00030  */
00031 
00032 #ifdef POK_NEEDS_LIBMATH
00033 
00034 #include "math_private.h"
00035 
00036 static const float huge = 1.0e+30;
00037 
00038 static const float
00039 one     = 1.0,
00040 halF[2] = {0.5,-0.5,},
00041 twom100 = 7.8886090522e-31,      /* 2**-100=0x0d800000 */
00042 o_threshold=  8.8721679688e+01,  /* 0x42b17180 */
00043 u_threshold= -1.0397208405e+02,  /* 0xc2cff1b5 */
00044 ln2HI[2]   ={ 6.9313812256e-01,         /* 0x3f317180 */
00045              -6.9313812256e-01,},       /* 0xbf317180 */
00046 ln2LO[2]   ={ 9.0580006145e-06,         /* 0x3717f7d1 */
00047              -9.0580006145e-06,},       /* 0xb717f7d1 */
00048 invln2 =  1.4426950216e+00,             /* 0x3fb8aa3b */
00049 P1   =  1.6666667163e-01, /* 0x3e2aaaab */
00050 P2   = -2.7777778450e-03, /* 0xbb360b61 */
00051 P3   =  6.6137559770e-05, /* 0x388ab355 */
00052 P4   = -1.6533901999e-06, /* 0xb5ddea0e */
00053 P5   =  4.1381369442e-08; /* 0x3331bb4c */
00054 
00055 float
00056 __ieee754_expf(float x) /* default IEEE double exp */
00057 {
00058         float y,hi,lo,c,t;
00059         int32_t k,xsb;
00060         uint32_t hx;
00061 
00062         hi = lo = 0;
00063         k = 0;
00064         GET_FLOAT_WORD(hx,x);
00065         xsb = (hx>>31)&1;               /* sign bit of x */
00066         hx &= 0x7fffffff;               /* high word of |x| */
00067 
00068     /* filter out non-finite argument */
00069         if(hx >= 0x42b17218) {                  /* if |x|>=88.721... */
00070             if(hx>0x7f800000)
00071                  return x+x;                    /* NaN */
00072             if(hx==0x7f800000)
00073                 return (xsb==0)? x:0.0;         /* exp(+-inf)={inf,0} */
00074             if(x > o_threshold) return huge*huge; /* overflow */
00075             if(x < u_threshold) return twom100*twom100; /* underflow */
00076         }
00077 
00078     /* argument reduction */
00079         if(hx > 0x3eb17218) {           /* if  |x| > 0.5 ln2 */
00080             if(hx < 0x3F851592) {       /* and |x| < 1.5 ln2 */
00081                 hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;
00082             } else {
00083                 k  = invln2*x+halF[xsb];
00084                 t  = k;
00085                 hi = x - t*ln2HI[0];    /* t*ln2HI is exact here */
00086                 lo = t*ln2LO[0];
00087             }
00088             x  = hi - lo;
00089         }
00090         else if(hx < 0x31800000)  {     /* when |x|<2**-28 */
00091             if(huge+x>one) return one+x;/* trigger inexact */
00092         }
00093         else k = 0;
00094 
00095     /* x is now in primary range */
00096         t  = x*x;
00097         c  = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
00098         if(k==0)        return one-((x*c)/(c-(float)2.0)-x);
00099         else            y = one-((lo-(x*c)/((float)2.0-c))-hi);
00100         if(k >= -125) {
00101             uint32_t hy;
00102             GET_FLOAT_WORD(hy,y);
00103             SET_FLOAT_WORD(y,hy+(k<<23));       /* add k to y's exponent */
00104             return y;
00105         } else {
00106             uint32_t hy;
00107             GET_FLOAT_WORD(hy,y);
00108             SET_FLOAT_WORD(y,hy+((k+100)<<23)); /* add k to y's exponent */
00109             return y*twom100;
00110         }
00111 }
00112 #endif
00113