File: | real/pow.c |
Location: | line 148, column 5 |
Description: | Value stored to 'i1' is never read |
1 | /* @(#)e_pow.c 1.5 04/04/22 SMI */ |
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/e_pow.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 | #include "prim.h" |
43 | #include "math.h" |
44 | |
45 | #ifdef __clang__1 |
46 | # pragma clang diagnostic ignored "-Wunused-variable" |
47 | # pragma clang diagnostic ignored "-Wstrict-aliasing" |
48 | #elif defined(__GNUC__4) && __GNUC__4 > 2 |
49 | # pragma GCC diagnostic ignored "-Wunused-but-set-variable" |
50 | # pragma GCC diagnostic ignored "-Wstrict-aliasing" |
51 | #endif |
52 | |
53 | static const double |
54 | bp[] = {1.0, 1.5,}, |
55 | dp_h[] = { 0.0, 5.84962487220764160156e-01,}, /* 0x3FE2B803, 0x40000000 */ |
56 | dp_l[] = { 0.0, 1.35003920212974897128e-08,}, /* 0x3E4CFDEB, 0x43CFD006 */ |
57 | zero = 0.0, |
58 | one = 1.0, |
59 | two = 2.0, |
60 | two53 = 9007199254740992.0, /* 0x43400000, 0x00000000 */ |
61 | huge = 1.0e300, |
62 | tiny = 1.0e-300, |
63 | /* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */ |
64 | L1 = 5.99999999999994648725e-01, /* 0x3FE33333, 0x33333303 */ |
65 | L2 = 4.28571428578550184252e-01, /* 0x3FDB6DB6, 0xDB6FABFF */ |
66 | L3 = 3.33333329818377432918e-01, /* 0x3FD55555, 0x518F264D */ |
67 | L4 = 2.72728123808534006489e-01, /* 0x3FD17460, 0xA91D4101 */ |
68 | L5 = 2.30660745775561754067e-01, /* 0x3FCD864A, 0x93C9DB65 */ |
69 | L6 = 2.06975017800338417784e-01, /* 0x3FCA7E28, 0x4A454EEF */ |
70 | P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */ |
71 | P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */ |
72 | P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */ |
73 | P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */ |
74 | P5 = 4.13813679705723846039e-08, /* 0x3E663769, 0x72BEA4D0 */ |
75 | lg2 = 6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */ |
76 | lg2_h = 6.93147182464599609375e-01, /* 0x3FE62E43, 0x00000000 */ |
77 | lg2_l = -1.90465429995776804525e-09, /* 0xBE205C61, 0x0CA86C39 */ |
78 | ovt = 8.0085662595372944372e-0017, /* -(1024-log2(ovfl+.5ulp)) */ |
79 | cp = 9.61796693925975554329e-01, /* 0x3FEEC709, 0xDC3A03FD =2/(3ln2) */ |
80 | cp_h = 9.61796700954437255859e-01, /* 0x3FEEC709, 0xE0000000 =(float)cp */ |
81 | cp_l = -7.02846165095275826516e-09, /* 0xBE3E2FE0, 0x145B01F5 =tail of cp_h*/ |
82 | ivln2 = 1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE =1/ln2 */ |
83 | ivln2_h = 1.44269502162933349609e+00, /* 0x3FF71547, 0x60000000 =24b 1/ln2*/ |
84 | ivln2_l = 1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/ |
85 | |
86 | /** |
87 | * @brief Expontation function. |
88 | * @version 1.3 |
89 | * @date 95/01/18 |
90 | * @details |
91 | * <pre> |
92 | * Method: Let x = 2 * (1+f) |
93 | * 1. Compute and return log2(x) in two pieces: |
94 | * log2(x) = w1 + w2, |
95 | * where w1 has 53-24 = 29 bit trailing zeros. |
96 | * 2. Perform y*log2(x) = n+y' by simulating muti-precision |
97 | * arithmetic, where |y'|<=0.5. |
98 | * 3. Return x**y = 2**n*exp(y'*log2) |
99 | * |
100 | * Special cases: |
101 | * 1. (anything) ** 0 is 1 |
102 | * 2. (anything) ** 1 is itself |
103 | * 3. (anything) ** NAN is NAN |
104 | * 4. NAN ** (anything except 0) is NAN |
105 | * 5. +-(|x| > 1) ** +INF is +INF |
106 | * 6. +-(|x| > 1) ** -INF is +0 |
107 | * 7. +-(|x| < 1) ** +INF is +0 |
108 | * 8. +-(|x| < 1) ** -INF is +INF |
109 | * 9. +-1 ** +-INF is NAN |
110 | * 10. +0 ** (+anything except 0, NAN) is +0 |
111 | * 11. -0 ** (+anything except 0, NAN, odd integer) is +0 |
112 | * 12. +0 ** (-anything except 0, NAN) is +INF |
113 | * 13. -0 ** (-anything except 0, NAN, odd integer) is +INF |
114 | * 14. -0 ** (odd integer) = -( +0 ** (odd integer) ) |
115 | * 15. +INF ** (+anything except 0,NAN) is +INF |
116 | * 16. +INF ** (-anything except 0,NAN) is +0 |
117 | * 17. -INF ** (anything) = -0 ** (-anything) |
118 | * 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer) |
119 | * 19. (-anything except 0 and inf) ** (non-integer) is NAN |
120 | * |
121 | * Accuracy: |
122 | * pow(x,y) returns x**y nearly rounded. In particular |
123 | * pow(integer,integer) |
124 | * always returns the correct integer provided it is |
125 | * representable. |
126 | * |
127 | * Constants : |
128 | * The hexadecimal values are the intended ones for the following |
129 | * constants. The decimal values may be used, provided that the |
130 | * compiler will convert from decimal to binary accurately enough |
131 | * to produce the hexadecimal values shown. |
132 | * </pre> |
133 | * @copyright Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
134 | * @license Developed at SunSoft, a Sun Microsystems, Inc. business. Permission |
135 | * to use, copy, modify, and distribute this software is freely granted, |
136 | * provided that this notice is preserved. |
137 | */ |
138 | |
139 | double pow(double x, double y) |
140 | { |
141 | double z,ax,z_h,z_l,p_h,p_l; |
142 | double y1,t1,t2,r,s,t,u,v,w; |
143 | sword i0,i1,i,j,k,yisint,n; |
144 | sword hx,hy,ix,iy; |
145 | uword lx,ly; |
146 | |
147 | i0 = ((*(int*)&one)>>29)^1; |
148 | i1=1-i0; |
Value stored to 'i1' is never read | |
149 | EXTRACT_WORDS(hx,lx,x)do { ieee_double_shape_type ew_u; ew_u.value = (x); (hx) = ew_u .parts.msw; (lx) = ew_u.parts.lsw; } while (0); |
150 | EXTRACT_WORDS(hy,ly,y)do { ieee_double_shape_type ew_u; ew_u.value = (y); (hy) = ew_u .parts.msw; (ly) = ew_u.parts.lsw; } while (0); |
151 | ix = hx&0x7fffffff; |
152 | iy = hy&0x7fffffff; |
153 | |
154 | /* y==zero: x**0 = 1 */ |
155 | if((iy|ly)==0) return one; |
156 | |
157 | /* +-NaN return x+y */ |
158 | if(ix > 0x7ff00000 || ((ix==0x7ff00000)&&(lx!=0)) || |
159 | iy > 0x7ff00000 || ((iy==0x7ff00000)&&(ly!=0))) |
160 | return x+y; |
161 | |
162 | /* determine if y is an odd int when x < 0 |
163 | * yisint = 0 ... y is not an integer |
164 | * yisint = 1 ... y is an odd int |
165 | * yisint = 2 ... y is an even int |
166 | */ |
167 | yisint = 0; |
168 | if(hx<0) { |
169 | if(iy>=0x43400000) yisint = 2; /* even integer y */ |
170 | else if(iy>=0x3ff00000) { |
171 | k = (iy>>20)-0x3ff; /* exponent */ |
172 | if(k>20) { |
173 | j = ly>>(52-k); |
174 | if((uword)(j<<(52-k))==ly) yisint = 2-(j&1); |
175 | } else if(ly==0) { |
176 | j = iy>>(20-k); |
177 | if((j<<(20-k))==iy) yisint = 2-(j&1); |
178 | } |
179 | } |
180 | } |
181 | |
182 | /* special value of y */ |
183 | if(ly==0) { |
184 | if (iy==0x7ff00000) { /* y is +-inf */ |
185 | if(((ix-0x3ff00000)|lx)==0) |
186 | return y - y; /* inf**+-1 is NaN */ |
187 | else if (ix >= 0x3ff00000)/* (|x|>1)**+-inf = inf,0 */ |
188 | return (hy>=0)? y: zero; |
189 | else /* (|x|<1)**-,+inf = inf,0 */ |
190 | return (hy<0)?-y: zero; |
191 | } |
192 | if(iy==0x3ff00000) { /* y is +-1 */ |
193 | if(hy<0) return one/x; |
194 | else return x; |
195 | } |
196 | if(hy==0x40000000) return x*x; /* y is 2 */ |
197 | if(hy==0x3fe00000) { /* y is 0.5 */ |
198 | if(hx>=0) /* x >= +0 */ |
199 | return sqrt(x); |
200 | } |
201 | } |
202 | |
203 | ax = fabs(x); |
204 | /* special value of x */ |
205 | if(lx==0) { |
206 | if(ix==0x7ff00000||ix==0||ix==0x3ff00000) { |
207 | z = ax; /*x is +-0,+-inf,+-1*/ |
208 | if(hy<0) z = one/z; /* z = (1/|x|) */ |
209 | if(hx<0) { |
210 | if(((ix-0x3ff00000)|yisint)==0) { |
211 | z = (z-z)/(z-z); /* (-1)**non-int is NaN */ |
212 | } else if(yisint==1) |
213 | z = -z; /* (x<0)**odd = -(|x|**odd) */ |
214 | } |
215 | return z; |
216 | } |
217 | } |
218 | |
219 | n = (hx>>31)+1; |
220 | |
221 | /* (x<0)**(non-int) is NaN */ |
222 | if((n|yisint)==0) return (x-x)/(x-x); |
223 | |
224 | s = one; /* s (sign of result -ve**odd) = -1 else = 1 */ |
225 | if((n|(yisint-1))==0) s = -one;/* (-ve)**(odd int) */ |
226 | |
227 | /* |y| is huge */ |
228 | if(iy>0x41e00000) { /* if |y| > 2**31 */ |
229 | if(iy>0x43f00000) { /* if |y| > 2**64, must o/uflow */ |
230 | if(ix<=0x3fefffff) return (hy<0)? huge*huge:tiny*tiny; |
231 | if(ix>=0x3ff00000) return (hy>0)? huge*huge:tiny*tiny; |
232 | } |
233 | /* over/underflow if x is not close to one */ |
234 | if(ix<0x3fefffff) return (hy<0)? s*huge*huge:s*tiny*tiny; |
235 | if(ix>0x3ff00000) return (hy>0)? s*huge*huge:s*tiny*tiny; |
236 | /* now |1-x| is tiny <= 2**-20, suffice to compute |
237 | log(x) by x-x^2/2+x^3/3-x^4/4 */ |
238 | t = ax-one; /* t has 20 trailing zeros */ |
239 | w = (t*t)*(0.5-t*(0.3333333333333333333333-t*0.25)); |
240 | u = ivln2_h*t; /* ivln2_h has 21 sig. bits */ |
241 | v = t*ivln2_l-w*ivln2; |
242 | t1 = u+v; |
243 | SET_LOW_WORD(t1,0)do { ieee_double_shape_type sl_u; sl_u.value = (t1); sl_u.parts .lsw = (0); (t1) = sl_u.value; } while (0); |
244 | t2 = v-(t1-u); |
245 | } else { |
246 | double ss,s2,s_h,s_l,t_h,t_l; |
247 | n = 0; |
248 | /* take care subnormal number */ |
249 | if(ix<0x00100000) |
250 | { |
251 | ax *= two53; |
252 | n -= 53; |
253 | GET_HIGH_WORD(ix,ax)do { ieee_double_shape_type gh_u; gh_u.value = (ax); (ix) = gh_u .parts.msw; } while (0); |
254 | } |
255 | n += ((ix)>>20)-0x3ff; |
256 | j = ix&0x000fffff; |
257 | /* determine interval */ |
258 | ix = j|0x3ff00000; /* normalize ix */ |
259 | if(j<=0x3988E) k=0; /* |x|<sqrt(3/2) */ |
260 | else if(j<0xBB67A) k=1; /* |x|<sqrt(3) */ |
261 | else { |
262 | k=0; |
263 | n+=1; |
264 | ix -= 0x00100000; |
265 | } |
266 | SET_HIGH_WORD(ax,ix)do { ieee_double_shape_type sh_u; sh_u.value = (ax); sh_u.parts .msw = (ix); (ax) = sh_u.value; } while (0); |
267 | |
268 | /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */ |
269 | u = ax-bp[k]; /* bp[0]=1.0, bp[1]=1.5 */ |
270 | v = one/(ax+bp[k]); |
271 | ss = u*v; |
272 | s_h = ss; |
273 | SET_LOW_WORD(s_h,0)do { ieee_double_shape_type sl_u; sl_u.value = (s_h); sl_u.parts .lsw = (0); (s_h) = sl_u.value; } while (0); |
274 | /* t_h=ax+bp[k] High */ |
275 | t_h = zero; |
276 | SET_HIGH_WORD(t_h,((ix>>1)|0x20000000)+0x00080000+(k<<18))do { ieee_double_shape_type sh_u; sh_u.value = (t_h); sh_u.parts .msw = (((ix>>1)|0x20000000)+0x00080000+(k<<18)); (t_h) = sh_u.value; } while (0); |
277 | t_l = ax - (t_h-bp[k]); |
278 | s_l = v*((u-s_h*t_h)-s_h*t_l); |
279 | /* compute log(ax) */ |
280 | s2 = ss*ss; |
281 | r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6))))); |
282 | r += s_l*(s_h+ss); |
283 | s2 = s_h*s_h; |
284 | t_h = 3.0+s2+r; |
285 | SET_LOW_WORD(t_h,0)do { ieee_double_shape_type sl_u; sl_u.value = (t_h); sl_u.parts .lsw = (0); (t_h) = sl_u.value; } while (0); |
286 | t_l = r-((t_h-3.0)-s2); |
287 | /* u+v = ss*(1+...) */ |
288 | u = s_h*t_h; |
289 | v = s_l*t_h+t_l*ss; |
290 | /* 2/(3log2)*(ss+...) */ |
291 | p_h = u+v; |
292 | SET_LOW_WORD(p_h,0)do { ieee_double_shape_type sl_u; sl_u.value = (p_h); sl_u.parts .lsw = (0); (p_h) = sl_u.value; } while (0); |
293 | p_l = v-(p_h-u); |
294 | z_h = cp_h*p_h; /* cp_h+cp_l = 2/(3*log2) */ |
295 | z_l = cp_l*p_h+p_l*cp+dp_l[k]; |
296 | /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */ |
297 | t = (double)n; |
298 | t1 = (((z_h+z_l)+dp_h[k])+t); |
299 | SET_LOW_WORD(t1,0)do { ieee_double_shape_type sl_u; sl_u.value = (t1); sl_u.parts .lsw = (0); (t1) = sl_u.value; } while (0); |
300 | t2 = z_l-(((t1-t)-dp_h[k])-z_h); |
301 | } |
302 | |
303 | /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */ |
304 | y1 = y; |
305 | SET_LOW_WORD(y1,0)do { ieee_double_shape_type sl_u; sl_u.value = (y1); sl_u.parts .lsw = (0); (y1) = sl_u.value; } while (0); |
306 | p_l = (y-y1)*t1+y*t2; |
307 | p_h = y1*t1; |
308 | z = p_l+p_h; |
309 | EXTRACT_WORDS(j,i,z)do { ieee_double_shape_type ew_u; ew_u.value = (z); (j) = ew_u .parts.msw; (i) = ew_u.parts.lsw; } while (0); |
310 | if (j>=0x40900000) { /* z >= 1024 */ |
311 | if(((j-0x40900000)|i)!=0) /* if z > 1024 */ |
312 | return s*huge*huge; /* overflow */ |
313 | else { |
314 | if(p_l+ovt>z-p_h) return s*huge*huge; /* overflow */ |
315 | } |
316 | } else if((j&0x7fffffff)>=0x4090cc00 ) { /* z <= -1075 */ |
317 | if(((j-0xc090cc00)|i)!=0) /* z < -1075 */ |
318 | return s*tiny*tiny; /* underflow */ |
319 | else { |
320 | if(p_l<=z-p_h) return s*tiny*tiny; /* underflow */ |
321 | } |
322 | } |
323 | /* |
324 | * compute 2**(p_h+p_l) |
325 | */ |
326 | i = j&0x7fffffff; |
327 | k = (i>>20)-0x3ff; |
328 | n = 0; |
329 | if(i>0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */ |
330 | n = j+(0x00100000>>(k+1)); |
331 | k = ((n&0x7fffffff)>>20)-0x3ff; /* new k for n */ |
332 | t = zero; |
333 | SET_HIGH_WORD(t,(n&~(0x000fffff>>k)))do { ieee_double_shape_type sh_u; sh_u.value = (t); sh_u.parts .msw = ((n&~(0x000fffff>>k))); (t) = sh_u.value; } while (0); |
334 | n = ((n&0x000fffff)|0x00100000)>>(20-k); |
335 | if(j<0) n = -n; |
336 | p_h -= t; |
337 | } |
338 | t = p_l+p_h; |
339 | SET_LOW_WORD(t,0)do { ieee_double_shape_type sl_u; sl_u.value = (t); sl_u.parts .lsw = (0); (t) = sl_u.value; } while (0); |
340 | u = t*lg2_h; |
341 | v = (p_l-(t-p_h))*lg2+t*lg2_l; |
342 | z = u+v; |
343 | w = v-(z-u); |
344 | t = z*z; |
345 | t1 = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); |
346 | r = (z*t1)/(t1-two)-(w+z*w); |
347 | z = one-(r-z); |
348 | GET_HIGH_WORD(j,z)do { ieee_double_shape_type gh_u; gh_u.value = (z); (j) = gh_u .parts.msw; } while (0); |
349 | j += (n<<20); |
350 | if((j>>20)<=0) z = scalbn(z,n); /* subnormal output */ |
351 | else |
352 | { |
353 | uword hz; |
354 | GET_HIGH_WORD(hz,z)do { ieee_double_shape_type gh_u; gh_u.value = (z); (hz) = gh_u .parts.msw; } while (0); |
355 | SET_HIGH_WORD(z,hz + (n<<20))do { ieee_double_shape_type sh_u; sh_u.value = (z); sh_u.parts .msw = (hz + (n<<20)); (z) = sh_u.value; } while (0); |
356 | } |
357 | return s*z; |
358 | } |