author | peter1138 |
Tue, 22 Jan 2008 07:27:06 +0000 | |
changeset 8374 | 7a1b6c89cb89 |
parent 8113 | 31b7784db761 |
child 9047 | 1b4e4af03845 |
permissions | -rw-r--r-- |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
1 |
/* $Id$ */ |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
2 |
|
8112
24b89cd40bfd
(svn r11673) -Codechange: move the overflow safe type to a separate file.
rubidium
parents:
8100
diff
changeset
|
3 |
/** @file overflowsafe_type.hpp An overflow safe integer-like type. */ |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
4 |
|
8112
24b89cd40bfd
(svn r11673) -Codechange: move the overflow safe type to a separate file.
rubidium
parents:
8100
diff
changeset
|
5 |
#ifndef OVERFLOWSAFE_TYPE_HPP |
24b89cd40bfd
(svn r11673) -Codechange: move the overflow safe type to a separate file.
rubidium
parents:
8100
diff
changeset
|
6 |
#define OVERFLOWSAFE_TYPE_HPP |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
7 |
|
8113
31b7784db761
(svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
8112
diff
changeset
|
8 |
#include "math_func.hpp" |
31b7784db761
(svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
8112
diff
changeset
|
9 |
|
7763
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
10 |
/** |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
11 |
* Overflow safe template for integers, i.e. integers that will never overflow |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
12 |
* you multiply the maximum value with 2, or add 2, or substract somethng from |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
13 |
* the minimum value, etc. |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
14 |
* @param T the type these integers are stored with. |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
15 |
* @param T_MAX the maximum value for the integers. |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
16 |
* @param T_MIN the minimum value for the integers. |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
17 |
*/ |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
18 |
template <class T, T T_MAX, T T_MIN> |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
19 |
class OverflowSafeInt |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
20 |
{ |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
21 |
private: |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
22 |
/** The non-overflow safe backend to store the value in. */ |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
23 |
T m_value; |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
24 |
public: |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
25 |
OverflowSafeInt() : m_value(0) { } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
26 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
27 |
OverflowSafeInt(const OverflowSafeInt& other) { this->m_value = other.m_value; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
28 |
OverflowSafeInt(const int64 int_) { this->m_value = int_; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
29 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
30 |
FORCEINLINE OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
31 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
32 |
FORCEINLINE OverflowSafeInt operator - () const { return OverflowSafeInt(-this->m_value); } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
33 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
34 |
/** |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
35 |
* Safe implementation of addition. |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
36 |
* @param other the amount to add |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
37 |
* @note when the addition would yield more than T_MAX (or less than T_MIN), |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
38 |
* it will be T_MAX (respectively T_MIN). |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
39 |
*/ |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
40 |
FORCEINLINE OverflowSafeInt& operator += (const OverflowSafeInt& other) |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
41 |
{ |
7923
91982bd3c549
(svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents:
7838
diff
changeset
|
42 |
if ((T_MAX - abs(other.m_value)) < abs(this->m_value) && |
7763
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
43 |
(this->m_value < 0) == (other.m_value < 0)) { |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
44 |
this->m_value = (this->m_value < 0) ? T_MIN : T_MAX ; |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
45 |
} else { |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
46 |
this->m_value += other.m_value; |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
47 |
} |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
48 |
return *this; |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
49 |
} |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
50 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
51 |
/* Operators for addition and substraction */ |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
52 |
FORCEINLINE OverflowSafeInt operator + (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result += other; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
53 |
FORCEINLINE OverflowSafeInt operator + (const int other) const { OverflowSafeInt result = *this; result += (int64)other; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
54 |
FORCEINLINE OverflowSafeInt operator + (const uint other) const { OverflowSafeInt result = *this; result += (int64)other; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
55 |
FORCEINLINE OverflowSafeInt& operator -= (const OverflowSafeInt& other) { return *this += (-other); } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
56 |
FORCEINLINE OverflowSafeInt operator - (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result -= other; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
57 |
FORCEINLINE OverflowSafeInt operator - (const int other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
58 |
FORCEINLINE OverflowSafeInt operator - (const uint other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
59 |
|
7838
954e607c2139
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
7763
diff
changeset
|
60 |
FORCEINLINE OverflowSafeInt& operator ++ () { return *this += 1; } |
954e607c2139
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
7763
diff
changeset
|
61 |
FORCEINLINE OverflowSafeInt& operator -- () { return *this += -1; } |
954e607c2139
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
7763
diff
changeset
|
62 |
FORCEINLINE OverflowSafeInt operator ++ (int) { OverflowSafeInt org = *this; *this += 1; return org; } |
954e607c2139
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
7763
diff
changeset
|
63 |
FORCEINLINE OverflowSafeInt operator -- (int) { OverflowSafeInt org = *this; *this += -1; return org; } |
7763
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
64 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
65 |
/** |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
66 |
* Safe implementation of multiplication. |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
67 |
* @param factor the factor to multiply this with. |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
68 |
* @note when the multiplication would yield more than T_MAX (or less than T_MIN), |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
69 |
* it will be T_MAX (respectively T_MIN). |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
70 |
*/ |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
71 |
FORCEINLINE OverflowSafeInt& operator *= (const int factor) |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
72 |
{ |
7923
91982bd3c549
(svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents:
7838
diff
changeset
|
73 |
if (factor != 0 && (T_MAX / abs(factor)) < abs(this->m_value)) { |
7763
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
74 |
this->m_value = ((this->m_value < 0) == (factor < 0)) ? T_MAX : T_MIN ; |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
75 |
} else { |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
76 |
this->m_value *= factor ; |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
77 |
} |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
78 |
return *this; |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
79 |
} |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
80 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
81 |
/* Operators for multiplication */ |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
82 |
FORCEINLINE OverflowSafeInt operator * (const int64 factor) const { OverflowSafeInt result = *this; result *= factor; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
83 |
FORCEINLINE OverflowSafeInt operator * (const int factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
84 |
FORCEINLINE OverflowSafeInt operator * (const uint factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
85 |
FORCEINLINE OverflowSafeInt operator * (const uint16 factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
86 |
FORCEINLINE OverflowSafeInt operator * (const byte factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
87 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
88 |
/* Operators for division */ |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
89 |
FORCEINLINE OverflowSafeInt& operator /= (const int divisor) { this->m_value /= divisor; return *this; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
90 |
FORCEINLINE OverflowSafeInt operator / (const OverflowSafeInt& divisor) const { OverflowSafeInt result = *this; result /= divisor.m_value; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
91 |
FORCEINLINE OverflowSafeInt operator / (const int divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
92 |
FORCEINLINE OverflowSafeInt operator / (const uint divisor) const { OverflowSafeInt result = *this; result /= (int)divisor; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
93 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
94 |
/* Operators for modulo */ |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
95 |
FORCEINLINE OverflowSafeInt& operator %= (const int divisor) { this->m_value %= divisor; return *this; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
96 |
FORCEINLINE OverflowSafeInt operator % (const int divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
97 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
98 |
/* Operators for shifting */ |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
99 |
FORCEINLINE OverflowSafeInt& operator <<= (const int shift) { this->m_value <<= shift; return *this; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
100 |
FORCEINLINE OverflowSafeInt operator << (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
101 |
FORCEINLINE OverflowSafeInt& operator >>= (const int shift) { this->m_value >>= shift; return *this; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
102 |
FORCEINLINE OverflowSafeInt operator >> (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
103 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
104 |
/* Operators for (in)equality when comparing overflow safe ints */ |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
105 |
FORCEINLINE bool operator == (const OverflowSafeInt& other) const { return this->m_value == other.m_value; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
106 |
FORCEINLINE bool operator != (const OverflowSafeInt& other) const { return !(*this == other); } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
107 |
FORCEINLINE bool operator > (const OverflowSafeInt& other) const { return this->m_value > other.m_value; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
108 |
FORCEINLINE bool operator >= (const OverflowSafeInt& other) const { return this->m_value >= other.m_value; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
109 |
FORCEINLINE bool operator < (const OverflowSafeInt& other) const { return !(*this >= other); } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
110 |
FORCEINLINE bool operator <= (const OverflowSafeInt& other) const { return !(*this > other); } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
111 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
112 |
/* Operators for (in)equality when comparing non-overflow safe ints */ |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
113 |
FORCEINLINE bool operator == (const int other) const { return this->m_value == other; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
114 |
FORCEINLINE bool operator != (const int other) const { return !(*this == other); } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
115 |
FORCEINLINE bool operator > (const int other) const { return this->m_value > other; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
116 |
FORCEINLINE bool operator >= (const int other) const { return this->m_value >= other; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
117 |
FORCEINLINE bool operator < (const int other) const { return !(*this >= other); } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
118 |
FORCEINLINE bool operator <= (const int other) const { return !(*this > other); } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
119 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
120 |
FORCEINLINE operator int64 () const { return this->m_value; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
121 |
}; |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
122 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
123 |
/* Sometimes we got int64 operator OverflowSafeInt instead of vice versa. Handle that properly */ |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
124 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator + (int64 a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + a; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
125 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator - (int64 a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return -b + a; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
126 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator * (int64 a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b * a; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
127 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator / (int64 a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
128 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
129 |
/* Sometimes we got int operator OverflowSafeInt instead of vice versa. Handle that properly */ |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
130 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator + (int a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + a; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
131 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator - (int a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return -b + a; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
132 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator * (int a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b * a; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
133 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator / (int a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
134 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
135 |
/* Sometimes we got uint operator OverflowSafeInt instead of vice versa. Handle that properly */ |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
136 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator + (uint a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + a; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
137 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator - (uint a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return -b + a; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
138 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator * (uint a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b * a; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
139 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator / (uint a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
140 |
|
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
141 |
/* Sometimes we got byte operator OverflowSafeInt instead of vice versa. Handle that properly */ |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
142 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator + (byte a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + a; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
143 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator - (byte a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return -b + a; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
144 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator * (byte a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b * a; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
145 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator / (byte a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; } |
342e5357b592
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6179
diff
changeset
|
146 |
|
8112
24b89cd40bfd
(svn r11673) -Codechange: move the overflow safe type to a separate file.
rubidium
parents:
8100
diff
changeset
|
147 |
typedef OverflowSafeInt<int64, INT64_MAX, INT64_MIN> OverflowSafeInt64; |
24b89cd40bfd
(svn r11673) -Codechange: move the overflow safe type to a separate file.
rubidium
parents:
8100
diff
changeset
|
148 |
|
24b89cd40bfd
(svn r11673) -Codechange: move the overflow safe type to a separate file.
rubidium
parents:
8100
diff
changeset
|
149 |
#endif /* OVERFLOWSAFE_TYPE_HPP */ |