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