Skip to content

Commit 7c9df77

Browse files
[ADT] Provide C++20-style bit functions
Tihs patches adds APInt::count{l,r}_{zero,one} and APInt::popcount to be consistent with those functions in ADT/bit.h. Once this patch lands, I'll take care of the migration. For now, I am intentionally leaving isPowerOf2 as is. Differential Revision: https://reviews.llvm.org/D144165
1 parent 93de5f1 commit 7c9df77

File tree

2 files changed

+335
-329
lines changed

2 files changed

+335
-329
lines changed

llvm/include/llvm/ADT/APInt.h

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,31 +1541,31 @@ class [[nodiscard]] APInt {
15411541
/// parsing the value in the string.
15421542
static unsigned getSufficientBitsNeeded(StringRef Str, uint8_t Radix);
15431543

1544-
/// The APInt version of the countLeadingZeros functions in
1545-
/// MathExtras.h.
1544+
/// The APInt version of std::countl_zero.
15461545
///
15471546
/// It counts the number of zeros from the most significant bit to the first
15481547
/// one bit.
15491548
///
15501549
/// \returns BitWidth if the value is zero, otherwise returns the number of
15511550
/// zeros from the most significant bit to the first one bits.
1552-
unsigned countLeadingZeros() const {
1551+
unsigned countl_zero() const {
15531552
if (isSingleWord()) {
15541553
unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth;
15551554
return llvm::countl_zero(U.VAL) - unusedBits;
15561555
}
15571556
return countLeadingZerosSlowCase();
15581557
}
15591558

1559+
unsigned countLeadingZeros() const { return countl_zero(); }
1560+
15601561
/// Count the number of leading one bits.
15611562
///
1562-
/// This function is an APInt version of the countLeadingOnes
1563-
/// functions in MathExtras.h. It counts the number of ones from the most
1564-
/// significant bit to the first zero bit.
1563+
/// This function is an APInt version of std::countl_one. It counts the number
1564+
/// of ones from the most significant bit to the first zero bit.
15651565
///
15661566
/// \returns 0 if the high order bit is not set, otherwise returns the number
15671567
/// of 1 bits from the most significant to the least
1568-
unsigned countLeadingOnes() const {
1568+
unsigned countl_one() const {
15691569
if (isSingleWord()) {
15701570
if (LLVM_UNLIKELY(BitWidth == 0))
15711571
return 0;
@@ -1574,6 +1574,8 @@ class [[nodiscard]] APInt {
15741574
return countLeadingOnesSlowCase();
15751575
}
15761576

1577+
unsigned countLeadingOnes() const { return countl_one(); }
1578+
15771579
/// Computes the number of leading bits of this APInt that are equal to its
15781580
/// sign bit.
15791581
unsigned getNumSignBits() const {
@@ -1582,46 +1584,50 @@ class [[nodiscard]] APInt {
15821584

15831585
/// Count the number of trailing zero bits.
15841586
///
1585-
/// This function is an APInt version of the countTrailingZeros
1586-
/// functions in MathExtras.h. It counts the number of zeros from the least
1587-
/// significant bit to the first set bit.
1587+
/// This function is an APInt version of the countr_zero. It counts the number
1588+
/// of zeros from the least significant bit to the first set bit.
15881589
///
15891590
/// \returns BitWidth if the value is zero, otherwise returns the number of
15901591
/// zeros from the least significant bit to the first one bit.
1591-
unsigned countTrailingZeros() const {
1592+
unsigned countr_zero() const {
15921593
if (isSingleWord()) {
15931594
unsigned TrailingZeros = llvm::countr_zero(U.VAL);
15941595
return (TrailingZeros > BitWidth ? BitWidth : TrailingZeros);
15951596
}
15961597
return countTrailingZerosSlowCase();
15971598
}
15981599

1600+
unsigned countTrailingZeros() const { return countr_zero(); }
1601+
15991602
/// Count the number of trailing one bits.
16001603
///
1601-
/// This function is an APInt version of the countTrailingOnes
1602-
/// functions in MathExtras.h. It counts the number of ones from the least
1603-
/// significant bit to the first zero bit.
1604+
/// This function is an APInt version of std::countr_one. It counts the number
1605+
/// of ones from the least significant bit to the first zero bit.
16041606
///
16051607
/// \returns BitWidth if the value is all ones, otherwise returns the number
16061608
/// of ones from the least significant bit to the first zero bit.
1607-
unsigned countTrailingOnes() const {
1609+
unsigned countr_one() const {
16081610
if (isSingleWord())
16091611
return llvm::countr_one(U.VAL);
16101612
return countTrailingOnesSlowCase();
16111613
}
16121614

1615+
unsigned countTrailingOnes() const { return countr_one(); }
1616+
16131617
/// Count the number of bits set.
16141618
///
1615-
/// This function is an APInt version of the countPopulation functions
1616-
/// in MathExtras.h. It counts the number of 1 bits in the APInt value.
1619+
/// This function is an APInt version of std::popcount. It counts the number
1620+
/// of 1 bits in the APInt value.
16171621
///
16181622
/// \returns 0 if the value is zero, otherwise returns the number of set bits.
1619-
unsigned countPopulation() const {
1623+
unsigned popcount() const {
16201624
if (isSingleWord())
16211625
return llvm::popcount(U.VAL);
16221626
return countPopulationSlowCase();
16231627
}
16241628

1629+
unsigned countPopulation() const { return popcount(); }
1630+
16251631
/// @}
16261632
/// \name Conversion Functions
16271633
/// @{

0 commit comments

Comments
 (0)