strafe-project/fixed_wide_vectors/src/macros/wide.rs

44 lines
1.3 KiB
Rust

#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_wide_operations {
( $struct: ident { $($field: ident), + }, $size: expr ) => {
impl<U,T:Copy+fixed_wide::wide::WideMul<Output=U>> fixed_wide::wide::WideMul for $struct<T> {
type Output=$struct<U>;
#[inline]
fn wide_mul(self, rhs: Self) -> Self::Output {
$struct{
$( $field: self.$field.wide_mul(rhs.$field) ), +
}
}
}
impl<U:std::ops::Add<Output=U>,T:Copy+fixed_wide::wide::WideMul<Output=U>> $struct<T> {
#[inline]
pub fn wide_dot(self, other: Self) -> U {
$crate::sum_repeating!(
$( + (self.$field.wide_mul(other.$field)) ) +
)
}
pub fn wide_length_squared(&self) -> U {
let squared = $struct {
$( $field: self.$field.wide_mul(self.$field) ), +
};
$crate::sum_repeating!(
$( + squared.$field ) +
)
}
}
};
}
// HACK: Allows us to sum repeating tokens in macros.
// See: https://stackoverflow.com/a/60187870/17452730
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! sum_repeating {
( + $($item: tt) * ) => {
$($item) *
};
}