about summary refs log tree commit diff
path: root/src/libstd/num
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-04-19 04:26:28 -0700
committerbors <bors@rust-lang.org>2014-04-19 04:26:28 -0700
commit2c22ae4378d5f9fc32e2f234121ea98db86201df (patch)
tree3e114b5106f871865538fd83d106e686b32f3ff6 /src/libstd/num
parentc7553ea17359cad2c8d30855380f78026127f7b7 (diff)
parent9f45484db5cc4570b4e489e82646efc5c628b6ea (diff)
downloadrust-2c22ae4378d5f9fc32e2f234121ea98db86201df.tar.gz
rust-2c22ae4378d5f9fc32e2f234121ea98db86201df.zip
auto merge of #13614 : cgaebel/rust/master, r=brson
We previously allocated 3x for every HashMap creation and resize. This patch reduces it to 1x.
Diffstat (limited to 'src/libstd/num')
-rw-r--r--src/libstd/num/mod.rs6
1 files changed, 6 insertions, 0 deletions
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<T: Unsigned + Int>(n: T) -> T {
     tmp + one()
 }
 
+// Returns `true` iff `n == 2^k` for some k.
+#[inline]
+pub fn is_power_of_two<T: Unsigned + Int>(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`.