From 4b8c211629436a3a72f62fdf5c201a4e195550f3 Mon Sep 17 00:00:00 2001 From: theoretical Date: Fri, 30 Jan 2015 12:54:30 -0500 Subject: [PATCH] uint128: Check for large shift count before truncating in << and >> - Correctly handle very large shift counts - uint128(1) << (uint64_t(1) << 32) should now be 0 as expected --- src/uint128.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/uint128.cpp b/src/uint128.cpp index b51d20e..e834fba 100644 --- a/src/uint128.cpp +++ b/src/uint128.cpp @@ -150,15 +150,14 @@ namespace fc uint128& uint128::operator<<=(const uint128& rhs) { - unsigned int n = rhs.to_integer(); - - if(n >= 128) + if(rhs >= 128) { hi = 0; lo = 0; } else { + unsigned int n = rhs.to_integer(); const unsigned int halfsize = 128 / 2; if(n >= halfsize){ @@ -181,17 +180,19 @@ namespace fc } } - return *this; + return *this; } uint128 & uint128::operator>>=(const uint128& rhs) { - unsigned int n = rhs.to_integer(); - - if(n >= 128) { + if(rhs >= 128) + { hi = 0; lo = 0; - } else { + } + else + { + unsigned int n = rhs.to_integer(); const unsigned int halfsize = 128 / 2; if(n >= halfsize) {