1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: declaration of wxLongLong class - best implementation of a 64
4 // bit integer for the current platform.
5 // Author: Jeffrey C. Ollie <jeff@ollie.clive.ia.us>, Vadim Zeitlin
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_LONGLONG_H
14 #define _WX_LONGLONG_H
17 #pragma interface "longlong.h"
21 #include "wx/wxchar.h"
23 #include <limits.h> // for LONG_MAX
25 // define this to compile wxLongLongWx in "test" mode: the results of all
26 // calculations will be compared with the real results taken from
27 // wxLongLongNative -- this is extremely useful to find the bugs in
28 // wxLongLongWx class!
30 // #define wxLONGLONG_TEST_MODE
32 #ifdef wxLONGLONG_TEST_MODE
33 #define wxUSE_LONGLONG_WX 1
34 #define wxUSE_LONGLONG_NATIVE 1
35 #endif // wxLONGLONG_TEST_MODE
37 // ----------------------------------------------------------------------------
38 // decide upon which class we will use
39 // ----------------------------------------------------------------------------
41 // to avoid compilation problems on 64bit machines with ambiguous method calls
42 // we will need to define this
43 #undef wxLongLongIsLong
45 // NB: we #define and not typedef wxLongLong_t because we want to be able to
46 // use 'unsigned wxLongLong_t' as well and because we use "#ifdef
47 // wxLongLong_t" below
48 #if defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
49 #define wxLongLong_t long
50 #define wxLongLongIsLong
51 #elif (defined(__VISUALC__) && defined(__WIN32__)) || defined( __VMS__ )
52 #define wxLongLong_t __int64
53 #elif defined(__BORLANDC__) && defined(__WIN32__) && (__BORLANDC__ >= 0x520)
54 #define wxLongLong_t __int64
55 #elif defined(__GNUG__)
56 #define wxLongLong_t long long
57 #elif defined(__MWERKS__)
58 #if __option(longlong)
59 #define wxLongLong_t long long
61 #error "The 64 bit integer support in CodeWarrior has been disabled."
62 #error "See the documentation on the 'longlong' pragma."
64 #elif defined(__VISAGECPP__) && __IBMCPP__ >= 400
65 #define wxLongLong_t long long
66 #else // no native long long type
67 // both warning and pragma warning are not portable, but at least an
68 // unknown pragma should never be an error - unless the compiler is
69 // seriously broken as Watcom C++ seems to be...
71 #pragma warning "Your compiler does not appear to support 64 bit "\
72 "integers, using emulation class instead."
74 #define wxUSE_LONGLONG_WX 1
77 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
78 // to disable automatic testing (useful for the test program which defines
79 // both classes) but by default we only use one class
80 #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
81 // don't use both classes unless wxUSE_LONGLONG_NATIVE was explicitly set:
82 // this is useful in test programs and only there
83 #ifndef wxUSE_LONGLONG_NATIVE
84 #define wxUSE_LONGLONG_NATIVE 0
87 class WXDLLEXPORT wxLongLongWx
;
88 typedef wxLongLongWx wxLongLong
;
90 // if nothing is defined, use native implementation by default, of course
91 #ifndef wxUSE_LONGLONG_NATIVE
92 #define wxUSE_LONGLONG_NATIVE 1
96 #ifndef wxUSE_LONGLONG_WX
97 #define wxUSE_LONGLONG_WX 0
98 class WXDLLEXPORT wxLongLongNative
;
99 typedef wxLongLongNative wxLongLong
;
102 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
103 // typedef wxLongLong as it wants, we don't do it
105 // ----------------------------------------------------------------------------
106 // choose the appropriate class
107 // ----------------------------------------------------------------------------
109 // we use iostream for wxLongLong output
110 #include "wx/ioswrap.h"
112 #if wxUSE_LONGLONG_NATIVE
114 class WXDLLEXPORT wxLongLongNative
118 // default ctor initializes to 0
119 wxLongLongNative() { m_ll
= 0; }
121 wxLongLongNative(wxLongLong_t ll
) { m_ll
= ll
; }
123 wxLongLongNative(long hi
, unsigned long lo
)
125 // assign first to avoid precision loss!
126 m_ll
= ((wxLongLong_t
) hi
) << 32;
127 m_ll
|= (wxLongLong_t
) lo
;
130 // default copy ctor is ok
134 // assignment operators
135 // from native 64 bit integer
136 wxLongLongNative
& operator=(wxLongLong_t ll
)
137 { m_ll
= ll
; return *this; }
139 // from double: this one has an explicit name because otherwise we
140 // would have ambiguity with "ll = int" and also because we don't want
141 // to have implicit conversions between doubles and wxLongLongs
142 wxLongLongNative
& Assign(double d
)
143 { m_ll
= (wxLongLong_t
)d
; return *this; }
145 // assignment operators from wxLongLongNative is ok
150 { return (long)(m_ll
>> 32); }
152 unsigned long GetLo() const
153 { return (unsigned long)m_ll
; }
155 // get absolute value
156 wxLongLongNative
Abs() const { return wxLongLongNative(*this).Abs(); }
157 wxLongLongNative
& Abs() { if ( m_ll
< 0 ) m_ll
= -m_ll
; return *this; }
159 // convert to native long long
160 wxLongLong_t
GetValue() const { return m_ll
; }
162 // convert to long with range checking in the debug mode (only!)
165 wxASSERT_MSG( (m_ll
>= LONG_MIN
) && (m_ll
<= LONG_MAX
),
166 _T("wxLongLong to long conversion loss of precision") );
171 // don't provide implicit conversion to wxLongLong_t or we will have an
172 // ambiguity for all arithmetic operations
173 //operator wxLongLong_t() const { return m_ll; }
177 wxLongLongNative
operator+(const wxLongLongNative
& ll
) const
178 { return wxLongLongNative(m_ll
+ ll
.m_ll
); }
179 wxLongLongNative
& operator+=(const wxLongLongNative
& ll
)
180 { m_ll
+= ll
.m_ll
; return *this; }
182 wxLongLongNative
operator+(const wxLongLong_t ll
) const
183 { return wxLongLongNative(m_ll
+ ll
); }
184 wxLongLongNative
& operator+=(const wxLongLong_t ll
)
185 { m_ll
+= ll
; return *this; }
188 wxLongLongNative
& operator++()
189 { m_ll
++; return *this; }
192 wxLongLongNative
& operator++(int)
193 { m_ll
++; return *this; }
196 wxLongLongNative
operator-() const
197 { return wxLongLongNative(-m_ll
); }
200 wxLongLongNative
operator-(const wxLongLongNative
& ll
) const
201 { return wxLongLongNative(m_ll
- ll
.m_ll
); }
202 wxLongLongNative
& operator-=(const wxLongLongNative
& ll
)
203 { m_ll
-= ll
.m_ll
; return *this; }
205 wxLongLongNative
operator-(const wxLongLong_t ll
) const
206 { return wxLongLongNative(m_ll
- ll
); }
207 wxLongLongNative
& operator-=(const wxLongLong_t ll
)
208 { m_ll
-= ll
; return *this; }
211 wxLongLongNative
& operator--()
212 { m_ll
--; return *this; }
215 wxLongLongNative
& operator--(int)
216 { m_ll
--; return *this; }
220 wxLongLongNative
operator<<(int shift
) const
221 { return wxLongLongNative(m_ll
<< shift
);; }
222 wxLongLongNative
& operator<<=(int shift
)
223 { m_ll
<<= shift
; return *this; }
226 wxLongLongNative
operator>>(int shift
) const
227 { return wxLongLongNative(m_ll
>> shift
);; }
228 wxLongLongNative
& operator>>=(int shift
)
229 { m_ll
>>= shift
; return *this; }
232 wxLongLongNative
operator&(const wxLongLongNative
& ll
) const
233 { return wxLongLongNative(m_ll
& ll
.m_ll
); }
234 wxLongLongNative
& operator&=(const wxLongLongNative
& ll
)
235 { m_ll
&= ll
.m_ll
; return *this; }
237 wxLongLongNative
operator|(const wxLongLongNative
& ll
) const
238 { return wxLongLongNative(m_ll
| ll
.m_ll
); }
239 wxLongLongNative
& operator|=(const wxLongLongNative
& ll
)
240 { m_ll
|= ll
.m_ll
; return *this; }
242 wxLongLongNative
operator^(const wxLongLongNative
& ll
) const
243 { return wxLongLongNative(m_ll
^ ll
.m_ll
); }
244 wxLongLongNative
& operator^=(const wxLongLongNative
& ll
)
245 { m_ll
^= ll
.m_ll
; return *this; }
247 // multiplication/division
248 wxLongLongNative
operator*(const wxLongLongNative
& ll
) const
249 { return wxLongLongNative(m_ll
* ll
.m_ll
); }
250 wxLongLongNative
operator*(long l
) const
251 { return wxLongLongNative(m_ll
* l
); }
252 wxLongLongNative
& operator*=(const wxLongLongNative
& ll
)
253 { m_ll
*= ll
.m_ll
; return *this; }
254 wxLongLongNative
& operator*=(long l
)
255 { m_ll
*= l
; return *this; }
257 wxLongLongNative
operator/(const wxLongLongNative
& ll
) const
258 { return wxLongLongNative(m_ll
/ ll
.m_ll
); }
259 wxLongLongNative
operator/(long l
) const
260 { return wxLongLongNative(m_ll
/ l
); }
261 wxLongLongNative
& operator/=(const wxLongLongNative
& ll
)
262 { m_ll
/= ll
.m_ll
; return *this; }
263 wxLongLongNative
& operator/=(long l
)
264 { m_ll
/= l
; return *this; }
266 wxLongLongNative
operator%(const wxLongLongNative
& ll
) const
267 { return wxLongLongNative(m_ll
% ll
.m_ll
); }
268 wxLongLongNative
operator%(long l
) const
269 { return wxLongLongNative(m_ll
% l
); }
272 bool operator==(const wxLongLongNative
& ll
) const
273 { return m_ll
== ll
.m_ll
; }
274 bool operator==(long l
) const
275 { return m_ll
== l
; }
276 bool operator!=(const wxLongLongNative
& ll
) const
277 { return m_ll
!= ll
.m_ll
; }
278 bool operator!=(long l
) const
279 { return m_ll
!= l
; }
280 bool operator<(const wxLongLongNative
& ll
) const
281 { return m_ll
< ll
.m_ll
; }
282 bool operator<(long l
) const
284 bool operator>(const wxLongLongNative
& ll
) const
285 { return m_ll
> ll
.m_ll
; }
286 bool operator>(long l
) const
288 bool operator<=(const wxLongLongNative
& ll
) const
289 { return m_ll
<= ll
.m_ll
; }
290 bool operator<=(long l
) const
291 { return m_ll
<= l
; }
292 bool operator>=(const wxLongLongNative
& ll
) const
293 { return m_ll
>= ll
.m_ll
; }
294 bool operator>=(long l
) const
295 { return m_ll
>= l
; }
298 // conversion to byte array: returns a pointer to static buffer!
299 void *asArray() const;
301 #if wxUSE_STD_IOSTREAM
303 friend ostream
& operator<<(ostream
&, const wxLongLongNative
&);
310 #endif // wxUSE_LONGLONG_NATIVE
312 #if wxUSE_LONGLONG_WX
314 class WXDLLEXPORT wxLongLongWx
318 // default ctor initializes to 0
323 #ifdef wxLONGLONG_TEST_MODE
327 #endif // wxLONGLONG_TEST_MODE
330 wxLongLongWx(long l
) { *this = l
; }
332 wxLongLongWx(long hi
, unsigned long lo
)
337 #ifdef wxLONGLONG_TEST_MODE
343 #endif // wxLONGLONG_TEST_MODE
346 // default copy ctor is ok in both cases
350 // assignment operators
352 wxLongLongWx
& operator=(long l
)
355 m_hi
= (l
< 0 ? -1l : 0l);
357 #ifdef wxLONGLONG_TEST_MODE
361 #endif // wxLONGLONG_TEST_MODE
366 wxLongLongWx
& Assign(double d
);
367 // can't have assignment operator from 2 longs
371 long GetHi() const { return m_hi
; }
373 unsigned long GetLo() const { return m_lo
; }
375 // get absolute value
376 wxLongLongWx
Abs() const { return wxLongLongWx(*this).Abs(); }
382 #ifdef wxLONGLONG_TEST_MODE
387 #endif // wxLONGLONG_TEST_MODE
392 // convert to long with range checking in the debug mode (only!)
395 wxASSERT_MSG( (m_hi
== 0l) || (m_hi
== -1l),
396 _T("wxLongLong to long conversion loss of precision") );
403 wxLongLongWx
operator+(const wxLongLongWx
& ll
) const;
404 wxLongLongWx
& operator+=(const wxLongLongWx
& ll
);
405 wxLongLongWx
operator+(long l
) const;
406 wxLongLongWx
& operator+=(long l
);
408 // pre increment operator
409 wxLongLongWx
& operator++();
411 // post increment operator
412 wxLongLongWx
& operator++(int) { return ++(*this); }
415 wxLongLongWx
operator-() const;
416 wxLongLongWx
& Negate();
419 wxLongLongWx
operator-(const wxLongLongWx
& ll
) const;
420 wxLongLongWx
& operator-=(const wxLongLongWx
& ll
);
422 // pre decrement operator
423 wxLongLongWx
& operator--();
425 // post decrement operator
426 wxLongLongWx
& operator--(int) { return --(*this); }
430 wxLongLongWx
operator<<(int shift
) const;
431 wxLongLongWx
& operator<<=(int shift
);
434 wxLongLongWx
operator>>(int shift
) const;
435 wxLongLongWx
& operator>>=(int shift
);
438 wxLongLongWx
operator&(const wxLongLongWx
& ll
) const;
439 wxLongLongWx
& operator&=(const wxLongLongWx
& ll
);
440 wxLongLongWx
operator|(const wxLongLongWx
& ll
) const;
441 wxLongLongWx
& operator|=(const wxLongLongWx
& ll
);
442 wxLongLongWx
operator^(const wxLongLongWx
& ll
) const;
443 wxLongLongWx
& operator^=(const wxLongLongWx
& ll
);
444 wxLongLongWx
operator~() const;
447 bool operator==(const wxLongLongWx
& ll
) const
448 { return m_lo
== ll
.m_lo
&& m_hi
== ll
.m_hi
; }
449 bool operator!=(const wxLongLongWx
& ll
) const
450 { return !(*this == ll
); }
451 bool operator<(const wxLongLongWx
& ll
) const;
452 bool operator>(const wxLongLongWx
& ll
) const;
453 bool operator<=(const wxLongLongWx
& ll
) const
454 { return *this < ll
|| *this == ll
; }
455 bool operator>=(const wxLongLongWx
& ll
) const
456 { return *this > ll
|| *this == ll
; }
458 bool operator<(long l
) const { return *this < wxLongLongWx(l
); }
459 bool operator>(long l
) const { return *this > wxLongLongWx(l
); }
460 bool operator==(long l
) const
462 return l
>= 0 ? (m_hi
== 0 && m_lo
== (unsigned long)l
)
463 : (m_hi
== -1 && m_lo
== (unsigned long)l
);
466 bool operator<=(long l
) const { return *this < l
|| *this == l
; }
467 bool operator>=(long l
) const { return *this > l
|| *this == l
; }
470 wxLongLongWx
operator*(const wxLongLongWx
& ll
) const;
471 wxLongLongWx
& operator*=(const wxLongLongWx
& ll
);
474 wxLongLongWx
operator/(const wxLongLongWx
& ll
) const;
475 wxLongLongWx
& operator/=(const wxLongLongWx
& ll
);
477 wxLongLongWx
operator%(const wxLongLongWx
& ll
) const;
479 void Divide(const wxLongLongWx
& divisor
,
480 wxLongLongWx
& quotient
,
481 wxLongLongWx
& remainder
) const;
484 #if wxUSE_STD_IOSTREAM
485 friend ostream
& operator<<(ostream
&, const wxLongLongWx
&);
486 #endif // wxUSE_STD_IOSTREAM
488 void *asArray() const;
491 // long is at least 32 bits, so represent our 64bit number as 2 longs
493 long m_hi
; // signed bit is in the high part
496 #ifdef wxLONGLONG_TEST_MODE
499 wxASSERT( (m_ll
>> 32) == m_hi
&& (unsigned long)m_ll
== m_lo
);
503 #endif // wxLONGLONG_TEST_MODE
506 #endif // wxUSE_LONGLONG_WX
508 // ----------------------------------------------------------------------------
510 // ----------------------------------------------------------------------------
512 inline bool WXDLLEXPORT
operator<(long l
, const wxLongLong
& ll
) { return ll
> l
; }
513 inline bool WXDLLEXPORT
operator>(long l
, const wxLongLong
& ll
) { return ll
> l
; }
514 inline bool WXDLLEXPORT
operator<=(long l
, const wxLongLong
& ll
) { return ll
> l
; }
515 inline bool WXDLLEXPORT
operator>=(long l
, const wxLongLong
& ll
) { return ll
> l
; }
516 inline bool WXDLLEXPORT
operator==(long l
, const wxLongLong
& ll
) { return ll
> l
; }
517 inline bool WXDLLEXPORT
operator!=(long l
, const wxLongLong
& ll
) { return ll
> l
; }
519 inline wxLongLong WXDLLEXPORT
operator+(long l
, const wxLongLong
& ll
) { return ll
+ l
; }
520 inline wxLongLong WXDLLEXPORT
operator-(long l
, const wxLongLong
& ll
) { return ll
- l
; }
522 #endif // _WX_LONGLONG_H