From 4d13b4ada7f428168e7ef9eaf6eb93da76200212 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Mon, 2 Sep 2024 16:35:01 -0700 Subject: [PATCH] paste (this sucks) --- fixed_wide/Cargo.lock | 7 +++++++ fixed_wide/Cargo.toml | 1 + fixed_wide/src/fixed.rs | 24 +++++++++++++----------- fixed_wide/src/lib.rs | 1 - fixed_wide/src/traits.rs | 8 -------- fixed_wide/src/zeroes.rs | 4 +++- 6 files changed, 24 insertions(+), 21 deletions(-) delete mode 100644 fixed_wide/src/traits.rs diff --git a/fixed_wide/Cargo.lock b/fixed_wide/Cargo.lock index e28b2bc..d8b81fb 100644 --- a/fixed_wide/Cargo.lock +++ b/fixed_wide/Cargo.lock @@ -20,9 +20,16 @@ version = "0.1.0" dependencies = [ "arrayvec", "bnum", + "paste", "typenum", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "typenum" version = "1.17.0" diff --git a/fixed_wide/Cargo.toml b/fixed_wide/Cargo.toml index 8b2b4e9..6b39bd1 100644 --- a/fixed_wide/Cargo.toml +++ b/fixed_wide/Cargo.toml @@ -12,3 +12,4 @@ zeroes=["ratio","dep:arrayvec"] bnum = "0.11.0" typenum = "1.17.0" arrayvec = { version = "0.7.6", optional = true } +paste = "1.0.15" diff --git a/fixed_wide/src/fixed.rs b/fixed_wide/src/fixed.rs index 4045ff1..a57135f 100644 --- a/fixed_wide/src/fixed.rs +++ b/fixed_wide/src/fixed.rs @@ -1,6 +1,5 @@ use bnum::{BInt,cast::As}; use typenum::{Sum,Unsigned}; -use crate::traits::WideMul; #[derive(Clone,Copy,Debug,Hash)] pub struct Fixed{ @@ -284,14 +283,15 @@ impl_shift_operator!( Fixed, Shr, shr, Self ); // let b:I64F64 = a.wide_mul(a); macro_rules! impl_wide_mul{ ($lhs:expr,$rhs:expr)=>{ - impl WideMul> for Fixed<$lhs,A> - where - A:std::ops::Add, - B:Unsigned, + impl Fixed<$lhs,A> { - type Output=Fixed<{$lhs+$rhs},Sum>; - fn wide_mul(self,rhs:Fixed<$rhs,B>)->Self::Output{ - Fixed::from_bits(self.bits.as_::>()*rhs.bits.as_::>()) + paste::item!{ + pub fn [](self,rhs:Fixed<$rhs,B>)->Fixed<{$lhs+$rhs},Sum> + where + A:std::ops::Add, + B:Unsigned,{ + Fixed::from_bits(self.bits.as_::>()*rhs.bits.as_::>()) + } } } }; @@ -324,7 +324,8 @@ impl Fixed{ macro_rules! impl_const{ ($n:expr)=>{ - impl Fixed<$n,F>{ + impl Fixed<$n,F>{ + paste::item!{ pub fn sqrt_unchecked(self)->Self{ //1<](Self::ONE); //descend down the bits and check if flipping each bit would push the square over the input value for shift in (0..=max_shift).rev(){ let new_result=result|Self::from_bits(BInt::from_bits(bnum::BUint::power_of_two(shift))); - if new_result.wide_mul(new_result)<=wide_self{ + if new_result.[](new_result)<=wide_self{ result=new_result; } } result } + } pub fn sqrt(self)->Self{ if self{ - type Output; - fn wide_mul(self,rhs:Rhs)->Self::Output; -} -pub trait WideDiv{ - type Output; - fn wide_div(self,rhs:Rhs)->Self::Output; -} diff --git a/fixed_wide/src/zeroes.rs b/fixed_wide/src/zeroes.rs index 60f5a47..8668dc5 100644 --- a/fixed_wide/src/zeroes.rs +++ b/fixed_wide/src/zeroes.rs @@ -11,6 +11,7 @@ macro_rules! impl_zeroes{ F:Unsigned+std::ops::Add, ::Output:Unsigned, { + paste::item!{ #[inline] pub fn zeroes2(a0:Self,a1:Self,a2:Self)->ArrayVec,2>{ let a2pos=match a2.cmp(&Self::ZERO){ @@ -18,7 +19,7 @@ macro_rules! impl_zeroes{ Ordering::Equal=>return ArrayVec::from_iter($crate::zeroes::zeroes1(a0,a1).into_iter()), Ordering::Less=>true, }; - let radicand=$crate::traits::WideMul::wide_mul(a1,a1)-$crate::traits::WideMul::wide_mul(a2,a0)*4; + let radicand=a1.[](a1)-a2.[](a0)*4; match radicand.cmp(&Fixed::<{$n*2},Sum>::ZERO){ Ordering::Greater=>{ //start with f64 sqrt @@ -37,6 +38,7 @@ macro_rules! impl_zeroes{ Ordering::Less=>ArrayVec::new_const(), } } + } #[inline] pub fn zeroes1(a0:Self,a1:Self)->ArrayVec,1>{ if a1==Self::ZERO{