From 2dbc60f3e071f8516da5b694411c0b5883a79650 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Wed, 28 Oct 2020 16:17:52 +0100 Subject: [PATCH] fixed next_pow2 for 0 --- src/utils.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index a3ffff9cb3..36da82184c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -89,13 +89,13 @@ impl Xoshiro256StarRand { /// Get the next higher power of two pub fn next_pow2(val: u64) -> u64 { - let mut out: u64 = val.wrapping_sub(1); + let mut out = val.wrapping_sub(1); out |= out >> 1; out |= out >> 2; out |= out >> 4; out |= out >> 8; out |= out >> 16; - return out + 1; + out.wrapping_add(1) } #[cfg(test)] @@ -119,5 +119,6 @@ mod tests { assert_eq!(next_pow2(2), 2); assert_eq!(next_pow2(3), 4); assert_eq!(next_pow2(1000), 1024); + assert_eq!(next_pow2(u32::MAX as u64), (u32::MAX as u64) + 1); } }