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
use num::arithmetic::traits::{OverflowingPow, OverflowingPowAssign, Parity, UnsignedAbs};
use num::basic::signeds::PrimitiveSigned;
use num::basic::unsigneds::PrimitiveUnsigned;
use num::conversion::traits::OverflowingFrom;
use num::logic::traits::BitIterable;

fn overflowing_pow_unsigned<T: PrimitiveUnsigned>(x: T, exp: u64) -> (T, bool) {
    if exp == 0 {
        (T::ONE, false)
    } else if x < T::TWO {
        (x, false)
    } else {
        let (mut power, mut overflow) = (x, false);
        for bit in exp.bits().rev().skip(1) {
            overflow |= power.overflowing_square_assign();
            if bit {
                overflow |= power.overflowing_mul_assign(x);
            }
        }
        (power, overflow)
    }
}

fn overflowing_unsigned_to_signed_neg<
    U: PrimitiveUnsigned,
    S: OverflowingFrom<U> + PrimitiveSigned,
>(
    x: U,
) -> (S, bool) {
    let (signed_x, overflow) = S::overflowing_from(x);
    if signed_x == S::MIN {
        (signed_x, false)
    } else {
        (signed_x.wrapping_neg(), overflow)
    }
}

fn overflowing_pow_signed<
    U: PrimitiveUnsigned,
    S: OverflowingFrom<U> + PrimitiveSigned + UnsignedAbs<Output = U>,
>(
    x: S,
    exp: u64,
) -> (S, bool) {
    let (p_abs, overflow) = OverflowingPow::overflowing_pow(x.unsigned_abs(), exp);
    let (p, overflow_2) = if x >= S::ZERO || exp.even() {
        S::overflowing_from(p_abs)
    } else {
        overflowing_unsigned_to_signed_neg(p_abs)
    };
    (p, overflow || overflow_2)
}

macro_rules! impl_overflowing_pow_unsigned {
    ($t:ident) => {
        impl OverflowingPow<u64> for $t {
            type Output = $t;

            /// This is a wrapper over the `overflowing_pow` functions in the standard library, for
            /// example [this one](u32::overflowing_pow).
            #[inline]
            fn overflowing_pow(self, exp: u64) -> ($t, bool) {
                overflowing_pow_unsigned(self, exp)
            }
        }
    };
}
apply_to_unsigneds!(impl_overflowing_pow_unsigned);

macro_rules! impl_overflowing_pow_signed {
    ($t:ident) => {
        impl OverflowingPow<u64> for $t {
            type Output = $t;

            /// This is a wrapper over the `overflowing_pow` functions in the standard library, for
            /// example [this one](i32::overflowing_pow).
            #[inline]
            fn overflowing_pow(self, exp: u64) -> ($t, bool) {
                overflowing_pow_signed(self, exp)
            }
        }
    };
}
apply_to_signeds!(impl_overflowing_pow_signed);

macro_rules! impl_overflowing_pow_primitive_int {
    ($t:ident) => {
        impl OverflowingPowAssign<u64> for $t {
            /// Raises a number to a power, in place.
            ///
            /// Returns a boolean indicating whether an arithmetic overflow occurred. If an
            /// overflow occurred, then the wrapped value is assigned.
            ///
            /// # Worst-case complexity
            /// $T(n) = O(n)$
            ///
            /// $M(n) = O(1)$
            ///
            /// where $T$ is time, $M$ is additional memory, and $n$ is `exp.significant_bits()`.
            ///
            /// # Examples
            /// See [here](super::overflowing_pow#overflowing_pow_assign).
            #[inline]
            fn overflowing_pow_assign(&mut self, exp: u64) -> bool {
                let overflow;
                (*self, overflow) = OverflowingPow::overflowing_pow(*self, exp);
                overflow
            }
        }
    };
}
apply_to_primitive_ints!(impl_overflowing_pow_primitive_int);