1 /* origin: FreeBSD /usr/src/lib/msun/src/e_powf.c */
2 /*
3  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
4  */
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  */
15 
16 use super::{fabsf, scalbnf, sqrtf};
17 
18 const BP: [f32; 2] = [1.0, 1.5];
19 const DP_H: [f32; 2] = [0.0, 5.84960938e-01]; /* 0x3f15c000 */
20 const DP_L: [f32; 2] = [0.0, 1.56322085e-06]; /* 0x35d1cfdc */
21 const TWO24: f32 = 16777216.0; /* 0x4b800000 */
22 const HUGE: f32 = 1.0e30;
23 const TINY: f32 = 1.0e-30;
24 const L1: f32 = 6.0000002384e-01; /* 0x3f19999a */
25 const L2: f32 = 4.2857143283e-01; /* 0x3edb6db7 */
26 const L3: f32 = 3.3333334327e-01; /* 0x3eaaaaab */
27 const L4: f32 = 2.7272811532e-01; /* 0x3e8ba305 */
28 const L5: f32 = 2.3066075146e-01; /* 0x3e6c3255 */
29 const L6: f32 = 2.0697501302e-01; /* 0x3e53f142 */
30 const P1: f32 = 1.6666667163e-01; /* 0x3e2aaaab */
31 const P2: f32 = -2.7777778450e-03; /* 0xbb360b61 */
32 const P3: f32 = 6.6137559770e-05; /* 0x388ab355 */
33 const P4: f32 = -1.6533901999e-06; /* 0xb5ddea0e */
34 const P5: f32 = 4.1381369442e-08; /* 0x3331bb4c */
35 const LG2: f32 = 6.9314718246e-01; /* 0x3f317218 */
36 const LG2_H: f32 = 6.93145752e-01; /* 0x3f317200 */
37 const LG2_L: f32 = 1.42860654e-06; /* 0x35bfbe8c */
38 const OVT: f32 = 4.2995665694e-08; /* -(128-log2(ovfl+.5ulp)) */
39 const CP: f32 = 9.6179670095e-01; /* 0x3f76384f =2/(3ln2) */
40 const CP_H: f32 = 9.6191406250e-01; /* 0x3f764000 =12b cp */
41 const CP_L: f32 = -1.1736857402e-04; /* 0xb8f623c6 =tail of cp_h */
42 const IVLN2: f32 = 1.4426950216e+00;
43 const IVLN2_H: f32 = 1.4426879883e+00;
44 const IVLN2_L: f32 = 7.0526075433e-06;
45 
46 #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
powf(x: f32, y: f32) -> f3247 pub fn powf(x: f32, y: f32) -> f32 {
48     let mut z: f32;
49     let mut ax: f32;
50     let z_h: f32;
51     let z_l: f32;
52     let mut p_h: f32;
53     let mut p_l: f32;
54     let y1: f32;
55     let mut t1: f32;
56     let t2: f32;
57     let mut r: f32;
58     let s: f32;
59     let mut sn: f32;
60     let mut t: f32;
61     let mut u: f32;
62     let mut v: f32;
63     let mut w: f32;
64     let i: i32;
65     let mut j: i32;
66     let mut k: i32;
67     let mut yisint: i32;
68     let mut n: i32;
69     let hx: i32;
70     let hy: i32;
71     let mut ix: i32;
72     let iy: i32;
73     let mut is: i32;
74 
75     hx = x.to_bits() as i32;
76     hy = y.to_bits() as i32;
77 
78     ix = hx & 0x7fffffff;
79     iy = hy & 0x7fffffff;
80 
81     /* x**0 = 1, even if x is NaN */
82     if iy == 0 {
83         return 1.0;
84     }
85 
86     /* 1**y = 1, even if y is NaN */
87     if hx == 0x3f800000 {
88         return 1.0;
89     }
90 
91     /* NaN if either arg is NaN */
92     if ix > 0x7f800000 || iy > 0x7f800000 {
93         return x + y;
94     }
95 
96     /* determine if y is an odd int when x < 0
97      * yisint = 0       ... y is not an integer
98      * yisint = 1       ... y is an odd int
99      * yisint = 2       ... y is an even int
100      */
101     yisint = 0;
102     if hx < 0 {
103         if iy >= 0x4b800000 {
104             yisint = 2; /* even integer y */
105         } else if iy >= 0x3f800000 {
106             k = (iy >> 23) - 0x7f; /* exponent */
107             j = iy >> (23 - k);
108             if (j << (23 - k)) == iy {
109                 yisint = 2 - (j & 1);
110             }
111         }
112     }
113 
114     /* special value of y */
115     if iy == 0x7f800000 {
116         /* y is +-inf */
117         if ix == 0x3f800000 {
118             /* (-1)**+-inf is 1 */
119             return 1.0;
120         } else if ix > 0x3f800000 {
121             /* (|x|>1)**+-inf = inf,0 */
122             return if hy >= 0 { y } else { 0.0 };
123         } else {
124             /* (|x|<1)**+-inf = 0,inf */
125             return if hy >= 0 { 0.0 } else { -y };
126         }
127     }
128     if iy == 0x3f800000 {
129         /* y is +-1 */
130         return if hy >= 0 { x } else { 1.0 / x };
131     }
132 
133     if hy == 0x40000000 {
134         /* y is 2 */
135         return x * x;
136     }
137 
138     if hy == 0x3f000000
139        /* y is  0.5 */
140        && hx >= 0
141     {
142         /* x >= +0 */
143         return sqrtf(x);
144     }
145 
146     ax = fabsf(x);
147     /* special value of x */
148     if ix == 0x7f800000 || ix == 0 || ix == 0x3f800000 {
149         /* x is +-0,+-inf,+-1 */
150         z = ax;
151         if hy < 0 {
152             /* z = (1/|x|) */
153             z = 1.0 / z;
154         }
155 
156         if hx < 0 {
157             if ((ix - 0x3f800000) | yisint) == 0 {
158                 z = (z - z) / (z - z); /* (-1)**non-int is NaN */
159             } else if yisint == 1 {
160                 z = -z; /* (x<0)**odd = -(|x|**odd) */
161             }
162         }
163         return z;
164     }
165 
166     sn = 1.0; /* sign of result */
167     if hx < 0 {
168         if yisint == 0 {
169             /* (x<0)**(non-int) is NaN */
170             return (x - x) / (x - x);
171         }
172 
173         if yisint == 1 {
174             /* (x<0)**(odd int) */
175             sn = -1.0;
176         }
177     }
178 
179     /* |y| is HUGE */
180     if iy > 0x4d000000 {
181         /* if |y| > 2**27 */
182         /* over/underflow if x is not close to one */
183         if ix < 0x3f7ffff8 {
184             return if hy < 0 {
185                 sn * HUGE * HUGE
186             } else {
187                 sn * TINY * TINY
188             };
189         }
190 
191         if ix > 0x3f800007 {
192             return if hy > 0 {
193                 sn * HUGE * HUGE
194             } else {
195                 sn * TINY * TINY
196             };
197         }
198 
199         /* now |1-x| is TINY <= 2**-20, suffice to compute
200         log(x) by x-x^2/2+x^3/3-x^4/4 */
201         t = ax - 1.; /* t has 20 trailing zeros */
202         w = (t * t) * (0.5 - t * (0.333333333333 - t * 0.25));
203         u = IVLN2_H * t; /* IVLN2_H has 16 sig. bits */
204         v = t * IVLN2_L - w * IVLN2;
205         t1 = u + v;
206         is = t1.to_bits() as i32;
207         t1 = f32::from_bits(is as u32 & 0xfffff000);
208         t2 = v - (t1 - u);
209     } else {
210         let mut s2: f32;
211         let mut s_h: f32;
212         let s_l: f32;
213         let mut t_h: f32;
214         let mut t_l: f32;
215 
216         n = 0;
217         /* take care subnormal number */
218         if ix < 0x00800000 {
219             ax *= TWO24;
220             n -= 24;
221             ix = ax.to_bits() as i32;
222         }
223         n += ((ix) >> 23) - 0x7f;
224         j = ix & 0x007fffff;
225         /* determine interval */
226         ix = j | 0x3f800000; /* normalize ix */
227         if j <= 0x1cc471 {
228             /* |x|<sqrt(3/2) */
229             k = 0;
230         } else if j < 0x5db3d7 {
231             /* |x|<sqrt(3)   */
232             k = 1;
233         } else {
234             k = 0;
235             n += 1;
236             ix -= 0x00800000;
237         }
238         ax = f32::from_bits(ix as u32);
239 
240         /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
241         u = ax - BP[k as usize]; /* bp[0]=1.0, bp[1]=1.5 */
242         v = 1.0 / (ax + BP[k as usize]);
243         s = u * v;
244         s_h = s;
245         is = s_h.to_bits() as i32;
246         s_h = f32::from_bits(is as u32 & 0xfffff000);
247         /* t_h=ax+bp[k] High */
248         is = (((ix as u32 >> 1) & 0xfffff000) | 0x20000000) as i32;
249         t_h = f32::from_bits(is as u32 + 0x00400000 + ((k as u32) << 21));
250         t_l = ax - (t_h - BP[k as usize]);
251         s_l = v * ((u - s_h * t_h) - s_h * t_l);
252         /* compute log(ax) */
253         s2 = s * s;
254         r = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
255         r += s_l * (s_h + s);
256         s2 = s_h * s_h;
257         t_h = 3.0 + s2 + r;
258         is = t_h.to_bits() as i32;
259         t_h = f32::from_bits(is as u32 & 0xfffff000);
260         t_l = r - ((t_h - 3.0) - s2);
261         /* u+v = s*(1+...) */
262         u = s_h * t_h;
263         v = s_l * t_h + t_l * s;
264         /* 2/(3log2)*(s+...) */
265         p_h = u + v;
266         is = p_h.to_bits() as i32;
267         p_h = f32::from_bits(is as u32 & 0xfffff000);
268         p_l = v - (p_h - u);
269         z_h = CP_H * p_h; /* cp_h+cp_l = 2/(3*log2) */
270         z_l = CP_L * p_h + p_l * CP + DP_L[k as usize];
271         /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */
272         t = n as f32;
273         t1 = ((z_h + z_l) + DP_H[k as usize]) + t;
274         is = t1.to_bits() as i32;
275         t1 = f32::from_bits(is as u32 & 0xfffff000);
276         t2 = z_l - (((t1 - t) - DP_H[k as usize]) - z_h);
277     };
278 
279     /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
280     is = y.to_bits() as i32;
281     y1 = f32::from_bits(is as u32 & 0xfffff000);
282     p_l = (y - y1) * t1 + y * t2;
283     p_h = y1 * t1;
284     z = p_l + p_h;
285     j = z.to_bits() as i32;
286     if j > 0x43000000 {
287         /* if z > 128 */
288         return sn * HUGE * HUGE; /* overflow */
289     } else if j == 0x43000000 {
290         /* if z == 128 */
291         if p_l + OVT > z - p_h {
292             return sn * HUGE * HUGE; /* overflow */
293         }
294     } else if (j & 0x7fffffff) > 0x43160000 {
295         /* z < -150 */
296         // FIXME: check should be  (uint32_t)j > 0xc3160000
297         return sn * TINY * TINY; /* underflow */
298     } else if j as u32 == 0xc3160000
299               /* z == -150 */
300               && p_l <= z - p_h
301     {
302         return sn * TINY * TINY; /* underflow */
303     }
304 
305     /*
306      * compute 2**(p_h+p_l)
307      */
308     i = j & 0x7fffffff;
309     k = (i >> 23) - 0x7f;
310     n = 0;
311     if i > 0x3f000000 {
312         /* if |z| > 0.5, set n = [z+0.5] */
313         n = j + (0x00800000 >> (k + 1));
314         k = ((n & 0x7fffffff) >> 23) - 0x7f; /* new k for n */
315         t = f32::from_bits(n as u32 & !(0x007fffff >> k));
316         n = ((n & 0x007fffff) | 0x00800000) >> (23 - k);
317         if j < 0 {
318             n = -n;
319         }
320         p_h -= t;
321     }
322     t = p_l + p_h;
323     is = t.to_bits() as i32;
324     t = f32::from_bits(is as u32 & 0xffff8000);
325     u = t * LG2_H;
326     v = (p_l - (t - p_h)) * LG2 + t * LG2_L;
327     z = u + v;
328     w = v - (z - u);
329     t = z * z;
330     t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
331     r = (z * t1) / (t1 - 2.0) - (w + z * w);
332     z = 1.0 - (r - z);
333     j = z.to_bits() as i32;
334     j += n << 23;
335     if (j >> 23) <= 0 {
336         /* subnormal output */
337         z = scalbnf(z, n);
338     } else {
339         z = f32::from_bits(j as u32);
340     }
341     sn * z
342 }
343