From 4046385d609f73d32069ec0680a92b6d04d96761 Mon Sep 17 00:00:00 2001 From: EFanZh Date: Sat, 29 Nov 2025 09:52:19 +0800 Subject: [PATCH] Add `ilog10` result range hints --- library/core/src/num/int_log10.rs | 105 +++++---- library/core/src/num/int_macros.rs | 6 +- library/core/src/num/nonzero.rs | 2 +- .../lib-optimizations/ilog10-range.rs | 213 ++++++++++++++++++ 4 files changed, 279 insertions(+), 47 deletions(-) create mode 100644 tests/codegen-llvm/lib-optimizations/ilog10-range.rs diff --git a/library/core/src/num/int_log10.rs b/library/core/src/num/int_log10.rs index 649a736b6e7b5..af8e1f90968d6 100644 --- a/library/core/src/num/int_log10.rs +++ b/library/core/src/num/int_log10.rs @@ -1,9 +1,11 @@ //! These functions compute the integer logarithm of their type, assuming //! that someone has already checked that the value is strictly positive. +use crate::num::NonZero; + // 0 < val <= u8::MAX #[inline] -pub(super) const fn u8(val: u8) -> u32 { +const fn u8_impl(val: u8) -> u32 { let val = val as u32; // For better performance, avoid branches by assembling the solution @@ -45,13 +47,13 @@ const fn less_than_5(val: u32) -> u32 { // 0 < val <= u16::MAX #[inline] -pub(super) const fn u16(val: u16) -> u32 { +const fn u16_impl(val: u16) -> u32 { less_than_5(val as u32) } // 0 < val <= u32::MAX #[inline] -pub(super) const fn u32(mut val: u32) -> u32 { +const fn u32_impl(mut val: u32) -> u32 { let mut log = 0; if val >= 100_000 { val /= 100_000; @@ -62,7 +64,7 @@ pub(super) const fn u32(mut val: u32) -> u32 { // 0 < val <= u64::MAX #[inline] -pub(super) const fn u64(mut val: u64) -> u32 { +const fn u64_impl(mut val: u64) -> u32 { let mut log = 0; if val >= 10_000_000_000 { val /= 10_000_000_000; @@ -77,66 +79,87 @@ pub(super) const fn u64(mut val: u64) -> u32 { // 0 < val <= u128::MAX #[inline] -pub(super) const fn u128(mut val: u128) -> u32 { +const fn u128_impl(mut val: u128) -> u32 { let mut log = 0; if val >= 100_000_000_000_000_000_000_000_000_000_000 { val /= 100_000_000_000_000_000_000_000_000_000_000; log += 32; - return log + u32(val as u32); + return log + u32_impl(val as u32); } if val >= 10_000_000_000_000_000 { val /= 10_000_000_000_000_000; log += 16; } - log + u64(val as u64) + log + u64_impl(val as u64) } -#[cfg(target_pointer_width = "16")] -#[inline] -pub(super) const fn usize(val: usize) -> u32 { - u16(val as _) -} +macro_rules! define_unsigned_ilog10 { + ($($ty:ident => $impl_fn:ident,)*) => {$( + #[inline] + pub(super) const fn $ty(val: NonZero<$ty>) -> u32 { + let result = $impl_fn(val.get()); -#[cfg(target_pointer_width = "32")] -#[inline] -pub(super) const fn usize(val: usize) -> u32 { - u32(val as _) -} + // SAFETY: Integer logarithm is monotonic non-decreasing, so the computed `result` cannot + // exceed the value produced for the maximum input. + unsafe { crate::hint::assert_unchecked(result <= const { $impl_fn($ty::MAX) }) }; -#[cfg(target_pointer_width = "64")] -#[inline] -pub(super) const fn usize(val: usize) -> u32 { - u64(val as _) + result + } + )*}; } -// 0 < val <= i8::MAX -#[inline] -pub(super) const fn i8(val: i8) -> u32 { - u8(val as u8) +define_unsigned_ilog10! { + u8 => u8_impl, + u16 => u16_impl, + u32 => u32_impl, + u64 => u64_impl, + u128 => u128_impl, } -// 0 < val <= i16::MAX #[inline] -pub(super) const fn i16(val: i16) -> u32 { - u16(val as u16) -} +pub(super) const fn usize(val: NonZero) -> u32 { + #[cfg(target_pointer_width = "16")] + let impl_fn = u16; -// 0 < val <= i32::MAX -#[inline] -pub(super) const fn i32(val: i32) -> u32 { - u32(val as u32) + #[cfg(target_pointer_width = "32")] + let impl_fn = u32; + + #[cfg(target_pointer_width = "64")] + let impl_fn = u64; + + // SAFETY: We have selected the correct `impl_fn`, so the converting `val` to the argument is + // safe. + impl_fn(unsafe { NonZero::new_unchecked(val.get() as _) }) } -// 0 < val <= i64::MAX -#[inline] -pub(super) const fn i64(val: i64) -> u32 { - u64(val as u64) +macro_rules! define_signed_ilog10 { + ($($ty:ident => $impl_fn:ident,)*) => {$( + // 0 < val <= $ty::MAX + #[inline] + pub(super) const fn $ty(val: $ty) -> Option { + if val > 0 { + let result = $impl_fn(val.cast_unsigned()); + + // SAFETY: Integer logarithm is monotonic non-decreasing, so the computed `result` + // cannot exceed the value produced for the maximum input. + unsafe { + crate::hint::assert_unchecked(result <= const { $impl_fn($ty::MAX.cast_unsigned()) }); + } + + Some(result) + } else { + None + } + } + )*}; } -// 0 < val <= i128::MAX -#[inline] -pub(super) const fn i128(val: i128) -> u32 { - u128(val as u128) +define_signed_ilog10! { + i8 => u8_impl, + i16 => u16_impl, + i32 => u32_impl, + i64 => u64_impl, + i128 => u128_impl, } /// Instantiate this panic logic once, rather than for all the ilog methods diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs index 62f83a3a14d94..3e1749855d730 100644 --- a/library/core/src/num/int_macros.rs +++ b/library/core/src/num/int_macros.rs @@ -3527,11 +3527,7 @@ macro_rules! int_impl { without modifying the original"] #[inline] pub const fn checked_ilog10(self) -> Option { - if self > 0 { - Some(int_log10::$ActualT(self as $ActualT)) - } else { - None - } + int_log10::$ActualT(self as $ActualT) } /// Computes the absolute value of `self`. diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index ee375dbaaab2d..2b5279efb7f79 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -1657,7 +1657,7 @@ macro_rules! nonzero_integer_signedness_dependent_methods { without modifying the original"] #[inline] pub const fn ilog10(self) -> u32 { - super::int_log10::$Int(self.get()) + super::int_log10::$Int(self) } /// Calculates the midpoint (average) between `self` and `rhs`. diff --git a/tests/codegen-llvm/lib-optimizations/ilog10-range.rs b/tests/codegen-llvm/lib-optimizations/ilog10-range.rs new file mode 100644 index 0000000000000..b466855389fb4 --- /dev/null +++ b/tests/codegen-llvm/lib-optimizations/ilog10-range.rs @@ -0,0 +1,213 @@ +//! Make sure the compiler knows the result range of `ilog10`. + +//@ compile-flags: -O -Z merge-functions=disabled + +#![crate_type = "lib"] + +use std::num::NonZero; + +// Signed integers. + +#[no_mangle] +fn i8_ilog10_range(value: i8) { + const MAX_RESULT: u32 = i8::MAX.ilog10(); + + // CHECK-LABEL: @i8_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value <= 0 || value.ilog10() <= MAX_RESULT); + assert!(value.checked_ilog10().is_none_or(|result| result <= MAX_RESULT)); +} + +#[no_mangle] +fn i16_ilog10_range(value: i16) { + const MAX_RESULT: u32 = i16::MAX.ilog10(); + + // CHECK-LABEL: @i16_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value <= 0 || value.ilog10() <= MAX_RESULT); + assert!(value.checked_ilog10().is_none_or(|result| result <= MAX_RESULT)); +} + +#[no_mangle] +fn i32_ilog10_range(value: i32) { + const MAX_RESULT: u32 = i32::MAX.ilog10(); + + // CHECK-LABEL: @i32_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value <= 0 || value.ilog10() <= MAX_RESULT); + assert!(value.checked_ilog10().is_none_or(|result| result <= MAX_RESULT)); +} + +#[no_mangle] +fn i64_ilog10_range(value: i64) { + const MAX_RESULT: u32 = i64::MAX.ilog10(); + + // CHECK-LABEL: @i64_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value <= 0 || value.ilog10() <= MAX_RESULT); + assert!(value.checked_ilog10().is_none_or(|result| result <= MAX_RESULT)); +} + +#[no_mangle] +fn i128_ilog10_range(value: i128) { + const MAX_RESULT: u32 = i128::MAX.ilog10(); + + // CHECK-LABEL: @i128_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value <= 0 || value.ilog10() <= MAX_RESULT); + assert!(value.checked_ilog10().is_none_or(|result| result <= MAX_RESULT)); +} + +#[no_mangle] +fn isize_ilog10_range(value: isize) { + const MAX_RESULT: u32 = isize::MAX.ilog10(); + + // CHECK-LABEL: @isize_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value <= 0 || value.ilog10() <= MAX_RESULT); + assert!(value.checked_ilog10().is_none_or(|result| result <= MAX_RESULT)); +} + +// Unsigned integer types. + +#[no_mangle] +fn u8_ilog10_range(value: u8) { + const MAX_RESULT: u32 = u8::MAX.ilog10(); + + // CHECK-LABEL: @u8_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value == 0 || value.ilog10() <= MAX_RESULT); + assert!(value.checked_ilog10().is_none_or(|result| result <= MAX_RESULT)); +} + +#[no_mangle] +fn u16_ilog10_range(value: u16) { + const MAX_RESULT: u32 = u16::MAX.ilog10(); + + // CHECK-LABEL: @u16_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value == 0 || value.ilog10() <= MAX_RESULT); + assert!(value.checked_ilog10().is_none_or(|result| result <= MAX_RESULT)); +} + +#[no_mangle] +fn u32_ilog10_range(value: u32) { + const MAX_RESULT: u32 = u32::MAX.ilog10(); + + // CHECK-LABEL: @u32_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value == 0 || value.ilog10() <= MAX_RESULT); + assert!(value.checked_ilog10().is_none_or(|result| result <= MAX_RESULT)); +} + +#[no_mangle] +fn u64_ilog10_range(value: u64) { + const MAX_RESULT: u32 = u64::MAX.ilog10(); + + // CHECK-LABEL: @u64_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value == 0 || value.ilog10() <= MAX_RESULT); + assert!(value.checked_ilog10().is_none_or(|result| result <= MAX_RESULT)); +} + +#[no_mangle] +fn u128_ilog10_range(value: u128) { + const MAX_RESULT: u32 = u128::MAX.ilog10(); + + // CHECK-LABEL: @u128_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value == 0 || value.ilog10() <= MAX_RESULT); + assert!(value.checked_ilog10().is_none_or(|result| result <= MAX_RESULT)); +} + +#[no_mangle] +fn usize_ilog10_range(value: usize) { + const MAX_RESULT: u32 = usize::MAX.ilog10(); + + // CHECK-LABEL: @usize_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value == 0 || value.ilog10() <= MAX_RESULT); + assert!(value.checked_ilog10().is_none_or(|result| result <= MAX_RESULT)); +} + +// Signed non-zero integers do not have `ilog10` methods. + +// Unsigned non-zero integers. + +#[no_mangle] +fn non_zero_u8_ilog10_range(value: NonZero) { + // CHECK-LABEL: @non_zero_u8_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value.ilog10() <= const { u8::MAX.ilog10() }); +} + +#[no_mangle] +fn non_zero_u16_ilog10_range(value: NonZero) { + // CHECK-LABEL: @non_zero_u16_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value.ilog10() <= const { u16::MAX.ilog10() }); +} + +#[no_mangle] +fn non_zero_u32_ilog10_range(value: NonZero) { + // CHECK-LABEL: @non_zero_u32_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value.ilog10() <= const { u32::MAX.ilog10() }); +} + +#[no_mangle] +fn non_zero_u64_ilog10_range(value: NonZero) { + // CHECK-LABEL: @non_zero_u64_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value.ilog10() <= const { u64::MAX.ilog10() }); +} + +#[no_mangle] +fn non_zero_u128_ilog10_range(value: NonZero) { + // CHECK-LABEL: @non_zero_u128_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value.ilog10() <= const { u128::MAX.ilog10() }); +} + +#[no_mangle] +fn non_zero_usize_ilog10_range(value: NonZero) { + // CHECK-LABEL: @non_zero_usize_ilog10_range( + // CHECK-NOT: panic + // CHECK: ret void + // CHECK-NEXT: } + assert!(value.ilog10() <= const { usize::MAX.ilog10() }); +}