Skip to content

Commit

Permalink
checked_ilog: add comments explaining the correctness of the 128 bit …
Browse files Browse the repository at this point in the history
…optimization
  • Loading branch information
FedericoStra committed Sep 18, 2023
1 parent 666d26f commit 84a865c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2481,6 +2481,14 @@ macro_rules! int_impl {

// Optimization for 128 bit wide integers.
if Self::BITS == 128 {
// The following is a correct lower bound for ⌊log(base,self)⌋ because
//
// log(base,self) = log(2,self) / log(2,base)
// ≥ ⌊log(2,self)⌋ / (⌊log(2,base)⌋ + 1)
//
// hence
//
// ⌊log(base,self)⌋ ≥ ⌊ ⌊log(2,self)⌋ / (⌊log(2,base)⌋ + 1) ⌋ .
n = self.ilog2() / (base.ilog2() + 1);
r = base.pow(n);
}
Expand Down
8 changes: 8 additions & 0 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,14 @@ macro_rules! uint_impl {

// Optimization for 128 bit wide integers.
if Self::BITS == 128 {
// The following is a correct lower bound for ⌊log(base,self)⌋ because
//
// log(base,self) = log(2,self) / log(2,base)
// ≥ ⌊log(2,self)⌋ / (⌊log(2,base)⌋ + 1)
//
// hence
//
// ⌊log(base,self)⌋ ≥ ⌊ ⌊log(2,self)⌋ / (⌊log(2,base)⌋ + 1) ⌋ .
n = self.ilog2() / (base.ilog2() + 1);
r = base.pow(n);
}
Expand Down

0 comments on commit 84a865c

Please sign in to comment.