2024-08-26 21:58:19 +00:00
|
|
|
use bnum::BInt;
|
2024-08-27 00:28:33 +00:00
|
|
|
use typenum::Unsigned;
|
2024-08-26 21:58:19 +00:00
|
|
|
use std::marker::PhantomData;
|
2024-08-23 20:42:44 +00:00
|
|
|
|
2024-08-26 23:03:02 +00:00
|
|
|
#[derive(Clone,Copy,Debug,Hash)]
|
2024-08-26 21:58:19 +00:00
|
|
|
pub struct Fixed<const CHUNKS:usize,Frac>{
|
2024-08-27 00:28:33 +00:00
|
|
|
pub(crate)bits:BInt<{CHUNKS}>,
|
|
|
|
pub(crate)frac:PhantomData<Frac>,
|
2024-08-23 20:00:22 +00:00
|
|
|
}
|
2024-08-23 20:42:44 +00:00
|
|
|
|
2024-08-27 23:49:49 +00:00
|
|
|
impl<const CHUNKS:usize,Frac:Unsigned> Fixed<CHUNKS,Frac>{
|
|
|
|
pub const ZERO:Self=Self{bits:BInt::<CHUNKS>::ZERO,frac:PhantomData};
|
|
|
|
pub const ONE:Self=Self{bits:BInt::<CHUNKS>::ONE.shl(Frac::U32),frac:PhantomData};
|
|
|
|
pub const NEG_ONE:Self=Self{bits:BInt::<CHUNKS>::NEG_ONE.shl(Frac::U32),frac:PhantomData};
|
|
|
|
}
|
|
|
|
|
2024-08-26 22:40:27 +00:00
|
|
|
impl<const CHUNKS:usize,FracDst:Unsigned,T> From<T> for Fixed<CHUNKS,FracDst>
|
|
|
|
where
|
|
|
|
BInt<CHUNKS>:From<T>
|
|
|
|
{
|
|
|
|
fn from(value:T)->Self{
|
2024-08-23 21:17:48 +00:00
|
|
|
Self{
|
2024-08-26 21:58:19 +00:00
|
|
|
bits:BInt::<{CHUNKS}>::from(value)<<FracDst::U32,
|
|
|
|
frac:PhantomData,
|
2024-08-23 21:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-26 21:58:19 +00:00
|
|
|
impl<const CHUNKS:usize,Frac> PartialEq for Fixed<CHUNKS,Frac>{
|
2024-08-23 21:17:48 +00:00
|
|
|
fn eq(&self,other:&Self)->bool{
|
2024-08-23 23:53:54 +00:00
|
|
|
self.bits.eq(&other.bits)
|
2024-08-23 21:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-26 21:58:19 +00:00
|
|
|
impl<const CHUNKS:usize,Frac> Eq for Fixed<CHUNKS,Frac>{}
|
2024-08-23 21:17:48 +00:00
|
|
|
|
2024-08-27 21:23:38 +00:00
|
|
|
impl<const CHUNKS:usize,Frac> PartialOrd for Fixed<CHUNKS,Frac>{
|
|
|
|
fn partial_cmp(&self,other:&Self)->Option<std::cmp::Ordering>{
|
|
|
|
self.bits.partial_cmp(&other.bits)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<const CHUNKS:usize,Frac> Ord for Fixed<CHUNKS,Frac>{
|
|
|
|
fn cmp(&self,other:&Self)->std::cmp::Ordering{
|
|
|
|
self.bits.cmp(&other.bits)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-26 23:03:02 +00:00
|
|
|
impl<const CHUNKS:usize,Frac> std::ops::Neg for Fixed<CHUNKS,Frac>{
|
|
|
|
type Output=Self;
|
|
|
|
fn neg(self)->Self{
|
|
|
|
Self{
|
|
|
|
bits:self.bits.neg(),
|
|
|
|
frac:PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-26 22:40:27 +00:00
|
|
|
macro_rules! impl_operator {
|
2024-08-26 22:57:02 +00:00
|
|
|
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
2024-08-27 21:58:20 +00:00
|
|
|
impl<const CHUNKS:usize,Frac> core::ops::$trait for $struct<CHUNKS,Frac>{
|
2024-08-26 22:40:27 +00:00
|
|
|
type Output = $output;
|
|
|
|
|
2024-08-26 22:57:02 +00:00
|
|
|
fn $method(self, other: Self) -> Self::Output {
|
2024-08-26 22:40:27 +00:00
|
|
|
Self {
|
2024-08-26 22:57:02 +00:00
|
|
|
bits:self.bits.$method(other.bits),
|
2024-08-26 22:40:27 +00:00
|
|
|
frac:PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2024-08-23 23:48:24 +00:00
|
|
|
}
|
2024-08-26 22:40:27 +00:00
|
|
|
macro_rules! impl_assign_operator {
|
|
|
|
( $struct: ident, $trait: ident, $method: ident ) => {
|
2024-08-27 21:58:20 +00:00
|
|
|
impl<const CHUNKS:usize,Frac> core::ops::$trait for $struct<CHUNKS,Frac>{
|
2024-08-26 22:57:02 +00:00
|
|
|
fn $method(&mut self, other: Self) {
|
|
|
|
self.bits.$method(other.bits);
|
2024-08-26 22:40:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 );
|
2024-08-27 21:58:32 +00:00
|
|
|
|
|
|
|
macro_rules! impl_shift_operator {
|
|
|
|
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
|
|
|
impl<const CHUNKS:usize,Frac> core::ops::$trait<u32> for $struct<CHUNKS,Frac>{
|
|
|
|
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<const CHUNKS:usize,Frac> core::ops::$trait<u32> for $struct<CHUNKS,Frac>{
|
|
|
|
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 );
|
2024-08-27 23:50:43 +00:00
|
|
|
|
|
|
|
macro_rules! impl_sqrt {
|
|
|
|
( $struct: ident, $chunks: expr) => {
|
|
|
|
impl<Frac:Unsigned> $struct<$chunks,Frac>{
|
|
|
|
pub fn sqrt(self)->$struct<{$chunks/2+1},typenum::PartialDiv<Frac,typenum::U2>>{
|
|
|
|
$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);
|