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:BInt<{CHUNKS}>, frac:PhantomData, } pub type FixedI192=Fixed<3,Frac>; pub type FixedI256=Fixed<4,Frac>; pub type FixedI512=Fixed<8,Frac>; impl From for Fixed{ fn from(value:i128)->Self{ Self{ bits:BInt::<{CHUNKS}>::from(value)< PartialEq for Fixed{ fn eq(&self,other:&Self)->bool{ self.bits.eq(&other.bits) } } impl Eq for Fixed{} impl std::ops::Add for Fixed{ type Output=Self; fn add(self,rhs:Self)->Self::Output{ Self{ bits:self.bits+rhs.bits, frac:PhantomData, } } } pub trait WideMul{ type Output; fn wide_mul(self,rhs:Rhs)->Self::Output; } //going wide from foreign fixed type impl WideMul> for FixedI64 where A:std::ops::Add, B:Unsigned, { type Output=FixedI128>; fn wide_mul(self,rhs:FixedI64)->Self::Output{ 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, B:Unsigned, { type Output=FixedI256>; fn wide_mul(self,rhs:FixedI128)->Self::Output{ FixedI256{ bits:I256::from(self.to_bits())*I256::from(rhs.to_bits()), frac:PhantomData, } } } //going wider native 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);