1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! Fast multiplication routines.

use crate::num::{as_cast, UnsignedInteger};

/// Multiply two unsigned, integral values, and return the hi and lo product.
///
/// The `full` type is the full type size, while the `half` type is the type
/// with exactly half the bits.
#[inline(always)]
pub fn mul<Full, Half>(x: Full, y: Full) -> (Full, Full)
where
    Full: UnsignedInteger,
    Half: UnsignedInteger,
{
    // Extract high-and-low masks.
    let x1 = x >> Half::BITS as i32;
    let x0 = x & as_cast(Half::MAX);
    let y1 = y >> Half::BITS as i32;
    let y0 = y & as_cast(Half::MAX);

    let w0 = x0 * y0;
    let tmp = (x1 * y0) + (w0 >> Half::BITS as i32);
    let w1 = tmp & as_cast(Half::MAX);
    let w2 = tmp >> Half::BITS as i32;
    let w1 = w1 + x0 * y1;
    let hi = (x1 * y1) + w2 + (w1 >> Half::BITS as i32);
    let lo = x.wrapping_mul(y);

    (hi, lo)
}

/// Multiply two unsigned, integral values, and return the hi product.
///
/// The `full` type is the full type size, while the `half` type is the type
/// with exactly half the bits.
#[inline(always)]
pub fn mulhi<Full, Half>(x: Full, y: Full) -> Full
where
    Full: UnsignedInteger,
    Half: UnsignedInteger,
{
    // Extract high-and-low masks.
    let x1 = x >> Half::BITS as i32;
    let x0 = x & as_cast(Half::MAX);
    let y1 = y >> Half::BITS as i32;
    let y0 = y & as_cast(Half::MAX);

    let w0 = x0 * y0;
    let m = (x0 * y1) + (w0 >> Half::BITS as i32);
    let w1 = m & as_cast(Half::MAX);
    let w2 = m >> Half::BITS as i32;

    let w3 = (x1 * y0 + w1) >> Half::BITS as i32;

    x1 * y1 + w2 + w3
}