libm/math/
powf.rs
1use super::{fabsf, scalbnf, sqrtf};
17
18const BP: [f32; 2] = [1.0, 1.5];
19const DP_H: [f32; 2] = [0.0, 5.84960938e-01]; const DP_L: [f32; 2] = [0.0, 1.56322085e-06]; const TWO24: f32 = 16777216.0; const HUGE: f32 = 1.0e30;
23const TINY: f32 = 1.0e-30;
24const L1: f32 = 6.0000002384e-01; const L2: f32 = 4.2857143283e-01; const L3: f32 = 3.3333334327e-01; const L4: f32 = 2.7272811532e-01; const L5: f32 = 2.3066075146e-01; const L6: f32 = 2.0697501302e-01; const P1: f32 = 1.6666667163e-01; const P2: f32 = -2.7777778450e-03; const P3: f32 = 6.6137559770e-05; const P4: f32 = -1.6533901999e-06; const P5: f32 = 4.1381369442e-08; const LG2: f32 = 6.9314718246e-01; const LG2_H: f32 = 6.93145752e-01; const LG2_L: f32 = 1.42860654e-06; const OVT: f32 = 4.2995665694e-08; const CP: f32 = 9.6179670095e-01; const CP_H: f32 = 9.6191406250e-01; const CP_L: f32 = -1.1736857402e-04; const IVLN2: f32 = 1.4426950216e+00;
43const IVLN2_H: f32 = 1.4426879883e+00;
44const IVLN2_L: f32 = 7.0526075433e-06;
45
46#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
48pub fn powf(x: f32, y: f32) -> f32 {
49 let mut z: f32;
50 let mut ax: f32;
51 let z_h: f32;
52 let z_l: f32;
53 let mut p_h: f32;
54 let mut p_l: f32;
55 let y1: f32;
56 let mut t1: f32;
57 let t2: f32;
58 let mut r: f32;
59 let s: f32;
60 let mut sn: f32;
61 let mut t: f32;
62 let mut u: f32;
63 let mut v: f32;
64 let mut w: f32;
65 let i: i32;
66 let mut j: i32;
67 let mut k: i32;
68 let mut yisint: i32;
69 let mut n: i32;
70 let hx: i32;
71 let hy: i32;
72 let mut ix: i32;
73 let iy: i32;
74 let mut is: i32;
75
76 hx = x.to_bits() as i32;
77 hy = y.to_bits() as i32;
78
79 ix = hx & 0x7fffffff;
80 iy = hy & 0x7fffffff;
81
82 if iy == 0 {
84 return 1.0;
85 }
86
87 if hx == 0x3f800000 {
89 return 1.0;
90 }
91
92 if ix > 0x7f800000 || iy > 0x7f800000 {
94 return x + y;
95 }
96
97 yisint = 0;
103 if hx < 0 {
104 if iy >= 0x4b800000 {
105 yisint = 2; } else if iy >= 0x3f800000 {
107 k = (iy >> 23) - 0x7f; j = iy >> (23 - k);
109 if (j << (23 - k)) == iy {
110 yisint = 2 - (j & 1);
111 }
112 }
113 }
114
115 if iy == 0x7f800000 {
117 if ix == 0x3f800000 {
119 return 1.0;
121 } else if ix > 0x3f800000 {
122 return if hy >= 0 { y } else { 0.0 };
124 } else {
125 return if hy >= 0 { 0.0 } else { -y };
127 }
128 }
129 if iy == 0x3f800000 {
130 return if hy >= 0 { x } else { 1.0 / x };
132 }
133
134 if hy == 0x40000000 {
135 return x * x;
137 }
138
139 if hy == 0x3f000000
140 && hx >= 0
142 {
143 return sqrtf(x);
145 }
146
147 ax = fabsf(x);
148 if ix == 0x7f800000 || ix == 0 || ix == 0x3f800000 {
150 z = ax;
152 if hy < 0 {
153 z = 1.0 / z;
155 }
156
157 if hx < 0 {
158 if ((ix - 0x3f800000) | yisint) == 0 {
159 z = (z - z) / (z - z); } else if yisint == 1 {
161 z = -z; }
163 }
164 return z;
165 }
166
167 sn = 1.0; if hx < 0 {
169 if yisint == 0 {
170 return (x - x) / (x - x);
172 }
173
174 if yisint == 1 {
175 sn = -1.0;
177 }
178 }
179
180 if iy > 0x4d000000 {
182 if ix < 0x3f7ffff8 {
185 return if hy < 0 { sn * HUGE * HUGE } else { sn * TINY * TINY };
186 }
187
188 if ix > 0x3f800007 {
189 return if hy > 0 { sn * HUGE * HUGE } else { sn * TINY * TINY };
190 }
191
192 t = ax - 1.; w = (t * t) * (0.5 - t * (0.333333333333 - t * 0.25));
196 u = IVLN2_H * t; v = t * IVLN2_L - w * IVLN2;
198 t1 = u + v;
199 is = t1.to_bits() as i32;
200 t1 = f32::from_bits(is as u32 & 0xfffff000);
201 t2 = v - (t1 - u);
202 } else {
203 let mut s2: f32;
204 let mut s_h: f32;
205 let s_l: f32;
206 let mut t_h: f32;
207 let mut t_l: f32;
208
209 n = 0;
210 if ix < 0x00800000 {
212 ax *= TWO24;
213 n -= 24;
214 ix = ax.to_bits() as i32;
215 }
216 n += ((ix) >> 23) - 0x7f;
217 j = ix & 0x007fffff;
218 ix = j | 0x3f800000; if j <= 0x1cc471 {
221 k = 0;
223 } else if j < 0x5db3d7 {
224 k = 1;
226 } else {
227 k = 0;
228 n += 1;
229 ix -= 0x00800000;
230 }
231 ax = f32::from_bits(ix as u32);
232
233 u = ax - i!(BP, k as usize); v = 1.0 / (ax + i!(BP, k as usize));
236 s = u * v;
237 s_h = s;
238 is = s_h.to_bits() as i32;
239 s_h = f32::from_bits(is as u32 & 0xfffff000);
240 is = (((ix as u32 >> 1) & 0xfffff000) | 0x20000000) as i32;
242 t_h = f32::from_bits(is as u32 + 0x00400000 + ((k as u32) << 21));
243 t_l = ax - (t_h - i!(BP, k as usize));
244 s_l = v * ((u - s_h * t_h) - s_h * t_l);
245 s2 = s * s;
247 r = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
248 r += s_l * (s_h + s);
249 s2 = s_h * s_h;
250 t_h = 3.0 + s2 + r;
251 is = t_h.to_bits() as i32;
252 t_h = f32::from_bits(is as u32 & 0xfffff000);
253 t_l = r - ((t_h - 3.0) - s2);
254 u = s_h * t_h;
256 v = s_l * t_h + t_l * s;
257 p_h = u + v;
259 is = p_h.to_bits() as i32;
260 p_h = f32::from_bits(is as u32 & 0xfffff000);
261 p_l = v - (p_h - u);
262 z_h = CP_H * p_h; z_l = CP_L * p_h + p_l * CP + i!(DP_L, k as usize);
264 t = n as f32;
266 t1 = ((z_h + z_l) + i!(DP_H, k as usize)) + t;
267 is = t1.to_bits() as i32;
268 t1 = f32::from_bits(is as u32 & 0xfffff000);
269 t2 = z_l - (((t1 - t) - i!(DP_H, k as usize)) - z_h);
270 };
271
272 is = y.to_bits() as i32;
274 y1 = f32::from_bits(is as u32 & 0xfffff000);
275 p_l = (y - y1) * t1 + y * t2;
276 p_h = y1 * t1;
277 z = p_l + p_h;
278 j = z.to_bits() as i32;
279 if j > 0x43000000 {
280 return sn * HUGE * HUGE; } else if j == 0x43000000 {
283 if p_l + OVT > z - p_h {
285 return sn * HUGE * HUGE; }
287 } else if (j & 0x7fffffff) > 0x43160000 {
288 return sn * TINY * TINY; } else if j as u32 == 0xc3160000
292 && p_l <= z - p_h
294 {
295 return sn * TINY * TINY; }
297
298 i = j & 0x7fffffff;
302 k = (i >> 23) - 0x7f;
303 n = 0;
304 if i > 0x3f000000 {
305 n = j + (0x00800000 >> (k + 1));
307 k = ((n & 0x7fffffff) >> 23) - 0x7f; t = f32::from_bits(n as u32 & !(0x007fffff >> k));
309 n = ((n & 0x007fffff) | 0x00800000) >> (23 - k);
310 if j < 0 {
311 n = -n;
312 }
313 p_h -= t;
314 }
315 t = p_l + p_h;
316 is = t.to_bits() as i32;
317 t = f32::from_bits(is as u32 & 0xffff8000);
318 u = t * LG2_H;
319 v = (p_l - (t - p_h)) * LG2 + t * LG2_L;
320 z = u + v;
321 w = v - (z - u);
322 t = z * z;
323 t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
324 r = (z * t1) / (t1 - 2.0) - (w + z * w);
325 z = 1.0 - (r - z);
326 j = z.to_bits() as i32;
327 j += n << 23;
328 if (j >> 23) <= 0 {
329 z = scalbnf(z, n);
331 } else {
332 z = f32::from_bits(j as u32);
333 }
334 sn * z
335}