]> granicus.if.org Git - llvm/commitdiff
Don't do uint64_t(1) << 64 in maxUIntN.
authorJustin Lebar <jlebar@google.com>
Sat, 16 Jul 2016 00:59:41 +0000 (00:59 +0000)
committerJustin Lebar <jlebar@google.com>
Sat, 16 Jul 2016 00:59:41 +0000 (00:59 +0000)
Summary:
This shift is undefined behavior (and, as compiled by clang, gives the
wrong answer for maxUIntN(64)).

Reviewers: mkuper

Subscribers: llvm-commits, jroelofs, rsmith

Differential Revision: https://reviews.llvm.org/D22430

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275656 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/MathExtras.h
unittests/Support/MathExtrasTest.cpp

index f44fd19c544b332f9a81fc6369b2384f1576b69a..b60732a9b3406f5c9c71ec979b97558b4900012b 100644 (file)
@@ -316,6 +316,9 @@ inline bool isShiftedUInt(uint64_t x) {
 inline uint64_t maxUIntN(uint64_t N) {
   assert(N > 0 && N <= 64 && "integer width out of range");
 
+  // uint64_t(1) << 64 is undefined behavior.
+  if (N == 64)
+    return std::numeric_limits<uint64_t>::max();
   return (UINT64_C(1) << N) - 1;
 }
 
index 04e16628e64b138595d4f2cbc2530e6efbe302e1..ef46311665db2925442444c49e3434c6cd883150 100644 (file)
@@ -131,6 +131,7 @@ TEST(MathExtras, minIntN) {
 TEST(MathExtras, maxUIntN) {
   EXPECT_EQ(0xffffULL, maxUIntN(16));
   EXPECT_EQ(0xffffffffULL, maxUIntN(32));
+  EXPECT_EQ(0xffffffffffffffffULL, maxUIntN(64));
   EXPECT_EQ(1ULL, maxUIntN(1));
   EXPECT_EQ(0x0fULL, maxUIntN(4));
 }