Skip to content

Commit 61808d5

Browse files
authored
cmov: fix size int implementation of cmovnz (#1412)
Current `Cmov` implementation for `usize` and `isize` does nothing since it mutates a temporary value. This commit fixes this and add tests for `usize` and `isize`. I am surprised that there is not a clippy lint that warns about this. Co-authored-by: Nics <NicsTr@users.noreply.github.com>
1 parent 2ec2736 commit 61808d5

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

cmov/src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,21 +225,27 @@ macro_rules! impl_cmov_traits_for_size_int {
225225
#[allow(clippy::cast_possible_truncation)]
226226
#[inline]
227227
fn cmovnz(&mut self, other: &Self, condition: Condition) {
228-
(*self as $int16).cmovnz(&(*other as $int16), condition);
228+
let mut tmp = *self as $int16;
229+
tmp.cmovnz(&(*other as $int16), condition);
230+
*self = tmp as $size;
229231
}
230232

231233
#[cfg(target_pointer_width = "32")]
232234
#[allow(clippy::cast_possible_truncation)]
233235
#[inline]
234236
fn cmovnz(&mut self, other: &Self, condition: Condition) {
235-
(*self as $int32).cmovnz(&(*other as $int32), condition);
237+
let mut tmp = *self as $int32;
238+
tmp.cmovnz(&(*other as $int32), condition);
239+
*self = tmp as $size;
236240
}
237241

238242
#[cfg(target_pointer_width = "64")]
239243
#[allow(clippy::cast_possible_truncation)]
240244
#[inline]
241245
fn cmovnz(&mut self, other: &Self, condition: Condition) {
242-
(*self as $int64).cmovnz(&(*other as $int64), condition);
246+
let mut tmp = *self as $int64;
247+
tmp.cmovnz(&(*other as $int64), condition);
248+
*self = tmp as $size;
243249
}
244250
}
245251

cmov/src/macros.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ mod tests {
5757
masknz_u16: u16,
5858
masknz_u32: u32,
5959
masknz_u64: u64,
60-
masknz_u128: u128
60+
masknz_u128: u128,
61+
masknz_usize: usize
6162
);
6263
}

cmov/tests/core_impls.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ int_tests!(
114114
0x1111_1111_1111_1111_1111_1111_1111_1111i128,
115115
-0x2222_2222_2222_2222_2222_2222_2222_2222i128
116116
);
117+
int_tests!(isize, 0x1111isize, -0x2222isize);
117118
int_tests!(u8, 0x11u8, 0x22u8);
118119
int_tests!(u16, 0x1111u16, 0x2222u16);
119120
int_tests!(u32, 0x1111_1111u32, 0x2222_2222u32);
@@ -123,6 +124,7 @@ int_tests!(
123124
0x1111_1111_1111_1111_2222_2222_2222_2222u128,
124125
0x2222_2222_2222_2222_3333_3333_3333_3333u128
125126
);
127+
int_tests!(usize, 0x1111usize, 0x2222usize);
126128

127129
mod ordering {
128130
use cmov::{Cmov, CmovEq};

cmov/tests/proptests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ macro_rules! byte_array_proptests {
9292
};
9393
}
9494

95-
int_proptests!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128);
95+
int_proptests!(
96+
i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize
97+
);
9698
byte_array_proptests!(
9799
array0: 0,
98100
array1: 1,

0 commit comments

Comments
 (0)