Skip to content

Commit 2fc8266

Browse files
committed
add macros to validate imm2, imm3, and imm4
1 parent 8efa2f1 commit 2fc8266

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

crates/core_arch/src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Utility macros.
22
33
// 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>();
4+
// out of 8-bit range.
5+
pub(crate) struct ValidateConstImm8<const imm8: i32>;
66
impl<const imm8: i32> ValidateConstImm8<imm8> {
77
pub(crate) const VALID: () = {
88
let _ = 1 / ((imm8 >= 0 && imm8 <= 255) as usize);

crates/core_arch/src/x86/macros.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,51 @@
11
//! Utility macros.
2+
// Helper struct used to trigger const eval errors when a const generic immediate value is
3+
// out of 2-bit range.
4+
pub(crate) struct ValidateConstImm2<const imm2: i32>;
5+
impl<const imm2: i32> ValidateConstImm2<imm2> {
6+
pub(crate) const VALID: () = {
7+
let _ = 1 / ((imm2 >= 0 && imm2 <= 3) as usize);
8+
};
9+
}
10+
11+
#[allow(unused)]
12+
macro_rules! static_assert_imm2 {
13+
($imm:ident) => {
14+
let _ = $crate::core_arch::x86::macros::ValidateConstImm2::<$imm>::VALID;
15+
};
16+
}
217

18+
// Helper struct used to trigger const eval errors when a const generic immediate value is
19+
// out of 3-bit range.
20+
pub(crate) struct ValidateConstImm3<const imm3: i32>;
21+
impl<const imm3: i32> ValidateConstImm3<imm3> {
22+
pub(crate) const VALID: () = {
23+
let _ = 1 / ((imm3 >= 0 && imm3 <= 7) as usize);
24+
};
25+
}
26+
27+
#[allow(unused)]
28+
macro_rules! static_assert_imm3 {
29+
($imm:ident) => {
30+
let _ = $crate::core_arch::x86::macros::ValidateConstImm3::<$imm>::VALID;
31+
};
32+
}
33+
34+
// Helper struct used to trigger const eval errors when a const generic immediate value is
35+
// out of 4-bit range.
36+
pub(crate) struct ValidateConstImm4<const imm4: i32>;
37+
impl<const imm4: i32> ValidateConstImm4<imm4> {
38+
pub(crate) const VALID: () = {
39+
let _ = 1 / ((imm4 >= 0 && imm4 <= 15) as usize);
40+
};
41+
}
42+
43+
#[allow(unused)]
44+
macro_rules! static_assert_imm4 {
45+
($imm:ident) => {
46+
let _ = $crate::core_arch::x86::macros::ValidateConstImm4::<$imm>::VALID;
47+
};
48+
}
349
macro_rules! constify_imm6 {
450
($imm8:expr, $expand:ident) => {
551
#[allow(overflowing_literals)]

0 commit comments

Comments
 (0)