blob: 8f7fbde8c3aeeccaa94e27e47fbb328e795f2d22 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//==-- llvm/Support/CheckedArithmetic.h - Safe arithmetical operations *- C++ //
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010018#include "llvm/ADT/Optional.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010019
20#include <type_traits>
21
22namespace {
23
24/// Utility function to apply a given method of \c APInt \p F to \p LHS and
Andrew Scullcdfcccc2018-10-05 20:58:37 +010025/// \p RHS.
26/// \return Empty optional if the operation overflows, or result otherwise.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010027template <typename T, typename F>
28typename std::enable_if<std::is_integral<T>::value && sizeof(T) * 8 <= 64,
Andrew Scullcdfcccc2018-10-05 20:58:37 +010029 llvm::Optional<T>>::type
30checkedOp(T LHS, T RHS, F Op, bool Signed = true) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010031 llvm::APInt ALHS(/*BitSize=*/sizeof(T) * 8, LHS, Signed);
32 llvm::APInt ARHS(/*BitSize=*/sizeof(T) * 8, RHS, Signed);
33 bool Overflow;
34 llvm::APInt Out = (ALHS.*Op)(ARHS, Overflow);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010035 if (Overflow)
36 return llvm::None;
37 return Signed ? Out.getSExtValue() : Out.getZExtValue();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010038}
39}
40
41namespace llvm {
42
Andrew Scullcdfcccc2018-10-05 20:58:37 +010043/// Add two signed integers \p LHS and \p RHS.
44/// \return Optional of sum if no signed overflow occurred,
45/// \c None otherwise.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010046template <typename T>
Andrew Scullcdfcccc2018-10-05 20:58:37 +010047typename std::enable_if<std::is_signed<T>::value, llvm::Optional<T>>::type
48checkedAdd(T LHS, T RHS) {
49 return checkedOp(LHS, RHS, &llvm::APInt::sadd_ov);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050}
51
Andrew Scullcdfcccc2018-10-05 20:58:37 +010052/// Multiply two signed integers \p LHS and \p RHS.
53/// \return Optional of product if no signed overflow occurred,
54/// \c None otherwise.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010055template <typename T>
Andrew Scullcdfcccc2018-10-05 20:58:37 +010056typename std::enable_if<std::is_signed<T>::value, llvm::Optional<T>>::type
57checkedMul(T LHS, T RHS) {
58 return checkedOp(LHS, RHS, &llvm::APInt::smul_ov);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010059}
60
Andrew Scullcdfcccc2018-10-05 20:58:37 +010061/// Multiply A and B, and add C to the resulting product.
62/// \return Optional of result if no signed overflow occurred,
63/// \c None otherwise.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010064template <typename T>
Andrew Scullcdfcccc2018-10-05 20:58:37 +010065typename std::enable_if<std::is_signed<T>::value, llvm::Optional<T>>::type
66checkedMulAdd(T A, T B, T C) {
67 if (auto Product = checkedMul(A, B))
68 return checkedAdd(*Product, C);
69 return llvm::None;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010070}
71
Andrew Scullcdfcccc2018-10-05 20:58:37 +010072/// Add two unsigned integers \p LHS and \p RHS.
73/// \return Optional of sum if no unsigned overflow occurred,
74/// \c None otherwise.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010075template <typename T>
Andrew Scullcdfcccc2018-10-05 20:58:37 +010076typename std::enable_if<std::is_unsigned<T>::value, llvm::Optional<T>>::type
77checkedAddUnsigned(T LHS, T RHS) {
78 return checkedOp(LHS, RHS, &llvm::APInt::uadd_ov, /*Signed=*/false);
79}
80
81/// Multiply two unsigned integers \p LHS and \p RHS.
82/// \return Optional of product if no unsigned overflow occurred,
83/// \c None otherwise.
84template <typename T>
85typename std::enable_if<std::is_unsigned<T>::value, llvm::Optional<T>>::type
86checkedMulUnsigned(T LHS, T RHS) {
87 return checkedOp(LHS, RHS, &llvm::APInt::umul_ov, /*Signed=*/false);
88}
89
90/// Multiply unsigned integers A and B, and add C to the resulting product.
91/// \return Optional of result if no unsigned overflow occurred,
92/// \c None otherwise.
93template <typename T>
94typename std::enable_if<std::is_unsigned<T>::value, llvm::Optional<T>>::type
95checkedMulAddUnsigned(T A, T B, T C) {
96 if (auto Product = checkedMulUnsigned(A, B))
97 return checkedAddUnsigned(*Product, C);
98 return llvm::None;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010099}
100
101} // End llvm namespace
102
103#endif