|
|
|
@ -1,35 +1,34 @@
|
|
|
|
|
use bnum::{BInt,cast::As};
|
|
|
|
|
use typenum::{Sum,Unsigned};
|
|
|
|
|
|
|
|
|
|
#[derive(Clone,Copy,Debug,Hash)]
|
|
|
|
|
pub struct Fixed<const CHUNKS:usize,Frac>{
|
|
|
|
|
pub(crate)bits:BInt<{CHUNKS}>,
|
|
|
|
|
pub(crate)frac:std::marker::PhantomData<Frac>,
|
|
|
|
|
/// N is the number of u64s to use
|
|
|
|
|
/// F is the number of fractional bits (always N*32 lol)
|
|
|
|
|
pub struct Fixed<const N:usize,const F:usize>{
|
|
|
|
|
pub(crate)bits:BInt<{N}>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<const CHUNKS:usize,Frac:Unsigned> Fixed<CHUNKS,Frac>{
|
|
|
|
|
pub const MAX:Self=Self::from_bits(BInt::<CHUNKS>::MAX);
|
|
|
|
|
pub const MIN:Self=Self::from_bits(BInt::<CHUNKS>::MIN);
|
|
|
|
|
pub const ZERO:Self=Self::from_bits(BInt::<CHUNKS>::ZERO);
|
|
|
|
|
pub const EPSILON:Self=Self::from_bits(BInt::<CHUNKS>::ONE);
|
|
|
|
|
pub const NEG_EPSILON:Self=Self::from_bits(BInt::<CHUNKS>::NEG_ONE);
|
|
|
|
|
pub const ONE:Self=Self::from_bits(BInt::<CHUNKS>::ONE.shl(Frac::U32));
|
|
|
|
|
pub const TWO:Self=Self::from_bits(BInt::<CHUNKS>::TWO.shl(Frac::U32));
|
|
|
|
|
pub const HALF:Self=Self::from_bits(BInt::<CHUNKS>::ONE.shl(Frac::U32-1));
|
|
|
|
|
pub const NEG_ONE:Self=Self::from_bits(BInt::<CHUNKS>::NEG_ONE.shl(Frac::U32));
|
|
|
|
|
pub const NEG_TWO:Self=Self::from_bits(BInt::<CHUNKS>::NEG_TWO.shl(Frac::U32));
|
|
|
|
|
pub const NEG_HALF:Self=Self::from_bits(BInt::<CHUNKS>::NEG_ONE.shl(Frac::U32-1));
|
|
|
|
|
impl<const N:usize,const F:usize> Fixed<N,F>{
|
|
|
|
|
pub const MAX:Self=Self::from_bits(BInt::<N>::MAX);
|
|
|
|
|
pub const MIN:Self=Self::from_bits(BInt::<N>::MIN);
|
|
|
|
|
pub const ZERO:Self=Self::from_bits(BInt::<N>::ZERO);
|
|
|
|
|
pub const EPSILON:Self=Self::from_bits(BInt::<N>::ONE);
|
|
|
|
|
pub const NEG_EPSILON:Self=Self::from_bits(BInt::<N>::NEG_ONE);
|
|
|
|
|
pub const ONE:Self=Self::from_bits(BInt::<N>::ONE.shl(F as u32));
|
|
|
|
|
pub const TWO:Self=Self::from_bits(BInt::<N>::TWO.shl(F as u32));
|
|
|
|
|
pub const HALF:Self=Self::from_bits(BInt::<N>::ONE.shl(F as u32-1));
|
|
|
|
|
pub const NEG_ONE:Self=Self::from_bits(BInt::<N>::NEG_ONE.shl(F as u32));
|
|
|
|
|
pub const NEG_TWO:Self=Self::from_bits(BInt::<N>::NEG_TWO.shl(F as u32));
|
|
|
|
|
pub const NEG_HALF:Self=Self::from_bits(BInt::<N>::NEG_ONE.shl(F as u32-1));
|
|
|
|
|
}
|
|
|
|
|
impl<const CHUNKS:usize,Frac> Fixed<CHUNKS,Frac>{
|
|
|
|
|
impl<const N:usize,const F:usize> Fixed<N,F>{
|
|
|
|
|
#[inline]
|
|
|
|
|
pub const fn from_bits(bits:BInt::<CHUNKS>)->Self{
|
|
|
|
|
pub const fn from_bits(bits:BInt::<N>)->Self{
|
|
|
|
|
Self{
|
|
|
|
|
bits,
|
|
|
|
|
frac:std::marker::PhantomData,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#[inline]
|
|
|
|
|
pub const fn to_bits(self)->BInt<CHUNKS>{
|
|
|
|
|
pub const fn to_bits(self)->BInt<N>{
|
|
|
|
|
self.bits
|
|
|
|
|
}
|
|
|
|
|
#[inline]
|
|
|
|
@ -38,34 +37,34 @@ impl<const CHUNKS:usize,Frac> Fixed<CHUNKS,Frac>{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<const CHUNKS:usize,Frac:Unsigned,T> From<T> for Fixed<CHUNKS,Frac>
|
|
|
|
|
impl<const N:usize,const F:usize,T> From<T> for Fixed<N,F>
|
|
|
|
|
where
|
|
|
|
|
BInt<CHUNKS>:From<T>
|
|
|
|
|
BInt<N>:From<T>
|
|
|
|
|
{
|
|
|
|
|
fn from(value:T)->Self{
|
|
|
|
|
Self::from_bits(BInt::<{CHUNKS}>::from(value)<<Frac::U32)
|
|
|
|
|
Self::from_bits(BInt::<{N}>::from(value)<<F as u32)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<const CHUNKS:usize,Frac> PartialEq for Fixed<CHUNKS,Frac>{
|
|
|
|
|
impl<const N:usize,const F:usize> PartialEq for Fixed<N,F>{
|
|
|
|
|
fn eq(&self,other:&Self)->bool{
|
|
|
|
|
self.bits.eq(&other.bits)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl<const CHUNKS:usize,Frac> Eq for Fixed<CHUNKS,Frac>{}
|
|
|
|
|
impl<const N:usize,const F:usize> Eq for Fixed<N,F>{}
|
|
|
|
|
|
|
|
|
|
impl<const CHUNKS:usize,Frac> PartialOrd for Fixed<CHUNKS,Frac>{
|
|
|
|
|
impl<const N:usize,const F:usize> PartialOrd for Fixed<N,F>{
|
|
|
|
|
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>{
|
|
|
|
|
impl<const N:usize,const F:usize> Ord for Fixed<N,F>{
|
|
|
|
|
fn cmp(&self,other:&Self)->std::cmp::Ordering{
|
|
|
|
|
self.bits.cmp(&other.bits)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<const CHUNKS:usize,Frac> std::ops::Neg for Fixed<CHUNKS,Frac>{
|
|
|
|
|
impl<const N:usize,const F:usize> std::ops::Neg for Fixed<N,F>{
|
|
|
|
|
type Output=Self;
|
|
|
|
|
fn neg(self)->Self{
|
|
|
|
|
Self::from_bits(self.bits.neg())
|
|
|
|
@ -74,38 +73,38 @@ impl<const CHUNKS:usize,Frac> std::ops::Neg for Fixed<CHUNKS,Frac>{
|
|
|
|
|
|
|
|
|
|
macro_rules! impl_additive_operator {
|
|
|
|
|
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
|
|
|
|
impl<const CHUNKS:usize,Frac> core::ops::$trait for $struct<CHUNKS,Frac>{
|
|
|
|
|
impl<const N:usize,const F:usize> core::ops::$trait for $struct<N,F>{
|
|
|
|
|
type Output = $output;
|
|
|
|
|
|
|
|
|
|
fn $method(self, other: Self) -> Self::Output {
|
|
|
|
|
Self::from_bits(self.bits.$method(other.bits))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl<const CHUNKS:usize,Frac:Unsigned,U> core::ops::$trait<U> for $struct<CHUNKS,Frac>
|
|
|
|
|
impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F>
|
|
|
|
|
where
|
|
|
|
|
BInt::<CHUNKS>:From<U>,
|
|
|
|
|
BInt::<N>:From<U>,
|
|
|
|
|
{
|
|
|
|
|
type Output = $output;
|
|
|
|
|
|
|
|
|
|
fn $method(self, other: U) -> Self::Output {
|
|
|
|
|
Self::from_bits(self.bits.$method(BInt::<CHUNKS>::from(other)<<Frac::U32))
|
|
|
|
|
Self::from_bits(self.bits.$method(BInt::<N>::from(other).shl(F as u32)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
macro_rules! impl_additive_assign_operator {
|
|
|
|
|
( $struct: ident, $trait: ident, $method: ident ) => {
|
|
|
|
|
impl<const CHUNKS:usize,Frac> core::ops::$trait for $struct<CHUNKS,Frac>{
|
|
|
|
|
impl<const N:usize,const F:usize> core::ops::$trait for $struct<N,F>{
|
|
|
|
|
fn $method(&mut self, other: Self) {
|
|
|
|
|
self.bits.$method(other.bits);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl<const CHUNKS:usize,Frac:Unsigned,U> core::ops::$trait<U> for $struct<CHUNKS,Frac>
|
|
|
|
|
impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F>
|
|
|
|
|
where
|
|
|
|
|
BInt::<CHUNKS>:From<U>,
|
|
|
|
|
BInt::<N>:From<U>,
|
|
|
|
|
{
|
|
|
|
|
fn $method(&mut self, other: U) {
|
|
|
|
|
self.bits.$method(BInt::<CHUNKS>::from(other)<<Frac::U32);
|
|
|
|
|
self.bits.$method(BInt::<N>::from(other)<<F as u32);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
@ -129,21 +128,21 @@ impl_additive_operator!( Fixed, BitXor, bitxor, Self );
|
|
|
|
|
|
|
|
|
|
macro_rules! impl_multiply_operator_const {
|
|
|
|
|
( $width:expr, $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
|
|
|
|
impl<Frac:Unsigned> core::ops::$trait for $struct<$width,Frac>{
|
|
|
|
|
impl<const F:usize> core::ops::$trait for $struct<$width,F>{
|
|
|
|
|
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_::<BInt::<{$width*2}>>();
|
|
|
|
|
let rhs=other.bits.as_::<BInt::<{$width*2}>>();
|
|
|
|
|
Self::from_bits(lhs.mul(rhs).shr(Frac::U32).as_())
|
|
|
|
|
Self::from_bits(lhs.mul(rhs).shr(F as u32).as_())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
macro_rules! impl_multiply_assign_operator_const {
|
|
|
|
|
( $width:expr, $struct: ident, $trait: ident, $method: ident ) => {
|
|
|
|
|
impl<Frac> core::ops::$trait for $struct<$width,Frac>{
|
|
|
|
|
impl<const F:usize> core::ops::$trait for $struct<$width,F>{
|
|
|
|
|
fn $method(&mut self, other: Self) {
|
|
|
|
|
self.bits.$method(other.bits);
|
|
|
|
|
}
|
|
|
|
@ -153,13 +152,13 @@ macro_rules! impl_multiply_assign_operator_const {
|
|
|
|
|
|
|
|
|
|
macro_rules! impl_divide_operator_const {
|
|
|
|
|
( $width:expr, $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
|
|
|
|
impl<Frac:Unsigned> core::ops::$trait for $struct<$width,Frac>{
|
|
|
|
|
impl<const F:usize> core::ops::$trait for $struct<$width,F>{
|
|
|
|
|
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_::<BInt::<{$width*2}>>().shl(Frac::U32);
|
|
|
|
|
//this only needs to be $width+F as u32/64+1 but MUH CONST GENERICS!!!!!
|
|
|
|
|
let lhs=self.bits.as_::<BInt::<{$width*2}>>().shl(F as u32);
|
|
|
|
|
let rhs=other.bits.as_::<BInt::<{$width*2}>>();
|
|
|
|
|
Self::from_bits(lhs.div(rhs).as_())
|
|
|
|
|
}
|
|
|
|
@ -168,7 +167,7 @@ macro_rules! impl_divide_operator_const {
|
|
|
|
|
}
|
|
|
|
|
macro_rules! impl_divide_assign_operator_const {
|
|
|
|
|
( $width:expr, $struct: ident, $trait: ident, $method: ident ) => {
|
|
|
|
|
impl<Frac> core::ops::$trait for $struct<$width,Frac>{
|
|
|
|
|
impl<const F:usize> core::ops::$trait for $struct<$width,F>{
|
|
|
|
|
fn $method(&mut self, other: Self) {
|
|
|
|
|
self.bits.$method(other.bits);
|
|
|
|
|
}
|
|
|
|
@ -178,26 +177,26 @@ macro_rules! impl_divide_assign_operator_const {
|
|
|
|
|
|
|
|
|
|
macro_rules! impl_multiplicatave_operator {
|
|
|
|
|
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
|
|
|
|
impl<const CHUNKS:usize,Frac,U> core::ops::$trait<U> for $struct<CHUNKS,Frac>
|
|
|
|
|
impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F>
|
|
|
|
|
where
|
|
|
|
|
BInt::<CHUNKS>:From<U>+core::ops::$trait,
|
|
|
|
|
BInt::<N>:From<U>+core::ops::$trait,
|
|
|
|
|
{
|
|
|
|
|
type Output = $output;
|
|
|
|
|
|
|
|
|
|
fn $method(self, other: U) -> Self::Output {
|
|
|
|
|
Self::from_bits(self.bits.$method(BInt::<CHUNKS>::from(other)))
|
|
|
|
|
Self::from_bits(self.bits.$method(BInt::<N>::from(other)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
macro_rules! impl_multiplicatave_assign_operator {
|
|
|
|
|
( $struct: ident, $trait: ident, $method: ident ) => {
|
|
|
|
|
impl<const CHUNKS:usize,Frac,U> core::ops::$trait<U> for $struct<CHUNKS,Frac>
|
|
|
|
|
impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F>
|
|
|
|
|
where
|
|
|
|
|
BInt::<CHUNKS>:From<U>+core::ops::$trait,
|
|
|
|
|
BInt::<N>:From<U>+core::ops::$trait,
|
|
|
|
|
{
|
|
|
|
|
fn $method(&mut self, other: U) {
|
|
|
|
|
self.bits.$method(BInt::<CHUNKS>::from(other));
|
|
|
|
|
self.bits.$method(BInt::<N>::from(other));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
@ -255,7 +254,7 @@ impl_multiplicatave_operator!( Fixed, Div, div, Self );
|
|
|
|
|
|
|
|
|
|
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>{
|
|
|
|
|
impl<const N:usize,const F:usize> core::ops::$trait<u32> for $struct<N,F>{
|
|
|
|
|
type Output = $output;
|
|
|
|
|
|
|
|
|
|
fn $method(self, other: u32) -> Self::Output {
|
|
|
|
@ -266,7 +265,7 @@ macro_rules! impl_shift_operator {
|
|
|
|
|
}
|
|
|
|
|
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>{
|
|
|
|
|
impl<const N:usize,const F:usize> core::ops::$trait<u32> for $struct<N,F>{
|
|
|
|
|
fn $method(&mut self, other: u32) {
|
|
|
|
|
self.bits.$method(other);
|
|
|
|
|
}
|
|
|
|
@ -283,13 +282,10 @@ impl_shift_operator!( Fixed, Shr, shr, Self );
|
|
|
|
|
// let b:I64F64 = a.wide_mul(a);
|
|
|
|
|
macro_rules! impl_wide_mul{
|
|
|
|
|
($lhs:expr,$rhs:expr)=>{
|
|
|
|
|
impl<A> Fixed<$lhs,A>
|
|
|
|
|
impl Fixed<$lhs,{$lhs*32}>
|
|
|
|
|
{
|
|
|
|
|
paste::item!{
|
|
|
|
|
pub fn [<wide_mul_ $lhs _ $rhs>]<B>(self,rhs:Fixed<$rhs,B>)->Fixed<{$lhs+$rhs},Sum<A,B>>
|
|
|
|
|
where
|
|
|
|
|
A:std::ops::Add<B>,
|
|
|
|
|
B:Unsigned,{
|
|
|
|
|
pub fn [<wide_mul_ $lhs _ $rhs>](self,rhs:Fixed<$rhs,{$rhs*32}>)->Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>{
|
|
|
|
|
Fixed::from_bits(self.bits.as_::<BInt<{$lhs+$rhs}>>()*rhs.bits.as_::<BInt<{$lhs+$rhs}>>())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -316,15 +312,15 @@ impl_wide_mul_all!(
|
|
|
|
|
(1,7),(2,7),(3,7),(4,7),(5,7),(6,7),(7,7),(8,7),
|
|
|
|
|
(1,8),(2,8),(3,8),(4,8),(5,8),(6,8),(7,8),(8,8)
|
|
|
|
|
);
|
|
|
|
|
impl<const SRC:usize,Frac> Fixed<SRC,Frac>{
|
|
|
|
|
pub fn resize_into<const DST:usize>(self)->Fixed<DST,Frac>{
|
|
|
|
|
impl<const SRC:usize,const F:usize> Fixed<SRC,F>{
|
|
|
|
|
pub fn resize_into<const DST:usize>(self)->Fixed<DST,F>{
|
|
|
|
|
Fixed::from_bits(self.bits.as_::<BInt<DST>>())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! impl_const{
|
|
|
|
|
($n:expr)=>{
|
|
|
|
|
impl<F:Unsigned+std::ops::Add> Fixed<$n,F>{
|
|
|
|
|
impl Fixed<$n,{$n*32}>{
|
|
|
|
|
paste::item!{
|
|
|
|
|
pub fn sqrt_unchecked(self)->Self{
|
|
|
|
|
//1<<max_shift must be the minimum power of two which when squared is greater than self
|
|
|
|
@ -333,8 +329,8 @@ macro_rules! impl_const{
|
|
|
|
|
//2. divide by 2 via >>1 (sqrt-ish)
|
|
|
|
|
//3. add on fractional offset
|
|
|
|
|
//Voila
|
|
|
|
|
let used_bits=self.bits.bits() as i32-1-F::I32;
|
|
|
|
|
let max_shift=((used_bits>>1)+F::I32) as u32;
|
|
|
|
|
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)
|
|
|
|
|