1 //==-- llvm/Support/CheckedArithmetic.h - Safe arithmetical operations *- C++ // 2 // 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 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 <optional> 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 25 /// \p RHS. 26 /// \return Empty optional if the operation overflows, or result otherwise. 27 template <typename T, typename F> 28 std::enable_if_t<std::is_integral_v<T> && sizeof(T) * 8 <= 64, std::optional<T>> 29 checkedOp(T LHS, T RHS, F Op, bool Signed = true) { 30 llvm::APInt ALHS(sizeof(T) * 8, LHS, Signed); 31 llvm::APInt ARHS(sizeof(T) * 8, RHS, Signed); 32 bool Overflow; 33 llvm::APInt Out = (ALHS.*Op)(ARHS, Overflow); 34 if (Overflow) 35 return std::nullopt; 36 return Signed ? Out.getSExtValue() : Out.getZExtValue(); 37 } 38 } 39 40 namespace llvm { 41 42 /// Add two signed integers \p LHS and \p RHS. 43 /// \return Optional of sum if no signed overflow occurred, 44 /// \c std::nullopt otherwise. 45 template <typename T> 46 std::enable_if_t<std::is_signed_v<T>, std::optional<T>> checkedAdd(T LHS, 47 T RHS) { 48 return checkedOp(LHS, RHS, &llvm::APInt::sadd_ov); 49 } 50 51 /// Subtract two signed integers \p LHS and \p RHS. 52 /// \return Optional of sum if no signed overflow occurred, 53 /// \c std::nullopt otherwise. 54 template <typename T> 55 std::enable_if_t<std::is_signed_v<T>, std::optional<T>> checkedSub(T LHS, 56 T RHS) { 57 return checkedOp(LHS, RHS, &llvm::APInt::ssub_ov); 58 } 59 60 /// Multiply two signed integers \p LHS and \p RHS. 61 /// \return Optional of product if no signed overflow occurred, 62 /// \c std::nullopt otherwise. 63 template <typename T> 64 std::enable_if_t<std::is_signed_v<T>, std::optional<T>> checkedMul(T LHS, 65 T RHS) { 66 return checkedOp(LHS, RHS, &llvm::APInt::smul_ov); 67 } 68 69 /// Multiply A and B, and add C to the resulting product. 70 /// \return Optional of result if no signed overflow occurred, 71 /// \c std::nullopt otherwise. 72 template <typename T> 73 std::enable_if_t<std::is_signed_v<T>, std::optional<T>> checkedMulAdd(T A, T B, 74 T C) { 75 if (auto Product = checkedMul(A, B)) 76 return checkedAdd(*Product, C); 77 return std::nullopt; 78 } 79 80 /// Add two unsigned integers \p LHS and \p RHS. 81 /// \return Optional of sum if no unsigned overflow occurred, 82 /// \c std::nullopt otherwise. 83 template <typename T> 84 std::enable_if_t<std::is_unsigned_v<T>, std::optional<T>> 85 checkedAddUnsigned(T LHS, T RHS) { 86 return checkedOp(LHS, RHS, &llvm::APInt::uadd_ov, /*Signed=*/false); 87 } 88 89 /// Multiply two unsigned integers \p LHS and \p RHS. 90 /// \return Optional of product if no unsigned overflow occurred, 91 /// \c std::nullopt otherwise. 92 template <typename T> 93 std::enable_if_t<std::is_unsigned_v<T>, std::optional<T>> 94 checkedMulUnsigned(T LHS, T RHS) { 95 return checkedOp(LHS, RHS, &llvm::APInt::umul_ov, /*Signed=*/false); 96 } 97 98 /// Multiply unsigned integers A and B, and add C to the resulting product. 99 /// \return Optional of result if no unsigned overflow occurred, 100 /// \c std::nullopt otherwise. 101 template <typename T> 102 std::enable_if_t<std::is_unsigned_v<T>, std::optional<T>> 103 checkedMulAddUnsigned(T A, T B, T C) { 104 if (auto Product = checkedMulUnsigned(A, B)) 105 return checkedAddUnsigned(*Product, C); 106 return std::nullopt; 107 } 108 109 } // End llvm namespace 110 111 #endif 112