Skip to content
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

Use log10 for optimizations #89737

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
#![feature(fmt_internals)]
#![feature(fn_traits)]
#![feature(inplace_iteration)]
#![feature(int_log)]
#![feature(iter_advance_by)]
#![feature(layout_for_ptr)]
#![feature(maybe_uninit_slice)]
Expand Down
16 changes: 6 additions & 10 deletions library/core/src/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,18 +309,13 @@ macro_rules! impl_Exp {
let (added_precision, subtracted_precision) = match f.precision() {
Some(fmt_prec) => {
// number of decimal digits minus 1
let mut tmp = n;
let mut prec = 0;
while tmp >= 10 {
tmp /= 10;
prec += 1;
}
let prec = n.checked_log10().unwrap_or(0) as usize;
(fmt_prec.saturating_sub(prec), prec.saturating_sub(fmt_prec))
}
None => (0,0)
None => (0, 0)
};
for _ in 1..subtracted_precision {
n/=10;
n /= 10;
exponent += 1;
}
if subtracted_precision != 0 {
Expand Down Expand Up @@ -392,7 +387,7 @@ macro_rules! impl_Exp {
// SAFETY: In either case, `exp_buf` is written within bounds and `exp_ptr[..len]`
// is contained within `exp_buf` since `len <= 3`.
let exp_slice = unsafe {
*exp_ptr.offset(0) = if upper {b'E'} else {b'e'};
*exp_ptr.offset(0) = if upper { b'E' } else { b'e' };
let len = if exponent < 10 {
*exp_ptr.offset(1) = (exponent as u8) + b'0';
2
Expand Down Expand Up @@ -482,7 +477,8 @@ impl_Exp!(i128, u128 as u128 via to_u128 named exp_u128);
fn parse_u64_into<const N: usize>(mut n: u64, buf: &mut [MaybeUninit<u8>; N], curr: &mut isize) {
let buf_ptr = MaybeUninit::slice_as_mut_ptr(buf);
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
assert!(*curr > 19);
const MIN_SIZE: isize = u64::MAX.log10() as isize;
assert!(*curr > MIN_SIZE);

// SAFETY:
// Writes at most 19 characters into the buffer. Guaranteed that any ptr into LUT is at most
Expand Down
6 changes: 1 addition & 5 deletions src/librustdoc/html/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,7 @@ crate fn print_src(
let lines = s.lines().count();
let mut line_numbers = Buffer::empty_from(buf);
let mut cols = 0;
let mut tmp = lines;
while tmp > 0 {
cols += 1;
tmp /= 10;
}
cols += lines.checked_log10().map(|cols| cols + 1).unwrap_or(0) as usize;
line_numbers.write_str("<pre class=\"line-numbers\">");
match source_context {
SourceContext::Standalone => {
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#![feature(iter_intersperse)]
#![feature(type_alias_impl_trait)]
#![feature(generic_associated_types)]
#![feature(int_log)]
#![recursion_limit = "256"]
#![warn(rustc::internal)]
#![allow(clippy::collapsible_if, clippy::collapsible_else_if)]
Expand Down