From b97b3021fc7b9eb912c61f6c9dfd03f0e35b7cc4 Mon Sep 17 00:00:00 2001 From: George Karpenkov Date: Wed, 13 Jun 2018 18:32:02 +0000 Subject: [PATCH] Add checkMulAdd helper function to CheckedArithmetic MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Multiplication followed by addition (https://en.wikipedia.org/wiki/Multiply–accumulate_operation) is a sufficiently common use-case to warrant a separate helper. Differential Revision: https://reviews.llvm.org/D48138 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@334635 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/CheckedArithmetic.h | 20 ++++++++++++++++++++ unittests/Support/CheckedArithmeticTest.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/llvm/Support/CheckedArithmetic.h b/include/llvm/Support/CheckedArithmetic.h index 3240487193a..2ec27f4d354 100644 --- a/include/llvm/Support/CheckedArithmetic.h +++ b/include/llvm/Support/CheckedArithmetic.h @@ -57,6 +57,16 @@ checkedMul(T LHS, T RHS) { return checkedOp(LHS, RHS, &llvm::APInt::smul_ov); } +/// Multiply A and B, and add C to the resulting product. +/// Return the value if available, None if overflowing. +template +typename std::enable_if::value, llvm::Optional>::type +checkedMulAdd(T A, T B, T C) { + if (auto Product = checkedMul(A, B)) + return checkedAdd(*Product, C); + return llvm::None; +} + /// Add two unsigned integers \p LHS and \p RHS, return wrapped result /// if available. template @@ -73,6 +83,16 @@ checkedMulUnsigned(T LHS, T RHS) { return checkedOp(LHS, RHS, &llvm::APInt::umul_ov, /*Signed=*/false); } +/// Multiply unsigned A and B, and add C to the resulting product. +/// Return the value if available, None if overflowing. +template +typename std::enable_if::value, llvm::Optional>::type +checkedMulAddUnsigned(T A, T B, T C) { + if (auto Product = checkedMulUnsigned(A, B)) + return checkedAddUnsigned(*Product, C); + return llvm::None; +} + } // End llvm namespace #endif diff --git a/unittests/Support/CheckedArithmeticTest.cpp b/unittests/Support/CheckedArithmeticTest.cpp index f797a9ed41e..18eee4c447e 100644 --- a/unittests/Support/CheckedArithmeticTest.cpp +++ b/unittests/Support/CheckedArithmeticTest.cpp @@ -32,6 +32,15 @@ TEST(CheckedArithmetic, CheckedMul) { EXPECT_EQ(checkedMul(10, 2), Optional(20)); } +TEST(CheckedArithmetic, CheckedMulAdd) { + const int64_t Max = std::numeric_limits::max(); + const int64_t Min = std::numeric_limits::min(); + EXPECT_EQ(checkedMulAdd(Max, 1, 2), None); + EXPECT_EQ(checkedMulAdd(1, 1, Max), None); + EXPECT_EQ(checkedMulAdd(1, -1, Min), None); + EXPECT_EQ(checkedMulAdd(10, 2, 3), Optional(23)); +} + TEST(CheckedArithmetic, CheckedMulSmall) { const int16_t Max = std::numeric_limits::max(); const int16_t Min = std::numeric_limits::min(); @@ -41,6 +50,15 @@ TEST(CheckedArithmetic, CheckedMulSmall) { EXPECT_EQ(checkedMul(10, 2), Optional(20)); } +TEST(CheckedArithmetic, CheckedMulAddSmall) { + const int16_t Max = std::numeric_limits::max(); + const int16_t Min = std::numeric_limits::min(); + EXPECT_EQ(checkedMulAdd(Max, 1, 2), None); + EXPECT_EQ(checkedMulAdd(1, 1, Max), None); + EXPECT_EQ(checkedMulAdd(1, -1, Min), None); + EXPECT_EQ(checkedMulAdd(10, 2, 3), Optional(23)); +} + TEST(CheckedArithmetic, CheckedAddUnsigned) { const uint64_t Max = std::numeric_limits::max(); EXPECT_EQ(checkedAddUnsigned(Max, Max), None); @@ -55,5 +73,12 @@ TEST(CheckedArithmetic, CheckedMulUnsigned) { EXPECT_EQ(checkedMulUnsigned(10, 2), Optional(20)); } +TEST(CheckedArithmetic, CheckedMulAddUnsigned) { + const uint64_t Max = std::numeric_limits::max(); + EXPECT_EQ(checkedMulAddUnsigned(Max, 1, 2), None); + EXPECT_EQ(checkedMulAddUnsigned(1, 1, Max), None); + EXPECT_EQ(checkedMulAddUnsigned(10, 2, 3), Optional(23)); +} + } // namespace -- 2.11.0