use easily optimisable division for truncate

This commit is contained in:
Corwin Kuiper 2021-06-06 18:27:15 +01:00
parent eb212e7f91
commit 900b594039

View file

@ -222,14 +222,7 @@ impl<I: FixedWidthUnsignedInteger, const N: usize> Num<I, N> {
} }
pub fn trunc(&self) -> I { pub fn trunc(&self) -> I {
let fractional_part = self.0 & ((I::one() << N) - I::one()); self.0 / (I::one() << N)
let self_as_int = self.0 >> N;
if self_as_int < I::zero() && fractional_part != I::zero() {
self_as_int + I::one()
} else {
self_as_int
}
} }
pub fn rem_euclid(&self, rhs: Self) -> Self { pub fn rem_euclid(&self, rhs: Self) -> Self {