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(SIZEOF_LONG_LONG) && SIZEOF_LONG_LONG >= 8
55 #define wxLongLong_t long long
56 #elif (defined(__VISUALC__) && defined(__WIN32__)) || defined( __VMS__ )
57 #define wxLongLong_t __int64
58 #elif defined(__BORLANDC__) && defined(__WIN32__) && (__BORLANDC__ >= 0x520)
59 #define wxLongLong_t __int64
60 #elif defined(__MWERKS__)
61 #if __option(longlong)
62 #define wxLongLong_t long long
64 #error "The 64 bit integer support in CodeWarrior has been disabled."
65 #error "See the documentation on the 'longlong' pragma."
67 #elif defined(__VISAGECPP__) && __IBMCPP__ >= 400
68 #define wxLongLong_t long long
69 #else // no native long long type
70 // both warning and pragma warning are not portable, but at least an
71 // unknown pragma should never be an error - except that, actually, some
72 // broken compilers don't like it, so we have to disable it in this case
74 #if !(defined(__WATCOMC__) || defined(__VISAGECPP__))
75 #pragma warning "Your compiler does not appear to support 64 bit "\
76 "integers, using emulation class instead.\n" \
77 "Please report your compiler version to " \
78 "wx-dev@lists.wxwindows.org!"
81 #define wxUSE_LONGLONG_WX 1
84 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
85 // to disable automatic testing (useful for the test program which defines
86 // both classes) but by default we only use one class
87 #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
88 // don't use both classes unless wxUSE_LONGLONG_NATIVE was explicitly set:
89 // this is useful in test programs and only there
90 #ifndef wxUSE_LONGLONG_NATIVE
91 #define wxUSE_LONGLONG_NATIVE 0
94 class WXDLLEXPORT wxLongLongWx
;
95 #if defined(__VISUALC__) && !defined(__WIN32__)
96 #define wxLongLong wxLongLongWx
98 typedef wxLongLongWx wxLongLong
;
102 // if nothing is defined, use native implementation by default, of course
103 #ifndef wxUSE_LONGLONG_NATIVE
104 #define wxUSE_LONGLONG_NATIVE 1
108 #ifndef wxUSE_LONGLONG_WX
109 #define wxUSE_LONGLONG_WX 0
110 class WXDLLEXPORT wxLongLongNative
;
111 typedef wxLongLongNative wxLongLong
;
114 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
115 // typedef wxLongLong as it wants, we don't do it
117 // ----------------------------------------------------------------------------
118 // choose the appropriate class
119 // ----------------------------------------------------------------------------
121 // we use iostream for wxLongLong output
122 #include "wx/ioswrap.h"
124 #if wxUSE_LONGLONG_NATIVE
126 class WXDLLEXPORT wxLongLongNative
130 // default ctor initializes to 0
131 wxLongLongNative() { m_ll
= 0; }
133 wxLongLongNative(wxLongLong_t ll
) { m_ll
= ll
; }
135 wxLongLongNative(long hi
, unsigned long lo
)
137 // assign first to avoid precision loss!
138 m_ll
= ((wxLongLong_t
) hi
) << 32;
139 m_ll
|= (wxLongLong_t
) lo
;
142 // default copy ctor is ok
146 // assignment operators
147 // from native 64 bit integer
148 wxLongLongNative
& operator=(wxLongLong_t ll
)
149 { m_ll
= ll
; return *this; }
151 // from double: this one has an explicit name because otherwise we
152 // would have ambiguity with "ll = int" and also because we don't want
153 // to have implicit conversions between doubles and wxLongLongs
154 wxLongLongNative
& Assign(double d
)
155 { m_ll
= (wxLongLong_t
)d
; return *this; }
157 // assignment operators from wxLongLongNative is ok
162 { return (long)(m_ll
>> 32); }
164 unsigned long GetLo() const
165 { return (unsigned long)m_ll
; }
167 // get absolute value
168 wxLongLongNative
Abs() const { return wxLongLongNative(*this).Abs(); }
169 wxLongLongNative
& Abs() { if ( m_ll
< 0 ) m_ll
= -m_ll
; return *this; }
171 // convert to native long long
172 wxLongLong_t
GetValue() const { return m_ll
; }
174 // convert to long with range checking in the debug mode (only!)
177 wxASSERT_MSG( (m_ll
>= LONG_MIN
) && (m_ll
<= LONG_MAX
),
178 _T("wxLongLong to long conversion loss of precision") );
183 // don't provide implicit conversion to wxLongLong_t or we will have an
184 // ambiguity for all arithmetic operations
185 //operator wxLongLong_t() const { return m_ll; }
189 wxLongLongNative
operator+(const wxLongLongNative
& ll
) const
190 { return wxLongLongNative(m_ll
+ ll
.m_ll
); }
191 wxLongLongNative
& operator+=(const wxLongLongNative
& ll
)
192 { m_ll
+= ll
.m_ll
; return *this; }
194 wxLongLongNative
operator+(const wxLongLong_t ll
) const
195 { return wxLongLongNative(m_ll
+ ll
); }
196 wxLongLongNative
& operator+=(const wxLongLong_t ll
)
197 { m_ll
+= ll
; return *this; }
200 wxLongLongNative
& operator++()
201 { m_ll
++; return *this; }
204 wxLongLongNative
& operator++(int)
205 { m_ll
++; return *this; }
208 wxLongLongNative
operator-() const
209 { return wxLongLongNative(-m_ll
); }
210 wxLongLongNative
& Negate() { m_ll
= -m_ll
; return *this; }
213 wxLongLongNative
operator-(const wxLongLongNative
& ll
) const
214 { return wxLongLongNative(m_ll
- ll
.m_ll
); }
215 wxLongLongNative
& operator-=(const wxLongLongNative
& ll
)
216 { m_ll
-= ll
.m_ll
; return *this; }
218 wxLongLongNative
operator-(const wxLongLong_t ll
) const
219 { return wxLongLongNative(m_ll
- ll
); }
220 wxLongLongNative
& operator-=(const wxLongLong_t ll
)
221 { m_ll
-= ll
; return *this; }
224 wxLongLongNative
& operator--()
225 { m_ll
--; return *this; }
228 wxLongLongNative
& operator--(int)
229 { m_ll
--; return *this; }
233 wxLongLongNative
operator<<(int shift
) const
234 { return wxLongLongNative(m_ll
<< shift
);; }
235 wxLongLongNative
& operator<<=(int shift
)
236 { m_ll
<<= shift
; return *this; }
239 wxLongLongNative
operator>>(int shift
) const
240 { return wxLongLongNative(m_ll
>> shift
);; }
241 wxLongLongNative
& operator>>=(int shift
)
242 { m_ll
>>= shift
; return *this; }
245 wxLongLongNative
operator&(const wxLongLongNative
& ll
) const
246 { return wxLongLongNative(m_ll
& ll
.m_ll
); }
247 wxLongLongNative
& operator&=(const wxLongLongNative
& ll
)
248 { m_ll
&= ll
.m_ll
; return *this; }
250 wxLongLongNative
operator|(const wxLongLongNative
& ll
) const
251 { return wxLongLongNative(m_ll
| ll
.m_ll
); }
252 wxLongLongNative
& operator|=(const wxLongLongNative
& ll
)
253 { m_ll
|= ll
.m_ll
; return *this; }
255 wxLongLongNative
operator^(const wxLongLongNative
& ll
) const
256 { return wxLongLongNative(m_ll
^ ll
.m_ll
); }
257 wxLongLongNative
& operator^=(const wxLongLongNative
& ll
)
258 { m_ll
^= ll
.m_ll
; return *this; }
260 // multiplication/division
261 wxLongLongNative
operator*(const wxLongLongNative
& ll
) const
262 { return wxLongLongNative(m_ll
* ll
.m_ll
); }
263 wxLongLongNative
operator*(long l
) const
264 { return wxLongLongNative(m_ll
* l
); }
265 wxLongLongNative
& operator*=(const wxLongLongNative
& ll
)
266 { m_ll
*= ll
.m_ll
; return *this; }
267 wxLongLongNative
& operator*=(long l
)
268 { m_ll
*= l
; return *this; }
270 wxLongLongNative
operator/(const wxLongLongNative
& ll
) const
271 { return wxLongLongNative(m_ll
/ ll
.m_ll
); }
272 wxLongLongNative
operator/(long l
) const
273 { return wxLongLongNative(m_ll
/ l
); }
274 wxLongLongNative
& operator/=(const wxLongLongNative
& ll
)
275 { m_ll
/= ll
.m_ll
; return *this; }
276 wxLongLongNative
& operator/=(long l
)
277 { m_ll
/= l
; return *this; }
279 wxLongLongNative
operator%(const wxLongLongNative
& ll
) const
280 { return wxLongLongNative(m_ll
% ll
.m_ll
); }
281 wxLongLongNative
operator%(long l
) const
282 { return wxLongLongNative(m_ll
% l
); }
285 bool operator==(const wxLongLongNative
& ll
) const
286 { return m_ll
== ll
.m_ll
; }
287 bool operator==(long l
) const
288 { return m_ll
== l
; }
289 bool operator!=(const wxLongLongNative
& ll
) const
290 { return m_ll
!= ll
.m_ll
; }
291 bool operator!=(long l
) const
292 { return m_ll
!= l
; }
293 bool operator<(const wxLongLongNative
& ll
) const
294 { return m_ll
< ll
.m_ll
; }
295 bool operator<(long l
) const
297 bool operator>(const wxLongLongNative
& ll
) const
298 { return m_ll
> ll
.m_ll
; }
299 bool operator>(long l
) const
301 bool operator<=(const wxLongLongNative
& ll
) const
302 { return m_ll
<= ll
.m_ll
; }
303 bool operator<=(long l
) const
304 { return m_ll
<= l
; }
305 bool operator>=(const wxLongLongNative
& ll
) const
306 { return m_ll
>= ll
.m_ll
; }
307 bool operator>=(long l
) const
308 { return m_ll
>= l
; }
312 // return the string representation of this number
313 wxString
ToString() const;
315 // conversion to byte array: returns a pointer to static buffer!
316 void *asArray() const;
318 #if wxUSE_STD_IOSTREAM
320 friend wxSTD ostream
& operator<<(wxSTD ostream
&, const wxLongLongNative
&);
327 #endif // wxUSE_LONGLONG_NATIVE
329 #if wxUSE_LONGLONG_WX
331 class WXDLLEXPORT wxLongLongWx
335 // default ctor initializes to 0
340 #ifdef wxLONGLONG_TEST_MODE
344 #endif // wxLONGLONG_TEST_MODE
347 wxLongLongWx(long l
) { *this = l
; }
349 wxLongLongWx(long hi
, unsigned long lo
)
354 #ifdef wxLONGLONG_TEST_MODE
360 #endif // wxLONGLONG_TEST_MODE
363 // default copy ctor is ok in both cases
367 // assignment operators
369 wxLongLongWx
& operator=(long l
)
372 m_hi
= (l
< 0 ? -1l : 0l);
374 #ifdef wxLONGLONG_TEST_MODE
378 #endif // wxLONGLONG_TEST_MODE
383 wxLongLongWx
& Assign(double d
);
384 // can't have assignment operator from 2 longs
388 long GetHi() const { return m_hi
; }
390 unsigned long GetLo() const { return m_lo
; }
392 // get absolute value
393 wxLongLongWx
Abs() const { return wxLongLongWx(*this).Abs(); }
399 #ifdef wxLONGLONG_TEST_MODE
404 #endif // wxLONGLONG_TEST_MODE
409 // convert to long with range checking in the debug mode (only!)
412 wxASSERT_MSG( (m_hi
== 0l) || (m_hi
== -1l),
413 _T("wxLongLong to long conversion loss of precision") );
420 wxLongLongWx
operator+(const wxLongLongWx
& ll
) const;
421 wxLongLongWx
& operator+=(const wxLongLongWx
& ll
);
422 wxLongLongWx
operator+(long l
) const;
423 wxLongLongWx
& operator+=(long l
);
425 // pre increment operator
426 wxLongLongWx
& operator++();
428 // post increment operator
429 wxLongLongWx
& operator++(int) { return ++(*this); }
432 wxLongLongWx
operator-() const;
433 wxLongLongWx
& Negate();
436 wxLongLongWx
operator-(const wxLongLongWx
& ll
) const;
437 wxLongLongWx
& operator-=(const wxLongLongWx
& ll
);
439 // pre decrement operator
440 wxLongLongWx
& operator--();
442 // post decrement operator
443 wxLongLongWx
& operator--(int) { return --(*this); }
447 wxLongLongWx
operator<<(int shift
) const;
448 wxLongLongWx
& operator<<=(int shift
);
451 wxLongLongWx
operator>>(int shift
) const;
452 wxLongLongWx
& operator>>=(int shift
);
455 wxLongLongWx
operator&(const wxLongLongWx
& ll
) const;
456 wxLongLongWx
& operator&=(const wxLongLongWx
& ll
);
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;
464 bool operator==(const wxLongLongWx
& ll
) const
465 { return m_lo
== ll
.m_lo
&& m_hi
== ll
.m_hi
; }
466 bool operator!=(const wxLongLongWx
& ll
) const
467 { return !(*this == ll
); }
468 bool operator<(const wxLongLongWx
& ll
) const;
469 bool operator>(const wxLongLongWx
& ll
) const;
470 bool operator<=(const wxLongLongWx
& ll
) const
471 { return *this < ll
|| *this == ll
; }
472 bool operator>=(const wxLongLongWx
& ll
) const
473 { return *this > ll
|| *this == ll
; }
475 bool operator<(long l
) const { return *this < wxLongLongWx(l
); }
476 bool operator>(long l
) const { return *this > wxLongLongWx(l
); }
477 bool operator==(long l
) const
479 return l
>= 0 ? (m_hi
== 0 && m_lo
== (unsigned long)l
)
480 : (m_hi
== -1 && m_lo
== (unsigned long)l
);
483 bool operator<=(long l
) const { return *this < l
|| *this == l
; }
484 bool operator>=(long l
) const { return *this > l
|| *this == l
; }
487 wxLongLongWx
operator*(const wxLongLongWx
& ll
) const;
488 wxLongLongWx
& operator*=(const wxLongLongWx
& ll
);
491 wxLongLongWx
operator/(const wxLongLongWx
& ll
) const;
492 wxLongLongWx
& operator/=(const wxLongLongWx
& ll
);
494 wxLongLongWx
operator%(const wxLongLongWx
& ll
) const;
496 void Divide(const wxLongLongWx
& divisor
,
497 wxLongLongWx
& quotient
,
498 wxLongLongWx
& remainder
) const;
502 // return the string representation of this number
503 wxString
ToString() const;
505 void *asArray() const;
507 #if wxUSE_STD_IOSTREAM
508 friend wxSTD ostream
& operator<<(wxSTD ostream
&, const wxLongLongWx
&);
509 #endif // wxUSE_STD_IOSTREAM
512 // long is at least 32 bits, so represent our 64bit number as 2 longs
514 long m_hi
; // signed bit is in the high part
517 #ifdef wxLONGLONG_TEST_MODE
520 wxASSERT( (m_ll
>> 32) == m_hi
&& (unsigned long)m_ll
== m_lo
);
524 #endif // wxLONGLONG_TEST_MODE
527 #endif // wxUSE_LONGLONG_WX
529 // ----------------------------------------------------------------------------
531 // ----------------------------------------------------------------------------
533 inline bool operator<(long l
, const wxLongLong
& ll
) { return ll
> l
; }
534 inline bool operator>(long l
, const wxLongLong
& ll
) { return ll
> l
; }
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
; }
540 inline wxLongLong
operator+(long l
, const wxLongLong
& ll
) { return ll
+ l
; }
541 inline wxLongLong
operator-(long l
, const wxLongLong
& ll
) { return ll
- l
; }
543 #endif // _WX_LONGLONG_H