From f1dfc2f482daa69d037f7c80b5744547c34f5628 Mon Sep 17 00:00:00 2001 From: Charles Pigott Date: Tue, 20 Jul 2021 09:05:08 +0100 Subject: Codechange: Make OverflowSafeInt constexpr --- src/core/overflowsafe_type.hpp | 167 +++++++++++++++++++++-------------------- 1 file changed, 87 insertions(+), 80 deletions(-) (limited to 'src') diff --git a/src/core/overflowsafe_type.hpp b/src/core/overflowsafe_type.hpp index 01400df0a..0976a090b 100644 --- a/src/core/overflowsafe_type.hpp +++ b/src/core/overflowsafe_type.hpp @@ -36,21 +36,21 @@ private: /** The non-overflow safe backend to store the value in. */ T m_value; public: - OverflowSafeInt() : m_value(0) { } + constexpr OverflowSafeInt() : m_value(0) { } - OverflowSafeInt(const OverflowSafeInt& other) { this->m_value = other.m_value; } - OverflowSafeInt(const int64 int_) { this->m_value = int_; } + constexpr OverflowSafeInt(const OverflowSafeInt& other) : m_value(other.m_value) { } + constexpr OverflowSafeInt(const int64 int_) : m_value(int_) { } - inline OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; } + inline constexpr OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; } - inline OverflowSafeInt operator - () const { return OverflowSafeInt(this->m_value == T_MIN ? T_MAX : -this->m_value); } + inline constexpr OverflowSafeInt operator - () const { return OverflowSafeInt(this->m_value == T_MIN ? T_MAX : -this->m_value); } /** * Safe implementation of addition. * @param other the amount to add * @note when the addition would yield more than T_MAX, it will be T_MAX. */ - inline OverflowSafeInt& operator += (const OverflowSafeInt& other) + inline constexpr OverflowSafeInt& operator += (const OverflowSafeInt& other) { #ifdef HAS_OVERFLOW_BUILTINS if (unlikely(__builtin_add_overflow(this->m_value, other.m_value, &this->m_value))) { @@ -73,7 +73,7 @@ public: * @param other the amount to subtract * @note when the subtraction would yield less than T_MIN, it will be T_MIN. */ - inline OverflowSafeInt& operator -= (const OverflowSafeInt& other) + inline constexpr OverflowSafeInt& operator -= (const OverflowSafeInt& other) { #ifdef HAS_OVERFLOW_BUILTINS if (unlikely(__builtin_sub_overflow(this->m_value, other.m_value, &this->m_value))) { @@ -91,18 +91,18 @@ public: return *this; } - /* Operators for addition and subtraction */ - inline OverflowSafeInt operator + (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result += other; return result; } - inline OverflowSafeInt operator + (const int other) const { OverflowSafeInt result = *this; result += (int64)other; return result; } - inline OverflowSafeInt operator + (const uint other) const { OverflowSafeInt result = *this; result += (int64)other; return result; } - inline OverflowSafeInt operator - (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result -= other; return result; } - inline OverflowSafeInt operator - (const int other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; } - inline OverflowSafeInt operator - (const uint other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; } + /* Operators for addition and subtraction. */ + inline constexpr OverflowSafeInt operator + (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result += other; return result; } + inline constexpr OverflowSafeInt operator + (const int other) const { OverflowSafeInt result = *this; result += (int64)other; return result; } + inline constexpr OverflowSafeInt operator + (const uint other) const { OverflowSafeInt result = *this; result += (int64)other; return result; } + inline constexpr OverflowSafeInt operator - (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result -= other; return result; } + inline constexpr OverflowSafeInt operator - (const int other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; } + inline constexpr OverflowSafeInt operator - (const uint other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; } - inline OverflowSafeInt& operator ++ () { return *this += 1; } - inline OverflowSafeInt& operator -- () { return *this += -1; } - inline OverflowSafeInt operator ++ (int) { OverflowSafeInt org = *this; *this += 1; return org; } - inline OverflowSafeInt operator -- (int) { OverflowSafeInt org = *this; *this += -1; return org; } + inline constexpr OverflowSafeInt& operator ++ () { return *this += 1; } + inline constexpr OverflowSafeInt& operator -- () { return *this += -1; } + inline constexpr OverflowSafeInt operator ++ (int) { OverflowSafeInt org = *this; *this += 1; return org; } + inline constexpr OverflowSafeInt operator -- (int) { OverflowSafeInt org = *this; *this += -1; return org; } /** * Safe implementation of multiplication. @@ -110,10 +110,10 @@ public: * @note when the multiplication would yield more than T_MAX (or less than T_MIN), * it will be T_MAX (respectively T_MIN). */ - inline OverflowSafeInt& operator *= (const int factor) + inline constexpr OverflowSafeInt& operator *= (const int factor) { #ifdef HAS_OVERFLOW_BUILTINS - bool is_result_positive = (this->m_value < 0) == (factor < 0); // -ve * -ve == +ve + const bool is_result_positive = (this->m_value < 0) == (factor < 0); // -ve * -ve == +ve if (unlikely(__builtin_mul_overflow(this->m_value, factor, &this->m_value))) { this->m_value = is_result_positive ? T_MAX : T_MIN; } @@ -135,75 +135,82 @@ public: return *this; } - /* Operators for multiplication */ - inline OverflowSafeInt operator * (const int64 factor) const { OverflowSafeInt result = *this; result *= factor; return result; } - inline OverflowSafeInt operator * (const int factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } - inline OverflowSafeInt operator * (const uint factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } - inline OverflowSafeInt operator * (const uint16 factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } - inline OverflowSafeInt operator * (const byte factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } + /* Operators for multiplication. */ + inline constexpr OverflowSafeInt operator * (const int64 factor) const { OverflowSafeInt result = *this; result *= factor; return result; } + inline constexpr OverflowSafeInt operator * (const int factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } + inline constexpr OverflowSafeInt operator * (const uint factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } + inline constexpr OverflowSafeInt operator * (const uint16 factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } + inline constexpr OverflowSafeInt operator * (const byte factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } - /* Operators for division */ - inline OverflowSafeInt& operator /= (const int64 divisor) { this->m_value /= divisor; return *this; } - inline OverflowSafeInt operator / (const OverflowSafeInt& divisor) const { OverflowSafeInt result = *this; result /= divisor.m_value; return result; } - inline OverflowSafeInt operator / (const int divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; } - inline OverflowSafeInt operator / (const uint divisor) const { OverflowSafeInt result = *this; result /= (int)divisor; return result; } + /* Operators for division. */ + inline constexpr OverflowSafeInt& operator /= (const int64 divisor) { this->m_value /= divisor; return *this; } + inline constexpr OverflowSafeInt operator / (const OverflowSafeInt& divisor) const { OverflowSafeInt result = *this; result /= divisor.m_value; return result; } + inline constexpr OverflowSafeInt operator / (const int divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; } + inline constexpr OverflowSafeInt operator / (const uint divisor) const { OverflowSafeInt result = *this; result /= (int)divisor; return result; } /* Operators for modulo */ - inline OverflowSafeInt& operator %= (const int divisor) { this->m_value %= divisor; return *this; } - inline OverflowSafeInt operator % (const int divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; } - - /* Operators for shifting */ - inline OverflowSafeInt& operator <<= (const int shift) { this->m_value <<= shift; return *this; } - inline OverflowSafeInt operator << (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; } - inline OverflowSafeInt& operator >>= (const int shift) { this->m_value >>= shift; return *this; } - inline OverflowSafeInt operator >> (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; } - - /* Operators for (in)equality when comparing overflow safe ints */ - inline bool operator == (const OverflowSafeInt& other) const { return this->m_value == other.m_value; } - inline bool operator != (const OverflowSafeInt& other) const { return !(*this == other); } - inline bool operator > (const OverflowSafeInt& other) const { return this->m_value > other.m_value; } - inline bool operator >= (const OverflowSafeInt& other) const { return this->m_value >= other.m_value; } - inline bool operator < (const OverflowSafeInt& other) const { return !(*this >= other); } - inline bool operator <= (const OverflowSafeInt& other) const { return !(*this > other); } - - /* Operators for (in)equality when comparing non-overflow safe ints */ - inline bool operator == (const int other) const { return this->m_value == other; } - inline bool operator != (const int other) const { return !(*this == other); } - inline bool operator > (const int other) const { return this->m_value > other; } - inline bool operator >= (const int other) const { return this->m_value >= other; } - inline bool operator < (const int other) const { return !(*this >= other); } - inline bool operator <= (const int other) const { return !(*this > other); } - - inline operator int64 () const { return this->m_value; } + inline constexpr OverflowSafeInt& operator %= (const int divisor) { this->m_value %= divisor; return *this; } + inline constexpr OverflowSafeInt operator % (const int divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; } + + /* Operators for shifting. */ + inline constexpr OverflowSafeInt& operator <<= (const int shift) { this->m_value <<= shift; return *this; } + inline constexpr OverflowSafeInt operator << (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; } + inline constexpr OverflowSafeInt& operator >>= (const int shift) { this->m_value >>= shift; return *this; } + inline constexpr OverflowSafeInt operator >> (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; } + + /* Operators for (in)equality when comparing overflow safe ints. */ + inline constexpr bool operator == (const OverflowSafeInt& other) const { return this->m_value == other.m_value; } + inline constexpr bool operator != (const OverflowSafeInt& other) const { return !(*this == other); } + inline constexpr bool operator > (const OverflowSafeInt& other) const { return this->m_value > other.m_value; } + inline constexpr bool operator >= (const OverflowSafeInt& other) const { return this->m_value >= other.m_value; } + inline constexpr bool operator < (const OverflowSafeInt& other) const { return !(*this >= other); } + inline constexpr bool operator <= (const OverflowSafeInt& other) const { return !(*this > other); } + + /* Operators for (in)equality when comparing non-overflow safe ints. */ + inline constexpr bool operator == (const int other) const { return this->m_value == other; } + inline constexpr bool operator != (const int other) const { return !(*this == other); } + inline constexpr bool operator > (const int other) const { return this->m_value > other; } + inline constexpr bool operator >= (const int other) const { return this->m_value >= other; } + inline constexpr bool operator < (const int other) const { return !(*this >= other); } + inline constexpr bool operator <= (const int other) const { return !(*this > other); } + + inline constexpr operator int64 () const { return this->m_value; } }; -/* Sometimes we got int64 operator OverflowSafeInt instead of vice versa. Handle that properly */ -template inline OverflowSafeInt operator + (int64 a, OverflowSafeInt b) { return b + a; } -template inline OverflowSafeInt operator - (int64 a, OverflowSafeInt b) { return -b + a; } -template inline OverflowSafeInt operator * (int64 a, OverflowSafeInt b) { return b * a; } -template inline OverflowSafeInt operator / (int64 a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } - -/* Sometimes we got int operator OverflowSafeInt instead of vice versa. Handle that properly */ -template inline OverflowSafeInt operator + (int a, OverflowSafeInt b) { return b + a; } -template inline OverflowSafeInt operator - (int a, OverflowSafeInt b) { return -b + a; } -template inline OverflowSafeInt operator * (int a, OverflowSafeInt b) { return b * a; } -template inline OverflowSafeInt operator / (int a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } - -/* Sometimes we got uint operator OverflowSafeInt instead of vice versa. Handle that properly */ -template inline OverflowSafeInt operator + (uint a, OverflowSafeInt b) { return b + a; } -template inline OverflowSafeInt operator - (uint a, OverflowSafeInt b) { return -b + a; } -template inline OverflowSafeInt operator * (uint a, OverflowSafeInt b) { return b * a; } -template inline OverflowSafeInt operator / (uint a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } - -/* Sometimes we got byte operator OverflowSafeInt instead of vice versa. Handle that properly */ -template inline OverflowSafeInt operator + (byte a, OverflowSafeInt b) { return b + (uint)a; } -template inline OverflowSafeInt operator - (byte a, OverflowSafeInt b) { return -b + (uint)a; } -template inline OverflowSafeInt operator * (byte a, OverflowSafeInt b) { return b * (uint)a; } -template inline OverflowSafeInt operator / (byte a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } + +/* Sometimes we got int64 operator OverflowSafeInt instead of vice versa. Handle that properly. */ +template inline constexpr OverflowSafeInt operator + (const int64 a, const OverflowSafeInt b) { return b + a; } +template inline constexpr OverflowSafeInt operator - (const int64 a, const OverflowSafeInt b) { return -b + a; } +template inline constexpr OverflowSafeInt operator * (const int64 a, const OverflowSafeInt b) { return b * a; } +template inline constexpr OverflowSafeInt operator / (const int64 a, const OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } + +/* Sometimes we got int operator OverflowSafeInt instead of vice versa. Handle that properly. */ +template inline constexpr OverflowSafeInt operator + (const int a, const OverflowSafeInt b) { return b + a; } +template inline constexpr OverflowSafeInt operator - (const int a, const OverflowSafeInt b) { return -b + a; } +template inline constexpr OverflowSafeInt operator * (const int a, const OverflowSafeInt b) { return b * a; } +template inline constexpr OverflowSafeInt operator / (const int a, const OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } + +/* Sometimes we got uint operator OverflowSafeInt instead of vice versa. Handle that properly. */ +template inline constexpr OverflowSafeInt operator + (const uint a, const OverflowSafeInt b) { return b + a; } +template inline constexpr OverflowSafeInt operator - (const uint a, const OverflowSafeInt b) { return -b + a; } +template inline constexpr OverflowSafeInt operator * (const uint a, const OverflowSafeInt b) { return b * a; } +template inline constexpr OverflowSafeInt operator / (const uint a, const OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } + +/* Sometimes we got byte operator OverflowSafeInt instead of vice versa. Handle that properly. */ +template inline constexpr OverflowSafeInt operator + (const byte a, const OverflowSafeInt b) { return b + (uint)a; } +template inline constexpr OverflowSafeInt operator - (const byte a, const OverflowSafeInt b) { return -b + (uint)a; } +template inline constexpr OverflowSafeInt operator * (const byte a, const OverflowSafeInt b) { return b * (uint)a; } +template inline constexpr OverflowSafeInt operator / (const byte a, const OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } typedef OverflowSafeInt OverflowSafeInt64; typedef OverflowSafeInt OverflowSafeInt32; +/* Some basic "unit tests". Also has the bonus of confirming that constexpr is working. */ +static_assert(OverflowSafeInt32(INT32_MIN) - 1 == OverflowSafeInt32(INT32_MIN)); +static_assert(OverflowSafeInt32(INT32_MAX) + 1 == OverflowSafeInt32(INT32_MAX)); +static_assert(OverflowSafeInt32(INT32_MAX) * 2 == OverflowSafeInt32(INT32_MAX)); +static_assert(OverflowSafeInt32(INT32_MIN) * 2 == OverflowSafeInt32(INT32_MIN)); + #undef HAS_OVERFLOW_BUILTINS #endif /* OVERFLOWSAFE_TYPE_HPP */ -- cgit v1.2.3-70-g09d2