Skip to content

Commit fc6f99a

Browse files
committed
panic_bounds_check elimination from format buffer
1 parent c06d03f commit fc6f99a

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

library/core/src/fmt/num.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ macro_rules! impl_Display {
239239
const MAX_DEC_N: usize = $unsigned::MAX.ilog(10) as usize + 1;
240240
let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
241241
// Leading zero count & write index in buf.
242-
let mut offset = MAX_DEC_N;
242+
let mut offset = buf.len();
243243
// Consume decimals from working copy until none left.
244244
let mut remain = self;
245245

@@ -248,34 +248,45 @@ macro_rules! impl_Display {
248248
#[allow(overflowing_literals)]
249249
#[allow(unused_comparisons)]
250250
while offset >= 4 && remain > 999 {
251+
// SAFETY: Offset from the initial buf.len() gets deducted
252+
// with underflow checks exclusively.
253+
unsafe { core::hint::assert_unchecked(offset <= buf.len()) }
254+
offset -= 4;
255+
251256
let quad = remain % 100_00;
252257
remain /= 100_00;
253258
let p1 = (quad / 100) as usize * 2;
254259
let p2 = (quad % 100) as usize * 2;
255-
offset -= 4;
256260
buf[offset + 0].write(DEC_DIGITS_LUT[p1 + 0]);
257261
buf[offset + 1].write(DEC_DIGITS_LUT[p1 + 1]);
258262
buf[offset + 2].write(DEC_DIGITS_LUT[p2 + 0]);
259263
buf[offset + 3].write(DEC_DIGITS_LUT[p2 + 1]);
260264
}
261265

262266
// Format per two digits from the lookup table.
263-
while offset >= 2 && remain > 9 {
267+
if offset >= 2 && remain > 9 {
268+
// SAFETY: Offset from the initial buf.len() gets deducted
269+
// with underflow checks exclusively.
270+
unsafe { core::hint::assert_unchecked(offset <= buf.len()) }
271+
offset -= 2;
272+
264273
let p = (remain % 100) as usize * 2;
265274
remain /= 100;
266-
offset -= 2;
267275
buf[offset + 0].write(DEC_DIGITS_LUT[p + 0]);
268276
buf[offset + 1].write(DEC_DIGITS_LUT[p + 1]);
269277
}
270278

271279
// Format the last remaining digit, if any.
272280
if offset != 0 && remain != 0 || offset == MAX_DEC_N {
281+
// SAFETY: Offset from the initial buf.len() gets deducted
282+
// with underflow checks exclusively.
283+
unsafe { core::hint::assert_unchecked(offset <= buf.len()) }
284+
offset -= 1;
285+
273286
// Either the compiler sees that remain < 10, or it prevents
274287
// a boundary check up next.
275288
let p = (remain % 10) as usize * 2;
276289
// not used: remain = 0;
277-
278-
offset -= 1;
279290
buf[offset].write(DEC_DIGITS_LUT[p + 1]);
280291
}
281292

0 commit comments

Comments
 (0)