Compare commits

...

7 Commits

Author SHA1 Message Date
14cec421e0 fix test 2024-09-09 15:33:10 -07:00
fb9228b7fd silence warning 2024-09-09 15:33:07 -07:00
3f0900b0ec ratio stuff 2024-09-09 15:25:01 -07:00
658c73a033 div op 2024-09-09 15:25:01 -07:00
b98eba27e5 wide operators 2024-09-09 15:25:01 -07:00
503f5b6d22 comment impl zones 2024-09-09 15:25:01 -07:00
f22cd653df wide-mul is a crate feature
here goes another bottom-up rewrite
2024-09-09 15:25:01 -07:00
3 changed files with 43 additions and 2 deletions

View File

@ -4,8 +4,9 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[features] [features]
default=["zeroes"] default=["zeroes","wide-mul"]
ratio=[] ratio=[]
wide-mul=[]
zeroes=["ratio","dep:arrayvec"] zeroes=["ratio","dep:arrayvec"]
[dependencies] [dependencies]

View File

@ -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);
}; };
} }

View File

@ -65,7 +65,7 @@ fn test_sqrt_zero(){
#[test] #[test]
fn test_sqrt_low(){ fn test_sqrt_low(){
let a=I32F32::HALF; let a=I32F32::HALF;
let b=a*a; let b=a.fixed_mul(a);
assert_eq!(b.sqrt(),a); assert_eq!(b.sqrt(),a);
} }
fn find_equiv_sqrt_via_f64(n:I32F32)->I32F32{ fn find_equiv_sqrt_via_f64(n:I32F32)->I32F32{