improve sqrt

This commit is contained in:
Quaternions 2024-08-29 16:24:13 -07:00
parent 75bd98ce19
commit e38126302e

View File

@ -455,21 +455,18 @@ impl Planar64{
pub fn sqrt(&self)->Self{ pub fn sqrt(&self)->Self{
const BITS:i32=64; const BITS:i32=64;
const FRAC:i32=32; const FRAC:i32=32;
let mut pow2=Self::raw(1i64<<(((BITS-FRAC-(self.0.leading_zeros() as i32)+1)>>1)+FRAC).saturating_sub(1)); let pow=(((BITS-FRAC-(self.0.leading_zeros() as i32)+1)>>1)+FRAC)-1;
let mut result=pow2; let mut result=Self::raw(1<<pow);
let wide_self=(self.0 as i128)<<FRAC; let wide_self=(self.0 as i128)<<FRAC;
loop{ for i in (0..=pow).rev(){
if pow2==Self::ZERO{ let new_result=result+Self::raw(1<<i);
break result;
}
let new_result=result+pow2;
match wide_self.cmp(&((new_result.0 as i128)*(new_result.0 as i128))){ match wide_self.cmp(&((new_result.0 as i128)*(new_result.0 as i128))){
core::cmp::Ordering::Less=>(), core::cmp::Ordering::Less=>(),
core::cmp::Ordering::Equal=>break new_result, core::cmp::Ordering::Equal=>return new_result,
core::cmp::Ordering::Greater=>result=new_result, core::cmp::Ordering::Greater=>result=new_result,
} }
pow2=Self::raw(pow2.0>>1);
} }
result
} }
#[inline] #[inline]
pub const fn signum_i64(&self)->i64{ pub const fn signum_i64(&self)->i64{