From 7dd6340bdadf86bd0facdea89d1876a5c36dc33b Mon Sep 17 00:00:00 2001 From: Ramkumar Ramachandra Date: Mon, 19 Aug 2024 16:03:42 +0100 Subject: MathExtras: template'ize alignToPowerOf2 (#97814) Follow up on 5627794 (MathExtras: avoid unnecessarily widening types) to change the overflow behavior of alignToPowerOf2 to only overflow if the result is not representable in the return type. This allows us to template'ize it, and avoid unnecessarily widening the types of arguments. --- llvm/unittests/Support/MathExtrasTest.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'llvm/unittests/Support/MathExtrasTest.cpp') diff --git a/llvm/unittests/Support/MathExtrasTest.cpp b/llvm/unittests/Support/MathExtrasTest.cpp index a1cc609..984185f 100644 --- a/llvm/unittests/Support/MathExtrasTest.cpp +++ b/llvm/unittests/Support/MathExtrasTest.cpp @@ -218,8 +218,11 @@ TEST(MathExtras, AlignToPowerOf2) { EXPECT_EQ(24u, alignToPowerOf2(17, 8)); EXPECT_EQ(0u, alignToPowerOf2(~0LL, 8)); EXPECT_EQ(240u, alignToPowerOf2(240, 16)); - EXPECT_EQ(static_cast(std::numeric_limits::max()) + 1, - alignToPowerOf2(std::numeric_limits::max(), 2)); + + // Overflow. + EXPECT_EQ(0u, alignToPowerOf2(static_cast(200), + static_cast(128))); + EXPECT_EQ(0u, alignToPowerOf2(std::numeric_limits::max(), 2)); } TEST(MathExtras, AlignDown) { -- cgit v1.1