Index: include/llvm/Support/MathExtras.h =================================================================== --- include/llvm/Support/MathExtras.h +++ include/llvm/Support/MathExtras.h @@ -717,6 +717,24 @@ return Z; } +/// \brief Multiply two unsigned integers, X and Y, of type T. Add the +/// unsigned integer, A, also of type T, to the product. +/// Clamp the result to the maximum representable value of T on overflow. +/// ResultOverflowed indicates if the result is larger than the maximum +/// representable value of type T. +template +typename std::enable_if::value, T>::type +SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed = nullptr) { + bool Dummy; + bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy; + + T Product = SaturatingMultiply(X, Y, &Overflowed); + if (Overflowed) + return Product; + + return SaturatingAdd(A, Product, &Overflowed); +} + extern const float huge_valf; } // End llvm namespace Index: unittests/Support/MathExtrasTest.cpp =================================================================== --- unittests/Support/MathExtrasTest.cpp +++ unittests/Support/MathExtrasTest.cpp @@ -304,4 +304,66 @@ SaturatingMultiplyTestHelper(); } +template +void SaturatingMultiplyAddTestHelper() +{ + const T Max = std::numeric_limits::max(); + bool ResultOverflowed; + + // Test basic multiply-add. + EXPECT_EQ(T(16), SaturatingMultiplyAdd(T(2), T(3), T(10))); + EXPECT_EQ(T(16), SaturatingMultiplyAdd(T(2), T(3), T(10), &ResultOverflowed)); + EXPECT_FALSE(ResultOverflowed); + + // Test multiply overflows, add doesn't overflow + EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, T(0), &ResultOverflowed)); + EXPECT_TRUE(ResultOverflowed); + + // Test multiply doesn't overflow, add overflows + EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), T(1), Max, &ResultOverflowed)); + EXPECT_TRUE(ResultOverflowed); + + // Test multiply-add with Max as operand + EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), T(1), Max)); + EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), T(1), Max, &ResultOverflowed)); + EXPECT_TRUE(ResultOverflowed); + + EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), Max, T(1))); + EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), Max, T(1), &ResultOverflowed)); + EXPECT_TRUE(ResultOverflowed); + + EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, T(1))); + EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, T(1), &ResultOverflowed)); + EXPECT_TRUE(ResultOverflowed); + + EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, Max)); + EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, Max, &ResultOverflowed)); + EXPECT_TRUE(ResultOverflowed); + + // Test multiply-add with 0 as operand + EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(1), T(1), T(0))); + EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(1), T(1), T(0), &ResultOverflowed)); + EXPECT_FALSE(ResultOverflowed); + + EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(1), T(0), T(1))); + EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(1), T(0), T(1), &ResultOverflowed)); + EXPECT_FALSE(ResultOverflowed); + + EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(0), T(0), T(1))); + EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(0), T(0), T(1), &ResultOverflowed)); + EXPECT_FALSE(ResultOverflowed); + + EXPECT_EQ(T(0), SaturatingMultiplyAdd(T(0), T(0), T(0))); + EXPECT_EQ(T(0), SaturatingMultiplyAdd(T(0), T(0), T(0), &ResultOverflowed)); + EXPECT_FALSE(ResultOverflowed); + +} + +TEST(MathExtras, SaturatingMultiplyAdd) { + SaturatingMultiplyAddTestHelper(); + SaturatingMultiplyAddTestHelper(); + SaturatingMultiplyAddTestHelper(); + SaturatingMultiplyAddTestHelper(); +} + }