Index: include/llvm/Support/CheckedArithmetic.h =================================================================== --- /dev/null +++ include/llvm/Support/CheckedArithmetic.h @@ -0,0 +1,86 @@ +//==-- 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 { + +/// 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 std::enable_if::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 (!Overflow && Res) + *Res = Out.getSExtValue(); + return Overflow; +} +} + +namespace llvm { + +/// 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 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 +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 +/// and the operation does not oveflow. +/// Does not guarantee saturating arithmetic. +/// \return Whether the result overflows. +template +typename std::enable_if::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 and the operation does not oveflow. +/// Does not guarantee saturating arithmetic. +/// \return Whether the result overflows. +template +typename std::enable_if::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 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,71 @@ +#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); +} + +TEST(CheckedArithmetic, CheckedAddUnsigned) { + const uint64_t Max = std::numeric_limits::max(); + EXPECT_EQ(checkedAddUnsigned(Max, Max), true); + EXPECT_EQ(checkedAddUnsigned(Max, 1), true); + uint64_t Out; + EXPECT_EQ(checkedAddUnsigned(10, 1, &Out), false); + EXPECT_EQ(Out, (uint64_t)11); +} + +TEST(CheckedArithmetic, CheckedMulUnsigned) { + const uint64_t Max = std::numeric_limits::max(); + EXPECT_EQ(checkedMulUnsigned(Max, 2), true); + EXPECT_EQ(checkedMulUnsigned(Max, Max), true); + uint64_t Out; + EXPECT_EQ(checkedMulUnsigned(10, 2, &Out), false); + EXPECT_EQ(Out, (uint64_t)20); +} + + +} // namespace