Skip to content

cmov: XOR within the ASM block #925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 3, 2023
72 changes: 66 additions & 6 deletions cmov/src/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ macro_rules! cmov {
};
}

macro_rules! cmov_eq {
($xor:expr, $instruction:expr, $lhs:expr, $rhs:expr, $condition:expr, $dst:expr) => {
let mut tmp = *$dst as u16;
unsafe {
asm! {
$xor,
$instruction,
inout(reg) *$lhs => _,
in(reg) *$rhs,
inlateout(reg) tmp,
in(reg) $condition as u16,
options(pure, nomem, nostack),
};
}
*$dst = tmp as u8;
};
}

impl Cmov for u16 {
#[inline]
fn cmovnz(&mut self, value: &Self, condition: Condition) {
Expand All @@ -40,12 +58,26 @@ impl Cmov for u16 {
impl CmovEq for u16 {
#[inline]
fn cmoveq(&self, rhs: &Self, input: Condition, output: &mut Condition) {
output.cmovz(&input, (self ^ rhs) as u8);
cmov_eq!(
"xor {0:x}, {1:x}",
"cmovz {2:e}, {3:e}",
self,
rhs,
input,
output
);
}

#[inline]
fn cmovne(&self, rhs: &Self, input: Condition, output: &mut Condition) {
output.cmovnz(&input, (self ^ rhs) as u8);
cmov_eq!(
"xor {0:x}, {1:x}",
"cmovnz {2:e}, {3:e}",
self,
rhs,
input,
output
);
}
}

Expand All @@ -64,12 +96,26 @@ impl Cmov for u32 {
impl CmovEq for u32 {
#[inline]
fn cmoveq(&self, rhs: &Self, input: Condition, output: &mut Condition) {
output.cmovz(&input, (self ^ rhs) as u8);
cmov_eq!(
"xor {0:e}, {1:e}",
"cmovz {2:e}, {3:e}",
self,
rhs,
input,
output
);
}

#[inline]
fn cmovne(&self, rhs: &Self, input: Condition, output: &mut Condition) {
output.cmovnz(&input, (self ^ rhs) as u8);
cmov_eq!(
"xor {0:e}, {1:e}",
"cmovnz {2:e}, {3:e}",
self,
rhs,
input,
output
);
}
}

Expand Down Expand Up @@ -140,11 +186,25 @@ impl Cmov for u64 {
impl CmovEq for u64 {
#[inline]
fn cmoveq(&self, rhs: &Self, input: Condition, output: &mut Condition) {
output.cmovz(&input, (self ^ rhs) as u8);
cmov_eq!(
"xor {0:r}, {1:r}",
"cmovz {2:r}, {3:r}",
self,
rhs,
input,
output
);
}

#[inline]
fn cmovne(&self, rhs: &Self, input: Condition, output: &mut Condition) {
output.cmovnz(&input, (self ^ rhs) as u8);
cmov_eq!(
"xor {0:r}, {1:r}",
"cmovnz {2:r}, {3:r}",
self,
rhs,
input,
output
);
}
}