tagging release
[dasher.git] / trunk / Src / Common / Types / int32.h
blob9c15039e4e3f46f9036ce3d104a377789b5ac93c
1 // int32.h
2 //
3 /////////////////////////////////////////////////////////////////////////////
4 //
5 // Copyright (c) 2004 David Ward
6 //
7 /////////////////////////////////////////////////////////////////////////////
9 #ifndef __int32_h__
10 #define __int32_h__
12 #include "../myassert.h"
13 #include "int.h"
15 #ifdef _DEBUG
17 class Cint64;
19 #ifdef _MSC_VER
20 #if _MSC_VER == 1300
21 #define NON_STANDARD_LL
22 #endif
23 #if _WIN32_WCE
24 #define NON_STANDARD_LL
25 #endif
27 #endif
29 #ifdef NON_STANDARD_LL
30 const int64 int64_0xffffffff80000000LL = 0xffffffff80000000i64;
31 const uint64 uint64_0xffffffff00000000ULL = 0xffffffff80000000ui64;
32 #else
33 const int64 int64_0xffffffff80000000LL = 0xffffffff80000000LL;
34 const uint64 uint64_0xffffffff00000000ULL = 0xffffffff00000000ULL;
35 #endif
37 class Cint32 {
38 public:
39 static int32 Max() {
40 return std::numeric_limits< int32 >::max();
42 static int32 Min() {
43 return std::numeric_limits< int32 >::min();
46 Cint32() {
49 Cint32(int32 i):m_i(i) {
52 Cint32(int64 i);
54 Cint32(double d) {
55 if(d > double (Cint32::Max()))
56 DASHER_ASSERT(0);
58 if(d < double (Cint32::Min()))
59 DASHER_ASSERT(0);
60 m_i = int32(d);
63 Cint32(Cint64 i);
65 operator double () const {
66 return double (m_i);
68 operator long () const {
69 return long (m_i);
71 operator int64() const {
72 return m_i;
74 operator int32() const {
75 return m_i;
77 Cint32 & operator+=(Cint32 rhs) {
78 if(!AreDifferentSigns(m_i, rhs.m_i)) {
79 // 2-ve or 2-ve
80 if(rhs.m_i < 0) {
81 // 2-ve
82 if(m_i < Min() - rhs.m_i) {
83 DASHER_ASSERT(0);
85 // ok
87 else {
88 // 2+ve
89 if(Max() - m_i < rhs.m_i) {
90 DASHER_ASSERT(0);
92 // ok
95 m_i += rhs.m_i;
96 return *this;
99 Cint32 & operator-=(Cint32 rhs) {
100 if(AreDifferentSigns(m_i, rhs.m_i)) {
101 // 1-ve, 1+ve
102 if(m_i >= 0) {
103 if(m_i > Max() + rhs.m_i) {
104 DASHER_ASSERT(0);
107 else {
108 // lhs -ve
109 if(m_i < Min() + rhs.m_i)
110 DASHER_ASSERT(0);
111 // ok
114 m_i -= rhs.m_i;
115 return *this;
118 Cint32 & operator*=(Cint32 rhs) {
120 int64 tmp = (int64) m_i * (int64) rhs.m_i;
122 // upper 33 bits must be equal
123 if((tmp & int64_0xffffffff80000000LL) == 0 || (tmp & int64_0xffffffff80000000LL) == int64_0xffffffff80000000LL) {
124 m_i = (int32) tmp;
125 return *this;
128 DASHER_ASSERT(0);
129 return *this;
132 Cint32 & operator/=(Cint32 rhs) {
133 if(rhs.m_i == 0)
134 DASHER_ASSERT(0);
136 // corner case where this = Min and rhs = -1
137 if(m_i == Min() && rhs.m_i == -1)
138 DASHER_ASSERT(0);
140 m_i /= rhs.m_i;
141 return *this;
144 bool operator!=(Cint32 rhs) const {
145 return m_i != rhs.m_i;
147 bool operator<=(int rhs)const {
148 return m_i <= rhs;
150 bool operator>=(int rhs)const {
151 return m_i >= rhs;
153 bool operator<(int rhs)const {
154 return m_i < rhs;
156 bool operator>(int rhs)const {
157 return m_i > rhs;
159 bool operator<(Cint32 rhs) const {
160 return m_i < rhs.m_i;
162 bool operator>(Cint32 rhs) const {
163 return m_i > rhs.m_i;
165 bool operator<=(Cint32 rhs) const {
166 return m_i <= rhs.m_i;
168 bool operator>=(Cint32 rhs) const {
169 return m_i >= rhs.m_i;
171 private:
172 int32 m_i;
175 inline Cint32 operator +(Cint32 lhs, Cint32 rhs) {
176 lhs += rhs;
177 return lhs;
180 inline Cint32 operator +(Cint32 lhs, int rhs) {
181 lhs += Cint32(rhs);
182 return lhs;
185 inline double operator +(double lhs, Cint32 rhs) {
186 return lhs + double (rhs);
189 inline double operator +(Cint32 lhs, double rhs) {
190 return double (lhs) + rhs;
193 inline Cint32 operator +(int lhs, Cint32 rhs) {
194 rhs += Cint32(lhs);
195 return rhs;
198 inline Cint32 operator -(Cint32 lhs, Cint32 rhs) {
199 lhs -= rhs;
200 return lhs;
203 inline double operator -(double lhs, Cint32 rhs) {
204 return lhs - double (rhs);
207 inline double operator -(Cint32 lhs, double rhs) {
208 return double (lhs) - rhs;
211 inline Cint32 operator -(Cint32 lhs, int rhs) {
212 lhs -= Cint32(rhs);
213 return lhs;
216 inline Cint32 operator -(int lhs, Cint32 rhs) {
217 Cint32 temp = lhs;
218 temp -= rhs;
219 return temp;
222 inline Cint32 operator *(Cint32 lhs, int rhs) {
223 lhs *= Cint32(rhs);
224 return lhs;
227 inline Cint32 operator *(int lhs, Cint32 rhs) {
228 Cint32 temp = lhs;
229 temp *= rhs;
230 return temp;
233 inline Cint32 operator *(Cint32 lhs, Cint32 rhs) {
234 lhs *= rhs;
235 return lhs;
238 /*inline Cint32 operator *(int lhs, Cint32 rhs)
240 rhs*=Cint32(lhs);
241 return rhs;
245 inline double operator *(double lhs, Cint32 rhs) {
246 return lhs * double (rhs);
249 inline double operator *(Cint32 lhs, double rhs) {
250 return rhs * double (lhs);
253 inline Cint32 operator /(Cint32 lhs, int rhs) {
254 lhs /= Cint32(rhs);
255 return lhs;
258 inline Cint32 operator /(Cint32 lhs, Cint32 rhs) {
259 lhs /= rhs;
260 return lhs;
263 inline Cint32 operator /(int lhs, Cint32 rhs) {
264 Cint32 temp(lhs);
265 temp /= rhs;
266 return temp;
269 inline double operator /(Cint32 lhs, double rhs) {
270 return double (lhs) / rhs;
273 inline double operator /(double lhs, Cint32 rhs) {
274 return lhs / double (rhs);
277 inline bool operator >(double lhs, Cint32 rhs) {
278 return lhs > double (rhs);
281 inline bool operator >(long lhs, Cint32 rhs) {
282 return lhs > long (rhs);
285 inline bool operator <(long lhs, Cint32 rhs) {
286 return lhs < long (rhs);
289 #include "int64.h"
291 inline Cint32::Cint32(Cint64 i) {
292 if((int64) i > int64(Cint32::Max()))
293 DASHER_ASSERT(0);
294 if((int64) i < int64(Cint32::Min()))
295 DASHER_ASSERT(0);
296 m_i = int32(i);
300 inline Cint32 abs(Cint32 t) {
301 return Cint32(abs(int32(t)));
304 #else
306 typedef int32 Cint32;
308 #endif // _DEBUG
310 #endif // __include__