use bnum::{BInt,cast::As}; #[derive(Clone,Copy,Debug,Hash)] /// N is the number of u64s to use /// F is the number of fractional bits (always N*32 lol) pub struct Fixed{ pub(crate)bits:BInt<{N}>, } impl Fixed{ pub const MAX:Self=Self::from_bits(BInt::::MAX); pub const MIN:Self=Self::from_bits(BInt::::MIN); pub const ZERO:Self=Self::from_bits(BInt::::ZERO); pub const EPSILON:Self=Self::from_bits(BInt::::ONE); pub const NEG_EPSILON:Self=Self::from_bits(BInt::::NEG_ONE); pub const ONE:Self=Self::from_bits(BInt::::ONE.shl(F as u32)); pub const TWO:Self=Self::from_bits(BInt::::TWO.shl(F as u32)); pub const HALF:Self=Self::from_bits(BInt::::ONE.shl(F as u32-1)); pub const NEG_ONE:Self=Self::from_bits(BInt::::NEG_ONE.shl(F as u32)); pub const NEG_TWO:Self=Self::from_bits(BInt::::NEG_TWO.shl(F as u32)); pub const NEG_HALF:Self=Self::from_bits(BInt::::NEG_ONE.shl(F as u32-1)); } impl Fixed{ #[inline] pub const fn from_bits(bits:BInt::)->Self{ Self{ bits, } } #[inline] pub const fn to_bits(self)->BInt{ self.bits } #[inline] pub const fn raw(value:i64)->Self{ Self::from_bits(BInt::from_bits(bnum::BUint::from_digit(value as u64))) } } impl From for Fixed where BInt:From { fn from(value:T)->Self{ Self::from_bits(BInt::<{N}>::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::from_bits(self.bits.neg()) } } impl std::iter::Sum for Fixed{ fn sum>(iter:I)->Self{ let mut sum=Self::ZERO; for elem in iter{ sum+=elem; } sum } } macro_rules! impl_additive_operator { ( $struct: ident, $trait: ident, $method: ident, $output: ty ) => { impl $struct{ #[inline] pub const fn $method(self, other: Self) -> Self { Self::from_bits(self.bits.$method(other.bits)) } } impl core::ops::$trait for $struct{ type Output = $output; fn $method(self, other: Self) -> Self::Output { self.$method(other) } } impl core::ops::$trait for $struct where BInt:::From, { type Output = $output; fn $method(self, other: U) -> Self::Output { Self::from_bits(self.bits.$method(BInt::::from(other).shl(F as u32))) } } }; } macro_rules! impl_additive_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 core::ops::$trait for $struct where BInt:::From, { fn $method(&mut self, other: U) { self.bits.$method(BInt::::from(other).shl(F as u32)); } } }; } // Impl arithmetic pperators impl_additive_assign_operator!( Fixed, AddAssign, add_assign ); impl_additive_operator!( Fixed, Add, add, Self ); impl_additive_assign_operator!( Fixed, SubAssign, sub_assign ); impl_additive_operator!( Fixed, Sub, sub, Self ); impl_additive_assign_operator!( Fixed, RemAssign, rem_assign ); impl_additive_operator!( Fixed, Rem, rem, Self ); // Impl bitwise operators impl_additive_assign_operator!( Fixed, BitAndAssign, bitand_assign ); impl_additive_operator!( Fixed, BitAnd, bitand, Self ); impl_additive_assign_operator!( Fixed, BitOrAssign, bitor_assign ); impl_additive_operator!( Fixed, BitOr, bitor, Self ); impl_additive_assign_operator!( Fixed, BitXorAssign, bitxor_assign ); impl_additive_operator!( Fixed, BitXor, bitxor, Self ); macro_rules! impl_multiplicative_operator_not_const_generic { ( ($struct: ident, $trait: ident, $method: ident, $output: ty ), $width:expr ) => { impl core::ops::$trait for $struct<$width,F>{ type Output = $output; fn $method(self, other: Self) -> Self::Output { paste::item!{ self.[](other) } } } }; } macro_rules! impl_multiplicative_assign_operator_not_const_generic { ( ($struct: ident, $trait: ident, $method: ident, $non_assign_method: ident ), $width:expr ) => { impl core::ops::$trait for $struct<$width,F>{ fn $method(&mut self, other: Self) { paste::item!{ *self=self.[](other); } } } }; } macro_rules! impl_multiply_operator_not_const_generic { ( ($struct: ident, $trait: ident, $method: ident, $output: ty ), $width:expr ) => { impl $struct<$width,F>{ paste::item!{ #[inline] pub fn [](self, other: Self) -> Self { let lhs=self.bits.as_::>(); let rhs=other.bits.as_::>(); Self::from_bits(lhs.mul(rhs).shr(F as u32).as_()) } } } impl_multiplicative_operator_not_const_generic!(($struct, $trait, $method, $output ), $width); } } macro_rules! impl_divide_operator_not_const_generic { ( ($struct: ident, $trait: ident, $method: ident, $output: ty ), $width:expr ) => { impl $struct<$width,F>{ paste::item!{ #[inline] pub fn [](self, other: Self) -> Self { //this only needs to be $width+F as u32/64+1 but MUH CONST GENERICS!!!!! let lhs=self.bits.as_::>().shl(F as u32); let rhs=other.bits.as_::>(); Self::from_bits(lhs.div(rhs).as_()) } } } impl_multiplicative_operator_not_const_generic!(($struct, $trait, $method, $output ), $width); }; } macro_rules! impl_multiplicative_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::from_bits(self.bits.$method(BInt::::from(other))) } } }; } macro_rules! impl_multiplicative_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! macro_repeated{ ( $macro:ident, $any:tt, $($repeated:tt),* )=>{ $( $macro!($any, $repeated); )* }; } macro_rules! macro_16 { ( $macro: ident, $any:tt ) => { macro_repeated!($macro,$any,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16); } } macro_16!( impl_multiplicative_assign_operator_not_const_generic, (Fixed, MulAssign, mul_assign, mul) ); macro_16!( impl_multiply_operator_not_const_generic, (Fixed, Mul, mul, Self) ); macro_16!( impl_multiplicative_assign_operator_not_const_generic, (Fixed, DivAssign, div_assign, div) ); macro_16!( impl_divide_operator_not_const_generic, (Fixed, Div, div, Self) ); impl_multiplicative_assign_operator!( Fixed, MulAssign, mul_assign ); impl_multiplicative_operator!( Fixed, Mul, mul, Self ); impl_multiplicative_assign_operator!( Fixed, DivAssign, div_assign ); impl_multiplicative_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::from_bits(self.bits.$method(other)) } } }; } 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 ); // WIDE MUL: multiply into a wider type // let a = I32F32::ONE; // let b:I64F64 = a.wide_mul(a); macro_rules! impl_wide_mul{ ( (), ($lhs:expr,$rhs:expr) )=>{ impl Fixed<$lhs,{$lhs*32}> { paste::item!{ pub fn [](self,rhs:Fixed<$rhs,{$rhs*32}>)->Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>{ Fixed::from_bits(self.bits.as_::>()*rhs.bits.as_::>()) } } } }; } //const generics sidestepped wahoo macro_repeated!( impl_wide_mul,(), (1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),(8,1),(9,1),(10,1),(11,1),(12,1),(13,1),(14,1),(15,1), (1,2),(2,2),(3,2),(4,2),(5,2),(6,2),(7,2),(8,2),(9,2),(10,2),(11,2),(12,2),(13,2),(14,2), (1,3),(2,3),(3,3),(4,3),(5,3),(6,3),(7,3),(8,3),(9,3),(10,3),(11,3),(12,3),(13,3), (1,4),(2,4),(3,4),(4,4),(5,4),(6,4),(7,4),(8,4),(9,4),(10,4),(11,4),(12,4), (1,5),(2,5),(3,5),(4,5),(5,5),(6,5),(7,5),(8,5),(9,5),(10,5),(11,5), (1,6),(2,6),(3,6),(4,6),(5,6),(6,6),(7,6),(8,6),(9,6),(10,6), (1,7),(2,7),(3,7),(4,7),(5,7),(6,7),(7,7),(8,7),(9,7), (1,8),(2,8),(3,8),(4,8),(5,8),(6,8),(7,8),(8,8), (1,9),(2,9),(3,9),(4,9),(5,9),(6,9),(7,9), (1,10),(2,10),(3,10),(4,10),(5,10),(6,10), (1,11),(2,11),(3,11),(4,11),(5,11), (1,12),(2,12),(3,12),(4,12), (1,13),(2,13),(3,13), (1,14),(2,14), (1,15) ); impl Fixed{ pub fn resize_into(self)->Fixed{ Fixed::from_bits(self.bits.as_::>()) } } macro_rules! impl_not_const_generic{ ($n:expr)=>{ impl Fixed<{$n*2},{$n*2*32}>{ pub fn halve_precision(self)->Fixed<$n,{$n*32}>{ Fixed::from_bits(bnum::cast::As::as_(self.bits.shr($n*32))) } } impl Fixed<$n,{$n*32}>{ paste::item!{ pub fn sqrt_unchecked(self)->Self{ //1<>1 (sqrt-ish) //3. add on fractional offset //Voila let used_bits=self.bits.bits() as i32-1-($n*32) as i32; let max_shift=((used_bits>>1)+($n*32) as i32) as u32; let mut result=Self::ZERO; //multiply by one to make the types match (hack) let wide_self=self.[](Self::ONE); //descend down the bits and check if flipping each bit would push the square over the input value for shift in (0..=max_shift).rev(){ let new_result=result|Self::from_bits(BInt::from_bits(bnum::BUint::power_of_two(shift))); if new_result.[](new_result)<=wide_self{ result=new_result; } } result } } pub fn sqrt(self)->Self{ if selfOption{ if self