[llvm] r326516 - Utility functions for checked arithmetic

George Karpenkov via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 1 16:30:43 PST 2018


Author: george.karpenkov
Date: Thu Mar  1 16:30:43 2018
New Revision: 326516

URL: http://llvm.org/viewvc/llvm-project?rev=326516&view=rev
Log:
Utility functions for checked arithmetic

Provide checkedAdd and checkedMul functions, providing checked
arithmetic on signed integers.

Differential Revision: https://reviews.llvm.org/D43704

Added:
    llvm/trunk/include/llvm/Support/CheckedArithmetic.h
    llvm/trunk/unittests/Support/CheckedArithmeticTest.cpp
Modified:
    llvm/trunk/unittests/Support/CMakeLists.txt

Added: llvm/trunk/include/llvm/Support/CheckedArithmetic.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/CheckedArithmetic.h?rev=326516&view=auto
==============================================================================
--- llvm/trunk/include/llvm/Support/CheckedArithmetic.h (added)
+++ llvm/trunk/include/llvm/Support/CheckedArithmetic.h Thu Mar  1 16:30:43 2018
@@ -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

Modified: llvm/trunk/unittests/Support/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/CMakeLists.txt?rev=326516&r1=326515&r2=326516&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/CMakeLists.txt (original)
+++ llvm/trunk/unittests/Support/CMakeLists.txt Thu Mar  1 16:30:43 2018
@@ -13,6 +13,7 @@ add_llvm_unittest(SupportTests
   CachePruningTest.cpp
   CrashRecoveryTest.cpp
   Casting.cpp
+  CheckedArithmeticTest.cpp
   Chrono.cpp
   CommandLineTest.cpp
   CompressionTest.cpp

Added: llvm/trunk/unittests/Support/CheckedArithmeticTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/CheckedArithmeticTest.cpp?rev=326516&view=auto
==============================================================================
--- llvm/trunk/unittests/Support/CheckedArithmeticTest.cpp (added)
+++ llvm/trunk/unittests/Support/CheckedArithmeticTest.cpp Thu Mar  1 16:30:43 2018
@@ -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




More information about the llvm-commits mailing list