Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //==-- llvm/Support/CheckedArithmetic.h - Safe arithmetical operations *- C++ // |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 18 | #include "llvm/ADT/Optional.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 19 | |
| 20 | #include <type_traits> |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | /// Utility function to apply a given method of \c APInt \p F to \p LHS and |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 25 | /// \p RHS. |
| 26 | /// \return Empty optional if the operation overflows, or result otherwise. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 27 | template <typename T, typename F> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 28 | std::enable_if_t<std::is_integral<T>::value && sizeof(T) * 8 <= 64, |
| 29 | llvm::Optional<T>> |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 30 | checkedOp(T LHS, T RHS, F Op, bool Signed = true) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 31 | llvm::APInt ALHS(sizeof(T) * 8, LHS, Signed); |
| 32 | llvm::APInt ARHS(sizeof(T) * 8, RHS, Signed); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 33 | bool Overflow; |
| 34 | llvm::APInt Out = (ALHS.*Op)(ARHS, Overflow); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 35 | if (Overflow) |
| 36 | return llvm::None; |
| 37 | return Signed ? Out.getSExtValue() : Out.getZExtValue(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
| 41 | namespace llvm { |
| 42 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 43 | /// Add two signed integers \p LHS and \p RHS. |
| 44 | /// \return Optional of sum if no signed overflow occurred, |
| 45 | /// \c None otherwise. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 46 | template <typename T> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 47 | std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>> |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 48 | checkedAdd(T LHS, T RHS) { |
| 49 | return checkedOp(LHS, RHS, &llvm::APInt::sadd_ov); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 52 | /// Subtract two signed integers \p LHS and \p RHS. |
| 53 | /// \return Optional of sum if no signed overflow occurred, |
| 54 | /// \c None otherwise. |
| 55 | template <typename T> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 56 | std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>> |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 57 | checkedSub(T LHS, T RHS) { |
| 58 | return checkedOp(LHS, RHS, &llvm::APInt::ssub_ov); |
| 59 | } |
| 60 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 61 | /// Multiply two signed integers \p LHS and \p RHS. |
| 62 | /// \return Optional of product if no signed overflow occurred, |
| 63 | /// \c None otherwise. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 64 | template <typename T> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 65 | std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>> |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 66 | checkedMul(T LHS, T RHS) { |
| 67 | return checkedOp(LHS, RHS, &llvm::APInt::smul_ov); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 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. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 73 | template <typename T> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 74 | std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>> |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 75 | checkedMulAdd(T A, T B, T C) { |
| 76 | if (auto Product = checkedMul(A, B)) |
| 77 | return checkedAdd(*Product, C); |
| 78 | return llvm::None; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 81 | /// Add two unsigned integers \p LHS and \p RHS. |
| 82 | /// \return Optional of sum if no unsigned overflow occurred, |
| 83 | /// \c None otherwise. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 84 | template <typename T> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 85 | std::enable_if_t<std::is_unsigned<T>::value, llvm::Optional<T>> |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 86 | checkedAddUnsigned(T LHS, T RHS) { |
| 87 | return checkedOp(LHS, RHS, &llvm::APInt::uadd_ov, /*Signed=*/false); |
| 88 | } |
| 89 | |
| 90 | /// Multiply two unsigned integers \p LHS and \p RHS. |
| 91 | /// \return Optional of product if no unsigned overflow occurred, |
| 92 | /// \c None otherwise. |
| 93 | template <typename T> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 94 | std::enable_if_t<std::is_unsigned<T>::value, llvm::Optional<T>> |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 95 | checkedMulUnsigned(T LHS, T RHS) { |
| 96 | return checkedOp(LHS, RHS, &llvm::APInt::umul_ov, /*Signed=*/false); |
| 97 | } |
| 98 | |
| 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> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 103 | std::enable_if_t<std::is_unsigned<T>::value, llvm::Optional<T>> |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 104 | checkedMulAddUnsigned(T A, T B, T C) { |
| 105 | if (auto Product = checkedMulUnsigned(A, B)) |
| 106 | return checkedAddUnsigned(*Product, C); |
| 107 | return llvm::None; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | } // End llvm namespace |
| 111 | |
| 112 | #endif |