rubidium@5587: /* $Id$ */ rubidium@5587: rubidium@8112: /** @file overflowsafe_type.hpp An overflow safe integer-like type. */ rubidium@5587: rubidium@8112: #ifndef OVERFLOWSAFE_TYPE_HPP rubidium@8112: #define OVERFLOWSAFE_TYPE_HPP rubidium@5587: rubidium@8113: #include "math_func.hpp" rubidium@8113: rubidium@7763: /** rubidium@7763: * Overflow safe template for integers, i.e. integers that will never overflow rubidium@7763: * you multiply the maximum value with 2, or add 2, or substract somethng from rubidium@7763: * the minimum value, etc. rubidium@7763: * @param T the type these integers are stored with. rubidium@7763: * @param T_MAX the maximum value for the integers. rubidium@7763: * @param T_MIN the minimum value for the integers. rubidium@7763: */ rubidium@7763: template rubidium@7763: class OverflowSafeInt rubidium@7763: { rubidium@7763: private: rubidium@7763: /** The non-overflow safe backend to store the value in. */ rubidium@7763: T m_value; rubidium@7763: public: rubidium@7763: OverflowSafeInt() : m_value(0) { } rubidium@7763: rubidium@7763: OverflowSafeInt(const OverflowSafeInt& other) { this->m_value = other.m_value; } rubidium@7763: OverflowSafeInt(const int64 int_) { this->m_value = int_; } rubidium@7763: rubidium@7763: FORCEINLINE OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; } rubidium@7763: rubidium@7763: FORCEINLINE OverflowSafeInt operator - () const { return OverflowSafeInt(-this->m_value); } rubidium@7763: rubidium@7763: /** rubidium@7763: * Safe implementation of addition. rubidium@7763: * @param other the amount to add rubidium@7763: * @note when the addition would yield more than T_MAX (or less than T_MIN), rubidium@7763: * it will be T_MAX (respectively T_MIN). rubidium@7763: */ rubidium@7763: FORCEINLINE OverflowSafeInt& operator += (const OverflowSafeInt& other) rubidium@7763: { skidd13@7923: if ((T_MAX - abs(other.m_value)) < abs(this->m_value) && rubidium@7763: (this->m_value < 0) == (other.m_value < 0)) { rubidium@7763: this->m_value = (this->m_value < 0) ? T_MIN : T_MAX ; rubidium@7763: } else { rubidium@7763: this->m_value += other.m_value; rubidium@7763: } rubidium@7763: return *this; rubidium@7763: } rubidium@7763: rubidium@7763: /* Operators for addition and substraction */ rubidium@7763: FORCEINLINE OverflowSafeInt operator + (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result += other; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator + (const int other) const { OverflowSafeInt result = *this; result += (int64)other; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator + (const uint other) const { OverflowSafeInt result = *this; result += (int64)other; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt& operator -= (const OverflowSafeInt& other) { return *this += (-other); } rubidium@7763: FORCEINLINE OverflowSafeInt operator - (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result -= other; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator - (const int other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator - (const uint other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; } rubidium@7763: truelight@7838: FORCEINLINE OverflowSafeInt& operator ++ () { return *this += 1; } truelight@7838: FORCEINLINE OverflowSafeInt& operator -- () { return *this += -1; } truelight@7838: FORCEINLINE OverflowSafeInt operator ++ (int) { OverflowSafeInt org = *this; *this += 1; return org; } truelight@7838: FORCEINLINE OverflowSafeInt operator -- (int) { OverflowSafeInt org = *this; *this += -1; return org; } rubidium@7763: rubidium@7763: /** rubidium@7763: * Safe implementation of multiplication. rubidium@7763: * @param factor the factor to multiply this with. rubidium@7763: * @note when the multiplication would yield more than T_MAX (or less than T_MIN), rubidium@7763: * it will be T_MAX (respectively T_MIN). rubidium@7763: */ rubidium@7763: FORCEINLINE OverflowSafeInt& operator *= (const int factor) rubidium@7763: { skidd13@7923: if (factor != 0 && (T_MAX / abs(factor)) < abs(this->m_value)) { rubidium@7763: this->m_value = ((this->m_value < 0) == (factor < 0)) ? T_MAX : T_MIN ; rubidium@7763: } else { rubidium@7763: this->m_value *= factor ; rubidium@7763: } rubidium@7763: return *this; rubidium@7763: } rubidium@7763: rubidium@7763: /* Operators for multiplication */ rubidium@7763: FORCEINLINE OverflowSafeInt operator * (const int64 factor) const { OverflowSafeInt result = *this; result *= factor; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator * (const int factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator * (const uint factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator * (const uint16 factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator * (const byte factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } rubidium@7763: rubidium@7763: /* Operators for division */ rubidium@7763: FORCEINLINE OverflowSafeInt& operator /= (const int divisor) { this->m_value /= divisor; return *this; } rubidium@7763: FORCEINLINE OverflowSafeInt operator / (const OverflowSafeInt& divisor) const { OverflowSafeInt result = *this; result /= divisor.m_value; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator / (const int divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator / (const uint divisor) const { OverflowSafeInt result = *this; result /= (int)divisor; return result; } rubidium@7763: rubidium@7763: /* Operators for modulo */ rubidium@7763: FORCEINLINE OverflowSafeInt& operator %= (const int divisor) { this->m_value %= divisor; return *this; } rubidium@7763: FORCEINLINE OverflowSafeInt operator % (const int divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; } rubidium@7763: rubidium@7763: /* Operators for shifting */ rubidium@7763: FORCEINLINE OverflowSafeInt& operator <<= (const int shift) { this->m_value <<= shift; return *this; } rubidium@7763: FORCEINLINE OverflowSafeInt operator << (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt& operator >>= (const int shift) { this->m_value >>= shift; return *this; } rubidium@7763: FORCEINLINE OverflowSafeInt operator >> (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; } rubidium@7763: rubidium@7763: /* Operators for (in)equality when comparing overflow safe ints */ rubidium@7763: FORCEINLINE bool operator == (const OverflowSafeInt& other) const { return this->m_value == other.m_value; } rubidium@7763: FORCEINLINE bool operator != (const OverflowSafeInt& other) const { return !(*this == other); } rubidium@7763: FORCEINLINE bool operator > (const OverflowSafeInt& other) const { return this->m_value > other.m_value; } rubidium@7763: FORCEINLINE bool operator >= (const OverflowSafeInt& other) const { return this->m_value >= other.m_value; } rubidium@7763: FORCEINLINE bool operator < (const OverflowSafeInt& other) const { return !(*this >= other); } rubidium@7763: FORCEINLINE bool operator <= (const OverflowSafeInt& other) const { return !(*this > other); } rubidium@7763: rubidium@7763: /* Operators for (in)equality when comparing non-overflow safe ints */ rubidium@7763: FORCEINLINE bool operator == (const int other) const { return this->m_value == other; } rubidium@7763: FORCEINLINE bool operator != (const int other) const { return !(*this == other); } rubidium@7763: FORCEINLINE bool operator > (const int other) const { return this->m_value > other; } rubidium@7763: FORCEINLINE bool operator >= (const int other) const { return this->m_value >= other; } rubidium@7763: FORCEINLINE bool operator < (const int other) const { return !(*this >= other); } rubidium@7763: FORCEINLINE bool operator <= (const int other) const { return !(*this > other); } rubidium@7763: rubidium@7763: FORCEINLINE operator int64 () const { return this->m_value; } rubidium@7763: }; rubidium@7763: rubidium@7763: /* Sometimes we got int64 operator OverflowSafeInt instead of vice versa. Handle that properly */ rubidium@7763: template FORCEINLINE OverflowSafeInt operator + (int64 a, OverflowSafeInt b) { return b + a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator - (int64 a, OverflowSafeInt b) { return -b + a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator * (int64 a, OverflowSafeInt b) { return b * a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator / (int64 a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } rubidium@7763: rubidium@7763: /* Sometimes we got int operator OverflowSafeInt instead of vice versa. Handle that properly */ rubidium@7763: template FORCEINLINE OverflowSafeInt operator + (int a, OverflowSafeInt b) { return b + a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator - (int a, OverflowSafeInt b) { return -b + a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator * (int a, OverflowSafeInt b) { return b * a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator / (int a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } rubidium@7763: rubidium@7763: /* Sometimes we got uint operator OverflowSafeInt instead of vice versa. Handle that properly */ rubidium@7763: template FORCEINLINE OverflowSafeInt operator + (uint a, OverflowSafeInt b) { return b + a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator - (uint a, OverflowSafeInt b) { return -b + a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator * (uint a, OverflowSafeInt b) { return b * a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator / (uint a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } rubidium@7763: rubidium@7763: /* Sometimes we got byte operator OverflowSafeInt instead of vice versa. Handle that properly */ rubidium@9047: template FORCEINLINE OverflowSafeInt operator + (byte a, OverflowSafeInt b) { return b + (uint)a; } rubidium@9047: template FORCEINLINE OverflowSafeInt operator - (byte a, OverflowSafeInt b) { return -b + (uint)a; } rubidium@9047: template FORCEINLINE OverflowSafeInt operator * (byte a, OverflowSafeInt b) { return b * (uint)a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator / (byte a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } rubidium@7763: rubidium@8112: typedef OverflowSafeInt OverflowSafeInt64; rubidium@8112: rubidium@8112: #endif /* OVERFLOWSAFE_TYPE_HPP */