diff options
Diffstat (limited to 'library/stdarch/crates/core_arch/src/macros.rs')
-rw-r--r-- | library/stdarch/crates/core_arch/src/macros.rs | 202 |
1 files changed, 45 insertions, 157 deletions
diff --git a/library/stdarch/crates/core_arch/src/macros.rs b/library/stdarch/crates/core_arch/src/macros.rs index 1c917c52b..0c86a24ca 100644 --- a/library/stdarch/crates/core_arch/src/macros.rs +++ b/library/stdarch/crates/core_arch/src/macros.rs @@ -1,84 +1,54 @@ //! Utility macros. -// Helper struct used to trigger const eval errors when the const generic immediate value `IMM` is -// out of `[MIN-MAX]` range. -pub(crate) struct ValidateConstImm<const IMM: i32, const MIN: i32, const MAX: i32>; -impl<const IMM: i32, const MIN: i32, const MAX: i32> ValidateConstImm<IMM, MIN, MAX> { - pub(crate) const VALID: () = { - assert!(IMM >= MIN && IMM <= MAX, "IMM value not in expected range"); - }; -} - -#[allow(unused_macros)] -macro_rules! static_assert_imm1 { - ($imm:ident) => { - let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 0, { (1 << 1) - 1 }>::VALID; - }; -} - -#[allow(unused_macros)] -macro_rules! static_assert_imm2 { - ($imm:ident) => { - let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 0, { (1 << 2) - 1 }>::VALID; - }; -} - -#[allow(unused_macros)] -macro_rules! static_assert_imm3 { - ($imm:ident) => { - let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 0, { (1 << 3) - 1 }>::VALID; - }; -} - -#[allow(unused_macros)] -macro_rules! static_assert_imm4 { - ($imm:ident) => { - let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 0, { (1 << 4) - 1 }>::VALID; - }; -} - -#[allow(unused_macros)] -macro_rules! static_assert_imm5 { - ($imm:ident) => { - let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 0, { (1 << 5) - 1 }>::VALID; +#[allow(unused)] +macro_rules! static_assert { + ($e:expr) => { + const { + assert!($e); + } }; -} - -#[allow(unused_macros)] -macro_rules! static_assert_imm6 { - ($imm:ident) => { - let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 0, { (1 << 6) - 1 }>::VALID; + ($e:expr, $msg:expr) => { + const { + assert!($e, $msg); + } }; } #[allow(unused_macros)] -macro_rules! static_assert_imm8 { - ($imm:ident) => { - let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 0, { (1 << 8) - 1 }>::VALID; +macro_rules! static_assert_uimm_bits { + ($imm:ident, $bits:expr) => { + // `0 <= $imm` produces a warning if the immediate has an unsigned type + #[allow(unused_comparisons)] + { + static_assert!( + 0 <= $imm && $imm <= (1 << $bits) - 1, + concat!( + stringify!($imm), + " doesn't fit in ", + stringify!($bits), + " bits", + ) + ) + } }; } #[allow(unused_macros)] -macro_rules! static_assert_imm16 { - ($imm:ident) => { - let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 0, { (1 << 16) - 1 }>::VALID; +macro_rules! static_assert_simm_bits { + ($imm:ident, $bits:expr) => { + static_assert!( + (-1 << ($bits - 1)) - 1 <= $imm && $imm <= (1 << ($bits - 1)) - 1, + concat!( + stringify!($imm), + " doesn't fit in ", + stringify!($bits), + " bits", + ) + ) }; } #[allow(unused)] -macro_rules! static_assert { - ($imm:ident : $ty:ty where $e:expr) => {{ - struct Validate<const $imm: $ty>(); - impl<const $imm: $ty> Validate<$imm> { - const VALID: () = { - assert!($e, concat!("Assertion failed: ", stringify!($e))); - }; - } - let _ = Validate::<$imm>::VALID; - }}; -} - -#[allow(unused)] macro_rules! types { ($( $(#[$doc:meta])* @@ -94,97 +64,15 @@ macro_rules! types { } #[allow(unused)] -macro_rules! simd_shuffle2 { - ($x:expr, $y:expr, <$(const $imm:ident : $ty:ty),+ $(,)?> $idx:expr $(,)?) => {{ - struct ConstParam<$(const $imm: $ty),+>; - impl<$(const $imm: $ty),+> ConstParam<$($imm),+> { - const IDX: [u32; 2] = $idx; - } - - simd_shuffle($x, $y, ConstParam::<$($imm),+>::IDX) - }}; - ($x:expr, $y:expr, $idx:expr $(,)?) => {{ - const IDX: [u32; 2] = $idx; - simd_shuffle($x, $y, IDX) - }}; -} - -#[allow(unused_macros)] -macro_rules! simd_shuffle4 { - ($x:expr, $y:expr, <$(const $imm:ident : $ty:ty),+ $(,)?> $idx:expr $(,)?) => {{ - struct ConstParam<$(const $imm: $ty),+>; - impl<$(const $imm: $ty),+> ConstParam<$($imm),+> { - const IDX: [u32; 4] = $idx; - } - - simd_shuffle($x, $y, ConstParam::<$($imm),+>::IDX) - }}; - ($x:expr, $y:expr, $idx:expr $(,)?) => {{ - const IDX: [u32; 4] = $idx; - simd_shuffle($x, $y, IDX) - }}; -} - -#[allow(unused_macros)] -macro_rules! simd_shuffle8 { - ($x:expr, $y:expr, <$(const $imm:ident : $ty:ty),+ $(,)?> $idx:expr $(,)?) => {{ - struct ConstParam<$(const $imm: $ty),+>; - impl<$(const $imm: $ty),+> ConstParam<$($imm),+> { - const IDX: [u32; 8] = $idx; - } - - simd_shuffle($x, $y, ConstParam::<$($imm),+>::IDX) - }}; - ($x:expr, $y:expr, $idx:expr $(,)?) => {{ - const IDX: [u32; 8] = $idx; - simd_shuffle($x, $y, IDX) - }}; -} - -#[allow(unused)] -macro_rules! simd_shuffle16 { - ($x:expr, $y:expr, <$(const $imm:ident : $ty:ty),+ $(,)?> $idx:expr $(,)?) => {{ - struct ConstParam<$(const $imm: $ty),+>; - impl<$(const $imm: $ty),+> ConstParam<$($imm),+> { - const IDX: [u32; 16] = $idx; - } - - simd_shuffle($x, $y, ConstParam::<$($imm),+>::IDX) - }}; - ($x:expr, $y:expr, $idx:expr $(,)?) => {{ - const IDX: [u32; 16] = $idx; - simd_shuffle($x, $y, IDX) - }}; -} - -#[allow(unused_macros)] -macro_rules! simd_shuffle32 { - ($x:expr, $y:expr, <$(const $imm:ident : $ty:ty),+> $(,)? $idx:expr $(,)?) => {{ - struct ConstParam<$(const $imm: $ty),+>; - impl<$(const $imm: $ty),+> ConstParam<$($imm),+> { - const IDX: [u32; 32] = $idx; - } - - simd_shuffle($x, $y, ConstParam::<$($imm),+>::IDX) - }}; - ($x:expr, $y:expr, $idx:expr $(,)?) => {{ - const IDX: [u32; 32] = $idx; - simd_shuffle($x, $y, IDX) - }}; -} - -#[allow(unused_macros)] -macro_rules! simd_shuffle64 { - ($x:expr, $y:expr, <$(const $imm:ident : $ty:ty),+ $(,)?> $idx:expr $(,)?) => {{ - struct ConstParam<$(const $imm: $ty),+>; - impl<$(const $imm: $ty),+> ConstParam<$($imm),+> { - const IDX: [u32; 64] = $idx; - } - - simd_shuffle($x, $y, ConstParam::<$($imm),+>::IDX) - }}; +macro_rules! simd_shuffle { ($x:expr, $y:expr, $idx:expr $(,)?) => {{ - const IDX: [u32; 64] = $idx; - simd_shuffle($x, $y, IDX) + simd_shuffle( + $x, + $y, + const { + let v: [u32; _] = $idx; + v + }, + ) }}; } |