aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Support/MathExtrasTest.cpp
diff options
context:
space:
mode:
authorJustin Lebar <jlebar@google.com>2016-07-17 18:19:21 +0000
committerJustin Lebar <jlebar@google.com>2016-07-17 18:19:21 +0000
commitcbba3c4aefe92e5d2f6aa2b7fe8062e47fc28b57 (patch)
treee7457b63c44def2021bc55022996fa0646556367 /llvm/unittests/Support/MathExtrasTest.cpp
parentf2d0066af751cf097a17e970e825475cb06be5bf (diff)
downloadllvm-cbba3c4aefe92e5d2f6aa2b7fe8062e47fc28b57.zip
llvm-cbba3c4aefe92e5d2f6aa2b7fe8062e47fc28b57.tar.gz
llvm-cbba3c4aefe92e5d2f6aa2b7fe8062e47fc28b57.tar.bz2
Fix isShiftedInt and isShiftedUint for widths > 32.
Summary: Previously we were doing 1 << S. "1" is an int, so this doesn't work when S >= 32. This patch also adds some static_asserts to these functions to ensure that we don't hit UB by shifting left too much. Reviewers: rnk Subscribers: llvm-commits, dylanmckay Differential Revision: https://reviews.llvm.org/D22441 llvm-svn: 275719
Diffstat (limited to 'llvm/unittests/Support/MathExtrasTest.cpp')
-rw-r--r--llvm/unittests/Support/MathExtrasTest.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/llvm/unittests/Support/MathExtrasTest.cpp b/llvm/unittests/Support/MathExtrasTest.cpp
index ef46311..3d449cc 100644
--- a/llvm/unittests/Support/MathExtrasTest.cpp
+++ b/llvm/unittests/Support/MathExtrasTest.cpp
@@ -388,4 +388,42 @@ TEST(MathExtras, SaturatingMultiplyAdd) {
SaturatingMultiplyAddTestHelper<uint64_t>();
}
+TEST(MathExtras, IsShiftedUInt) {
+ EXPECT_TRUE((isShiftedUInt<1, 0>(0)));
+ EXPECT_TRUE((isShiftedUInt<1, 0>(1)));
+ EXPECT_FALSE((isShiftedUInt<1, 0>(2)));
+ EXPECT_FALSE((isShiftedUInt<1, 0>(3)));
+ EXPECT_FALSE((isShiftedUInt<1, 0>(0x8000000000000000)));
+ EXPECT_TRUE((isShiftedUInt<1, 63>(0x8000000000000000)));
+ EXPECT_TRUE((isShiftedUInt<2, 62>(0xC000000000000000)));
+ EXPECT_FALSE((isShiftedUInt<2, 62>(0xE000000000000000)));
+
+ // 0x201 is ten bits long and has a 1 in the MSB and LSB.
+ EXPECT_TRUE((isShiftedUInt<10, 5>(uint64_t(0x201) << 5)));
+ EXPECT_FALSE((isShiftedUInt<10, 5>(uint64_t(0x201) << 4)));
+ EXPECT_FALSE((isShiftedUInt<10, 5>(uint64_t(0x201) << 6)));
}
+
+TEST(MathExtras, IsShiftedInt) {
+ EXPECT_TRUE((isShiftedInt<1, 0>(0)));
+ EXPECT_TRUE((isShiftedInt<1, 0>(-1)));
+ EXPECT_FALSE((isShiftedInt<1, 0>(2)));
+ EXPECT_FALSE((isShiftedInt<1, 0>(3)));
+ EXPECT_FALSE((isShiftedInt<1, 0>(0x8000000000000000)));
+ EXPECT_TRUE((isShiftedInt<1, 63>(0x8000000000000000)));
+ EXPECT_TRUE((isShiftedInt<2, 62>(0xC000000000000000)));
+ EXPECT_FALSE((isShiftedInt<2, 62>(0xE000000000000000)));
+
+ // 0x201 is ten bits long and has a 1 in the MSB and LSB.
+ EXPECT_TRUE((isShiftedInt<11, 5>(int64_t(0x201) << 5)));
+ EXPECT_FALSE((isShiftedInt<11, 5>(int64_t(0x201) << 3)));
+ EXPECT_FALSE((isShiftedInt<11, 5>(int64_t(0x201) << 6)));
+ EXPECT_TRUE((isShiftedInt<11, 5>(-(int64_t(0x201) << 5))));
+ EXPECT_FALSE((isShiftedInt<11, 5>(-(int64_t(0x201) << 3))));
+ EXPECT_FALSE((isShiftedInt<11, 5>(-(int64_t(0x201) << 6))));
+
+ EXPECT_TRUE((isShiftedInt<6, 10>(-(int64_t(1) << 15))));
+ EXPECT_FALSE((isShiftedInt<6, 10>(int64_t(1) << 15)));
+}
+
+} // namespace