Line data Source code
1 : //==-- llvm/Support/CheckedArithmetic.h - Safe arithmetical operations *- C++ //
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // This file contains generic functions for operating on integers which
11 : // give the indication on whether the operation has overflown.
12 : //
13 : //===----------------------------------------------------------------------===//
14 :
15 : #ifndef LLVM_SUPPORT_CHECKEDARITHMETIC_H
16 : #define LLVM_SUPPORT_CHECKEDARITHMETIC_H
17 :
18 : #include "llvm/ADT/APInt.h"
19 : #include "llvm/ADT/Optional.h"
20 :
21 : #include <type_traits>
22 :
23 : namespace {
24 :
25 : /// Utility function to apply a given method of \c APInt \p F to \p LHS and
26 : /// \p RHS.
27 : /// \return Empty optional if the operation overflows, or result otherwise.
28 : template <typename T, typename F>
29 : typename std::enable_if<std::is_integral<T>::value && sizeof(T) * 8 <= 64,
30 : llvm::Optional<T>>::type
31 3722 : checkedOp(T LHS, T RHS, F Op, bool Signed = true) {
32 3710 : llvm::APInt ALHS(/*BitSize=*/sizeof(T) * 8, LHS, Signed);
33 3710 : llvm::APInt ARHS(/*BitSize=*/sizeof(T) * 8, RHS, Signed);
34 : bool Overflow;
35 3722 : llvm::APInt Out = (ALHS.*Op)(ARHS, Overflow);
36 3722 : if (Overflow)
37 : return llvm::None;
38 3699 : return Signed ? Out.getSExtValue() : Out.getZExtValue();
39 : }
40 12 : }
41 :
42 : namespace llvm {
43 :
44 12 : /// Add two signed integers \p LHS and \p RHS.
45 12 : /// \return Optional of sum if no signed overflow occurred,
46 : /// \c None otherwise.
47 6 : template <typename T>
48 : typename std::enable_if<std::is_signed<T>::value, llvm::Optional<T>>::type
49 16 : checkedAdd(T LHS, T RHS) {
50 1852 : return checkedOp(LHS, RHS, &llvm::APInt::sadd_ov);
51 16 : }
52 :
53 16 : /// Multiply two signed integers \p LHS and \p RHS.
54 16 : /// \return Optional of product if no signed overflow occurred,
55 : /// \c None otherwise.
56 14 : template <typename T>
57 : typename std::enable_if<std::is_signed<T>::value, llvm::Optional<T>>::type
58 16 : checkedMul(T LHS, T RHS) {
59 1858 : return checkedOp(LHS, RHS, &llvm::APInt::smul_ov);
60 16 : }
61 :
62 16 : /// Multiply A and B, and add C to the resulting product.
63 16 : /// \return Optional of result if no signed overflow occurred,
64 : /// \c None otherwise.
65 7 : template <typename T>
66 : typename std::enable_if<std::is_signed<T>::value, llvm::Optional<T>>::type
67 1842 : checkedMulAdd(T A, T B, T C) {
68 1842 : if (auto Product = checkedMul(A, B))
69 1836 : return checkedAdd(*Product, C);
70 : return llvm::None;
71 : }
72 :
73 : /// Add two unsigned integers \p LHS and \p RHS.
74 : /// \return Optional of sum if no unsigned overflow occurred,
75 : /// \c None otherwise.
76 : template <typename T>
77 8 : typename std::enable_if<std::is_unsigned<T>::value, llvm::Optional<T>>::type
78 : checkedAddUnsigned(T LHS, T RHS) {
79 : return checkedOp(LHS, RHS, &llvm::APInt::uadd_ov, /*Signed=*/false);
80 : }
81 :
82 : /// Multiply two unsigned integers \p LHS and \p RHS.
83 : /// \return Optional of product if no unsigned overflow occurred,
84 : /// \c None otherwise.
85 : template <typename T>
86 8 : typename std::enable_if<std::is_unsigned<T>::value, llvm::Optional<T>>::type
87 : checkedMulUnsigned(T LHS, T RHS) {
88 : return checkedOp(LHS, RHS, &llvm::APInt::umul_ov, /*Signed=*/false);
89 : }
90 :
91 : /// Multiply unsigned integers A and B, and add C to the resulting product.
92 : /// \return Optional of result if no unsigned overflow occurred,
93 : /// \c None otherwise.
94 8 : template <typename T>
95 8 : typename std::enable_if<std::is_unsigned<T>::value, llvm::Optional<T>>::type
96 8 : checkedMulAddUnsigned(T A, T B, T C) {
97 : if (auto Product = checkedMulUnsigned(A, B))
98 : return checkedAddUnsigned(*Product, C);
99 4 : return llvm::None;
100 4 : }
101 4 :
102 : } // End llvm namespace
103 :
104 4 : #endif
|