|
| 1 | +//! `x86_64` intrinsics |
| 2 | +
|
| 3 | +use core::arch::asm; |
| 4 | + |
| 5 | +/// Unsigned integer addition of two operands with carry flag. |
| 6 | +/// |
| 7 | +/// Performs an unsigned addition of the destination operand (first operand), |
| 8 | +/// the source operand (second operand) and the carry-flag (CF) and stores the |
| 9 | +/// result in the destination operand. The destination operand is a |
| 10 | +/// general-purpose register, whereas the source operand can be a |
| 11 | +/// general-purpose register or memory location. The state of CF can represent |
| 12 | +/// a carry from a previous addition. The instruction sets the CF flag with the |
| 13 | +/// carry generated by the unsigned addition of the operands. |
| 14 | +/// |
| 15 | +/// The ADCX instruction is executed in the context of multi-precision addition |
| 16 | +/// where we add a series of operands with a carry-chain. At the beginning of a |
| 17 | +/// chain of additions, we need to make sure the CF is in a desired initial |
| 18 | +/// state. Often, this initial state needs to be 0, which can be achieved with |
| 19 | +/// an instruction to zero the CF (e.g. XOR). |
| 20 | +#[inline(always)] |
| 21 | +pub unsafe fn adcx(a: &mut u64, b: u64) { |
| 22 | + asm! { |
| 23 | + "adcx {0}, {1}", |
| 24 | + inout(reg) *a, |
| 25 | + in(reg) b |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +/// Unsigned integer addition of two operations with overflow flag. |
| 30 | +/// |
| 31 | +/// Performs an unsigned addition of the destination operand (first operand), |
| 32 | +/// the source operand (second operand) and the overflow-flag (OF) and stores |
| 33 | +/// the result in the destination operand. The destination operand is a |
| 34 | +/// general-purpose register, whereas the source operand can be a |
| 35 | +/// general-purpose register or memory location. The state of OF represents a |
| 36 | +/// carry from a previous addition. The instruction sets the OF flag with the |
| 37 | +/// carry generated by the unsigned addition of the operands. |
| 38 | +/// |
| 39 | +/// The ADOX instruction is executed in the context of multi-precision |
| 40 | +/// addition, where we add a series of operands with a carry-chain. At the |
| 41 | +/// beginning of a chain of additions, we execute an instruction to zero the OF |
| 42 | +/// (e.g. XOR). |
| 43 | +#[inline(always)] |
| 44 | +pub unsafe fn adox(a: &mut u64, b: u64) { |
| 45 | + asm! { |
| 46 | + "adox {0}, {1}", |
| 47 | + inout(reg) *a, |
| 48 | + in(reg) b |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +/// Move if zero. |
| 53 | +/// |
| 54 | +/// Uses a `test` instruction to check if the given `condition` value is |
| 55 | +/// equal to zero, then calls `cmovz` (a.k.a. `cmove`) to conditionally move |
| 56 | +/// `src` to `dst` when `condition` is equal to zero. |
| 57 | +#[inline] |
| 58 | +pub fn cmovz(condition: u64, src: u64, dst: &mut u64) { |
| 59 | + unsafe { |
| 60 | + asm! { |
| 61 | + "test {0}, {0}", |
| 62 | + "cmovz {1}, {2}", |
| 63 | + in(reg) condition, |
| 64 | + inlateout(reg) *dst, |
| 65 | + in(reg) src |
| 66 | + }; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/// Move if not zero. |
| 71 | +/// |
| 72 | +/// Uses a `test` instruction to check if the given `condition` value is not |
| 73 | +/// equal to zero, then calls `cmovnz` (a.k.a. `cmovne`) to conditionally move |
| 74 | +/// `src` to `dst` when `condition` is nonzero. |
| 75 | +#[inline] |
| 76 | +pub fn cmovnz(condition: u64, src: u64, dst: &mut u64) { |
| 77 | + unsafe { |
| 78 | + asm! { |
| 79 | + "test {0}, {0}", |
| 80 | + "cmovnz {1}, {2}", |
| 81 | + in(reg) condition, |
| 82 | + inlateout(reg) *dst, |
| 83 | + in(reg) src |
| 84 | + }; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/// Unsigned multiply without affecting flags. |
| 89 | +/// |
| 90 | +/// Performs an unsigned multiplication of the implicit source operand and the |
| 91 | +/// specified source operand (the third operand) and stores the low half of the |
| 92 | +/// result in the second destination (second operand), the high half of the |
| 93 | +/// result in the first destination operand (first operand), without reading or |
| 94 | +/// writing the arithmetic flags. |
| 95 | +/// |
| 96 | +/// This enables efficient programming where the software can interleave add |
| 97 | +/// with carry operations and multiplications. |
| 98 | +/// |
| 99 | +/// If the first and second operand are identical, it will contain the high |
| 100 | +/// half of the multiplication result. |
| 101 | +#[inline(always)] |
| 102 | +pub unsafe fn mulx(a: u64, b: u64, lo: &mut u64, hi: &mut u64) { |
| 103 | + asm! { |
| 104 | + "mulx {2}, {1}, {0}", |
| 105 | + in(reg) b, |
| 106 | + lateout(reg) *lo, |
| 107 | + lateout(reg) *hi, |
| 108 | + in("rdx") a |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +#[cfg(test)] |
| 113 | +mod tests { |
| 114 | + use super::*; |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn cmovz_works() { |
| 118 | + let mut n = 24; |
| 119 | + cmovz(42, 42, &mut n); |
| 120 | + assert_eq!(n, 24); |
| 121 | + cmovz(0, 42, &mut n); |
| 122 | + assert_eq!(n, 42); |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn cmovnz_works() { |
| 127 | + let mut n = 24; |
| 128 | + cmovnz(0, 42, &mut n); |
| 129 | + assert_eq!(n, 24); |
| 130 | + cmovnz(42, 42, &mut n); |
| 131 | + assert_eq!(n, 42); |
| 132 | + } |
| 133 | +} |
0 commit comments