Fix APInt::rotl and APInt::rotr so that they work correctly. Found while writing...
authorEli Friedman <eli.friedman@gmail.com>
Thu, 22 Dec 2011 03:15:35 +0000 (03:15 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Thu, 22 Dec 2011 03:15:35 +0000 (03:15 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@147134 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/APInt.cpp

index 143ded920e217b1c03ce2658b38f011521863554..b5411bec9d4d5380958dc2959250c508f0593f51 100644 (file)
@@ -1338,14 +1338,10 @@ APInt APInt::rotl(const APInt &rotateAmt) const {
 }
 
 APInt APInt::rotl(unsigned rotateAmt) const {
+  rotateAmt %= BitWidth;
   if (rotateAmt == 0)
     return *this;
-  // Don't get too fancy, just use existing shift/or facilities
-  APInt hi(*this);
-  APInt lo(*this);
-  hi.shl(rotateAmt);
-  lo.lshr(BitWidth - rotateAmt);
-  return hi | lo;
+  return shl(rotateAmt) | lshr(BitWidth - rotateAmt);
 }
 
 APInt APInt::rotr(const APInt &rotateAmt) const {
@@ -1353,14 +1349,10 @@ APInt APInt::rotr(const APInt &rotateAmt) const {
 }
 
 APInt APInt::rotr(unsigned rotateAmt) const {
+  rotateAmt %= BitWidth;
   if (rotateAmt == 0)
     return *this;
-  // Don't get too fancy, just use existing shift/or facilities
-  APInt hi(*this);
-  APInt lo(*this);
-  lo.lshr(rotateAmt);
-  hi.shl(BitWidth - rotateAmt);
-  return hi | lo;
+  return lshr(rotateAmt) | shl(BitWidth - rotateAmt);
 }
 
 // Square Root - this method computes and returns the square root of "this".