From f2fec0b3b964ac6e36d72a3765953c77310a2541 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 6 Sep 2024 11:25:46 -0700 Subject: [PATCH] implement a bunch of fixed wide stuff --- fixed_wide/src/fixed.rs | 9 ++ fixed_wide_vectors/src/macros/fixed_wide.rs | 142 +++++++++++++++++++- fixed_wide_vectors/src/macros/matrix.rs | 13 +- fixed_wide_vectors/src/macros/vector.rs | 13 +- fixed_wide_vectors/src/matrix.rs | 2 +- fixed_wide_vectors/src/named.rs | 6 +- fixed_wide_vectors/src/vector.rs | 2 +- 7 files changed, 176 insertions(+), 11 deletions(-) diff --git a/fixed_wide/src/fixed.rs b/fixed_wide/src/fixed.rs index d0aad2d..ee7662e 100644 --- a/fixed_wide/src/fixed.rs +++ b/fixed_wide/src/fixed.rs @@ -201,6 +201,15 @@ macro_rules! impl_multiplicatave_assign_operator { } }; } +impl std::iter::Sum for Fixed{ + fn sum>(iter:I)->Self{ + let mut sum=Self::ZERO; + for elem in iter{ + sum+=elem; + } + sum + } +} macro_rules! impl_operator_16 { ( $macro: ident, $struct: ident, $trait: ident, $method: ident, $output: ty ) => { diff --git a/fixed_wide_vectors/src/macros/fixed_wide.rs b/fixed_wide_vectors/src/macros/fixed_wide.rs index dabb773..76458d6 100644 --- a/fixed_wide_vectors/src/macros/fixed_wide.rs +++ b/fixed_wide_vectors/src/macros/fixed_wide.rs @@ -2,13 +2,14 @@ #[macro_export(local_inner_macros)] macro_rules! impl_wide_vector_operations_2arg_not_const_generic { ( + (), ($lhs:expr, $rhs:expr) ) => { impl Vector>{ paste::item!{ #[inline] pub fn [](self,rhs:Vector>)->Vector>{ - self.array.map_zip(|(a,b)|a.[](b)) + self.map_zip(rhs,|(a,b)|a.[](b)) } #[inline] pub fn [](self,rhs:Vector>)->fixed_wide::fixed::Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>{ @@ -22,9 +23,10 @@ macro_rules! impl_wide_vector_operations_2arg_not_const_generic { #[macro_export(local_inner_macros)] macro_rules! impl_wide_vector_operations_1arg_not_const_generic { ( + (), $n:expr ) => { - impl $struct>{ + impl Vector>{ paste::item!{ #[inline] pub fn wide_length_squared(&self)->fixed_wide::fixed::Fixed<{$n*2},{$n*2*32}>{ @@ -34,3 +36,139 @@ macro_rules! impl_wide_vector_operations_1arg_not_const_generic { } }; } + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! do_macro_8x8{ + ( + $macro:ident, + $any:tt + )=>{ + $crate::macro_repeated!($macro, $any, + (1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),(8,1), + (1,2),(2,2),(3,2),(4,2),(5,2),(6,2),(7,2),(8,2), + (1,3),(2,3),(3,3),(4,3),(5,3),(6,3),(7,3),(8,3), + (1,4),(2,4),(3,4),(4,4),(5,4),(6,4),(7,4),(8,4), + (1,5),(2,5),(3,5),(4,5),(5,5),(6,5),(7,5),(8,5), + (1,6),(2,6),(3,6),(4,6),(5,6),(6,6),(7,6),(8,6), + (1,7),(2,7),(3,7),(4,7),(5,7),(6,7),(7,7),(8,7), + (1,8),(2,8),(3,8),(4,8),(5,8),(6,8),(7,8),(8,8) + ); + }; +} + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! do_macro_8{ + ( + $macro:ident, + $any:tt + )=>{ + $crate::macro_repeated!($macro, $any, 1,2,3,4,5,6,7,8); + }; +} + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_wide_vector_operations { + () => { + $crate::do_macro_8!(impl_wide_vector_operations_1arg_not_const_generic,()); + $crate::do_macro_8x8!(impl_wide_vector_operations_2arg_not_const_generic,()); + }; +} + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_vector_3_wide_cross { + ( + (), + ($lhs:expr, $rhs:expr) + )=>{ + impl Vector<3,fixed_wide::fixed::Fixed<{$lhs},{$lhs*32}>>{ + paste::item!{ + #[inline] + pub fn [](self,rhs:Vector<3,fixed_wide::fixed::Fixed<{$rhs},{$rhs*32}>>)->Vector<3,fixed_wide::fixed::Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>>{ + Vector{array:[ + self.y.[](rhs.z)-self.z.[](rhs.y), + self.z.[](rhs.x)-self.x.[](rhs.z), + self.x.[](rhs.y)-self.y.[](rhs.x), + ]} + } + } + } + } +} + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_vector_wide_3 { + ()=>{ + $crate::do_macro_8x8!(impl_vector_3_wide_cross,()); + } +} + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! do_macro_4_dumb{ + ( + $macro:ident, + $any:tt + )=>{ + $crate::macro_repeated!($macro, $any, (1,2),(2,4),(3,6),(4,8)); + }; +} + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_wide_3x3_det_not_const_generic { + ( + $n: expr, + $_2n: expr + )=>{ + impl Matrix<3,3,fixed_wide::fixed::Fixed<$n,{$n*32}>>{ + paste::item!{ + pub fn [](self)->fixed_wide::fixed::Fixed<{$n*3},{$n*3*32}>{ + //[] will not compile, so the doubles are hardcoded above + self.x_axis.[](self.y_axis.[](self.z_axis)) + } + } + } + } +} +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_wide_3x3_det_not_const_generic_shim { + ( + (),($n: expr,$_2n: expr) + )=>{ + $crate::impl_matrix_wide_3x3_det_not_const_generic!($n,$_2n); + } +} +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_wide_3x3_adjugate_not_const_generic { + ( + (), + $n: expr + )=>{ + impl Matrix<3,3,fixed_wide::fixed::Fixed<$n,{$n*32}>>{ + paste::item!{ + pub fn [](self)->Matrix<3,3,fixed_wide::fixed::Fixed<{$n*2},{$n*2*32}>>{ + Matrix{array:[ + [self.y_axis.y.[](self.z_axis.z)-self.y_axis.z.[](self.z_axis.y),self.x_axis.z.[](self.z_axis.y)-self.x_axis.y.[](self.z_axis.z),self.x_axis.y.[](self.y_axis.z)-self.x_axis.z.[](self.y_axis.y)], + [self.y_axis.z.[](self.z_axis.x)-self.y_axis.x.[](self.z_axis.z),self.x_axis.x.[](self.z_axis.z)-self.x_axis.z.[](self.z_axis.x),self.x_axis.z.[](self.y_axis.x)-self.x_axis.x.[](self.y_axis.z)], + [self.y_axis.x.[](self.z_axis.y)-self.y_axis.y.[](self.z_axis.x),self.x_axis.y.[](self.z_axis.x)-self.x_axis.x.[](self.z_axis.y),self.x_axis.x.[](self.y_axis.y)-self.x_axis.y.[](self.y_axis.x)], + ]} + } + } + } + } +} +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_wide_3x3 { + ()=>{ + $crate::do_macro_4_dumb!(impl_matrix_wide_3x3_det_not_const_generic_shim,()); + $crate::do_macro_8!(impl_matrix_wide_3x3_adjugate_not_const_generic,()); + } +} diff --git a/fixed_wide_vectors/src/macros/matrix.rs b/fixed_wide_vectors/src/macros/matrix.rs index e564df4..14013ac 100644 --- a/fixed_wide_vectors/src/macros/matrix.rs +++ b/fixed_wide_vectors/src/macros/matrix.rs @@ -81,10 +81,10 @@ macro_rules! impl_matrix { macro_rules! impl_matrix_named_fields_shape { ( ($struct_outer:ident, $size_outer: expr), - ($struct_inner:ident, $size_inner: expr) + ($size_inner: expr) ) => { impl core::ops::Deref for Matrix<$size_outer,$size_inner,T>{ - type Target=$struct_outer<$struct_inner>; + type Target=$struct_outer>; fn deref(&self)->&Self::Target{ unsafe{core::mem::transmute(&self.array)} } @@ -118,3 +118,12 @@ macro_rules! impl_matrix_named_fields { $crate::macro_repeated!(impl_matrix_named_fields_shape_shim,$vector_infos,$($matrix_info),+); } } + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_3x3 { + ()=>{ + #[cfg(feature="fixed_wide")] + $crate::impl_matrix_wide_3x3!(); + } +} diff --git a/fixed_wide_vectors/src/macros/vector.rs b/fixed_wide_vectors/src/macros/vector.rs index 6b24583..8d26a27 100644 --- a/fixed_wide_vectors/src/macros/vector.rs +++ b/fixed_wide_vectors/src/macros/vector.rs @@ -120,8 +120,8 @@ macro_rules! impl_vector { $crate::impl_vector_operator!(BitXor, bitxor ); // Impl floating-point based methods - //#[cfg(feature="fixed_wide")] - //$crate::impl_wide_vector_operations!( $struct { $($field), + }, $size ); + #[cfg(feature="fixed_wide")] + $crate::impl_wide_vector_operations!(); } } #[doc(hidden)] @@ -178,3 +178,12 @@ macro_rules! impl_vector_named_fields { } } } + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_vector_3 { + ()=>{ + #[cfg(feature="fixed_wide")] + $crate::impl_vector_wide_3!(); + } +} diff --git a/fixed_wide_vectors/src/matrix.rs b/fixed_wide_vectors/src/matrix.rs index 784433c..02070ec 100644 --- a/fixed_wide_vectors/src/matrix.rs +++ b/fixed_wide_vectors/src/matrix.rs @@ -6,4 +6,4 @@ pub struct Matrix{ crate::impl_matrix!(); //Special case 3x3 matrix operations because I cba to write macros for the arbitrary cases -//crate::impl_matrix_3x3!(); +crate::impl_matrix_3x3!(); diff --git a/fixed_wide_vectors/src/named.rs b/fixed_wide_vectors/src/named.rs index f490ee9..b467357 100644 --- a/fixed_wide_vectors/src/named.rs +++ b/fixed_wide_vectors/src/named.rs @@ -52,8 +52,8 @@ crate::impl_matrix_named_fields!( ), //inner struct ( - (Vector2, 2), - (Vector3, 3), - (Vector4, 4) + (2), + (3), + (4) ) ); diff --git a/fixed_wide_vectors/src/vector.rs b/fixed_wide_vectors/src/vector.rs index ffea1f0..212f126 100644 --- a/fixed_wide_vectors/src/vector.rs +++ b/fixed_wide_vectors/src/vector.rs @@ -6,4 +6,4 @@ pub struct Vector{ crate::impl_vector!(); //cross product -//crate::impl_vector_3!(); +crate::impl_vector_3!();