1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//! Exponentiation.

use core::ops::{Shl, Shr};

use crate::{ibig::IBig, ubig::UBig, Sign::*};

impl UBig {
    /// Raises self to the power of `exp`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use dashu_int::UBig;
    /// assert_eq!(UBig::from(3u8).pow(3), 27);
    /// ```
    #[inline]
    pub fn pow(&self, exp: usize) -> UBig {
        // remove factor 2 before actual powering
        let shift = self.trailing_zeros().unwrap_or(0);
        let result = if shift != 0 {
            self.repr()
                .shr(shift)
                .as_typed()
                .pow(exp)
                .into_typed()
                .shl(exp * shift)
        } else {
            self.repr().pow(exp)
        };
        UBig(result)
    }
}

impl IBig {
    /// Raises self to the power of `exp`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use dashu_int::IBig;
    /// assert_eq!(IBig::from(-3).pow(3), -27);
    /// ```
    #[inline]
    pub fn pow(&self, exp: usize) -> IBig {
        let (sign, mag) = self.as_sign_repr();
        let sign = if sign == Negative && exp % 2 == 1 {
            Negative
        } else {
            Positive
        };

        // remove factor 2 before actual powering
        let shift = mag.trailing_zeros().unwrap_or(0);
        let result = if shift != 0 {
            mag.shr(shift)
                .as_typed()
                .pow(exp)
                .into_typed()
                .shl(exp * shift)
        } else {
            mag.pow(exp)
        };
        IBig(result.with_sign(sign))
    }
}

// TODO: change the algorithm to right-to-left exponentiation, which should be faster because the exponent has to be
//       a small integer that fits in a word

pub(crate) mod repr {
    use dashu_base::DivRem;

    use crate::{
        arch::word::{DoubleWord, Word},
        buffer::Buffer,
        math::{self, bit_len, max_exp_in_word},
        memory::{self, MemoryAllocation},
        mul, mul_ops,
        primitive::{extend_word, shrink_dword, split_dword},
        repr::{
            Repr,
            TypedReprRef::{self, *},
        },
        sqr,
    };

    impl TypedReprRef<'_> {
        pub fn pow(self, exp: usize) -> Repr {
            // shortcuts
            match exp {
                0 => return Repr::one(),
                1 => return Repr::from_ref(self),
                2 => return self.square(),
                _ => {}
            };

            match self {
                RefSmall(dword) => {
                    if let Some(word) = shrink_dword(dword) {
                        pow_word_base(word, exp)
                    } else {
                        pow_dword_base(dword, exp)
                    }
                }
                RefLarge(words) => pow_large_base(words, exp),
            }
        }
    }

    pub(crate) fn pow_word_base(base: Word, exp: usize) -> Repr {
        debug_assert!(exp > 1);
        match base {
            0 => return Repr::zero(),
            1 => return Repr::one(),
            2 => return Repr::zero().into_typed().set_bit(exp),
            b if b.is_power_of_two() => {
                return Repr::zero()
                    .into_typed()
                    .set_bit(exp * base.trailing_zeros() as usize)
            }
            _ => {}
        }

        // lift the base to a full word and some shortcuts
        let (wexp, wbase) = max_exp_in_word(base);
        if exp < wexp {
            return Repr::from_word(base.pow(exp as u32));
        } else if exp < 2 * wexp {
            let pow = base.pow((exp - wexp) as u32);
            return Repr::from_dword(extend_word(wbase) * extend_word(pow));
        }

        // by now wexp / exp >= 2, result = wbase ^ (wexp / exp) * base ^ (wexp % exp)
        let (exp, exp_rem) = exp.div_rem(wexp);
        let mut res = Buffer::allocate(exp + 1); // result is at most exp + 1 words
        let mut allocation = MemoryAllocation::new(
            memory::add_layout(
                memory::array_layout::<Word>(exp / 2 + 1), // store res before squaring
                sqr::memory_requirement_exact(exp / 2 + 1),
            ), // memory for squaring
        );
        let mut memory = allocation.memory();

        // res = wbase * wbase
        let mut p = bit_len(exp) - 2;
        let (lo, hi) = split_dword(extend_word(wbase) * extend_word(wbase));
        res.push(lo);
        res.push(hi);

        loop {
            if exp & (1 << p) != 0 {
                let carry = mul::mul_word_in_place(&mut res, wbase);
                res.push_resizing(carry); // actually never resize
            }
            if p == 0 {
                break;
            }
            p -= 1;

            // res = square(res)
            let (tmp, mut memory) = memory.allocate_slice_copy(&res);
            res.fill(0);
            res.push_zeros(res.len());
            sqr::square(&mut res, tmp, &mut memory);
        }

        // carry out the remaining multiplications
        let pow_rem = base.pow(exp_rem as u32);
        let carry = mul::mul_word_in_place(&mut res, pow_rem);
        res.push_resizing(carry);
        Repr::from_buffer(res)
    }

    pub(crate) fn pow_dword_base(base: DoubleWord, exp: usize) -> Repr {
        debug_assert!(exp > 1);
        debug_assert!(base > Word::MAX as DoubleWord);

        let mut res = Buffer::allocate(2 * exp); // result is at most 2 * exp words
        let mut allocation = MemoryAllocation::new(
            memory::add_layout(
                memory::array_layout::<Word>(exp), // store res before squaring
                sqr::memory_requirement_exact(exp),
            ), // memory for squaring
        );
        let mut memory = allocation.memory();

        // res = base * base
        let mut p = bit_len(exp) - 2;
        let (lo, hi) = math::mul_add_carry_dword(base, base, 0);
        let (n0, n1) = split_dword(lo);
        res.push(n0);
        res.push(n1);
        let (n2, n3) = split_dword(hi);
        res.push(n2);
        res.push(n3);

        loop {
            if exp & (1 << p) != 0 {
                let carry = mul::mul_dword_in_place(&mut res, base);
                if carry > 0 {
                    let (c0, c1) = split_dword(carry);
                    res.push(c0);
                    res.push_resizing(c1); // actually never resize
                }
            }
            if p == 0 {
                break;
            }
            p -= 1;

            // res = square(res)
            let (tmp, mut memory) = memory.allocate_slice_copy(&res);
            res.fill(0);
            res.push_zeros(res.len());
            sqr::square(&mut res, tmp, &mut memory);
        }

        Repr::from_buffer(res)
    }

    pub(crate) fn pow_large_base(base: &[Word], exp: usize) -> Repr {
        debug_assert!(exp > 1);
        let mut p = bit_len(exp) - 2;
        let mut res = mul_ops::repr::square_large(base);
        loop {
            if exp & (1 << p) != 0 {
                res = mul_ops::repr::mul_large(res.as_slice(), base);
            }
            if p == 0 {
                break;
            }
            p -= 1;
            res = mul_ops::repr::square_large(res.as_slice());
        }
        res
    }
}