POK
e_exp.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_exp.c 5.1 93/09/24 */
18 /*
19  * ====================================================
20  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
21  *
22  * Developed at SunPro, a Sun Microsystems, Inc. business.
23  * Permission to use, copy, modify, and distribute this
24  * software is freely granted, provided that this notice
25  * is preserved.
26  * ====================================================
27  */
28 
29 /* __ieee754_exp(x)
30  * Returns the exponential of x.
31  *
32  * Method
33  * 1. Argument reduction:
34  * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
35  * Given x, find r and integer k such that
36  *
37  * x = k*ln2 + r, |r| <= 0.5*ln2.
38  *
39  * Here r will be represented as r = hi-lo for better
40  * accuracy.
41  *
42  * 2. Approximation of exp(r) by a special rational function on
43  * the interval [0,0.34658]:
44  * Write
45  * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
46  * We use a special Reme algorithm on [0,0.34658] to generate
47  * a polynomial of degree 5 to approximate R. The maximum error
48  * of this polynomial approximation is bounded by 2**-59. In
49  * other words,
50  * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
51  * (where z=r*r, and the values of P1 to P5 are listed below)
52  * and
53  * | 5 | -59
54  * | 2.0+P1*z+...+P5*z - R(z) | <= 2
55  * | |
56  * The computation of exp(r) thus becomes
57  * 2*r
58  * exp(r) = 1 + -------
59  * R - r
60  * r*R1(r)
61  * = 1 + r + ----------- (for better accuracy)
62  * 2 - R1(r)
63  * where
64  * 2 4 10
65  * R1(r) = r - (P1*r + P2*r + ... + P5*r ).
66  *
67  * 3. Scale back to obtain exp(x):
68  * From step 1, we have
69  * exp(x) = 2^k * exp(r)
70  *
71  * Special cases:
72  * exp(INF) is INF, exp(NaN) is NaN;
73  * exp(-INF) is 0, and
74  * for finite argument, only exp(0)=1 is exact.
75  *
76  * Accuracy:
77  * according to an error analysis, the error is always less than
78  * 1 ulp (unit in the last place).
79  *
80  * Misc. info.
81  * For IEEE double
82  * if x > 7.09782712893383973096e+02 then exp(x) overflow
83  * if x < -7.45133219101941108420e+02 then exp(x) underflow
84  *
85  * Constants:
86  * The hexadecimal values are the intended ones for the following
87  * constants. The decimal values may be used, provided that the
88  * compiler will convert from decimal to binary accurately enough
89  * to produce the hexadecimal values shown.
90  */
91 
92 #ifdef POK_NEEDS_LIBMATH
93 
94 #include "math_private.h"
95 
96 static const double
97 one = 1.0,
98 halF[2] = {0.5,-0.5,},
99 huge = 1.0e+300,
100 twom1000= 9.33263618503218878990e-302, /* 2**-1000=0x01700000,0*/
101 o_threshold= 7.09782712893383973096e+02, /* 0x40862E42, 0xFEFA39EF */
102 u_threshold= -7.45133219101941108420e+02, /* 0xc0874910, 0xD52D3051 */
103 ln2HI[2] ={ 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */
104  -6.93147180369123816490e-01,},/* 0xbfe62e42, 0xfee00000 */
105 ln2LO[2] ={ 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */
106  -1.90821492927058770002e-10,},/* 0xbdea39ef, 0x35793c76 */
107 invln2 = 1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */
108 P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */
109 P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */
110 P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */
111 P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */
112 P5 = 4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */
113 
114 
115 double
116 __ieee754_exp(double x) /* default IEEE double exp */
117 {
118  double y,hi,lo,c,t;
119  int32_t k,xsb;
120  uint32_t hx;
121 
122  hi = lo = 0;
123  k = 0;
124  GET_HIGH_WORD(hx,x);
125  xsb = (hx>>31)&1; /* sign bit of x */
126  hx &= 0x7fffffff; /* high word of |x| */
127 
128  /* filter out non-finite argument */
129  if(hx >= 0x40862E42) { /* if |x|>=709.78... */
130  if(hx>=0x7ff00000) {
131  uint32_t lx;
132  GET_LOW_WORD(lx,x);
133  if(((hx&0xfffff)|lx)!=0)
134  return x+x; /* NaN */
135  else return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */
136  }
137  if(x > o_threshold) return huge*huge; /* overflow */
138  if(x < u_threshold) return twom1000*twom1000; /* underflow */
139  }
140 
141  /* argument reduction */
142  if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */
143  if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */
144  hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;
145  } else {
146  k = invln2*x+halF[xsb];
147  t = k;
148  hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */
149  lo = t*ln2LO[0];
150  }
151  x = hi - lo;
152  }
153  else if(hx < 0x3e300000) { /* when |x|<2**-28 */
154  if(huge+x>one) return one+x;/* trigger inexact */
155  }
156  else k = 0;
157 
158  /* x is now in primary range */
159  t = x*x;
160  c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
161  if(k==0) return one-((x*c)/(c-2.0)-x);
162  else y = one-((lo-(x*c)/(2.0-c))-hi);
163  if(k >= -1021) {
164  uint32_t hy;
165  GET_HIGH_WORD(hy,y);
166  SET_HIGH_WORD(y,hy+(k<<20)); /* add k to y's exponent */
167  return y;
168  } else {
169  uint32_t hy;
170  GET_HIGH_WORD(hy,y);
171  SET_HIGH_WORD(y,hy+((k+1000)<<20)); /* add k to y's exponent */
172  return y*twom1000;
173  }
174 }
175 #endif