Skip to content

Commit 9ab0f63

Browse files
committed
Optimize pow when self is a power of two
if base == 2 ** k, then (2 ** k) ** n == 2 ** (k * n) == 1 << (k * n)
1 parent 8948390 commit 9ab0f63

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

library/core/src/num/uint_macros.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3315,6 +3315,16 @@ macro_rules! uint_impl {
33153315
let mut acc = 1;
33163316

33173317
if intrinsics::is_val_statically_known(exp) {
3318+
// change of base:
3319+
// if base == 2 ** k, then
3320+
// (2 ** k) ** n
3321+
// == 2 ** (k * n)
3322+
// == 1 << (k * n)
3323+
if self.is_power_of_two() {
3324+
let k = self.ilog2();
3325+
return 1 << (k * exp)
3326+
}
3327+
33183328
while exp > 1 {
33193329
if (exp & 1) == 1 {
33203330
acc = acc * base;

0 commit comments

Comments
 (0)