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 codegen of fmt_num to delete unreachable panic #122770

Merged
merged 3 commits into from
Nov 14, 2024
Merged
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
14 changes: 14 additions & 0 deletions library/core/benches/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,17 @@ fn write_u64_min(bh: &mut Bencher) {
test::black_box(format!("{}", 0u64));
});
}

#[bench]
fn write_u8_max(bh: &mut Bencher) {
bh.iter(|| {
test::black_box(format!("{}", u8::MAX));
});
}

#[bench]
fn write_u8_min(bh: &mut Bencher) {
bh.iter(|| {
test::black_box(format!("{}", 0u8));
});
}
8 changes: 4 additions & 4 deletions library/core/src/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,23 @@ unsafe trait GenericRadix: Sized {
if is_nonnegative {
// Accumulate each digit of the number from the least significant
// to the most significant figure.
for byte in buf.iter_mut().rev() {
loop {
let n = x % base; // Get the current place value.
x = x / base; // Deaccumulate the number.
byte.write(Self::digit(n.to_u8())); // Store the digit in the buffer.
curr -= 1;
buf[curr].write(Self::digit(n.to_u8())); // Store the digit in the buffer.
if x == zero {
// No more digits left to accumulate.
break;
};
}
} else {
// Do the same as above, but accounting for two's complement.
for byte in buf.iter_mut().rev() {
loop {
let n = zero - (x % base); // Get the current place value.
x = x / base; // Deaccumulate the number.
byte.write(Self::digit(n.to_u8())); // Store the digit in the buffer.
curr -= 1;
buf[curr].write(Self::digit(n.to_u8())); // Store the digit in the buffer.
if x == zero {
// No more digits left to accumulate.
break;
Expand Down
Loading