OpenTTD Source 20261129-master-g72a8bdc861
overflowsafe_type.hpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef OVERFLOWSAFE_TYPE_HPP
11#define OVERFLOWSAFE_TYPE_HPP
12
13#include "math_func.hpp"
14
15
16#ifdef __has_builtin
17# if __has_builtin(__builtin_add_overflow) && __has_builtin(__builtin_sub_overflow) && __has_builtin(__builtin_mul_overflow)
18# define HAS_OVERFLOW_BUILTINS
19# endif
20#endif
21
28template <class T>
30private:
31 static constexpr T T_MAX = std::numeric_limits<T>::max();
32 static constexpr T T_MIN = std::numeric_limits<T>::min();
33
40 static constexpr T ConvertIfSafe(std::integral auto value)
41 {
42 /* T must be larger than value or the same size and the same sign. */
43 static_assert(sizeof(T) > sizeof(value) || (sizeof(T) == sizeof(value) && std::numeric_limits<T>::is_signed == std::numeric_limits<decltype(value)>::is_signed));
44 return static_cast<T>(value);
45 }
46
49public:
50 using BaseType = T;
51
55 constexpr OverflowSafeInt() = default;
56
61 constexpr OverflowSafeInt(std::integral auto value) : m_value(ConvertIfSafe(value)) {}
62
69 template <class Tother>
70 constexpr OverflowSafeInt(const OverflowSafeInt<Tother> &value) : m_value(ConvertIfSafe(value.base())) {}
71
77 constexpr OverflowSafeInt &operator=(std::integral auto other) { this->m_value = ConvertIfSafe(other); return *this; }
78
79 constexpr OverflowSafeInt operator-() const { return OverflowSafeInt{this->m_value == T_MIN ? T_MAX : -this->m_value}; }
80
88 {
89#ifdef HAS_OVERFLOW_BUILTINS
90 if (__builtin_add_overflow(this->m_value, other.m_value, &this->m_value)) [[unlikely]] {
91 this->m_value = (other.m_value < 0) ? T_MIN : T_MAX;
92 }
93#else
94 if (this->m_value > 0 && other.m_value > 0 && (T_MAX - other.m_value) < this->m_value) {
95 this->m_value = T_MAX;
96 } else if (this->m_value < 0 && other.m_value < 0 && (this->m_value == T_MIN || other.m_value == T_MIN || ((T_MAX + this->m_value) + other.m_value < (T_MIN + T_MAX)))) {
97 this->m_value = T_MIN;
98 } else {
99 this->m_value += other.m_value;
100 }
101#endif
102 return *this;
103 }
104
112 {
113#ifdef HAS_OVERFLOW_BUILTINS
114 if (__builtin_sub_overflow(this->m_value, other.m_value, &this->m_value)) [[unlikely]] {
115 this->m_value = (other.m_value < 0) ? T_MAX : T_MIN;
116 }
117#else
118 if (this->m_value > 0 && other.m_value < 0 && (T_MAX + other.m_value) < this->m_value) {
119 this->m_value = T_MAX;
120 } else if (this->m_value < 0 && other.m_value > 0 && (T_MAX + this->m_value) < (T_MIN + T_MAX) + other.m_value) {
121 this->m_value = T_MIN;
122 } else {
123 this->m_value -= other.m_value;
124 }
125#endif
126 return *this;
127 }
128
134 constexpr OverflowSafeInt operator+(const OverflowSafeInt &other) const { OverflowSafeInt result = *this; result += other; return result; }
135 constexpr OverflowSafeInt operator+(std::integral auto other) const { OverflowSafeInt result = *this; result += other; return result; }
137
143 constexpr OverflowSafeInt operator-(const OverflowSafeInt &other) const { OverflowSafeInt result = *this; result -= other; return result; }
144 constexpr OverflowSafeInt operator-(std::integral auto other) const { OverflowSafeInt result = *this; result -= other; return result; }
146
147 constexpr OverflowSafeInt &operator++() { return *this += 1; }
148 constexpr OverflowSafeInt operator++(int) { OverflowSafeInt org = *this; *this += 1; return org; }
149 constexpr OverflowSafeInt &operator--() { return *this += -1; }
150 constexpr OverflowSafeInt operator--(int) { OverflowSafeInt org = *this; *this += -1; return org; }
151
160 {
161#ifdef HAS_OVERFLOW_BUILTINS
162 const bool is_result_positive = (this->m_value < 0) == (factor.m_value < 0); // -ve * -ve == +ve
163 if (__builtin_mul_overflow(this->m_value, factor.m_value, &this->m_value)) [[unlikely]] {
164 this->m_value = is_result_positive ? T_MAX : T_MIN;
165 }
166#else
167 if (factor.m_value == -1) {
168 this->m_value = (this->m_value == T_MIN) ? T_MAX : -this->m_value;
169 } else if (factor.m_value > 0 && this->m_value > 0 && (T_MAX / factor.m_value) < this->m_value) {
170 this->m_value = T_MAX;
171 } else if (factor.m_value > 0 && this->m_value < 0 && (T_MIN / factor.m_value) > this->m_value) {
172 this->m_value = T_MIN;
173 } else if (factor.m_value < 0 && this->m_value > 0 && (T_MIN / factor.m_value) < this->m_value) {
174 this->m_value = T_MIN;
175 } else if (factor.m_value < 0 && this->m_value < 0 && (T_MAX / factor.m_value) > this->m_value) {
176 this->m_value = T_MAX;
177 } else {
178 this->m_value *= factor.m_value;
179 }
180#endif
181 return *this;
182 }
183
189 constexpr OverflowSafeInt operator*(const OverflowSafeInt &factor) const { OverflowSafeInt result = *this; result *= factor; return result; }
190 constexpr OverflowSafeInt operator*(std::integral auto factor) const { OverflowSafeInt result = *this; result *= factor; return result; }
192
198 constexpr OverflowSafeInt &operator/=(const OverflowSafeInt &divisor) { this->m_value /= divisor.m_value; return *this; }
199
205 constexpr OverflowSafeInt operator/(const OverflowSafeInt &divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; }
206 constexpr OverflowSafeInt operator/(std::integral auto divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; }
208
214 constexpr OverflowSafeInt &operator%=(const OverflowSafeInt &divisor) { this->m_value %= divisor.m_value; return *this; }
215
221 constexpr OverflowSafeInt operator%(const OverflowSafeInt &divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; }
222 constexpr OverflowSafeInt operator%(std::integral auto divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; }
224
225 /* Operators for shifting. */
226 constexpr OverflowSafeInt &operator<<=(const int shift) { this->m_value <<= shift; return *this; }
227 constexpr OverflowSafeInt operator<<(const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; }
228 constexpr OverflowSafeInt &operator>>=(const int shift) { this->m_value >>= shift; return *this; }
229 constexpr OverflowSafeInt operator>>(const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; }
230
236 constexpr bool operator==(const OverflowSafeInt &other) const { return this->m_value == other.m_value; }
237 constexpr bool operator==(std::integral auto other) const { return this->m_value == ConvertIfSafe(other); }
239
245 constexpr auto operator<=>(const OverflowSafeInt &other) const { return this->m_value <=> other.m_value; }
246 constexpr auto operator<=>(std::integral auto other) const { return this->m_value <=> ConvertIfSafe(other); }
248
249 constexpr operator T() const { return this->m_value; }
250
251 static constexpr OverflowSafeInt<T> max() { return T_MAX; }
252 static constexpr OverflowSafeInt<T> min() { return T_MIN; }
253
258 constexpr BaseType base() const noexcept { return this->m_value; }
259
266 friend constexpr OverflowSafeInt operator+(std::integral auto lhs, const OverflowSafeInt &rhs) noexcept { return OverflowSafeInt{lhs} + rhs; }
267 friend constexpr OverflowSafeInt operator-(std::integral auto lhs, const OverflowSafeInt &rhs) noexcept { return OverflowSafeInt{lhs} - rhs; }
268 friend constexpr OverflowSafeInt operator*(std::integral auto lhs, const OverflowSafeInt &rhs) noexcept { return OverflowSafeInt{lhs} * rhs; }
269 friend constexpr OverflowSafeInt operator/(std::integral auto lhs, const OverflowSafeInt &rhs) noexcept { return OverflowSafeInt{lhs} / rhs; }
270 friend constexpr OverflowSafeInt operator%(std::integral auto lhs, const OverflowSafeInt &rhs) noexcept { return OverflowSafeInt{lhs} % rhs; }
272};
273
276
279
280/* Some basic "unit tests". Also has the bonus of confirming that constexpr is working. */
281static_assert(OverflowSafeInt32(INT32_MIN) - 1 == OverflowSafeInt32(INT32_MIN));
282static_assert(OverflowSafeInt32(INT32_MAX) + 1 == OverflowSafeInt32(INT32_MAX));
283static_assert(OverflowSafeInt32(INT32_MAX) * 2 == OverflowSafeInt32(INT32_MAX));
284static_assert(OverflowSafeInt32(INT32_MIN) * 2 == OverflowSafeInt32(INT32_MIN));
285
286/* Specialisation of the generic ClampTo function for overflow safe integers to normal integers. */
287template <typename To, typename From>
288constexpr To ClampTo(OverflowSafeInt<From> value) { return ClampTo<To>(value.base()); }
289
290#undef HAS_OVERFLOW_BUILTINS
291
292#endif /* OVERFLOWSAFE_TYPE_HPP */
Overflow safe template for integers, i.e.
constexpr OverflowSafeInt operator*(std::integral auto factor) const
constexpr OverflowSafeInt & operator*=(const OverflowSafeInt &factor)
Safe implementation of multiplication.
constexpr OverflowSafeInt operator/(const OverflowSafeInt &divisor) const
constexpr OverflowSafeInt operator+(const OverflowSafeInt &other) const
constexpr OverflowSafeInt operator*(const OverflowSafeInt &factor) const
constexpr bool operator==(const OverflowSafeInt &other) const
friend constexpr OverflowSafeInt operator%(std::integral auto lhs, const OverflowSafeInt &rhs) noexcept
constexpr OverflowSafeInt operator/(std::integral auto divisor) const
constexpr OverflowSafeInt & operator+=(const OverflowSafeInt &other)
Safe implementation of addition.
constexpr auto operator<=>(std::integral auto other) const
friend constexpr OverflowSafeInt operator*(std::integral auto lhs, const OverflowSafeInt &rhs) noexcept
static constexpr T ConvertIfSafe(std::integral auto value)
Convert from any integral type to the base type.
constexpr OverflowSafeInt & operator/=(const OverflowSafeInt &divisor)
Divide operator.
constexpr OverflowSafeInt()=default
Construct an overflow-safe type instance with a default value.
constexpr OverflowSafeInt operator-(const OverflowSafeInt &other) const
constexpr auto operator<=>(const OverflowSafeInt &other) const
constexpr OverflowSafeInt operator%(const OverflowSafeInt &divisor) const
constexpr bool operator==(std::integral auto other) const
friend constexpr OverflowSafeInt operator+(std::integral auto lhs, const OverflowSafeInt &rhs) noexcept
constexpr OverflowSafeInt operator-(std::integral auto other) const
constexpr BaseType base() const noexcept
constexpr OverflowSafeInt & operator%=(const OverflowSafeInt &divisor)
Modulo operator.
static constexpr int64_t T_MAX
constexpr OverflowSafeInt(std::integral auto value)
Construct an overflow-safe type from an integral type.
constexpr OverflowSafeInt & operator=(std::integral auto other)
Safely assign an integral value to this overflow-safe instance.
constexpr OverflowSafeInt(const OverflowSafeInt< Tother > &value)
Safely construct an overflow-safe type from an overflow-safe type with a different base type.
constexpr OverflowSafeInt operator+(std::integral auto other) const
constexpr OverflowSafeInt operator%(std::integral auto divisor) const
static constexpr int64_t T_MIN
constexpr OverflowSafeInt & operator-=(const OverflowSafeInt &other)
Safe implementation of subtraction.
friend constexpr OverflowSafeInt operator/(std::integral auto lhs, const OverflowSafeInt &rhs) noexcept
friend constexpr OverflowSafeInt operator-(std::integral auto lhs, const OverflowSafeInt &rhs) noexcept
#define T
Climate temperate.
Definition engines.h:91
constexpr enum_type & operator++(enum_type &e)
Prefix increment.
Definition enum_type.hpp:41
constexpr enum_type operator-(enum_type e, int offset)
Subtract integer.
Integer math functions.
constexpr To ClampTo(From value)
Clamp the given value down to lie within the requested type.
OverflowSafeInt< int64_t > OverflowSafeInt64
A 64-bit overflow safe type.
OverflowSafeInt< int32_t > OverflowSafeInt32
A 32-bit overflow safe type.