From d760c3318391dbaf215f40e53de09a2ce2d2e253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Falk=20H=C3=BCffner?= Date: Sun, 5 Sep 2021 17:09:21 +0200 Subject: [PATCH] Change return type for T::{log,log2,log10} to u32. The value is at most 128, and this is consistent with using u32 for small values elsewhere (e.g. BITS, count_ones, leading_zeros). --- library/core/src/num/int_log10.rs | 4 ++-- library/core/src/num/int_macros.rs | 19 ++++++++----------- library/core/src/num/uint_macros.rs | 19 ++++++++----------- library/core/tests/num/int_log.rs | 22 +++++++++++----------- 4 files changed, 29 insertions(+), 35 deletions(-) diff --git a/library/core/src/num/int_log10.rs b/library/core/src/num/int_log10.rs index a23ca51ef87..e4599067f85 100644 --- a/library/core/src/num/int_log10.rs +++ b/library/core/src/num/int_log10.rs @@ -116,8 +116,8 @@ pub const fn i128(val: i128) -> u32 { macro_rules! impl_checked { ($T:ident) => { - pub const fn $T(val: $T) -> Option<$T> { - if val > 0 { Some(unchecked::$T(val) as $T) } else { None } + pub const fn $T(val: $T) -> Option { + if val > 0 { Some(unchecked::$T(val)) } else { None } } }; } diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs index 780d6c34c91..77643290cc4 100644 --- a/library/core/src/num/int_macros.rs +++ b/library/core/src/num/int_macros.rs @@ -2026,7 +2026,7 @@ pub const fn checked_next_multiple_of(self, rhs: Self) -> Option { #[track_caller] #[rustc_inherit_overflow_checks] #[allow(arithmetic_overflow)] - pub const fn log(self, base: Self) -> Self { + pub const fn log(self, base: Self) -> u32 { match self.checked_log(base) { Some(n) => n, None => { @@ -2060,7 +2060,7 @@ pub const fn log(self, base: Self) -> Self { #[track_caller] #[rustc_inherit_overflow_checks] #[allow(arithmetic_overflow)] - pub const fn log2(self) -> Self { + pub const fn log2(self) -> u32 { match self.checked_log2() { Some(n) => n, None => { @@ -2094,7 +2094,7 @@ pub const fn log2(self) -> Self { #[track_caller] #[rustc_inherit_overflow_checks] #[allow(arithmetic_overflow)] - pub const fn log10(self) -> Self { + pub const fn log10(self) -> u32 { match self.checked_log10() { Some(n) => n, None => { @@ -2125,7 +2125,7 @@ pub const fn log10(self) -> Self { #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] - pub const fn checked_log(self, base: Self) -> Option { + pub const fn checked_log(self, base: Self) -> Option { if self <= 0 || base <= 1 { None } else { @@ -2161,12 +2161,12 @@ pub const fn checked_log(self, base: Self) -> Option { #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] - pub const fn checked_log2(self) -> Option { + pub const fn checked_log2(self) -> Option { if self <= 0 { None } else { // SAFETY: We just checked that this number is positive - let log = (Self::BITS - 1) as Self - unsafe { intrinsics::ctlz_nonzero(self) }; + let log = (Self::BITS - 1) - unsafe { intrinsics::ctlz_nonzero(self) as u32 }; Some(log) } } @@ -2185,11 +2185,8 @@ pub const fn checked_log2(self) -> Option { #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] - pub const fn checked_log10(self) -> Option { - match int_log10::$ActualT(self as $ActualT) { - Some(s) => Some(s as Self), - None => None, - } + pub const fn checked_log10(self) -> Option { + int_log10::$ActualT(self as $ActualT) } /// Computes the absolute value of `self`. diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index 02a5ed4ca80..46e64c33b84 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -660,7 +660,7 @@ pub const fn checked_rem_euclid(self, rhs: Self) -> Option { #[track_caller] #[rustc_inherit_overflow_checks] #[allow(arithmetic_overflow)] - pub const fn log(self, base: Self) -> Self { + pub const fn log(self, base: Self) -> u32 { match self.checked_log(base) { Some(n) => n, None => { @@ -694,7 +694,7 @@ pub const fn log(self, base: Self) -> Self { #[track_caller] #[rustc_inherit_overflow_checks] #[allow(arithmetic_overflow)] - pub const fn log2(self) -> Self { + pub const fn log2(self) -> u32 { match self.checked_log2() { Some(n) => n, None => { @@ -728,7 +728,7 @@ pub const fn log2(self) -> Self { #[track_caller] #[rustc_inherit_overflow_checks] #[allow(arithmetic_overflow)] - pub const fn log10(self) -> Self { + pub const fn log10(self) -> u32 { match self.checked_log10() { Some(n) => n, None => { @@ -759,7 +759,7 @@ pub const fn log10(self) -> Self { #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] - pub const fn checked_log(self, base: Self) -> Option { + pub const fn checked_log(self, base: Self) -> Option { if self <= 0 || base <= 1 { None } else { @@ -795,12 +795,12 @@ pub const fn checked_log(self, base: Self) -> Option { #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] - pub const fn checked_log2(self) -> Option { + pub const fn checked_log2(self) -> Option { if self <= 0 { None } else { // SAFETY: We just checked that this number is positive - let log = (Self::BITS - 1) as Self - unsafe { intrinsics::ctlz_nonzero(self) }; + let log = (Self::BITS - 1) - unsafe { intrinsics::ctlz_nonzero(self) as u32 }; Some(log) } } @@ -819,11 +819,8 @@ pub const fn checked_log2(self) -> Option { #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] - pub const fn checked_log10(self) -> Option { - match int_log10::$ActualT(self as $ActualT) { - Some(s) => Some(s as Self), - None => None, - } + pub const fn checked_log10(self) -> Option { + int_log10::$ActualT(self as $ActualT) } /// Checked negation. Computes `-self`, returning `None` unless `self == diff --git a/library/core/tests/num/int_log.rs b/library/core/tests/num/int_log.rs index 51122c11ce1..1517e8a952f 100644 --- a/library/core/tests/num/int_log.rs +++ b/library/core/tests/num/int_log.rs @@ -26,10 +26,10 @@ fn checked_log() { assert_eq!(i.checked_log(4), None); } for i in 1..=i16::MAX { - assert_eq!(i.checked_log(13), Some((i as f32).log(13.0) as i16)); + assert_eq!(i.checked_log(13), Some((i as f32).log(13.0) as u32)); } for i in 1..=u16::MAX { - assert_eq!(i.checked_log(13), Some((i as f32).log(13.0) as u16)); + assert_eq!(i.checked_log(13), Some((i as f32).log(13.0) as u32)); } } @@ -46,19 +46,19 @@ fn checked_log2() { assert_eq!(0i16.checked_log2(), None); for i in 1..=u8::MAX { - assert_eq!(i.checked_log2(), Some((i as f32).log2() as u8)); + assert_eq!(i.checked_log2(), Some((i as f32).log2() as u32)); } for i in 1..=u16::MAX { // Guard against Android's imprecise f32::log2 implementation. if i != 8192 && i != 32768 { - assert_eq!(i.checked_log2(), Some((i as f32).log2() as u16)); + assert_eq!(i.checked_log2(), Some((i as f32).log2() as u32)); } } for i in i8::MIN..=0 { assert_eq!(i.checked_log2(), None); } for i in 1..=i8::MAX { - assert_eq!(i.checked_log2(), Some((i as f32).log2() as i8)); + assert_eq!(i.checked_log2(), Some((i as f32).log2() as u32)); } for i in i16::MIN..=0 { assert_eq!(i.checked_log2(), None); @@ -66,7 +66,7 @@ fn checked_log2() { for i in 1..=i16::MAX { // Guard against Android's imprecise f32::log2 implementation. if i != 8192 { - assert_eq!(i.checked_log2(), Some((i as f32).log2() as i16)); + assert_eq!(i.checked_log2(), Some((i as f32).log2() as u32)); } } } @@ -75,9 +75,9 @@ fn checked_log2() { #[test] #[cfg(not(target_os = "android"))] fn checked_log2_not_android() { - assert_eq!(8192u16.checked_log2(), Some((8192f32).log2() as u16)); - assert_eq!(32768u16.checked_log2(), Some((32768f32).log2() as u16)); - assert_eq!(8192i16.checked_log2(), Some((8192f32).log2() as i16)); + assert_eq!(8192u16.checked_log2(), Some((8192f32).log2() as u32)); + assert_eq!(32768u16.checked_log2(), Some((32768f32).log2() as u32)); + assert_eq!(8192i16.checked_log2(), Some((8192f32).log2() as u32)); } #[test] @@ -91,10 +91,10 @@ fn checked_log10() { assert_eq!(i.checked_log10(), None); } for i in 1..=i16::MAX { - assert_eq!(i.checked_log10(), Some((i as f32).log10() as i16)); + assert_eq!(i.checked_log10(), Some((i as f32).log10() as u32)); } for i in 1..=u16::MAX { - assert_eq!(i.checked_log10(), Some((i as f32).log10() as u16)); + assert_eq!(i.checked_log10(), Some((i as f32).log10() as u32)); } } -- GitLab