|
|
@ -1,6 +1,7 @@
|
|
|
|
use bnum::{BInt,cast::As};
|
|
|
|
use bnum::{BInt,cast::As};
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone,Copy,Debug,Hash)]
|
|
|
|
#[derive(Clone,Copy,Debug,Hash)]
|
|
|
|
|
|
|
|
/// A Fixed point number for which multiply operations widen the bits in the output. (when the wide-mul feature is enabled)
|
|
|
|
/// N is the number of u64s to use
|
|
|
|
/// N is the number of u64s to use
|
|
|
|
/// F is the number of fractional bits (always N*32 lol)
|
|
|
|
/// F is the number of fractional bits (always N*32 lol)
|
|
|
|
pub struct Fixed<const N:usize,const F:usize>{
|
|
|
|
pub struct Fixed<const N:usize,const F:usize>{
|
|
|
@ -139,6 +140,10 @@ impl_additive_operator!( Fixed, BitOr, bitor, Self );
|
|
|
|
impl_additive_assign_operator!( Fixed, BitXorAssign, bitxor_assign );
|
|
|
|
impl_additive_assign_operator!( Fixed, BitXorAssign, bitxor_assign );
|
|
|
|
impl_additive_operator!( Fixed, BitXor, bitxor, Self );
|
|
|
|
impl_additive_operator!( Fixed, BitXor, bitxor, Self );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// non-wide operators. The result is the same width as the inputs.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This macro is not used in the default configuration.
|
|
|
|
|
|
|
|
#[allow(unused_macros)]
|
|
|
|
macro_rules! impl_multiplicative_operator_not_const_generic {
|
|
|
|
macro_rules! impl_multiplicative_operator_not_const_generic {
|
|
|
|
( ($struct: ident, $trait: ident, $method: ident, $output: ty ), $width:expr ) => {
|
|
|
|
( ($struct: ident, $trait: ident, $method: ident, $output: ty ), $width:expr ) => {
|
|
|
|
impl<const F:usize> core::ops::$trait for $struct<$width,F>{
|
|
|
|
impl<const F:usize> core::ops::$trait for $struct<$width,F>{
|
|
|
@ -175,6 +180,7 @@ macro_rules! impl_multiply_operator_not_const_generic {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature="wide-mul"))]
|
|
|
|
impl_multiplicative_operator_not_const_generic!(($struct, $trait, $method, $output ), $width);
|
|
|
|
impl_multiplicative_operator_not_const_generic!(($struct, $trait, $method, $output ), $width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -191,6 +197,7 @@ macro_rules! impl_divide_operator_not_const_generic {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(not(feature="wide-mul"),not(feature="ratio")))]
|
|
|
|
impl_multiplicative_operator_not_const_generic!(($struct, $trait, $method, $output ), $width);
|
|
|
|
impl_multiplicative_operator_not_const_generic!(($struct, $trait, $method, $output ), $width);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -248,6 +255,15 @@ impl_multiplicative_assign_operator!( Fixed, MulAssign, mul_assign );
|
|
|
|
impl_multiplicative_operator!( Fixed, Mul, mul, Self );
|
|
|
|
impl_multiplicative_operator!( Fixed, Mul, mul, Self );
|
|
|
|
impl_multiplicative_assign_operator!( Fixed, DivAssign, div_assign );
|
|
|
|
impl_multiplicative_assign_operator!( Fixed, DivAssign, div_assign );
|
|
|
|
impl_multiplicative_operator!( Fixed, Div, div, Self );
|
|
|
|
impl_multiplicative_operator!( Fixed, Div, div, Self );
|
|
|
|
|
|
|
|
#[cfg(feature="ratio")]
|
|
|
|
|
|
|
|
impl<const LHS_N:usize,const LHS_F:usize,const RHS_N:usize,const RHS_F:usize> core::ops::Div<Fixed<RHS_N,RHS_F>> for Fixed<LHS_N,LHS_F>{
|
|
|
|
|
|
|
|
type Output=crate::ratio::Ratio<Fixed<LHS_N,LHS_F>,Fixed<RHS_N,RHS_F>>;
|
|
|
|
|
|
|
|
fn div(self, other: Fixed<RHS_N,RHS_F>)->Self::Output{
|
|
|
|
|
|
|
|
crate::ratio::Ratio::new(self,other)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// wide operators. The result width is the sum of the input widths, i.e. none of the multiplication
|
|
|
|
|
|
|
|
|
|
|
|
macro_rules! impl_shift_operator {
|
|
|
|
macro_rules! impl_shift_operator {
|
|
|
|
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
|
|
|
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
|
|
@ -274,6 +290,28 @@ impl_shift_operator!( Fixed, Shl, shl, Self );
|
|
|
|
impl_shift_assign_operator!( Fixed, ShrAssign, shr_assign );
|
|
|
|
impl_shift_assign_operator!( Fixed, ShrAssign, shr_assign );
|
|
|
|
impl_shift_operator!( Fixed, Shr, shr, Self );
|
|
|
|
impl_shift_operator!( Fixed, Shr, shr, Self );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
macro_rules! impl_wide_operators{
|
|
|
|
|
|
|
|
($lhs:expr,$rhs:expr)=>{
|
|
|
|
|
|
|
|
impl core::ops::Mul<Fixed<$rhs,{$rhs*32}>> for Fixed<$lhs,{$lhs*32}>{
|
|
|
|
|
|
|
|
type Output=Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>;
|
|
|
|
|
|
|
|
fn mul(self, other: Fixed<$rhs,{$rhs*32}>)->Self::Output{
|
|
|
|
|
|
|
|
paste::item!{
|
|
|
|
|
|
|
|
self.[<wide_mul_ $lhs _ $rhs>](other)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature="ratio"))]
|
|
|
|
|
|
|
|
impl core::ops::Div<Fixed<$rhs,{$rhs*32}>> for Fixed<$lhs,{$lhs*32}>{
|
|
|
|
|
|
|
|
type Output=Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>;
|
|
|
|
|
|
|
|
fn div(self, other: Fixed<$rhs,{$rhs*32}>)->Self::Output{
|
|
|
|
|
|
|
|
paste::item!{
|
|
|
|
|
|
|
|
self.[<wide_div_ $lhs _ $rhs>](other)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WIDE MUL: multiply into a wider type
|
|
|
|
// WIDE MUL: multiply into a wider type
|
|
|
|
// let a = I32F32::ONE;
|
|
|
|
// let a = I32F32::ONE;
|
|
|
|
// let b:I64F64 = a.wide_mul(a);
|
|
|
|
// let b:I64F64 = a.wide_mul(a);
|
|
|
@ -301,6 +339,8 @@ macro_rules! impl_wide_not_const_generic{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature="wide-mul")]
|
|
|
|
|
|
|
|
impl_wide_operators!($lhs,$rhs);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|