diff options
| author | orivej <[email protected]> | 2022-02-10 16:45:01 +0300 |
|---|---|---|
| committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:45:01 +0300 |
| commit | 2d37894b1b037cf24231090eda8589bbb44fb6fc (patch) | |
| tree | be835aa92c6248212e705f25388ebafcf84bc7a1 /contrib/libs/llvm12/include/llvm/Support/CheckedArithmetic.h | |
| parent | 718c552901d703c502ccbefdfc3c9028d608b947 (diff) | |
Restoring authorship annotation for <[email protected]>. Commit 2 of 2.
Diffstat (limited to 'contrib/libs/llvm12/include/llvm/Support/CheckedArithmetic.h')
| -rw-r--r-- | contrib/libs/llvm12/include/llvm/Support/CheckedArithmetic.h | 242 |
1 files changed, 121 insertions, 121 deletions
diff --git a/contrib/libs/llvm12/include/llvm/Support/CheckedArithmetic.h b/contrib/libs/llvm12/include/llvm/Support/CheckedArithmetic.h index ca4c3bfaf39..29be66ad599 100644 --- a/contrib/libs/llvm12/include/llvm/Support/CheckedArithmetic.h +++ b/contrib/libs/llvm12/include/llvm/Support/CheckedArithmetic.h @@ -1,123 +1,123 @@ -#pragma once - -#ifdef __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-parameter" -#endif - -//==-- llvm/Support/CheckedArithmetic.h - Safe arithmetical operations *- C++ // -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// This file contains generic functions for operating on integers which -// give the indication on whether the operation has overflown. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_SUPPORT_CHECKEDARITHMETIC_H -#define LLVM_SUPPORT_CHECKEDARITHMETIC_H - -#include "llvm/ADT/APInt.h" -#include "llvm/ADT/Optional.h" - -#include <type_traits> - -namespace { - -/// Utility function to apply a given method of \c APInt \p F to \p LHS and -/// \p RHS. -/// \return Empty optional if the operation overflows, or result otherwise. -template <typename T, typename F> -std::enable_if_t<std::is_integral<T>::value && sizeof(T) * 8 <= 64, - llvm::Optional<T>> -checkedOp(T LHS, T RHS, F Op, bool Signed = true) { +#pragma once + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif + +//==-- llvm/Support/CheckedArithmetic.h - Safe arithmetical operations *- C++ // +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file contains generic functions for operating on integers which +// give the indication on whether the operation has overflown. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_SUPPORT_CHECKEDARITHMETIC_H +#define LLVM_SUPPORT_CHECKEDARITHMETIC_H + +#include "llvm/ADT/APInt.h" +#include "llvm/ADT/Optional.h" + +#include <type_traits> + +namespace { + +/// Utility function to apply a given method of \c APInt \p F to \p LHS and +/// \p RHS. +/// \return Empty optional if the operation overflows, or result otherwise. +template <typename T, typename F> +std::enable_if_t<std::is_integral<T>::value && sizeof(T) * 8 <= 64, + llvm::Optional<T>> +checkedOp(T LHS, T RHS, F Op, bool Signed = true) { llvm::APInt ALHS(sizeof(T) * 8, LHS, Signed); llvm::APInt ARHS(sizeof(T) * 8, RHS, Signed); - bool Overflow; - llvm::APInt Out = (ALHS.*Op)(ARHS, Overflow); - if (Overflow) - return llvm::None; - return Signed ? Out.getSExtValue() : Out.getZExtValue(); -} -} - -namespace llvm { - -/// Add two signed integers \p LHS and \p RHS. -/// \return Optional of sum if no signed overflow occurred, -/// \c None otherwise. -template <typename T> -std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>> -checkedAdd(T LHS, T RHS) { - return checkedOp(LHS, RHS, &llvm::APInt::sadd_ov); -} - -/// Subtract two signed integers \p LHS and \p RHS. -/// \return Optional of sum if no signed overflow occurred, -/// \c None otherwise. -template <typename T> -std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>> -checkedSub(T LHS, T RHS) { - return checkedOp(LHS, RHS, &llvm::APInt::ssub_ov); -} - -/// Multiply two signed integers \p LHS and \p RHS. -/// \return Optional of product if no signed overflow occurred, -/// \c None otherwise. -template <typename T> -std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>> -checkedMul(T LHS, T RHS) { - return checkedOp(LHS, RHS, &llvm::APInt::smul_ov); -} - -/// Multiply A and B, and add C to the resulting product. -/// \return Optional of result if no signed overflow occurred, -/// \c None otherwise. -template <typename T> -std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>> -checkedMulAdd(T A, T B, T C) { - if (auto Product = checkedMul(A, B)) - return checkedAdd(*Product, C); - return llvm::None; -} - -/// Add two unsigned integers \p LHS and \p RHS. -/// \return Optional of sum if no unsigned overflow occurred, -/// \c None otherwise. -template <typename T> -std::enable_if_t<std::is_unsigned<T>::value, llvm::Optional<T>> -checkedAddUnsigned(T LHS, T RHS) { - return checkedOp(LHS, RHS, &llvm::APInt::uadd_ov, /*Signed=*/false); -} - -/// Multiply two unsigned integers \p LHS and \p RHS. -/// \return Optional of product if no unsigned overflow occurred, -/// \c None otherwise. -template <typename T> -std::enable_if_t<std::is_unsigned<T>::value, llvm::Optional<T>> -checkedMulUnsigned(T LHS, T RHS) { - return checkedOp(LHS, RHS, &llvm::APInt::umul_ov, /*Signed=*/false); -} - -/// Multiply unsigned integers A and B, and add C to the resulting product. -/// \return Optional of result if no unsigned overflow occurred, -/// \c None otherwise. -template <typename T> -std::enable_if_t<std::is_unsigned<T>::value, llvm::Optional<T>> -checkedMulAddUnsigned(T A, T B, T C) { - if (auto Product = checkedMulUnsigned(A, B)) - return checkedAddUnsigned(*Product, C); - return llvm::None; -} - -} // End llvm namespace - -#endif - -#ifdef __GNUC__ -#pragma GCC diagnostic pop -#endif + bool Overflow; + llvm::APInt Out = (ALHS.*Op)(ARHS, Overflow); + if (Overflow) + return llvm::None; + return Signed ? Out.getSExtValue() : Out.getZExtValue(); +} +} + +namespace llvm { + +/// Add two signed integers \p LHS and \p RHS. +/// \return Optional of sum if no signed overflow occurred, +/// \c None otherwise. +template <typename T> +std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>> +checkedAdd(T LHS, T RHS) { + return checkedOp(LHS, RHS, &llvm::APInt::sadd_ov); +} + +/// Subtract two signed integers \p LHS and \p RHS. +/// \return Optional of sum if no signed overflow occurred, +/// \c None otherwise. +template <typename T> +std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>> +checkedSub(T LHS, T RHS) { + return checkedOp(LHS, RHS, &llvm::APInt::ssub_ov); +} + +/// Multiply two signed integers \p LHS and \p RHS. +/// \return Optional of product if no signed overflow occurred, +/// \c None otherwise. +template <typename T> +std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>> +checkedMul(T LHS, T RHS) { + return checkedOp(LHS, RHS, &llvm::APInt::smul_ov); +} + +/// Multiply A and B, and add C to the resulting product. +/// \return Optional of result if no signed overflow occurred, +/// \c None otherwise. +template <typename T> +std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>> +checkedMulAdd(T A, T B, T C) { + if (auto Product = checkedMul(A, B)) + return checkedAdd(*Product, C); + return llvm::None; +} + +/// Add two unsigned integers \p LHS and \p RHS. +/// \return Optional of sum if no unsigned overflow occurred, +/// \c None otherwise. +template <typename T> +std::enable_if_t<std::is_unsigned<T>::value, llvm::Optional<T>> +checkedAddUnsigned(T LHS, T RHS) { + return checkedOp(LHS, RHS, &llvm::APInt::uadd_ov, /*Signed=*/false); +} + +/// Multiply two unsigned integers \p LHS and \p RHS. +/// \return Optional of product if no unsigned overflow occurred, +/// \c None otherwise. +template <typename T> +std::enable_if_t<std::is_unsigned<T>::value, llvm::Optional<T>> +checkedMulUnsigned(T LHS, T RHS) { + return checkedOp(LHS, RHS, &llvm::APInt::umul_ov, /*Signed=*/false); +} + +/// Multiply unsigned integers A and B, and add C to the resulting product. +/// \return Optional of result if no unsigned overflow occurred, +/// \c None otherwise. +template <typename T> +std::enable_if_t<std::is_unsigned<T>::value, llvm::Optional<T>> +checkedMulAddUnsigned(T A, T B, T C) { + if (auto Product = checkedMulUnsigned(A, B)) + return checkedAddUnsigned(*Product, C); + return llvm::None; +} + +} // End llvm namespace + +#endif + +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif |
