Index: llvm/trunk/include/llvm/Support/CheckedArithmetic.h =================================================================== --- llvm/trunk/include/llvm/Support/CheckedArithmetic.h +++ llvm/trunk/include/llvm/Support/CheckedArithmetic.h @@ -57,6 +57,16 @@ 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 @@ 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 Index: llvm/trunk/unittests/Support/CheckedArithmeticTest.cpp =================================================================== --- llvm/trunk/unittests/Support/CheckedArithmeticTest.cpp +++ llvm/trunk/unittests/Support/CheckedArithmeticTest.cpp @@ -32,6 +32,15 @@ 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 @@ 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 @@ 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