strafe-project/src/macros/wide.rs

44 lines
1.4 KiB
Rust
Raw Normal View History

2024-08-23 22:57:12 +00:00
#[doc(hidden)]
#[macro_export(local_inner_macros)]
2024-08-23 23:26:19 +00:00
macro_rules! impl_wide_operations {
2024-08-23 22:57:12 +00:00
( $struct: ident { $($field: ident), + }, $size: expr ) => {
2024-08-23 23:26:19 +00:00
impl<U,T:Copy+fixed_wide::wide::WideMul<Output=U>> fixed_wide::wide::WideMul for $struct<T> {
type Output=$struct<U>;
2024-08-23 22:57:12 +00:00
#[inline]
2024-08-23 23:26:19 +00:00
fn wide_mul(self, rhs: Self) -> Self::Output {
$struct{
$( $field: self.$field.wide_mul(rhs.$field) ), +
}
}
}
impl<T:Copy+fixed_wide::wide::WideMul<Output=T>+std::ops::Add<Output=T>> $struct<T> {
#[inline]
pub fn wide_dot(&self, other: &Self) -> <T as fixed_wide::wide::WideMul>::Output {
2024-08-23 22:57:12 +00:00
$crate::sum_repeating!(
2024-08-23 23:26:19 +00:00
$( + (self.$field.wide_mul(other.$field)) ) +
2024-08-23 22:57:12 +00:00
)
}
2024-08-23 23:26:19 +00:00
pub fn length_squared(&self) -> <T as fixed_wide::wide::WideMul>::Output {
2024-08-23 22:57:12 +00:00
let squared = Self {
2024-08-23 23:26:19 +00:00
$( $field: self.$field.wide_mul(self.$field) ), +
2024-08-23 22:57:12 +00:00
};
$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) *
};
}