[PATCH] D139036: [Support][MathExtras] Add variadic SaturatingAdd
Alexander Shaposhnikov via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 30 11:27:39 PST 2022
alexander-shaposhnikov updated this revision to Diff 479036.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D139036/new/
https://reviews.llvm.org/D139036
Files:
llvm/include/llvm/Support/MathExtras.h
llvm/unittests/Support/MathExtrasTest.cpp
Index: llvm/unittests/Support/MathExtrasTest.cpp
===================================================================
--- llvm/unittests/Support/MathExtrasTest.cpp
+++ llvm/unittests/Support/MathExtrasTest.cpp
@@ -309,9 +309,7 @@
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,38 @@
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(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) {
Index: llvm/include/llvm/Support/MathExtras.h
===================================================================
--- llvm/include/llvm/Support/MathExtras.h
+++ llvm/include/llvm/Support/MathExtras.h
@@ -753,6 +753,18 @@
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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139036.479036.patch
Type: text/x-patch
Size: 2947 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221130/00e2f1c9/attachment.bin>
More information about the llvm-commits
mailing list