OpenTTD Source  13.2.1
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 #include <limits>
16 
17 #ifdef __has_builtin
18 # if __has_builtin(__builtin_add_overflow) && __has_builtin(__builtin_sub_overflow) && __has_builtin(__builtin_mul_overflow)
19 # define HAS_OVERFLOW_BUILTINS
20 # endif
21 #endif
22 
29 template <class T>
31 {
32 private:
33  static constexpr T T_MAX = std::numeric_limits<T>::max();
34  static constexpr T T_MIN = std::numeric_limits<T>::min();
35 
38 public:
39  constexpr OverflowSafeInt() : m_value(0) { }
40 
41  constexpr OverflowSafeInt(const OverflowSafeInt& other) : m_value(other.m_value) { }
42  constexpr OverflowSafeInt(const T int_) : m_value(int_) { }
43 
44  inline constexpr OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; }
45  inline constexpr OverflowSafeInt& operator = (T other) { this->m_value = other; return *this; }
46 
47  inline constexpr OverflowSafeInt operator - () const { return OverflowSafeInt(this->m_value == T_MIN ? T_MAX : -this->m_value); }
48 
54  inline constexpr OverflowSafeInt& operator += (const OverflowSafeInt& other)
55  {
56 #ifdef HAS_OVERFLOW_BUILTINS
57  if (unlikely(__builtin_add_overflow(this->m_value, other.m_value, &this->m_value))) {
58  this->m_value = (other.m_value < 0) ? T_MIN : T_MAX;
59  }
60 #else
61  if (this->m_value > 0 && other.m_value > 0 && (T_MAX - other.m_value) < this->m_value) {
62  this->m_value = T_MAX;
63  } 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)))) {
64  this->m_value = T_MIN;
65  } else {
66  this->m_value += other.m_value;
67  }
68 #endif
69  return *this;
70  }
71 
77  inline constexpr OverflowSafeInt& operator -= (const OverflowSafeInt& other)
78  {
79 #ifdef HAS_OVERFLOW_BUILTINS
80  if (unlikely(__builtin_sub_overflow(this->m_value, other.m_value, &this->m_value))) {
81  this->m_value = (other.m_value < 0) ? T_MAX : T_MIN;
82  }
83 #else
84  if (this->m_value > 0 && other.m_value < 0 && (T_MAX + other.m_value) < this->m_value) {
85  this->m_value = T_MAX;
86  } else if (this->m_value < 0 && other.m_value > 0 && (T_MAX + this->m_value) < (T_MIN + T_MAX) + other.m_value) {
87  this->m_value = T_MIN;
88  } else {
89  this->m_value -= other.m_value;
90  }
91 #endif
92  return *this;
93  }
94 
95  /* Operators for addition and subtraction. */
96  inline constexpr OverflowSafeInt operator + (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result += other; return result; }
97  inline constexpr OverflowSafeInt operator + (const int other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
98  inline constexpr OverflowSafeInt operator + (const uint other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
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)other; return result; }
101  inline constexpr OverflowSafeInt operator - (const uint other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
102 
103  inline constexpr OverflowSafeInt& operator ++ () { return *this += 1; }
104  inline constexpr OverflowSafeInt& operator -- () { return *this += -1; }
105  inline constexpr OverflowSafeInt operator ++ (int) { OverflowSafeInt org = *this; *this += 1; return org; }
106  inline constexpr OverflowSafeInt operator -- (int) { OverflowSafeInt org = *this; *this += -1; return org; }
107 
114  inline constexpr OverflowSafeInt& operator *= (const int factor)
115  {
116 #ifdef HAS_OVERFLOW_BUILTINS
117  const bool is_result_positive = (this->m_value < 0) == (factor < 0); // -ve * -ve == +ve
118  if (unlikely(__builtin_mul_overflow(this->m_value, factor, &this->m_value))) {
119  this->m_value = is_result_positive ? T_MAX : T_MIN;
120  }
121 #else
122  if (factor == -1) {
123  this->m_value = (this->m_value == T_MIN) ? T_MAX : -this->m_value;
124  } else if (factor > 0 && this->m_value > 0 && (T_MAX / factor) < this->m_value) {
125  this->m_value = T_MAX;
126  } else if (factor > 0 && this->m_value < 0 && (T_MIN / factor) > this->m_value) {
127  this->m_value = T_MIN;
128  } else if (factor < 0 && this->m_value > 0 && (T_MIN / factor) < this->m_value) {
129  this->m_value = T_MIN;
130  } else if (factor < 0 && this->m_value < 0 && (T_MAX / factor) > this->m_value) {
131  this->m_value = T_MAX;
132  } else {
133  this->m_value *= factor;
134  }
135 #endif
136  return *this;
137  }
138 
139  /* Operators for multiplication. */
140  inline constexpr OverflowSafeInt operator * (const int64 factor) const { OverflowSafeInt result = *this; result *= factor; return result; }
141  inline constexpr OverflowSafeInt operator * (const int factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
142  inline constexpr OverflowSafeInt operator * (const uint factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
143  inline constexpr OverflowSafeInt operator * (const uint16 factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
144  inline constexpr OverflowSafeInt operator * (const byte factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
145 
146  /* Operators for division. */
147  inline constexpr OverflowSafeInt& operator /= (const int64 divisor) { this->m_value /= divisor; return *this; }
148  inline constexpr OverflowSafeInt operator / (const OverflowSafeInt& divisor) const { OverflowSafeInt result = *this; result /= divisor.m_value; return result; }
149  inline constexpr OverflowSafeInt operator / (const int divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; }
150  inline constexpr OverflowSafeInt operator / (const uint divisor) const { OverflowSafeInt result = *this; result /= (int)divisor; return result; }
151 
152  /* Operators for modulo */
153  inline constexpr OverflowSafeInt& operator %= (const int divisor) { this->m_value %= divisor; return *this; }
154  inline constexpr OverflowSafeInt operator % (const int divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; }
155 
156  /* Operators for shifting. */
157  inline constexpr OverflowSafeInt& operator <<= (const int shift) { this->m_value <<= shift; return *this; }
158  inline constexpr OverflowSafeInt operator << (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; }
159  inline constexpr OverflowSafeInt& operator >>= (const int shift) { this->m_value >>= shift; return *this; }
160  inline constexpr OverflowSafeInt operator >> (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; }
161 
162  /* Operators for (in)equality when comparing overflow safe ints. */
163  inline constexpr bool operator == (const OverflowSafeInt& other) const { return this->m_value == other.m_value; }
164  inline constexpr bool operator != (const OverflowSafeInt& other) const { return !(*this == other); }
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->m_value >= other.m_value; }
167  inline constexpr bool operator < (const OverflowSafeInt& other) const { return !(*this >= other); }
168  inline constexpr bool operator <= (const OverflowSafeInt& other) const { return !(*this > other); }
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 bool operator != (const int other) const { return !(*this == 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->m_value >= other; }
175  inline constexpr bool operator < (const int other) const { return !(*this >= other); }
176  inline constexpr bool operator <= (const int other) const { return !(*this > other); }
177 
178  inline constexpr operator T () const { return this->m_value; }
179 
180  static inline constexpr OverflowSafeInt<T> max() { return T_MAX; }
181  static inline constexpr OverflowSafeInt<T> min() { return T_MIN; }
182 };
183 
184 
185 /* Sometimes we got int64 operator OverflowSafeInt instead of vice versa. Handle that properly. */
186 template <class T> inline constexpr OverflowSafeInt<T> operator + (const int64 a, const OverflowSafeInt<T> b) { return b + a; }
187 template <class T> inline constexpr OverflowSafeInt<T> operator - (const int64 a, const OverflowSafeInt<T> b) { return -b + a; }
188 template <class T> inline constexpr OverflowSafeInt<T> operator * (const int64 a, const OverflowSafeInt<T> b) { return b * a; }
189 template <class T> inline constexpr OverflowSafeInt<T> operator / (const int64 a, const OverflowSafeInt<T> b) { return (OverflowSafeInt<T>)a / (int)b; }
190 
191 /* Sometimes we got int operator OverflowSafeInt instead of vice versa. Handle that properly. */
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 b * a; }
195 template <class T> inline constexpr OverflowSafeInt<T> operator / (const int a, const OverflowSafeInt<T> b) { return (OverflowSafeInt<T>)a / (int)b; }
196 
197 /* Sometimes we got uint operator OverflowSafeInt instead of vice versa. Handle that properly. */
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 b * a; }
201 template <class T> inline constexpr OverflowSafeInt<T> operator / (const uint a, const OverflowSafeInt<T> b) { return (OverflowSafeInt<T>)a / (int)b; }
202 
203 /* Sometimes we got byte operator OverflowSafeInt instead of vice versa. Handle that properly. */
204 template <class T> inline constexpr OverflowSafeInt<T> operator + (const byte a, const OverflowSafeInt<T> b) { return b + (uint)a; }
205 template <class T> inline constexpr OverflowSafeInt<T> operator - (const byte a, const OverflowSafeInt<T> b) { return -b + (uint)a; }
206 template <class T> inline constexpr OverflowSafeInt<T> operator * (const byte a, const OverflowSafeInt<T> b) { return b * (uint)a; }
207 template <class T> inline constexpr OverflowSafeInt<T> operator / (const byte a, const OverflowSafeInt<T> b) { return (OverflowSafeInt<T>)a / (int)b; }
208 
211 
212 /* Some basic "unit tests". Also has the bonus of confirming that constexpr is working. */
213 static_assert(OverflowSafeInt32(INT32_MIN) - 1 == OverflowSafeInt32(INT32_MIN));
214 static_assert(OverflowSafeInt32(INT32_MAX) + 1 == OverflowSafeInt32(INT32_MAX));
215 static_assert(OverflowSafeInt32(INT32_MAX) * 2 == OverflowSafeInt32(INT32_MAX));
216 static_assert(OverflowSafeInt32(INT32_MIN) * 2 == OverflowSafeInt32(INT32_MIN));
217 
218 #undef HAS_OVERFLOW_BUILTINS
219 
220 #endif /* OVERFLOWSAFE_TYPE_HPP */
OverflowSafeInt::operator-=
constexpr OverflowSafeInt & operator-=(const OverflowSafeInt &other)
Safe implementation of subtraction.
Definition: overflowsafe_type.hpp:77
math_func.hpp
OverflowSafeInt::operator*=
constexpr OverflowSafeInt & operator*=(const int factor)
Safe implementation of multiplication.
Definition: overflowsafe_type.hpp:114
OverflowSafeInt::operator+=
constexpr OverflowSafeInt & operator+=(const OverflowSafeInt &other)
Safe implementation of addition.
Definition: overflowsafe_type.hpp:54
OverflowSafeInt::m_value
T m_value
The non-overflow safe backend to store the value in.
Definition: overflowsafe_type.hpp:37
OverflowSafeInt
Overflow safe template for integers, i.e.
Definition: overflowsafe_type.hpp:30