@@ -1491,18 +1491,25 @@ macro_rules! uint_impl {
1491
1491
without modifying the original"]
1492
1492
#[ inline]
1493
1493
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
1501
1508
let mut n = 1 ;
1502
1509
let mut r = base;
1503
1510
1504
1511
// Optimization for 128 bit wide integers.
1505
- if <$SelfT> :: BITS == 128 {
1512
+ if Self :: BITS == 128 {
1506
1513
// The following is a correct lower bound for ⌊log(base,self)⌋ because
1507
1514
//
1508
1515
// log(base,self) = log(2,self) / log(2,base)
@@ -1511,59 +1518,15 @@ macro_rules! uint_impl {
1511
1518
// hence
1512
1519
//
1513
1520
// ⌊log(base,self)⌋ ≥ ⌊ ⌊log(2,self)⌋ / (⌊log(2,base)⌋ + 1) ⌋ .
1514
- n = x . ilog2( ) / ( base. ilog2( ) + 1 ) ;
1521
+ n = self . ilog2( ) / ( base. ilog2( ) + 1 ) ;
1515
1522
r = base. pow( n) ;
1516
1523
}
1517
1524
1518
- while r <= x / base {
1525
+ while r <= self / base {
1519
1526
n += 1 ;
1520
1527
r *= base;
1521
1528
}
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)
1567
1530
}
1568
1531
}
1569
1532
0 commit comments