From 35c996054a42499f2195ad1945658389812f550a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Cassiers?= Date: Mon, 22 Jun 2015 16:49:00 +0200 Subject: [PATCH] Use wrapping operations for bit-level functions This handles a underflow panic on round_up_to_power_of_two(0). --- src/utils.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 5e7f8baf1dc..5bcf16ea36b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -119,26 +119,26 @@ pub fn format_visibility(vis: Visibility) -> &'static str { // Based on the trick layed out at // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 pub fn round_up_to_power_of_two(mut x: usize) -> usize { - x -= 1; + x = x.wrapping_sub(1); x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x |= x >> 32; - x + 1 + x.wrapping_add(1) } #[inline] #[cfg(target_pointer_width="32")] pub fn round_up_to_power_of_two(mut x: usize) -> usize { - x -= 1; + x = x.wrapping_sub(1); x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; - x + 1 + x.wrapping_add(1) } // Macro for deriving implementations of Decodable for enums @@ -161,6 +161,7 @@ macro_rules! impl_enum_decodable { #[test] fn power_rounding() { + assert_eq!(0, round_up_to_power_of_two(0)); assert_eq!(1, round_up_to_power_of_two(1)); assert_eq!(64, round_up_to_power_of_two(33)); assert_eq!(256, round_up_to_power_of_two(256));