Skip to content

Commit cfc519a

Browse files
committed
add macros to validate imm2, imm3, and imm4
1 parent 894e91c commit cfc519a

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

crates/core_arch/src/macros.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
11
//! Utility macros.
22
3-
// Helper struct used to trigger const eval errors when a const generic immediate value is
4-
// out of range.
5-
pub(crate) struct ValidateConstImm8<const imm8: i32>();
6-
impl<const imm8: i32> ValidateConstImm8<imm8> {
3+
// Helper struct used to trigger const eval errors when the const generic immediate value `imm` is
4+
// out of `bits`-bit range.
5+
pub(crate) struct ValidateConstImm<const imm: i32, const bits: i32>;
6+
impl<const imm: i32, const bits: i32> ValidateConstImm<imm, bits> {
77
pub(crate) const VALID: () = {
8-
let _ = 1 / ((imm8 >= 0 && imm8 <= 255) as usize);
8+
let _ = 1 / ((imm >= 0 && imm < (1 << bits)) as usize);
9+
};
10+
}
11+
12+
#[allow(unused)]
13+
macro_rules! static_assert_imm2 {
14+
($imm:ident) => {
15+
let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 2>::VALID;
16+
};
17+
}
18+
19+
#[allow(unused)]
20+
macro_rules! static_assert_imm3 {
21+
($imm:ident) => {
22+
let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 3>::VALID;
23+
};
24+
}
25+
26+
#[allow(unused)]
27+
macro_rules! static_assert_imm4 {
28+
($imm:ident) => {
29+
let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 4>::VALID;
930
};
1031
}
1132

1233
#[allow(unused)]
1334
macro_rules! static_assert_imm8 {
1435
($imm:ident) => {
15-
let _ = $crate::core_arch::macros::ValidateConstImm8::<$imm>::VALID;
36+
let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 8>::VALID;
1637
};
1738
}
1839

0 commit comments

Comments
 (0)