extract trait impls into named functions + fix spelling

This commit is contained in:
Quaternions 2024-09-09 13:58:24 -07:00
parent 62419e94e1
commit 803f1bea9e

View File

@ -82,11 +82,16 @@ impl<const N:usize,const F:usize> std::iter::Sum for Fixed<N,F>{
macro_rules! impl_additive_operator { macro_rules! impl_additive_operator {
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => { ( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
impl<const N:usize,const F:usize> $struct<N,F>{
#[inline]
pub const fn $method(self, other: Self) -> Self {
Self::from_bits(self.bits.$method(other.bits))
}
}
impl<const N:usize,const F:usize> core::ops::$trait for $struct<N,F>{ impl<const N:usize,const F:usize> core::ops::$trait for $struct<N,F>{
type Output = $output; type Output = $output;
fn $method(self, other: Self) -> Self::Output { fn $method(self, other: Self) -> Self::Output {
Self::from_bits(self.bits.$method(other.bits)) self.$method(other)
} }
} }
impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F> impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F>
@ -94,7 +99,6 @@ macro_rules! impl_additive_operator {
BInt::<N>:From<U>, BInt::<N>:From<U>,
{ {
type Output = $output; type Output = $output;
fn $method(self, other: U) -> Self::Output { fn $method(self, other: U) -> Self::Output {
Self::from_bits(self.bits.$method(BInt::<N>::from(other).shl(F as u32))) Self::from_bits(self.bits.$method(BInt::<N>::from(other).shl(F as u32)))
} }
@ -135,56 +139,63 @@ impl_additive_operator!( Fixed, BitOr, bitor, Self );
impl_additive_assign_operator!( Fixed, BitXorAssign, bitxor_assign ); impl_additive_assign_operator!( Fixed, BitXorAssign, bitxor_assign );
impl_additive_operator!( Fixed, BitXor, bitxor, Self ); impl_additive_operator!( Fixed, BitXor, bitxor, Self );
macro_rules! impl_multiply_operator_not_const_generic { macro_rules! impl_multiplicative_operator_not_const_generic {
( ($struct: ident, $trait: ident, $method: ident, $output: ty ), $width:expr ) => { ( ($struct: ident, $trait: ident, $method: ident, $output: ty ), $width:expr ) => {
impl<const F:usize> core::ops::$trait for $struct<$width,F>{ impl<const F:usize> core::ops::$trait for $struct<$width,F>{
type Output = $output; type Output = $output;
fn $method(self, other: Self) -> Self::Output { fn $method(self, other: Self) -> Self::Output {
//this can be done better but that is a job for later paste::item!{
self.[<fixed_ $method>](other)
}
}
}
};
}
macro_rules! impl_multiplicative_assign_operator_not_const_generic {
( ($struct: ident, $trait: ident, $method: ident, $non_assign_method: ident ), $width:expr ) => {
impl<const F:usize> core::ops::$trait for $struct<$width,F>{
fn $method(&mut self, other: Self) {
paste::item!{
*self=self.[<fixed_ $non_assign_method>](other);
}
}
}
};
}
macro_rules! impl_multiply_operator_not_const_generic {
( ($struct: ident, $trait: ident, $method: ident, $output: ty ), $width:expr ) => {
impl<const F:usize> $struct<$width,F>{
paste::item!{
#[inline]
pub fn [<fixed_ $method>](self, other: Self) -> Self {
let lhs=self.bits.as_::<BInt::<{$width*2}>>(); let lhs=self.bits.as_::<BInt::<{$width*2}>>();
let rhs=other.bits.as_::<BInt::<{$width*2}>>(); let rhs=other.bits.as_::<BInt::<{$width*2}>>();
Self::from_bits(lhs.mul(rhs).shr(F as u32).as_()) Self::from_bits(lhs.mul(rhs).shr(F as u32).as_())
} }
}
};
}
macro_rules! impl_multiply_assign_operator_not_const_generic {
( ($struct: ident, $trait: ident, $method: ident ), $width:expr ) => {
impl<const F:usize> core::ops::$trait for $struct<$width,F>{
fn $method(&mut self, other: Self) {
self.bits.$method(other.bits);
} }
} }
}; impl_multiplicative_operator_not_const_generic!(($struct, $trait, $method, $output ), $width);
}
} }
macro_rules! impl_divide_operator_not_const_generic { macro_rules! impl_divide_operator_not_const_generic {
( ($struct: ident, $trait: ident, $method: ident, $output: ty ), $width:expr ) => { ( ($struct: ident, $trait: ident, $method: ident, $output: ty ), $width:expr ) => {
impl<const F:usize> core::ops::$trait for $struct<$width,F>{ impl<const F:usize> $struct<$width,F>{
type Output = $output; paste::item!{
#[inline]
fn $method(self, other: Self) -> Self::Output { pub fn [<fixed_ $method>](self, other: Self) -> Self {
//this can be done better but that is a job for later
//this only needs to be $width+F as u32/64+1 but MUH CONST GENERICS!!!!! //this only needs to be $width+F as u32/64+1 but MUH CONST GENERICS!!!!!
let lhs=self.bits.as_::<BInt::<{$width*2}>>().shl(F as u32); let lhs=self.bits.as_::<BInt::<{$width*2}>>().shl(F as u32);
let rhs=other.bits.as_::<BInt::<{$width*2}>>(); let rhs=other.bits.as_::<BInt::<{$width*2}>>();
Self::from_bits(lhs.div(rhs).as_()) Self::from_bits(lhs.div(rhs).as_())
} }
}
};
}
macro_rules! impl_divide_assign_operator_not_const_generic {
( ($struct: ident, $trait: ident, $method: ident ), $width:expr ) => {
impl<const F:usize> core::ops::$trait for $struct<$width,F>{
fn $method(&mut self, other: Self) {
self.bits.$method(other.bits);
} }
} }
impl_multiplicative_operator_not_const_generic!(($struct, $trait, $method, $output ), $width);
}; };
} }
macro_rules! impl_multiplicatave_operator { macro_rules! impl_multiplicative_operator {
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => { ( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F> impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F>
where where
@ -198,7 +209,7 @@ macro_rules! impl_multiplicatave_operator {
} }
}; };
} }
macro_rules! impl_multiplicatave_assign_operator { macro_rules! impl_multiplicative_assign_operator {
( $struct: ident, $trait: ident, $method: ident ) => { ( $struct: ident, $trait: ident, $method: ident ) => {
impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F> impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F>
where where
@ -229,14 +240,14 @@ macro_rules! macro_16 {
} }
} }
macro_16!( impl_multiply_assign_operator_not_const_generic, (Fixed, MulAssign, mul_assign) ); macro_16!( impl_multiplicative_assign_operator_not_const_generic, (Fixed, MulAssign, mul_assign, mul) );
macro_16!( impl_multiply_operator_not_const_generic, (Fixed, Mul, mul, Self) ); macro_16!( impl_multiply_operator_not_const_generic, (Fixed, Mul, mul, Self) );
macro_16!( impl_divide_assign_operator_not_const_generic, (Fixed, DivAssign, div_assign) ); macro_16!( impl_multiplicative_assign_operator_not_const_generic, (Fixed, DivAssign, div_assign, div) );
macro_16!( impl_divide_operator_not_const_generic, (Fixed, Div, div, Self) ); macro_16!( impl_divide_operator_not_const_generic, (Fixed, Div, div, Self) );
impl_multiplicatave_assign_operator!( Fixed, MulAssign, mul_assign ); impl_multiplicative_assign_operator!( Fixed, MulAssign, mul_assign );
impl_multiplicatave_operator!( Fixed, Mul, mul, Self ); impl_multiplicative_operator!( Fixed, Mul, mul, Self );
impl_multiplicatave_assign_operator!( Fixed, DivAssign, div_assign ); impl_multiplicative_assign_operator!( Fixed, DivAssign, div_assign );
impl_multiplicatave_operator!( Fixed, Div, div, Self ); impl_multiplicative_operator!( Fixed, Div, div, Self );
macro_rules! impl_shift_operator { macro_rules! impl_shift_operator {
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => { ( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {