[llvm] d59a196 - [Support][MathExtras] Add variadic SaturatingAdd
Alexander Shaposhnikov via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 1 12:25:41 PST 2022
Author: Alexander Shaposhnikov
Date: 2022-12-01T20:24:49Z
New Revision: d59a196b8cb8bb59e38ab2bd93b3454141e33bc9
URL: https://github.com/llvm/llvm-project/commit/d59a196b8cb8bb59e38ab2bd93b3454141e33bc9
DIFF: https://github.com/llvm/llvm-project/commit/d59a196b8cb8bb59e38ab2bd93b3454141e33bc9.diff
LOG: [Support][MathExtras] Add variadic SaturatingAdd
Add variadic SaturatingAdd.
Test plan: ninja check-llvm-unit check-all
Differential revision: https://reviews.llvm.org/D139036
Added:
Modified:
llvm/include/llvm/Support/MathExtras.h
llvm/unittests/Support/MathExtrasTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
index 7e26e01f617fa..ddde8a2607536 100644
--- a/llvm/include/llvm/Support/MathExtras.h
+++ b/llvm/include/llvm/Support/MathExtras.h
@@ -753,6 +753,18 @@ SaturatingAdd(T X, T Y, bool *ResultOverflowed = nullptr) {
return Z;
}
+/// Add multiple unsigned integers of type T. Clamp the result to the
+/// maximum representable value of T on overflow.
+template <class T, class... Ts>
+std::enable_if_t<std::is_unsigned_v<T>, T> SaturatingAdd(T X, T Y, T Z,
+ Ts... Args) {
+ bool Overflowed = false;
+ T XY = SaturatingAdd(X, Y, &Overflowed);
+ if (Overflowed)
+ return SaturatingAdd(std::numeric_limits<T>::max(), T(1), Args...);
+ return SaturatingAdd(XY, Z, Args...);
+}
+
/// Multiply two unsigned integers, X and Y, of type T. 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.
diff --git a/llvm/unittests/Support/MathExtrasTest.cpp b/llvm/unittests/Support/MathExtrasTest.cpp
index 23034d64028c6..855871985891f 100644
--- a/llvm/unittests/Support/MathExtrasTest.cpp
+++ b/llvm/unittests/Support/MathExtrasTest.cpp
@@ -309,9 +309,7 @@ TEST(MathExtras, alignTo) {
EXPECT_EQ(552u, alignTo(321, 255, 42));
}
-template<typename T>
-void SaturatingAddTestHelper()
-{
+template <typename T> void SaturatingAddTestHelper() {
const T Max = std::numeric_limits<T>::max();
bool ResultOverflowed;
@@ -334,6 +332,42 @@ void SaturatingAddTestHelper()
EXPECT_EQ(Max, SaturatingAdd(Max, Max));
EXPECT_EQ(Max, SaturatingAdd(Max, Max, &ResultOverflowed));
EXPECT_TRUE(ResultOverflowed);
+
+ EXPECT_EQ(T(6), SaturatingAdd(T(1), T(2), T(3)));
+ EXPECT_EQ(T(6), SaturatingAdd(T(1), T(2), T(3), &ResultOverflowed));
+ EXPECT_FALSE(ResultOverflowed);
+
+ EXPECT_EQ(T(10), SaturatingAdd(T(1), T(2), T(3), T(4)));
+ EXPECT_EQ(T(10), SaturatingAdd(T(1), T(2), T(3), T(4), &ResultOverflowed));
+ EXPECT_FALSE(ResultOverflowed);
+
+ EXPECT_EQ(Max, SaturatingAdd(Max, T(0), T(0)));
+ EXPECT_EQ(Max, SaturatingAdd(Max, T(0), T(0), &ResultOverflowed));
+ EXPECT_FALSE(ResultOverflowed);
+
+ EXPECT_EQ(Max, SaturatingAdd(T(0), T(0), Max));
+ EXPECT_EQ(Max, SaturatingAdd(T(0), T(0), Max, &ResultOverflowed));
+ EXPECT_FALSE(ResultOverflowed);
+
+ EXPECT_EQ(Max, SaturatingAdd(Max, T(0), T(1)));
+ EXPECT_EQ(Max, SaturatingAdd(Max, T(0), T(1), &ResultOverflowed));
+ EXPECT_TRUE(ResultOverflowed);
+
+ EXPECT_EQ(Max, SaturatingAdd(T(0), T(1), Max));
+ EXPECT_EQ(Max, SaturatingAdd(T(0), T(1), Max, &ResultOverflowed));
+ EXPECT_TRUE(ResultOverflowed);
+
+ EXPECT_EQ(Max, SaturatingAdd(T(1), T(Max - 2), T(1)));
+ EXPECT_EQ(Max, SaturatingAdd(T(1), T(Max - 2), T(1), &ResultOverflowed));
+ EXPECT_FALSE(ResultOverflowed);
+
+ EXPECT_EQ(Max, SaturatingAdd(T(1), T(1), T(Max - 2)));
+ EXPECT_EQ(Max, SaturatingAdd(T(1), T(1), T(Max - 2), &ResultOverflowed));
+ EXPECT_FALSE(ResultOverflowed);
+
+ EXPECT_EQ(Max, SaturatingAdd(Max, Max, Max));
+ EXPECT_EQ(Max, SaturatingAdd(Max, Max, Max, &ResultOverflowed));
+ EXPECT_TRUE(ResultOverflowed);
}
TEST(MathExtras, SaturatingAdd) {
More information about the llvm-commits
mailing list