e_expf.c revision 50476
12116Sjkh/* e_expf.c -- float version of e_exp.c. 22116Sjkh * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. 32116Sjkh */ 42116Sjkh 52116Sjkh/* 62116Sjkh * ==================================================== 72116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 82116Sjkh * 92116Sjkh * Developed at SunPro, a Sun Microsystems, Inc. business. 102116Sjkh * Permission to use, copy, modify, and distribute this 118870Srgrimes * software is freely granted, provided that this notice 122116Sjkh * is preserved. 132116Sjkh * ==================================================== 142116Sjkh */ 152116Sjkh 162116Sjkh#ifndef lint 1750476Speterstatic char rcsid[] = "$FreeBSD: head/lib/msun/src/e_expf.c 50476 1999-08-28 00:22:10Z peter $"; 182116Sjkh#endif 192116Sjkh 202116Sjkh#include "math.h" 212116Sjkh#include "math_private.h" 222116Sjkh 232116Sjkh#ifdef __STDC__ 242116Sjkhstatic const float 252116Sjkh#else 262116Sjkhstatic float 272116Sjkh#endif 282116Sjkhone = 1.0, 292116SjkhhalF[2] = {0.5,-0.5,}, 302116Sjkhhuge = 1.0e+30, 312116Sjkhtwom100 = 7.8886090522e-31, /* 2**-100=0x0d800000 */ 322116Sjkho_threshold= 8.8721679688e+01, /* 0x42b17180 */ 332116Sjkhu_threshold= -1.0397208405e+02, /* 0xc2cff1b5 */ 342116Sjkhln2HI[2] ={ 6.9313812256e-01, /* 0x3f317180 */ 352116Sjkh -6.9313812256e-01,}, /* 0xbf317180 */ 362116Sjkhln2LO[2] ={ 9.0580006145e-06, /* 0x3717f7d1 */ 372116Sjkh -9.0580006145e-06,}, /* 0xb717f7d1 */ 382116Sjkhinvln2 = 1.4426950216e+00, /* 0x3fb8aa3b */ 392116SjkhP1 = 1.6666667163e-01, /* 0x3e2aaaab */ 402116SjkhP2 = -2.7777778450e-03, /* 0xbb360b61 */ 412116SjkhP3 = 6.6137559770e-05, /* 0x388ab355 */ 422116SjkhP4 = -1.6533901999e-06, /* 0xb5ddea0e */ 432116SjkhP5 = 4.1381369442e-08; /* 0x3331bb4c */ 442116Sjkh 452116Sjkh#ifdef __STDC__ 462116Sjkh float __ieee754_expf(float x) /* default IEEE double exp */ 472116Sjkh#else 482116Sjkh float __ieee754_expf(x) /* default IEEE double exp */ 492116Sjkh float x; 502116Sjkh#endif 512116Sjkh{ 5217141Sjkh float y,hi=0.0,lo=0.0,c,t; 5317141Sjkh int32_t k=0,xsb; 542116Sjkh u_int32_t hx; 552116Sjkh 562116Sjkh GET_FLOAT_WORD(hx,x); 572116Sjkh xsb = (hx>>31)&1; /* sign bit of x */ 582116Sjkh hx &= 0x7fffffff; /* high word of |x| */ 592116Sjkh 602116Sjkh /* filter out non-finite argument */ 612116Sjkh if(hx >= 0x42b17218) { /* if |x|>=88.721... */ 622116Sjkh if(hx>0x7f800000) 632116Sjkh return x+x; /* NaN */ 642116Sjkh if(hx==0x7f800000) 652116Sjkh return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */ 662116Sjkh if(x > o_threshold) return huge*huge; /* overflow */ 672116Sjkh if(x < u_threshold) return twom100*twom100; /* underflow */ 682116Sjkh } 692116Sjkh 702116Sjkh /* argument reduction */ 718870Srgrimes if(hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */ 722116Sjkh if(hx < 0x3F851592) { /* and |x| < 1.5 ln2 */ 732116Sjkh hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb; 742116Sjkh } else { 752116Sjkh k = invln2*x+halF[xsb]; 762116Sjkh t = k; 772116Sjkh hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */ 782116Sjkh lo = t*ln2LO[0]; 792116Sjkh } 802116Sjkh x = hi - lo; 818870Srgrimes } 822116Sjkh else if(hx < 0x31800000) { /* when |x|<2**-28 */ 832116Sjkh if(huge+x>one) return one+x;/* trigger inexact */ 842116Sjkh } 852116Sjkh else k = 0; 862116Sjkh 872116Sjkh /* x is now in primary range */ 882116Sjkh t = x*x; 892116Sjkh c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); 908870Srgrimes if(k==0) return one-((x*c)/(c-(float)2.0)-x); 912116Sjkh else y = one-((lo-(x*c)/((float)2.0-c))-hi); 922116Sjkh if(k >= -125) { 932116Sjkh u_int32_t hy; 942116Sjkh GET_FLOAT_WORD(hy,y); 952116Sjkh SET_FLOAT_WORD(y,hy+(k<<23)); /* add k to y's exponent */ 962116Sjkh return y; 972116Sjkh } else { 982116Sjkh u_int32_t hy; 992116Sjkh GET_FLOAT_WORD(hy,y); 1002116Sjkh SET_FLOAT_WORD(y,hy+((k+100)<<23)); /* add k to y's exponent */ 1012116Sjkh return y*twom100; 1022116Sjkh } 1032116Sjkh} 104