const_soft_float/soft_f64/
pow.rs1use crate::abs_diff;
2use crate::soft_f64::SoftF64;
3
4type F = SoftF64;
5
6pub(crate) const fn pow(a: F, b: i32) -> F {
7 let mut a = a;
8 let recip = b < 0;
9 let mut pow = abs_diff(b, 0);
10 let mut mul = F::ONE;
11 loop {
12 if (pow & 1) != 0 {
13 mul = mul.mul(a);
14 }
15 pow >>= 1;
16 if pow == 0 {
17 break;
18 }
19 a = a.mul(a);
20 }
21
22 if recip {
23 F::ONE.div(mul)
24 } else {
25 mul
26 }
27}
28
29#[cfg(test)]
30mod test {
31 use crate::soft_f64::SoftF64;
32
33 #[test]
34 fn sanity_check() {
35 assert_eq!(SoftF64(2.0).powi(2).0, 4.0)
36 }
37}