compiler_builtins/math/libm_math/
powf.rs1use core::cmp::Ordering;
17
18use super::{fabsf, scalbnf, sqrtf};
19
20const BP: [f32; 2] = [1.0, 1.5];
21const 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;
25const TINY: f32 = 1.0e-30;
26const 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;
45const IVLN2_H: f32 = 1.4426879883e+00;
46const IVLN2_L: f32 = 7.0526075433e-06;
47
48#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
50pub fn powf(x: f32, y: f32) -> f32 {
51 let mut z: f32;
52 let mut ax: f32;
53 let z_h: f32;
54 let z_l: f32;
55 let mut p_h: f32;
56 let mut p_l: f32;
57 let y1: f32;
58 let mut t1: f32;
59 let t2: f32;
60 let mut r: f32;
61 let s: f32;
62 let mut sn: f32;
63 let mut t: f32;
64 let mut u: f32;
65 let mut v: f32;
66 let mut w: f32;
67 let i: i32;
68 let mut j: i32;
69 let mut k: i32;
70 let mut yisint: i32;
71 let mut n: i32;
72 let hx: i32;
73 let hy: i32;
74 let mut ix: i32;
75 let iy: i32;
76 let mut is: i32;
77
78 hx = x.to_bits() as i32;
79 hy = y.to_bits() as i32;
80
81 ix = hx & 0x7fffffff;
82 iy = hy & 0x7fffffff;
83
84 if iy == 0 {
86 return 1.0;
87 }
88
89 if hx == 0x3f800000 {
91 return 1.0;
92 }
93
94 if ix > 0x7f800000 || iy > 0x7f800000 {
96 return x + y;
97 }
98
99 yisint = 0;
105 if hx < 0 {
106 if iy >= 0x4b800000 {
107 yisint = 2; } else if iy >= 0x3f800000 {
109 k = (iy >> 23) - 0x7f; j = iy >> (23 - k);
111 if (j << (23 - k)) == iy {
112 yisint = 2 - (j & 1);
113 }
114 }
115 }
116
117 if iy == 0x7f800000 {
119 match ix.cmp(&0x3f800000) {
121 Ordering::Equal => return 1.0,
123 Ordering::Greater => return if hy >= 0 { y } else { 0.0 },
125 Ordering::Less => 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 {
186 sn * HUGE * HUGE
187 } else {
188 sn * TINY * TINY
189 };
190 }
191
192 if ix > 0x3f800007 {
193 return if hy > 0 {
194 sn * HUGE * HUGE
195 } else {
196 sn * TINY * TINY
197 };
198 }
199
200 t = ax - 1.; w = (t * t) * (0.5 - t * (0.333333333333 - t * 0.25));
204 u = IVLN2_H * t; v = t * IVLN2_L - w * IVLN2;
206 t1 = u + v;
207 is = t1.to_bits() as i32;
208 t1 = f32::from_bits(is as u32 & 0xfffff000);
209 t2 = v - (t1 - u);
210 } else {
211 let mut s2: f32;
212 let mut s_h: f32;
213 let s_l: f32;
214 let mut t_h: f32;
215 let mut t_l: f32;
216
217 n = 0;
218 if ix < 0x00800000 {
220 ax *= TWO24;
221 n -= 24;
222 ix = ax.to_bits() as i32;
223 }
224 n += ((ix) >> 23) - 0x7f;
225 j = ix & 0x007fffff;
226 ix = j | 0x3f800000; if j <= 0x1cc471 {
229 k = 0;
231 } else if j < 0x5db3d7 {
232 k = 1;
234 } else {
235 k = 0;
236 n += 1;
237 ix -= 0x00800000;
238 }
239 ax = f32::from_bits(ix as u32);
240
241 u = ax - i!(BP, k as usize); v = 1.0 / (ax + i!(BP, k as usize));
244 s = u * v;
245 s_h = s;
246 is = s_h.to_bits() as i32;
247 s_h = f32::from_bits(is as u32 & 0xfffff000);
248 is = (((ix as u32 >> 1) & 0xfffff000) | 0x20000000) as i32;
250 t_h = f32::from_bits(is as u32 + 0x00400000 + ((k as u32) << 21));
251 t_l = ax - (t_h - i!(BP, k as usize));
252 s_l = v * ((u - s_h * t_h) - s_h * t_l);
253 s2 = s * s;
255 r = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
256 r += s_l * (s_h + s);
257 s2 = s_h * s_h;
258 t_h = 3.0 + s2 + r;
259 is = t_h.to_bits() as i32;
260 t_h = f32::from_bits(is as u32 & 0xfffff000);
261 t_l = r - ((t_h - 3.0) - s2);
262 u = s_h * t_h;
264 v = s_l * t_h + t_l * s;
265 p_h = u + v;
267 is = p_h.to_bits() as i32;
268 p_h = f32::from_bits(is as u32 & 0xfffff000);
269 p_l = v - (p_h - u);
270 z_h = CP_H * p_h; z_l = CP_L * p_h + p_l * CP + i!(DP_L, k as usize);
272 t = n as f32;
274 t1 = ((z_h + z_l) + i!(DP_H, k as usize)) + t;
275 is = t1.to_bits() as i32;
276 t1 = f32::from_bits(is as u32 & 0xfffff000);
277 t2 = z_l - (((t1 - t) - i!(DP_H, k as usize)) - z_h);
278 };
279
280 is = y.to_bits() as i32;
282 y1 = f32::from_bits(is as u32 & 0xfffff000);
283 p_l = (y - y1) * t1 + y * t2;
284 p_h = y1 * t1;
285 z = p_l + p_h;
286 j = z.to_bits() as i32;
287 if j > 0x43000000 {
288 return sn * HUGE * HUGE; } else if j == 0x43000000 {
291 if p_l + OVT > z - p_h {
293 return sn * HUGE * HUGE; }
295 } else if (j & 0x7fffffff) > 0x43160000 {
296 return sn * TINY * TINY; } else if j as u32 == 0xc3160000
300 && p_l <= z - p_h
302 {
303 return sn * TINY * TINY; }
305
306 i = j & 0x7fffffff;
310 k = (i >> 23) - 0x7f;
311 n = 0;
312 if i > 0x3f000000 {
313 n = j + (0x00800000 >> (k + 1));
315 k = ((n & 0x7fffffff) >> 23) - 0x7f; t = f32::from_bits(n as u32 & !(0x007fffff >> k));
317 n = ((n & 0x007fffff) | 0x00800000) >> (23 - k);
318 if j < 0 {
319 n = -n;
320 }
321 p_h -= t;
322 }
323 t = p_l + p_h;
324 is = t.to_bits() as i32;
325 t = f32::from_bits(is as u32 & 0xffff8000);
326 u = t * LG2_H;
327 v = (p_l - (t - p_h)) * LG2 + t * LG2_L;
328 z = u + v;
329 w = v - (z - u);
330 t = z * z;
331 t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
332 r = (z * t1) / (t1 - 2.0) - (w + z * w);
333 z = 1.0 - (r - z);
334 j = z.to_bits() as i32;
335 j += n << 23;
336 if (j >> 23) <= 0 {
337 z = scalbnf(z, n);
339 } else {
340 z = f32::from_bits(j as u32);
341 }
342 sn * z
343}