10 #ifndef OVERFLOWSAFE_TYPE_HPP
11 #define OVERFLOWSAFE_TYPE_HPP
18 # if __has_builtin(__builtin_add_overflow) && __has_builtin(__builtin_sub_overflow) && __has_builtin(__builtin_mul_overflow)
19 # define HAS_OVERFLOW_BUILTINS
33 static constexpr T T_MAX = std::numeric_limits<T>::max();
34 static constexpr T T_MIN = std::numeric_limits<T>::min();
45 inline constexpr
OverflowSafeInt& operator = (T other) { this->m_value = other;
return *
this; }
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;
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;
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;
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;
103 inline constexpr
OverflowSafeInt& operator ++ () {
return *
this += 1; }
104 inline constexpr
OverflowSafeInt& operator -- () {
return *
this += -1; }
116 #ifdef HAS_OVERFLOW_BUILTINS
117 const bool is_result_positive = (this->m_value < 0) == (factor < 0);
118 if (unlikely(__builtin_mul_overflow(this->m_value, factor, &this->m_value))) {
119 this->m_value = is_result_positive ? T_MAX : T_MIN;
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;
133 this->m_value *= factor;
143 inline constexpr
OverflowSafeInt operator * (
const uint16 factor)
const {
OverflowSafeInt result = *
this; result *= (int64)factor;
return result; }
147 inline constexpr
OverflowSafeInt& operator /= (
const int64 divisor) { this->m_value /= divisor;
return *
this; }
153 inline constexpr
OverflowSafeInt& operator %= (
const int divisor) { this->m_value %= divisor;
return *
this; }
157 inline constexpr
OverflowSafeInt& operator <<= (
const int shift) { this->m_value <<= shift;
return *
this; }
159 inline constexpr
OverflowSafeInt& operator >>= (
const int shift) { this->m_value >>= shift;
return *
this; }
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); }
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); }
178 inline constexpr
operator T ()
const {
return this->
m_value; }
218 #undef HAS_OVERFLOW_BUILTINS