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