Skip to content

Commit 84a865c

Browse files
committed
checked_ilog: add comments explaining the correctness of the 128 bit optimization
1 parent 666d26f commit 84a865c

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

library/core/src/num/int_macros.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2481,6 +2481,14 @@ macro_rules! int_impl {
24812481

24822482
// Optimization for 128 bit wide integers.
24832483
if Self::BITS == 128 {
2484+
// The following is a correct lower bound for ⌊log(base,self)⌋ because
2485+
//
2486+
// log(base,self) = log(2,self) / log(2,base)
2487+
// ≥ ⌊log(2,self)⌋ / (⌊log(2,base)⌋ + 1)
2488+
//
2489+
// hence
2490+
//
2491+
// ⌊log(base,self)⌋ ≥ ⌊ ⌊log(2,self)⌋ / (⌊log(2,base)⌋ + 1) ⌋ .
24842492
n = self.ilog2() / (base.ilog2() + 1);
24852493
r = base.pow(n);
24862494
}

library/core/src/num/uint_macros.rs

+8
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,14 @@ macro_rules! uint_impl {
814814

815815
// Optimization for 128 bit wide integers.
816816
if Self::BITS == 128 {
817+
// The following is a correct lower bound for ⌊log(base,self)⌋ because
818+
//
819+
// log(base,self) = log(2,self) / log(2,base)
820+
// ≥ ⌊log(2,self)⌋ / (⌊log(2,base)⌋ + 1)
821+
//
822+
// hence
823+
//
824+
// ⌊log(base,self)⌋ ≥ ⌊ ⌊log(2,self)⌋ / (⌊log(2,base)⌋ + 1) ⌋ .
817825
n = self.ilog2() / (base.ilog2() + 1);
818826
r = base.pow(n);
819827
}

0 commit comments

Comments
 (0)