| 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 |
pub(crate) fn mulhi(x: u128, y: u128) -> u128 { |
| 8 |
let x_lo = x as u64; |
| 9 |
let x_hi = (x >> 64) as u64; |
| 10 |
let y_lo = y as u64; |
| 11 |
let y_hi = (y >> 64) as u64; |
| 12 |
|
| 13 |
// handle possibility of overflow |
| 14 |
let carry = (u128::from(x_lo) * u128::from(y_lo)) >> 64; |
| 15 |
let m = u128::from(x_lo) * u128::from(y_hi) + carry; |
| 16 |
let high1 = m >> 64; |
| 17 |
|
| 18 |
let m_lo = m as u64; |
| 19 |
let high2 = (u128::from(x_hi) * u128::from(y_lo) + u128::from(m_lo)) >> 64; |
| 20 |
|
| 21 |
u128::from(x_hi) * u128::from(y_hi) + high1 + high2 |
| 22 |
} |