[llvm] r275656 - Don't do uint64_t(1) << 64 in maxUIntN.

Justin Lebar via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 15 17:59:41 PDT 2016


Author: jlebar
Date: Fri Jul 15 19:59:41 2016
New Revision: 275656

URL: http://llvm.org/viewvc/llvm-project?rev=275656&view=rev
Log:
Don't do uint64_t(1) << 64 in maxUIntN.

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

Modified:
    llvm/trunk/include/llvm/Support/MathExtras.h
    llvm/trunk/unittests/Support/MathExtrasTest.cpp

Modified: llvm/trunk/include/llvm/Support/MathExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MathExtras.h?rev=275656&r1=275655&r2=275656&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MathExtras.h (original)
+++ llvm/trunk/include/llvm/Support/MathExtras.h Fri Jul 15 19:59:41 2016
@@ -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;
 }
 

Modified: llvm/trunk/unittests/Support/MathExtrasTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/MathExtrasTest.cpp?rev=275656&r1=275655&r2=275656&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/MathExtrasTest.cpp (original)
+++ llvm/trunk/unittests/Support/MathExtrasTest.cpp Fri Jul 15 19:59:41 2016
@@ -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));
 }




More information about the llvm-commits mailing list