ryu/pretty/
mantissa.rs

1use crate::digit_table::DIGIT_TABLE;
2use core::ptr;
3
4#[cfg_attr(feature = "no-panic", inline)]
5pub unsafe fn write_mantissa_long(mut output: u64, mut result: *mut u8) {
6    if (output >> 32) != 0 {
7        // One expensive 64-bit division.
8        let mut output2 = (output - 100_000_000 * (output / 100_000_000)) as u32;
9        output /= 100_000_000;
10
11        let c = output2 % 10_000;
12        output2 /= 10_000;
13        let d = output2 % 10_000;
14        let c0 = (c % 100) << 1;
15        let c1 = (c / 100) << 1;
16        let d0 = (d % 100) << 1;
17        let d1 = (d / 100) << 1;
18        ptr::copy_nonoverlapping(DIGIT_TABLE.as_ptr().offset(c0 as isize), result.sub(2), 2);
19        ptr::copy_nonoverlapping(DIGIT_TABLE.as_ptr().offset(c1 as isize), result.sub(4), 2);
20        ptr::copy_nonoverlapping(DIGIT_TABLE.as_ptr().offset(d0 as isize), result.sub(6), 2);
21        ptr::copy_nonoverlapping(DIGIT_TABLE.as_ptr().offset(d1 as isize), result.sub(8), 2);
22        result = result.sub(8);
23    }
24    write_mantissa(output as u32, result);
25}
26
27#[cfg_attr(feature = "no-panic", inline)]
28pub unsafe fn write_mantissa(mut output: u32, mut result: *mut u8) {
29    while output >= 10_000 {
30        let c = output - 10_000 * (output / 10_000);
31        output /= 10_000;
32        let c0 = (c % 100) << 1;
33        let c1 = (c / 100) << 1;
34        ptr::copy_nonoverlapping(DIGIT_TABLE.as_ptr().offset(c0 as isize), result.sub(2), 2);
35        ptr::copy_nonoverlapping(DIGIT_TABLE.as_ptr().offset(c1 as isize), result.sub(4), 2);
36        result = result.sub(4);
37    }
38    if output >= 100 {
39        let c = (output % 100) << 1;
40        output /= 100;
41        ptr::copy_nonoverlapping(DIGIT_TABLE.as_ptr().offset(c as isize), result.sub(2), 2);
42        result = result.sub(2);
43    }
44    if output >= 10 {
45        let c = output << 1;
46        ptr::copy_nonoverlapping(DIGIT_TABLE.as_ptr().offset(c as isize), result.sub(2), 2);
47    } else {
48        *result.sub(1) = b'0' + output as u8;
49    }
50}