[llvm] r367287 - [APInt] Introduce clearLowBits()
Roman Lebedev via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 30 00:09:41 PDT 2019
Author: lebedevri
Date: Tue Jul 30 00:09:41 2019
New Revision: 367287
URL: http://llvm.org/viewvc/llvm-project?rev=367287&view=rev
Log:
[APInt] Introduce clearLowBits()
Summary: Equivalent to `x & -2^K`.
Reviewers: RKSimon, craig.topper
Reviewed By: RKSimon, craig.topper
Subscribers: dexonsmith, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65369
Modified:
llvm/trunk/include/llvm/ADT/APInt.h
llvm/trunk/unittests/ADT/APIntTest.cpp
Modified: llvm/trunk/include/llvm/ADT/APInt.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/APInt.h?rev=367287&r1=367286&r2=367287&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/APInt.h (original)
+++ llvm/trunk/include/llvm/ADT/APInt.h Tue Jul 30 00:09:41 2019
@@ -1467,6 +1467,13 @@ public:
U.pVal[whichWord(BitPosition)] &= Mask;
}
+ /// Set bottom loBits bits to 0.
+ void clearLowBits(unsigned loBits) {
+ assert(loBits <= BitWidth && "More bits than bitwidth");
+ APInt Keep = getHighBitsSet(BitWidth, BitWidth - loBits);
+ *this &= Keep;
+ }
+
/// Set the sign bit to 0.
void clearSignBit() {
clearBit(BitWidth - 1);
Modified: llvm/trunk/unittests/ADT/APIntTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APIntTest.cpp?rev=367287&r1=367286&r2=367287&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/APIntTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APIntTest.cpp Tue Jul 30 00:09:41 2019
@@ -2117,6 +2117,71 @@ TEST(APIntTest, getHiBits) {
EXPECT_EQ(0xc, i128.getHiBits(4));
}
+TEST(APIntTest, clearLowBits) {
+ APInt i64hi32 = APInt::getAllOnesValue(64);
+ i64hi32.clearLowBits(32);
+ EXPECT_EQ(32u, i64hi32.countLeadingOnes());
+ EXPECT_EQ(0u, i64hi32.countLeadingZeros());
+ EXPECT_EQ(64u, i64hi32.getActiveBits());
+ EXPECT_EQ(32u, i64hi32.countTrailingZeros());
+ EXPECT_EQ(0u, i64hi32.countTrailingOnes());
+ EXPECT_EQ(32u, i64hi32.countPopulation());
+
+ APInt i128hi64 = APInt::getAllOnesValue(128);
+ i128hi64.clearLowBits(64);
+ EXPECT_EQ(64u, i128hi64.countLeadingOnes());
+ EXPECT_EQ(0u, i128hi64.countLeadingZeros());
+ EXPECT_EQ(128u, i128hi64.getActiveBits());
+ EXPECT_EQ(64u, i128hi64.countTrailingZeros());
+ EXPECT_EQ(0u, i128hi64.countTrailingOnes());
+ EXPECT_EQ(64u, i128hi64.countPopulation());
+
+ APInt i128hi24 = APInt::getAllOnesValue(128);
+ i128hi24.clearLowBits(104);
+ EXPECT_EQ(24u, i128hi24.countLeadingOnes());
+ EXPECT_EQ(0u, i128hi24.countLeadingZeros());
+ EXPECT_EQ(128u, i128hi24.getActiveBits());
+ EXPECT_EQ(104u, i128hi24.countTrailingZeros());
+ EXPECT_EQ(0u, i128hi24.countTrailingOnes());
+ EXPECT_EQ(24u, i128hi24.countPopulation());
+
+ APInt i128hi104 = APInt::getAllOnesValue(128);
+ i128hi104.clearLowBits(24);
+ EXPECT_EQ(104u, i128hi104.countLeadingOnes());
+ EXPECT_EQ(0u, i128hi104.countLeadingZeros());
+ EXPECT_EQ(128u, i128hi104.getActiveBits());
+ EXPECT_EQ(24u, i128hi104.countTrailingZeros());
+ EXPECT_EQ(0u, i128hi104.countTrailingOnes());
+ EXPECT_EQ(104u, i128hi104.countPopulation());
+
+ APInt i128hi0 = APInt::getAllOnesValue(128);
+ i128hi0.clearLowBits(128);
+ EXPECT_EQ(0u, i128hi0.countLeadingOnes());
+ EXPECT_EQ(128u, i128hi0.countLeadingZeros());
+ EXPECT_EQ(0u, i128hi0.getActiveBits());
+ EXPECT_EQ(128u, i128hi0.countTrailingZeros());
+ EXPECT_EQ(0u, i128hi0.countTrailingOnes());
+ EXPECT_EQ(0u, i128hi0.countPopulation());
+
+ APInt i80hi1 = APInt::getAllOnesValue(80);
+ i80hi1.clearLowBits(79);
+ EXPECT_EQ(1u, i80hi1.countLeadingOnes());
+ EXPECT_EQ(0u, i80hi1.countLeadingZeros());
+ EXPECT_EQ(80u, i80hi1.getActiveBits());
+ EXPECT_EQ(79u, i80hi1.countTrailingZeros());
+ EXPECT_EQ(0u, i80hi1.countTrailingOnes());
+ EXPECT_EQ(1u, i80hi1.countPopulation());
+
+ APInt i32hi16 = APInt::getAllOnesValue(32);
+ i32hi16.clearLowBits(16);
+ EXPECT_EQ(16u, i32hi16.countLeadingOnes());
+ EXPECT_EQ(0u, i32hi16.countLeadingZeros());
+ EXPECT_EQ(32u, i32hi16.getActiveBits());
+ EXPECT_EQ(16u, i32hi16.countTrailingZeros());
+ EXPECT_EQ(0u, i32hi16.countTrailingOnes());
+ EXPECT_EQ(16u, i32hi16.countPopulation());
+}
+
TEST(APIntTest, GCD) {
using APIntOps::GreatestCommonDivisor;
More information about the llvm-commits
mailing list