From 9f45484db5cc4570b4e489e82646efc5c628b6ea Mon Sep 17 00:00:00 2001 From: Clark Gaebel Date: Wed, 26 Mar 2014 21:58:08 -0400 Subject: Reduce HashMap allocations. --- src/libstd/num/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/libstd') diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index 12befed743a..b21a80cfd23 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -311,6 +311,12 @@ pub fn next_power_of_two(n: T) -> T { tmp + one() } +// Returns `true` iff `n == 2^k` for some k. +#[inline] +pub fn is_power_of_two(n: T) -> bool { + (n - one()) & n == zero() +} + /// Returns the smallest power of 2 greater than or equal to `n`. If the next /// power of two is greater than the type's maximum value, `None` is returned, /// otherwise the power of 2 is wrapped in `Some`. -- cgit 1.4.1-3-g733a5