Skip to content

Commit d760c33

Browse files
author
Falk Hüffner
committed
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).
1 parent 7e1e3eb commit d760c33

File tree

4 files changed

+29
-35
lines changed

4 files changed

+29
-35
lines changed

library/core/src/num/int_log10.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ mod unchecked {
116116

117117
macro_rules! impl_checked {
118118
($T:ident) => {
119-
pub const fn $T(val: $T) -> Option<$T> {
120-
if val > 0 { Some(unchecked::$T(val) as $T) } else { None }
119+
pub const fn $T(val: $T) -> Option<u32> {
120+
if val > 0 { Some(unchecked::$T(val)) } else { None }
121121
}
122122
};
123123
}

library/core/src/num/int_macros.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -2026,7 +2026,7 @@ macro_rules! int_impl {
20262026
#[track_caller]
20272027
#[rustc_inherit_overflow_checks]
20282028
#[allow(arithmetic_overflow)]
2029-
pub const fn log(self, base: Self) -> Self {
2029+
pub const fn log(self, base: Self) -> u32 {
20302030
match self.checked_log(base) {
20312031
Some(n) => n,
20322032
None => {
@@ -2060,7 +2060,7 @@ macro_rules! int_impl {
20602060
#[track_caller]
20612061
#[rustc_inherit_overflow_checks]
20622062
#[allow(arithmetic_overflow)]
2063-
pub const fn log2(self) -> Self {
2063+
pub const fn log2(self) -> u32 {
20642064
match self.checked_log2() {
20652065
Some(n) => n,
20662066
None => {
@@ -2094,7 +2094,7 @@ macro_rules! int_impl {
20942094
#[track_caller]
20952095
#[rustc_inherit_overflow_checks]
20962096
#[allow(arithmetic_overflow)]
2097-
pub const fn log10(self) -> Self {
2097+
pub const fn log10(self) -> u32 {
20982098
match self.checked_log10() {
20992099
Some(n) => n,
21002100
None => {
@@ -2125,7 +2125,7 @@ macro_rules! int_impl {
21252125
#[must_use = "this returns the result of the operation, \
21262126
without modifying the original"]
21272127
#[inline]
2128-
pub const fn checked_log(self, base: Self) -> Option<Self> {
2128+
pub const fn checked_log(self, base: Self) -> Option<u32> {
21292129
if self <= 0 || base <= 1 {
21302130
None
21312131
} else {
@@ -2161,12 +2161,12 @@ macro_rules! int_impl {
21612161
#[must_use = "this returns the result of the operation, \
21622162
without modifying the original"]
21632163
#[inline]
2164-
pub const fn checked_log2(self) -> Option<Self> {
2164+
pub const fn checked_log2(self) -> Option<u32> {
21652165
if self <= 0 {
21662166
None
21672167
} else {
21682168
// SAFETY: We just checked that this number is positive
2169-
let log = (Self::BITS - 1) as Self - unsafe { intrinsics::ctlz_nonzero(self) };
2169+
let log = (Self::BITS - 1) - unsafe { intrinsics::ctlz_nonzero(self) as u32 };
21702170
Some(log)
21712171
}
21722172
}
@@ -2185,11 +2185,8 @@ macro_rules! int_impl {
21852185
#[must_use = "this returns the result of the operation, \
21862186
without modifying the original"]
21872187
#[inline]
2188-
pub const fn checked_log10(self) -> Option<Self> {
2189-
match int_log10::$ActualT(self as $ActualT) {
2190-
Some(s) => Some(s as Self),
2191-
None => None,
2192-
}
2188+
pub const fn checked_log10(self) -> Option<u32> {
2189+
int_log10::$ActualT(self as $ActualT)
21932190
}
21942191

21952192
/// Computes the absolute value of `self`.

library/core/src/num/uint_macros.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ macro_rules! uint_impl {
660660
#[track_caller]
661661
#[rustc_inherit_overflow_checks]
662662
#[allow(arithmetic_overflow)]
663-
pub const fn log(self, base: Self) -> Self {
663+
pub const fn log(self, base: Self) -> u32 {
664664
match self.checked_log(base) {
665665
Some(n) => n,
666666
None => {
@@ -694,7 +694,7 @@ macro_rules! uint_impl {
694694
#[track_caller]
695695
#[rustc_inherit_overflow_checks]
696696
#[allow(arithmetic_overflow)]
697-
pub const fn log2(self) -> Self {
697+
pub const fn log2(self) -> u32 {
698698
match self.checked_log2() {
699699
Some(n) => n,
700700
None => {
@@ -728,7 +728,7 @@ macro_rules! uint_impl {
728728
#[track_caller]
729729
#[rustc_inherit_overflow_checks]
730730
#[allow(arithmetic_overflow)]
731-
pub const fn log10(self) -> Self {
731+
pub const fn log10(self) -> u32 {
732732
match self.checked_log10() {
733733
Some(n) => n,
734734
None => {
@@ -759,7 +759,7 @@ macro_rules! uint_impl {
759759
#[must_use = "this returns the result of the operation, \
760760
without modifying the original"]
761761
#[inline]
762-
pub const fn checked_log(self, base: Self) -> Option<Self> {
762+
pub const fn checked_log(self, base: Self) -> Option<u32> {
763763
if self <= 0 || base <= 1 {
764764
None
765765
} else {
@@ -795,12 +795,12 @@ macro_rules! uint_impl {
795795
#[must_use = "this returns the result of the operation, \
796796
without modifying the original"]
797797
#[inline]
798-
pub const fn checked_log2(self) -> Option<Self> {
798+
pub const fn checked_log2(self) -> Option<u32> {
799799
if self <= 0 {
800800
None
801801
} else {
802802
// SAFETY: We just checked that this number is positive
803-
let log = (Self::BITS - 1) as Self - unsafe { intrinsics::ctlz_nonzero(self) };
803+
let log = (Self::BITS - 1) - unsafe { intrinsics::ctlz_nonzero(self) as u32 };
804804
Some(log)
805805
}
806806
}
@@ -819,11 +819,8 @@ macro_rules! uint_impl {
819819
#[must_use = "this returns the result of the operation, \
820820
without modifying the original"]
821821
#[inline]
822-
pub const fn checked_log10(self) -> Option<Self> {
823-
match int_log10::$ActualT(self as $ActualT) {
824-
Some(s) => Some(s as Self),
825-
None => None,
826-
}
822+
pub const fn checked_log10(self) -> Option<u32> {
823+
int_log10::$ActualT(self as $ActualT)
827824
}
828825

829826
/// Checked negation. Computes `-self`, returning `None` unless `self ==

library/core/tests/num/int_log.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ fn checked_log() {
2626
assert_eq!(i.checked_log(4), None);
2727
}
2828
for i in 1..=i16::MAX {
29-
assert_eq!(i.checked_log(13), Some((i as f32).log(13.0) as i16));
29+
assert_eq!(i.checked_log(13), Some((i as f32).log(13.0) as u32));
3030
}
3131
for i in 1..=u16::MAX {
32-
assert_eq!(i.checked_log(13), Some((i as f32).log(13.0) as u16));
32+
assert_eq!(i.checked_log(13), Some((i as f32).log(13.0) as u32));
3333
}
3434
}
3535

@@ -46,27 +46,27 @@ fn checked_log2() {
4646
assert_eq!(0i16.checked_log2(), None);
4747

4848
for i in 1..=u8::MAX {
49-
assert_eq!(i.checked_log2(), Some((i as f32).log2() as u8));
49+
assert_eq!(i.checked_log2(), Some((i as f32).log2() as u32));
5050
}
5151
for i in 1..=u16::MAX {
5252
// Guard against Android's imprecise f32::log2 implementation.
5353
if i != 8192 && i != 32768 {
54-
assert_eq!(i.checked_log2(), Some((i as f32).log2() as u16));
54+
assert_eq!(i.checked_log2(), Some((i as f32).log2() as u32));
5555
}
5656
}
5757
for i in i8::MIN..=0 {
5858
assert_eq!(i.checked_log2(), None);
5959
}
6060
for i in 1..=i8::MAX {
61-
assert_eq!(i.checked_log2(), Some((i as f32).log2() as i8));
61+
assert_eq!(i.checked_log2(), Some((i as f32).log2() as u32));
6262
}
6363
for i in i16::MIN..=0 {
6464
assert_eq!(i.checked_log2(), None);
6565
}
6666
for i in 1..=i16::MAX {
6767
// Guard against Android's imprecise f32::log2 implementation.
6868
if i != 8192 {
69-
assert_eq!(i.checked_log2(), Some((i as f32).log2() as i16));
69+
assert_eq!(i.checked_log2(), Some((i as f32).log2() as u32));
7070
}
7171
}
7272
}
@@ -75,9 +75,9 @@ fn checked_log2() {
7575
#[test]
7676
#[cfg(not(target_os = "android"))]
7777
fn checked_log2_not_android() {
78-
assert_eq!(8192u16.checked_log2(), Some((8192f32).log2() as u16));
79-
assert_eq!(32768u16.checked_log2(), Some((32768f32).log2() as u16));
80-
assert_eq!(8192i16.checked_log2(), Some((8192f32).log2() as i16));
78+
assert_eq!(8192u16.checked_log2(), Some((8192f32).log2() as u32));
79+
assert_eq!(32768u16.checked_log2(), Some((32768f32).log2() as u32));
80+
assert_eq!(8192i16.checked_log2(), Some((8192f32).log2() as u32));
8181
}
8282

8383
#[test]
@@ -91,10 +91,10 @@ fn checked_log10() {
9191
assert_eq!(i.checked_log10(), None);
9292
}
9393
for i in 1..=i16::MAX {
94-
assert_eq!(i.checked_log10(), Some((i as f32).log10() as i16));
94+
assert_eq!(i.checked_log10(), Some((i as f32).log10() as u32));
9595
}
9696
for i in 1..=u16::MAX {
97-
assert_eq!(i.checked_log10(), Some((i as f32).log10() as u16));
97+
assert_eq!(i.checked_log10(), Some((i as f32).log10() as u32));
9898
}
9999
}
100100

0 commit comments

Comments
 (0)