1*2fe8fb19SBen Gras /* e_expf.c -- float version of e_exp.c.
2*2fe8fb19SBen Gras * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3*2fe8fb19SBen Gras */
4*2fe8fb19SBen Gras
5*2fe8fb19SBen Gras /*
6*2fe8fb19SBen Gras * ====================================================
7*2fe8fb19SBen Gras * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8*2fe8fb19SBen Gras *
9*2fe8fb19SBen Gras * Developed at SunPro, a Sun Microsystems, Inc. business.
10*2fe8fb19SBen Gras * Permission to use, copy, modify, and distribute this
11*2fe8fb19SBen Gras * software is freely granted, provided that this notice
12*2fe8fb19SBen Gras * is preserved.
13*2fe8fb19SBen Gras * ====================================================
14*2fe8fb19SBen Gras */
15*2fe8fb19SBen Gras
16*2fe8fb19SBen Gras #include <sys/cdefs.h>
17*2fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
18*2fe8fb19SBen Gras __RCSID("$NetBSD: e_expf.c,v 1.9 2002/05/26 22:01:49 wiz Exp $");
19*2fe8fb19SBen Gras #endif
20*2fe8fb19SBen Gras
21*2fe8fb19SBen Gras #include "math.h"
22*2fe8fb19SBen Gras #include "math_private.h"
23*2fe8fb19SBen Gras
24*2fe8fb19SBen Gras static const float huge = 1.0e+30;
25*2fe8fb19SBen Gras
26*2fe8fb19SBen Gras static const float
27*2fe8fb19SBen Gras one = 1.0,
28*2fe8fb19SBen Gras halF[2] = {0.5,-0.5,},
29*2fe8fb19SBen Gras twom100 = 7.8886090522e-31, /* 2**-100=0x0d800000 */
30*2fe8fb19SBen Gras o_threshold= 8.8721679688e+01, /* 0x42b17180 */
31*2fe8fb19SBen Gras u_threshold= -1.0397208405e+02, /* 0xc2cff1b5 */
32*2fe8fb19SBen Gras ln2HI[2] ={ 6.9313812256e-01, /* 0x3f317180 */
33*2fe8fb19SBen Gras -6.9313812256e-01,}, /* 0xbf317180 */
34*2fe8fb19SBen Gras ln2LO[2] ={ 9.0580006145e-06, /* 0x3717f7d1 */
35*2fe8fb19SBen Gras -9.0580006145e-06,}, /* 0xb717f7d1 */
36*2fe8fb19SBen Gras invln2 = 1.4426950216e+00, /* 0x3fb8aa3b */
37*2fe8fb19SBen Gras P1 = 1.6666667163e-01, /* 0x3e2aaaab */
38*2fe8fb19SBen Gras P2 = -2.7777778450e-03, /* 0xbb360b61 */
39*2fe8fb19SBen Gras P3 = 6.6137559770e-05, /* 0x388ab355 */
40*2fe8fb19SBen Gras P4 = -1.6533901999e-06, /* 0xb5ddea0e */
41*2fe8fb19SBen Gras P5 = 4.1381369442e-08; /* 0x3331bb4c */
42*2fe8fb19SBen Gras
43*2fe8fb19SBen Gras float
__ieee754_expf(float x)44*2fe8fb19SBen Gras __ieee754_expf(float x) /* default IEEE double exp */
45*2fe8fb19SBen Gras {
46*2fe8fb19SBen Gras float y,hi,lo,c,t;
47*2fe8fb19SBen Gras int32_t k,xsb;
48*2fe8fb19SBen Gras u_int32_t hx;
49*2fe8fb19SBen Gras
50*2fe8fb19SBen Gras hi = lo = 0;
51*2fe8fb19SBen Gras k = 0;
52*2fe8fb19SBen Gras GET_FLOAT_WORD(hx,x);
53*2fe8fb19SBen Gras xsb = (hx>>31)&1; /* sign bit of x */
54*2fe8fb19SBen Gras hx &= 0x7fffffff; /* high word of |x| */
55*2fe8fb19SBen Gras
56*2fe8fb19SBen Gras /* filter out non-finite argument */
57*2fe8fb19SBen Gras if(hx >= 0x42b17218) { /* if |x|>=88.721... */
58*2fe8fb19SBen Gras if(hx>0x7f800000)
59*2fe8fb19SBen Gras return x+x; /* NaN */
60*2fe8fb19SBen Gras if(hx==0x7f800000)
61*2fe8fb19SBen Gras return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */
62*2fe8fb19SBen Gras if(x > o_threshold) return huge*huge; /* overflow */
63*2fe8fb19SBen Gras if(x < u_threshold) return twom100*twom100; /* underflow */
64*2fe8fb19SBen Gras }
65*2fe8fb19SBen Gras
66*2fe8fb19SBen Gras /* argument reduction */
67*2fe8fb19SBen Gras if(hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
68*2fe8fb19SBen Gras if(hx < 0x3F851592) { /* and |x| < 1.5 ln2 */
69*2fe8fb19SBen Gras hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;
70*2fe8fb19SBen Gras } else {
71*2fe8fb19SBen Gras k = invln2*x+halF[xsb];
72*2fe8fb19SBen Gras t = k;
73*2fe8fb19SBen Gras hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */
74*2fe8fb19SBen Gras lo = t*ln2LO[0];
75*2fe8fb19SBen Gras }
76*2fe8fb19SBen Gras x = hi - lo;
77*2fe8fb19SBen Gras }
78*2fe8fb19SBen Gras else if(hx < 0x31800000) { /* when |x|<2**-28 */
79*2fe8fb19SBen Gras if(huge+x>one) return one+x;/* trigger inexact */
80*2fe8fb19SBen Gras }
81*2fe8fb19SBen Gras else k = 0;
82*2fe8fb19SBen Gras
83*2fe8fb19SBen Gras /* x is now in primary range */
84*2fe8fb19SBen Gras t = x*x;
85*2fe8fb19SBen Gras c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
86*2fe8fb19SBen Gras if(k==0) return one-((x*c)/(c-(float)2.0)-x);
87*2fe8fb19SBen Gras else y = one-((lo-(x*c)/((float)2.0-c))-hi);
88*2fe8fb19SBen Gras if(k >= -125) {
89*2fe8fb19SBen Gras u_int32_t hy;
90*2fe8fb19SBen Gras GET_FLOAT_WORD(hy,y);
91*2fe8fb19SBen Gras SET_FLOAT_WORD(y,hy+(k<<23)); /* add k to y's exponent */
92*2fe8fb19SBen Gras return y;
93*2fe8fb19SBen Gras } else {
94*2fe8fb19SBen Gras u_int32_t hy;
95*2fe8fb19SBen Gras GET_FLOAT_WORD(hy,y);
96*2fe8fb19SBen Gras SET_FLOAT_WORD(y,hy+((k+100)<<23)); /* add k to y's exponent */
97*2fe8fb19SBen Gras return y*twom100;
98*2fe8fb19SBen Gras }
99*2fe8fb19SBen Gras }
100