Back to index

glibc  2.9
s_expm1f.c
Go to the documentation of this file.
00001 /* s_expm1f.c -- float version of s_expm1.c.
00002  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
00003  */
00004 
00005 /*
00006  * ====================================================
00007  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00008  *
00009  * Developed at SunPro, a Sun Microsystems, Inc. business.
00010  * Permission to use, copy, modify, and distribute this
00011  * software is freely granted, provided that this notice
00012  * is preserved.
00013  * ====================================================
00014  */
00015 
00016 #if defined(LIBM_SCCS) && !defined(lint)
00017 static char rcsid[] = "$NetBSD: s_expm1f.c,v 1.5 1995/05/10 20:47:11 jtc Exp $";
00018 #endif
00019 
00020 #include "math.h"
00021 #include "math_private.h"
00022 
00023 static const volatile float huge = 1.0e+30;
00024 static const volatile float tiny = 1.0e-30;
00025 
00026 #ifdef __STDC__
00027 static const float
00028 #else
00029 static float
00030 #endif
00031 one           = 1.0,
00032 o_threshold   = 8.8721679688e+01,/* 0x42b17180 */
00033 ln2_hi        = 6.9313812256e-01,/* 0x3f317180 */
00034 ln2_lo        = 9.0580006145e-06,/* 0x3717f7d1 */
00035 invln2        = 1.4426950216e+00,/* 0x3fb8aa3b */
00036        /* scaled coefficients related to expm1 */
00037 Q1  =  -3.3333335072e-02, /* 0xbd088889 */
00038 Q2  =   1.5873016091e-03, /* 0x3ad00d01 */
00039 Q3  =  -7.9365076090e-05, /* 0xb8a670cd */
00040 Q4  =   4.0082177293e-06, /* 0x36867e54 */
00041 Q5  =  -2.0109921195e-07; /* 0xb457edbb */
00042 
00043 #ifdef __STDC__
00044        float __expm1f(float x)
00045 #else
00046        float __expm1f(x)
00047        float x;
00048 #endif
00049 {
00050        float y,hi,lo,c,t,e,hxs,hfx,r1;
00051        int32_t k,xsb;
00052        u_int32_t hx;
00053 
00054        GET_FLOAT_WORD(hx,x);
00055        xsb = hx&0x80000000;        /* sign bit of x */
00056        if(xsb==0) y=x; else y= -x; /* y = |x| */
00057        hx &= 0x7fffffff;           /* high word of |x| */
00058 
00059     /* filter out huge and non-finite argument */
00060        if(hx >= 0x4195b844) {                    /* if |x|>=27*ln2 */
00061            if(hx >= 0x42b17218) {         /* if |x|>=88.721... */
00062                 if(hx>0x7f800000)
00063                   return x+x;       /* NaN */
00064               if(hx==0x7f800000)
00065                   return (xsb==0)? x:-1.0;/* exp(+-inf)={inf,-1} */
00066                if(x > o_threshold) return huge*huge; /* overflow */
00067            }
00068            if(xsb!=0) { /* x < -27*ln2, return -1.0 with inexact */
00069               if(x+tiny<(float)0.0)       /* raise inexact */
00070               return tiny-one;     /* return -1 */
00071            }
00072        }
00073 
00074     /* argument reduction */
00075        if(hx > 0x3eb17218) {              /* if  |x| > 0.5 ln2 */
00076            if(hx < 0x3F851592) {   /* and |x| < 1.5 ln2 */
00077               if(xsb==0)
00078                   {hi = x - ln2_hi; lo =  ln2_lo;  k =  1;}
00079               else
00080                   {hi = x + ln2_hi; lo = -ln2_lo;  k = -1;}
00081            } else {
00082               k  = invln2*x+((xsb==0)?(float)0.5:(float)-0.5);
00083               t  = k;
00084               hi = x - t*ln2_hi;   /* t*ln2_hi is exact here */
00085               lo = t*ln2_lo;
00086            }
00087            x  = hi - lo;
00088            c  = (hi-x)-lo;
00089        }
00090        else if(hx < 0x33000000) {         /* when |x|<2**-25, return x */
00091            t = huge+x;      /* return x with inexact flags when x!=0 */
00092            return x - (t-(huge+x));
00093        }
00094        else k = 0;
00095 
00096     /* x is now in primary range */
00097        hfx = (float)0.5*x;
00098        hxs = x*hfx;
00099        r1 = one+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5))));
00100        t  = (float)3.0-r1*hfx;
00101        e  = hxs*((r1-t)/((float)6.0 - x*t));
00102        if(k==0) return x - (x*e-hxs);            /* c is 0 */
00103        else {
00104            e  = (x*(e-c)-c);
00105            e -= hxs;
00106            if(k== -1) return (float)0.5*(x-e)-(float)0.5;
00107            if(k==1) {
00108                      if(x < (float)-0.25) return -(float)2.0*(e-(x+(float)0.5));
00109                      else         return  one+(float)2.0*(x-e);
00110            }
00111            if (k <= -2 || k>56) {   /* suffice to return exp(x)-1 */
00112                int32_t i;
00113                y = one-(e-x);
00114               GET_FLOAT_WORD(i,y);
00115               SET_FLOAT_WORD(y,i+(k<<23));       /* add k to y's exponent */
00116                return y-one;
00117            }
00118            t = one;
00119            if(k<23) {
00120                int32_t i;
00121                SET_FLOAT_WORD(t,0x3f800000 - (0x1000000>>k)); /* t=1-2^-k */
00122                      y = t-(e-x);
00123               GET_FLOAT_WORD(i,y);
00124               SET_FLOAT_WORD(y,i+(k<<23));       /* add k to y's exponent */
00125           } else {
00126                int32_t i;
00127               SET_FLOAT_WORD(t,((0x7f-k)<<23));  /* 2^-k */
00128                      y = x-(e+t);
00129                      y += one;
00130               GET_FLOAT_WORD(i,y);
00131               SET_FLOAT_WORD(y,i+(k<<23));       /* add k to y's exponent */
00132            }
00133        }
00134        return y;
00135 }
00136 weak_alias (__expm1f, expm1f)