aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Gohman <gohman@apple.com>2008-02-13 21:11:05 +0000
committerDan Gohman <gohman@apple.com>2008-02-13 21:11:05 +0000
commit42dd77f20702d5ca1e0f3882ad74e7a02fc9589c (patch)
treecf6d98a99a0f295b59c917372e31fc8511a4598e
parentb5cfaaedbea10d8fcf5ca23be7955ca0f4fd5c9c (diff)
Add countTrailingOnes member functions to APInt.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47086 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/ADT/APInt.h14
-rw-r--r--lib/Support/APInt.cpp12
2 files changed, 24 insertions, 2 deletions
diff --git a/include/llvm/ADT/APInt.h b/include/llvm/ADT/APInt.h
index fe9d2388f3..8fa1f950bc 100644
--- a/include/llvm/ADT/APInt.h
+++ b/include/llvm/ADT/APInt.h
@@ -913,8 +913,9 @@ public:
/// one bits.
uint32_t countLeadingZeros() const;
- /// countLeadingOnes - This function counts the number of contiguous 1 bits
- /// in the high order bits. The count stops when the first 0 bit is reached.
+ /// countLeadingOnes - This function is an APInt version of the
+ /// countLeadingOnes_{32,64} functions in MathExtras.h. It counts the number
+ /// of ones from the most significant bit to the first zero bit.
/// @returns 0 if the high order bit is not set
/// @returns the number of 1 bits from the most significant to the least
/// @brief Count the number of leading one bits.
@@ -929,6 +930,15 @@ public:
/// @brief Count the number of trailing zero bits.
uint32_t countTrailingZeros() const;
+ /// countTrailingOnes - This function is an APInt version of the
+ /// countTrailingOnes_{32,64} functions in MathExtras.h. It counts
+ /// the number of ones from the least significant bit to the first zero bit.
+ /// @returns BitWidth if the value is all ones.
+ /// @returns the number of ones from the least significant bit to the first
+ /// zero bit.
+ /// @brief Count the number of trailing one bits.
+ uint32_t countTrailingOnes() const;
+
/// countPopulation - This function is an APInt version of the
/// countPopulation_{32,64} functions in MathExtras.h. It counts the number
/// of 1 bits in the APInt value.
diff --git a/lib/Support/APInt.cpp b/lib/Support/APInt.cpp
index 88f32810d6..c8482e9c71 100644
--- a/lib/Support/APInt.cpp
+++ b/lib/Support/APInt.cpp
@@ -813,6 +813,18 @@ uint32_t APInt::countTrailingZeros() const {
return std::min(Count, BitWidth);
}
+uint32_t APInt::countTrailingOnes() const {
+ if (isSingleWord())
+ return std::min(uint32_t(CountTrailingOnes_64(VAL)), BitWidth);
+ uint32_t Count = 0;
+ uint32_t i = 0;
+ for (; i < getNumWords() && pVal[i] == -1; ++i)
+ Count += APINT_BITS_PER_WORD;
+ if (i < getNumWords())
+ Count += CountTrailingOnes_64(pVal[i]);
+ return std::min(Count, BitWidth);
+}
+
uint32_t APInt::countPopulation() const {
if (isSingleWord())
return CountPopulation_64(VAL);