[llvm] r266101 - APInt: Add overload of isMask
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 12 11:17:23 PDT 2016
Author: arsenm
Date: Tue Apr 12 13:17:23 2016
New Revision: 266101
URL: http://llvm.org/viewvc/llvm-project?rev=266101&view=rev
Log:
APInt: Add overload of isMask
This mimics the version in MathExtras.h which isn't testing for a
specific mask size.
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=266101&r1=266100&r2=266101&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/APInt.h (original)
+++ llvm/trunk/include/llvm/ADT/APInt.h Tue Apr 12 13:17:23 2016
@@ -1782,6 +1782,13 @@ inline bool isMask(unsigned numBits, con
APIVal == APInt::getLowBitsSet(APIVal.getBitWidth(), numBits);
}
+/// \returns true if the argument is a non-empty sequence of ones starting at
+/// the least significant bit with the remainder zero (32 bit version).
+/// Ex. isMask(0x0000FFFFU) == true.
+inline bool isMask(const APInt &Value) {
+ return (Value != 0) && ((Value + 1) & Value) == 0;
+}
+
/// \brief Return true if the argument APInt value contains a sequence of ones
/// with the remainder zero.
inline bool isShiftedMask(unsigned numBits, const APInt &APIVal) {
Modified: llvm/trunk/unittests/ADT/APIntTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APIntTest.cpp?rev=266101&r1=266100&r2=266101&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/APIntTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APIntTest.cpp Tue Apr 12 13:17:23 2016
@@ -994,6 +994,23 @@ TEST(APIntTest, IsSplat) {
EXPECT_TRUE(E.isSplat(32));
}
+TEST(APIntTest, isMask) {
+ EXPECT_FALSE(APIntOps::isMask(APInt(32, 0x01010101)));
+ EXPECT_FALSE(APIntOps::isMask(APInt(32, 0xf0000000)));
+ EXPECT_FALSE(APIntOps::isMask(APInt(32, 0xffff0000)));
+ EXPECT_FALSE(APIntOps::isMask(APInt(32, 0xff << 1)));
+
+ for (int N : { 1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256 }) {
+ EXPECT_FALSE(APIntOps::isMask(APInt(N, 0)));
+
+ APInt One(N, 1);
+ for (int I = 1; I <= N; ++I) {
+ APInt MaskVal = One.shl(I) - 1;
+ EXPECT_TRUE(APIntOps::isMask(MaskVal));
+ }
+ }
+}
+
#if defined(__clang__)
// Disable the pragma warning from versions of Clang without -Wself-move
#pragma clang diagnostic push
More information about the llvm-commits
mailing list