From 803f1bea9e1bb56b3a7997432f30b01a5a82f021 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Mon, 9 Sep 2024 13:58:24 -0700 Subject: [PATCH] extract trait impls into named functions + fix spelling --- fixed_wide/src/fixed.rs | 85 +++++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/fixed_wide/src/fixed.rs b/fixed_wide/src/fixed.rs index 29f59b4..4cdfdef 100644 --- a/fixed_wide/src/fixed.rs +++ b/fixed_wide/src/fixed.rs @@ -82,11 +82,16 @@ impl std::iter::Sum for Fixed{ macro_rules! impl_additive_operator { ( $struct: ident, $trait: ident, $method: ident, $output: ty ) => { + impl $struct{ + #[inline] + pub const fn $method(self, other: Self) -> Self { + Self::from_bits(self.bits.$method(other.bits)) + } + } impl core::ops::$trait for $struct{ type Output = $output; - fn $method(self, other: Self) -> Self::Output { - Self::from_bits(self.bits.$method(other.bits)) + self.$method(other) } } impl core::ops::$trait for $struct @@ -94,7 +99,6 @@ macro_rules! impl_additive_operator { BInt:::From, { type Output = $output; - fn $method(self, other: U) -> Self::Output { Self::from_bits(self.bits.$method(BInt::::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_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 ) => { impl core::ops::$trait for $struct<$width,F>{ type Output = $output; - fn $method(self, other: Self) -> Self::Output { - //this can be done better but that is a job for later + paste::item!{ + self.[](other) + } + } + } + }; +} +macro_rules! impl_multiplicative_assign_operator_not_const_generic { + ( ($struct: ident, $trait: ident, $method: ident, $non_assign_method: ident ), $width:expr ) => { + impl core::ops::$trait for $struct<$width,F>{ + fn $method(&mut self, other: Self) { + paste::item!{ + *self=self.[](other); + } + } + } + }; +} + +macro_rules! impl_multiply_operator_not_const_generic { + ( ($struct: ident, $trait: ident, $method: ident, $output: ty ), $width:expr ) => { + impl $struct<$width,F>{ + paste::item!{ + #[inline] + pub fn [](self, other: Self) -> Self { let lhs=self.bits.as_::>(); let rhs=other.bits.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 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 { ( ($struct: ident, $trait: ident, $method: ident, $output: ty ), $width:expr ) => { - impl core::ops::$trait for $struct<$width,F>{ - type Output = $output; - - fn $method(self, other: Self) -> Self::Output { - //this can be done better but that is a job for later + impl $struct<$width,F>{ + paste::item!{ + #[inline] + pub fn [](self, other: Self) -> Self { //this only needs to be $width+F as u32/64+1 but MUH CONST GENERICS!!!!! let lhs=self.bits.as_::>().shl(F as u32); let rhs=other.bits.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 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 ) => { impl core::ops::$trait for $struct 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 ) => { impl core::ops::$trait for $struct 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_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) ); -impl_multiplicatave_assign_operator!( Fixed, MulAssign, mul_assign ); -impl_multiplicatave_operator!( Fixed, Mul, mul, Self ); -impl_multiplicatave_assign_operator!( Fixed, DivAssign, div_assign ); -impl_multiplicatave_operator!( Fixed, Div, div, Self ); +impl_multiplicative_assign_operator!( Fixed, MulAssign, mul_assign ); +impl_multiplicative_operator!( Fixed, Mul, mul, Self ); +impl_multiplicative_assign_operator!( Fixed, DivAssign, div_assign ); +impl_multiplicative_operator!( Fixed, Div, div, Self ); macro_rules! impl_shift_operator { ( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {