OpenTTD Source 20260218-master-g2123fca5ea
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>
29class OverflowSafeInt
30{
31private:
32 static constexpr T T_MAX = std::numeric_limits<T>::max();
33 static constexpr T T_MIN = std::numeric_limits<T>::min();
34
37public:
38 using BaseType = T;
39
40 constexpr OverflowSafeInt() : m_value(0) { }
41
42 constexpr OverflowSafeInt(const OverflowSafeInt &other) : m_value(other.m_value) { }
43 constexpr OverflowSafeInt(const T int_) : m_value(int_) { }
44
45 inline constexpr OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; }
46 inline constexpr OverflowSafeInt& operator = (T other) { this->m_value = other; return *this; }
47
48 inline constexpr OverflowSafeInt operator - () const { return OverflowSafeInt(this->m_value == T_MIN ? T_MAX : -this->m_value); }
49
56 inline constexpr OverflowSafeInt& operator += (const OverflowSafeInt& other)
57 {
58#ifdef HAS_OVERFLOW_BUILTINS
59 if (__builtin_add_overflow(this->m_value, other.m_value, &this->m_value)) [[unlikely]] {
60 this->m_value = (other.m_value < 0) ? T_MIN : T_MAX;
61 }
62#else
63 if (this->m_value > 0 && other.m_value > 0 && (T_MAX - other.m_value) < this->m_value) {
64 this->m_value = T_MAX;
65 } 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)))) {
66 this->m_value = T_MIN;
67 } else {
68 this->m_value += other.m_value;
69 }
70#endif
71 return *this;
72 }
73
80 inline constexpr OverflowSafeInt& operator -= (const OverflowSafeInt& other)
81 {
82#ifdef HAS_OVERFLOW_BUILTINS
83 if (__builtin_sub_overflow(this->m_value, other.m_value, &this->m_value)) [[unlikely]] {
84 this->m_value = (other.m_value < 0) ? T_MAX : T_MIN;
85 }
86#else
87 if (this->m_value > 0 && other.m_value < 0 && (T_MAX + other.m_value) < this->m_value) {
88 this->m_value = T_MAX;
89 } else if (this->m_value < 0 && other.m_value > 0 && (T_MAX + this->m_value) < (T_MIN + T_MAX) + other.m_value) {
90 this->m_value = T_MIN;
91 } else {
92 this->m_value -= other.m_value;
93 }
94#endif
95 return *this;
96 }
97
98 /* Operators for addition and subtraction. */
99 inline constexpr OverflowSafeInt operator + (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result += other; return result; }
100 inline constexpr OverflowSafeInt operator + (const int other) const { OverflowSafeInt result = *this; result += (int64_t)other; return result; }
101 inline constexpr OverflowSafeInt operator + (const uint other) const { OverflowSafeInt result = *this; result += (int64_t)other; return result; }
102 inline constexpr OverflowSafeInt operator - (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result -= other; return result; }
103 inline constexpr OverflowSafeInt operator - (const int other) const { OverflowSafeInt result = *this; result -= (int64_t)other; return result; }
104 inline constexpr OverflowSafeInt operator - (const uint other) const { OverflowSafeInt result = *this; result -= (int64_t)other; return result; }
105
106 inline constexpr OverflowSafeInt& operator ++ () { return *this += 1; }
107 inline constexpr OverflowSafeInt& operator -- () { return *this += -1; }
108 inline constexpr OverflowSafeInt operator ++ (int) { OverflowSafeInt org = *this; *this += 1; return org; }
109 inline constexpr OverflowSafeInt operator -- (int) { OverflowSafeInt org = *this; *this += -1; return org; }
110
118 inline constexpr OverflowSafeInt& operator *= (const int factor)
119 {
120#ifdef HAS_OVERFLOW_BUILTINS
121 const bool is_result_positive = (this->m_value < 0) == (factor < 0); // -ve * -ve == +ve
122 if (__builtin_mul_overflow(this->m_value, factor, &this->m_value)) [[unlikely]] {
123 this->m_value = is_result_positive ? T_MAX : T_MIN;
124 }
125#else
126 if (factor == -1) {
127 this->m_value = (this->m_value == T_MIN) ? T_MAX : -this->m_value;
128 } else if (factor > 0 && this->m_value > 0 && (T_MAX / factor) < this->m_value) {
129 this->m_value = T_MAX;
130 } else if (factor > 0 && this->m_value < 0 && (T_MIN / factor) > this->m_value) {
131 this->m_value = T_MIN;
132 } else if (factor < 0 && this->m_value > 0 && (T_MIN / factor) < this->m_value) {
133 this->m_value = T_MIN;
134 } else if (factor < 0 && this->m_value < 0 && (T_MAX / factor) > this->m_value) {
135 this->m_value = T_MAX;
136 } else {
137 this->m_value *= factor;
138 }
139#endif
140 return *this;
141 }
142
143 /* Operators for multiplication. */
144 inline constexpr OverflowSafeInt operator * (const int64_t factor) const { OverflowSafeInt result = *this; result *= factor; return result; }
145 inline constexpr OverflowSafeInt operator * (const int factor) const { OverflowSafeInt result = *this; result *= (int64_t)factor; return result; }
146 inline constexpr OverflowSafeInt operator * (const uint factor) const { OverflowSafeInt result = *this; result *= (int64_t)factor; return result; }
147 inline constexpr OverflowSafeInt operator * (const uint16_t factor) const { OverflowSafeInt result = *this; result *= (int64_t)factor; return result; }
148 inline constexpr OverflowSafeInt operator * (const uint8_t factor) const { OverflowSafeInt result = *this; result *= (int64_t)factor; return result; }
149
150 /* Operators for division. */
151 inline constexpr OverflowSafeInt& operator /= (const int64_t divisor) { this->m_value /= divisor; return *this; }
152 inline constexpr OverflowSafeInt operator / (const OverflowSafeInt& divisor) const { OverflowSafeInt result = *this; result /= divisor.m_value; return result; }
153 inline constexpr OverflowSafeInt operator / (const int divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; }
154 inline constexpr OverflowSafeInt operator / (const uint divisor) const { OverflowSafeInt result = *this; result /= (int)divisor; return result; }
155
156 /* Operators for modulo */
157 inline constexpr OverflowSafeInt& operator %= (const int divisor) { this->m_value %= divisor; return *this; }
158 inline constexpr OverflowSafeInt operator % (const int divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; }
159
160 /* Operators for shifting. */
161 inline constexpr OverflowSafeInt& operator <<= (const int shift) { this->m_value <<= shift; return *this; }
162 inline constexpr OverflowSafeInt operator << (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; }
163 inline constexpr OverflowSafeInt& operator >>= (const int shift) { this->m_value >>= shift; return *this; }
164 inline constexpr OverflowSafeInt operator >> (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; }
165
166 /* Operators for (in)equality when comparing overflow safe ints. */
167 inline constexpr bool operator == (const OverflowSafeInt& other) const { return this->m_value == other.m_value; }
168 inline constexpr auto operator <=>(const OverflowSafeInt& other) const { return this->m_value <=> other.m_value; }
169
170 /* Operators for (in)equality when comparing non-overflow safe ints. */
171 inline constexpr bool operator == (const int other) const { return this->m_value == other; }
172 inline constexpr auto operator <=>(const int other) const { return this->m_value <=> other; }
173
174 inline constexpr operator T () const { return this->m_value; }
175
176 static inline constexpr OverflowSafeInt<T> max() { return T_MAX; }
177 static inline constexpr OverflowSafeInt<T> min() { return T_MIN; }
178
179 BaseType base() const noexcept { return this->m_value; }
180};
181
182
183/* Sometimes we got int64_t operator OverflowSafeInt instead of vice versa. Handle that properly. */
184template <class T> inline constexpr OverflowSafeInt<T> operator + (const int64_t a, const OverflowSafeInt<T> b) { return b + a; }
185template <class T> inline constexpr OverflowSafeInt<T> operator - (const int64_t a, const OverflowSafeInt<T> b) { return -b + a; }
186template <class T> inline constexpr OverflowSafeInt<T> operator * (const int64_t a, const OverflowSafeInt<T> b) { return b * a; }
187template <class T> inline constexpr OverflowSafeInt<T> operator / (const int64_t a, const OverflowSafeInt<T> b) { return (OverflowSafeInt<T>)a / (int)b; }
188
189/* Sometimes we got int operator OverflowSafeInt instead of vice versa. Handle that properly. */
190template <class T> inline constexpr OverflowSafeInt<T> operator + (const int a, const OverflowSafeInt<T> b) { return b + a; }
191template <class T> inline constexpr OverflowSafeInt<T> operator - (const int a, const OverflowSafeInt<T> b) { return -b + a; }
192template <class T> inline constexpr OverflowSafeInt<T> operator * (const int a, const OverflowSafeInt<T> b) { return b * a; }
193template <class T> inline constexpr OverflowSafeInt<T> operator / (const int a, const OverflowSafeInt<T> b) { return (OverflowSafeInt<T>)a / (int)b; }
194
195/* Sometimes we got uint operator OverflowSafeInt instead of vice versa. Handle that properly. */
196template <class T> inline constexpr OverflowSafeInt<T> operator + (const uint a, const OverflowSafeInt<T> b) { return b + a; }
197template <class T> inline constexpr OverflowSafeInt<T> operator - (const uint a, const OverflowSafeInt<T> b) { return -b + a; }
198template <class T> inline constexpr OverflowSafeInt<T> operator * (const uint a, const OverflowSafeInt<T> b) { return b * a; }
199template <class T> inline constexpr OverflowSafeInt<T> operator / (const uint a, const OverflowSafeInt<T> b) { return (OverflowSafeInt<T>)a / (int)b; }
200
201/* Sometimes we got byte operator OverflowSafeInt instead of vice versa. Handle that properly. */
202template <class T> inline constexpr OverflowSafeInt<T> operator + (const uint8_t a, const OverflowSafeInt<T> b) { return b + (uint)a; }
203template <class T> inline constexpr OverflowSafeInt<T> operator - (const uint8_t a, const OverflowSafeInt<T> b) { return -b + (uint)a; }
204template <class T> inline constexpr OverflowSafeInt<T> operator * (const uint8_t a, const OverflowSafeInt<T> b) { return b * (uint)a; }
205template <class T> inline constexpr OverflowSafeInt<T> operator / (const uint8_t a, const OverflowSafeInt<T> b) { return (OverflowSafeInt<T>)a / (int)b; }
206
207typedef OverflowSafeInt<int64_t> OverflowSafeInt64;
208typedef OverflowSafeInt<int32_t> OverflowSafeInt32;
209
210/* Some basic "unit tests". Also has the bonus of confirming that constexpr is working. */
211static_assert(OverflowSafeInt32(INT32_MIN) - 1 == OverflowSafeInt32(INT32_MIN));
212static_assert(OverflowSafeInt32(INT32_MAX) + 1 == OverflowSafeInt32(INT32_MAX));
213static_assert(OverflowSafeInt32(INT32_MAX) * 2 == OverflowSafeInt32(INT32_MAX));
214static_assert(OverflowSafeInt32(INT32_MIN) * 2 == OverflowSafeInt32(INT32_MIN));
215
216/* Specialisation of the generic ClampTo function for overflow safe integers to normal integers. */
217template <typename To, typename From>
218constexpr To ClampTo(OverflowSafeInt<From> value) { return ClampTo<To>(From(value)); }
219
220#undef HAS_OVERFLOW_BUILTINS
221
222#endif /* OVERFLOWSAFE_TYPE_HPP */
Overflow safe template for integers, i.e.
constexpr OverflowSafeInt & operator+=(const OverflowSafeInt &other)
Safe implementation of addition.
constexpr OverflowSafeInt & operator*=(const int factor)
Safe implementation of multiplication.
constexpr OverflowSafeInt & operator-=(const OverflowSafeInt &other)
Safe implementation of subtraction.
Integer math functions.
constexpr To ClampTo(From value)
Clamp the given value down to lie within the requested type.