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

Improve implementations of UpperExp and LowerExp traits on integers #131149

Closed
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
23 changes: 14 additions & 9 deletions library/core/src/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ macro_rules! impl_Display {
#[cfg(not(feature = "optimize_for_size"))]
impl $t {
fn _fmt(mut self: $t, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
const SIZE: usize = $t::MAX.ilog(10) as usize + 1;
const SIZE: usize = $t::MAX.ilog10() as usize + 1;
let mut buf = [MaybeUninit::<u8>::uninit(); SIZE];
let mut curr = SIZE;
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
Expand Down Expand Up @@ -359,7 +359,8 @@ macro_rules! impl_Exp {
mut n: $u,
is_nonnegative: bool,
upper: bool,
f: &mut fmt::Formatter<'_>
f: &mut fmt::Formatter<'_>,
buf: &mut [MaybeUninit::<u8>],
) -> fmt::Result {
let (mut n, mut exponent, trailing_zeros, added_precision) = {
let mut exponent = 0;
Expand Down Expand Up @@ -405,9 +406,8 @@ macro_rules! impl_Exp {

// Since `curr` always decreases by the number of digits copied, this means
// that `curr >= 0`.
let mut buf = [MaybeUninit::<u8>::uninit(); 40];
let mut curr = buf.len(); //index for buf
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
let buf_ptr = MaybeUninit::slice_as_mut_ptr(buf);
let lut_ptr = DEC_DIGITS_LUT.as_ptr();

// decode 2 chars at a time
Expand Down Expand Up @@ -500,10 +500,12 @@ macro_rules! impl_Exp {
// convert the negative num to positive by summing 1 to its 2s complement
(!self.$conv_fn()).wrapping_add(1)
};
$name(n, is_nonnegative, false, f)
const SIZE: usize = $t::MAX.ilog10() as usize + 1;
let mut buf = [MaybeUninit::<u8>::uninit(); SIZE];
$name(n, is_nonnegative, false, f, &mut buf)
}
})*
$(
}

#[stable(feature = "integer_exp_format", since = "1.42.0")]
impl fmt::UpperExp for $t {
#[allow(unused_comparisons)]
Expand All @@ -515,9 +517,12 @@ macro_rules! impl_Exp {
// convert the negative num to positive by summing 1 to its 2s complement
(!self.$conv_fn()).wrapping_add(1)
};
$name(n, is_nonnegative, true, f)
const SIZE: usize = $t::MAX.ilog10() as usize + 1;
let mut buf = [MaybeUninit::<u8>::uninit(); SIZE];
$name(n, is_nonnegative, true, f, &mut buf)
}
})*
}
)*
};
}

Expand Down
Loading