32 static constexpr T T_MAX = std::numeric_limits<T>::max();
33 static constexpr T T_MIN = std::numeric_limits<T>::min();
46 inline constexpr OverflowSafeInt& operator = (T other) { this->m_value = other;
return *
this; }
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;
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;
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;
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;
104 inline constexpr OverflowSafeInt& operator ++ () {
return *
this += 1; }
105 inline constexpr OverflowSafeInt& operator -- () {
return *
this += -1; }
117#ifdef HAS_OVERFLOW_BUILTINS
118 const bool is_result_positive = (this->m_value < 0) == (factor < 0);
119 if (__builtin_mul_overflow(this->m_value, factor, &this->m_value)) [[unlikely]] {
120 this->m_value = is_result_positive ? T_MAX : T_MIN;
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;
134 this->m_value *= factor;
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; }
148 inline constexpr OverflowSafeInt& operator /= (
const int64_t divisor) { this->m_value /= divisor;
return *
this; }
154 inline constexpr OverflowSafeInt& operator %= (
const int divisor) { this->m_value %= divisor;
return *
this; }
158 inline constexpr OverflowSafeInt& operator <<= (
const int shift) { this->m_value <<= shift;
return *
this; }
160 inline constexpr OverflowSafeInt& operator >>= (
const int shift) { this->m_value >>= shift;
return *
this; }
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 == other); }
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->m_value >= other.
m_value; }
168 inline constexpr bool operator < (
const OverflowSafeInt& other)
const {
return !(*
this >= other); }
169 inline constexpr bool operator <= (
const OverflowSafeInt& 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 == 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->m_value >= other; }
176 inline constexpr bool operator < (
const int other)
const {
return !(*
this >= other); }
177 inline constexpr bool operator <= (
const int other)
const {
return !(*
this > other); }
179 inline constexpr operator T ()
const {
return this->
m_value; }
184 BaseType base() const noexcept {
return this->
m_value; }