10#ifndef OVERFLOWSAFE_TYPE_HPP
11#define OVERFLOWSAFE_TYPE_HPP
17# if __has_builtin(__builtin_add_overflow) && __has_builtin(__builtin_sub_overflow) && __has_builtin(__builtin_mul_overflow)
18# define HAS_OVERFLOW_BUILTINS
31 static constexpr T T_MAX = std::numeric_limits<T>::max();
32 static constexpr T T_MIN = std::numeric_limits<T>::min();
43 static_assert(
sizeof(
T) >
sizeof(value) || (
sizeof(
T) ==
sizeof(value) && std::numeric_limits<T>::is_signed == std::numeric_limits<
decltype(value)>::is_signed));
44 return static_cast<T>(value);
69 template <
class Tother>
89#ifdef HAS_OVERFLOW_BUILTINS
90 if (__builtin_add_overflow(this->m_value, other.
m_value, &this->m_value)) [[unlikely]] {
95 this->m_value =
T_MAX;
97 this->m_value =
T_MIN;
113#ifdef HAS_OVERFLOW_BUILTINS
114 if (__builtin_sub_overflow(this->m_value, other.
m_value, &this->m_value)) [[unlikely]] {
119 this->m_value =
T_MAX;
121 this->m_value =
T_MIN;
123 this->m_value -= other.
m_value;
161#ifdef HAS_OVERFLOW_BUILTINS
162 const bool is_result_positive = (this->m_value < 0) == (factor.
m_value < 0);
163 if (__builtin_mul_overflow(this->m_value, factor.
m_value, &this->m_value)) [[unlikely]] {
164 this->m_value = is_result_positive ?
T_MAX :
T_MIN;
168 this->m_value = (this->m_value ==
T_MIN) ?
T_MAX : -this->m_value;
169 }
else if (factor.
m_value > 0 && this->m_value > 0 && (
T_MAX / factor.
m_value) < this->m_value) {
170 this->m_value =
T_MAX;
171 }
else if (factor.
m_value > 0 && this->m_value < 0 && (
T_MIN / factor.
m_value) > this->m_value) {
172 this->m_value =
T_MIN;
173 }
else if (factor.
m_value < 0 && this->m_value > 0 && (
T_MIN / factor.
m_value) < this->m_value) {
174 this->m_value =
T_MIN;
175 }
else if (factor.
m_value < 0 && this->m_value < 0 && (
T_MAX / factor.
m_value) > this->m_value) {
176 this->m_value =
T_MAX;
178 this->m_value *= factor.
m_value;
226 constexpr OverflowSafeInt &operator<<=(
const int shift) { this->m_value <<= shift;
return *
this; }
249 constexpr operator T()
const {
return this->m_value; }
258 constexpr BaseType
base() const noexcept {
return this->m_value; }
287template <
typename To,
typename From>
290#undef HAS_OVERFLOW_BUILTINS
Overflow safe template for integers, i.e.
constexpr OverflowSafeInt operator*(std::integral auto factor) const
constexpr OverflowSafeInt & operator*=(const OverflowSafeInt &factor)
Safe implementation of multiplication.
constexpr OverflowSafeInt operator/(const OverflowSafeInt &divisor) const
constexpr OverflowSafeInt operator+(const OverflowSafeInt &other) const
constexpr OverflowSafeInt operator*(const OverflowSafeInt &factor) const
constexpr bool operator==(const OverflowSafeInt &other) const
friend constexpr OverflowSafeInt operator%(std::integral auto lhs, const OverflowSafeInt &rhs) noexcept
constexpr OverflowSafeInt operator/(std::integral auto divisor) const
constexpr OverflowSafeInt & operator+=(const OverflowSafeInt &other)
Safe implementation of addition.
constexpr auto operator<=>(std::integral auto other) const
friend constexpr OverflowSafeInt operator*(std::integral auto lhs, const OverflowSafeInt &rhs) noexcept
static constexpr T ConvertIfSafe(std::integral auto value)
Convert from any integral type to the base type.
constexpr OverflowSafeInt & operator/=(const OverflowSafeInt &divisor)
Divide operator.
constexpr OverflowSafeInt()=default
Construct an overflow-safe type instance with a default value.
constexpr OverflowSafeInt operator-(const OverflowSafeInt &other) const
constexpr auto operator<=>(const OverflowSafeInt &other) const
constexpr OverflowSafeInt operator%(const OverflowSafeInt &divisor) const
constexpr bool operator==(std::integral auto other) const
friend constexpr OverflowSafeInt operator+(std::integral auto lhs, const OverflowSafeInt &rhs) noexcept
constexpr OverflowSafeInt operator-(std::integral auto other) const
constexpr BaseType base() const noexcept
constexpr OverflowSafeInt & operator%=(const OverflowSafeInt &divisor)
Modulo operator.
static constexpr int64_t T_MAX
constexpr OverflowSafeInt(std::integral auto value)
Construct an overflow-safe type from an integral type.
constexpr OverflowSafeInt & operator=(std::integral auto other)
Safely assign an integral value to this overflow-safe instance.
constexpr OverflowSafeInt(const OverflowSafeInt< Tother > &value)
Safely construct an overflow-safe type from an overflow-safe type with a different base type.
constexpr OverflowSafeInt operator+(std::integral auto other) const
constexpr OverflowSafeInt operator%(std::integral auto divisor) const
static constexpr int64_t T_MIN
constexpr OverflowSafeInt & operator-=(const OverflowSafeInt &other)
Safe implementation of subtraction.
friend constexpr OverflowSafeInt operator/(std::integral auto lhs, const OverflowSafeInt &rhs) noexcept
friend constexpr OverflowSafeInt operator-(std::integral auto lhs, const OverflowSafeInt &rhs) noexcept
#define T
Climate temperate.
constexpr enum_type & operator++(enum_type &e)
Prefix increment.
constexpr enum_type operator-(enum_type e, int offset)
Subtract integer.
constexpr To ClampTo(From value)
Clamp the given value down to lie within the requested type.
OverflowSafeInt< int64_t > OverflowSafeInt64
A 64-bit overflow safe type.
OverflowSafeInt< int32_t > OverflowSafeInt32
A 32-bit overflow safe type.