1use super::{CubicRoot, SquareRoot};
2use crate::{CubicRootRem, NormalizedRootRem, SquareRootRem};
3
4impl SquareRoot for u8 {
8 type Output = u8;
9
10 #[inline]
11 fn sqrt(&self) -> Self::Output {
12 self.sqrt_rem().0
13 }
14}
15
16impl CubicRoot for u8 {
17 type Output = u8;
18
19 #[inline]
20 fn cbrt(&self) -> Self::Output {
21 self.cbrt_rem().0
22 }
23}
24
25macro_rules! impl_root_using_rootrem {
26 ($t:ty, $half:ty) => {
27 impl SquareRoot for $t {
28 type Output = $half;
29
30 #[inline]
31 fn sqrt(&self) -> $half {
32 if *self == 0 {
33 return 0;
34 }
35
36 let shift = self.leading_zeros() & !1; let (root, _) = (self << shift).normalized_sqrt_rem();
39 root >> (shift / 2)
40 }
41 }
42
43 impl CubicRoot for $t {
44 type Output = $half;
45
46 #[inline]
47 fn cbrt(&self) -> $half {
48 if *self == 0 {
49 return 0;
50 }
51
52 let mut shift = self.leading_zeros();
54 shift -= shift % 3; let (root, _) = (self << shift).normalized_cbrt_rem();
56 root >> (shift / 3)
57 }
58 }
59 };
60}
61
62impl_root_using_rootrem!(u16, u8);
63impl_root_using_rootrem!(u32, u16);
64impl_root_using_rootrem!(u64, u32);
65impl_root_using_rootrem!(u128, u64);