|
0
|
1 //=========================================================================== |
|
|
2 // |
|
|
3 // s_expm1.c |
|
|
4 // |
|
|
5 // Part of the standard mathematical function library |
|
|
6 // |
|
|
7 //=========================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
|
25 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. |
|
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //=========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): jlarmour@cygnus.co.uk |
|
|
33 // Contributors: jlarmour@cygnus.co.uk |
|
|
34 // Date: 1998-02-13 |
|
|
35 // Purpose: |
|
|
36 // Description: |
|
|
37 // Usage: |
|
|
38 // |
|
|
39 //####DESCRIPTIONEND#### |
|
|
40 // |
|
|
41 //=========================================================================== |
|
|
42 |
|
|
43 // CONFIGURATION |
|
|
44 |
|
|
45 #include <pkgconf/libm.h> // Configuration header |
|
|
46 |
|
|
47 // Include the Math library? |
|
|
48 #ifdef CYGPKG_LIBM |
|
|
49 |
|
|
50 // Derived from code with the following copyright |
|
|
51 |
|
|
52 |
|
|
53 /* @(#)s_expm1.c 1.3 95/01/18 */ |
|
|
54 /* |
|
|
55 * ==================================================== |
|
|
56 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
|
|
57 * |
|
|
58 * Developed at SunSoft, a Sun Microsystems, Inc. business. |
|
|
59 * Permission to use, copy, modify, and distribute this |
|
|
60 * software is freely granted, provided that this notice |
|
|
61 * is preserved. |
|
|
62 * ==================================================== |
|
|
63 */ |
|
|
64 |
|
|
65 /* expm1(x) |
|
|
66 * Returns exp(x)-1, the exponential of x minus 1. |
|
|
67 * |
|
|
68 * Method |
|
|
69 * 1. Argument reduction: |
|
|
70 * Given x, find r and integer k such that |
|
|
71 * |
|
|
72 * x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658 |
|
|
73 * |
|
|
74 * Here a correction term c will be computed to compensate |
|
|
75 * the error in r when rounded to a floating-point number. |
|
|
76 * |
|
|
77 * 2. Approximating expm1(r) by a special rational function on |
|
|
78 * the interval [0,0.34658]: |
|
|
79 * Since |
|
|
80 * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ... |
|
|
81 * we define R1(r*r) by |
|
|
82 * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r) |
|
|
83 * That is, |
|
|
84 * R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r) |
|
|
85 * = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r)) |
|
|
86 * = 1 - r^2/60 + r^4/2520 - r^6/100800 + ... |
|
|
87 * We use a special Reme algorithm on [0,0.347] to generate |
|
|
88 * a polynomial of degree 5 in r*r to approximate R1. The |
|
|
89 * maximum error of this polynomial approximation is bounded |
|
|
90 * by 2**-61. In other words, |
|
|
91 * R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5 |
|
|
92 * where Q1 = -1.6666666666666567384E-2, |
|
|
93 * Q2 = 3.9682539681370365873E-4, |
|
|
94 * Q3 = -9.9206344733435987357E-6, |
|
|
95 * Q4 = 2.5051361420808517002E-7, |
|
|
96 * Q5 = -6.2843505682382617102E-9; |
|
|
97 * (where z=r*r, and the values of Q1 to Q5 are listed below) |
|
|
98 * with error bounded by |
|
|
99 * | 5 | -61 |
|
|
100 * | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2 |
|
|
101 * | | |
|
|
102 * |
|
|
103 * expm1(r) = exp(r)-1 is then computed by the following |
|
|
104 * specific way which minimize the accumulation rounding error: |
|
|
105 * 2 3 |
|
|
106 * r r [ 3 - (R1 + R1*r/2) ] |
|
|
107 * expm1(r) = r + --- + --- * [--------------------] |
|
|
108 * 2 2 [ 6 - r*(3 - R1*r/2) ] |
|
|
109 * |
|
|
110 * To compensate the error in the argument reduction, we use |
|
|
111 * expm1(r+c) = expm1(r) + c + expm1(r)*c |
|
|
112 * ~ expm1(r) + c + r*c |
|
|
113 * Thus c+r*c will be added in as the correction terms for |
|
|
114 * expm1(r+c). Now rearrange the term to avoid optimization |
|
|
115 * screw up: |
|
|
116 * ( 2 2 ) |
|
|
117 * ({ ( r [ R1 - (3 - R1*r/2) ] ) } r ) |
|
|
118 * expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- ) |
|
|
119 * ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 ) |
|
|
120 * ( ) |
|
|
121 * |
|
|
122 * = r - E |
|
|
123 * 3. Scale back to obtain expm1(x): |
|
|
124 * From step 1, we have |
|
|
125 * expm1(x) = either 2^k*[expm1(r)+1] - 1 |
|
|
126 * = or 2^k*[expm1(r) + (1-2^-k)] |
|
|
127 * 4. Implementation notes: |
|
|
128 * (A). To save one multiplication, we scale the coefficient Qi |
|
|
129 * to Qi*2^i, and replace z by (x^2)/2. |
|
|
130 * (B). To achieve maximum accuracy, we compute expm1(x) by |
|
|
131 * (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf) |
|
|
132 * (ii) if k=0, return r-E |
|
|
133 * (iii) if k=-1, return 0.5*(r-E)-0.5 |
|
|
134 * (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E) |
|
|
135 * else return 1.0+2.0*(r-E); |
|
|
136 * (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1) |
|
|
137 * (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else |
|
|
138 * (vii) return 2^k(1-((E+2^-k)-r)) |
|
|
139 * |
|
|
140 * Special cases: |
|
|
141 * expm1(INF) is INF, expm1(NaN) is NaN; |
|
|
142 * expm1(-INF) is -1, and |
|
|
143 * for finite argument, only expm1(0)=0 is exact. |
|
|
144 * |
|
|
145 * Accuracy: |
|
|
146 * according to an error analysis, the error is always less than |
|
|
147 * 1 ulp (unit in the last place). |
|
|
148 * |
|
|
149 * Misc. info. |
|
|
150 * For IEEE double |
|
|
151 * if x > 7.09782712893383973096e+02 then expm1(x) overflow |
|
|
152 * |
|
|
153 * Constants: |
|
|
154 * The hexadecimal values are the intended ones for the following |
|
|
155 * constants. The decimal values may be used, provided that the |
|
|
156 * compiler will convert from decimal to binary accurately enough |
|
|
157 * to produce the hexadecimal values shown. |
|
|
158 */ |
|
|
159 |
|
|
160 #include "mathincl/fdlibm.h" |
|
|
161 |
|
|
162 static const double |
|
|
163 one = 1.0, |
|
|
164 huge = 1.0e+300, |
|
|
165 tiny = 1.0e-300, |
|
|
166 o_threshold = 7.09782712893383973096e+02,/* 0x40862E42, 0xFEFA39EF */ |
|
|
167 ln2_hi = 6.93147180369123816490e-01,/* 0x3fe62e42, 0xfee00000 */ |
|
|
168 ln2_lo = 1.90821492927058770002e-10,/* 0x3dea39ef, 0x35793c76 */ |
|
|
169 invln2 = 1.44269504088896338700e+00,/* 0x3ff71547, 0x652b82fe */ |
|
|
170 /* scaled coefficients related to expm1 */ |
|
|
171 Q1 = -3.33333333333331316428e-02, /* BFA11111 111110F4 */ |
|
|
172 Q2 = 1.58730158725481460165e-03, /* 3F5A01A0 19FE5585 */ |
|
|
173 Q3 = -7.93650757867487942473e-05, /* BF14CE19 9EAADBB7 */ |
|
|
174 Q4 = 4.00821782732936239552e-06, /* 3ED0CFCA 86E65239 */ |
|
|
175 Q5 = -2.01099218183624371326e-07; /* BE8AFDB7 6E09C32D */ |
|
|
176 |
|
|
177 double expm1(double x) |
|
|
178 { |
|
|
179 double y,hi,lo,c,t,e,hxs,hfx,r1; |
|
|
180 int k,xsb; |
|
|
181 unsigned hx; |
|
|
182 |
|
|
183 c=0.0; /* placate compiler */ |
|
|
184 hx = CYG_LIBM_HI(x); /* high word of x */ |
|
|
185 xsb = hx&0x80000000; /* sign bit of x */ |
|
|
186 if(xsb==0) y=x; else y= -x; /* y = |x| */ |
|
|
187 hx &= 0x7fffffff; /* high word of |x| */ |
|
|
188 |
|
|
189 /* filter out huge and non-finite argument */ |
|
|
190 if(hx >= 0x4043687A) { /* if |x|>=56*ln2 */ |
|
|
191 if(hx >= 0x40862E42) { /* if |x|>=709.78... */ |
|
|
192 if(hx>=0x7ff00000) { |
|
|
193 if(((hx&0xfffff)|CYG_LIBM_LO(x))!=0) |
|
|
194 return x+x; /* NaN */ |
|
|
195 else return (xsb==0)? x:-1.0;/* exp(+-inf)={inf,-1} */ |
|
|
196 } |
|
|
197 if(x > o_threshold) return huge*huge; /* overflow */ |
|
|
198 } |
|
|
199 if(xsb!=0) { /* x < -56*ln2, return -1.0 with inexact */ |
|
|
200 if(x+tiny<0.0) /* raise inexact */ |
|
|
201 return tiny-one; /* return -1 */ |
|
|
202 } |
|
|
203 } |
|
|
204 |
|
|
205 /* argument reduction */ |
|
|
206 if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ |
|
|
207 if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */ |
|
|
208 if(xsb==0) |
|
|
209 {hi = x - ln2_hi; lo = ln2_lo; k = 1;} |
|
|
210 else |
|
|
211 {hi = x + ln2_hi; lo = -ln2_lo; k = -1;} |
|
|
212 } else { |
|
|
213 k = invln2*x+((xsb==0)?0.5:-0.5); |
|
|
214 t = k; |
|
|
215 hi = x - t*ln2_hi; /* t*ln2_hi is exact here */ |
|
|
216 lo = t*ln2_lo; |
|
|
217 } |
|
|
218 x = hi - lo; |
|
|
219 c = (hi-x)-lo; |
|
|
220 } |
|
|
221 else if(hx < 0x3c900000) { /* when |x|<2**-54, return x */ |
|
|
222 t = huge+x; /* return x with inexact flags when x!=0 */ |
|
|
223 return x - (t-(huge+x)); |
|
|
224 } |
|
|
225 else k = 0; |
|
|
226 |
|
|
227 /* x is now in primary range */ |
|
|
228 hfx = 0.5*x; |
|
|
229 hxs = x*hfx; |
|
|
230 r1 = one+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5)))); |
|
|
231 t = 3.0-r1*hfx; |
|
|
232 e = hxs*((r1-t)/(6.0 - x*t)); |
|
|
233 if(k==0) return x - (x*e-hxs); /* c is 0 */ |
|
|
234 else { |
|
|
235 e = (x*(e-c)-c); |
|
|
236 e -= hxs; |
|
|
237 if(k== -1) return 0.5*(x-e)-0.5; |
|
|
238 if(k==1) { |
|
|
239 if(x < -0.25) return -2.0*(e-(x+0.5)); |
|
|
240 else return one+2.0*(x-e); |
|
|
241 } |
|
|
242 if (k <= -2 || k>56) { /* suffice to return exp(x)-1 */ |
|
|
243 y = one-(e-x); |
|
|
244 CYG_LIBM_HI(y) += (k<<20); /* add k to y's exponent */ |
|
|
245 return y-one; |
|
|
246 } |
|
|
247 t = one; |
|
|
248 if(k<20) { |
|
|
249 CYG_LIBM_HI(t) = 0x3ff00000 - (0x200000>>k); /* t=1-2^-k */ |
|
|
250 y = t-(e-x); |
|
|
251 CYG_LIBM_HI(y) += (k<<20); /* add k to y's exponent */ |
|
|
252 } else { |
|
|
253 CYG_LIBM_HI(t) = ((0x3ff-k)<<20); /* 2^-k */ |
|
|
254 y = x-(e+t); |
|
|
255 y += one; |
|
|
256 CYG_LIBM_HI(y) += (k<<20); /* add k to y's exponent */ |
|
|
257 } |
|
|
258 } |
|
|
259 return y; |
|
|
260 } |
|
|
261 |
|
|
262 #endif // ifdef CYGPKG_LIBM |
|
|
263 |
|
|
264 // EOF s_expm1.c |