From f32bd4a667367e9bf105a6501ce47cc0e20d8a52 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Mon, 26 Aug 2024 15:40:27 -0700 Subject: [PATCH] bad operator impl --- fixed_wide/src/wide.rs | 134 +++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 72 deletions(-) diff --git a/fixed_wide/src/wide.rs b/fixed_wide/src/wide.rs index 114c673..09cb448 100644 --- a/fixed_wide/src/wide.rs +++ b/fixed_wide/src/wide.rs @@ -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{ bits:BInt<{CHUNKS}>, frac:PhantomData, } -pub type FixedI192=Fixed<3,Frac>; -pub type FixedI256=Fixed<4,Frac>; -pub type FixedI512=Fixed<8,Frac>; -impl From for Fixed{ - fn from(value:i128)->Self{ +impl From for Fixed + where + BInt:From +{ + fn from(value:T)->Self{ Self{ bits:BInt::<{CHUNKS}>::from(value)< PartialEq for Fixed{ } impl Eq for Fixed{} -impl std::ops::Add for Fixed{ - type Output=Self; - fn add(self,rhs:Self)->Self::Output{ - Self{ - bits:self.bits+rhs.bits, - frac:PhantomData, - } - } +macro_rules! impl_operator { + ( $struct: ident, $trait: ident, $method: ident, $output: ty ) => { + impl core::ops::$trait for $struct + where + $struct:From + { + type Output = $output; + + fn $method(self, other: T) -> Self::Output { + Self { + bits:self.bits.$method($struct::::from(other).bits), + frac:PhantomData, + } + } + } + }; } +macro_rules! impl_assign_operator { + ( $struct: ident, $trait: ident, $method: ident ) => { + impl core::ops::$trait for $struct + where + $struct:From + { + fn $method(&mut self, other: T) { + self.bits.$method($struct::::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{ type Output; fn wide_mul(self,rhs:Rhs)->Self::Output; } -//going wide from foreign fixed type -impl WideMul> for FixedI64 - where - A:std::ops::Add, - B:Unsigned, -{ - type Output=FixedI128>; - fn wide_mul(self,rhs:FixedI64)->Self::Output{ - self.wide_mul(rhs) - } -} -impl WideMul> for FixedI128 - where - A:std::ops::Add, - B:Unsigned, -{ - type Output=FixedI192>; - fn wide_mul(self,rhs:FixedI64)->Self::Output{ - FixedI192{ - bits:BInt::<3>::from(self.to_bits())*BInt::<3>::from(rhs.to_bits()), - frac:PhantomData, - } - } -} -impl WideMul> for FixedI64 - where - A:std::ops::Add, - B:Unsigned, -{ - type Output=FixedI192>; - fn wide_mul(self,rhs:FixedI128)->Self::Output{ - FixedI192{ - bits:BInt::<3>::from(self.to_bits())*BInt::<3>::from(rhs.to_bits()), - frac:PhantomData, - } - } -} -impl WideMul> for FixedI128 - where - A:std::ops::Add, - B:Unsigned, -{ - type Output=FixedI256>; - fn wide_mul(self,rhs:FixedI128)->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);