Skip to content

Commit 904ed91

Browse files
committed
ctutils: use signed Cmov* trait impls
Uses the `Cmov`/`CmovEq` impls added to signed integers (e.g. `i8`, `i16`) in #1280 to simplify the macros for writing impls of `CtEq` and `CtSelect` which are backed by the afforementioned `Cmov*` traits. This also allows all of the optimization work to happen in the `cmov` crate.
1 parent 36e4cef commit 904ed91

File tree

3 files changed

+10
-41
lines changed

3 files changed

+10
-41
lines changed

ctutils/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ edition = "2024"
1717
rust-version = "1.85"
1818

1919
[dependencies]
20-
cmov = "0.4"
20+
cmov = "0.4.2"
2121

2222
# optional dependencies
2323
subtle = { version = "2", optional = true, default-features = false }

ctutils/src/traits/ct_eq.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ where
2020
}
2121

2222
// Impl `CtEq` using the `cmov::CmovEq` trait
23-
macro_rules! impl_unsigned_ct_eq_with_cmov {
24-
( $($uint:ty),+ ) => {
23+
macro_rules! impl_ct_eq_with_cmov_eq {
24+
( $($ty:ty),+ ) => {
2525
$(
26-
impl CtEq for $uint {
26+
impl CtEq for $ty {
2727
#[inline]
2828
fn ct_eq(&self, other: &Self) -> Choice {
2929
let mut ret = 0;
@@ -35,23 +35,7 @@ macro_rules! impl_unsigned_ct_eq_with_cmov {
3535
};
3636
}
3737

38-
// Impl `CtEq` by first casting to unsigned then using the unsigned `CtEq` impls
39-
// TODO(tarcieri): add signed integer support to `cmov`
40-
macro_rules! impl_signed_ct_eq_with_cmov {
41-
( $($int:ty => $uint:ty),+ ) => {
42-
$(
43-
impl CtEq for $int {
44-
#[inline]
45-
fn ct_eq(&self, other: &Self) -> Choice {
46-
(*self as $uint).ct_eq(&(*other as $uint))
47-
}
48-
}
49-
)+
50-
};
51-
}
52-
53-
impl_unsigned_ct_eq_with_cmov!(u8, u16, u32, u64, u128);
54-
impl_signed_ct_eq_with_cmov!(i8 => u8, i16 => u16, i32 => u32, i64 => u64, i128 => u128);
38+
impl_ct_eq_with_cmov_eq!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128);
5539

5640
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
5741
impl CtEq for usize {

ctutils/src/traits/ct_select.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ pub trait CtSelect: Sized {
2525
}
2626

2727
// Impl `CtSelect` using the `cmov::Cmov` trait
28-
macro_rules! impl_unsigned_ct_select_with_cmov {
29-
( $($uint:ty),+ ) => {
28+
macro_rules! impl_ct_select_with_cmov {
29+
( $($ty:ty),+ ) => {
3030
$(
31-
impl CtSelect for $uint {
31+
impl CtSelect for $ty {
3232
#[inline]
3333
fn ct_select(&self, other: &Self, choice: Choice) -> Self {
3434
let mut ret = *self;
3535
ret.ct_assign(other, choice);
3636
ret
3737
}
3838

39+
#[inline]
3940
fn ct_assign(&mut self, other: &Self, choice: Choice) {
4041
self.cmovnz(other, choice.into());
4142
}
@@ -44,23 +45,7 @@ macro_rules! impl_unsigned_ct_select_with_cmov {
4445
};
4546
}
4647

47-
// Impl `CtSelect` by first casting to unsigned then using the unsigned `CtSelect` impls
48-
// TODO(tarcieri): add signed integer support to `cmov`
49-
macro_rules! impl_signed_ct_select_with_cmov {
50-
( $($int:ty => $uint:ty),+ ) => {
51-
$(
52-
impl CtSelect for $int {
53-
#[inline]
54-
fn ct_select(&self, other: &Self, choice: Choice) -> Self {
55-
(*self as $uint).ct_select(&(*other as $uint), choice) as Self
56-
}
57-
}
58-
)+
59-
};
60-
}
61-
62-
impl_unsigned_ct_select_with_cmov!(u8, u16, u32, u64, u128);
63-
impl_signed_ct_select_with_cmov!(i8 => u8, i16 => u16, i32 => u32, i64 => u64, i128 => u128);
48+
impl_ct_select_with_cmov!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128);
6449

6550
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
6651
impl CtSelect for usize {

0 commit comments

Comments
 (0)