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