Skip to content

Commit 2faa397

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 547b713 commit 2faa397

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
@@ -3317,6 +3317,16 @@ macro_rules! uint_impl {
33173317
let mut acc = 1;
33183318

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

0 commit comments

Comments
 (0)