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

44 lines
1.2 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-28 17:47:30 +00:00
( $struct: ident { $($field: ident), + }, $size: expr ) => {
impl<U,T:Copy+fixed_wide_traits::wide::WideMul<Output=U>> fixed_wide_traits::wide::WideMul for $struct<T> {
2024-08-23 23:26:19 +00:00
type Output=$struct<U>;
2024-08-28 17:47:30 +00:00
#[inline]
fn wide_mul(self, rhs: Self) -> Self::Output {
$struct{
$( $field: self.$field.wide_mul(rhs.$field) ), +
}
}
2024-08-23 23:26:19 +00:00
}
2024-08-28 17:04:58 +00:00
impl<V:core::ops::Add<Output=V>,U,T:fixed_wide_traits::wide::WideMul<U,Output=V>> fixed_wide_traits::wide::WideDot<$struct<U>> for $struct<T> {
type Output=V;
#[inline]
fn wide_dot(self, rhs: $struct<U>) -> Self::Output {
$crate::sum_repeating!(
2024-08-28 17:47:30 +00:00
$( + (self.$field.wide_mul(rhs.$field)) ) +
)
}
}
impl<U:std::ops::Add<Output=U>,T:Copy+fixed_wide_traits::wide::WideMul<Output=U>> $struct<T> {
#[inline]
pub fn wide_length_squared(&self) -> U {
$crate::sum_repeating!(
$( + self.$field.wide_mul(self.$field) ) +
)
2024-08-28 17:04:58 +00:00
}
}
2024-08-28 17:47:30 +00:00
};
2024-08-23 22:57:12 +00:00
}
// 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 {
2024-08-28 17:47:30 +00:00
( + $($item: tt) * ) => {
$($item) *
};
2024-08-23 22:57:12 +00:00
}