OpenTTD Source: src/core/overflowsafe_type.hpp Source File

OpenTTD Source 15.0-RC2

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