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