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/string.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
49 // first check for generic cases which are long on 64bit machine and "long
50 // long", then check for specific compilers
51 #if defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
52 #define wxLongLong_t long
53 #define wxLongLongIsLong
54 #elif (defined(__VISUALC__) && defined(__WIN32__)) || defined( __VMS__ )
55 #define wxLongLong_t __int64
56 #elif defined(__BORLANDC__) && defined(__WIN32__) && (__BORLANDC__ >= 0x520)
57 #define wxLongLong_t __int64
58 #elif defined(SIZEOF_LONG_LONG) && SIZEOF_LONG_LONG >= 8
59 #define wxLongLong_t long long
60 #elif defined(__MINGW32__)
61 #define wxLongLong_t long long
62 #elif defined(__MWERKS__)
63 #if __option(longlong)
64 #define wxLongLong_t long long
66 #error "The 64 bit integer support in CodeWarrior has been disabled."
67 #error "See the documentation on the 'longlong' pragma."
69 #elif defined(__VISAGECPP__) && __IBMCPP__ >= 400
70 #define wxLongLong_t long long
71 #else // no native long long type
72 // both warning and pragma warning are not portable, but at least an
73 // unknown pragma should never be an error - except that, actually, some
74 // broken compilers don't like it, so we have to disable it in this case
76 #if !(defined(__WATCOMC__) || defined(__VISAGECPP__))
77 #pragma warning "Your compiler does not appear to support 64 bit "\
78 "integers, using emulation class instead.\n" \
79 "Please report your compiler version to " \
80 "wx-dev@lists.wxwindows.org!"
83 #define wxUSE_LONGLONG_WX 1
86 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
87 // to disable automatic testing (useful for the test program which defines
88 // both classes) but by default we only use one class
89 #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
90 // don't use both classes unless wxUSE_LONGLONG_NATIVE was explicitly set:
91 // this is useful in test programs and only there
92 #ifndef wxUSE_LONGLONG_NATIVE
93 #define wxUSE_LONGLONG_NATIVE 0
96 class WXDLLEXPORT wxLongLongWx
;
97 #if defined(__VISUALC__) && !defined(__WIN32__)
98 #define wxLongLong wxLongLongWx
100 typedef wxLongLongWx wxLongLong
;
104 // if nothing is defined, use native implementation by default, of course
105 #ifndef wxUSE_LONGLONG_NATIVE
106 #define wxUSE_LONGLONG_NATIVE 1
110 #ifndef wxUSE_LONGLONG_WX
111 #define wxUSE_LONGLONG_WX 0
112 class WXDLLEXPORT wxLongLongNative
;
113 typedef wxLongLongNative wxLongLong
;
116 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
117 // typedef wxLongLong as it wants, we don't do it
119 // ----------------------------------------------------------------------------
120 // choose the appropriate class
121 // ----------------------------------------------------------------------------
123 // we use iostream for wxLongLong output
124 #include "wx/ioswrap.h"
126 #if wxUSE_LONGLONG_NATIVE
128 class WXDLLEXPORT wxLongLongNative
132 // default ctor initializes to 0
133 wxLongLongNative() { m_ll
= 0; }
135 wxLongLongNative(wxLongLong_t ll
) { m_ll
= ll
; }
137 wxLongLongNative(long hi
, unsigned long lo
)
139 // assign first to avoid precision loss!
140 m_ll
= ((wxLongLong_t
) hi
) << 32;
141 m_ll
|= (wxLongLong_t
) lo
;
144 // default copy ctor is ok
148 // assignment operators
149 // from native 64 bit integer
150 wxLongLongNative
& operator=(wxLongLong_t ll
)
151 { m_ll
= ll
; return *this; }
153 // from double: this one has an explicit name because otherwise we
154 // would have ambiguity with "ll = int" and also because we don't want
155 // to have implicit conversions between doubles and wxLongLongs
156 wxLongLongNative
& Assign(double d
)
157 { m_ll
= (wxLongLong_t
)d
; return *this; }
159 // assignment operators from wxLongLongNative is ok
164 { return (long)(m_ll
>> 32); }
166 unsigned long GetLo() const
167 { return (unsigned long)m_ll
; }
169 // get absolute value
170 wxLongLongNative
Abs() const { return wxLongLongNative(*this).Abs(); }
171 wxLongLongNative
& Abs() { if ( m_ll
< 0 ) m_ll
= -m_ll
; return *this; }
173 // convert to native long long
174 wxLongLong_t
GetValue() const { return m_ll
; }
176 // convert to long with range checking in the debug mode (only!)
179 wxASSERT_MSG( (m_ll
>= LONG_MIN
) && (m_ll
<= LONG_MAX
),
180 _T("wxLongLong to long conversion loss of precision") );
185 // don't provide implicit conversion to wxLongLong_t or we will have an
186 // ambiguity for all arithmetic operations
187 //operator wxLongLong_t() const { return m_ll; }
191 wxLongLongNative
operator+(const wxLongLongNative
& ll
) const
192 { return wxLongLongNative(m_ll
+ ll
.m_ll
); }
193 wxLongLongNative
& operator+=(const wxLongLongNative
& ll
)
194 { m_ll
+= ll
.m_ll
; return *this; }
196 wxLongLongNative
operator+(const wxLongLong_t ll
) const
197 { return wxLongLongNative(m_ll
+ ll
); }
198 wxLongLongNative
& operator+=(const wxLongLong_t ll
)
199 { m_ll
+= ll
; return *this; }
202 wxLongLongNative
& operator++()
203 { m_ll
++; return *this; }
206 wxLongLongNative
& operator++(int)
207 { m_ll
++; return *this; }
210 wxLongLongNative
operator-() const
211 { return wxLongLongNative(-m_ll
); }
212 wxLongLongNative
& Negate() { m_ll
= -m_ll
; return *this; }
215 wxLongLongNative
operator-(const wxLongLongNative
& ll
) const
216 { return wxLongLongNative(m_ll
- ll
.m_ll
); }
217 wxLongLongNative
& operator-=(const wxLongLongNative
& ll
)
218 { m_ll
-= ll
.m_ll
; return *this; }
220 wxLongLongNative
operator-(const wxLongLong_t ll
) const
221 { return wxLongLongNative(m_ll
- ll
); }
222 wxLongLongNative
& operator-=(const wxLongLong_t ll
)
223 { m_ll
-= ll
; return *this; }
226 wxLongLongNative
& operator--()
227 { m_ll
--; return *this; }
230 wxLongLongNative
& operator--(int)
231 { m_ll
--; return *this; }
235 wxLongLongNative
operator<<(int shift
) const
236 { return wxLongLongNative(m_ll
<< shift
);; }
237 wxLongLongNative
& operator<<=(int shift
)
238 { m_ll
<<= shift
; return *this; }
241 wxLongLongNative
operator>>(int shift
) const
242 { return wxLongLongNative(m_ll
>> shift
);; }
243 wxLongLongNative
& operator>>=(int shift
)
244 { m_ll
>>= shift
; return *this; }
247 wxLongLongNative
operator&(const wxLongLongNative
& ll
) const
248 { return wxLongLongNative(m_ll
& ll
.m_ll
); }
249 wxLongLongNative
& operator&=(const wxLongLongNative
& ll
)
250 { m_ll
&= ll
.m_ll
; return *this; }
252 wxLongLongNative
operator|(const wxLongLongNative
& ll
) const
253 { return wxLongLongNative(m_ll
| ll
.m_ll
); }
254 wxLongLongNative
& operator|=(const wxLongLongNative
& ll
)
255 { m_ll
|= ll
.m_ll
; return *this; }
257 wxLongLongNative
operator^(const wxLongLongNative
& ll
) const
258 { return wxLongLongNative(m_ll
^ ll
.m_ll
); }
259 wxLongLongNative
& operator^=(const wxLongLongNative
& ll
)
260 { m_ll
^= ll
.m_ll
; return *this; }
262 // multiplication/division
263 wxLongLongNative
operator*(const wxLongLongNative
& ll
) const
264 { return wxLongLongNative(m_ll
* ll
.m_ll
); }
265 wxLongLongNative
operator*(long l
) const
266 { return wxLongLongNative(m_ll
* l
); }
267 wxLongLongNative
& operator*=(const wxLongLongNative
& ll
)
268 { m_ll
*= ll
.m_ll
; return *this; }
269 wxLongLongNative
& operator*=(long l
)
270 { m_ll
*= l
; return *this; }
272 wxLongLongNative
operator/(const wxLongLongNative
& ll
) const
273 { return wxLongLongNative(m_ll
/ ll
.m_ll
); }
274 wxLongLongNative
operator/(long l
) const
275 { return wxLongLongNative(m_ll
/ l
); }
276 wxLongLongNative
& operator/=(const wxLongLongNative
& ll
)
277 { m_ll
/= ll
.m_ll
; return *this; }
278 wxLongLongNative
& operator/=(long l
)
279 { m_ll
/= l
; return *this; }
281 wxLongLongNative
operator%(const wxLongLongNative
& ll
) const
282 { return wxLongLongNative(m_ll
% ll
.m_ll
); }
283 wxLongLongNative
operator%(long l
) const
284 { return wxLongLongNative(m_ll
% l
); }
287 bool operator==(const wxLongLongNative
& ll
) const
288 { return m_ll
== ll
.m_ll
; }
289 bool operator==(long l
) const
290 { return m_ll
== l
; }
291 bool operator!=(const wxLongLongNative
& ll
) const
292 { return m_ll
!= ll
.m_ll
; }
293 bool operator!=(long l
) const
294 { return m_ll
!= l
; }
295 bool operator<(const wxLongLongNative
& ll
) const
296 { return m_ll
< ll
.m_ll
; }
297 bool operator<(long l
) const
299 bool operator>(const wxLongLongNative
& ll
) const
300 { return m_ll
> ll
.m_ll
; }
301 bool operator>(long l
) const
303 bool operator<=(const wxLongLongNative
& ll
) const
304 { return m_ll
<= ll
.m_ll
; }
305 bool operator<=(long l
) const
306 { return m_ll
<= l
; }
307 bool operator>=(const wxLongLongNative
& ll
) const
308 { return m_ll
>= ll
.m_ll
; }
309 bool operator>=(long l
) const
310 { return m_ll
>= l
; }
314 // return the string representation of this number
315 wxString
ToString() const;
317 // conversion to byte array: returns a pointer to static buffer!
318 void *asArray() const;
320 #if wxUSE_STD_IOSTREAM
322 friend wxSTD ostream
& operator<<(wxSTD ostream
&, const wxLongLongNative
&);
329 #endif // wxUSE_LONGLONG_NATIVE
331 #if wxUSE_LONGLONG_WX
333 class WXDLLEXPORT wxLongLongWx
337 // default ctor initializes to 0
342 #ifdef wxLONGLONG_TEST_MODE
346 #endif // wxLONGLONG_TEST_MODE
349 wxLongLongWx(long l
) { *this = l
; }
351 wxLongLongWx(long hi
, unsigned long lo
)
356 #ifdef wxLONGLONG_TEST_MODE
362 #endif // wxLONGLONG_TEST_MODE
365 // default copy ctor is ok in both cases
369 // assignment operators
371 wxLongLongWx
& operator=(long l
)
374 m_hi
= (l
< 0 ? -1l : 0l);
376 #ifdef wxLONGLONG_TEST_MODE
380 #endif // wxLONGLONG_TEST_MODE
385 wxLongLongWx
& Assign(double d
);
386 // can't have assignment operator from 2 longs
390 long GetHi() const { return m_hi
; }
392 unsigned long GetLo() const { return m_lo
; }
394 // get absolute value
395 wxLongLongWx
Abs() const { return wxLongLongWx(*this).Abs(); }
401 #ifdef wxLONGLONG_TEST_MODE
406 #endif // wxLONGLONG_TEST_MODE
411 // convert to long with range checking in the debug mode (only!)
414 wxASSERT_MSG( (m_hi
== 0l) || (m_hi
== -1l),
415 _T("wxLongLong to long conversion loss of precision") );
422 wxLongLongWx
operator+(const wxLongLongWx
& ll
) const;
423 wxLongLongWx
& operator+=(const wxLongLongWx
& ll
);
424 wxLongLongWx
operator+(long l
) const;
425 wxLongLongWx
& operator+=(long l
);
427 // pre increment operator
428 wxLongLongWx
& operator++();
430 // post increment operator
431 wxLongLongWx
& operator++(int) { return ++(*this); }
434 wxLongLongWx
operator-() const;
435 wxLongLongWx
& Negate();
438 wxLongLongWx
operator-(const wxLongLongWx
& ll
) const;
439 wxLongLongWx
& operator-=(const wxLongLongWx
& ll
);
441 // pre decrement operator
442 wxLongLongWx
& operator--();
444 // post decrement operator
445 wxLongLongWx
& operator--(int) { return --(*this); }
449 wxLongLongWx
operator<<(int shift
) const;
450 wxLongLongWx
& operator<<=(int shift
);
453 wxLongLongWx
operator>>(int shift
) const;
454 wxLongLongWx
& operator>>=(int shift
);
457 wxLongLongWx
operator&(const wxLongLongWx
& ll
) const;
458 wxLongLongWx
& operator&=(const wxLongLongWx
& ll
);
459 wxLongLongWx
operator|(const wxLongLongWx
& ll
) const;
460 wxLongLongWx
& operator|=(const wxLongLongWx
& ll
);
461 wxLongLongWx
operator^(const wxLongLongWx
& ll
) const;
462 wxLongLongWx
& operator^=(const wxLongLongWx
& ll
);
463 wxLongLongWx
operator~() const;
466 bool operator==(const wxLongLongWx
& ll
) const
467 { return m_lo
== ll
.m_lo
&& m_hi
== ll
.m_hi
; }
468 bool operator!=(const wxLongLongWx
& ll
) const
469 { return !(*this == ll
); }
470 bool operator<(const wxLongLongWx
& ll
) const;
471 bool operator>(const wxLongLongWx
& ll
) const;
472 bool operator<=(const wxLongLongWx
& ll
) const
473 { return *this < ll
|| *this == ll
; }
474 bool operator>=(const wxLongLongWx
& ll
) const
475 { return *this > ll
|| *this == ll
; }
477 bool operator<(long l
) const { return *this < wxLongLongWx(l
); }
478 bool operator>(long l
) const { return *this > wxLongLongWx(l
); }
479 bool operator==(long l
) const
481 return l
>= 0 ? (m_hi
== 0 && m_lo
== (unsigned long)l
)
482 : (m_hi
== -1 && m_lo
== (unsigned long)l
);
485 bool operator<=(long l
) const { return *this < l
|| *this == l
; }
486 bool operator>=(long l
) const { return *this > l
|| *this == l
; }
489 wxLongLongWx
operator*(const wxLongLongWx
& ll
) const;
490 wxLongLongWx
& operator*=(const wxLongLongWx
& ll
);
493 wxLongLongWx
operator/(const wxLongLongWx
& ll
) const;
494 wxLongLongWx
& operator/=(const wxLongLongWx
& ll
);
496 wxLongLongWx
operator%(const wxLongLongWx
& ll
) const;
498 void Divide(const wxLongLongWx
& divisor
,
499 wxLongLongWx
& quotient
,
500 wxLongLongWx
& remainder
) const;
504 // return the string representation of this number
505 wxString
ToString() const;
507 void *asArray() const;
509 #if wxUSE_STD_IOSTREAM
510 friend wxSTD ostream
& operator<<(wxSTD ostream
&, const wxLongLongWx
&);
511 #endif // wxUSE_STD_IOSTREAM
514 // long is at least 32 bits, so represent our 64bit number as 2 longs
516 long m_hi
; // signed bit is in the high part
519 #ifdef wxLONGLONG_TEST_MODE
522 wxASSERT( (m_ll
>> 32) == m_hi
&& (unsigned long)m_ll
== m_lo
);
526 #endif // wxLONGLONG_TEST_MODE
529 #endif // wxUSE_LONGLONG_WX
531 // ----------------------------------------------------------------------------
533 // ----------------------------------------------------------------------------
535 inline bool operator<(long l
, const wxLongLong
& ll
) { return ll
> l
; }
536 inline bool operator>(long l
, const wxLongLong
& ll
) { return ll
> l
; }
537 inline bool operator<=(long l
, const wxLongLong
& ll
) { return ll
> l
; }
538 inline bool operator>=(long l
, const wxLongLong
& ll
) { return ll
> l
; }
539 inline bool operator==(long l
, const wxLongLong
& ll
) { return ll
> l
; }
540 inline bool operator!=(long l
, const wxLongLong
& ll
) { return ll
> l
; }
542 inline wxLongLong
operator+(long l
, const wxLongLong
& ll
) { return ll
+ l
; }
543 inline wxLongLong
operator-(long l
, const wxLongLong
& ll
) { return ll
- l
; }
545 #endif // _WX_LONGLONG_H