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:
parent
e8d948a3d2
commit
22722ac55d
25
src/lib.rs
25
src/lib.rs
|
@ -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 {
|
||||
if exp == 0 { return T::one() }
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
// avoid overflow if we won't need it
|
||||
if exp > 1 {
|
||||
base = base.clone() * base;
|
||||
}
|
||||
exp = exp >> 1;
|
||||
}
|
||||
acc
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
Loading…
Reference in New Issue