From 6e61dd242b17415b3df63e7518c9e5cce4e9bcf4 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Mon, 17 Nov 2014 15:46:21 +0000 Subject: [PATCH] Use a more canonical way of computing a mask with N trailing 1s. NFC. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@222137 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Bitcode/BitstreamReader.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/include/llvm/Bitcode/BitstreamReader.h b/include/llvm/Bitcode/BitstreamReader.h index ecf82352779..a4d2b1d8b4c 100644 --- a/include/llvm/Bitcode/BitstreamReader.h +++ b/include/llvm/Bitcode/BitstreamReader.h @@ -338,16 +338,14 @@ public: } word_t Read(unsigned NumBits) { - static const unsigned BitsInWord = sizeof(word_t) * 8; - - assert(NumBits && NumBits <= BitsInWord && + assert(NumBits && NumBits <= sizeof(word_t) * 8 && "Cannot return zero or more than BitsInWord bits!"); static const unsigned Mask = sizeof(word_t) > 4 ? 0x3f : 0x1f; // If the field is fully contained by CurWord, return it quickly. if (BitsInCurWord >= NumBits) { - word_t R = CurWord & (~word_t(0) >> (BitsInWord - NumBits)); + word_t R = CurWord & ((word_t(1) << NumBits) - 1); // Use a mask to avoid undefined behavior. CurWord >>= (NumBits & Mask); @@ -365,7 +363,7 @@ public: if (BitsLeft > BitsInCurWord) return 0; - word_t R2 = CurWord & (~word_t(0) >> (BitsInWord - BitsLeft)); + word_t R2 = CurWord & ((word_t(1) << BitsLeft) - 1); // Use a mask to avoid undefined behavior. CurWord >>= (BitsLeft & Mask); -- 2.34.1