use bnum::{BInt,cast::As}; 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 MAX:Self=Self{bits:BInt::::MAX,frac:PhantomData}; pub const MIN:Self=Self{bits:BInt::::MIN,frac:PhantomData}; pub const ZERO:Self=Self{bits:BInt::::ZERO,frac:PhantomData}; pub const EPSILON:Self=Self{bits:BInt::::ONE,frac:PhantomData}; pub const NEG_EPSILON:Self=Self{bits:BInt::::NEG_ONE,frac:PhantomData}; pub const ONE:Self=Self{bits:BInt::::ONE.shl(Frac::U32),frac:PhantomData}; pub const TWO:Self=Self{bits:BInt::::TWO.shl(Frac::U32),frac:PhantomData}; pub const HALF:Self=Self{bits:BInt::::ONE.shl(Frac::U32-1),frac:PhantomData}; pub const NEG_ONE:Self=Self{bits:BInt::::NEG_ONE.shl(Frac::U32),frac:PhantomData}; pub const NEG_TWO:Self=Self{bits:BInt::::NEG_TWO.shl(Frac::U32),frac:PhantomData}; pub const NEG_HALF:Self=Self{bits:BInt::::NEG_ONE.shl(Frac::U32-1),frac:PhantomData}; pub const fn from_bits(bits:BInt::)->Self{ Self{ bits, frac:PhantomData, } } pub const fn to_bits(self)->BInt{ self.bits } } 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_additive_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, } } } impl core::ops::$trait for $struct where BInt:::From, { type Output = $output; fn $method(self, other: U) -> Self::Output { Self { bits:self.bits.$method(BInt::::from(other)< { impl core::ops::$trait for $struct{ fn $method(&mut self, other: Self) { self.bits.$method(other.bits); } } impl core::ops::$trait for $struct where BInt:::From, { fn $method(&mut self, other: U) { self.bits.$method(BInt::::from(other)< { impl core::ops::$trait for $struct<$width,Frac>{ type Output = $output; fn $method(self, other: Self) -> Self::Output { //this can be done better but that is a job for later let lhs=self.bits.as_::>(); let rhs=other.bits.as_::>(); Self { bits:lhs.mul(rhs).shr(Frac::U32).as_(), frac:PhantomData, } } } }; } macro_rules! impl_multiply_assign_operator_const { ( $width:expr, $struct: ident, $trait: ident, $method: ident ) => { impl core::ops::$trait for $struct<$width,Frac>{ fn $method(&mut self, other: Self) { self.bits.$method(other.bits); } } }; } macro_rules! impl_divide_operator_const { ( $width:expr, $struct: ident, $trait: ident, $method: ident, $output: ty ) => { impl core::ops::$trait for $struct<$width,Frac>{ type Output = $output; fn $method(self, other: Self) -> Self::Output { //this can be done better but that is a job for later //this only needs to be $width+Frac::U32/64+1 but MUH CONST GENERICS!!!!! let lhs=self.bits.as_::>().shl(Frac::U32); let rhs=other.bits.as_::>(); Self { bits:lhs.div(rhs).as_(), frac:PhantomData, } } } }; } macro_rules! impl_divide_assign_operator_const { ( $width:expr, $struct: ident, $trait: ident, $method: ident ) => { impl core::ops::$trait for $struct<$width,Frac>{ fn $method(&mut self, other: Self) { self.bits.$method(other.bits); } } }; } macro_rules! impl_multiplicatave_operator { ( $struct: ident, $trait: ident, $method: ident, $output: ty ) => { impl core::ops::$trait for $struct where BInt:::From+core::ops::$trait, { type Output = $output; fn $method(self, other: U) -> Self::Output { Self { bits:self.bits.$method(BInt::::from(other)), frac:PhantomData, } } } }; } macro_rules! impl_multiplicatave_assign_operator { ( $struct: ident, $trait: ident, $method: ident ) => { impl core::ops::$trait for $struct where BInt:::From+core::ops::$trait, { fn $method(&mut self, other: U) { self.bits.$method(BInt::::from(other)); } } }; } macro_rules! impl_operator_16 { ( $macro: ident, $struct: ident, $trait: ident, $method: ident, $output: ty ) => { $macro!(1,$struct,$trait,$method,$output); $macro!(2,$struct,$trait,$method,$output); $macro!(3,$struct,$trait,$method,$output); $macro!(4,$struct,$trait,$method,$output); $macro!(5,$struct,$trait,$method,$output); $macro!(6,$struct,$trait,$method,$output); $macro!(7,$struct,$trait,$method,$output); $macro!(8,$struct,$trait,$method,$output); $macro!(9,$struct,$trait,$method,$output); $macro!(10,$struct,$trait,$method,$output); $macro!(11,$struct,$trait,$method,$output); $macro!(12,$struct,$trait,$method,$output); $macro!(13,$struct,$trait,$method,$output); $macro!(14,$struct,$trait,$method,$output); $macro!(15,$struct,$trait,$method,$output); $macro!(16,$struct,$trait,$method,$output); } } macro_rules! impl_assign_operator_16 { ( $macro: ident, $struct: ident, $trait: ident, $method: ident ) => { $macro!(1,$struct,$trait,$method); $macro!(2,$struct,$trait,$method); $macro!(3,$struct,$trait,$method); $macro!(4,$struct,$trait,$method); $macro!(5,$struct,$trait,$method); $macro!(6,$struct,$trait,$method); $macro!(7,$struct,$trait,$method); $macro!(8,$struct,$trait,$method); $macro!(9,$struct,$trait,$method); $macro!(10,$struct,$trait,$method); $macro!(11,$struct,$trait,$method); $macro!(12,$struct,$trait,$method); $macro!(13,$struct,$trait,$method); $macro!(14,$struct,$trait,$method); $macro!(15,$struct,$trait,$method); $macro!(16,$struct,$trait,$method); } } impl_assign_operator_16!( impl_multiply_assign_operator_const, Fixed, MulAssign, mul_assign ); impl_operator_16!( impl_multiply_operator_const, Fixed, Mul, mul, Self ); impl_assign_operator_16!( impl_divide_assign_operator_const, Fixed, DivAssign, div_assign ); impl_operator_16!( impl_divide_operator_const, Fixed, Div, div, Self ); impl_multiplicatave_assign_operator!( Fixed, MulAssign, mul_assign ); impl_multiplicatave_operator!( Fixed, Mul, mul, Self ); impl_multiplicatave_assign_operator!( Fixed, DivAssign, div_assign ); impl_multiplicatave_operator!( Fixed, Div, div, 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 );