llvm.org GIT mirror llvm / 1dca546
APInt: Add overload of isMask This mimics the version in MathExtras.h which isn't testing for a specific mask size. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266101 91177308-0d34-0410-b5e6-96231b3b80d8 Matt Arsenault 4 years ago
2 changed file(s) with 24 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
17811781 APIVal == APInt::getLowBitsSet(APIVal.getBitWidth(), numBits);
17821782 }
17831783
1784 /// \returns true if the argument is a non-empty sequence of ones starting at
1785 /// the least significant bit with the remainder zero (32 bit version).
1786 /// Ex. isMask(0x0000FFFFU) == true.
1787 inline bool isMask(const APInt &Value) {
1788 return (Value != 0) && ((Value + 1) & Value) == 0;
1789 }
1790
17841791 /// \brief Return true if the argument APInt value contains a sequence of ones
17851792 /// with the remainder zero.
17861793 inline bool isShiftedMask(unsigned numBits, const APInt &APIVal) {
993993 EXPECT_TRUE(E.isSplat(32));
994994 }
995995
996 TEST(APIntTest, isMask) {
997 EXPECT_FALSE(APIntOps::isMask(APInt(32, 0x01010101)));
998 EXPECT_FALSE(APIntOps::isMask(APInt(32, 0xf0000000)));
999 EXPECT_FALSE(APIntOps::isMask(APInt(32, 0xffff0000)));
1000 EXPECT_FALSE(APIntOps::isMask(APInt(32, 0xff << 1)));
1001
1002 for (int N : { 1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256 }) {
1003 EXPECT_FALSE(APIntOps::isMask(APInt(N, 0)));
1004
1005 APInt One(N, 1);
1006 for (int I = 1; I <= N; ++I) {
1007 APInt MaskVal = One.shl(I) - 1;
1008 EXPECT_TRUE(APIntOps::isMask(MaskVal));
1009 }
1010 }
1011 }
1012
9961013 #if defined(__clang__)
9971014 // Disable the pragma warning from versions of Clang without -Wself-move
9981015 #pragma clang diagnostic push