diff options
| author | Irfan Hudda <irfanhudda@gmail.com> | 2017-04-28 21:50:02 +0530 |
|---|---|---|
| committer | Irfan Hudda <irfanhudda@gmail.com> | 2017-04-28 21:50:02 +0530 |
| commit | 8f9caff988954ab19f4a242d905aa6092a03bddd (patch) | |
| tree | 5e759fbe963808475dd2ad0ec674855c3db7dfed | |
| parent | f7bd370818b003f57e47a1894ff0d791ad23628d (diff) | |
| download | rust-8f9caff988954ab19f4a242d905aa6092a03bddd.tar.gz rust-8f9caff988954ab19f4a242d905aa6092a03bddd.zip | |
Add helper functions for `next_power_of_two` and `checked_next_power_of_two`
| -rw-r--r-- | src/libcore/num/mod.rs | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index bef7a20cd7f..0c7832168bb 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -2327,6 +2327,18 @@ macro_rules! uint_impl { (self.wrapping_sub(1)) & self == 0 && !(self == 0) } + fn round_up_to_one_less_than_a_power_of_two(self) -> Self { + let bits = size_of::<Self>() as u32 * 8; + let z = self.leading_zeros(); + (if z == bits { 0 as Self } else { !0 }).wrapping_shr(z) + } + + fn one_less_than_next_power_of_two(self) -> Self { + self.wrapping_sub(1) + .round_up_to_one_less_than_a_power_of_two() + .wrapping_add(if self == 0 { 1 } else { 0 }) + } + /// Returns the smallest power of two greater than or equal to `self`. /// More details about overflow behavior can be found in [RFC 560]. /// @@ -2343,13 +2355,7 @@ macro_rules! uint_impl { #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn next_power_of_two(self) -> Self { - let bits = size_of::<Self>() * 8; - let one: Self = 1; - if self == 0 { - 1 - } else { - one << (bits - (self - one).leading_zeros() as usize) - } + self.one_less_than_next_power_of_two() + 1 } /// Returns the smallest power of two greater than or equal to `n`. If @@ -2367,9 +2373,7 @@ macro_rules! uint_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn checked_next_power_of_two(self) -> Option<Self> { - let bits = size_of::<Self>() * 8; - let one: Self = 1; - let npot = one << ((bits - self.wrapping_sub(one).leading_zeros() as usize) % bits); + let npot = self.one_less_than_next_power_of_two().wrapping_add(1); if npot >= self { Some(npot) } else { |
