LLVM 20.0.0git
CheckedArithmetic.h
Go to the documentation of this file.
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
22namespace {
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.
27template <typename T, typename F>
28std::enable_if_t<std::is_integral_v<T> && sizeof(T) * 8 <= 64, std::optional<T>>
29checkedOp(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
40namespace 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.
45template <typename T>
46std::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.
54template <typename T>
55std::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.
63template <typename T>
64std::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.
72template <typename T>
73std::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.
83template <typename T>
84std::enable_if_t<std::is_unsigned_v<T>, std::optional<T>>
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.
92template <typename T>
93std::enable_if_t<std::is_unsigned_v<T>, std::optional<T>>
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.
101template <typename T>
102std::enable_if_t<std::is_unsigned_v<T>, std::optional<T>>
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
This file implements a class to represent arbitrary precision integral constant values and operations...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define F(x, y, z)
Definition: MD5.cpp:55
#define T
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition: APInt.h:78
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1945
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1520
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1902
APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1909
APInt smul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1934
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1915
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1542
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::enable_if_t< std::is_unsigned_v< T >, std::optional< T > > checkedMulUnsigned(T LHS, T RHS)
Multiply two unsigned integers LHS and RHS.
std::enable_if_t< std::is_unsigned_v< T >, std::optional< T > > checkedAddUnsigned(T LHS, T RHS)
Add two unsigned integers LHS and RHS.
std::enable_if_t< std::is_unsigned_v< T >, std::optional< T > > checkedMulAddUnsigned(T A, T B, T C)
Multiply unsigned integers A and B, and add C to the resulting product.
std::enable_if_t< std::is_signed_v< T >, std::optional< T > > checkedSub(T LHS, T RHS)
Subtract two signed integers LHS and RHS.
std::enable_if_t< std::is_signed_v< T >, std::optional< T > > checkedAdd(T LHS, T RHS)
Add two signed integers LHS and RHS.
std::enable_if_t< std::is_signed_v< T >, std::optional< T > > checkedMulAdd(T A, T B, T C)
Multiply A and B, and add C to the resulting product.
std::enable_if_t< std::is_signed_v< T >, std::optional< T > > checkedMul(T LHS, T RHS)
Multiply two signed integers LHS and RHS.