13a8617a8SJordan K. Hubbard /* e_expf.c -- float version of e_exp.c. 23a8617a8SJordan K. Hubbard * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. 33a8617a8SJordan K. Hubbard */ 43a8617a8SJordan K. Hubbard 53a8617a8SJordan K. Hubbard /* 63a8617a8SJordan K. Hubbard * ==================================================== 73a8617a8SJordan K. Hubbard * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 83a8617a8SJordan K. Hubbard * 93a8617a8SJordan K. Hubbard * Developed at SunPro, a Sun Microsystems, Inc. business. 103a8617a8SJordan K. Hubbard * Permission to use, copy, modify, and distribute this 113a8617a8SJordan K. Hubbard * software is freely granted, provided that this notice 123a8617a8SJordan K. Hubbard * is preserved. 133a8617a8SJordan K. Hubbard * ==================================================== 143a8617a8SJordan K. Hubbard */ 153a8617a8SJordan K. Hubbard 163a8617a8SJordan K. Hubbard #ifndef lint 177f3dea24SPeter Wemm static char rcsid[] = "$FreeBSD$"; 183a8617a8SJordan K. Hubbard #endif 193a8617a8SJordan K. Hubbard 203a8617a8SJordan K. Hubbard #include "math.h" 213a8617a8SJordan K. Hubbard #include "math_private.h" 223a8617a8SJordan K. Hubbard 233a8617a8SJordan K. Hubbard static const float 243a8617a8SJordan K. Hubbard one = 1.0, 253a8617a8SJordan K. Hubbard halF[2] = {0.5,-0.5,}, 263a8617a8SJordan K. Hubbard huge = 1.0e+30, 273a8617a8SJordan K. Hubbard o_threshold= 8.8721679688e+01, /* 0x42b17180 */ 283a8617a8SJordan K. Hubbard u_threshold= -1.0397208405e+02, /* 0xc2cff1b5 */ 2990880193SBruce Evans ln2HI[2] ={ 6.9314575195e-01, /* 0x3f317200 */ 3090880193SBruce Evans -6.9314575195e-01,}, /* 0xbf317200 */ 3190880193SBruce Evans ln2LO[2] ={ 1.4286067653e-06, /* 0x35bfbe8e */ 3290880193SBruce Evans -1.4286067653e-06,}, /* 0xb5bfbe8e */ 333a8617a8SJordan K. Hubbard invln2 = 1.4426950216e+00, /* 0x3fb8aa3b */ 343a8617a8SJordan K. Hubbard P1 = 1.6666667163e-01, /* 0x3e2aaaab */ 353a8617a8SJordan K. Hubbard P2 = -2.7777778450e-03, /* 0xbb360b61 */ 363a8617a8SJordan K. Hubbard P3 = 6.6137559770e-05, /* 0x388ab355 */ 373a8617a8SJordan K. Hubbard P4 = -1.6533901999e-06, /* 0xb5ddea0e */ 383a8617a8SJordan K. Hubbard P5 = 4.1381369442e-08; /* 0x3331bb4c */ 393a8617a8SJordan K. Hubbard 4086c2e0c0SDavid Schultz static volatile float twom100 = 7.8886090522e-31; /* 2**-100=0x0d800000 */ 4186c2e0c0SDavid Schultz 42a82bbc73SAlfred Perlstein float 43aa28340dSDavid Schultz __ieee754_expf(float x) /* default IEEE double exp */ 443a8617a8SJordan K. Hubbard { 45aa28340dSDavid Schultz float y,hi=0.0,lo=0.0,c,t; 4651295a4dSJordan K. Hubbard int32_t k=0,xsb; 473a8617a8SJordan K. Hubbard u_int32_t hx; 483a8617a8SJordan K. Hubbard 493a8617a8SJordan K. Hubbard GET_FLOAT_WORD(hx,x); 503a8617a8SJordan K. Hubbard xsb = (hx>>31)&1; /* sign bit of x */ 51aa28340dSDavid Schultz hx &= 0x7fffffff; /* high word of |x| */ 523a8617a8SJordan K. Hubbard 533a8617a8SJordan K. Hubbard /* filter out non-finite argument */ 543a8617a8SJordan K. Hubbard if(hx >= 0x42b17218) { /* if |x|>=88.721... */ 553a8617a8SJordan K. Hubbard if(hx>0x7f800000) 563a8617a8SJordan K. Hubbard return x+x; /* NaN */ 573a8617a8SJordan K. Hubbard if(hx==0x7f800000) 583a8617a8SJordan K. Hubbard return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */ 593a8617a8SJordan K. Hubbard if(x > o_threshold) return huge*huge; /* overflow */ 603a8617a8SJordan K. Hubbard if(x < u_threshold) return twom100*twom100; /* underflow */ 613a8617a8SJordan K. Hubbard } 623a8617a8SJordan K. Hubbard 633a8617a8SJordan K. Hubbard /* argument reduction */ 643a8617a8SJordan K. Hubbard if(hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */ 653a8617a8SJordan K. Hubbard if(hx < 0x3F851592) { /* and |x| < 1.5 ln2 */ 66aa28340dSDavid Schultz hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb; 673a8617a8SJordan K. Hubbard } else { 683a8617a8SJordan K. Hubbard k = invln2*x+halF[xsb]; 693a8617a8SJordan K. Hubbard t = k; 70aa28340dSDavid Schultz hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */ 71aa28340dSDavid Schultz lo = t*ln2LO[0]; 723a8617a8SJordan K. Hubbard } 73aa28340dSDavid Schultz x = hi - lo; 743a8617a8SJordan K. Hubbard } 753a8617a8SJordan K. Hubbard else if(hx < 0x31800000) { /* when |x|<2**-28 */ 763a8617a8SJordan K. Hubbard if(huge+x>one) return one+x;/* trigger inexact */ 773a8617a8SJordan K. Hubbard } 783a8617a8SJordan K. Hubbard else k = 0; 793a8617a8SJordan K. Hubbard 803a8617a8SJordan K. Hubbard /* x is now in primary range */ 813a8617a8SJordan K. Hubbard t = x*x; 823a8617a8SJordan K. Hubbard c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); 83aa28340dSDavid Schultz if(k==0) return one-((x*c)/(c-(float)2.0)-x); 84aa28340dSDavid Schultz else y = one-((lo-(x*c)/((float)2.0-c))-hi); 853a8617a8SJordan K. Hubbard if(k >= -125) { 863a8617a8SJordan K. Hubbard u_int32_t hy; 873a8617a8SJordan K. Hubbard GET_FLOAT_WORD(hy,y); 883a8617a8SJordan K. Hubbard SET_FLOAT_WORD(y,hy+(k<<23)); /* add k to y's exponent */ 893a8617a8SJordan K. Hubbard return y; 903a8617a8SJordan K. Hubbard } else { 913a8617a8SJordan K. Hubbard u_int32_t hy; 923a8617a8SJordan K. Hubbard GET_FLOAT_WORD(hy,y); 933a8617a8SJordan K. Hubbard SET_FLOAT_WORD(y,hy+((k+100)<<23)); /* add k to y's exponent */ 943a8617a8SJordan K. Hubbard return y*twom100; 953a8617a8SJordan K. Hubbard } 963a8617a8SJordan K. Hubbard } 97