efficient fixed mul

This commit is contained in:
Quaternions 2024-09-21 15:42:29 -07:00
parent 546a4aa8c7
commit ba357ee99b

View File

@ -295,10 +295,14 @@ macro_rules! impl_multiply_operator_not_const_generic {
impl<const F:usize> $struct<$width,F>{ impl<const F:usize> $struct<$width,F>{
paste::item!{ paste::item!{
#[inline] #[inline]
pub fn [<fixed_ $method>](self, other: Self) -> Self { pub fn [<fixed_ $method>](self, rhs: Self) -> Self {
let lhs=self.bits.as_::<BInt::<{$width*2}>>(); let (low,high)=self.bits.unsigned_abs().widening_mul(rhs.bits.unsigned_abs());
let rhs=other.bits.as_::<BInt::<{$width*2}>>(); let out:BInt::<{$width*2}>=unsafe{core::mem::transmute([low,high])};
Self::from_bits(lhs.mul(rhs).shr(F as u32).as_()) 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_())
}
} }
} }
} }