]>
Commit | Line | Data |
---|---|---|
3dfed10e | 1 | //! Utility macros. |
17df50a5 XL |
2 | //! |
3 | // Helper struct used to trigger const eval errors when the const generic immediate value `imm` is | |
4 | // not a round number. | |
5 | pub(crate) struct ValidateConstRound<const IMM: i32>; | |
6 | impl<const IMM: i32> ValidateConstRound<IMM> { | |
7 | pub(crate) const VALID: () = { | |
8 | assert!( | |
9 | IMM == 4 || IMM == 8 || IMM == 9 || IMM == 10 || IMM == 11, | |
10 | "Invalid IMM value" | |
11 | ); | |
3dfed10e XL |
12 | }; |
13 | } | |
14 | ||
17df50a5 XL |
15 | #[allow(unused)] |
16 | macro_rules! static_assert_rounding { | |
17 | ($imm:ident) => { | |
18 | let _ = $crate::core_arch::x86::macros::ValidateConstRound::<$imm>::VALID; | |
3dfed10e XL |
19 | }; |
20 | } | |
21 | ||
17df50a5 XL |
22 | // Helper struct used to trigger const eval errors when the const generic immediate value `imm` is |
23 | // not a sae number. | |
24 | pub(crate) struct ValidateConstSae<const IMM: i32>; | |
25 | impl<const IMM: i32> ValidateConstSae<IMM> { | |
26 | pub(crate) const VALID: () = { | |
27 | assert!(IMM == 4 || IMM == 8, "Invalid IMM value"); | |
3dfed10e XL |
28 | }; |
29 | } | |
30 | ||
3dfed10e | 31 | #[allow(unused)] |
17df50a5 XL |
32 | macro_rules! static_assert_sae { |
33 | ($imm:ident) => { | |
34 | let _ = $crate::core_arch::x86::macros::ValidateConstSae::<$imm>::VALID; | |
3dfed10e XL |
35 | }; |
36 | } | |
37 | ||
17df50a5 XL |
38 | // Helper struct used to trigger const eval errors when the const generic immediate value `imm` is |
39 | // not a mantissas sae number. | |
40 | pub(crate) struct ValidateConstMantissasSae<const IMM: i32>; | |
41 | impl<const IMM: i32> ValidateConstMantissasSae<IMM> { | |
42 | pub(crate) const VALID: () = { | |
43 | assert!(IMM == 4 || IMM == 8 || IMM == 12, "Invalid IMM value"); | |
1b1a35ee XL |
44 | }; |
45 | } | |
46 | ||
1b1a35ee | 47 | #[allow(unused)] |
17df50a5 XL |
48 | macro_rules! static_assert_mantissas_sae { |
49 | ($imm:ident) => { | |
50 | let _ = $crate::core_arch::x86::macros::ValidateConstMantissasSae::<$imm>::VALID; | |
1b1a35ee XL |
51 | }; |
52 | } | |
53 | ||
17df50a5 XL |
54 | // Helper struct used to trigger const eval errors when the unsigned const generic immediate value |
55 | // `IMM` is out of `[MIN-MAX]` range. | |
56 | pub(crate) struct ValidateConstImmU32<const IMM: u32, const MIN: u32, const MAX: u32>; | |
57 | impl<const IMM: u32, const MIN: u32, const MAX: u32> ValidateConstImmU32<IMM, MIN, MAX> { | |
58 | pub(crate) const VALID: () = { | |
59 | assert!(IMM >= MIN && IMM <= MAX, "IMM value not in expected range"); | |
1b1a35ee XL |
60 | }; |
61 | } | |
62 | ||
17df50a5 XL |
63 | #[allow(unused_macros)] |
64 | macro_rules! static_assert_imm_u8 { | |
65 | ($imm:ident) => { | |
66 | let _ = | |
67 | $crate::core_arch::x86::macros::ValidateConstImmU32::<$imm, 0, { (1 << 8) - 1 }>::VALID; | |
1b1a35ee XL |
68 | }; |
69 | } | |
70 | ||
17df50a5 XL |
71 | // Helper struct used to trigger const eval errors when the const generic immediate value `SCALE` is |
72 | // not valid for gather instructions: the only valid scale values are 1, 2, 4 and 8. | |
73 | pub(crate) struct ValidateConstGatherScale<const SCALE: i32>; | |
74 | impl<const SCALE: i32> ValidateConstGatherScale<SCALE> { | |
75 | pub(crate) const VALID: () = { | |
76 | assert!( | |
77 | SCALE == 1 || SCALE == 2 || SCALE == 4 || SCALE == 8, | |
78 | "Invalid SCALE value" | |
79 | ); | |
1b1a35ee XL |
80 | }; |
81 | } | |
82 | ||
fc512014 | 83 | #[allow(unused)] |
17df50a5 XL |
84 | macro_rules! static_assert_imm8_scale { |
85 | ($imm:ident) => { | |
86 | let _ = $crate::core_arch::x86::macros::ValidateConstGatherScale::<$imm>::VALID; | |
fc512014 XL |
87 | }; |
88 | } | |
89 | ||
3dfed10e XL |
90 | #[cfg(test)] |
91 | macro_rules! assert_approx_eq { | |
92 | ($a:expr, $b:expr, $eps:expr) => {{ | |
93 | let (a, b) = (&$a, &$b); | |
94 | assert!( | |
95 | (*a - *b).abs() < $eps, | |
96 | "assertion failed: `(left !== right)` \ | |
97 | (left: `{:?}`, right: `{:?}`, expect diff: `{:?}`, real diff: `{:?}`)", | |
98 | *a, | |
99 | *b, | |
100 | $eps, | |
101 | (*a - *b).abs() | |
102 | ); | |
103 | }}; | |
104 | } |