Skip to content

Commit 582883a

Browse files
authored
cmov: XOR within the ASM block (#925)
1 parent 895b4a5 commit 582883a

File tree

1 file changed

+66
-6
lines changed

1 file changed

+66
-6
lines changed

cmov/src/x86.rs

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ macro_rules! cmov {
2525
};
2626
}
2727

28+
macro_rules! cmov_eq {
29+
($xor:expr, $instruction:expr, $lhs:expr, $rhs:expr, $condition:expr, $dst:expr) => {
30+
let mut tmp = *$dst as u16;
31+
unsafe {
32+
asm! {
33+
$xor,
34+
$instruction,
35+
inout(reg) *$lhs => _,
36+
in(reg) *$rhs,
37+
inlateout(reg) tmp,
38+
in(reg) $condition as u16,
39+
options(pure, nomem, nostack),
40+
};
41+
}
42+
*$dst = tmp as u8;
43+
};
44+
}
45+
2846
impl Cmov for u16 {
2947
#[inline]
3048
fn cmovnz(&mut self, value: &Self, condition: Condition) {
@@ -40,12 +58,26 @@ impl Cmov for u16 {
4058
impl CmovEq for u16 {
4159
#[inline]
4260
fn cmoveq(&self, rhs: &Self, input: Condition, output: &mut Condition) {
43-
output.cmovz(&input, (self ^ rhs) as u8);
61+
cmov_eq!(
62+
"xor {0:x}, {1:x}",
63+
"cmovz {2:e}, {3:e}",
64+
self,
65+
rhs,
66+
input,
67+
output
68+
);
4469
}
4570

4671
#[inline]
4772
fn cmovne(&self, rhs: &Self, input: Condition, output: &mut Condition) {
48-
output.cmovnz(&input, (self ^ rhs) as u8);
73+
cmov_eq!(
74+
"xor {0:x}, {1:x}",
75+
"cmovnz {2:e}, {3:e}",
76+
self,
77+
rhs,
78+
input,
79+
output
80+
);
4981
}
5082
}
5183

@@ -64,12 +96,26 @@ impl Cmov for u32 {
6496
impl CmovEq for u32 {
6597
#[inline]
6698
fn cmoveq(&self, rhs: &Self, input: Condition, output: &mut Condition) {
67-
output.cmovz(&input, (self ^ rhs) as u8);
99+
cmov_eq!(
100+
"xor {0:e}, {1:e}",
101+
"cmovz {2:e}, {3:e}",
102+
self,
103+
rhs,
104+
input,
105+
output
106+
);
68107
}
69108

70109
#[inline]
71110
fn cmovne(&self, rhs: &Self, input: Condition, output: &mut Condition) {
72-
output.cmovnz(&input, (self ^ rhs) as u8);
111+
cmov_eq!(
112+
"xor {0:e}, {1:e}",
113+
"cmovnz {2:e}, {3:e}",
114+
self,
115+
rhs,
116+
input,
117+
output
118+
);
73119
}
74120
}
75121

@@ -140,11 +186,25 @@ impl Cmov for u64 {
140186
impl CmovEq for u64 {
141187
#[inline]
142188
fn cmoveq(&self, rhs: &Self, input: Condition, output: &mut Condition) {
143-
output.cmovz(&input, (self ^ rhs) as u8);
189+
cmov_eq!(
190+
"xor {0:r}, {1:r}",
191+
"cmovz {2:r}, {3:r}",
192+
self,
193+
rhs,
194+
input,
195+
output
196+
);
144197
}
145198

146199
#[inline]
147200
fn cmovne(&self, rhs: &Self, input: Condition, output: &mut Condition) {
148-
output.cmovnz(&input, (self ^ rhs) as u8);
201+
cmov_eq!(
202+
"xor {0:r}, {1:r}",
203+
"cmovnz {2:r}, {3:r}",
204+
self,
205+
rhs,
206+
input,
207+
output
208+
);
149209
}
150210
}

0 commit comments

Comments
 (0)