| //! Utility macros. |
| |
| macro_rules! constify_imm6 { |
| ($imm8:expr, $expand:ident) => { |
| #[allow(overflowing_literals)] |
| match ($imm8) & 0b1_1111 { |
| 0 => $expand!(0), |
| 1 => $expand!(1), |
| 2 => $expand!(2), |
| 3 => $expand!(3), |
| 4 => $expand!(4), |
| 5 => $expand!(5), |
| 6 => $expand!(6), |
| 7 => $expand!(7), |
| 8 => $expand!(8), |
| 9 => $expand!(9), |
| 10 => $expand!(10), |
| 11 => $expand!(11), |
| 12 => $expand!(12), |
| 13 => $expand!(13), |
| 14 => $expand!(14), |
| 15 => $expand!(15), |
| 16 => $expand!(16), |
| 17 => $expand!(17), |
| 18 => $expand!(18), |
| 19 => $expand!(19), |
| 20 => $expand!(20), |
| 21 => $expand!(21), |
| 22 => $expand!(22), |
| 23 => $expand!(23), |
| 24 => $expand!(24), |
| 25 => $expand!(25), |
| 26 => $expand!(26), |
| 27 => $expand!(27), |
| 28 => $expand!(28), |
| 29 => $expand!(29), |
| 30 => $expand!(30), |
| _ => $expand!(31), |
| } |
| }; |
| } |
| |
| macro_rules! constify_imm4 { |
| ($imm8:expr, $expand:ident) => { |
| #[allow(overflowing_literals)] |
| match ($imm8) & 0b1111 { |
| 0 => $expand!(0), |
| 1 => $expand!(1), |
| 2 => $expand!(2), |
| 3 => $expand!(3), |
| 4 => $expand!(4), |
| 5 => $expand!(5), |
| 6 => $expand!(6), |
| 7 => $expand!(7), |
| 8 => $expand!(8), |
| 9 => $expand!(9), |
| 10 => $expand!(10), |
| 11 => $expand!(11), |
| 12 => $expand!(12), |
| 13 => $expand!(13), |
| 14 => $expand!(14), |
| _ => $expand!(15), |
| } |
| }; |
| } |
| |
| macro_rules! constify_imm3 { |
| ($imm8:expr, $expand:ident) => { |
| #[allow(overflowing_literals)] |
| match ($imm8) & 0b111 { |
| 0 => $expand!(0), |
| 1 => $expand!(1), |
| 2 => $expand!(2), |
| 3 => $expand!(3), |
| 4 => $expand!(4), |
| 5 => $expand!(5), |
| 6 => $expand!(6), |
| _ => $expand!(7), |
| } |
| }; |
| } |
| |
| macro_rules! constify_imm2 { |
| ($imm8:expr, $expand:ident) => { |
| #[allow(overflowing_literals)] |
| match ($imm8) & 0b11 { |
| 0 => $expand!(0), |
| 1 => $expand!(1), |
| 2 => $expand!(2), |
| _ => $expand!(3), |
| } |
| }; |
| } |
| |
| #[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() |
| ); |
| }}; |
| } |