save one shr operation
This commit is contained in:
parent
95651d7091
commit
67c30b8535
@ -70,15 +70,14 @@ impl<const CHUNKS:usize,Frac:Unsigned> Fixed<CHUNKS,Frac>
|
|||||||
//0110.0000
|
//0110.0000
|
||||||
//pow2 = 0100.0000
|
//pow2 = 0100.0000
|
||||||
let mut pow2=Self{
|
let mut pow2=Self{
|
||||||
bits:BInt::<CHUNKS>::ONE.shl((((CHUNKS as i32*64-Frac::I32-(self.bits.leading_zeros() as i32)+1)>>1)+Frac::I32) as u32),
|
bits:BInt::<CHUNKS>::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,
|
frac:PhantomData,
|
||||||
};
|
};
|
||||||
let mut result=pow2>>1;
|
let mut result=pow2;
|
||||||
|
|
||||||
//cheat to make the types match
|
//cheat to make the types match
|
||||||
let wide_self=self.wide_mul(Fixed::<CHUNKS,Frac>::ONE);
|
let wide_self=self.wide_mul(Fixed::<CHUNKS,Frac>::ONE);
|
||||||
loop{
|
loop{
|
||||||
pow2>>=1;
|
|
||||||
if pow2==Self::ZERO{
|
if pow2==Self::ZERO{
|
||||||
break result;
|
break result;
|
||||||
}
|
}
|
||||||
@ -87,10 +86,11 @@ impl<const CHUNKS:usize,Frac:Unsigned> Fixed<CHUNKS,Frac>
|
|||||||
//note that the implicit truncation in the multiply
|
//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.
|
//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)){
|
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::Equal=>break new_result,
|
||||||
core::cmp::Ordering::Greater=>result=new_result,
|
core::cmp::Ordering::Greater=>result=new_result,
|
||||||
}
|
}
|
||||||
|
pow2>>=1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn sqrt(self)->Self{
|
pub fn sqrt(self)->Self{
|
||||||
|
Loading…
Reference in New Issue
Block a user