In SIMD, there are functions like i8x16_abs, u16x8_abs and f32x4_abs .etc.
In order to reduce redundancy, I decided to use macro to call the corresponding function name by specifying data type. Like:
simd_abs!(i8) // Call `i8x16_abs`
simd_abs!(u16) // Call `u16x8_abs`
I found some crates like paste. However, I don't kown how to pass the compile time constant's value in macro.
macro_rules! simd_abs_inner {
($typ: ty, $chunks: expr) => {
paste! { [< $typ x $chunks _abs> ] }
};
}
macro_rules! simd_abs {
($typ: ty) => {
// The `chunks` variable should be a compile time constant
const chunks: usize = 16 / mem::size_of::<$typ>();
simd_abs_inner!($typ, chunks)(/*...*/)
};
}
When $typ is i8, the real called function is i8xchunks_abs. The problem is that chunks varibale is not a literal so paste just replace it by its variable's name. But the compiler do know the chunks's value in the compile time. How to use compile time constants to generate a function name in Rust?