From ba357ee99b48a5bcde8a5e061043058e4af344fb Mon Sep 17 00:00:00 2001 From: Quaternions Date: Sat, 21 Sep 2024 15:42:29 -0700 Subject: [PATCH] efficient fixed mul --- fixed_wide/src/fixed.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/fixed_wide/src/fixed.rs b/fixed_wide/src/fixed.rs index a566151..05e8b26 100644 --- a/fixed_wide/src/fixed.rs +++ b/fixed_wide/src/fixed.rs @@ -295,10 +295,14 @@ macro_rules! impl_multiply_operator_not_const_generic { 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_()) + pub fn [](self, rhs: Self) -> Self { + let (low,high)=self.bits.unsigned_abs().widening_mul(rhs.bits.unsigned_abs()); + let out:BInt::<{$width*2}>=unsafe{core::mem::transmute([low,high])}; + if self.is_negative()==rhs.is_negative(){ + Self::from_bits(out.shr(F as u32).as_()) + }else{ + -Self::from_bits(out.shr(F as u32).as_()) + } } } }