wide-mul crate feature
This commit is contained in:
parent
8ee6204a42
commit
206e219467
@ -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]
|
||||||
|
@ -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,13 @@ 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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 +288,30 @@ 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 );
|
||||||
|
|
||||||
|
// wide operators. The result width is the sum of the input widths, i.e. none of the multiplication
|
||||||
|
|
||||||
|
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);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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{
|
||||||
|
Loading…
Reference in New Issue
Block a user