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
use crate::num::{as_cast, UnsignedInteger};
#[inline(always)]
pub fn mul<Full, Half>(x: Full, y: Full) -> (Full, Full)
where
Full: UnsignedInteger,
Half: UnsignedInteger,
{
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)
}
#[inline(always)]
pub fn mulhi<Full, Half>(x: Full, y: Full) -> Full
where
Full: UnsignedInteger,
Half: UnsignedInteger,
{
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
}