llvm.org GIT mirror llvm / a835321
Utility functions for checked arithmetic Provide checkedAdd and checkedMul functions, providing checked arithmetic on signed integers. Differential Revision: https://reviews.llvm.org/D43704 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326516 91177308-0d34-0410-b5e6-96231b3b80d8 George Karpenkov 2 years ago
3 changed file(s) with 155 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //==-- llvm/Support/CheckedArithmetic.h - Safe arithmetical operations *- C++ //
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains generic functions for operating on integers which
10 // give the indication on whether the operation has overflown.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_CHECKEDARITHMETIC_H
15 #define LLVM_SUPPORT_CHECKEDARITHMETIC_H
16
17 #include "llvm/ADT/APInt.h"
18
19 #include
20
21 namespace {
22
23 /// Utility function to apply a given method of \c APInt \p F to \p LHS and
24 /// \p RHS, and write the output into \p Res.
25 /// \return Whether the operation overflows.
26 template
27 typename std::enable_if::value && sizeof(T) * 8 <= 64,
28 bool>::type
29 checkedOp(T LHS, T RHS, F Op, T *Res = nullptr, bool Signed = true) {
30 llvm::APInt ALHS(/*BitSize=*/sizeof(T) * 8, LHS, Signed);
31 llvm::APInt ARHS(/*BitSize=*/sizeof(T) * 8, RHS, Signed);
32 bool Overflow;
33 llvm::APInt Out = (ALHS.*Op)(ARHS, Overflow);
34 if (Res)
35 *Res = Signed ? Out.getSExtValue() : Out.getZExtValue();
36 return Overflow;
37 }
38 }
39
40 namespace llvm {
41
42 /// Add two signed integers \p LHS and \p RHS, write into \p Res if non-null.
43 /// Does not guarantee saturating arithmetic.
44 /// \return Whether the result overflows.
45 template
46 typename std::enable_if::value, bool>::type
47 checkedAdd(T LHS, T RHS, T *Res = nullptr) {
48 return checkedOp(LHS, RHS, &llvm::APInt::sadd_ov, Res);
49 }
50
51 /// Multiply two signed integers \p LHS and \p RHS, write into \p Res if
52 /// non-null.
53 /// Does not guarantee saturating arithmetic.
54 /// \return Whether the result overflows.
55 template
56 typename std::enable_if::value, bool>::type
57 checkedMul(T LHS, T RHS, T *Res = nullptr) {
58 return checkedOp(LHS, RHS, &llvm::APInt::smul_ov, Res);
59 }
60
61 /// Add two unsigned integers \p LHS and \p RHS, write into \p Res if non-null.
62 /// Does not guarantee saturating arithmetic.
63 /// \return Whether the result overflows.
64 template
65 typename std::enable_if::value, bool>::type
66 checkedAddUnsigned(T LHS, T RHS, T *Res = nullptr) {
67 return checkedOp(LHS, RHS, &llvm::APInt::uadd_ov, Res, /*Signed=*/false);
68 }
69
70 /// Multiply two unsigned integers \p LHS and \p RHS, write into \p Res if
71 /// non-null.
72 /// Does not guarantee saturating arithmetic.
73 /// \return Whether the result overflows.
74 template
75 typename std::enable_if::value, bool>::type
76 checkedMulUnsigned(T LHS, T RHS, T *Res = nullptr) {
77 return checkedOp(LHS, RHS, &llvm::APInt::umul_ov, Res, /*Signed=*/false);
78 }
79
80 } // End llvm namespace
81
82 #endif
1212 CachePruningTest.cpp
1313 CrashRecoveryTest.cpp
1414 Casting.cpp
15 CheckedArithmeticTest.cpp
1516 Chrono.cpp
1617 CommandLineTest.cpp
1718 CompressionTest.cpp
0 #include "llvm/Support/CheckedArithmetic.h"
1 #include "gtest/gtest.h"
2
3 using namespace llvm;
4
5 namespace {
6
7 TEST(CheckedArithmetic, CheckedAdd) {
8 int64_t Out;
9 const int64_t Max = std::numeric_limits::max();
10 const int64_t Min = std::numeric_limits::min();
11 EXPECT_EQ(checkedAdd(Max, Max, &Out), true);
12 EXPECT_EQ(checkedAdd(Min, -1, &Out), true);
13 EXPECT_EQ(checkedAdd(Max, 1, &Out), true);
14 EXPECT_EQ(checkedAdd(10, 1, &Out), false);
15 EXPECT_EQ(Out, 11);
16 }
17
18 TEST(CheckedArithmetic, CheckedAddSmall) {
19 int16_t Out;
20 const int16_t Max = std::numeric_limits::max();
21 const int16_t Min = std::numeric_limits::min();
22 EXPECT_EQ(checkedAdd(Max, Max, &Out), true);
23 EXPECT_EQ(checkedAdd(Min, -1, &Out), true);
24 EXPECT_EQ(checkedAdd(Max, 1, &Out), true);
25 EXPECT_EQ(checkedAdd(10, 1, &Out), false);
26 EXPECT_EQ(Out, 11);
27 }
28
29 TEST(CheckedArithmetic, CheckedMul) {
30 int64_t Out;
31 const int64_t Max = std::numeric_limits::max();
32 const int64_t Min = std::numeric_limits::min();
33 EXPECT_EQ(checkedMul(Max, 2, &Out), true);
34 EXPECT_EQ(checkedMul(Max, Max, &Out), true);
35 EXPECT_EQ(checkedMul(Min, 2, &Out), true);
36 EXPECT_EQ(checkedMul(10, 2, &Out), false);
37 EXPECT_EQ(Out, 20);
38 }
39
40 TEST(CheckedArithmetic, CheckedMulSmall) {
41 int16_t Out;
42 const int16_t Max = std::numeric_limits::max();
43 const int16_t Min = std::numeric_limits::min();
44 EXPECT_EQ(checkedMul(Max, 2, &Out), true);
45 EXPECT_EQ(checkedMul(Max, Max, &Out), true);
46 EXPECT_EQ(checkedMul(Min, 2, &Out), true);
47 EXPECT_EQ(checkedMul(10, 2, &Out), false);
48 EXPECT_EQ(Out, 20);
49 }
50
51 TEST(CheckedArithmetic, CheckedAddUnsigned) {
52 uint64_t Out;
53 const uint64_t Max = std::numeric_limits::max();
54 EXPECT_EQ(checkedAddUnsigned(Max, Max, &Out), true);
55 EXPECT_EQ(checkedAddUnsigned(Max, 1, &Out), true);
56 EXPECT_EQ(checkedAddUnsigned(10, 1, &Out), false);
57 EXPECT_EQ(Out, uint64_t(11));
58 }
59
60 TEST(CheckedArithmetic, CheckedMulUnsigned) {
61 uint64_t Out;
62 const uint64_t Max = std::numeric_limits::max();
63 EXPECT_EQ(checkedMulUnsigned(Max, 2, &Out), true);
64 EXPECT_EQ(checkedMulUnsigned(Max, Max, &Out), true);
65 EXPECT_EQ(checkedMulUnsigned(10, 2, &Out), false);
66 EXPECT_EQ(Out, uint64_t(20));
67 }
68
69
70 } // namespace