[PATCH] D48138: Add checkMulAdd helper function to CheckedArithmetic
George Karpenkov via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 13 11:36:25 PDT 2018
This revision was automatically updated to reflect the committed changes.
Closed by commit rL334635: Add checkMulAdd helper function to CheckedArithmetic (authored by george.karpenkov, committed by ).
Herald added a subscriber: llvm-commits.
Changed prior to commit:
https://reviews.llvm.org/D48138?vs=151201&id=151213#toc
Repository:
rL LLVM
https://reviews.llvm.org/D48138
Files:
llvm/trunk/include/llvm/Support/CheckedArithmetic.h
llvm/trunk/unittests/Support/CheckedArithmeticTest.cpp
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 T>
+typename std::enable_if<std::is_signed<T>::value, llvm::Optional<T>>::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 <typename T>
@@ -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 T>
+typename std::enable_if<std::is_unsigned<T>::value, llvm::Optional<T>>::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<int64_t>(10, 2), Optional<int64_t>(20));
}
+TEST(CheckedArithmetic, CheckedMulAdd) {
+ const int64_t Max = std::numeric_limits<int64_t>::max();
+ const int64_t Min = std::numeric_limits<int64_t>::min();
+ EXPECT_EQ(checkedMulAdd<int64_t>(Max, 1, 2), None);
+ EXPECT_EQ(checkedMulAdd<int64_t>(1, 1, Max), None);
+ EXPECT_EQ(checkedMulAdd<int64_t>(1, -1, Min), None);
+ EXPECT_EQ(checkedMulAdd<int64_t>(10, 2, 3), Optional<int64_t>(23));
+}
+
TEST(CheckedArithmetic, CheckedMulSmall) {
const int16_t Max = std::numeric_limits<int16_t>::max();
const int16_t Min = std::numeric_limits<int16_t>::min();
@@ -41,6 +50,15 @@
EXPECT_EQ(checkedMul<int16_t>(10, 2), Optional<int16_t>(20));
}
+TEST(CheckedArithmetic, CheckedMulAddSmall) {
+ const int16_t Max = std::numeric_limits<int16_t>::max();
+ const int16_t Min = std::numeric_limits<int16_t>::min();
+ EXPECT_EQ(checkedMulAdd<int16_t>(Max, 1, 2), None);
+ EXPECT_EQ(checkedMulAdd<int16_t>(1, 1, Max), None);
+ EXPECT_EQ(checkedMulAdd<int16_t>(1, -1, Min), None);
+ EXPECT_EQ(checkedMulAdd<int16_t>(10, 2, 3), Optional<int16_t>(23));
+}
+
TEST(CheckedArithmetic, CheckedAddUnsigned) {
const uint64_t Max = std::numeric_limits<uint64_t>::max();
EXPECT_EQ(checkedAddUnsigned<uint64_t>(Max, Max), None);
@@ -55,5 +73,12 @@
EXPECT_EQ(checkedMulUnsigned<uint64_t>(10, 2), Optional<uint64_t>(20));
}
+TEST(CheckedArithmetic, CheckedMulAddUnsigned) {
+ const uint64_t Max = std::numeric_limits<uint64_t>::max();
+ EXPECT_EQ(checkedMulAddUnsigned<uint64_t>(Max, 1, 2), None);
+ EXPECT_EQ(checkedMulAddUnsigned<uint64_t>(1, 1, Max), None);
+ EXPECT_EQ(checkedMulAddUnsigned<uint64_t>(10, 2, 3), Optional<uint64_t>(23));
+}
+
} // namespace
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48138.151213.patch
Type: text/x-patch
Size: 3314 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180613/7a284229/attachment.bin>
More information about the llvm-commits
mailing list