use super::{CubicRoot, SquareRoot};
use crate::{CubicRootRem, NormalizedRootRem, SquareRootRem};
impl SquareRoot for u8 {
type Output = u8;
#[inline]
fn sqrt(&self) -> Self::Output {
self.sqrt_rem().0
}
}
impl CubicRoot for u8 {
type Output = u8;
#[inline]
fn cbrt(&self) -> Self::Output {
self.cbrt_rem().0
}
}
macro_rules! impl_root_using_rootrem {
($t:ty, $half:ty) => {
impl SquareRoot for $t {
type Output = $half;
#[inline]
fn sqrt(&self) -> $half {
if *self == 0 {
return 0;
}
let shift = self.leading_zeros() & !1; let (root, _) = (self << shift).normalized_sqrt_rem();
root >> (shift / 2)
}
}
impl CubicRoot for $t {
type Output = $half;
#[inline]
fn cbrt(&self) -> $half {
if *self == 0 {
return 0;
}
let mut shift = self.leading_zeros();
shift -= shift % 3; let (root, _) = (self << shift).normalized_cbrt_rem();
root >> (shift / 3)
}
}
};
}
impl_root_using_rootrem!(u16, u8);
impl_root_using_rootrem!(u32, u16);
impl_root_using_rootrem!(u64, u32);
impl_root_using_rootrem!(u128, u64);