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