POK
e_expf.c
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Fri Jan 30 14:41:34 2009
15  */
16 
17 /* e_expf.c -- float version of e_exp.c.
18  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
19  */
20 
21 /*
22  * ====================================================
23  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
24  *
25  * Developed at SunPro, a Sun Microsystems, Inc. business.
26  * Permission to use, copy, modify, and distribute this
27  * software is freely granted, provided that this notice
28  * is preserved.
29  * ====================================================
30  */
31 
32 #ifdef POK_NEEDS_LIBMATH
33 
34 #include "math_private.h"
35 
36 static const float huge = 1.0e+30;
37 
38 static const float
39 one = 1.0,
40 halF[2] = {0.5,-0.5,},
41 twom100 = 7.8886090522e-31, /* 2**-100=0x0d800000 */
42 o_threshold= 8.8721679688e+01, /* 0x42b17180 */
43 u_threshold= -1.0397208405e+02, /* 0xc2cff1b5 */
44 ln2HI[2] ={ 6.9313812256e-01, /* 0x3f317180 */
45  -6.9313812256e-01,}, /* 0xbf317180 */
46 ln2LO[2] ={ 9.0580006145e-06, /* 0x3717f7d1 */
47  -9.0580006145e-06,}, /* 0xb717f7d1 */
48 invln2 = 1.4426950216e+00, /* 0x3fb8aa3b */
49 P1 = 1.6666667163e-01, /* 0x3e2aaaab */
50 P2 = -2.7777778450e-03, /* 0xbb360b61 */
51 P3 = 6.6137559770e-05, /* 0x388ab355 */
52 P4 = -1.6533901999e-06, /* 0xb5ddea0e */
53 P5 = 4.1381369442e-08; /* 0x3331bb4c */
54 
55 float
56 __ieee754_expf(float x) /* default IEEE double exp */
57 {
58  float y,hi,lo,c,t;
59  int32_t k,xsb;
60  uint32_t hx;
61 
62  hi = lo = 0;
63  k = 0;
64  GET_FLOAT_WORD(hx,x);
65  xsb = (hx>>31)&1; /* sign bit of x */
66  hx &= 0x7fffffff; /* high word of |x| */
67 
68  /* filter out non-finite argument */
69  if(hx >= 0x42b17218) { /* if |x|>=88.721... */
70  if(hx>0x7f800000)
71  return x+x; /* NaN */
72  if(hx==0x7f800000)
73  return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */
74  if(x > o_threshold) return huge*huge; /* overflow */
75  if(x < u_threshold) return twom100*twom100; /* underflow */
76  }
77 
78  /* argument reduction */
79  if(hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
80  if(hx < 0x3F851592) { /* and |x| < 1.5 ln2 */
81  hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;
82  } else {
83  k = invln2*x+halF[xsb];
84  t = k;
85  hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */
86  lo = t*ln2LO[0];
87  }
88  x = hi - lo;
89  }
90  else if(hx < 0x31800000) { /* when |x|<2**-28 */
91  if(huge+x>one) return one+x;/* trigger inexact */
92  }
93  else k = 0;
94 
95  /* x is now in primary range */
96  t = x*x;
97  c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
98  if(k==0) return one-((x*c)/(c-(float)2.0)-x);
99  else y = one-((lo-(x*c)/((float)2.0-c))-hi);
100  if(k >= -125) {
101  uint32_t hy;
102  GET_FLOAT_WORD(hy,y);
103  SET_FLOAT_WORD(y,hy+(k<<23)); /* add k to y's exponent */
104  return y;
105  } else {
106  uint32_t hy;
107  GET_FLOAT_WORD(hy,y);
108  SET_FLOAT_WORD(y,hy+((k+100)<<23)); /* add k to y's exponent */
109  return y*twom100;
110  }
111 }
112 #endif
113