Skip to content

Commit 00035b5

Browse files
committed
checked_ilog: improve performance
1 parent 8ed1d4a commit 00035b5

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

library/core/src/num/int_macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2477,18 +2477,18 @@ macro_rules! int_impl {
24772477
None
24782478
} else {
24792479
let mut n = 0;
2480-
let mut r = self;
2480+
let mut r = 1;
24812481

24822482
// Optimization for 128 bit wide integers.
24832483
if Self::BITS == 128 {
24842484
let b = Self::ilog2(self) / (Self::ilog2(base) + 1);
24852485
n += b;
2486-
r /= base.pow(b as u32);
2486+
r *= base.pow(b);
24872487
}
24882488

2489-
while r >= base {
2490-
r /= base;
2489+
while r <= self / base {
24912490
n += 1;
2491+
r *= base;
24922492
}
24932493
Some(n)
24942494
}

library/core/src/num/uint_macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -810,18 +810,18 @@ macro_rules! uint_impl {
810810
None
811811
} else {
812812
let mut n = 0;
813-
let mut r = self;
813+
let mut r = 1;
814814

815815
// Optimization for 128 bit wide integers.
816816
if Self::BITS == 128 {
817817
let b = Self::ilog2(self) / (Self::ilog2(base) + 1);
818818
n += b;
819-
r /= base.pow(b as u32);
819+
r *= base.pow(b);
820820
}
821821

822-
while r >= base {
823-
r /= base;
822+
while r <= self / base {
824823
n += 1;
824+
r *= base;
825825
}
826826
Some(n)
827827
}

0 commit comments

Comments
 (0)