bad operator impl
This commit is contained in:
parent
5175a2ea18
commit
f32bd4a667
@ -1,7 +1,5 @@
|
||||
use bnum::cast::As;
|
||||
use bnum::types::{I256,I512};
|
||||
use bnum::BInt;
|
||||
use fixed::{FixedI64,FixedI128};
|
||||
use typenum::{Sum,Unsigned};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
@ -10,12 +8,12 @@ pub struct Fixed<const CHUNKS:usize,Frac>{
|
||||
bits:BInt<{CHUNKS}>,
|
||||
frac:PhantomData<Frac>,
|
||||
}
|
||||
pub type FixedI192<Frac>=Fixed<3,Frac>;
|
||||
pub type FixedI256<Frac>=Fixed<4,Frac>;
|
||||
pub type FixedI512<Frac>=Fixed<8,Frac>;
|
||||
|
||||
impl<const CHUNKS:usize,FracDst:Unsigned> From<i128> for Fixed<CHUNKS,FracDst>{
|
||||
fn from(value:i128)->Self{
|
||||
impl<const CHUNKS:usize,FracDst:Unsigned,T> From<T> for Fixed<CHUNKS,FracDst>
|
||||
where
|
||||
BInt<CHUNKS>:From<T>
|
||||
{
|
||||
fn from(value:T)->Self{
|
||||
Self{
|
||||
bits:BInt::<{CHUNKS}>::from(value)<<FracDst::U32,
|
||||
frac:PhantomData,
|
||||
@ -30,72 +28,61 @@ impl<const CHUNKS:usize,Frac> PartialEq for Fixed<CHUNKS,Frac>{
|
||||
}
|
||||
impl<const CHUNKS:usize,Frac> Eq for Fixed<CHUNKS,Frac>{}
|
||||
|
||||
impl<const CHUNKS:usize,Frac> std::ops::Add for Fixed<CHUNKS,Frac>{
|
||||
type Output=Self;
|
||||
fn add(self,rhs:Self)->Self::Output{
|
||||
macro_rules! impl_operator {
|
||||
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
||||
impl<const CHUNKS:usize,Frac,T> core::ops::$trait<T> for $struct<CHUNKS,Frac>
|
||||
where
|
||||
$struct<CHUNKS,Frac>:From<T>
|
||||
{
|
||||
type Output = $output;
|
||||
|
||||
fn $method(self, other: T) -> Self::Output {
|
||||
Self {
|
||||
bits:self.bits+rhs.bits,
|
||||
bits:self.bits.$method($struct::<CHUNKS,Frac>::from(other).bits),
|
||||
frac:PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
macro_rules! impl_assign_operator {
|
||||
( $struct: ident, $trait: ident, $method: ident ) => {
|
||||
impl<const CHUNKS:usize,Frac,T> core::ops::$trait<T> for $struct<CHUNKS,Frac>
|
||||
where
|
||||
$struct<CHUNKS,Frac>:From<T>
|
||||
{
|
||||
fn $method(&mut self, other: T) {
|
||||
self.bits.$method($struct::<CHUNKS,Frac>::from(other).bits);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// 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 );
|
||||
|
||||
pub trait WideMul<Rhs=Self>{
|
||||
type Output;
|
||||
fn wide_mul(self,rhs:Rhs)->Self::Output;
|
||||
}
|
||||
|
||||
//going wide from foreign fixed type
|
||||
impl<A,B> WideMul<FixedI64<B>> for FixedI64<A>
|
||||
where
|
||||
A:std::ops::Add<B>,
|
||||
B:Unsigned,
|
||||
{
|
||||
type Output=FixedI128<Sum<A,B>>;
|
||||
fn wide_mul(self,rhs:FixedI64<B>)->Self::Output{
|
||||
self.wide_mul(rhs)
|
||||
}
|
||||
}
|
||||
impl<A,B> WideMul<FixedI64<B>> for FixedI128<A>
|
||||
where
|
||||
A:std::ops::Add<B>,
|
||||
B:Unsigned,
|
||||
{
|
||||
type Output=FixedI192<Sum<A,B>>;
|
||||
fn wide_mul(self,rhs:FixedI64<B>)->Self::Output{
|
||||
FixedI192{
|
||||
bits:BInt::<3>::from(self.to_bits())*BInt::<3>::from(rhs.to_bits()),
|
||||
frac:PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<A,B> WideMul<FixedI128<B>> for FixedI64<A>
|
||||
where
|
||||
A:std::ops::Add<B>,
|
||||
B:Unsigned,
|
||||
{
|
||||
type Output=FixedI192<Sum<A,B>>;
|
||||
fn wide_mul(self,rhs:FixedI128<B>)->Self::Output{
|
||||
FixedI192{
|
||||
bits:BInt::<3>::from(self.to_bits())*BInt::<3>::from(rhs.to_bits()),
|
||||
frac:PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<A,B> WideMul<FixedI128<B>> for FixedI128<A>
|
||||
where
|
||||
A:std::ops::Add<B>,
|
||||
B:Unsigned,
|
||||
{
|
||||
type Output=FixedI256<Sum<A,B>>;
|
||||
fn wide_mul(self,rhs:FixedI128<B>)->Self::Output{
|
||||
FixedI256{
|
||||
bits:I256::from(self.to_bits())*I256::from(rhs.to_bits()),
|
||||
frac:PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//going wider native
|
||||
macro_rules! impl_wide_mul {
|
||||
($lhs: expr,$rhs: expr) => {
|
||||
@ -114,9 +101,12 @@ macro_rules! impl_wide_mul {
|
||||
}
|
||||
};
|
||||
}
|
||||
impl_wide_mul!(3,3);impl_wide_mul!(4,3);impl_wide_mul!(5,3);impl_wide_mul!(6,3);impl_wide_mul!(7,3);impl_wide_mul!(8,3);
|
||||
impl_wide_mul!(3,4);impl_wide_mul!(4,4);impl_wide_mul!(5,4);impl_wide_mul!(6,4);impl_wide_mul!(7,4);impl_wide_mul!(8,4);
|
||||
impl_wide_mul!(3,5);impl_wide_mul!(4,5);impl_wide_mul!(5,5);impl_wide_mul!(6,5);impl_wide_mul!(7,5);impl_wide_mul!(8,5);
|
||||
impl_wide_mul!(3,6);impl_wide_mul!(4,6);impl_wide_mul!(5,6);impl_wide_mul!(6,6);impl_wide_mul!(7,6);impl_wide_mul!(8,6);
|
||||
impl_wide_mul!(3,7);impl_wide_mul!(4,7);impl_wide_mul!(5,7);impl_wide_mul!(6,7);impl_wide_mul!(7,7);impl_wide_mul!(8,7);
|
||||
impl_wide_mul!(3,8);impl_wide_mul!(4,8);impl_wide_mul!(5,8);impl_wide_mul!(6,8);impl_wide_mul!(7,8);impl_wide_mul!(8,8);
|
||||
//const generics sidestepped wahoo
|
||||
impl_wide_mul!(1,1);impl_wide_mul!(2,1);impl_wide_mul!(3,1);impl_wide_mul!(4,1);impl_wide_mul!(5,1);impl_wide_mul!(6,1);impl_wide_mul!(7,1);impl_wide_mul!(8,1);
|
||||
impl_wide_mul!(1,2);impl_wide_mul!(2,2);impl_wide_mul!(3,2);impl_wide_mul!(4,2);impl_wide_mul!(5,2);impl_wide_mul!(6,2);impl_wide_mul!(7,2);impl_wide_mul!(8,2);
|
||||
impl_wide_mul!(1,3);impl_wide_mul!(2,3);impl_wide_mul!(3,3);impl_wide_mul!(4,3);impl_wide_mul!(5,3);impl_wide_mul!(6,3);impl_wide_mul!(7,3);impl_wide_mul!(8,3);
|
||||
impl_wide_mul!(1,4);impl_wide_mul!(2,4);impl_wide_mul!(3,4);impl_wide_mul!(4,4);impl_wide_mul!(5,4);impl_wide_mul!(6,4);impl_wide_mul!(7,4);impl_wide_mul!(8,4);
|
||||
impl_wide_mul!(1,5);impl_wide_mul!(2,5);impl_wide_mul!(3,5);impl_wide_mul!(4,5);impl_wide_mul!(5,5);impl_wide_mul!(6,5);impl_wide_mul!(7,5);impl_wide_mul!(8,5);
|
||||
impl_wide_mul!(1,6);impl_wide_mul!(2,6);impl_wide_mul!(3,6);impl_wide_mul!(4,6);impl_wide_mul!(5,6);impl_wide_mul!(6,6);impl_wide_mul!(7,6);impl_wide_mul!(8,6);
|
||||
impl_wide_mul!(1,7);impl_wide_mul!(2,7);impl_wide_mul!(3,7);impl_wide_mul!(4,7);impl_wide_mul!(5,7);impl_wide_mul!(6,7);impl_wide_mul!(7,7);impl_wide_mul!(8,7);
|
||||
impl_wide_mul!(1,8);impl_wide_mul!(2,8);impl_wide_mul!(3,8);impl_wide_mul!(4,8);impl_wide_mul!(5,8);impl_wide_mul!(6,8);impl_wide_mul!(7,8);impl_wide_mul!(8,8);
|
||||
|
Loading…
Reference in New Issue
Block a user