implement a bunch of fixed wide stuff

This commit is contained in:
Quaternions 2024-09-06 11:25:46 -07:00
parent dae72d73d5
commit f2fec0b3b9
7 changed files with 176 additions and 11 deletions

View File

@ -201,6 +201,15 @@ macro_rules! impl_multiplicatave_assign_operator {
} }
}; };
} }
impl<const N:usize,const F:usize> std::iter::Sum for Fixed<N,F>{
fn sum<I:Iterator<Item=Self>>(iter:I)->Self{
let mut sum=Self::ZERO;
for elem in iter{
sum+=elem;
}
sum
}
}
macro_rules! impl_operator_16 { macro_rules! impl_operator_16 {
( $macro: ident, $struct: ident, $trait: ident, $method: ident, $output: ty ) => { ( $macro: ident, $struct: ident, $trait: ident, $method: ident, $output: ty ) => {

View File

@ -2,13 +2,14 @@
#[macro_export(local_inner_macros)] #[macro_export(local_inner_macros)]
macro_rules! impl_wide_vector_operations_2arg_not_const_generic { macro_rules! impl_wide_vector_operations_2arg_not_const_generic {
( (
(),
($lhs:expr, $rhs:expr) ($lhs:expr, $rhs:expr)
) => { ) => {
impl<const N:usize> Vector<N,fixed_wide::fixed::Fixed<{$lhs},{$lhs*32}>>{ impl<const N:usize> Vector<N,fixed_wide::fixed::Fixed<{$lhs},{$lhs*32}>>{
paste::item!{ paste::item!{
#[inline] #[inline]
pub fn [<wide_mul_ $lhs _ $rhs>](self,rhs:Vector<N,fixed_wide::fixed::Fixed<{$rhs},{$rhs*32}>>)->Vector<N,fixed_wide::fixed::Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>>{ pub fn [<wide_mul_ $lhs _ $rhs>](self,rhs:Vector<N,fixed_wide::fixed::Fixed<{$rhs},{$rhs*32}>>)->Vector<N,fixed_wide::fixed::Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>>{
self.array.map_zip(|(a,b)|a.[<wide_mul_ $lhs _ $rhs>](b)) self.map_zip(rhs,|(a,b)|a.[<wide_mul_ $lhs _ $rhs>](b))
} }
#[inline] #[inline]
pub fn [<wide_dot_ $lhs _ $rhs>](self,rhs:Vector<N,fixed_wide::fixed::Fixed<{$rhs},{$rhs*32}>>)->fixed_wide::fixed::Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>{ pub fn [<wide_dot_ $lhs _ $rhs>](self,rhs:Vector<N,fixed_wide::fixed::Fixed<{$rhs},{$rhs*32}>>)->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_export(local_inner_macros)]
macro_rules! impl_wide_vector_operations_1arg_not_const_generic { macro_rules! impl_wide_vector_operations_1arg_not_const_generic {
( (
(),
$n:expr $n:expr
) => { ) => {
impl $struct<fixed_wide::fixed::Fixed<{$n},{$n*32}>>{ impl<const N:usize> Vector<N,fixed_wide::fixed::Fixed<{$n},{$n*32}>>{
paste::item!{ paste::item!{
#[inline] #[inline]
pub fn wide_length_squared(&self)->fixed_wide::fixed::Fixed<{$n*2},{$n*2*32}>{ 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 [<wide_cross_ $lhs _ $rhs>](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.[<wide_mul_ $lhs _ $rhs>](rhs.z)-self.z.[<wide_mul_ $lhs _ $rhs>](rhs.y),
self.z.[<wide_mul_ $lhs _ $rhs>](rhs.x)-self.x.[<wide_mul_ $lhs _ $rhs>](rhs.z),
self.x.[<wide_mul_ $lhs _ $rhs>](rhs.y)-self.y.[<wide_mul_ $lhs _ $rhs>](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 [<wide_det_3x3_ $n>](self)->fixed_wide::fixed::Fixed<{$n*3},{$n*3*32}>{
//[<wide_dot_ $n _ $n*2>] will not compile, so the doubles are hardcoded above
self.x_axis.[<wide_dot_ $n _ $_2n>](self.y_axis.[<wide_cross_ $n _ $n>](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 [<wide_adjugate_3x3_ $n>](self)->Matrix<3,3,fixed_wide::fixed::Fixed<{$n*2},{$n*2*32}>>{
Matrix{array:[
[self.y_axis.y.[<wide_mul_ $n _ $n>](self.z_axis.z)-self.y_axis.z.[<wide_mul_ $n _ $n>](self.z_axis.y),self.x_axis.z.[<wide_mul_ $n _ $n>](self.z_axis.y)-self.x_axis.y.[<wide_mul_ $n _ $n>](self.z_axis.z),self.x_axis.y.[<wide_mul_ $n _ $n>](self.y_axis.z)-self.x_axis.z.[<wide_mul_ $n _ $n>](self.y_axis.y)],
[self.y_axis.z.[<wide_mul_ $n _ $n>](self.z_axis.x)-self.y_axis.x.[<wide_mul_ $n _ $n>](self.z_axis.z),self.x_axis.x.[<wide_mul_ $n _ $n>](self.z_axis.z)-self.x_axis.z.[<wide_mul_ $n _ $n>](self.z_axis.x),self.x_axis.z.[<wide_mul_ $n _ $n>](self.y_axis.x)-self.x_axis.x.[<wide_mul_ $n _ $n>](self.y_axis.z)],
[self.y_axis.x.[<wide_mul_ $n _ $n>](self.z_axis.y)-self.y_axis.y.[<wide_mul_ $n _ $n>](self.z_axis.x),self.x_axis.y.[<wide_mul_ $n _ $n>](self.z_axis.x)-self.x_axis.x.[<wide_mul_ $n _ $n>](self.z_axis.y),self.x_axis.x.[<wide_mul_ $n _ $n>](self.y_axis.y)-self.x_axis.y.[<wide_mul_ $n _ $n>](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,());
}
}

View File

@ -81,10 +81,10 @@ macro_rules! impl_matrix {
macro_rules! impl_matrix_named_fields_shape { macro_rules! impl_matrix_named_fields_shape {
( (
($struct_outer:ident, $size_outer: expr), ($struct_outer:ident, $size_outer: expr),
($struct_inner:ident, $size_inner: expr) ($size_inner: expr)
) => { ) => {
impl<T> core::ops::Deref for Matrix<$size_outer,$size_inner,T>{ impl<T> core::ops::Deref for Matrix<$size_outer,$size_inner,T>{
type Target=$struct_outer<$struct_inner<T>>; type Target=$struct_outer<Vector<$size_inner,T>>;
fn deref(&self)->&Self::Target{ fn deref(&self)->&Self::Target{
unsafe{core::mem::transmute(&self.array)} 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),+); $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!();
}
}

View File

@ -120,8 +120,8 @@ macro_rules! impl_vector {
$crate::impl_vector_operator!(BitXor, bitxor ); $crate::impl_vector_operator!(BitXor, bitxor );
// Impl floating-point based methods // Impl floating-point based methods
//#[cfg(feature="fixed_wide")] #[cfg(feature="fixed_wide")]
//$crate::impl_wide_vector_operations!( $struct { $($field), + }, $size ); $crate::impl_wide_vector_operations!();
} }
} }
#[doc(hidden)] #[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!();
}
}

View File

@ -6,4 +6,4 @@ pub struct Matrix<const X:usize,const Y:usize,T>{
crate::impl_matrix!(); crate::impl_matrix!();
//Special case 3x3 matrix operations because I cba to write macros for the arbitrary cases //Special case 3x3 matrix operations because I cba to write macros for the arbitrary cases
//crate::impl_matrix_3x3!(); crate::impl_matrix_3x3!();

View File

@ -52,8 +52,8 @@ crate::impl_matrix_named_fields!(
), ),
//inner struct //inner struct
( (
(Vector2, 2), (2),
(Vector3, 3), (3),
(Vector4, 4) (4)
) )
); );

View File

@ -6,4 +6,4 @@ pub struct Vector<const N:usize,T>{
crate::impl_vector!(); crate::impl_vector!();
//cross product //cross product
//crate::impl_vector_3!(); crate::impl_vector_3!();