From e5f95b97ce0f5a9a5db2710bb33af44e10bf21d9 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 3 Sep 2024 12:01:15 -0700 Subject: [PATCH] matrix: macro mat mul --- fixed_wide_vectors/src/macros/fixed_wide.rs | 111 +++++++++++++++++--- fixed_wide_vectors/src/macros/matrix.rs | 2 +- 2 files changed, 96 insertions(+), 17 deletions(-) diff --git a/fixed_wide_vectors/src/macros/fixed_wide.rs b/fixed_wide_vectors/src/macros/fixed_wide.rs index b8fbbae..824dc54 100644 --- a/fixed_wide_vectors/src/macros/fixed_wide.rs +++ b/fixed_wide_vectors/src/macros/fixed_wide.rs @@ -83,17 +83,94 @@ macro_rules! impl_wide_vector_operations { }; } +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_wide_dot_transpose_helper { + ( + $value:ident, + ($struct: ident { $($field: ident), + }), + ($from_struct: ident { $($from_field: ident), + }), + $static_field: ident + ) => { + $struct { + $( + $field: $value.$from_field.$static_field + ), + + } + } +} +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_wide_dot_inner { + ( + // MatY.MatX = MatY + $lhs:ident, $lhs_field_outer:ident, $wide_dot:ident, $rhs:ident, + $struct_inner_thru: tt, //VecX + ($struct_inner: ident { $($field_inner: ident), + }), //VecX + ($rhs_struct_inner: ident { $($rhs_field_inner: ident), + }), //VecZ + $rhs_outer: tt //MatX + ) => { + $rhs_struct_inner { + $( + $rhs_field_inner: $lhs.$lhs_field_outer.$wide_dot( + //construct a transposed vector with the same width as $struct_outer + $crate::impl_matrix_wide_dot_transpose_helper!{ + $rhs, + $struct_inner_thru, //VecZ + $rhs_outer, //MatX + $rhs_field_inner + } + ) + ), + + } + } +} +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_wide_dot_outer { + ( + // MatY.MatX = MatY + $lhs:ident, $wide_dot:ident, $rhs:ident, + //result matrix shape + ($struct_outer: ident { $($field_outer: ident), + }),//MatY + $rhs_struct_inner: tt,//VecZ + //inner loop shape + $struct_inner: tt,//VecX + $rhs_matrix: tt//MatX + + ) => { + $struct_outer { + $( + $field_outer: $crate::impl_matrix_wide_dot_inner!{ + $lhs, $field_outer, $wide_dot, $rhs, + $struct_inner, //VecX + $struct_inner, //VecX + $rhs_struct_inner, //VecZ + $rhs_matrix //MatX + } + ), + + } + } +} // Notes: // Mat3.dot(Vec2) -> Vec3 +// lhs.dot(rhs) -> out +// lhs = Mat3 +// rhs = Mat4 +// out = Mat3 // Mat3.dot(Mat4) -> Mat3 -// mat.mat can be implemented off the back of mat.vec +// how to matrix multiply: +// RHS TRANSPOSE +// Mat4 -> Mat2 +// rhs_t = Mat2 +// inner loop: +// out[y][x] = lhs[y].dot(rhs_t[x]) #[doc(hidden)] #[macro_export(local_inner_macros)] -macro_rules! impl_matrix_wide_mul { +macro_rules! impl_matrix_wide_dot { ( - //TODO: Fixed point impls ($struct_outer: ident { $($field_outer: ident), + }, $vector_outer: ident { $($vector_field_outer: ident), + }, $size_outer: expr), ($struct_inner: ident { $($field_inner: ident), + }, $matrix_inner: ident { $($matrix_field_inner: ident), + }, $size_inner: expr), ($rhs_struct_inner: ident { $($rhs_field_inner: ident), + }, $rhs_matrix_inner: ident { $($rhs_matrix_field_inner: ident), + }, $rhs_size_inner: expr), @@ -103,13 +180,15 @@ macro_rules! impl_matrix_wide_mul { paste::item!{ #[inline] pub fn [](self,rhs:$matrix_inner<$rhs_struct_inner>>)->$struct_outer<$rhs_struct_inner>>{ - //just made this up, don't trust it - let tr=rhs.transpose(); - //TODO: use a macro expansion instead of transpose and map - self.map(|axis| - tr.map(|trax| - axis.[](trax) - ).to_vector() + $crate::impl_matrix_wide_dot_outer!( + //constituent idents + self,[],rhs, + //result matrix shape + ($struct_outer { $($field_outer), + }), + ($rhs_struct_inner { $($rhs_field_inner), + }), + //inner loop shape + ($struct_inner { $($field_inner), + }), + ($matrix_inner { $($matrix_field_inner), + }) ) } } @@ -119,32 +198,32 @@ macro_rules! impl_matrix_wide_mul { #[doc(hidden)] #[macro_export(local_inner_macros)] -macro_rules! impl_matrix_wide_mul_shim { +macro_rules! impl_matrix_wide_dot_shim { ( ($outer_info:tt,$inner_info:tt,$rhs_info:tt), ($lhs: expr, $rhs: expr) ) => { - $crate::impl_matrix_wide_mul!($outer_info,$inner_info,$rhs_info,($lhs,$rhs)); + $crate::impl_matrix_wide_dot!($outer_info,$inner_info,$rhs_info,($lhs,$rhs)); } } #[doc(hidden)] #[macro_export(local_inner_macros)] -macro_rules! impl_matrix_wide_mul_8x8 { +macro_rules! impl_matrix_wide_dot_8x8 { ( ($outer_info:tt,$inner_info:tt), $rhs_info:tt ) => { - $crate::do_macro_8x8!(impl_matrix_wide_mul_shim,($outer_info,$inner_info,$rhs_info)); + $crate::do_macro_8x8!(impl_matrix_wide_dot_shim,($outer_info,$inner_info,$rhs_info)); } } #[doc(hidden)] #[macro_export(local_inner_macros)] -macro_rules! impl_matrix_wide_mul_repeat_rhs { +macro_rules! impl_matrix_wide_dot_repeat_rhs { ( ($outer_info:tt,($($rhs_info:tt),+)), $inner_info:tt ) => { - $crate::macro_repeated!(impl_matrix_wide_mul_8x8,($outer_info,$inner_info),$($rhs_info),+); + $crate::macro_repeated!(impl_matrix_wide_dot_8x8,($outer_info,$inner_info),$($rhs_info),+); } } #[doc(hidden)] diff --git a/fixed_wide_vectors/src/macros/matrix.rs b/fixed_wide_vectors/src/macros/matrix.rs index e457ced..eeed579 100644 --- a/fixed_wide_vectors/src/macros/matrix.rs +++ b/fixed_wide_vectors/src/macros/matrix.rs @@ -48,7 +48,7 @@ macro_rules! impl_matrix_inner_shim { ) => { $crate::macro_repeated!(impl_matrix_inner,$matrix_info,$($vector_info),+); #[cfg(feature="fixed_wide")] - $crate::macro_repeated!(impl_matrix_wide_mul_repeat_rhs,($matrix_info,($($vector_info),+)),$($vector_info),+); + $crate::macro_repeated!(impl_matrix_wide_dot_repeat_rhs,($matrix_info,($($vector_info),+)),$($vector_info),+); } } #[doc(hidden)]