about summary refs log tree commit diff
path: root/library/core/src
diff options
context:
space:
mode:
authorStuart Cook <Zalathar@users.noreply.github.com>2025-08-30 20:29:07 +1000
committerGitHub <noreply@github.com>2025-08-30 20:29:07 +1000
commitdfbba07012ac533ad4dd1f770347cfd547b7eb8c (patch)
tree3f1a36dccf8f61ffc049d6941dc2afa2a6fb3910 /library/core/src
parentb5c19e839f177592e39691f779b0f8b057471a1a (diff)
parent2c21b884a59814836cbd9386492d9ba71039fc75 (diff)
downloadrust-dfbba07012ac533ad4dd1f770347cfd547b7eb8c.tar.gz
rust-dfbba07012ac533ad4dd1f770347cfd547b7eb8c.zip
Rollup merge of #145776 - ChaiTRex:ilog_specialization, r=joboet
Optimize `.ilog({2,10})` to `.ilog{2,10}()`

Optimize `.ilog({2,10})` to `.ilog{2,10}()`

Inform compiler of optimizations when the base is known at compile time and there's a cheaper method available:

* `{integer}.checked_ilog(2)` -> `{integer}.checked_ilog2()`
* `{integer}.checked_ilog(10)` -> `{integer}.checked_ilog10()`
* `{integer}.ilog(2)` -> `{integer}.ilog2()`
* `{integer}.ilog(10)` -> `{integer}.ilog10()`
Diffstat (limited to 'library/core/src')
-rw-r--r--library/core/src/num/uint_macros.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs
index c556d951034..c1e656fdea2 100644
--- a/library/core/src/num/uint_macros.rs
+++ b/library/core/src/num/uint_macros.rs
@@ -1491,6 +1491,20 @@ macro_rules! uint_impl {
                       without modifying the original"]
         #[inline]
         pub const fn checked_ilog(self, base: Self) -> Option<u32> {
+            // Inform compiler of optimizations when the base is known at
+            // compile time and there's a cheaper method available.
+            //
+            // Note: Like all optimizations, this is not guaranteed to be
+            // applied by the compiler. If you want those specific bases,
+            // use `.checked_ilog2()` or `.checked_ilog10()` directly.
+            if core::intrinsics::is_val_statically_known(base) {
+                if base == 2 {
+                    return self.checked_ilog2();
+                } else if base == 10 {
+                    return self.checked_ilog10();
+                }
+            }
+
             if self <= 0 || base <= 1 {
                 None
             } else if self < base {