pow: shift exp powers of 2 before accumulating

We can save a multiplication if we start the accumulation basically at
the first set bit of the exponent, rather than starting at one and
waiting to multiply.  We only need one itself if the exponent is zero,
which is easy to pre-check.

Before:
    test pow_bench         ... bench:   8,267,370 ns/iter (+/- 93,319)

After:
    test pow_bench         ... bench:   7,506,463 ns/iter (+/- 116,311)
This commit is contained in:
Josh Stone 2016-01-06 18:27:50 -08:00
parent e8d948a3d2
commit 22722ac55d
1 changed files with 15 additions and 14 deletions

View File

@ -141,22 +141,23 @@ pub fn abs_sub<T: Signed>(x: T, y: T) -> T {
/// ```
#[inline]
pub fn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) -> T {
if exp == 1 { base }
else {
let mut acc = one::<T>();
while exp > 0 {
if (exp & 1) == 1 {
acc = acc * base.clone();
}
if exp == 0 { return T::one() }
// avoid overflow if we won't need it
if exp > 1 {
base = base.clone() * base;
}
exp = exp >> 1;
}
acc
while exp & 1 == 0 {
base = base.clone() * base;
exp >>= 1;
}
if exp == 1 { return base }
let mut acc = base.clone();
while exp > 1 {
exp >>= 1;
base = base.clone() * base;
if exp & 1 == 1 {
acc = acc * base.clone();
}
}
acc
}
#[cfg(test)]