From 3c6330bf36305265302023ec9c50a58181ee79db Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 2 Oct 2024 11:35:15 +0200 Subject: [PATCH] Improve implementations of `UpperExp` and `LowerExp` traits on integers --- library/core/src/fmt/num.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/library/core/src/fmt/num.rs b/library/core/src/fmt/num.rs index aecd725eca561..1dd3aba3bae5f 100644 --- a/library/core/src/fmt/num.rs +++ b/library/core/src/fmt/num.rs @@ -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::::uninit(); SIZE]; let mut curr = SIZE; let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf); @@ -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::], ) -> fmt::Result { let (mut n, mut exponent, trailing_zeros, added_precision) = { let mut exponent = 0; @@ -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::::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 @@ -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::::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)] @@ -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::::uninit(); SIZE]; + $name(n, is_nonnegative, true, f, &mut buf) } - })* + } + )* }; }