[PATCH] D91045: [ADT] fix PackedVector crash for long bits
Jason Hsu via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 8 18:59:33 PST 2020
tuoxie007 created this revision.
Herald added subscribers: llvm-commits, dexonsmith.
Herald added a project: LLVM.
tuoxie007 requested review of this revision.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D91045
Files:
llvm/include/llvm/ADT/PackedVector.h
llvm/unittests/ADT/PackedVectorTest.cpp
Index: llvm/unittests/ADT/PackedVectorTest.cpp
===================================================================
--- llvm/unittests/ADT/PackedVectorTest.cpp
+++ llvm/unittests/ADT/PackedVectorTest.cpp
@@ -61,6 +61,20 @@
EXPECT_EQ(3U, Vec[1]);
}
+TEST(PackedVectorTest, LongBits) {
+ PackedVector<unsigned, 30> Vec1(8);
+ Vec1[0] = 100;
+ EXPECT_EQ(Vec1[0], 100);
+ Vec1[1] = 0x40000000 - 100;
+ EXPECT_EQ(Vec1[1], 0x40000000 - 100);
+
+ PackedVector<signed, 30> Vec2(8);
+ Vec2[7] = -100;
+ EXPECT_EQ(Vec2[7], -100);
+ Vec2[6] = 100 - 0x20000000;
+ EXPECT_EQ(Vec2[6], 100 - 0x20000000);
+}
+
#ifdef EXPECT_DEBUG_DEATH
TEST(PackedVectorTest, UnsignedValues) {
Index: llvm/include/llvm/ADT/PackedVector.h
===================================================================
--- llvm/include/llvm/ADT/PackedVector.h
+++ llvm/include/llvm/ADT/PackedVector.h
@@ -30,14 +30,14 @@
static T getValue(const BitVectorTy &Bits, unsigned Idx) {
T val = T();
for (unsigned i = 0; i != BitNum; ++i)
- val = T(val | ((Bits[(Idx << (BitNum-1)) + i] ? 1UL : 0UL) << i));
+ val = T(val | ((Bits[(Idx * BitNum) + i] ? 1UL : 0UL) << i));
return val;
}
static void setValue(BitVectorTy &Bits, unsigned Idx, T val) {
assert((val >> BitNum) == 0 && "value is too big");
for (unsigned i = 0; i != BitNum; ++i)
- Bits[(Idx << (BitNum-1)) + i] = val & (T(1) << i);
+ Bits[(Idx * BitNum) + i] = val & (T(1) << i);
}
};
@@ -47,8 +47,8 @@
static T getValue(const BitVectorTy &Bits, unsigned Idx) {
T val = T();
for (unsigned i = 0; i != BitNum-1; ++i)
- val = T(val | ((Bits[(Idx << (BitNum-1)) + i] ? 1UL : 0UL) << i));
- if (Bits[(Idx << (BitNum-1)) + BitNum-1])
+ val = T(val | ((Bits[(Idx * BitNum) + i] ? 1UL : 0UL) << i));
+ if (Bits[(Idx * BitNum) + BitNum - 1])
val = ~val;
return val;
}
@@ -56,11 +56,11 @@
static void setValue(BitVectorTy &Bits, unsigned Idx, T val) {
if (val < 0) {
val = ~val;
- Bits.set((Idx << (BitNum-1)) + BitNum-1);
+ Bits.set((Idx * BitNum) + BitNum - 1);
}
assert((val >> (BitNum-1)) == 0 && "value is too big");
for (unsigned i = 0; i != BitNum-1; ++i)
- Bits[(Idx << (BitNum-1)) + i] = val & (T(1) << i);
+ Bits[(Idx * BitNum) + i] = val & (T(1) << i);
}
};
@@ -98,17 +98,17 @@
};
PackedVector() = default;
- explicit PackedVector(unsigned size) : Bits(size << (BitNum-1)) {}
+ explicit PackedVector(unsigned size) : Bits(size * BitNum) {}
bool empty() const { return Bits.empty(); }
- unsigned size() const { return Bits.size() >> (BitNum - 1); }
+ unsigned size() const { return Bits.size() / BitNum; }
void clear() { Bits.clear(); }
- void resize(unsigned N) { Bits.resize(N << (BitNum - 1)); }
+ void resize(unsigned N) { Bits.resize(N * BitNum); }
- void reserve(unsigned N) { Bits.reserve(N << (BitNum-1)); }
+ void reserve(unsigned N) { Bits.reserve(N * BitNum); }
PackedVector &reset() {
Bits.reset();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91045.303747.patch
Type: text/x-patch
Size: 3044 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201109/4c47982a/attachment.bin>
More information about the llvm-commits
mailing list