Skip to content

Commit

Permalink
cmov: fix builds on x86 (32-bit) targets (#863)
Browse files Browse the repository at this point in the history
  • Loading branch information
brxken128 authored Mar 24, 2023
1 parent d771c90 commit b05b15a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
26 changes: 26 additions & 0 deletions cmov/src/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl Cmov for u32 {
}
}

#[cfg(target_arch = "x86_64")]
impl Cmov for u64 {
#[inline(always)]
fn cmovz(&mut self, value: Self, condition: Condition) {
Expand All @@ -60,3 +61,28 @@ impl Cmov for u64 {
cmov!("cmovnz {1:r}, {2:r}", self, value, condition);
}
}

#[cfg(target_arch = "x86")]
impl Cmov for u64 {
#[inline(always)]
fn cmovz(&mut self, value: Self, condition: Condition) {
let mut lo = (*self & u32::MAX as u64) as u32;
let mut hi = (*self >> 32) as u32;

lo.cmovz((value & u32::MAX as u64) as u32, condition);
hi.cmovz((value >> 32) as u32, condition);

*self = (lo as u64) | (hi as u64) << 32;
}

#[inline(always)]
fn cmovnz(&mut self, value: Self, condition: Condition) {
let mut lo = (*self & u32::MAX as u64) as u32;
let mut hi = (*self >> 32) as u32;

lo.cmovnz((value & u32::MAX as u64) as u32, condition);
hi.cmovnz((value >> 32) as u32, condition);

*self = (lo as u64) | (hi as u64) << 32;
}
}
44 changes: 22 additions & 22 deletions cmov/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,27 @@ mod u64 {

#[test]
fn cmovz_works() {
let mut n = 0x11111111_11111111u64;
let mut n = 0x1111_1111_1111_1111_u64;

for cond in 1..0xFF {
n.cmovz(0x22222222_22222222, cond);
assert_eq!(n, 0x11111111_11111111);
n.cmovz(0x2222_2222_2222_2222, cond);
assert_eq!(n, 0x1111_1111_1111_1111);
}

n.cmovz(0x22222222_22222222, 0);
assert_eq!(n, 0x22222222_22222222);
n.cmovz(0x2222_2222_2222_2222, 0);
assert_eq!(n, 0x2222_2222_2222_2222);
}

#[test]
fn cmovnz_works() {
let mut n = 0x11111111_11111111u64;
n.cmovnz(0x22222222_22222222, 0);
assert_eq!(n, 0x11111111_11111111);
let mut n = 0x1111_1111_1111_1111_u64;
n.cmovnz(0x2222_2222_2222_2222, 0);
assert_eq!(n, 0x1111_1111_1111_1111);

for cond in 1..0xFF {
let mut n = 0x11111111_11111111u64;
n.cmovnz(0x22222222_22222222, cond);
assert_eq!(n, 0x22222222_22222222);
let mut n = 0x1111_1111_1111_1111_u64;
n.cmovnz(0x2222_2222_2222_2222, cond);
assert_eq!(n, 0x2222_2222_2222_2222);
}
}
}
Expand All @@ -123,27 +123,27 @@ mod u128 {

#[test]
fn cmovz_works() {
let mut n = 0x11111111_11111111_22222222_22222222u128;
let mut n = 0x1111_1111_1111_1111_2222_2222_2222_2222_u128;

for cond in 1..0xFF {
n.cmovz(0x22222222_22222222_33333333_33333333, cond);
assert_eq!(n, 0x11111111_11111111_22222222_22222222);
n.cmovz(0x2222_2222_2222_2222_3333_3333_3333_3333, cond);
assert_eq!(n, 0x1111_1111_1111_1111_2222_2222_2222_2222);
}

n.cmovz(0x22222222_22222222_33333333_33333333, 0);
assert_eq!(n, 0x22222222_22222222_33333333_33333333);
n.cmovz(0x2222_2222_2222_2222_3333_3333_3333_3333, 0);
assert_eq!(n, 0x2222_2222_2222_2222_3333_3333_3333_3333);
}

#[test]
fn cmovnz_works() {
let mut n = 0x11111111_11111111_22222222_22222222u128;
n.cmovnz(0x22222222_22222222_33333333_33333333, 0);
assert_eq!(n, 0x11111111_11111111_22222222_22222222);
let mut n = 0x1111_1111_1111_1111_2222_2222_2222_2222_u128;
n.cmovnz(0x2222_2222_2222_2222_3333_3333_3333_3333, 0);
assert_eq!(n, 0x1111_1111_1111_1111_2222_2222_2222_2222);

for cond in 1..0xFF {
let mut n = 0x11111111_11111111u128;
n.cmovnz(0x22222222_22222222_33333333_33333333, cond);
assert_eq!(n, 0x22222222_22222222_33333333_33333333);
let mut n = 0x1111_1111_1111_1111_u128;
n.cmovnz(0x2222_2222_2222_2222_3333_3333_3333_3333, cond);
assert_eq!(n, 0x2222_2222_2222_2222_3333_3333_3333_3333);
}
}
}

0 comments on commit b05b15a

Please sign in to comment.