llvm.org GIT mirror llvm / 0616793
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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275656 91177308-0d34-0410-b5e6-96231b3b80d8 Justin Lebar 4 years ago
2 changed file(s) with 4 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
315315 inline uint64_t maxUIntN(uint64_t N) {
316316 assert(N > 0 && N <= 64 && "integer width out of range");
317317
318 // uint64_t(1) << 64 is undefined behavior.
319 if (N == 64)
320 return std::numeric_limits::max();
318321 return (UINT64_C(1) << N) - 1;
319322 }
320323
130130 TEST(MathExtras, maxUIntN) {
131131 EXPECT_EQ(0xffffULL, maxUIntN(16));
132132 EXPECT_EQ(0xffffffffULL, maxUIntN(32));
133 EXPECT_EQ(0xffffffffffffffffULL, maxUIntN(64));
133134 EXPECT_EQ(1ULL, maxUIntN(1));
134135 EXPECT_EQ(0x0fULL, maxUIntN(4));
135136 }