diff --git a/fixed_wide/src/wide.rs b/fixed_wide/src/wide.rs index 9d050ce..114c673 100644 --- a/fixed_wide/src/wide.rs +++ b/fixed_wide/src/wide.rs @@ -1,38 +1,41 @@ use bnum::cast::As; use bnum::types::{I256,I512}; +use bnum::BInt; use fixed::{FixedI64,FixedI128}; use typenum::{Sum,Unsigned}; +use std::marker::PhantomData; #[derive(Clone,Copy,Debug)] -pub struct Fixed{ - bits:Int, - phantom:std::marker::PhantomData, +pub struct Fixed{ + bits:BInt<{CHUNKS}>, + frac:PhantomData, } -pub type FixedI256=Fixed; -pub type FixedI512=Fixed; +pub type FixedI192=Fixed<3,Frac>; +pub type FixedI256=Fixed<4,Frac>; +pub type FixedI512=Fixed<8,Frac>; -impl+std::ops::Shl,FracDst:Unsigned> From for Fixed{ +impl From for Fixed{ fn from(value:i128)->Self{ Self{ - bits:Int::from(value)<::from(value)< PartialEq for Fixed{ +impl PartialEq for Fixed{ fn eq(&self,other:&Self)->bool{ self.bits.eq(&other.bits) } } -impl Eq for Fixed{} +impl Eq for Fixed{} -impl,Frac> std::ops::Add for Fixed{ +impl std::ops::Add for Fixed{ type Output=Self; fn add(self,rhs:Self)->Self::Output{ Self{ bits:self.bits+rhs.bits, - phantom:std::marker::PhantomData, + frac:PhantomData, } } } @@ -53,6 +56,32 @@ impl WideMul> for FixedI64 self.wide_mul(rhs) } } +impl WideMul> for FixedI128 + where + A:std::ops::Add, + B:Unsigned, +{ + type Output=FixedI192>; + fn wide_mul(self,rhs:FixedI64)->Self::Output{ + FixedI192{ + bits:BInt::<3>::from(self.to_bits())*BInt::<3>::from(rhs.to_bits()), + frac:PhantomData, + } + } +} +impl WideMul> for FixedI64 + where + A:std::ops::Add, + B:Unsigned, +{ + type Output=FixedI192>; + fn wide_mul(self,rhs:FixedI128)->Self::Output{ + FixedI192{ + bits:BInt::<3>::from(self.to_bits())*BInt::<3>::from(rhs.to_bits()), + frac:PhantomData, + } + } +} impl WideMul> for FixedI128 where A:std::ops::Add, @@ -62,21 +91,32 @@ impl WideMul> for FixedI128 fn wide_mul(self,rhs:FixedI128)->Self::Output{ FixedI256{ bits:I256::from(self.to_bits())*I256::from(rhs.to_bits()), - phantom:std::marker::PhantomData, + frac:PhantomData, } } } + //going wider native -impl WideMul> for FixedI256 - where - A:std::ops::Add, - B:Unsigned, -{ - type Output=FixedI512>; - fn wide_mul(self,rhs:FixedI256)->Self::Output{ - FixedI512{ - bits:self.bits.as_::()*rhs.bits.as_::(), - phantom:std::marker::PhantomData, +macro_rules! impl_wide_mul { + ($lhs: expr,$rhs: expr) => { + impl WideMul> for Fixed<$lhs,A> + where + A:std::ops::Add, + B:Unsigned, + { + type Output=Fixed<{$lhs+$rhs},Sum>; + fn wide_mul(self,rhs:Fixed<$rhs,B>)->Self::Output{ + Fixed{ + bits:self.bits.as_::>()*rhs.bits.as_::>(), + frac:PhantomData, + } + } } - } + }; } +impl_wide_mul!(3,3);impl_wide_mul!(4,3);impl_wide_mul!(5,3);impl_wide_mul!(6,3);impl_wide_mul!(7,3);impl_wide_mul!(8,3); +impl_wide_mul!(3,4);impl_wide_mul!(4,4);impl_wide_mul!(5,4);impl_wide_mul!(6,4);impl_wide_mul!(7,4);impl_wide_mul!(8,4); +impl_wide_mul!(3,5);impl_wide_mul!(4,5);impl_wide_mul!(5,5);impl_wide_mul!(6,5);impl_wide_mul!(7,5);impl_wide_mul!(8,5); +impl_wide_mul!(3,6);impl_wide_mul!(4,6);impl_wide_mul!(5,6);impl_wide_mul!(6,6);impl_wide_mul!(7,6);impl_wide_mul!(8,6); +impl_wide_mul!(3,7);impl_wide_mul!(4,7);impl_wide_mul!(5,7);impl_wide_mul!(6,7);impl_wide_mul!(7,7);impl_wide_mul!(8,7); +impl_wide_mul!(3,8);impl_wide_mul!(4,8);impl_wide_mul!(5,8);impl_wide_mul!(6,8);impl_wide_mul!(7,8);impl_wide_mul!(8,8);