File: | real/expm1.c |
Location: | line 167, column 10 |
Description: | Value stored to 'y' is never read |
1 | /* @(#)s_expm1.c 1.5 04/04/22 */ |
2 | |
3 | /* |
4 | * Copyright (c) 2015-2017 Carsten Sonne Larsen <cs@innolan.dk> |
5 | * All rights reserved. |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions |
9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * |
27 | * The origin source code can be obtained from: |
28 | * http://www.netlib.org/fdlibm/s_expm1.c |
29 | * |
30 | */ |
31 | |
32 | /* |
33 | * ==================================================== |
34 | * Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved. |
35 | * |
36 | * Permission to use, copy, modify, and distribute this |
37 | * software is freely granted, provided that this notice |
38 | * is preserved. |
39 | * ==================================================== |
40 | * |
41 | */ |
42 | |
43 | #include "prim.h" |
44 | #include "math.h" |
45 | |
46 | /* expm1(x) |
47 | * Returns exp(x)-1, the exponential of x minus 1. |
48 | * |
49 | * Method |
50 | * 1. Argument reduction: |
51 | * Given x, find r and integer k such that |
52 | * |
53 | * x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658 |
54 | * |
55 | * Here a correction term c will be computed to compensate |
56 | * the error in r when rounded to a floating-point number. |
57 | * |
58 | * 2. Approximating expm1(r) by a special rational function on |
59 | * the interval [0,0.34658]: |
60 | * Since |
61 | * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ... |
62 | * we define R1(r*r) by |
63 | * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r) |
64 | * That is, |
65 | * R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r) |
66 | * = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r)) |
67 | * = 1 - r^2/60 + r^4/2520 - r^6/100800 + ... |
68 | * We use a special Remes algorithm on [0,0.347] to generate |
69 | * a polynomial of degree 5 in r*r to approximate R1. The |
70 | * maximum error of this polynomial approximation is bounded |
71 | * by 2**-61. In other words, |
72 | * R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5 |
73 | * where Q1 = -1.6666666666666567384E-2, |
74 | * Q2 = 3.9682539681370365873E-4, |
75 | * Q3 = -9.9206344733435987357E-6, |
76 | * Q4 = 2.5051361420808517002E-7, |
77 | * Q5 = -6.2843505682382617102E-9; |
78 | * (where z=r*r, and the values of Q1 to Q5 are listed below) |
79 | * with error bounded by |
80 | * | 5 | -61 |
81 | * | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2 |
82 | * | | |
83 | * |
84 | * expm1(r) = exp(r)-1 is then computed by the following |
85 | * specific way which minimize the accumulation rounding error: |
86 | * 2 3 |
87 | * r r [ 3 - (R1 + R1*r/2) ] |
88 | * expm1(r) = r + --- + --- * [--------------------] |
89 | * 2 2 [ 6 - r*(3 - R1*r/2) ] |
90 | * |
91 | * To compensate the error in the argument reduction, we use |
92 | * expm1(r+c) = expm1(r) + c + expm1(r)*c |
93 | * ~ expm1(r) + c + r*c |
94 | * Thus c+r*c will be added in as the correction terms for |
95 | * expm1(r+c). Now rearrange the term to avoid optimization |
96 | * screw up: |
97 | * ( 2 2 ) |
98 | * ({ ( r [ R1 - (3 - R1*r/2) ] ) } r ) |
99 | * expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- ) |
100 | * ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 ) |
101 | * ( ) |
102 | * |
103 | * = r - E |
104 | * 3. Scale back to obtain expm1(x): |
105 | * From step 1, we have |
106 | * expm1(x) = either 2^k*[expm1(r)+1] - 1 |
107 | * = or 2^k*[expm1(r) + (1-2^-k)] |
108 | * 4. Implementation notes: |
109 | * (A). To save one multiplication, we scale the coefficient Qi |
110 | * to Qi*2^i, and replace z by (x^2)/2. |
111 | * (B). To achieve maximum accuracy, we compute expm1(x) by |
112 | * (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf) |
113 | * (ii) if k=0, return r-E |
114 | * (iii) if k=-1, return 0.5*(r-E)-0.5 |
115 | * (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E) |
116 | * else return 1.0+2.0*(r-E); |
117 | * (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1) |
118 | * (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else |
119 | * (vii) return 2^k(1-((E+2^-k)-r)) |
120 | * |
121 | * Special cases: |
122 | * expm1(INF) is INF, expm1(NaN) is NaN; |
123 | * expm1(-INF) is -1, and |
124 | * for finite argument, only expm1(0)=0 is exact. |
125 | * |
126 | * Accuracy: |
127 | * according to an error analysis, the error is always less than |
128 | * 1 ulp (unit in the last place). |
129 | * |
130 | * Misc. info. |
131 | * For IEEE double |
132 | * if x > 7.09782712893383973096e+02 then expm1(x) overflow |
133 | * |
134 | * Constants: |
135 | * The hexadecimal values are the intended ones for the following |
136 | * constants. The decimal values may be used, provided that the |
137 | * compiler will convert from decimal to binary accurately enough |
138 | * to produce the hexadecimal values shown. |
139 | */ |
140 | |
141 | static const double |
142 | one = 1.0, |
143 | huge = 1.0e+300, |
144 | tiny = 1.0e-300, |
145 | o_threshold = 7.09782712893383973096e+02,/* 0x40862E42, 0xFEFA39EF */ |
146 | ln2_hi = 6.93147180369123816490e-01,/* 0x3fe62e42, 0xfee00000 */ |
147 | ln2_lo = 1.90821492927058770002e-10,/* 0x3dea39ef, 0x35793c76 */ |
148 | invln2 = 1.44269504088896338700e+00,/* 0x3ff71547, 0x652b82fe */ |
149 | /* scaled coefficients related to expm1 */ |
150 | Q1 = -3.33333333333331316428e-02, /* BFA11111 111110F4 */ |
151 | Q2 = 1.58730158725481460165e-03, /* 3F5A01A0 19FE5585 */ |
152 | Q3 = -7.93650757867487942473e-05, /* BF14CE19 9EAADBB7 */ |
153 | Q4 = 4.00821782732936239552e-06, /* 3ED0CFCA 86E65239 */ |
154 | Q5 = -2.01099218183624371326e-07; /* BE8AFDB7 6E09C32D */ |
155 | |
156 | double expm1(double x) |
157 | { |
158 | double y,hi,lo,c,t,e,hxs,hfx,r1; |
159 | sword k,xsb; |
160 | uword hx; |
161 | |
162 | c = 0.0; |
163 | |
164 | GET_HIGH_WORD(hx,x)do { ieee_double_shape_type gh_u; gh_u.value = (x); (hx) = gh_u .parts.msw; } while (0); /* high word of x */ |
165 | xsb = hx&0x80000000; /* sign bit of x */ |
166 | if(xsb==0) y=x; |
167 | else y= -x; /* y = |x| */ |
Value stored to 'y' is never read | |
168 | hx &= 0x7fffffff; /* high word of |x| */ |
169 | |
170 | /* filter out huge and non-finite argument */ |
171 | if(hx >= 0x4043687A) { /* if |x|>=56*ln2 */ |
172 | if(hx >= 0x40862E42) { /* if |x|>=709.78... */ |
173 | if(hx>=0x7ff00000) { |
174 | uword low; |
175 | GET_LOW_WORD(low,x)do { ieee_double_shape_type gl_u; gl_u.value = (x); (low) = gl_u .parts.lsw; } while (0); |
176 | if(((hx&0xfffff)|low)!=0) |
177 | return x+x; /* NaN */ |
178 | else return (xsb==0)? x:-1.0;/* exp(+-inf)={inf,-1} */ |
179 | } |
180 | if(x > o_threshold) return huge*huge; /* overflow */ |
181 | } |
182 | if(xsb!=0) { /* x < -56*ln2, return -1.0 with inexact */ |
183 | if(x+tiny<0.0) /* raise inexact */ |
184 | return tiny-one; /* return -1 */ |
185 | } |
186 | } |
187 | |
188 | /* argument reduction */ |
189 | if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ |
190 | if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */ |
191 | if(xsb==0) |
192 | { |
193 | hi = x - ln2_hi; |
194 | lo = ln2_lo; |
195 | k = 1; |
196 | } |
197 | else |
198 | { |
199 | hi = x + ln2_hi; |
200 | lo = -ln2_lo; |
201 | k = -1; |
202 | } |
203 | } else { |
204 | k = (sword)(invln2*x+((xsb==0)?0.5:-0.5)); |
205 | t = k; |
206 | hi = x - t*ln2_hi; /* t*ln2_hi is exact here */ |
207 | lo = t*ln2_lo; |
208 | } |
209 | x = hi - lo; |
210 | c = (hi-x)-lo; |
211 | } |
212 | else if(hx < 0x3c900000) { /* when |x|<2**-54, return x */ |
213 | t = huge+x; /* return x with inexact flags when x!=0 */ |
214 | return x - (t-(huge+x)); |
215 | } |
216 | else k = 0; |
217 | |
218 | /* x is now in primary range */ |
219 | hfx = 0.5*x; |
220 | hxs = x*hfx; |
221 | r1 = one+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5)))); |
222 | t = 3.0-r1*hfx; |
223 | e = hxs*((r1-t)/(6.0 - x*t)); |
224 | if(k==0) return x - (x*e-hxs); /* c is 0 */ |
225 | else { |
226 | e = (x*(e-c)-c); |
227 | e -= hxs; |
228 | if(k== -1) return 0.5*(x-e)-0.5; |
229 | if(k==1) { |
230 | if(x < -0.25) return -2.0*(e-(x+0.5)); |
231 | else return one+2.0*(x-e); |
232 | } |
233 | if (k <= -2 || k>56) { /* suffice to return exp(x)-1 */ |
234 | uword hy; |
235 | |
236 | y = one-(e-x); |
237 | GET_HIGH_WORD(hy,y)do { ieee_double_shape_type gh_u; gh_u.value = (y); (hy) = gh_u .parts.msw; } while (0); |
238 | SET_HIGH_WORD(y, hy + (k<<20))do { ieee_double_shape_type sh_u; sh_u.value = (y); sh_u.parts .msw = (hy + (k<<20)); (y) = sh_u.value; } while (0); /* add k to y's exponent */ |
239 | return y-one; |
240 | } |
241 | t = one; |
242 | if(k<20) { |
243 | uword hy; |
244 | |
245 | SET_HIGH_WORD(t, 0x3ff00000 - (0x200000>>k))do { ieee_double_shape_type sh_u; sh_u.value = (t); sh_u.parts .msw = (0x3ff00000 - (0x200000>>k)); (t) = sh_u.value; } while (0); /* t=1-2^-k */ |
246 | y = t-(e-x); |
247 | GET_HIGH_WORD(hy, y)do { ieee_double_shape_type gh_u; gh_u.value = (y); (hy) = gh_u .parts.msw; } while (0); |
248 | SET_HIGH_WORD(y, hy + (k<<20))do { ieee_double_shape_type sh_u; sh_u.value = (y); sh_u.parts .msw = (hy + (k<<20)); (y) = sh_u.value; } while (0); /* add k to y's exponent */ |
249 | } else { |
250 | uword hy; |
251 | |
252 | SET_HIGH_WORD(t, (0x3ff-k)<<20)do { ieee_double_shape_type sh_u; sh_u.value = (t); sh_u.parts .msw = ((0x3ff-k)<<20); (t) = sh_u.value; } while (0); /* 2^-k */ |
253 | y = x-(e+t); |
254 | y += one; |
255 | GET_HIGH_WORD(hy, y)do { ieee_double_shape_type gh_u; gh_u.value = (y); (hy) = gh_u .parts.msw; } while (0); |
256 | SET_HIGH_WORD(y, hy + (k<<20))do { ieee_double_shape_type sh_u; sh_u.value = (y); sh_u.parts .msw = (hy + (k<<20)); (y) = sh_u.value; } while (0); /* add k to y's exponent */ |
257 | } |
258 | } |
259 | return y; |
260 | } |