/rust/registry/src/index.crates.io-1949cf8c6b5b557f/itoa-1.0.17/src/u128_ext.rs
Line | Count | Source |
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 | 0 | pub(crate) fn mulhi(x: u128, y: u128) -> u128 { |
8 | 0 | let x_lo = x as u64; |
9 | 0 | let x_hi = (x >> 64) as u64; |
10 | 0 | let y_lo = y as u64; |
11 | 0 | let y_hi = (y >> 64) as u64; |
12 | | |
13 | | // handle possibility of overflow |
14 | 0 | let carry = (u128::from(x_lo) * u128::from(y_lo)) >> 64; |
15 | 0 | let m = u128::from(x_lo) * u128::from(y_hi) + carry; |
16 | 0 | let high1 = m >> 64; |
17 | | |
18 | 0 | let m_lo = m as u64; |
19 | 0 | let high2 = (u128::from(x_hi) * u128::from(y_lo) + u128::from(m_lo)) >> 64; |
20 | | |
21 | 0 | u128::from(x_hi) * u128::from(y_hi) + high1 + high2 |
22 | 0 | } |