From 67c30b85358e9e3817a82670926a147492d5fa1c Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 29 Aug 2024 15:21:10 -0700 Subject: [PATCH] save one shr operation --- fixed_wide/src/fixed_wide_traits.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fixed_wide/src/fixed_wide_traits.rs b/fixed_wide/src/fixed_wide_traits.rs index f8b0d5c..566d396 100644 --- a/fixed_wide/src/fixed_wide_traits.rs +++ b/fixed_wide/src/fixed_wide_traits.rs @@ -70,15 +70,14 @@ impl Fixed //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), + 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 mut result=pow2>>1; + let mut result=pow2; //cheat to make the types match let wide_self=self.wide_mul(Fixed::::ONE); loop{ - pow2>>=1; if pow2==Self::ZERO{ break result; } @@ -87,10 +86,11 @@ impl Fixed //note that the implicit truncation in the multiply //means that the algorithm can return a result which squares to a number greater than the input. match wide_self.cmp(&new_result.wide_mul(new_result)){ - core::cmp::Ordering::Less=>continue, + core::cmp::Ordering::Less=>(), core::cmp::Ordering::Equal=>break new_result, core::cmp::Ordering::Greater=>result=new_result, } + pow2>>=1; } } pub fn sqrt(self)->Self{