1 //==-- llvm/Support/CheckedArithmetic.h - Safe arithmetical operations *- C++ //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains generic functions for operating on integers which
10 // give the indication on whether the operation has overflown.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_CHECKEDARITHMETIC_H
15 #define LLVM_SUPPORT_CHECKEDARITHMETIC_H
17 #include "llvm/ADT/APInt.h"
18 #include "llvm/ADT/Optional.h"
20 #include <type_traits>
24 /// Utility function to apply a given method of \c APInt \p F to \p LHS and
26 /// \return Empty optional if the operation overflows, or result otherwise.
27 template <typename T
, typename F
>
28 typename
std::enable_if
<std::is_integral
<T
>::value
&& sizeof(T
) * 8 <= 64,
29 llvm::Optional
<T
>>::type
30 checkedOp(T LHS
, T RHS
, F Op
, bool Signed
= true) {
31 llvm::APInt
ALHS(/*BitSize=*/sizeof(T
) * 8, LHS
, Signed
);
32 llvm::APInt
ARHS(/*BitSize=*/sizeof(T
) * 8, RHS
, Signed
);
34 llvm::APInt Out
= (ALHS
.*Op
)(ARHS
, Overflow
);
37 return Signed
? Out
.getSExtValue() : Out
.getZExtValue();
43 /// Add two signed integers \p LHS and \p RHS.
44 /// \return Optional of sum if no signed overflow occurred,
45 /// \c None otherwise.
47 typename
std::enable_if
<std::is_signed
<T
>::value
, llvm::Optional
<T
>>::type
48 checkedAdd(T LHS
, T RHS
) {
49 return checkedOp(LHS
, RHS
, &llvm::APInt::sadd_ov
);
52 /// Subtract two signed integers \p LHS and \p RHS.
53 /// \return Optional of sum if no signed overflow occurred,
54 /// \c None otherwise.
56 typename
std::enable_if
<std::is_signed
<T
>::value
, llvm::Optional
<T
>>::type
57 checkedSub(T LHS
, T RHS
) {
58 return checkedOp(LHS
, RHS
, &llvm::APInt::ssub_ov
);
61 /// Multiply two signed integers \p LHS and \p RHS.
62 /// \return Optional of product if no signed overflow occurred,
63 /// \c None otherwise.
65 typename
std::enable_if
<std::is_signed
<T
>::value
, llvm::Optional
<T
>>::type
66 checkedMul(T LHS
, T RHS
) {
67 return checkedOp(LHS
, RHS
, &llvm::APInt::smul_ov
);
70 /// Multiply A and B, and add C to the resulting product.
71 /// \return Optional of result if no signed overflow occurred,
72 /// \c None otherwise.
74 typename
std::enable_if
<std::is_signed
<T
>::value
, llvm::Optional
<T
>>::type
75 checkedMulAdd(T A
, T B
, T C
) {
76 if (auto Product
= checkedMul(A
, B
))
77 return checkedAdd(*Product
, C
);
81 /// Add two unsigned integers \p LHS and \p RHS.
82 /// \return Optional of sum if no unsigned overflow occurred,
83 /// \c None otherwise.
85 typename
std::enable_if
<std::is_unsigned
<T
>::value
, llvm::Optional
<T
>>::type
86 checkedAddUnsigned(T LHS
, T RHS
) {
87 return checkedOp(LHS
, RHS
, &llvm::APInt::uadd_ov
, /*Signed=*/false);
90 /// Multiply two unsigned integers \p LHS and \p RHS.
91 /// \return Optional of product if no unsigned overflow occurred,
92 /// \c None otherwise.
94 typename
std::enable_if
<std::is_unsigned
<T
>::value
, llvm::Optional
<T
>>::type
95 checkedMulUnsigned(T LHS
, T RHS
) {
96 return checkedOp(LHS
, RHS
, &llvm::APInt::umul_ov
, /*Signed=*/false);
99 /// Multiply unsigned integers A and B, and add C to the resulting product.
100 /// \return Optional of result if no unsigned overflow occurred,
101 /// \c None otherwise.
102 template <typename T
>
103 typename
std::enable_if
<std::is_unsigned
<T
>::value
, llvm::Optional
<T
>>::type
104 checkedMulAddUnsigned(T A
, T B
, T C
) {
105 if (auto Product
= checkedMulUnsigned(A
, B
))
106 return checkedAddUnsigned(*Product
, C
);
110 } // End llvm namespace