Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
checked_arithmetic.h
1// Copyright 2024 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <optional>
17#include <type_traits>
18
19#include "pw_preprocessor/compiler.h"
20
21namespace pw {
22
25
41template <typename A, typename B, typename T>
42[[nodiscard]] constexpr bool CheckedAdd(A a, B b, T& result) {
43 T temp = 0;
44 if (PW_ADD_OVERFLOW(a, b, &temp)) {
45 return false;
46 }
47 result = temp;
48 return true;
49}
50
65template <typename T, typename A, typename B>
66[[nodiscard]] constexpr std::optional<T> CheckedAdd(A a, B b) {
67 T result;
68
69 if (PW_ADD_OVERFLOW(a, b, &result)) {
70 return std::nullopt;
71 }
72
73 return result;
74}
75
87template <typename T, typename Inc>
88[[nodiscard]] constexpr bool CheckedIncrement(T& base, Inc inc) {
89 return CheckedAdd(base, inc, base);
90}
91
107template <typename A, typename B, typename T>
108[[nodiscard]] constexpr bool CheckedSub(A a, B b, T& result) {
109 T temp = 0;
110 if (PW_SUB_OVERFLOW(a, b, &temp)) {
111 return false;
112 }
113 result = temp;
114 return true;
115}
116
131template <typename T, typename A, typename B>
132[[nodiscard]] constexpr std::optional<T> CheckedSub(A a, B b) {
133 T result;
134
135 if (PW_SUB_OVERFLOW(a, b, &result)) {
136 return std::nullopt;
137 }
138
139 return result;
140}
141
153template <typename T, typename Dec>
154[[nodiscard]] constexpr bool CheckedDecrement(T& base, Dec dec) {
155 return CheckedSub(base, dec, base);
156}
157
173template <typename A, typename B, typename T>
174[[nodiscard]] constexpr bool CheckedMul(A a, B b, T& result) {
175 T temp = 0;
176 if (PW_MUL_OVERFLOW(a, b, &temp)) {
177 return false;
178 }
179 result = temp;
180 return true;
181}
182
197template <typename T, typename A, typename B>
198[[nodiscard]] constexpr std::optional<T> CheckedMul(A a, B b) {
199 T result;
200
201 if (PW_MUL_OVERFLOW(a, b, &result)) {
202 return std::nullopt;
203 }
204
205 return result;
206}
208
209} // namespace pw
constexpr bool CheckedDecrement(T &base, Dec dec)
Definition: checked_arithmetic.h:154
constexpr bool CheckedIncrement(T &base, Inc inc)
Definition: checked_arithmetic.h:88
constexpr bool CheckedAdd(A a, B b, T &result)
Definition: checked_arithmetic.h:42
constexpr bool CheckedMul(A a, B b, T &result)
Definition: checked_arithmetic.h:174
constexpr bool CheckedSub(A a, B b, T &result)
Definition: checked_arithmetic.h:108
#define PW_SUB_OVERFLOW(a, b, out)
Definition: compiler.h:294
#define PW_ADD_OVERFLOW(a, b, out)
Definition: compiler.h:282
#define PW_MUL_OVERFLOW(a, b, out)
Definition: compiler.h:304
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27