Skip to main content

core/stdarch/crates/core_arch/src/x86/
macros.rs

1//! Utility macros.
2
3// Helper macro used to trigger const eval errors when the const generic immediate value `imm` is
4// not a round number.
5#[allow(unused)]
6macro_rules! static_assert_round_mode {
7    ($imm:ident) => {
8        static_assert!($imm >= 0 && $imm < 5, "Invalid IMM value")
9    };
10}
11
12// Helper macro used to trigger const eval errors when the const generic immediate value `imm` is
13// not a round number.
14#[allow(unused)]
15macro_rules! static_assert_rounding {
16    ($imm:ident) => {
17        static_assert!(
18            $imm == 4 || $imm == 8 || $imm == 9 || $imm == 10 || $imm == 11,
19            "Invalid IMM value"
20        )
21    };
22}
23
24// Helper macro used to trigger const eval errors when the const generic immediate value `imm` is
25// not a sae number.
26#[allow(unused)]
27macro_rules! static_assert_sae {
28    ($imm:ident) => {
29        static_assert!($imm == 4 || $imm == 8, "Invalid IMM value")
30    };
31}
32
33// Helper macro used to trigger const eval errors when the const generic immediate value `imm` is
34// not an extended rounding number
35#[allow(unused)]
36macro_rules! static_assert_extended_rounding {
37    ($imm: ident) => {
38        static_assert!(($imm & 7) < 5 && ($imm & !15) == 0, "Invalid IMM value")
39    };
40}
41
42// Helper macro used to trigger const eval errors when the const generic immediate value `imm` is
43// not a mantissas sae number.
44#[allow(unused)]
45macro_rules! static_assert_mantissas_sae {
46    ($imm:ident) => {
47        static_assert!($imm == 4 || $imm == 8 || $imm == 12, "Invalid IMM value")
48    };
49}
50
51// Helper macro used to trigger const eval errors when the const generic immediate value `SCALE` is
52// not valid for gather instructions: the only valid scale values are 1, 2, 4 and 8.
53#[allow(unused)]
54macro_rules! static_assert_imm8_scale {
55    ($imm:ident) => {
56        static_assert!(
57            $imm == 1 || $imm == 2 || $imm == 4 || $imm == 8,
58            "Invalid SCALE value"
59        )
60    };
61}
62
63#[cfg(test)]
64macro_rules! assert_approx_eq {
65    ($a:expr, $b:expr, $eps:expr) => {{
66        let (a, b) = (&$a, &$b);
67        assert!(
68            (*a - *b).abs() < $eps,
69            "assertion failed: `(left !== right)` \
70             (left: `{:?}`, right: `{:?}`, expect diff: `{:?}`, real diff: `{:?}`)",
71            *a,
72            *b,
73            $eps,
74            (*a - *b).abs()
75        );
76    }};
77}
78
79// x86-32 wants to use a 32-bit address size, but asm! defaults to using the full
80// register name (e.g. rax). We have to explicitly override the placeholder to
81// use the 32-bit register name in that case.
82
83#[cfg(target_pointer_width = "32")]
84macro_rules! vpl {
85    ($inst:expr) => {
86        concat!($inst, ", [{p:e}]")
87    };
88}
89#[cfg(target_pointer_width = "64")]
90macro_rules! vpl {
91    ($inst:expr) => {
92        concat!($inst, ", [{p}]")
93    };
94}
95
96#[cfg(target_pointer_width = "32")]
97macro_rules! vps {
98    ($inst1:expr, $inst2:expr) => {
99        concat!($inst1, " [{p:e}]", $inst2)
100    };
101}
102#[cfg(target_pointer_width = "64")]
103macro_rules! vps {
104    ($inst1:expr, $inst2:expr) => {
105        concat!($inst1, " [{p}]", $inst2)
106    };
107}