|
| 1 | +macro_rules! impl_asymmetric { |
| 2 | + ( |
| 3 | + $unsigned_name:ident, // name of the unsigned function |
| 4 | + $signed_name:ident, // name of the signed function |
| 5 | + $half_division:ident, // function for division of a $uX by a $uX |
| 6 | + $asymmetric_division:ident, // function for division of a $uD by a $uX |
| 7 | + $n_h:expr, // the number of bits in $iH or $uH |
| 8 | + $uH:ident, // unsigned integer with half the bit width of $uX |
| 9 | + $uX:ident, // unsigned integer with half the bit width of $uD |
| 10 | + $uD:ident, // unsigned integer with double the bit width of $uX |
| 11 | + $iD:ident, // signed version of $uD |
| 12 | + $($unsigned_attr:meta),*; // attributes for the unsigned function |
| 13 | + $($signed_attr:meta),* // attributes for the signed function |
| 14 | + ) => { |
| 15 | + /// Computes the quotient and remainder of `duo` divided by `div` and returns them as a |
| 16 | + /// tuple. |
| 17 | + /// |
| 18 | + /// This is optimized for dividing integers with the same bitwidth as the largest operand in |
| 19 | + /// an asymmetrically sized division. For example, the x86-64 `divq` assembly instruction |
| 20 | + /// can divide a 128 bit integer by a 64 bit integer if the quotient fits in 64 bits. |
| 21 | + /// |
| 22 | + /// # Panics |
| 23 | + /// |
| 24 | + /// When attempting to divide by zero, this function will panic. |
| 25 | + $( |
| 26 | + #[$unsigned_attr] |
| 27 | + )* |
| 28 | + pub fn $unsigned_name(duo: $uD, div: $uD) -> ($uD,$uD) { |
| 29 | + #[inline(always)] |
| 30 | + fn carrying_mul(lhs: $uX, rhs: $uX) -> ($uX, $uX) { |
| 31 | + let tmp = (lhs as $uD).wrapping_mul(rhs as $uD); |
| 32 | + (tmp as $uX, (tmp >> ($n_h * 2)) as $uX) |
| 33 | + } |
| 34 | + #[inline(always)] |
| 35 | + fn carrying_mul_add(lhs: $uX, mul: $uX, add: $uX) -> ($uX, $uX) { |
| 36 | + let tmp = (lhs as $uD).wrapping_mul(mul as $uD).wrapping_add(add as $uD); |
| 37 | + (tmp as $uX, (tmp >> ($n_h * 2)) as $uX) |
| 38 | + } |
| 39 | + |
| 40 | + let n: u32 = $n_h * 2; |
| 41 | + |
| 42 | + // Many of these subalgorithms are taken from trifecta.rs, see that for better |
| 43 | + // documentation |
| 44 | + |
| 45 | + let duo_lo = duo as $uX; |
| 46 | + let duo_hi = (duo >> n) as $uX; |
| 47 | + let div_lo = div as $uX; |
| 48 | + let div_hi = (div >> n) as $uX; |
| 49 | + if div_hi == 0 { |
| 50 | + if div_lo == 0 { |
| 51 | + // division by zero |
| 52 | + ::abort(); |
| 53 | + } |
| 54 | + if duo_hi < div_lo { |
| 55 | + // plain $uD by $uX division that will fit into $uX |
| 56 | + let tmp = unsafe { $asymmetric_division(duo, div_lo) }; |
| 57 | + return (tmp.0 as $uD, tmp.1 as $uD) |
| 58 | + } else if (div_lo >> $n_h) == 0 { |
| 59 | + // Short division of $uD by a $uH. |
| 60 | + let div_0 = div_lo as $uH as $uX; |
| 61 | + let (quo_hi, rem_3) = $half_division(duo_hi, div_0); |
| 62 | + |
| 63 | + let duo_mid = |
| 64 | + ((duo >> $n_h) as $uH as $uX) |
| 65 | + | (rem_3 << $n_h); |
| 66 | + let (quo_1, rem_2) = $half_division(duo_mid, div_0); |
| 67 | + |
| 68 | + let duo_lo = |
| 69 | + (duo as $uH as $uX) |
| 70 | + | (rem_2 << $n_h); |
| 71 | + let (quo_0, rem_1) = $half_division(duo_lo, div_0); |
| 72 | + |
| 73 | + return ( |
| 74 | + (quo_0 as $uD) |
| 75 | + | ((quo_1 as $uD) << $n_h) |
| 76 | + | ((quo_hi as $uD) << n), |
| 77 | + rem_1 as $uD |
| 78 | + ) |
| 79 | + } else { |
| 80 | + // Short division using the $uD by $uX division |
| 81 | + let (quo_hi, rem_hi) = $half_division(duo_hi, div_lo); |
| 82 | + let tmp = unsafe { |
| 83 | + $asymmetric_division((duo_lo as $uD) | ((rem_hi as $uD) << n), div_lo) |
| 84 | + }; |
| 85 | + return ((tmp.0 as $uD) | ((quo_hi as $uD) << n), tmp.1 as $uD) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + let duo_lz = duo_hi.leading_zeros(); |
| 90 | + let div_lz = div_hi.leading_zeros(); |
| 91 | + let rel_leading_sb = div_lz.wrapping_sub(duo_lz); |
| 92 | + if rel_leading_sb < $n_h { |
| 93 | + // Some x86_64 CPUs have bad `divq` implementations that make putting |
| 94 | + // a `mul` or `mul - 1` algorithm here beneficial |
| 95 | + let shift = n.wrapping_sub(duo_lz); |
| 96 | + let duo_sig_n = (duo >> shift) as $uX; |
| 97 | + let div_sig_n = (div >> shift) as $uX; |
| 98 | + let mul = $half_division(duo_sig_n, div_sig_n).0; |
| 99 | + let div_lo = div as $uX; |
| 100 | + let div_hi = (div >> n) as $uX; |
| 101 | + let (tmp_lo, carry) = carrying_mul(mul,div_lo); |
| 102 | + let (tmp_hi, overflow) = carrying_mul_add(mul,div_hi,carry); |
| 103 | + let tmp = (tmp_lo as $uD) | ((tmp_hi as $uD) << n); |
| 104 | + if ((overflow & 1) != 0) || (duo < tmp) { |
| 105 | + return ( |
| 106 | + mul.wrapping_sub(1) as $uD, |
| 107 | + duo.wrapping_add(div.wrapping_sub(tmp)) |
| 108 | + ) |
| 109 | + } else { |
| 110 | + return ( |
| 111 | + mul as $uD, |
| 112 | + duo.wrapping_sub(tmp) |
| 113 | + ) |
| 114 | + } |
| 115 | + } else { |
| 116 | + // This has been adapted from |
| 117 | + // https://www.codeproject.com/tips/785014/uint-division-modulus which was in turn |
| 118 | + // adapted from www.hackersdelight.org |
| 119 | + |
| 120 | + // This is similar to the `mul` or `mul - 1` algorithm in that it uses only more |
| 121 | + // significant parts of `duo` and `div` to divide a large integer with a smaller |
| 122 | + // division instruction. |
| 123 | + let tmp = unsafe { |
| 124 | + $asymmetric_division(duo >> 1, ((div << div_lz) >> n) as $uX) |
| 125 | + }; |
| 126 | + let mut quo = tmp.0 >> ((n - 1) - div_lz); |
| 127 | + if quo != 0 { |
| 128 | + quo -= 1; |
| 129 | + } |
| 130 | + // Note that this is a large $uD multiplication being used here |
| 131 | + let mut rem = duo - ((quo as $uD) * div); |
| 132 | + |
| 133 | + if rem >= div { |
| 134 | + quo += 1; |
| 135 | + rem -= div; |
| 136 | + } |
| 137 | + return (quo as $uD, rem) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /// Computes the quotient and remainder of `duo` divided by `div` and returns them as a |
| 142 | + /// tuple. |
| 143 | + /// |
| 144 | + /// This is optimized for dividing integers with the same bitwidth as the largest operand in |
| 145 | + /// an asymmetrically sized division. For example, the x86-64 `divq` assembly instruction |
| 146 | + /// can divide a 128 bit integer by a 64 bit integer if the quotient fits in 64 bits. |
| 147 | + /// |
| 148 | + /// # Panics |
| 149 | + /// |
| 150 | + /// When attempting to divide by zero, this function will panic. |
| 151 | + $( |
| 152 | + #[$signed_attr] |
| 153 | + )* |
| 154 | + pub fn $signed_name(duo: $iD, div: $iD) -> ($iD,$iD) { |
| 155 | + match (duo < 0, div < 0) { |
| 156 | + (false,false) => { |
| 157 | + let t = $unsigned_name(duo as $uD,div as $uD); |
| 158 | + (t.0 as $iD,t.1 as $iD) |
| 159 | + }, |
| 160 | + (true,false) => { |
| 161 | + let t = $unsigned_name(duo.wrapping_neg() as $uD,div as $uD); |
| 162 | + ((t.0 as $iD).wrapping_neg(),(t.1 as $iD).wrapping_neg()) |
| 163 | + }, |
| 164 | + (false,true) => { |
| 165 | + let t = $unsigned_name(duo as $uD,div.wrapping_neg() as $uD); |
| 166 | + ((t.0 as $iD).wrapping_neg(),t.1 as $iD) |
| 167 | + }, |
| 168 | + (true,true) => { |
| 169 | + let t = $unsigned_name(duo.wrapping_neg() as $uD,div.wrapping_neg() as $uD); |
| 170 | + (t.0 as $iD,(t.1 as $iD).wrapping_neg()) |
| 171 | + }, |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments