matrix: directly implement dot product to avoid a copy

This commit is contained in:
Quaternions 2024-09-04 12:03:12 -07:00
parent 6aba246453
commit c51dc6098c

View File

@ -87,16 +87,14 @@ macro_rules! impl_wide_vector_operations {
#[macro_export(local_inner_macros)] #[macro_export(local_inner_macros)]
macro_rules! impl_matrix_wide_mul_transpose_helper { macro_rules! impl_matrix_wide_mul_transpose_helper {
( (
$value:ident, $lhs_axis:expr, $wide_mul:ident, $rhs:ident,
($struct: ident { $($field: ident), + }), ($struct: ident { $($field: ident), + }),
($from_struct: ident { $($from_field: ident), + }), ($from_struct: ident { $($from_field: ident), + }),
$static_field: ident $static_field: ident
) => { ) => {
$struct { $crate::sum_repeating!(
$( $( + $lhs_axis.$field.$wide_mul($rhs.$from_field.$static_field) ) +
$field: $value.$from_field.$static_field )
), +
}
} }
} }
#[doc(hidden)] #[doc(hidden)]
@ -104,7 +102,7 @@ macro_rules! impl_matrix_wide_mul_transpose_helper {
macro_rules! impl_matrix_wide_mul_inner { macro_rules! impl_matrix_wide_mul_inner {
( (
// MatY<VecX>.MatX<VecZ> = MatY<VecZ> // MatY<VecX>.MatX<VecZ> = MatY<VecZ>
$lhs:ident, $lhs_field_outer:ident, $wide_dot:ident, $rhs:ident, $lhs:ident, $lhs_field_outer:ident, $wide_mul:ident, $rhs:ident,
$struct_inner_thru: tt, //VecX $struct_inner_thru: tt, //VecX
($struct_inner: ident { $($field_inner: ident), + }), //VecX ($struct_inner: ident { $($field_inner: ident), + }), //VecX
($rhs_struct_inner: ident { $($rhs_field_inner: ident), + }), //VecZ ($rhs_struct_inner: ident { $($rhs_field_inner: ident), + }), //VecZ
@ -112,15 +110,14 @@ macro_rules! impl_matrix_wide_mul_inner {
) => { ) => {
$rhs_struct_inner { $rhs_struct_inner {
$( $(
$rhs_field_inner: $lhs.$lhs_field_outer.$wide_dot( //directly dot product to avoid a copy
//construct a transposed vector with the same width as $struct_outer $rhs_field_inner: $crate::impl_matrix_wide_mul_transpose_helper!{
$crate::impl_matrix_wide_mul_transpose_helper!{ //lhs_axis
$rhs, $lhs.$lhs_field_outer,$wide_mul,$rhs,
$struct_inner_thru, //VecZ $struct_inner_thru, //VecZ
$rhs_outer, //MatX $rhs_outer, //MatX
$rhs_field_inner $rhs_field_inner
} }
)
), + ), +
} }
} }
@ -130,7 +127,7 @@ macro_rules! impl_matrix_wide_mul_inner {
macro_rules! impl_matrix_wide_mul_outer { macro_rules! impl_matrix_wide_mul_outer {
( (
// MatY<VecX>.MatX<VecZ> = MatY<VecZ> // MatY<VecX>.MatX<VecZ> = MatY<VecZ>
$lhs:ident, $wide_dot:ident, $rhs:ident, $lhs:ident, $wide_mul:ident, $rhs:ident,
//result matrix shape //result matrix shape
($struct_outer: ident { $($field_outer: ident), + }),//MatY ($struct_outer: ident { $($field_outer: ident), + }),//MatY
$rhs_struct_inner: tt,//VecZ $rhs_struct_inner: tt,//VecZ
@ -142,7 +139,7 @@ macro_rules! impl_matrix_wide_mul_outer {
$struct_outer { $struct_outer {
$( $(
$field_outer: $crate::impl_matrix_wide_mul_inner!{ $field_outer: $crate::impl_matrix_wide_mul_inner!{
$lhs, $field_outer, $wide_dot, $rhs, $lhs, $field_outer, $wide_mul, $rhs,
$struct_inner, //VecX $struct_inner, //VecX
$struct_inner, //VecX $struct_inner, //VecX
$rhs_struct_inner, //VecZ $rhs_struct_inner, //VecZ
@ -182,7 +179,7 @@ macro_rules! impl_matrix_wide_mul {
pub fn [<wide_dot_ $size_outer x $size_inner _ $size_inner x $rhs_size_inner _ $lhs _ $rhs>](self,rhs:$matrix_inner<$rhs_struct_inner<fixed_wide::fixed::Fixed<{$rhs},{$rhs*32}>>>)->$struct_outer<$rhs_struct_inner<fixed_wide::fixed::Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>>>{ pub fn [<wide_dot_ $size_outer x $size_inner _ $size_inner x $rhs_size_inner _ $lhs _ $rhs>](self,rhs:$matrix_inner<$rhs_struct_inner<fixed_wide::fixed::Fixed<{$rhs},{$rhs*32}>>>)->$struct_outer<$rhs_struct_inner<fixed_wide::fixed::Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>>>{
$crate::impl_matrix_wide_mul_outer!( $crate::impl_matrix_wide_mul_outer!(
//constituent idents //constituent idents
self,[<wide_dot_ $lhs _ $rhs>],rhs, self,[<wide_mul_ $lhs _ $rhs>],rhs,
//result matrix shape //result matrix shape
($struct_outer { $($field_outer), + }), ($struct_outer { $($field_outer), + }),
($rhs_struct_inner { $($rhs_field_inner), + }), ($rhs_struct_inner { $($rhs_field_inner), + }),