llvm.org GIT mirror llvm / 6e61dd2
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 Rafael Espindola 6 years ago
1 changed file(s) with 3 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
337337 }
338338
339339 word_t Read(unsigned NumBits) {
340 static const unsigned BitsInWord = sizeof(word_t) * 8;
341
342 assert(NumBits && NumBits <= BitsInWord &&
340 assert(NumBits && NumBits <= sizeof(word_t) * 8 &&
343341 "Cannot return zero or more than BitsInWord bits!");
344342
345343 static const unsigned Mask = sizeof(word_t) > 4 ? 0x3f : 0x1f;
346344
347345 // If the field is fully contained by CurWord, return it quickly.
348346 if (BitsInCurWord >= NumBits) {
349 word_t R = CurWord & (~word_t(0) >> (BitsInWord - NumBits));
347 word_t R = CurWord & ((word_t(1) << NumBits) - 1);
350348
351349 // Use a mask to avoid undefined behavior.
352350 CurWord >>= (NumBits & Mask);
364362 if (BitsLeft > BitsInCurWord)
365363 return 0;
366364
367 word_t R2 = CurWord & (~word_t(0) >> (BitsInWord - BitsLeft));
365 word_t R2 = CurWord & ((word_t(1) << BitsLeft) - 1);
368366
369367 // Use a mask to avoid undefined behavior.
370368 CurWord >>= (BitsLeft & Mask);