diff options
author | George Karpenkov <ekarpenkov@apple.com> | 2018-03-02 00:30:43 +0000 |
---|---|---|
committer | George Karpenkov <ekarpenkov@apple.com> | 2018-03-02 00:30:43 +0000 |
commit | 3f547fe8571c2bb6b3fd10bef6af08da1436bd85 (patch) | |
tree | 7ce0cdce3bbeb4ffbb46e04754a1d4d7583f753c | |
parent | 113446ca372df474dd51384852c65329c59bff13 (diff) | |
download | bcm5719-llvm-3f547fe8571c2bb6b3fd10bef6af08da1436bd85.tar.gz bcm5719-llvm-3f547fe8571c2bb6b3fd10bef6af08da1436bd85.zip |
Utility functions for checked arithmetic
Provide checkedAdd and checkedMul functions, providing checked
arithmetic on signed integers.
Differential Revision: https://reviews.llvm.org/D43704
llvm-svn: 326516
-rw-r--r-- | llvm/include/llvm/Support/CheckedArithmetic.h | 83 | ||||
-rw-r--r-- | llvm/unittests/Support/CMakeLists.txt | 1 | ||||
-rw-r--r-- | llvm/unittests/Support/CheckedArithmeticTest.cpp | 71 |
3 files changed, 155 insertions, 0 deletions
diff --git a/llvm/include/llvm/Support/CheckedArithmetic.h b/llvm/include/llvm/Support/CheckedArithmetic.h new file mode 100644 index 00000000000..68bce0621a3 --- /dev/null +++ b/llvm/include/llvm/Support/CheckedArithmetic.h @@ -0,0 +1,83 @@ +//==-- llvm/Support/CheckedArithmetic.h - Safe arithmetical operations *- C++ // +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file contains generic functions for operating on integers which +// give the indication on whether the operation has overflown. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_SUPPORT_CHECKEDARITHMETIC_H +#define LLVM_SUPPORT_CHECKEDARITHMETIC_H + +#include "llvm/ADT/APInt.h" + +#include <type_traits> + +namespace { + +/// Utility function to apply a given method of \c APInt \p F to \p LHS and +/// \p RHS, and write the output into \p Res. +/// \return Whether the operation overflows. +template <typename T, typename F> +typename std::enable_if<std::is_integral<T>::value && sizeof(T) * 8 <= 64, + bool>::type +checkedOp(T LHS, T RHS, F Op, T *Res = nullptr, bool Signed = true) { + llvm::APInt ALHS(/*BitSize=*/sizeof(T) * 8, LHS, Signed); + llvm::APInt ARHS(/*BitSize=*/sizeof(T) * 8, RHS, Signed); + bool Overflow; + llvm::APInt Out = (ALHS.*Op)(ARHS, Overflow); + if (Res) + *Res = Signed ? Out.getSExtValue() : Out.getZExtValue(); + return Overflow; +} +} + +namespace llvm { + +/// Add two signed integers \p LHS and \p RHS, write into \p Res if non-null. +/// Does not guarantee saturating arithmetic. +/// \return Whether the result overflows. +template <typename T> +typename std::enable_if<std::is_signed<T>::value, bool>::type +checkedAdd(T LHS, T RHS, T *Res = nullptr) { + return checkedOp(LHS, RHS, &llvm::APInt::sadd_ov, Res); +} + +/// Multiply two signed integers \p LHS and \p RHS, write into \p Res if +/// non-null. +/// Does not guarantee saturating arithmetic. +/// \return Whether the result overflows. +template <typename T> +typename std::enable_if<std::is_signed<T>::value, bool>::type +checkedMul(T LHS, T RHS, T *Res = nullptr) { + return checkedOp(LHS, RHS, &llvm::APInt::smul_ov, Res); +} + +/// Add two unsigned integers \p LHS and \p RHS, write into \p Res if non-null. +/// Does not guarantee saturating arithmetic. +/// \return Whether the result overflows. +template <typename T> +typename std::enable_if<std::is_unsigned<T>::value, bool>::type +checkedAddUnsigned(T LHS, T RHS, T *Res = nullptr) { + return checkedOp(LHS, RHS, &llvm::APInt::uadd_ov, Res, /*Signed=*/false); +} + +/// Multiply two unsigned integers \p LHS and \p RHS, write into \p Res if +/// non-null. +/// Does not guarantee saturating arithmetic. +/// \return Whether the result overflows. +template <typename T> +typename std::enable_if<std::is_unsigned<T>::value, bool>::type +checkedMulUnsigned(T LHS, T RHS, T *Res = nullptr) { + return checkedOp(LHS, RHS, &llvm::APInt::umul_ov, Res, /*Signed=*/false); +} + +} // End llvm namespace + +#endif diff --git a/llvm/unittests/Support/CMakeLists.txt b/llvm/unittests/Support/CMakeLists.txt index 1eb3a04d91d..89e16ef4b3f 100644 --- a/llvm/unittests/Support/CMakeLists.txt +++ b/llvm/unittests/Support/CMakeLists.txt @@ -13,6 +13,7 @@ add_llvm_unittest(SupportTests CachePruningTest.cpp CrashRecoveryTest.cpp Casting.cpp + CheckedArithmeticTest.cpp Chrono.cpp CommandLineTest.cpp CompressionTest.cpp diff --git a/llvm/unittests/Support/CheckedArithmeticTest.cpp b/llvm/unittests/Support/CheckedArithmeticTest.cpp new file mode 100644 index 00000000000..7fc63498e2b --- /dev/null +++ b/llvm/unittests/Support/CheckedArithmeticTest.cpp @@ -0,0 +1,71 @@ +#include "llvm/Support/CheckedArithmetic.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { + +TEST(CheckedArithmetic, CheckedAdd) { + int64_t Out; + const int64_t Max = std::numeric_limits<int64_t>::max(); + const int64_t Min = std::numeric_limits<int64_t>::min(); + EXPECT_EQ(checkedAdd<int64_t>(Max, Max, &Out), true); + EXPECT_EQ(checkedAdd<int64_t>(Min, -1, &Out), true); + EXPECT_EQ(checkedAdd<int64_t>(Max, 1, &Out), true); + EXPECT_EQ(checkedAdd<int64_t>(10, 1, &Out), false); + EXPECT_EQ(Out, 11); +} + +TEST(CheckedArithmetic, CheckedAddSmall) { + int16_t Out; + const int16_t Max = std::numeric_limits<int16_t>::max(); + const int16_t Min = std::numeric_limits<int16_t>::min(); + EXPECT_EQ(checkedAdd<int16_t>(Max, Max, &Out), true); + EXPECT_EQ(checkedAdd<int16_t>(Min, -1, &Out), true); + EXPECT_EQ(checkedAdd<int16_t>(Max, 1, &Out), true); + EXPECT_EQ(checkedAdd<int16_t>(10, 1, &Out), false); + EXPECT_EQ(Out, 11); +} + +TEST(CheckedArithmetic, CheckedMul) { + int64_t Out; + const int64_t Max = std::numeric_limits<int64_t>::max(); + const int64_t Min = std::numeric_limits<int64_t>::min(); + EXPECT_EQ(checkedMul<int64_t>(Max, 2, &Out), true); + EXPECT_EQ(checkedMul<int64_t>(Max, Max, &Out), true); + EXPECT_EQ(checkedMul<int64_t>(Min, 2, &Out), true); + EXPECT_EQ(checkedMul<int64_t>(10, 2, &Out), false); + EXPECT_EQ(Out, 20); +} + +TEST(CheckedArithmetic, CheckedMulSmall) { + int16_t Out; + const int16_t Max = std::numeric_limits<int16_t>::max(); + const int16_t Min = std::numeric_limits<int16_t>::min(); + EXPECT_EQ(checkedMul<int16_t>(Max, 2, &Out), true); + EXPECT_EQ(checkedMul<int16_t>(Max, Max, &Out), true); + EXPECT_EQ(checkedMul<int16_t>(Min, 2, &Out), true); + EXPECT_EQ(checkedMul<int16_t>(10, 2, &Out), false); + EXPECT_EQ(Out, 20); +} + +TEST(CheckedArithmetic, CheckedAddUnsigned) { + uint64_t Out; + const uint64_t Max = std::numeric_limits<uint64_t>::max(); + EXPECT_EQ(checkedAddUnsigned<uint64_t>(Max, Max, &Out), true); + EXPECT_EQ(checkedAddUnsigned<uint64_t>(Max, 1, &Out), true); + EXPECT_EQ(checkedAddUnsigned<uint64_t>(10, 1, &Out), false); + EXPECT_EQ(Out, uint64_t(11)); +} + +TEST(CheckedArithmetic, CheckedMulUnsigned) { + uint64_t Out; + const uint64_t Max = std::numeric_limits<uint64_t>::max(); + EXPECT_EQ(checkedMulUnsigned<uint64_t>(Max, 2, &Out), true); + EXPECT_EQ(checkedMulUnsigned<uint64_t>(Max, Max, &Out), true); + EXPECT_EQ(checkedMulUnsigned<uint64_t>(10, 2, &Out), false); + EXPECT_EQ(Out, uint64_t(20)); +} + + +} // namespace |