yali/operations/
pow.rs

1use std::ops::{BitXor, Mul};
2
3use crate::Number;
4
5impl<const N: usize> BitXor for Number<N> {
6    fn bitxor(self, rhs: Self) -> Self::Output {
7        self.pow(rhs, &Self::mul, 1u16.into(), false)
8    }
9
10    type Output = Self;
11}
12
13impl<const N: usize> Number<N> {
14    pub(in crate::operations) fn pow(
15        mut self,
16        mut rhs: Self,
17        function: &impl Fn(Self, Self) -> Self,
18        start: Self,
19        _debug: bool,
20    ) -> Self {
21        let mut result = start;
22        let zero = 0u16.into();
23        while rhs > zero {
24            if rhs.body[rhs.body.len() - 1] & 1 == 1 {
25                if _debug {
26                    dbg!(result);
27                    dbg!(self);
28                }
29                result = function(result, self);
30                if _debug {
31                    println!("=");
32                    dbg!(result);
33                }
34            }
35            if _debug {
36                dbg!(self);
37            }
38            self = function(self, self);
39            if _debug {
40                println!("=");
41                dbg!(self);
42            }
43            rhs = rhs >> 1;
44        }
45
46        return result;
47    }
48}