pow uses shifts and zero comp

This commit is contained in:
NikVolf 2016-07-13 11:29:18 +02:00
parent 420f2ad6c4
commit 02ecb6b37b

View File

@ -713,9 +713,8 @@ macro_rules! construct_uint {
/// Fast exponentation by squaring /// Fast exponentation by squaring
/// https://en.wikipedia.org/wiki/Exponentiation_by_squaring /// https://en.wikipedia.org/wiki/Exponentiation_by_squaring
fn overflowing_pow(self, expon: Self) -> (Self, bool) { fn overflowing_pow(self, expon: Self) -> (Self, bool) {
if expon == Self::zero() { if expon.is_zero() { return (Self::one(), false) }
return (Self::one(), false)
}
let is_even = |x : &Self| x.low_u64() & 1 == 0; let is_even = |x : &Self| x.low_u64() & 1 == 0;
let u_one = Self::one(); let u_one = Self::one();
@ -728,11 +727,11 @@ macro_rules! construct_uint {
while n > u_one { while n > u_one {
if is_even(&n) { if is_even(&n) {
x = overflowing!(x.overflowing_mul(x), overflow); x = overflowing!(x.overflowing_mul(x), overflow);
n = n / u_two; n = n >> 1;
} else { } else {
y = overflowing!(x.overflowing_mul(y), overflow); y = overflowing!(x.overflowing_mul(y), overflow);
x = overflowing!(x.overflowing_mul(x), overflow); x = overflowing!(x.overflowing_mul(x), overflow);
n = (n - u_one) / u_two; n = (n - u_one) >> 1;
} }
} }
let res = overflowing!(x.overflowing_mul(y), overflow); let res = overflowing!(x.overflowing_mul(y), overflow);