1 //! Utility macros. 2 3 // Helper struct used to trigger const eval errors when the const generic immediate value `IMM` is 4 // out of `[MIN-MAX]` range. 5 pub(crate) struct ValidateConstImm<const IMM: i32, const MIN: i32, const MAX: i32>; 6 impl<const IMM: i32, const MIN: i32, const MAX: i32> ValidateConstImm<IMM, MIN, MAX> { 7 pub(crate) const VALID: () = { 8 assert!(IMM >= MIN && IMM <= MAX, "IMM value not in expected range"); 9 }; 10 } 11 12 #[allow(unused_macros)] 13 macro_rules! static_assert_imm1 { 14 ($imm:ident) => { 15 let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 0, { (1 << 1) - 1 }>::VALID; 16 }; 17 } 18 19 #[allow(unused_macros)] 20 macro_rules! static_assert_imm2 { 21 ($imm:ident) => { 22 let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 0, { (1 << 2) - 1 }>::VALID; 23 }; 24 } 25 26 #[allow(unused_macros)] 27 macro_rules! static_assert_imm3 { 28 ($imm:ident) => { 29 let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 0, { (1 << 3) - 1 }>::VALID; 30 }; 31 } 32 33 #[allow(unused_macros)] 34 macro_rules! static_assert_imm4 { 35 ($imm:ident) => { 36 let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 0, { (1 << 4) - 1 }>::VALID; 37 }; 38 } 39 40 #[allow(unused_macros)] 41 macro_rules! static_assert_imm5 { 42 ($imm:ident) => { 43 let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 0, { (1 << 5) - 1 }>::VALID; 44 }; 45 } 46 47 #[allow(unused_macros)] 48 macro_rules! static_assert_imm6 { 49 ($imm:ident) => { 50 let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 0, { (1 << 6) - 1 }>::VALID; 51 }; 52 } 53 54 #[allow(unused_macros)] 55 macro_rules! static_assert_imm8 { 56 ($imm:ident) => { 57 let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 0, { (1 << 8) - 1 }>::VALID; 58 }; 59 } 60 61 #[allow(unused_macros)] 62 macro_rules! static_assert_imm16 { 63 ($imm:ident) => { 64 let _ = $crate::core_arch::macros::ValidateConstImm::<$imm, 0, { (1 << 16) - 1 }>::VALID; 65 }; 66 } 67 68 #[allow(unused)] 69 macro_rules! static_assert { 70 ($imm:ident : $ty:ty where $e:expr) => {{ 71 struct Validate<const $imm: $ty>(); 72 impl<const $imm: $ty> Validate<$imm> { 73 const VALID: () = { 74 assert!($e, concat!("Assertion failed: ", stringify!($e))); 75 }; 76 } 77 let _ = Validate::<$imm>::VALID; 78 }}; 79 } 80 81 #[allow(unused)] 82 macro_rules! types { 83 ($( 84 $(#[$doc:meta])* 85 pub struct $name:ident($($fields:tt)*); 86 )*) => ($( 87 $(#[$doc])* 88 #[derive(Copy, Clone, Debug)] 89 #[allow(non_camel_case_types)] 90 #[repr(simd)] 91 #[allow(clippy::missing_inline_in_public_items)] 92 pub struct $name($($fields)*); 93 )*) 94 } 95 96 #[allow(unused_macros)] 97 macro_rules! simd_shuffle2 { 98 ($x:expr, $y:expr, <$(const $imm:ident : $ty:ty),+ $(,)?> $idx:expr $(,)?) => {{ 99 struct ConstParam<$(const $imm: $ty),+>; 100 impl<$(const $imm: $ty),+> ConstParam<$($imm),+> { 101 const IDX: [u32; 2] = $idx; 102 } 103 104 simd_shuffle2($x, $y, ConstParam::<$($imm),+>::IDX) 105 }}; 106 ($x:expr, $y:expr, $idx:expr $(,)?) => {{ 107 const IDX: [u32; 2] = $idx; 108 simd_shuffle2($x, $y, IDX) 109 }}; 110 } 111 112 #[allow(unused_macros)] 113 macro_rules! simd_shuffle4 { 114 ($x:expr, $y:expr, <$(const $imm:ident : $ty:ty),+ $(,)?> $idx:expr $(,)?) => {{ 115 struct ConstParam<$(const $imm: $ty),+>; 116 impl<$(const $imm: $ty),+> ConstParam<$($imm),+> { 117 const IDX: [u32; 4] = $idx; 118 } 119 120 simd_shuffle4($x, $y, ConstParam::<$($imm),+>::IDX) 121 }}; 122 ($x:expr, $y:expr, $idx:expr $(,)?) => {{ 123 const IDX: [u32; 4] = $idx; 124 simd_shuffle4($x, $y, IDX) 125 }}; 126 } 127 128 #[allow(unused_macros)] 129 macro_rules! simd_shuffle8 { 130 ($x:expr, $y:expr, <$(const $imm:ident : $ty:ty),+ $(,)?> $idx:expr $(,)?) => {{ 131 struct ConstParam<$(const $imm: $ty),+>; 132 impl<$(const $imm: $ty),+> ConstParam<$($imm),+> { 133 const IDX: [u32; 8] = $idx; 134 } 135 136 simd_shuffle8($x, $y, ConstParam::<$($imm),+>::IDX) 137 }}; 138 ($x:expr, $y:expr, $idx:expr $(,)?) => {{ 139 const IDX: [u32; 8] = $idx; 140 simd_shuffle8($x, $y, IDX) 141 }}; 142 } 143 144 #[allow(unused_macros)] 145 macro_rules! simd_shuffle16 { 146 ($x:expr, $y:expr, <$(const $imm:ident : $ty:ty),+ $(,)?> $idx:expr $(,)?) => {{ 147 struct ConstParam<$(const $imm: $ty),+>; 148 impl<$(const $imm: $ty),+> ConstParam<$($imm),+> { 149 const IDX: [u32; 16] = $idx; 150 } 151 152 simd_shuffle16($x, $y, ConstParam::<$($imm),+>::IDX) 153 }}; 154 ($x:expr, $y:expr, $idx:expr $(,)?) => {{ 155 const IDX: [u32; 16] = $idx; 156 simd_shuffle16($x, $y, IDX) 157 }}; 158 } 159 160 #[allow(unused_macros)] 161 macro_rules! simd_shuffle32 { 162 ($x:expr, $y:expr, <$(const $imm:ident : $ty:ty),+> $(,)? $idx:expr $(,)?) => {{ 163 struct ConstParam<$(const $imm: $ty),+>; 164 impl<$(const $imm: $ty),+> ConstParam<$($imm),+> { 165 const IDX: [u32; 32] = $idx; 166 } 167 168 simd_shuffle32($x, $y, ConstParam::<$($imm),+>::IDX) 169 }}; 170 ($x:expr, $y:expr, $idx:expr $(,)?) => {{ 171 const IDX: [u32; 32] = $idx; 172 simd_shuffle32($x, $y, IDX) 173 }}; 174 } 175 176 #[allow(unused_macros)] 177 macro_rules! simd_shuffle64 { 178 ($x:expr, $y:expr, <$(const $imm:ident : $ty:ty),+ $(,)?> $idx:expr $(,)?) => {{ 179 struct ConstParam<$(const $imm: $ty),+>; 180 impl<$(const $imm: $ty),+> ConstParam<$($imm),+> { 181 const IDX: [u32; 64] = $idx; 182 } 183 184 simd_shuffle64($x, $y, ConstParam::<$($imm),+>::IDX) 185 }}; 186 ($x:expr, $y:expr, $idx:expr $(,)?) => {{ 187 const IDX: [u32; 64] = $idx; 188 simd_shuffle64($x, $y, IDX) 189 }}; 190 } 191