/rust/registry/src/index.crates.io-6f17d22bba15001f/itoa-1.0.15/src/udiv128.rs
Line | Count | Source (jump to first uncovered line) |
1 | | #[cfg(feature = "no-panic")] |
2 | | use no_panic::no_panic; |
3 | | |
4 | | /// Multiply unsigned 128 bit integers, return upper 128 bits of the result |
5 | | #[inline] |
6 | | #[cfg_attr(feature = "no-panic", no_panic)] |
7 | 5.24k | fn u128_mulhi(x: u128, y: u128) -> u128 { |
8 | 5.24k | let x_lo = x as u64; |
9 | 5.24k | let x_hi = (x >> 64) as u64; |
10 | 5.24k | let y_lo = y as u64; |
11 | 5.24k | let y_hi = (y >> 64) as u64; |
12 | 5.24k | |
13 | 5.24k | // handle possibility of overflow |
14 | 5.24k | let carry = (x_lo as u128 * y_lo as u128) >> 64; |
15 | 5.24k | let m = x_lo as u128 * y_hi as u128 + carry; |
16 | 5.24k | let high1 = m >> 64; |
17 | 5.24k | |
18 | 5.24k | let m_lo = m as u64; |
19 | 5.24k | let high2 = (x_hi as u128 * y_lo as u128 + m_lo as u128) >> 64; |
20 | 5.24k | |
21 | 5.24k | x_hi as u128 * y_hi as u128 + high1 + high2 |
22 | 5.24k | } |
23 | | |
24 | | /// Divide `n` by 1e19 and return quotient and remainder |
25 | | /// |
26 | | /// Integer division algorithm is based on the following paper: |
27 | | /// |
28 | | /// T. Granlund and P. Montgomery, “Division by Invariant Integers Using Multiplication” |
29 | | /// in Proc. of the SIGPLAN94 Conference on Programming Language Design and |
30 | | /// Implementation, 1994, pp. 61–72 |
31 | | /// |
32 | | #[inline] |
33 | | #[cfg_attr(feature = "no-panic", no_panic)] |
34 | 13.1k | pub fn udivmod_1e19(n: u128) -> (u128, u64) { |
35 | 13.1k | let d = 10_000_000_000_000_000_000_u64; // 10^19 |
36 | | |
37 | 13.1k | let quot = if n < 1 << 83 { |
38 | 7.86k | ((n >> 19) as u64 / (d >> 19)) as u128 |
39 | | } else { |
40 | 5.24k | u128_mulhi(n, 156927543384667019095894735580191660403) >> 62 |
41 | | }; |
42 | | |
43 | 13.1k | let rem = (n - quot * d as u128) as u64; |
44 | 13.1k | debug_assert_eq!(quot, n / d as u128); |
45 | 13.1k | debug_assert_eq!(rem as u128, n % d as u128); |
46 | | |
47 | 13.1k | (quot, rem) |
48 | 13.1k | } |