1use super::{fabs, get_high_word, scalbn, sqrt, with_set_high_word, with_set_low_word};
61
62const BP: [f64; 2] = [1.0, 1.5];
63const DP_H: [f64; 2] = [0.0, 5.84962487220764160156e-01]; const DP_L: [f64; 2] = [0.0, 1.35003920212974897128e-08]; const TWO53: f64 = 9007199254740992.0; const HUGE: f64 = 1.0e300;
67const TINY: f64 = 1.0e-300;
68
69const L1: f64 = 5.99999999999994648725e-01; const L2: f64 = 4.28571428578550184252e-01; const L3: f64 = 3.33333329818377432918e-01; const L4: f64 = 2.72728123808534006489e-01; const L5: f64 = 2.30660745775561754067e-01; const L6: f64 = 2.06975017800338417784e-01; const P1: f64 = 1.66666666666666019037e-01; const P2: f64 = -2.77777777770155933842e-03; const P3: f64 = 6.61375632143793436117e-05; const P4: f64 = -1.65339022054652515390e-06; const P5: f64 = 4.13813679705723846039e-08; const LG2: f64 = 6.93147180559945286227e-01; const LG2_H: f64 = 6.93147182464599609375e-01; const LG2_L: f64 = -1.90465429995776804525e-09; const OVT: f64 = 8.0085662595372944372e-017; const CP: f64 = 9.61796693925975554329e-01; const CP_H: f64 = 9.61796700954437255859e-01; const CP_L: f64 = -7.02846165095275826516e-09; const IVLN2: f64 = 1.44269504088896338700e+00; const IVLN2_H: f64 = 1.44269502162933349609e+00; const IVLN2_L: f64 = 1.92596299112661746887e-08; #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
94pub fn pow(x: f64, y: f64) -> f64 {
95    let t1: f64;
96    let t2: f64;
97
98    let (hx, lx): (i32, u32) = ((x.to_bits() >> 32) as i32, x.to_bits() as u32);
99    let (hy, ly): (i32, u32) = ((y.to_bits() >> 32) as i32, y.to_bits() as u32);
100
101    let mut ix: i32 = hx & 0x7fffffff_i32;
102    let iy: i32 = hy & 0x7fffffff_i32;
103
104    if ((iy as u32) | ly) == 0 {
106        return 1.0;
107    }
108
109    if hx == 0x3ff00000 && lx == 0 {
111        return 1.0;
112    }
113
114    if ix > 0x7ff00000
116        || (ix == 0x7ff00000 && lx != 0)
117        || iy > 0x7ff00000
118        || (iy == 0x7ff00000 && ly != 0)
119    {
120        return x + y;
121    }
122
123    let mut yisint: i32 = 0;
129    let mut k: i32;
130    let mut j: i32;
131    if hx < 0 {
132        if iy >= 0x43400000 {
133            yisint = 2; } else if iy >= 0x3ff00000 {
135            k = (iy >> 20) - 0x3ff; if k > 20 {
138                j = (ly >> (52 - k)) as i32;
139
140                if (j << (52 - k)) == (ly as i32) {
141                    yisint = 2 - (j & 1);
142                }
143            } else if ly == 0 {
144                j = iy >> (20 - k);
145
146                if (j << (20 - k)) == iy {
147                    yisint = 2 - (j & 1);
148                }
149            }
150        }
151    }
152
153    if ly == 0 {
154        if iy == 0x7ff00000 {
156            return if ((ix - 0x3ff00000) | (lx as i32)) == 0 {
159                1.0
161            } else if ix >= 0x3ff00000 {
162                if hy >= 0 { y } else { 0.0 }
164            } else {
165                if hy >= 0 { 0.0 } else { -y }
167            };
168        }
169
170        if iy == 0x3ff00000 {
171            return if hy >= 0 { x } else { 1.0 / x };
173        }
174
175        if hy == 0x40000000 {
176            return x * x;
178        }
179
180        if hy == 0x3fe00000 {
181            if hx >= 0 {
183                return sqrt(x);
185            }
186        }
187    }
188
189    let mut ax: f64 = fabs(x);
190    if lx == 0 {
191        if ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000 {
193            let mut z: f64 = ax;
195
196            if hy < 0 {
197                z = 1.0 / z;
199            }
200
201            if hx < 0 {
202                if ((ix - 0x3ff00000) | yisint) == 0 {
203                    z = (z - z) / (z - z); } else if yisint == 1 {
205                    z = -z; }
207            }
208
209            return z;
210        }
211    }
212
213    let mut s: f64 = 1.0; if hx < 0 {
215        if yisint == 0 {
216            return (x - x) / (x - x);
218        }
219
220        if yisint == 1 {
221            s = -1.0;
223        }
224    }
225
226    if iy > 0x41e00000 {
228        if iy > 0x43f00000 {
230            if ix <= 0x3fefffff {
232                return if hy < 0 { HUGE * HUGE } else { TINY * TINY };
233            }
234
235            if ix >= 0x3ff00000 {
236                return if hy > 0 { HUGE * HUGE } else { TINY * TINY };
237            }
238        }
239
240        if ix < 0x3fefffff {
242            return if hy < 0 {
243                s * HUGE * HUGE
244            } else {
245                s * TINY * TINY
246            };
247        }
248        if ix > 0x3ff00000 {
249            return if hy > 0 {
250                s * HUGE * HUGE
251            } else {
252                s * TINY * TINY
253            };
254        }
255
256        let t: f64 = ax - 1.0; let w: f64 = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25));
260        let u: f64 = IVLN2_H * t; let v: f64 = t * IVLN2_L - w * IVLN2;
262        t1 = with_set_low_word(u + v, 0);
263        t2 = v - (t1 - u);
264    } else {
265        let mut n: i32 = 0;
267
268        if ix < 0x00100000 {
269            ax *= TWO53;
271            n -= 53;
272            ix = get_high_word(ax) as i32;
273        }
274
275        n += (ix >> 20) - 0x3ff;
276        j = ix & 0x000fffff;
277
278        let k: i32;
280        ix = j | 0x3ff00000; if j <= 0x3988E {
282            k = 0;
284        } else if j < 0xBB67A {
285            k = 1;
287        } else {
288            k = 0;
289            n += 1;
290            ix -= 0x00100000;
291        }
292        ax = with_set_high_word(ax, ix as u32);
293
294        let u: f64 = ax - i!(BP, k as usize); let v: f64 = 1.0 / (ax + i!(BP, k as usize));
297        let ss: f64 = u * v;
298        let s_h = with_set_low_word(ss, 0);
299
300        let t_h: f64 = with_set_high_word(
302            0.0,
303            ((ix as u32 >> 1) | 0x20000000) + 0x00080000 + ((k as u32) << 18),
304        );
305        let t_l: f64 = ax - (t_h - i!(BP, k as usize));
306        let s_l: f64 = v * ((u - s_h * t_h) - s_h * t_l);
307
308        let s2: f64 = ss * ss;
310        let mut r: f64 = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
311        r += s_l * (s_h + ss);
312        let s2: f64 = s_h * s_h;
313        let t_h: f64 = with_set_low_word(3.0 + s2 + r, 0);
314        let t_l: f64 = r - ((t_h - 3.0) - s2);
315
316        let u: f64 = s_h * t_h;
318        let v: f64 = s_l * t_h + t_l * ss;
319
320        let p_h: f64 = with_set_low_word(u + v, 0);
322        let p_l = v - (p_h - u);
323        let z_h: f64 = CP_H * p_h; let z_l: f64 = CP_L * p_h + p_l * CP + i!(DP_L, k as usize);
325
326        let t: f64 = n as f64;
328        t1 = with_set_low_word(((z_h + z_l) + i!(DP_H, k as usize)) + t, 0);
329        t2 = z_l - (((t1 - t) - i!(DP_H, k as usize)) - z_h);
330    }
331
332    let y1: f64 = with_set_low_word(y, 0);
334    let p_l: f64 = (y - y1) * t1 + y * t2;
335    let mut p_h: f64 = y1 * t1;
336    let z: f64 = p_l + p_h;
337    let mut j: i32 = (z.to_bits() >> 32) as i32;
338    let i: i32 = z.to_bits() as i32;
339    if j >= 0x40900000 {
342        if (j - 0x40900000) | i != 0 {
344            return s * HUGE * HUGE; }
347
348        if p_l + OVT > z - p_h {
349            return s * HUGE * HUGE; }
351    } else if (j & 0x7fffffff) >= 0x4090cc00 {
352        if (((j as u32) - 0xc090cc00) | (i as u32)) != 0 {
356            return s * TINY * TINY; }
359
360        if p_l <= z - p_h {
361            return s * TINY * TINY; }
363    }
364
365    let i: i32 = j & 0x7fffffff_i32;
367    k = (i >> 20) - 0x3ff;
368    let mut n: i32 = 0;
369
370    if i > 0x3fe00000 {
371        n = j + (0x00100000 >> (k + 1));
373        k = ((n & 0x7fffffff) >> 20) - 0x3ff; let t: f64 = with_set_high_word(0.0, (n & !(0x000fffff >> k)) as u32);
375        n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
376        if j < 0 {
377            n = -n;
378        }
379        p_h -= t;
380    }
381
382    let t: f64 = with_set_low_word(p_l + p_h, 0);
383    let u: f64 = t * LG2_H;
384    let v: f64 = (p_l - (t - p_h)) * LG2 + t * LG2_L;
385    let mut z: f64 = u + v;
386    let w: f64 = v - (z - u);
387    let t: f64 = z * z;
388    let t1: f64 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
389    let r: f64 = (z * t1) / (t1 - 2.0) - (w + z * w);
390    z = 1.0 - (r - z);
391    j = get_high_word(z) as i32;
392    j += n << 20;
393
394    if (j >> 20) <= 0 {
395        z = scalbn(z, n);
397    } else {
398        z = with_set_high_word(z, j as u32);
399    }
400
401    s * z
402}
403
404#[cfg(test)]
405mod tests {
406    extern crate core;
407
408    use self::core::f64::consts::{E, PI};
409    use super::pow;
410
411    const POS_ZERO: &[f64] = &[0.0];
412    const NEG_ZERO: &[f64] = &[-0.0];
413    const POS_ONE: &[f64] = &[1.0];
414    const NEG_ONE: &[f64] = &[-1.0];
415    const POS_FLOATS: &[f64] = &[99.0 / 70.0, E, PI];
416    const NEG_FLOATS: &[f64] = &[-99.0 / 70.0, -E, -PI];
417    const POS_SMALL_FLOATS: &[f64] = &[(1.0 / 2.0), f64::MIN_POSITIVE, f64::EPSILON];
418    const NEG_SMALL_FLOATS: &[f64] = &[-(1.0 / 2.0), -f64::MIN_POSITIVE, -f64::EPSILON];
419    const POS_EVENS: &[f64] = &[2.0, 6.0, 8.0, 10.0, 22.0, 100.0, f64::MAX];
420    const NEG_EVENS: &[f64] = &[f64::MIN, -100.0, -22.0, -10.0, -8.0, -6.0, -2.0];
421    const POS_ODDS: &[f64] = &[3.0, 7.0];
422    const NEG_ODDS: &[f64] = &[-7.0, -3.0];
423    const NANS: &[f64] = &[f64::NAN];
424    const POS_INF: &[f64] = &[f64::INFINITY];
425    const NEG_INF: &[f64] = &[f64::NEG_INFINITY];
426
427    const ALL: &[&[f64]] = &[
428        POS_ZERO,
429        NEG_ZERO,
430        NANS,
431        NEG_SMALL_FLOATS,
432        POS_SMALL_FLOATS,
433        NEG_FLOATS,
434        POS_FLOATS,
435        NEG_EVENS,
436        POS_EVENS,
437        NEG_ODDS,
438        POS_ODDS,
439        NEG_INF,
440        POS_INF,
441        NEG_ONE,
442        POS_ONE,
443    ];
444    const POS: &[&[f64]] = &[POS_ZERO, POS_ODDS, POS_ONE, POS_FLOATS, POS_EVENS, POS_INF];
445    const NEG: &[&[f64]] = &[NEG_ZERO, NEG_ODDS, NEG_ONE, NEG_FLOATS, NEG_EVENS, NEG_INF];
446
447    fn pow_test(base: f64, exponent: f64, expected: f64) {
448        let res = pow(base, exponent);
449        assert!(
450            if expected.is_nan() {
451                res.is_nan()
452            } else {
453                pow(base, exponent) == expected
454            },
455            "{base} ** {exponent} was {res} instead of {expected}",
456        );
457    }
458
459    fn test_sets_as_base(sets: &[&[f64]], exponent: f64, expected: f64) {
460        sets.iter()
461            .for_each(|s| s.iter().for_each(|val| pow_test(*val, exponent, expected)));
462    }
463
464    fn test_sets_as_exponent(base: f64, sets: &[&[f64]], expected: f64) {
465        sets.iter()
466            .for_each(|s| s.iter().for_each(|val| pow_test(base, *val, expected)));
467    }
468
469    fn test_sets(sets: &[&[f64]], computed: &dyn Fn(f64) -> f64, expected: &dyn Fn(f64) -> f64) {
470        sets.iter().for_each(|s| {
471            s.iter().for_each(|val| {
472                let exp = expected(*val);
473                let res = computed(*val);
474
475                #[cfg(all(target_arch = "x86", not(target_feature = "sse2")))]
476                let exp = force_eval!(exp);
477                #[cfg(all(target_arch = "x86", not(target_feature = "sse2")))]
478                let res = force_eval!(res);
479                assert!(
480                    if exp.is_nan() {
481                        res.is_nan()
482                    } else {
483                        exp == res
484                    },
485                    "test for {val} was {res} instead of {exp}",
486                );
487            })
488        });
489    }
490
491    #[test]
492    fn zero_as_exponent() {
493        test_sets_as_base(ALL, 0.0, 1.0);
494        test_sets_as_base(ALL, -0.0, 1.0);
495    }
496
497    #[test]
498    fn one_as_base() {
499        test_sets_as_exponent(1.0, ALL, 1.0);
500    }
501
502    #[test]
503    fn nan_inputs() {
504        test_sets_as_exponent(f64::NAN, &ALL[2..], f64::NAN);
507
508        test_sets_as_base(&ALL[..(ALL.len() - 2)], f64::NAN, f64::NAN);
511    }
512
513    #[test]
514    fn infinity_as_base() {
515        test_sets_as_exponent(f64::INFINITY, &POS[1..], f64::INFINITY);
518
519        test_sets_as_exponent(f64::INFINITY, &NEG[1..], 0.0);
521
522        test_sets_as_exponent(f64::NEG_INFINITY, &[POS_ODDS], f64::NEG_INFINITY);
525
526        test_sets(ALL, &|v: f64| pow(f64::NEG_INFINITY, v), &|v: f64| {
530            pow(-0.0, -v)
531        });
532    }
533
534    #[test]
535    fn infinity_as_exponent() {
536        test_sets_as_base(&ALL[5..(ALL.len() - 2)], f64::INFINITY, f64::INFINITY);
539
540        test_sets_as_base(&ALL[5..ALL.len() - 2], f64::NEG_INFINITY, 0.0);
542
543        let base_below_one = &[POS_ZERO, NEG_ZERO, NEG_SMALL_FLOATS, POS_SMALL_FLOATS];
545
546        test_sets_as_base(base_below_one, f64::INFINITY, 0.0);
548
549        test_sets_as_base(base_below_one, f64::NEG_INFINITY, f64::INFINITY);
551
552        test_sets_as_base(&[NEG_ONE, POS_ONE], f64::INFINITY, 1.0);
555
556        test_sets_as_base(&[NEG_ONE, POS_ONE], f64::NEG_INFINITY, 1.0);
558    }
559
560    #[test]
561    fn zero_as_base() {
562        test_sets_as_exponent(0.0, &POS[1..], 0.0);
565
566        test_sets_as_exponent(0.0, &NEG[1..], f64::INFINITY);
569
570        test_sets_as_exponent(-0.0, &POS[3..], 0.0);
573
574        test_sets_as_exponent(-0.0, &NEG[3..], f64::INFINITY);
577
578        test_sets_as_exponent(-0.0, &[POS_ODDS], -0.0);
580
581        test_sets_as_exponent(-0.0, &[NEG_ODDS], f64::NEG_INFINITY);
584    }
585
586    #[test]
587    fn special_cases() {
588        test_sets(ALL, &|v: f64| pow(v, 1.0), &|v: f64| v);
591
592        test_sets(ALL, &|v: f64| pow(v, -1.0), &|v: f64| 1.0 / v);
595
596        [POS_ZERO, NEG_ZERO, POS_ONE, NEG_ONE, POS_EVENS, NEG_EVENS]
599            .iter()
600            .for_each(|int_set| {
601                int_set.iter().for_each(|int| {
602                    test_sets(ALL, &|v: f64| pow(-v, *int), &|v: f64| {
603                        pow(-1.0, *int) * pow(v, *int)
604                    });
605                })
606            });
607
608        NEG[1..(NEG.len() - 1)].iter().for_each(|set| {
611            set.iter().for_each(|val| {
612                test_sets(&ALL[3..7], &|v: f64| pow(*val, v), &|_| f64::NAN);
613            })
614        });
615    }
616
617    #[test]
618    fn normal_cases() {
619        assert_eq!(pow(2.0, 20.0), (1 << 20) as f64);
620        assert_eq!(pow(-1.0, 9.0), -1.0);
621        assert!(pow(-1.0, 2.2).is_nan());
622        assert!(pow(-1.0, -1.14).is_nan());
623    }
624}