diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index b32e4167da1d4..1fae362471d9c 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -586,14 +586,14 @@ macro_rules! int_impl { fn rotate_left(self, n: uint) -> $T { // Protect against undefined behaviour for over-long bit shifts let n = n % $BITS; - (self << n) | (self >> ($BITS - n)) + (self << n) | (self >> (($BITS - n) % $BITS)) } #[inline] fn rotate_right(self, n: uint) -> $T { // Protect against undefined behaviour for over-long bit shifts let n = n % $BITS; - (self >> n) | (self << ($BITS - n)) + (self >> n) | (self << (($BITS - n) % $BITS)) } #[inline] diff --git a/src/libcoretest/num/int_macros.rs b/src/libcoretest/num/int_macros.rs index 940b036ca907b..d078b51408512 100644 --- a/src/libcoretest/num/int_macros.rs +++ b/src/libcoretest/num/int_macros.rs @@ -114,6 +114,15 @@ mod tests { assert_eq!(_1.rotate_left(124), _1); assert_eq!(_0.rotate_right(124), _0); assert_eq!(_1.rotate_right(124), _1); + + // Rotating by 0 should have no effect + assert_eq!(A.rotate_left(0), A); + assert_eq!(B.rotate_left(0), B); + assert_eq!(C.rotate_left(0), C); + // Rotating by a multiple of word size should also have no effect + assert_eq!(A.rotate_left(64), A); + assert_eq!(B.rotate_left(64), B); + assert_eq!(C.rotate_left(64), C); } #[test] diff --git a/src/libcoretest/num/uint_macros.rs b/src/libcoretest/num/uint_macros.rs index 2272af67daf85..aefaa90520e79 100644 --- a/src/libcoretest/num/uint_macros.rs +++ b/src/libcoretest/num/uint_macros.rs @@ -74,6 +74,15 @@ mod tests { assert_eq!(_1.rotate_left(124), _1); assert_eq!(_0.rotate_right(124), _0); assert_eq!(_1.rotate_right(124), _1); + + // Rotating by 0 should have no effect + assert_eq!(A.rotate_left(0), A); + assert_eq!(B.rotate_left(0), B); + assert_eq!(C.rotate_left(0), C); + // Rotating by a multiple of word size should also have no effect + assert_eq!(A.rotate_left(64), A); + assert_eq!(B.rotate_left(64), B); + assert_eq!(C.rotate_left(64), C); } #[test]