Skip to content

Commit

Permalink
Add #[inline] to int log10 functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Oct 12, 2021
1 parent 9475e60 commit a6bb1fb
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions library/core/src/num/int_log10.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod unchecked {
// 0 < val <= u8::MAX
#[inline]
pub const fn u8(val: u8) -> u32 {
let val = val as u32;

Expand All @@ -20,6 +21,7 @@ mod unchecked {
}

// 0 < val < 100_000
#[inline]
const fn less_than_5(val: u32) -> u32 {
// Similar to u8, when adding one of these constants to val,
// we get two possible bit patterns above the low 17 bits,
Expand All @@ -40,11 +42,13 @@ mod unchecked {
}

// 0 < val <= u16::MAX
#[inline]
pub const fn u16(val: u16) -> u32 {
less_than_5(val as u32)
}

// 0 < val <= u32::MAX
#[inline]
pub const fn u32(mut val: u32) -> u32 {
let mut log = 0;
if val >= 100_000 {
Expand All @@ -55,6 +59,7 @@ mod unchecked {
}

// 0 < val <= u64::MAX
#[inline]
pub const fn u64(mut val: u64) -> u32 {
let mut log = 0;
if val >= 10_000_000_000 {
Expand All @@ -69,6 +74,7 @@ mod unchecked {
}

// 0 < val <= u128::MAX
#[inline]
pub const fn u128(mut val: u128) -> u32 {
let mut log = 0;
if val >= 100_000_000_000_000_000_000_000_000_000_000 {
Expand All @@ -84,33 +90,39 @@ mod unchecked {
}

// 0 < val <= i8::MAX
#[inline]
pub const fn i8(val: i8) -> u32 {
u8(val as u8)
}

// 0 < val <= i16::MAX
#[inline]
pub const fn i16(val: i16) -> u32 {
u16(val as u16)
}

// 0 < val <= i32::MAX
#[inline]
pub const fn i32(val: i32) -> u32 {
u32(val as u32)
}

// 0 < val <= i64::MAX
#[inline]
pub const fn i64(val: i64) -> u32 {
u64(val as u64)
}

// 0 < val <= i128::MAX
#[inline]
pub const fn i128(val: i128) -> u32 {
u128(val as u128)
}
}

macro_rules! impl_checked {
($T:ident) => {
#[inline]
pub const fn $T(val: $T) -> Option<u32> {
if val > 0 { Some(unchecked::$T(val)) } else { None }
}
Expand Down

0 comments on commit a6bb1fb

Please sign in to comment.