From 61aad93f8dea749f327372839fbd53c3f02f392d Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 29 Aug 2024 18:29:04 -0700 Subject: [PATCH] sqrt: closed loop over bit shift --- fixed_wide/src/fixed_wide_traits.rs | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/fixed_wide/src/fixed_wide_traits.rs b/fixed_wide/src/fixed_wide_traits.rs index d34419c..0c8e9d8 100644 --- a/fixed_wide/src/fixed_wide_traits.rs +++ b/fixed_wide/src/fixed_wide_traits.rs @@ -69,30 +69,18 @@ impl Fixed //sqrt //0110.0000 //pow2 = 0100.0000 - let mut pow2=Self{ - bits:BInt::::ONE.shl(((((CHUNKS as i32*64-Frac::I32-(self.bits.leading_zeros() as i32)+1)>>1)+Frac::I32) as u32).saturating_sub(1)), - frac:PhantomData, - }; + let max_shift=((((CHUNKS as i32*64-Frac::I32-(self.bits.leading_zeros() as i32)+1)>>1)+Frac::I32) as u32).saturating_sub(1); let mut result=Self::ZERO; //cheat to make the types match let wide_self=self.wide_mul(Fixed::::ONE); - loop{ - //TODO: closed loop over bit shift exponent rather than pow2 - if pow2==Self::ZERO{ - break result; + for shift in (0..=max_shift).rev(){ + let new_result=result|(Fixed::::EPSILON<(), - core::cmp::Ordering::Equal=>break new_result, - core::cmp::Ordering::Greater=>result=new_result, - } - pow2>>=1; } + result } pub fn sqrt(self)->Self{ if self