core/fmt/num.rs
1//! Integer and floating-point number formatting
2
3use crate::fmt::NumBuffer;
4use crate::mem::MaybeUninit;
5use crate::num::fmt as numfmt;
6use crate::{fmt, str};
7
8/// Formatting of integers with a non-decimal radix.
9macro_rules! radix_integer {
10 (fmt::$Trait:ident for $Signed:ident and $Unsigned:ident, $prefix:literal, $dig_tab:literal) => {
11 #[stable(feature = "rust1", since = "1.0.0")]
12 impl fmt::$Trait for $Unsigned {
13 /// Format unsigned integers in the radix.
14 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15 // Check macro arguments at compile time.
16 const {
17 assert!($Unsigned::MIN == 0, "need unsigned");
18 assert!($dig_tab.is_ascii(), "need single-byte entries");
19 }
20
21 // ASCII digits in ascending order are used as a lookup table.
22 const DIG_TAB: &[u8] = $dig_tab;
23 const BASE: $Unsigned = DIG_TAB.len() as $Unsigned;
24 const MAX_DIG_N: usize = $Unsigned::MAX.ilog(BASE) as usize + 1;
25
26 // Buffer digits of self with right alignment.
27 let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DIG_N];
28 // Count the number of bytes in buf that are not initialized.
29 let mut offset = buf.len();
30
31 // Accumulate each digit of the number from the least
32 // significant to the most significant figure.
33 let mut remain = *self;
34 loop {
35 let digit = remain % BASE;
36 remain /= BASE;
37
38 offset -= 1;
39 // SAFETY: `remain` will reach 0 and we will break before `offset` wraps
40 unsafe { core::hint::assert_unchecked(offset < buf.len()) }
41 buf[offset].write(DIG_TAB[digit as usize]);
42 if remain == 0 {
43 break;
44 }
45 }
46
47 // SAFETY: Starting from `offset`, all elements of the slice have been set.
48 let digits = unsafe { slice_buffer_to_str(&buf, offset) };
49 f.pad_integral(true, $prefix, digits)
50 }
51 }
52
53 #[stable(feature = "rust1", since = "1.0.0")]
54 impl fmt::$Trait for $Signed {
55 /// Format signed integers in the two’s-complement form.
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 fmt::$Trait::fmt(&self.cast_unsigned(), f)
58 }
59 }
60 };
61}
62
63/// Formatting of integers with a non-decimal radix.
64macro_rules! radix_integers {
65 ($Signed:ident, $Unsigned:ident) => {
66 radix_integer! { fmt::Binary for $Signed and $Unsigned, "0b", b"01" }
67 radix_integer! { fmt::Octal for $Signed and $Unsigned, "0o", b"01234567" }
68 radix_integer! { fmt::LowerHex for $Signed and $Unsigned, "0x", b"0123456789abcdef" }
69 radix_integer! { fmt::UpperHex for $Signed and $Unsigned, "0x", b"0123456789ABCDEF" }
70 };
71}
72radix_integers! { isize, usize }
73radix_integers! { i8, u8 }
74radix_integers! { i16, u16 }
75radix_integers! { i32, u32 }
76radix_integers! { i64, u64 }
77radix_integers! { i128, u128 }
78
79macro_rules! impl_Debug {
80 ($($T:ident)*) => {
81 $(
82 #[stable(feature = "rust1", since = "1.0.0")]
83 impl fmt::Debug for $T {
84 #[inline]
85 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86 if f.debug_lower_hex() {
87 fmt::LowerHex::fmt(self, f)
88 } else if f.debug_upper_hex() {
89 fmt::UpperHex::fmt(self, f)
90 } else {
91 fmt::Display::fmt(self, f)
92 }
93 }
94 }
95 )*
96 };
97}
98
99// The string of all two-digit numbers in range 00..99 is used as a lookup table.
100static DECIMAL_PAIRS: &[u8; 200] = b"\
101 0001020304050607080910111213141516171819\
102 2021222324252627282930313233343536373839\
103 4041424344454647484950515253545556575859\
104 6061626364656667686970717273747576777879\
105 8081828384858687888990919293949596979899";
106
107/// This function converts a slice of ascii characters into a `&str` starting from `offset`.
108///
109/// # Safety
110///
111/// `buf` content starting from `offset` index MUST BE initialized and MUST BE ascii
112/// characters.
113unsafe fn slice_buffer_to_str(buf: &[MaybeUninit<u8>], offset: usize) -> &str {
114 // SAFETY: `offset` is always included between 0 and `buf`'s length.
115 let written = unsafe { buf.get_unchecked(offset..) };
116 // SAFETY: (`assume_init_ref`) All buf content since offset is set.
117 // SAFETY: (`from_utf8_unchecked`) Writes use ASCII from the lookup table exclusively.
118 unsafe { str::from_utf8_unchecked(written.assume_init_ref()) }
119}
120
121macro_rules! impl_Display {
122 ($($Signed:ident, $Unsigned:ident),* ; as $T:ident into $fmt_fn:ident) => {
123
124 $(
125 const _: () = {
126 assert!($Signed::MIN < 0, "need signed");
127 assert!($Unsigned::MIN == 0, "need unsigned");
128 assert!($Signed::BITS == $Unsigned::BITS, "need counterparts");
129 assert!($Signed::BITS <= $T::BITS, "need lossless conversion");
130 assert!($Unsigned::BITS <= $T::BITS, "need lossless conversion");
131 };
132
133 #[stable(feature = "rust1", since = "1.0.0")]
134 impl fmt::Display for $Unsigned {
135 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136 #[cfg(not(feature = "optimize_for_size"))]
137 {
138 const MAX_DEC_N: usize = $Unsigned::MAX.ilog10() as usize + 1;
139 // Buffer decimals for self with right alignment.
140 let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
141
142 // SAFETY: `buf` is always big enough to contain all the digits.
143 unsafe { f.pad_integral(true, "", self._fmt(&mut buf)) }
144 }
145 #[cfg(feature = "optimize_for_size")]
146 {
147 // Lossless conversion (with as) is asserted at the top of
148 // this macro.
149 ${concat($fmt_fn, _small)}(*self as $T, true, f)
150 }
151 }
152 }
153
154 #[stable(feature = "rust1", since = "1.0.0")]
155 impl fmt::Display for $Signed {
156 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157 #[cfg(not(feature = "optimize_for_size"))]
158 {
159 const MAX_DEC_N: usize = $Unsigned::MAX.ilog10() as usize + 1;
160 // Buffer decimals for self with right alignment.
161 let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
162
163 // SAFETY: `buf` is always big enough to contain all the digits.
164 unsafe { f.pad_integral(*self >= 0, "", self.unsigned_abs()._fmt(&mut buf)) }
165 }
166 #[cfg(feature = "optimize_for_size")]
167 {
168 // Lossless conversion (with as) is asserted at the top of
169 // this macro.
170 return ${concat($fmt_fn, _small)}(self.unsigned_abs() as $T, *self >= 0, f);
171 }
172 }
173 }
174
175 #[cfg(not(feature = "optimize_for_size"))]
176 impl $Unsigned {
177 #[doc(hidden)]
178 #[unstable(
179 feature = "fmt_internals",
180 reason = "specialized method meant to only be used by `SpecToString` implementation",
181 issue = "none"
182 )]
183 pub unsafe fn _fmt<'a>(self, buf: &'a mut [MaybeUninit::<u8>]) -> &'a str {
184 // SAFETY: `buf` will always be big enough to contain all digits.
185 let offset = unsafe { self._fmt_inner(buf) };
186 // SAFETY: Starting from `offset`, all elements of the slice have been set.
187 unsafe { slice_buffer_to_str(buf, offset) }
188 }
189
190 unsafe fn _fmt_inner(self, buf: &mut [MaybeUninit::<u8>]) -> usize {
191 // Count the number of bytes in buf that are not initialized.
192 let mut offset = buf.len();
193 // Consume the least-significant decimals from a working copy.
194 let mut remain = self;
195
196 // Format per four digits from the lookup table.
197 // Four digits need a 16-bit $Unsigned or wider.
198 while size_of::<Self>() > 1 && remain > 999.try_into().expect("branch is not hit for types that cannot fit 999 (u8)") {
199 // SAFETY: All of the decimals fit in buf due to MAX_DEC_N
200 // and the while condition ensures at least 4 more decimals.
201 unsafe { core::hint::assert_unchecked(offset >= 4) }
202 // SAFETY: The offset counts down from its initial buf.len()
203 // without underflow due to the previous precondition.
204 unsafe { core::hint::assert_unchecked(offset <= buf.len()) }
205 offset -= 4;
206
207 // pull two pairs
208 let scale: Self = 1_00_00.try_into().expect("branch is not hit for types that cannot fit 1E4 (u8)");
209 let quad = remain % scale;
210 remain /= scale;
211 let pair1 = (quad / 100) as usize;
212 let pair2 = (quad % 100) as usize;
213 buf[offset + 0].write(DECIMAL_PAIRS[pair1 * 2 + 0]);
214 buf[offset + 1].write(DECIMAL_PAIRS[pair1 * 2 + 1]);
215 buf[offset + 2].write(DECIMAL_PAIRS[pair2 * 2 + 0]);
216 buf[offset + 3].write(DECIMAL_PAIRS[pair2 * 2 + 1]);
217 }
218
219 // Format per two digits from the lookup table.
220 if remain > 9 {
221 // SAFETY: All of the decimals fit in buf due to MAX_DEC_N
222 // and the if condition ensures at least 2 more decimals.
223 unsafe { core::hint::assert_unchecked(offset >= 2) }
224 // SAFETY: The offset counts down from its initial buf.len()
225 // without underflow due to the previous precondition.
226 unsafe { core::hint::assert_unchecked(offset <= buf.len()) }
227 offset -= 2;
228
229 let pair = (remain % 100) as usize;
230 remain /= 100;
231 buf[offset + 0].write(DECIMAL_PAIRS[pair * 2 + 0]);
232 buf[offset + 1].write(DECIMAL_PAIRS[pair * 2 + 1]);
233 }
234
235 // Format the last remaining digit, if any.
236 if remain != 0 || self == 0 {
237 // SAFETY: All of the decimals fit in buf due to MAX_DEC_N
238 // and the if condition ensures (at least) 1 more decimals.
239 unsafe { core::hint::assert_unchecked(offset >= 1) }
240 // SAFETY: The offset counts down from its initial buf.len()
241 // without underflow due to the previous precondition.
242 unsafe { core::hint::assert_unchecked(offset <= buf.len()) }
243 offset -= 1;
244
245 // Either the compiler sees that remain < 10, or it prevents
246 // a boundary check up next.
247 let last = (remain & 15) as usize;
248 buf[offset].write(DECIMAL_PAIRS[last * 2 + 1]);
249 // not used: remain = 0;
250 }
251
252 offset
253 }
254 }
255
256 impl $Signed {
257 /// Allows users to write an integer (in signed decimal format) into a variable `buf` of
258 /// type [`NumBuffer`] that is passed by the caller by mutable reference.
259 ///
260 /// # Examples
261 ///
262 /// ```
263 /// #![feature(int_format_into)]
264 /// use core::fmt::NumBuffer;
265 ///
266 #[doc = concat!("let n = 0", stringify!($Signed), ";")]
267 /// let mut buf = NumBuffer::new();
268 /// assert_eq!(n.format_into(&mut buf), "0");
269 ///
270 #[doc = concat!("let n1 = 32", stringify!($Signed), ";")]
271 /// assert_eq!(n1.format_into(&mut buf), "32");
272 ///
273 #[doc = concat!("let n2 = ", stringify!($Signed::MAX), ";")]
274 #[doc = concat!("assert_eq!(n2.format_into(&mut buf), ", stringify!($Signed::MAX), ".to_string());")]
275 /// ```
276 #[unstable(feature = "int_format_into", issue = "138215")]
277 pub fn format_into(self, buf: &mut NumBuffer<Self>) -> &str {
278 let mut offset;
279
280 #[cfg(not(feature = "optimize_for_size"))]
281 // SAFETY: `buf` will always be big enough to contain all digits.
282 unsafe {
283 offset = self.unsigned_abs()._fmt_inner(&mut buf.buf);
284 }
285 #[cfg(feature = "optimize_for_size")]
286 {
287 // Lossless conversion (with as) is asserted at the top of
288 // this macro.
289 offset = ${concat($fmt_fn, _in_buf_small)}(self.unsigned_abs() as $T, &mut buf.buf);
290 }
291 // Only difference between signed and unsigned are these 4 lines.
292 if self < 0 {
293 offset -= 1;
294 buf.buf[offset].write(b'-');
295 }
296 // SAFETY: Starting from `offset`, all elements of the slice have been set.
297 unsafe { slice_buffer_to_str(&buf.buf, offset) }
298 }
299 }
300
301 impl $Unsigned {
302 /// Allows users to write an integer (in signed decimal format) into a variable `buf` of
303 /// type [`NumBuffer`] that is passed by the caller by mutable reference.
304 ///
305 /// # Examples
306 ///
307 /// ```
308 /// #![feature(int_format_into)]
309 /// use core::fmt::NumBuffer;
310 ///
311 #[doc = concat!("let n = 0", stringify!($Unsigned), ";")]
312 /// let mut buf = NumBuffer::new();
313 /// assert_eq!(n.format_into(&mut buf), "0");
314 ///
315 #[doc = concat!("let n1 = 32", stringify!($Unsigned), ";")]
316 /// assert_eq!(n1.format_into(&mut buf), "32");
317 ///
318 #[doc = concat!("let n2 = ", stringify!($Unsigned::MAX), ";")]
319 #[doc = concat!("assert_eq!(n2.format_into(&mut buf), ", stringify!($Unsigned::MAX), ".to_string());")]
320 /// ```
321 #[unstable(feature = "int_format_into", issue = "138215")]
322 pub fn format_into(self, buf: &mut NumBuffer<Self>) -> &str {
323 let offset;
324
325 #[cfg(not(feature = "optimize_for_size"))]
326 // SAFETY: `buf` will always be big enough to contain all digits.
327 unsafe {
328 offset = self._fmt_inner(&mut buf.buf);
329 }
330 #[cfg(feature = "optimize_for_size")]
331 {
332 // Lossless conversion (with as) is asserted at the top of
333 // this macro.
334 offset = ${concat($fmt_fn, _in_buf_small)}(self as $T, &mut buf.buf);
335 }
336 // SAFETY: Starting from `offset`, all elements of the slice have been set.
337 unsafe { slice_buffer_to_str(&buf.buf, offset) }
338 }
339 }
340
341 )*
342
343 #[cfg(feature = "optimize_for_size")]
344 fn ${concat($fmt_fn, _in_buf_small)}(mut n: $T, buf: &mut [MaybeUninit::<u8>]) -> usize {
345 let mut curr = buf.len();
346
347 // SAFETY: To show that it's OK to copy into `buf_ptr`, notice that at the beginning
348 // `curr == buf.len() == 39 > log(n)` since `n < 2^128 < 10^39`, and at
349 // each step this is kept the same as `n` is divided. Since `n` is always
350 // non-negative, this means that `curr > 0` so `buf_ptr[curr..curr + 1]`
351 // is safe to access.
352 loop {
353 curr -= 1;
354 buf[curr].write((n % 10) as u8 + b'0');
355 n /= 10;
356
357 if n == 0 {
358 break;
359 }
360 }
361 curr
362 }
363
364 #[cfg(feature = "optimize_for_size")]
365 fn ${concat($fmt_fn, _small)}(n: $T, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
366 const MAX_DEC_N: usize = $T::MAX.ilog(10) as usize + 1;
367 let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
368
369 let offset = ${concat($fmt_fn, _in_buf_small)}(n, &mut buf);
370 // SAFETY: Starting from `offset`, all elements of the slice have been set.
371 let buf_slice = unsafe { slice_buffer_to_str(&buf, offset) };
372 f.pad_integral(is_nonnegative, "", buf_slice)
373 }
374 };
375}
376
377macro_rules! impl_Exp {
378 ($($Signed:ident, $Unsigned:ident),* ; as $T:ident into $fmt_fn:ident) => {
379 const _: () = assert!($T::MIN == 0, "need unsigned");
380
381 fn $fmt_fn(
382 f: &mut fmt::Formatter<'_>,
383 n: $T,
384 is_nonnegative: bool,
385 letter_e: u8
386 ) -> fmt::Result {
387 debug_assert!(letter_e.is_ascii_alphabetic(), "single-byte character");
388
389 // Print the integer as a coefficient in range (-10, 10).
390 let mut exp = n.checked_ilog10().unwrap_or(0) as usize;
391 debug_assert!(n / (10 as $T).pow(exp as u32) < 10);
392
393 // Precisison is counted as the number of digits in the fraction.
394 let mut coef_prec = exp;
395 // Keep the digits as an integer (paired with its coef_prec count).
396 let mut coef = n;
397
398 // A Formatter may set the precision to a fixed number of decimals.
399 let more_prec = match f.precision() {
400 None => {
401 // Omit any and all trailing zeroes.
402 while coef_prec != 0 && coef % 10 == 0 {
403 coef /= 10;
404 coef_prec -= 1;
405 }
406 0
407 },
408
409 Some(fmt_prec) if fmt_prec >= coef_prec => {
410 // Count the number of additional zeroes needed.
411 fmt_prec - coef_prec
412 },
413
414 Some(fmt_prec) => {
415 // Count the number of digits to drop.
416 let less_prec = coef_prec - fmt_prec;
417 assert!(less_prec > 0);
418 // Scale down the coefficient/precision pair. For example,
419 // coef 123456 gets coef_prec 5 (to make 1.23456). To format
420 // the number with 2 decimals, i.e., fmt_prec 2, coef should
421 // be scaled by 10⁵⁻²=1000 to get coef 123 with coef_prec 2.
422
423 // SAFETY: Any precision less than coef_prec will cause a
424 // power of ten below the coef value.
425 let scale = unsafe {
426 (10 as $T).checked_pow(less_prec as u32).unwrap_unchecked()
427 };
428 let floor = coef / scale;
429 // Round half to even conform documentation.
430 let over = coef % scale;
431 let half = scale / 2;
432 let round_up = if over < half {
433 0
434 } else if over > half {
435 1
436 } else {
437 floor & 1 // round odd up to even
438 };
439 // Adding one to a scale down of at least 10 won't overflow.
440 coef = floor + round_up;
441 coef_prec = fmt_prec;
442
443 // The round_up may have caused the coefficient to reach 10
444 // (which is not permitted). For example, anything in range
445 // [9.95, 10) becomes 10.0 when adjusted to precision 1.
446 if round_up != 0 && coef.checked_ilog10().unwrap_or(0) as usize > coef_prec {
447 debug_assert_eq!(coef, (10 as $T).pow(coef_prec as u32 + 1));
448 coef /= 10; // drop one trailing zero
449 exp += 1; // one power of ten higher
450 }
451 0
452 },
453 };
454
455 // Allocate a text buffer with lazy initialization.
456 const MAX_DEC_N: usize = $T::MAX.ilog10() as usize + 1;
457 const MAX_COEF_LEN: usize = MAX_DEC_N + ".".len();
458 const MAX_TEXT_LEN: usize = MAX_COEF_LEN + "e99".len();
459 let mut buf = [MaybeUninit::<u8>::uninit(); MAX_TEXT_LEN];
460
461 // Encode the coefficient in buf[..coef_len].
462 let (lead_dec, coef_len) = if coef_prec == 0 && more_prec == 0 {
463 (coef, 1_usize) // single digit; no fraction
464 } else {
465 buf[1].write(b'.');
466 let fraction_range = 2..(2 + coef_prec);
467
468 // Consume the least-significant decimals from a working copy.
469 let mut remain = coef;
470 #[cfg(feature = "optimize_for_size")] {
471 for i in fraction_range.clone().rev() {
472 let digit = (remain % 10) as usize;
473 remain /= 10;
474 buf[i].write(b'0' + digit as u8);
475 }
476 }
477 #[cfg(not(feature = "optimize_for_size"))] {
478 // Write digits per two at a time with a lookup table.
479 for i in fraction_range.clone().skip(1).rev().step_by(2) {
480 let pair = (remain % 100) as usize;
481 remain /= 100;
482 buf[i - 1].write(DECIMAL_PAIRS[pair * 2 + 0]);
483 buf[i - 0].write(DECIMAL_PAIRS[pair * 2 + 1]);
484 }
485 // An odd number of digits leave one digit remaining.
486 if coef_prec & 1 != 0 {
487 let digit = (remain % 10) as usize;
488 remain /= 10;
489 buf[fraction_range.start].write(b'0' + digit as u8);
490 }
491 }
492
493 (remain, fraction_range.end)
494 };
495 debug_assert!(lead_dec < 10);
496 debug_assert!(lead_dec != 0 || coef == 0, "significant digits only");
497 buf[0].write(b'0' + lead_dec as u8);
498
499 // SAFETY: The number of decimals is limited, captured by MAX.
500 unsafe { core::hint::assert_unchecked(coef_len <= MAX_COEF_LEN) }
501 // Encode the scale factor in buf[coef_len..text_len].
502 buf[coef_len].write(letter_e);
503 let text_len: usize = match exp {
504 ..10 => {
505 buf[coef_len + 1].write(b'0' + exp as u8);
506 coef_len + 2
507 },
508 10..100 => {
509 #[cfg(feature = "optimize_for_size")] {
510 buf[coef_len + 1].write(b'0' + (exp / 10) as u8);
511 buf[coef_len + 2].write(b'0' + (exp % 10) as u8);
512 }
513 #[cfg(not(feature = "optimize_for_size"))] {
514 buf[coef_len + 1].write(DECIMAL_PAIRS[exp * 2 + 0]);
515 buf[coef_len + 2].write(DECIMAL_PAIRS[exp * 2 + 1]);
516 }
517 coef_len + 3
518 },
519 #[ferrocene::annotation("Branch is unreachable. See SAFETY comment below.")]
520 _ => {
521 const { assert!($T::MAX.ilog10() < 100) };
522 // SAFETY: A `u256::MAX` would get exponent 77.
523 unsafe { core::hint::unreachable_unchecked() }
524 }
525 };
526 // SAFETY: All bytes up until text_len have been set.
527 let text = unsafe { buf[..text_len].assume_init_ref() };
528
529 if more_prec == 0 {
530 // SAFETY: Text is set with ASCII exclusively: either a decimal,
531 // or a LETTER_E, or a dot. ASCII implies valid UTF-8.
532 let as_str = unsafe { str::from_utf8_unchecked(text) };
533 f.pad_integral(is_nonnegative, "", as_str)
534 } else {
535 let parts = &[
536 numfmt::Part::Copy(&text[..coef_len]),
537 numfmt::Part::Zero(more_prec),
538 numfmt::Part::Copy(&text[coef_len..]),
539 ];
540 let sign = if !is_nonnegative {
541 "-"
542 } else if f.sign_plus() {
543 "+"
544 } else {
545 ""
546 };
547 // SAFETY: Text is set with ASCII exclusively: either a decimal,
548 // or a LETTER_E, or a dot. ASCII implies valid UTF-8.
549 unsafe { f.pad_formatted_parts(&numfmt::Formatted { sign, parts }) }
550 }
551 }
552
553 $(
554 const _: () = {
555 assert!($Signed::MIN < 0, "need signed");
556 assert!($Unsigned::MIN == 0, "need unsigned");
557 assert!($Signed::BITS == $Unsigned::BITS, "need counterparts");
558 assert!($Signed::BITS <= $T::BITS, "need lossless conversion");
559 assert!($Unsigned::BITS <= $T::BITS, "need lossless conversion");
560 };
561 #[stable(feature = "integer_exp_format", since = "1.42.0")]
562 impl fmt::LowerExp for $Signed {
563 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
564 $fmt_fn(f, self.unsigned_abs() as $T, *self >= 0, b'e')
565 }
566 }
567 #[stable(feature = "integer_exp_format", since = "1.42.0")]
568 impl fmt::LowerExp for $Unsigned {
569 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
570 $fmt_fn(f, *self as $T, true, b'e')
571 }
572 }
573 #[stable(feature = "integer_exp_format", since = "1.42.0")]
574 impl fmt::UpperExp for $Signed {
575 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
576 $fmt_fn(f, self.unsigned_abs() as $T, *self >= 0, b'E')
577 }
578 }
579 #[stable(feature = "integer_exp_format", since = "1.42.0")]
580 impl fmt::UpperExp for $Unsigned {
581 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
582 $fmt_fn(f, *self as $T, true, b'E')
583 }
584 }
585 )*
586
587 };
588}
589
590impl_Debug! {
591 i8 i16 i32 i64 i128 isize
592 u8 u16 u32 u64 u128 usize
593}
594
595// Include wasm32 in here since it doesn't reflect the native pointer size, and
596// often cares strongly about getting a smaller code size.
597#[cfg(any(target_pointer_width = "64", target_arch = "wasm32"))]
598mod imp {
599 use super::*;
600 impl_Display!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize; as u64 into display_u64);
601 impl_Exp!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize; as u64 into exp_u64);
602}
603
604#[cfg(not(any(target_pointer_width = "64", target_arch = "wasm32")))]
605mod imp {
606 use super::*;
607 impl_Display!(i8, u8, i16, u16, i32, u32, isize, usize; as u32 into display_u32);
608 impl_Display!(i64, u64; as u64 into display_u64);
609
610 impl_Exp!(i8, u8, i16, u16, i32, u32, isize, usize; as u32 into exp_u32);
611 impl_Exp!(i64, u64; as u64 into exp_u64);
612}
613impl_Exp!(i128, u128; as u128 into exp_u128);
614
615const U128_MAX_DEC_N: usize = u128::MAX.ilog10() as usize + 1;
616
617#[stable(feature = "rust1", since = "1.0.0")]
618impl fmt::Display for u128 {
619 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
620 let mut buf = [MaybeUninit::<u8>::uninit(); U128_MAX_DEC_N];
621
622 // SAFETY: `buf` is always big enough to contain all the digits.
623 unsafe { f.pad_integral(true, "", self._fmt(&mut buf)) }
624 }
625}
626
627#[stable(feature = "rust1", since = "1.0.0")]
628impl fmt::Display for i128 {
629 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
630 // This is not a typo, we use the maximum number of digits of `u128`, hence why we use
631 // `U128_MAX_DEC_N`.
632 let mut buf = [MaybeUninit::<u8>::uninit(); U128_MAX_DEC_N];
633
634 let is_nonnegative = *self >= 0;
635 // SAFETY: `buf` is always big enough to contain all the digits.
636 unsafe { f.pad_integral(is_nonnegative, "", self.unsigned_abs()._fmt(&mut buf)) }
637 }
638}
639
640impl u128 {
641 /// Format optimized for u128. Computation of 128 bits is limited by processing
642 /// in batches of 16 decimals at a time.
643 #[doc(hidden)]
644 #[unstable(
645 feature = "fmt_internals",
646 reason = "specialized method meant to only be used by `SpecToString` implementation",
647 issue = "none"
648 )]
649 pub unsafe fn _fmt<'a>(self, buf: &'a mut [MaybeUninit<u8>]) -> &'a str {
650 // SAFETY: `buf` will always be big enough to contain all digits.
651 let offset = unsafe { self._fmt_inner(buf) };
652 // SAFETY: Starting from `offset`, all elements of the slice have been set.
653 unsafe { slice_buffer_to_str(buf, offset) }
654 }
655
656 unsafe fn _fmt_inner(self, buf: &mut [MaybeUninit<u8>]) -> usize {
657 // Optimize common-case zero, which would also need special treatment due to
658 // its "leading" zero.
659 if self == 0 {
660 let offset = buf.len() - 1;
661 buf[offset].write(b'0');
662 return offset;
663 }
664 // Take the 16 least-significant decimals.
665 let (quot_1e16, mod_1e16) = div_rem_1e16(self);
666 let (mut remain, mut offset) = if quot_1e16 == 0 {
667 (mod_1e16, U128_MAX_DEC_N)
668 } else {
669 // Write digits at buf[23..39].
670 enc_16lsd::<{ U128_MAX_DEC_N - 16 }>(buf, mod_1e16);
671
672 // Take another 16 decimals.
673 let (quot2, mod2) = div_rem_1e16(quot_1e16);
674 if quot2 == 0 {
675 (mod2, U128_MAX_DEC_N - 16)
676 } else {
677 // Write digits at buf[7..23].
678 enc_16lsd::<{ U128_MAX_DEC_N - 32 }>(buf, mod2);
679 // Quot2 has at most 7 decimals remaining after two 1e16 divisions.
680 (quot2 as u64, U128_MAX_DEC_N - 32)
681 }
682 };
683
684 // Format per four digits from the lookup table.
685 while remain > 999 {
686 // SAFETY: All of the decimals fit in buf due to U128_MAX_DEC_N
687 // and the while condition ensures at least 4 more decimals.
688 unsafe { core::hint::assert_unchecked(offset >= 4) }
689 // SAFETY: The offset counts down from its initial buf.len()
690 // without underflow due to the previous precondition.
691 unsafe { core::hint::assert_unchecked(offset <= buf.len()) }
692 offset -= 4;
693
694 // pull two pairs
695 let quad = remain % 1_00_00;
696 remain /= 1_00_00;
697 let pair1 = (quad / 100) as usize;
698 let pair2 = (quad % 100) as usize;
699 buf[offset + 0].write(DECIMAL_PAIRS[pair1 * 2 + 0]);
700 buf[offset + 1].write(DECIMAL_PAIRS[pair1 * 2 + 1]);
701 buf[offset + 2].write(DECIMAL_PAIRS[pair2 * 2 + 0]);
702 buf[offset + 3].write(DECIMAL_PAIRS[pair2 * 2 + 1]);
703 }
704
705 // Format per two digits from the lookup table.
706 if remain > 9 {
707 // SAFETY: All of the decimals fit in buf due to U128_MAX_DEC_N
708 // and the if condition ensures at least 2 more decimals.
709 unsafe { core::hint::assert_unchecked(offset >= 2) }
710 // SAFETY: The offset counts down from its initial buf.len()
711 // without underflow due to the previous precondition.
712 unsafe { core::hint::assert_unchecked(offset <= buf.len()) }
713 offset -= 2;
714
715 let pair = (remain % 100) as usize;
716 remain /= 100;
717 buf[offset + 0].write(DECIMAL_PAIRS[pair * 2 + 0]);
718 buf[offset + 1].write(DECIMAL_PAIRS[pair * 2 + 1]);
719 }
720
721 // Format the last remaining digit, if any.
722 if remain != 0 {
723 // SAFETY: All of the decimals fit in buf due to U128_MAX_DEC_N
724 // and the if condition ensures (at least) 1 more decimals.
725 unsafe { core::hint::assert_unchecked(offset >= 1) }
726 // SAFETY: The offset counts down from its initial buf.len()
727 // without underflow due to the previous precondition.
728 unsafe { core::hint::assert_unchecked(offset <= buf.len()) }
729 offset -= 1;
730
731 // Either the compiler sees that remain < 10, or it prevents
732 // a boundary check up next.
733 let last = (remain & 15) as usize;
734 buf[offset].write(DECIMAL_PAIRS[last * 2 + 1]);
735 // not used: remain = 0;
736 }
737 offset
738 }
739
740 /// Allows users to write an integer (in signed decimal format) into a variable `buf` of
741 /// type [`NumBuffer`] that is passed by the caller by mutable reference.
742 ///
743 /// # Examples
744 ///
745 /// ```
746 /// #![feature(int_format_into)]
747 /// use core::fmt::NumBuffer;
748 ///
749 /// let n = 0u128;
750 /// let mut buf = NumBuffer::new();
751 /// assert_eq!(n.format_into(&mut buf), "0");
752 ///
753 /// let n1 = 32u128;
754 /// let mut buf1 = NumBuffer::new();
755 /// assert_eq!(n1.format_into(&mut buf1), "32");
756 ///
757 /// let n2 = u128::MAX;
758 /// let mut buf2 = NumBuffer::new();
759 /// assert_eq!(n2.format_into(&mut buf2), u128::MAX.to_string());
760 /// ```
761 #[unstable(feature = "int_format_into", issue = "138215")]
762 pub fn format_into(self, buf: &mut NumBuffer<Self>) -> &str {
763 let diff = buf.capacity() - U128_MAX_DEC_N;
764 // FIXME: Once const generics are better, use `NumberBufferTrait::BUF_SIZE` as generic const
765 // for `fmt_u128_inner`.
766 //
767 // In the meantime, we have to use a slice starting at index 1 and add 1 to the returned
768 // offset to ensure the number is correctly generated at the end of the buffer.
769 // SAFETY: `diff` will always be between 0 and its initial value.
770 unsafe { self._fmt(buf.buf.get_unchecked_mut(diff..)) }
771 }
772}
773
774impl i128 {
775 /// Allows users to write an integer (in signed decimal format) into a variable `buf` of
776 /// type [`NumBuffer`] that is passed by the caller by mutable reference.
777 ///
778 /// # Examples
779 ///
780 /// ```
781 /// #![feature(int_format_into)]
782 /// use core::fmt::NumBuffer;
783 ///
784 /// let n = 0i128;
785 /// let mut buf = NumBuffer::new();
786 /// assert_eq!(n.format_into(&mut buf), "0");
787 ///
788 /// let n1 = i128::MIN;
789 /// assert_eq!(n1.format_into(&mut buf), i128::MIN.to_string());
790 ///
791 /// let n2 = i128::MAX;
792 /// assert_eq!(n2.format_into(&mut buf), i128::MAX.to_string());
793 /// ```
794 #[unstable(feature = "int_format_into", issue = "138215")]
795 pub fn format_into(self, buf: &mut NumBuffer<Self>) -> &str {
796 let diff = buf.capacity() - U128_MAX_DEC_N;
797 // FIXME: Once const generics are better, use `NumberBufferTrait::BUF_SIZE` as generic const
798 // for `fmt_u128_inner`.
799 //
800 // In the meantime, we have to use a slice starting at index 1 and add 1 to the returned
801 // offset to ensure the number is correctly generated at the end of the buffer.
802 let mut offset =
803 // SAFETY: `buf` will always be big enough to contain all digits.
804 unsafe { self.unsigned_abs()._fmt_inner(buf.buf.get_unchecked_mut(diff..)) };
805 // We put back the offset at the right position.
806 offset += diff;
807 // Only difference between signed and unsigned are these 4 lines.
808 if self < 0 {
809 offset -= 1;
810 // SAFETY: `buf` will always be big enough to contain all digits plus the minus sign.
811 unsafe {
812 buf.buf.get_unchecked_mut(offset).write(b'-');
813 }
814 }
815 // SAFETY: Starting from `offset`, all elements of the slice have been set.
816 unsafe { slice_buffer_to_str(&buf.buf, offset) }
817 }
818}
819
820/// Encodes the 16 least-significant decimals of n into `buf[OFFSET .. OFFSET +
821/// 16 ]`.
822fn enc_16lsd<const OFFSET: usize>(buf: &mut [MaybeUninit<u8>], n: u64) {
823 // Consume the least-significant decimals from a working copy.
824 let mut remain = n;
825
826 // Format per four digits from the lookup table.
827 for quad_index in (0..4).rev() {
828 // pull two pairs
829 let quad = remain % 1_00_00;
830 remain /= 1_00_00;
831 let pair1 = (quad / 100) as usize;
832 let pair2 = (quad % 100) as usize;
833 buf[quad_index * 4 + OFFSET + 0].write(DECIMAL_PAIRS[pair1 * 2 + 0]);
834 buf[quad_index * 4 + OFFSET + 1].write(DECIMAL_PAIRS[pair1 * 2 + 1]);
835 buf[quad_index * 4 + OFFSET + 2].write(DECIMAL_PAIRS[pair2 * 2 + 0]);
836 buf[quad_index * 4 + OFFSET + 3].write(DECIMAL_PAIRS[pair2 * 2 + 1]);
837 }
838}
839
840/// Euclidean division plus remainder with constant 1E16 basically consumes 16
841/// decimals from n.
842///
843/// The integer division algorithm is based on the following paper:
844///
845/// T. Granlund and P. Montgomery, “Division by Invariant Integers Using Multiplication”
846/// in Proc. of the SIGPLAN94 Conference on Programming Language Design and
847/// Implementation, 1994, pp. 61–72
848///
849#[inline]
850fn div_rem_1e16(n: u128) -> (u128, u64) {
851 const D: u128 = 1_0000_0000_0000_0000;
852 // The check inlines well with the caller flow.
853 if n < D {
854 return (0, n as u64);
855 }
856
857 // These constant values are computed with the CHOOSE_MULTIPLIER procedure
858 // from the Granlund & Montgomery paper, using N=128, prec=128 and d=1E16.
859 const M_HIGH: u128 = 76624777043294442917917351357515459181;
860 const SH_POST: u8 = 51;
861
862 let quot = n.widening_mul(M_HIGH).1 >> SH_POST;
863 let rem = n - quot * D;
864 (quot, rem as u64)
865}