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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
use int::Int; #[inline] fn mulx_u8(x: u8, y: u8) -> (u8, u8) { let result: u16 = (x as u16) * (y as u16); let hi = (result >> 8) as u8; (result as u8, hi) } #[inline] fn mulx_u16(x: u16, y: u16) -> (u16, u16) { let result: u32 = (x as u32) * (y as u32); let hi = (result >> 16) as u16; (result as u16, hi) } #[inline] fn mulx_u32(x: u32, y: u32) -> (u32, u32) { let result: u64 = (x as u64) * (y as u64); let hi = (result >> 32) as u32; (result as u32, hi) } #[inline] fn mulx_u64(x: u64, y: u64) -> (u64, u64) { #[cfg(RUSTC_IS_NIGHTLY)] { let result: u128 = (x as u128) * (y as u128); let hi = (result >> 64) as u64; (result as u64, hi) } #[cfg(not(RUSTC_IS_NIGHTLY))] { let u1 = x & 0xffffffff; let v1 = y & 0xffffffff; let t = u1 * v1; let w3 = t & 0xffffffff; let k = t >> 32; let x = x >> 32; let t1 = (x * v1) + k; let k1 = t1 & 0xffffffff; let w1 = t1 >> 32; let y1 = y >> 32; let t2 = (u1 * y1) + k1; let k2 = t2 >> 32; let hi = (x * y1) + w1 + k2; let lo = (t2 << 32) + w3; (lo, hi) } } /// Unsigned multiply without affecting flags. /// /// Unsigned multiplication of `x` with `y` returning a pair `(lo, hi)` with /// the low half and the high half of the result. /// /// # Assembly Instructions /// /// - [`MULX`](http://www.felixcloutier.com/x86/MULX.html): /// - Description: Unsigned multiply without affecting flags. /// - Architecture: x86. /// - Instruction set: BMI2. /// - Registers: 32/64 bit. /// /// # Example /// /// ``` /// use bitintr::x86::bmi2::*; /// /// { // 8-bit /// let a: u8 = 128; /// let b: u8 = 128; /// let (lo, hi): (u8, u8) = mulx(a, b); /// // result = 16384 = 0b0100_0000_0000_0000u16 /// // ^~hi~~~~~ ^~lo~~~~~ /// assert_eq!(lo, 0b0000_0000); /// assert_eq!(hi, 0b0100_0000); /// } /// { // 16-bit /// let a: u16 = 65_500; /// let b: u16 = 65_500; /// let (lo, hi): (u16, u16) = a.mulx(b); /// // result = 4290250000 = 0b1111_1111_1011_1000_0000_0101_0001_0000u32 /// // ^~hi~~~~~~~~~~~~~~~ ^~lo~~~~~~~~~~~~~~~ /// assert_eq!(lo, 0b0000_0101_0001_0000); /// assert_eq!(hi, 0b1111_1111_1011_1000); /// } /// { // 32-bit /// let a: u32 = 4_294_967_200; /// let b: u32 = 2; /// let (lo, hi): (u32, u32) = mulx(a, b); /// // result = 8589934400 /// // = 0b0001_1111_1111_1111_1111_1111_1111_0100_0000u64 /// // ^~hi ^~lo~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /// assert_eq!(lo, 0b1111_1111_1111_1111_1111_1111_0100_0000u32); /// assert_eq!(hi, 0b0001u32); /// } /// { // 64-bit /// let a: u64 = 9_223_372_036_854_775_800; /// let b: u64 = 100; /// let (lo, hi): (u64, u64) = mulx(a, b); /// // result = 922337203685477580000 /// // = 0b00110001_11111111_11111111_11111111_11111111_11111111_11111111_11111100_11100000u128 /// // ^~hi~~~~ ^~lo~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /// assert_eq!(lo, 0b11111111_11111111_11111111_11111111_11111111_11111111_11111100_11100000u64); /// assert_eq!(hi, 0b00110001u64); /// } /// { // 8-bit /// let a: i8 = 128u8 as i8; /// let b: i8 = 128u8 as i8; /// let (lo, hi): (i8, i8) = mulx(a, b); /// // result = _____ = 0b0100_0000_0000_0000u16 /// // ^~hi~~~~~ ^~lo~~~~~ /// assert_eq!(lo, 0b0000_0000); /// assert_eq!(hi, 0b0100_0000); /// } /// { // 16-bit /// let a: i16 = 65_500u16 as i16; /// let b: i16 = 65_500u16 as i16; /// let (lo, hi): (i16, i16) = mulx(a, b); /// // result = 4290250000 = 0b1111_1111_1011_1000_0000_0101_0001_0000u32 /// // ^~hi~~~~~~~~~~~~~~~ ^~lo~~~~~~~~~~~~~~~ /// assert_eq!(lo, 0b0000_0101_0001_0000u16 as i16); /// assert_eq!(hi, 0b1111_1111_1011_1000u16 as i16); /// } /// ``` #[inline] pub fn mulx<T: Int>(x: T, y: T) -> (T, T) { match T::bit_size().to_u8() { 8 => { let (x, y) = mulx_u8(x.to_u8(), y.to_u8()); (T::from_u8(x), T::from_u8(y)) } 16 => { let (x, y) = mulx_u16(x.to_u16(), y.to_u16()); (T::from_u16(x), T::from_u16(y)) } 32 => { let (x, y) = mulx_u32(x.to_u32(), y.to_u32()); (T::from_u32(x), T::from_u32(y)) } 64 => { let (x, y) = mulx_u64(x.to_u64(), y.to_u64()); (T::from_u64(x), T::from_u64(y)) } _ => unreachable!(), } } /// Method version of [`mulx`](fn.mulx.html). pub trait MULX: Sized { #[inline] fn mulx(self, Self) -> (Self, Self); } impl<T: Int> MULX for T { #[inline] fn mulx(self, x: Self) -> (Self, Self) { mulx(self, x) } }