comparison fmgen/e_expf.c @ 0:c55ea9478c80

Hello Gensokyo!
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 21 May 2013 10:29:21 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c55ea9478c80
1 /* e_expf.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 <stdint.h>
17
18 static const float
19 one = 1.0,
20 halF[2] = {0.5,-0.5,},
21 ln2HI[2] ={ 6.9313812256e-01, /* 0x3f317180 */
22 -6.9313812256e-01,}, /* 0xbf317180 */
23 ln2LO[2] ={ 9.0580006145e-06, /* 0x3717f7d1 */
24 -9.0580006145e-06,}, /* 0xb717f7d1 */
25 invln2 = 1.4426950216e+00, /* 0x3fb8aa3b */
26 P1 = 1.6666667163e-01, /* 0x3e2aaaab */
27 P2 = -2.7777778450e-03, /* 0xbb360b61 */
28 P3 = 6.6137559770e-05, /* 0x388ab355 */
29 P4 = -1.6533901999e-06, /* 0xb5ddea0e */
30 P5 = 4.1381369442e-08; /* 0x3331bb4c */
31
32 typedef union {
33 float value;
34 uint32_t word;
35 } _f32;
36
37 /* Get a 32 bit int from a float. */
38 #define GET_FLOAT_WORD(i,d) \
39 do { \
40 _f32 gf_u; \
41 gf_u.value = (d); \
42 (i) = gf_u.word; \
43 } while (0)
44
45 /* Set a float from a 32 bit int. */
46 #define SET_FLOAT_WORD(d,i) \
47 do { \
48 _f32 sf_u; \
49 sf_u.word = (i); \
50 (d) = sf_u.value; \
51 } while (0)
52
53 float expf(float x) /* default IEEE double exp */
54 {
55 float y,hi=0,lo=0,c,t;
56 int32_t k,xsb;
57 uint32_t hx;
58
59 GET_FLOAT_WORD(hx,x);
60 xsb = (hx>>31)&1; /* sign bit of x */
61 hx &= 0x7fffffff; /* high word of |x| */
62
63 /* filter out non-finite argument */
64 if(hx >= 0x42b17218) { /* if |x|>=88.721... */
65 if(hx>0x7f800000)
66 return x+x; /* NaN */
67 if(hx==0x7f800000)
68 return (xsb==0)? x:0.0f; /* exp(+-inf)={inf,0} */
69 }
70
71 /* argument reduction */
72 if(hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
73 if(hx < 0x3F851592) { /* and |x| < 1.5 ln2 */
74 hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;
75 } else {
76 k = invln2*x+halF[xsb];
77 t = k;
78 hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */
79 lo = t*ln2LO[0];
80 }
81 x = hi - lo;
82 }
83 else k = 0;
84
85 /* x is now in primary range */
86 t = x*x;
87 c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
88 if(k==0) return one-((x*c)/(c-(float)2.0)-x);
89 else y = one-((lo-(x*c)/((float)2.0-c))-hi);
90 {
91 uint32_t hy;
92 GET_FLOAT_WORD(hy,y);
93 SET_FLOAT_WORD(y,hy+(k<<23)); /* add k to y's exponent */
94 return y;
95 }
96 }
97