Index: include/llvm/Support/CheckedArithmetic.h =================================================================== --- /dev/null +++ include/llvm/Support/CheckedArithmetic.h @@ -0,0 +1,66 @@ +//==-- 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 +#include + +namespace llvm { + +/// Utility function to apply a given method of \c APInt to \p LHS and \p RHS, +/// and write the output into \p Res. +/// \return Whether the operation overflows. +template +typename std::enable_if::value && sizeof(T) * 8 <= 64, + bool>::type +checkedOp( + T LHS, T RHS, + std::function Op, + T *Res = nullptr) { + llvm::APInt ALHS(/*BitSize=*/sizeof(T) * 8, LHS, /*Signed=*/true); + llvm::APInt ARHS(/*BitSize=*/sizeof(T) * 8, RHS, /*Signed=*/true); + bool Overflow; + llvm::APInt Out = Op(&ALHS, ARHS, Overflow); + if (!Overflow && Res) + *Res = Out.getSExtValue(); + return Overflow; +} + +/// Add two signed integers \p LHS and \p RHS, write into \p Res if non-null +/// and the operation does not oveflow. +/// Does not guarantee saturating arithmetic. +/// \return Whether the result overflows. +template +typename std::enable_if::value, bool>::type +checkedAdd(T LHS, T RHS, T *Res = nullptr) { + return checkedOp(LHS, RHS, &llvm::APInt::sadd_ov, Res); +} + +/// Multiply two integers \p LHS and \p RHS, write into \p Res if non-null and +/// the operation does not oveflow. +/// Does not guarantee saturating arithmetic. +/// \return Whether the result overflows. +template +typename std::enable_if::value, bool>::type +checkedMul(T LHS, T RHS, T *Res = nullptr) { + return checkedOp(LHS, RHS, &llvm::APInt::smul_ov, Res); +} + +} // End llvm namespace + +#endif Index: unittests/Support/CMakeLists.txt =================================================================== --- unittests/Support/CMakeLists.txt +++ unittests/Support/CMakeLists.txt @@ -13,6 +13,7 @@ CachePruningTest.cpp CrashRecoveryTest.cpp Casting.cpp + CheckedArithmeticTest.cpp Chrono.cpp CommandLineTest.cpp CompressionTest.cpp Index: unittests/Support/CheckedArithmeticTest.cpp =================================================================== --- /dev/null +++ unittests/Support/CheckedArithmeticTest.cpp @@ -0,0 +1,52 @@ +#include "llvm/Support/CheckedArithmetic.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { + +TEST(CheckedArithmetic, CheckedAdd) { + const int64_t Max = std::numeric_limits::max(); + const int64_t Min = std::numeric_limits::min(); + EXPECT_EQ(checkedAdd(Max, Max), true); + EXPECT_EQ(checkedAdd(Min, -1), true); + EXPECT_EQ(checkedAdd(Max, 1), true); + int64_t Out; + EXPECT_EQ(checkedAdd(10, 1, &Out), false); + EXPECT_EQ(Out, 11); +} + +TEST(CheckedArithmetic, CheckedAddSmall) { + const int16_t Max = std::numeric_limits::max(); + const int16_t Min = std::numeric_limits::min(); + EXPECT_EQ(checkedAdd(Max, Max), true); + EXPECT_EQ(checkedAdd(Min, -1), true); + EXPECT_EQ(checkedAdd(Max, 1), true); + int16_t Out; + EXPECT_EQ(checkedAdd(10, 1, &Out), false); + EXPECT_EQ(Out, 11); +} + +TEST(CheckedArithmetic, CheckedMul) { + const int64_t Max = std::numeric_limits::max(); + const int64_t Min = std::numeric_limits::min(); + EXPECT_EQ(checkedMul(Max, 2), true); + EXPECT_EQ(checkedMul(Max, Max), true); + EXPECT_EQ(checkedMul(Min, 2), true); + int64_t Out; + EXPECT_EQ(checkedMul(10, 2, &Out), false); + EXPECT_EQ(Out, 20); +} + +TEST(CheckedArithmetic, CheckedMulSmall) { + const int16_t Max = std::numeric_limits::max(); + const int16_t Min = std::numeric_limits::min(); + EXPECT_EQ(checkedMul(Max, 2), true); + EXPECT_EQ(checkedMul(Max, Max), true); + EXPECT_EQ(checkedMul(Min, 2), true); + int16_t Out; + EXPECT_EQ(checkedMul(10, 2, &Out), false); + EXPECT_EQ(Out, 20); +} + +} // namespace