Skip to content

Commit b05b15a

Browse files
authored
cmov: fix builds on x86 (32-bit) targets (#863)
1 parent d771c90 commit b05b15a

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

cmov/src/x86.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl Cmov for u32 {
4949
}
5050
}
5151

52+
#[cfg(target_arch = "x86_64")]
5253
impl Cmov for u64 {
5354
#[inline(always)]
5455
fn cmovz(&mut self, value: Self, condition: Condition) {
@@ -60,3 +61,28 @@ impl Cmov for u64 {
6061
cmov!("cmovnz {1:r}, {2:r}", self, value, condition);
6162
}
6263
}
64+
65+
#[cfg(target_arch = "x86")]
66+
impl Cmov for u64 {
67+
#[inline(always)]
68+
fn cmovz(&mut self, value: Self, condition: Condition) {
69+
let mut lo = (*self & u32::MAX as u64) as u32;
70+
let mut hi = (*self >> 32) as u32;
71+
72+
lo.cmovz((value & u32::MAX as u64) as u32, condition);
73+
hi.cmovz((value >> 32) as u32, condition);
74+
75+
*self = (lo as u64) | (hi as u64) << 32;
76+
}
77+
78+
#[inline(always)]
79+
fn cmovnz(&mut self, value: Self, condition: Condition) {
80+
let mut lo = (*self & u32::MAX as u64) as u32;
81+
let mut hi = (*self >> 32) as u32;
82+
83+
lo.cmovnz((value & u32::MAX as u64) as u32, condition);
84+
hi.cmovnz((value >> 32) as u32, condition);
85+
86+
*self = (lo as u64) | (hi as u64) << 32;
87+
}
88+
}

cmov/tests/lib.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,27 +93,27 @@ mod u64 {
9393

9494
#[test]
9595
fn cmovz_works() {
96-
let mut n = 0x11111111_11111111u64;
96+
let mut n = 0x1111_1111_1111_1111_u64;
9797

9898
for cond in 1..0xFF {
99-
n.cmovz(0x22222222_22222222, cond);
100-
assert_eq!(n, 0x11111111_11111111);
99+
n.cmovz(0x2222_2222_2222_2222, cond);
100+
assert_eq!(n, 0x1111_1111_1111_1111);
101101
}
102102

103-
n.cmovz(0x22222222_22222222, 0);
104-
assert_eq!(n, 0x22222222_22222222);
103+
n.cmovz(0x2222_2222_2222_2222, 0);
104+
assert_eq!(n, 0x2222_2222_2222_2222);
105105
}
106106

107107
#[test]
108108
fn cmovnz_works() {
109-
let mut n = 0x11111111_11111111u64;
110-
n.cmovnz(0x22222222_22222222, 0);
111-
assert_eq!(n, 0x11111111_11111111);
109+
let mut n = 0x1111_1111_1111_1111_u64;
110+
n.cmovnz(0x2222_2222_2222_2222, 0);
111+
assert_eq!(n, 0x1111_1111_1111_1111);
112112

113113
for cond in 1..0xFF {
114-
let mut n = 0x11111111_11111111u64;
115-
n.cmovnz(0x22222222_22222222, cond);
116-
assert_eq!(n, 0x22222222_22222222);
114+
let mut n = 0x1111_1111_1111_1111_u64;
115+
n.cmovnz(0x2222_2222_2222_2222, cond);
116+
assert_eq!(n, 0x2222_2222_2222_2222);
117117
}
118118
}
119119
}
@@ -123,27 +123,27 @@ mod u128 {
123123

124124
#[test]
125125
fn cmovz_works() {
126-
let mut n = 0x11111111_11111111_22222222_22222222u128;
126+
let mut n = 0x1111_1111_1111_1111_2222_2222_2222_2222_u128;
127127

128128
for cond in 1..0xFF {
129-
n.cmovz(0x22222222_22222222_33333333_33333333, cond);
130-
assert_eq!(n, 0x11111111_11111111_22222222_22222222);
129+
n.cmovz(0x2222_2222_2222_2222_3333_3333_3333_3333, cond);
130+
assert_eq!(n, 0x1111_1111_1111_1111_2222_2222_2222_2222);
131131
}
132132

133-
n.cmovz(0x22222222_22222222_33333333_33333333, 0);
134-
assert_eq!(n, 0x22222222_22222222_33333333_33333333);
133+
n.cmovz(0x2222_2222_2222_2222_3333_3333_3333_3333, 0);
134+
assert_eq!(n, 0x2222_2222_2222_2222_3333_3333_3333_3333);
135135
}
136136

137137
#[test]
138138
fn cmovnz_works() {
139-
let mut n = 0x11111111_11111111_22222222_22222222u128;
140-
n.cmovnz(0x22222222_22222222_33333333_33333333, 0);
141-
assert_eq!(n, 0x11111111_11111111_22222222_22222222);
139+
let mut n = 0x1111_1111_1111_1111_2222_2222_2222_2222_u128;
140+
n.cmovnz(0x2222_2222_2222_2222_3333_3333_3333_3333, 0);
141+
assert_eq!(n, 0x1111_1111_1111_1111_2222_2222_2222_2222);
142142

143143
for cond in 1..0xFF {
144-
let mut n = 0x11111111_11111111u128;
145-
n.cmovnz(0x22222222_22222222_33333333_33333333, cond);
146-
assert_eq!(n, 0x22222222_22222222_33333333_33333333);
144+
let mut n = 0x1111_1111_1111_1111_u128;
145+
n.cmovnz(0x2222_2222_2222_2222_3333_3333_3333_3333, cond);
146+
assert_eq!(n, 0x2222_2222_2222_2222_3333_3333_3333_3333);
147147
}
148148
}
149149
}

0 commit comments

Comments
 (0)