CheckedArithmetic.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //==-- llvm/Support/CheckedArithmetic.h - Safe arithmetical operations *- C++ //
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file contains generic functions for operating on integers which
  15. // give the indication on whether the operation has overflown.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_SUPPORT_CHECKEDARITHMETIC_H
  19. #define LLVM_SUPPORT_CHECKEDARITHMETIC_H
  20. #include "llvm/ADT/APInt.h"
  21. #include "llvm/ADT/Optional.h"
  22. #include <type_traits>
  23. namespace {
  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<T>::value && sizeof(T) * 8 <= 64,
  29. llvm::Optional<T>>
  30. checkedOp(T LHS, T RHS, F Op, bool Signed = true) {
  31. llvm::APInt ALHS(sizeof(T) * 8, LHS, Signed);
  32. llvm::APInt ARHS(sizeof(T) * 8, RHS, Signed);
  33. bool Overflow;
  34. llvm::APInt Out = (ALHS.*Op)(ARHS, Overflow);
  35. if (Overflow)
  36. return llvm::None;
  37. return Signed ? Out.getSExtValue() : Out.getZExtValue();
  38. }
  39. }
  40. namespace llvm {
  41. /// Add two signed integers \p LHS and \p RHS.
  42. /// \return Optional of sum if no signed overflow occurred,
  43. /// \c None otherwise.
  44. template <typename T>
  45. std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>>
  46. checkedAdd(T LHS, T RHS) {
  47. return checkedOp(LHS, RHS, &llvm::APInt::sadd_ov);
  48. }
  49. /// Subtract two signed integers \p LHS and \p RHS.
  50. /// \return Optional of sum if no signed overflow occurred,
  51. /// \c None otherwise.
  52. template <typename T>
  53. std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>>
  54. checkedSub(T LHS, T RHS) {
  55. return checkedOp(LHS, RHS, &llvm::APInt::ssub_ov);
  56. }
  57. /// Multiply two signed integers \p LHS and \p RHS.
  58. /// \return Optional of product if no signed overflow occurred,
  59. /// \c None otherwise.
  60. template <typename T>
  61. std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>>
  62. checkedMul(T LHS, T RHS) {
  63. return checkedOp(LHS, RHS, &llvm::APInt::smul_ov);
  64. }
  65. /// Multiply A and B, and add C to the resulting product.
  66. /// \return Optional of result if no signed overflow occurred,
  67. /// \c None otherwise.
  68. template <typename T>
  69. std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>>
  70. checkedMulAdd(T A, T B, T C) {
  71. if (auto Product = checkedMul(A, B))
  72. return checkedAdd(*Product, C);
  73. return llvm::None;
  74. }
  75. /// Add two unsigned integers \p LHS and \p RHS.
  76. /// \return Optional of sum if no unsigned overflow occurred,
  77. /// \c None otherwise.
  78. template <typename T>
  79. std::enable_if_t<std::is_unsigned<T>::value, llvm::Optional<T>>
  80. checkedAddUnsigned(T LHS, T RHS) {
  81. return checkedOp(LHS, RHS, &llvm::APInt::uadd_ov, /*Signed=*/false);
  82. }
  83. /// Multiply two unsigned integers \p LHS and \p RHS.
  84. /// \return Optional of product if no unsigned overflow occurred,
  85. /// \c None otherwise.
  86. template <typename T>
  87. std::enable_if_t<std::is_unsigned<T>::value, llvm::Optional<T>>
  88. checkedMulUnsigned(T LHS, T RHS) {
  89. return checkedOp(LHS, RHS, &llvm::APInt::umul_ov, /*Signed=*/false);
  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. template <typename T>
  95. std::enable_if_t<std::is_unsigned<T>::value, llvm::Optional<T>>
  96. checkedMulAddUnsigned(T A, T B, T C) {
  97. if (auto Product = checkedMulUnsigned(A, B))
  98. return checkedAddUnsigned(*Product, C);
  99. return llvm::None;
  100. }
  101. } // End llvm namespace
  102. #endif
  103. #ifdef __GNUC__
  104. #pragma GCC diagnostic pop
  105. #endif