-
Notifications
You must be signed in to change notification settings - Fork 13.3k
special case for integer log10 #86930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
mod unchecked { | ||
// 0 < val <= u8::MAX | ||
pub const fn u8(val: u8) -> u32 { | ||
if val >= 100 { | ||
2 | ||
} else if val >= 10 { | ||
1 | ||
} else { | ||
0 | ||
} | ||
} | ||
|
||
// 0 < val <= u16::MAX | ||
pub const fn u16(val: u16) -> u32 { | ||
if val >= 10_000 { | ||
4 | ||
} else if val >= 1000 { | ||
3 | ||
} else if val >= 100 { | ||
2 | ||
} else if val >= 10 { | ||
1 | ||
} else { | ||
0 | ||
} | ||
} | ||
|
||
// 0 < val < 100_000_000 | ||
const fn less_than_8(mut val: u32) -> u32 { | ||
let mut log = 0; | ||
if val >= 10_000 { | ||
val /= 10_000; | ||
log += 4; | ||
} | ||
log + if val >= 1000 { | ||
3 | ||
} else if val >= 100 { | ||
2 | ||
} else if val >= 10 { | ||
1 | ||
} else { | ||
0 | ||
} | ||
} | ||
|
||
// 0 < val <= u32::MAX | ||
pub const fn u32(mut val: u32) -> u32 { | ||
let mut log = 0; | ||
if val >= 100_000_000 { | ||
val /= 100_000_000; | ||
log += 8; | ||
} | ||
log + less_than_8(val) | ||
} | ||
|
||
// 0 < val < 10_000_000_000_000_000 | ||
const fn less_than_16(mut val: u64) -> u32 { | ||
let mut log = 0; | ||
if val >= 100_000_000 { | ||
val /= 100_000_000; | ||
log += 8; | ||
} | ||
log + less_than_8(val as u32) | ||
} | ||
|
||
// 0 < val <= u64::MAX | ||
pub const fn u64(mut val: u64) -> u32 { | ||
let mut log = 0; | ||
if val >= 10_000_000_000_000_000 { | ||
val /= 10_000_000_000_000_000; | ||
log += 16; | ||
} | ||
log + less_than_16(val) | ||
} | ||
|
||
// 0 < val <= u128::MAX | ||
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 { | ||
val /= 100_000_000_000_000_000_000_000_000_000_000; | ||
log += 32; | ||
return log + less_than_8(val as u32); | ||
} | ||
if val >= 10_000_000_000_000_000 { | ||
val /= 10_000_000_000_000_000; | ||
log += 16; | ||
} | ||
log + less_than_16(val as u64) | ||
} | ||
|
||
// 0 < val <= i8::MAX | ||
pub const fn i8(val: i8) -> u32 { | ||
u8(val as u8) | ||
} | ||
|
||
// 0 < val <= i16::MAX | ||
pub const fn i16(val: i16) -> u32 { | ||
u16(val as u16) | ||
} | ||
|
||
// 0 < val <= i32::MAX | ||
pub const fn i32(val: i32) -> u32 { | ||
u32(val as u32) | ||
} | ||
|
||
// 0 < val <= i64::MAX | ||
pub const fn i64(val: i64) -> u32 { | ||
u64(val as u64) | ||
} | ||
|
||
// 0 < val <= i128::MAX | ||
pub const fn i128(val: i128) -> u32 { | ||
u128(val as u128) | ||
} | ||
} | ||
|
||
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 } | ||
} | ||
}; | ||
} | ||
|
||
impl_checked! { u8 } | ||
impl_checked! { u16 } | ||
impl_checked! { u32 } | ||
impl_checked! { u64 } | ||
impl_checked! { u128 } | ||
impl_checked! { i8 } | ||
impl_checked! { i16 } | ||
impl_checked! { i32 } | ||
impl_checked! { i64 } | ||
impl_checked! { i128 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tried doing a binary search instead a linear chain of inequalities? I'm not sure if it would actually be faster due to unpredictable branches.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did try it and it seemed to be slightly slower (just over the error thresholds), so I thought it better to leave the simpler code since there was no clear gain.