@@ -1491,18 +1491,25 @@ macro_rules! uint_impl {
14911491 without modifying the original"]
14921492 #[ inline]
14931493 pub const fn checked_ilog( self , base: Self ) -> Option <u32 > {
1494- // Preconditions for calling inner function `slow_ilog`:
1495- //
1496- // 1: base >= 2
1497- // 2: x >= base
1498- #[ inline( always) ]
1499- const fn slow_ilog( x: $SelfT, base: $SelfT) -> u32 {
1500- // Since x >= base, n >= 1
1494+ if core:: intrinsics:: is_val_statically_known( base) {
1495+ if base == 2 {
1496+ return self . checked_ilog2( ) ;
1497+ } else if base == 10 {
1498+ return self . checked_ilog10( ) ;
1499+ }
1500+ }
1501+
1502+ if self <= 0 || base <= 1 {
1503+ None
1504+ } else if self < base {
1505+ Some ( 0 )
1506+ } else {
1507+ // Since base >= self, n >= 1
15011508 let mut n = 1 ;
15021509 let mut r = base;
15031510
15041511 // Optimization for 128 bit wide integers.
1505- if <$SelfT> :: BITS == 128 {
1512+ if Self :: BITS == 128 {
15061513 // The following is a correct lower bound for ⌊log(base,self)⌋ because
15071514 //
15081515 // log(base,self) = log(2,self) / log(2,base)
@@ -1511,59 +1518,15 @@ macro_rules! uint_impl {
15111518 // hence
15121519 //
15131520 // ⌊log(base,self)⌋ ≥ ⌊ ⌊log(2,self)⌋ / (⌊log(2,base)⌋ + 1) ⌋ .
1514- n = x . ilog2( ) / ( base. ilog2( ) + 1 ) ;
1521+ n = self . ilog2( ) / ( base. ilog2( ) + 1 ) ;
15151522 r = base. pow( n) ;
15161523 }
15171524
1518- while r <= x / base {
1525+ while r <= self / base {
15191526 n += 1 ;
15201527 r *= base;
15211528 }
1522- n
1523- }
1524-
1525- if core:: intrinsics:: is_val_statically_known( self ) {
1526- if self <= 0 {
1527- None
1528- } else if core:: intrinsics:: is_val_statically_known( base) {
1529- if base <= 1 { // precondition 1
1530- None
1531- } else if self < base { // precondition 2
1532- Some ( 0 )
1533- } else if base == 2 {
1534- self . checked_ilog2( )
1535- } else if base == 10 {
1536- self . checked_ilog10( )
1537- } else { // all preconditions satisfied
1538- Some ( slow_ilog( self , base) )
1539- }
1540- } else if base <= 1 { // precondition 1
1541- None
1542- } else if self < base { // precondition 2
1543- Some ( 0 )
1544- } else { // all preconditions satisfied
1545- Some ( slow_ilog( self , base) )
1546- }
1547- } else if core:: intrinsics:: is_val_statically_known( base) {
1548- if base <= 1 { // precondition 1
1549- None
1550- } else if base == 2 {
1551- self . checked_ilog2( )
1552- } else if base == 10 {
1553- self . checked_ilog10( )
1554- } else if self <= 0 {
1555- None
1556- } else if self < base { // precondition 2
1557- Some ( 0 )
1558- } else { // all preconditions satisfied
1559- Some ( slow_ilog( self , base) )
1560- }
1561- } else if self <= 0 || base <= 1 { // precondition 1
1562- None
1563- } else if self < base { // precondition 2
1564- Some ( 0 )
1565- } else { // all preconditions satisfied
1566- Some ( slow_ilog( self , base) )
1529+ Some ( n)
15671530 }
15681531 }
15691532
0 commit comments