use bnum::BInt; use typenum::Unsigned; use std::marker::PhantomData; #[derive(Clone,Copy,Debug,Hash)] pub struct Fixed{ pub(crate)bits:BInt<{CHUNKS}>, pub(crate)frac:PhantomData, } impl Fixed{ pub const ZERO:Self=Self{bits:BInt::::ZERO,frac:PhantomData}; pub const ONE:Self=Self{bits:BInt::::ONE.shl(Frac::U32),frac:PhantomData}; pub const NEG_ONE:Self=Self{bits:BInt::::NEG_ONE.shl(Frac::U32),frac:PhantomData}; } impl From for Fixed where BInt:From { fn from(value:T)->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 PartialOrd for Fixed{ fn partial_cmp(&self,other:&Self)->Option{ self.bits.partial_cmp(&other.bits) } } impl Ord for Fixed{ fn cmp(&self,other:&Self)->std::cmp::Ordering{ self.bits.cmp(&other.bits) } } impl std::ops::Neg for Fixed{ type Output=Self; fn neg(self)->Self{ Self{ bits:self.bits.neg(), frac:PhantomData, } } } macro_rules! impl_operator { ( $struct: ident, $trait: ident, $method: ident, $output: ty ) => { impl core::ops::$trait for $struct{ type Output = $output; fn $method(self, other: Self) -> Self::Output { Self { bits:self.bits.$method(other.bits), frac:PhantomData, } } } }; } macro_rules! impl_assign_operator { ( $struct: ident, $trait: ident, $method: ident ) => { impl core::ops::$trait for $struct{ fn $method(&mut self, other: Self) { self.bits.$method(other.bits); } } }; } // Impl arithmetic pperators impl_assign_operator!( Fixed, AddAssign, add_assign ); impl_operator!( Fixed, Add, add, Self ); impl_assign_operator!( Fixed, SubAssign, sub_assign ); impl_operator!( Fixed, Sub, sub, Self ); impl_assign_operator!( Fixed, MulAssign, mul_assign ); impl_operator!( Fixed, Mul, mul, Self ); impl_assign_operator!( Fixed, DivAssign, div_assign ); impl_operator!( Fixed, Div, div, Self ); impl_assign_operator!( Fixed, RemAssign, rem_assign ); impl_operator!( Fixed, Rem, rem, Self ); // Impl bitwise operators impl_assign_operator!( Fixed, BitAndAssign, bitand_assign ); impl_operator!( Fixed, BitAnd, bitand, Self ); impl_assign_operator!( Fixed, BitOrAssign, bitor_assign ); impl_operator!( Fixed, BitOr, bitor, Self ); impl_assign_operator!( Fixed, BitXorAssign, bitxor_assign ); impl_operator!( Fixed, BitXor, bitxor, Self ); macro_rules! impl_shift_operator { ( $struct: ident, $trait: ident, $method: ident, $output: ty ) => { impl core::ops::$trait for $struct{ type Output = $output; fn $method(self, other: u32) -> Self::Output { Self { bits:self.bits.$method(other), frac:PhantomData, } } } }; } macro_rules! impl_shift_assign_operator { ( $struct: ident, $trait: ident, $method: ident ) => { impl core::ops::$trait for $struct{ fn $method(&mut self, other: u32) { self.bits.$method(other); } } }; } impl_shift_assign_operator!( Fixed, ShlAssign, shl_assign ); impl_shift_operator!( Fixed, Shl, shl, Self ); impl_shift_assign_operator!( Fixed, ShrAssign, shr_assign ); impl_shift_operator!( Fixed, Shr, shr, Self ); macro_rules! impl_sqrt { ( $struct: ident, $chunks: expr) => { impl $struct<$chunks,Frac>{ pub fn sqrt(self)->$struct<{$chunks/2+1},typenum::PartialDiv>{ $struct{ bits:self.bits.sqrt(), frac:PhantomData, } } } }; } impl_sqrt!(Fixed,1);impl_sqrt!(Fixed,2);impl_sqrt!(Fixed,3);impl_sqrt!(Fixed,4);impl_sqrt!(Fixed,5);impl_sqrt!(Fixed,6);impl_sqrt!(Fixed,7);impl_sqrt!(Fixed,8); impl_sqrt!(Fixed,9);impl_sqrt!(Fixed,10);impl_sqrt!(Fixed,11);impl_sqrt!(Fixed,12);impl_sqrt!(Fixed,13);impl_sqrt!(Fixed,14);impl_sqrt!(Fixed,15);impl_sqrt!(Fixed,16);