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
use crate::num::arithmetic::traits::{Parity, SaturatingPow, SaturatingPowAssign};
use crate::num::basic::signeds::PrimitiveSigned;
use crate::num::basic::unsigneds::PrimitiveUnsigned;

fn saturating_pow_unsigned<T: PrimitiveUnsigned>(x: T, exp: u64) -> T {
    if exp == 0 {
        T::ONE
    } else if x < T::TWO {
        x
    } else if let Some(p) = x.checked_pow(exp) {
        p
    } else {
        T::MAX
    }
}

fn saturating_pow_signed<T: PrimitiveSigned>(x: T, exp: u64) -> T {
    if exp == 0 {
        T::ONE
    } else if x == T::ZERO || x == T::ONE {
        x
    } else if x == T::NEGATIVE_ONE {
        if exp.even() {
            T::ONE
        } else {
            T::NEGATIVE_ONE
        }
    } else if let Some(p) = x.checked_pow(exp) {
        p
    } else if x > T::ZERO || exp.even() {
        T::MAX
    } else {
        T::MIN
    }
}

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

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

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

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

macro_rules! impl_saturating_pow_primitive_int {
    ($t:ident) => {
        impl SaturatingPowAssign<u64> for $t {
            /// Raises a number to a power, in place, saturating at the numeric bounds instead of
            /// overflowing.
            ///
            /// $$
            /// x \gets \\begin{cases}
            ///     x^y & \text{if} \\quad m \leq x^y \leq M, \\\\
            ///     M & \text{if} \\quad x^y > M, \\\\
            ///     m & \text{if} \\quad x^y < m,
            /// \\end{cases}
            /// $$
            /// where $m$ is `Self::MIN` and $M$ is `Self::MAX`.
            ///
            /// # Worst-case complexity
            /// Constant time and additional memory.
            ///
            /// # Examples
            /// See [here](super::saturating_pow#saturating_pow_assign).
            #[inline]
            fn saturating_pow_assign(&mut self, exp: u64) {
                *self = SaturatingPow::saturating_pow(*self, exp);
            }
        }
    };
}
apply_to_primitive_ints!(impl_saturating_pow_primitive_int);