1/// Creates an unsigned division function optimized for dividing integers with the same
2/// bitwidth as the largest operand in an asymmetrically sized division. For example, x86-64 has an
3/// assembly instruction that can divide a 128 bit integer by a 64 bit integer if the quotient fits
4/// in 64 bits. The 128 bit version of this algorithm would use that fast hardware division to
5/// construct a full 128 bit by 128 bit division.
6#[allow(unused_macros)]
7macro_rules! impl_asymmetric {
8 (
9 $fn:ident, // name of the unsigned division function
10 $zero_div_fn:ident, // function called when division by zero is attempted
11 $half_division:ident, // function for division of a $uX by a $uX
12 $asymmetric_division:ident, // function for division of a $uD by a $uX
13 $n_h:expr, // the number of bits in a $iH or $uH
14 $uH:ident, // unsigned integer with half the bit width of $uX
15 $uX:ident, // unsigned integer with half the bit width of $uD
16 $uD:ident // unsigned integer type for the inputs and outputs of `$fn`
17 ) => {
18 /// Computes the quotient and remainder of `duo` divided by `div` and returns them as a
19 /// tuple.
20 pub fn $fn(duo: $uD, div: $uD) -> ($uD, $uD) {
21 let n: u32 = $n_h * 2;
22
23 let duo_lo = duo as $uX;
24 let duo_hi = (duo >> n) as $uX;
25 let div_lo = div as $uX;
26 let div_hi = (div >> n) as $uX;
27 if div_hi == 0 {
28 if div_lo == 0 {
29 $zero_div_fn()
30 }
31 if duo_hi < div_lo {
32 // `$uD` by `$uX` division with a quotient that will fit into a `$uX`
33 let (quo, rem) = unsafe { $asymmetric_division(duo, div_lo) };
34 return (quo as $uD, rem as $uD);
35 } else {
36 // Short division using the $uD by $uX division
37 let (quo_hi, rem_hi) = $half_division(duo_hi, div_lo);
38 let tmp = unsafe {
39 $asymmetric_division((duo_lo as $uD) | ((rem_hi as $uD) << n), div_lo)
40 };
41 return ((tmp.0 as $uD) | ((quo_hi as $uD) << n), tmp.1 as $uD);
42 }
43 }
44
45 // This has been adapted from
46 // https://www.codeproject.com/tips/785014/uint-division-modulus which was in turn
47 // adapted from Hacker's Delight. This is similar to the two possibility algorithm
48 // in that it uses only more significant parts of `duo` and `div` to divide a large
49 // integer with a smaller division instruction.
50 let div_lz = div_hi.leading_zeros();
51 let div_extra = n - div_lz;
52 let div_sig_n = (div >> div_extra) as $uX;
53 let tmp = unsafe { $asymmetric_division(duo >> 1, div_sig_n) };
54
55 let mut quo = tmp.0 >> ((n - 1) - div_lz);
56 if quo != 0 {
57 quo -= 1;
58 }
59
60 // Note that this is a full `$uD` multiplication being used here
61 let mut rem = duo - (quo as $uD).wrapping_mul(div);
62 if div <= rem {
63 quo += 1;
64 rem -= div;
65 }
66 return (quo as $uD, rem);
67 }
68 };
69}
70