Skip to content

Commit 5597725

Browse files
authored
cmov: fix portable cmovnz for thumbv6m-none-eabi (#1332)
`thumbv6m-none-eabi` (Cortex M0, M0+ and M1) compiler emits non-constant time assembly when using `cmovnz` (portable version). This commit fixes it. --------- Co-authored-by: Nics <NicsTr@users.noreply.github.com>
1 parent 9e555db commit 5597725

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

.typos.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
extend-exclude = [
33
".git/"
44
]
5+
6+
[default.extend-words]
7+
# Prefix of Conditional MOVe
8+
"CMO" = "CMO"

cmov/src/portable.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use core::hint::black_box;
1212
/// Bitwise non-zero: returns `1` if `x != 0`, and otherwise returns `0`.
1313
macro_rules! bitnz {
1414
($value:expr, $bits:expr) => {
15-
($value | $value.wrapping_neg()) >> ($bits - 1)
15+
black_box(($value | $value.wrapping_neg()) >> ($bits - 1))
1616
};
1717
}
1818

@@ -75,13 +75,13 @@ impl CmovEq for u32 {
7575
impl Cmov for u64 {
7676
#[inline]
7777
fn cmovnz(&mut self, value: &Self, condition: Condition) {
78-
let mask = black_box((bitnz!(condition, u8::BITS) as u64).wrapping_sub(1));
78+
let mask = (bitnz!(condition, u8::BITS) as u64).wrapping_sub(1);
7979
*self = (*self & mask) | (*value & !mask);
8080
}
8181

8282
#[inline]
8383
fn cmovz(&mut self, value: &Self, condition: Condition) {
84-
let mask = black_box((1 ^ bitnz!(condition, u8::BITS) as u64).wrapping_sub(1));
84+
let mask = (1 ^ bitnz!(condition, u8::BITS) as u64).wrapping_sub(1);
8585
*self = (*self & mask) | (*value & !mask);
8686
}
8787
}
@@ -90,12 +90,12 @@ impl CmovEq for u64 {
9090
#[inline]
9191
fn cmovne(&self, rhs: &Self, input: Condition, output: &mut Condition) {
9292
let ne = bitnz!(self ^ rhs, u64::BITS) as u8;
93-
output.cmovnz(&input, black_box(ne));
93+
output.cmovnz(&input, ne);
9494
}
9595

9696
#[inline]
9797
fn cmoveq(&self, rhs: &Self, input: Condition, output: &mut Condition) {
9898
let ne = bitnz!(self ^ rhs, u64::BITS) as u8;
99-
output.cmovnz(&input, black_box(ne ^ 1));
99+
output.cmovnz(&input, ne ^ 1);
100100
}
101101
}

0 commit comments

Comments
 (0)