[llvm] 49f7d2c - [Support] Use llvm::countr_zero and llvm::Log2_64 in APInt.cpp (NFC)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 25 21:22:17 PST 2023


Author: Kazu Hirata
Date: 2023-01-25T21:22:11-08:00
New Revision: 49f7d2c4f0fa105a854c2aee78347652212ace3d

URL: https://github.com/llvm/llvm-project/commit/49f7d2c4f0fa105a854c2aee78347652212ace3d
DIFF: https://github.com/llvm/llvm-project/commit/49f7d2c4f0fa105a854c2aee78347652212ace3d.diff

LOG: [Support] Use llvm::countr_zero and llvm::Log2_64 in APInt.cpp (NFC)

partMSB and partLSB never get 0 as the argument.  That is, we don't
rely on find{First,Last}Set's ability to return
std::numeric_limits<T>::max() on input 0.

This patch replaces partLSB and partMSB with llvm::countr_zero and
llvm::Log2_64, respectively.

FWIW, nobody in LLVM (except unit test MathExtrasTest.cpp) relies on
find{First,Last}Set's ability to return std::numeric_limits<T>::max()
on input 0.

Added: 
    

Modified: 
    llvm/lib/Support/APInt.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index afe7478a8b2a5..bc1c888eb7a92 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -2290,14 +2290,6 @@ static inline APInt::WordType highHalf(APInt::WordType part) {
   return part >> (APInt::APINT_BITS_PER_WORD / 2);
 }
 
-/// Returns the bit number of the most significant set bit of a part.
-/// If the input number has no bits set -1U is returned.
-static unsigned partMSB(APInt::WordType value) { return findLastSet(value); }
-
-/// Returns the bit number of the least significant set bit of a part.  If the
-/// input number has no bits set -1U is returned.
-static unsigned partLSB(APInt::WordType value) { return findFirstSet(value); }
-
 /// Sets the least significant part of a bignum to the input value, and zeroes
 /// out higher parts.
 void APInt::tcSet(WordType *dst, WordType part, unsigned parts) {
@@ -2342,7 +2334,7 @@ void APInt::tcClearBit(WordType *parts, unsigned bit) {
 unsigned APInt::tcLSB(const WordType *parts, unsigned n) {
   for (unsigned i = 0; i < n; i++) {
     if (parts[i] != 0) {
-      unsigned lsb = partLSB(parts[i]);
+      unsigned lsb = llvm::countr_zero(parts[i]);
       return lsb + i * APINT_BITS_PER_WORD;
     }
   }
@@ -2357,7 +2349,8 @@ unsigned APInt::tcMSB(const WordType *parts, unsigned n) {
     --n;
 
     if (parts[n] != 0) {
-      unsigned msb = partMSB(parts[n]);
+      static_assert(sizeof(parts[n]) <= sizeof(uint64_t));
+      unsigned msb = llvm::Log2_64(parts[n]);
 
       return msb + n * APINT_BITS_PER_WORD;
     }


        


More information about the llvm-commits mailing list