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.
69 // Err, actually, Watcom C++ doesn't like it.
70 // (well, if the compilers are _that_ broken, I'm removing it (VZ))
71 #if 0 //ndef __WATCOMC__
72 #pragma warning "Your compiler does not appear to support 64 bit "\
73 "integers, using emulation class instead."
75 #define wxUSE_LONGLONG_WX 1
78 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
79 // to disable automatic testing (useful for the test program which defines
80 // both classes) but by default we only use one class
81 #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
82 // don't use both classes unless wxUSE_LONGLONG_NATIVE was explicitly set:
83 // this is useful in test programs nad only there
84 #ifndef wxUSE_LONGLONG_NATIVE
85 #define wxUSE_LONGLONG_NATIVE 0
88 class WXDLLEXPORT wxLongLongWx
;
89 typedef wxLongLongWx wxLongLong
;
91 // if nothing is defined, use native implementation by default, of course
92 #ifndef wxUSE_LONGLONG_NATIVE
93 #define wxUSE_LONGLONG_NATIVE 1
97 #ifndef wxUSE_LONGLONG_WX
98 #define wxUSE_LONGLONG_WX 0
99 class WXDLLEXPORT wxLongLongNative
;
100 typedef wxLongLongNative wxLongLong
;
103 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
104 // typedef wxLongLong as it wants, we don't do it
106 // ----------------------------------------------------------------------------
107 // choose the appropriate class
108 // ----------------------------------------------------------------------------
110 // we use iostream for wxLongLong output
111 #include "wx/ioswrap.h"
113 #if wxUSE_LONGLONG_NATIVE
115 class WXDLLEXPORT wxLongLongNative
119 // default ctor initializes to 0
120 wxLongLongNative() { m_ll
= 0; }
122 wxLongLongNative(wxLongLong_t ll
) { m_ll
= ll
; }
124 wxLongLongNative(long hi
, unsigned long lo
)
126 // assign first to avoid precision loss!
127 m_ll
= ((wxLongLong_t
) hi
) << 32;
128 m_ll
|= (wxLongLong_t
) lo
;
131 // default copy ctor is ok
135 // assignment operators
136 // from native 64 bit integer
137 wxLongLongNative
& operator=(wxLongLong_t ll
)
138 { m_ll
= ll
; return *this; }
140 // from double: this one has an explicit name because otherwise we
141 // would have ambiguity with "ll = int" and also because we don't want
142 // to have implicit conversions between doubles and wxLongLongs
143 wxLongLongNative
& Assign(double d
)
144 { m_ll
= (wxLongLong_t
)d
; return *this; }
146 // assignment operators from wxLongLongNative is ok
151 { return (long)(m_ll
>> 32); }
153 unsigned long GetLo() const
154 { return (unsigned long)m_ll
; }
156 // get absolute value
157 wxLongLongNative
Abs() const { return wxLongLongNative(*this).Abs(); }
158 wxLongLongNative
& Abs() { if ( m_ll
< 0 ) m_ll
= -m_ll
; return *this; }
160 // convert to native long long
161 wxLongLong_t
GetValue() const { return m_ll
; }
163 // convert to long with range checking in the debug mode (only!)
166 wxASSERT_MSG( (m_ll
>= LONG_MIN
) && (m_ll
<= LONG_MAX
),
167 _T("wxLongLong to long conversion loss of precision") );
172 // don't provide implicit conversion to wxLongLong_t or we will have an
173 // ambiguity for all arithmetic operations
174 //operator wxLongLong_t() const { return m_ll; }
178 wxLongLongNative
operator+(const wxLongLongNative
& ll
) const
179 { return wxLongLongNative(m_ll
+ ll
.m_ll
); }
180 wxLongLongNative
& operator+=(const wxLongLongNative
& ll
)
181 { m_ll
+= ll
.m_ll
; return *this; }
183 wxLongLongNative
operator+(const wxLongLong_t ll
) const
184 { return wxLongLongNative(m_ll
+ ll
); }
185 wxLongLongNative
& operator+=(const wxLongLong_t ll
)
186 { m_ll
+= ll
; return *this; }
189 wxLongLongNative
& operator++()
190 { m_ll
++; return *this; }
193 wxLongLongNative
& operator++(int)
194 { m_ll
++; return *this; }
197 wxLongLongNative
operator-() const
198 { return wxLongLongNative(-m_ll
); }
201 wxLongLongNative
operator-(const wxLongLongNative
& ll
) const
202 { return wxLongLongNative(m_ll
- ll
.m_ll
); }
203 wxLongLongNative
& operator-=(const wxLongLongNative
& ll
)
204 { m_ll
-= ll
.m_ll
; return *this; }
206 wxLongLongNative
operator-(const wxLongLong_t ll
) const
207 { return wxLongLongNative(m_ll
- ll
); }
208 wxLongLongNative
& operator-=(const wxLongLong_t ll
)
209 { m_ll
-= ll
; return *this; }
212 wxLongLongNative
& operator--()
213 { m_ll
--; return *this; }
216 wxLongLongNative
& operator--(int)
217 { m_ll
--; return *this; }
221 wxLongLongNative
operator<<(int shift
) const
222 { return wxLongLongNative(m_ll
<< shift
);; }
223 wxLongLongNative
& operator<<=(int shift
)
224 { m_ll
<<= shift
; return *this; }
227 wxLongLongNative
operator>>(int shift
) const
228 { return wxLongLongNative(m_ll
>> shift
);; }
229 wxLongLongNative
& operator>>=(int shift
)
230 { m_ll
>>= shift
; return *this; }
233 wxLongLongNative
operator&(const wxLongLongNative
& ll
) const
234 { return wxLongLongNative(m_ll
& ll
.m_ll
); }
235 wxLongLongNative
& operator&=(const wxLongLongNative
& ll
)
236 { m_ll
&= ll
.m_ll
; return *this; }
238 wxLongLongNative
operator|(const wxLongLongNative
& ll
) const
239 { return wxLongLongNative(m_ll
| ll
.m_ll
); }
240 wxLongLongNative
& operator|=(const wxLongLongNative
& ll
)
241 { m_ll
|= ll
.m_ll
; return *this; }
243 wxLongLongNative
operator^(const wxLongLongNative
& ll
) const
244 { return wxLongLongNative(m_ll
^ ll
.m_ll
); }
245 wxLongLongNative
& operator^=(const wxLongLongNative
& ll
)
246 { m_ll
^= ll
.m_ll
; return *this; }
248 // multiplication/division
249 wxLongLongNative
operator*(const wxLongLongNative
& ll
) const
250 { return wxLongLongNative(m_ll
* ll
.m_ll
); }
251 wxLongLongNative
operator*(long l
) const
252 { return wxLongLongNative(m_ll
* l
); }
253 wxLongLongNative
& operator*=(const wxLongLongNative
& ll
)
254 { m_ll
*= ll
.m_ll
; return *this; }
255 wxLongLongNative
& operator*=(long l
)
256 { m_ll
*= l
; return *this; }
258 wxLongLongNative
operator/(const wxLongLongNative
& ll
) const
259 { return wxLongLongNative(m_ll
/ ll
.m_ll
); }
260 wxLongLongNative
operator/(long l
) const
261 { return wxLongLongNative(m_ll
/ l
); }
262 wxLongLongNative
& operator/=(const wxLongLongNative
& ll
)
263 { m_ll
/= ll
.m_ll
; return *this; }
264 wxLongLongNative
& operator/=(long l
)
265 { m_ll
/= l
; return *this; }
267 wxLongLongNative
operator%(const wxLongLongNative
& ll
) const
268 { return wxLongLongNative(m_ll
% ll
.m_ll
); }
269 wxLongLongNative
operator%(long l
) const
270 { return wxLongLongNative(m_ll
% l
); }
273 bool operator==(const wxLongLongNative
& ll
) const
274 { return m_ll
== ll
.m_ll
; }
275 bool operator==(long l
) const
276 { return m_ll
== l
; }
277 bool operator!=(const wxLongLongNative
& ll
) const
278 { return m_ll
!= ll
.m_ll
; }
279 bool operator!=(long l
) const
280 { return m_ll
!= l
; }
281 bool operator<(const wxLongLongNative
& ll
) const
282 { return m_ll
< ll
.m_ll
; }
283 bool operator<(long l
) const
285 bool operator>(const wxLongLongNative
& ll
) const
286 { return m_ll
> ll
.m_ll
; }
287 bool operator>(long l
) const
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
296 { return m_ll
>= l
; }
299 // conversion to byte array: returns a pointer to static buffer!
300 void *asArray() const;
302 #if wxUSE_STD_IOSTREAM
304 friend ostream
& operator<<(ostream
&, const wxLongLongNative
&);
311 #endif // wxUSE_LONGLONG_NATIVE
313 #if wxUSE_LONGLONG_WX
315 class WXDLLEXPORT wxLongLongWx
319 // default ctor initializes to 0
324 #ifdef wxLONGLONG_TEST_MODE
328 #endif // wxLONGLONG_TEST_MODE
331 wxLongLongWx(long l
) { *this = l
; }
333 wxLongLongWx(long hi
, unsigned long lo
)
338 #ifdef wxLONGLONG_TEST_MODE
344 #endif // wxLONGLONG_TEST_MODE
347 // default copy ctor is ok in both cases
351 // assignment operators
353 wxLongLongWx
& operator=(long l
)
356 m_hi
= (l
< 0 ? -1l : 0l);
358 #ifdef wxLONGLONG_TEST_MODE
362 #endif // wxLONGLONG_TEST_MODE
367 wxLongLongWx
& Assign(double d
);
368 // can't have assignment operator from 2 longs
372 long GetHi() const { return m_hi
; }
374 unsigned long GetLo() const { return m_lo
; }
376 // get absolute value
377 wxLongLongWx
Abs() const { return wxLongLongWx(*this).Abs(); }
383 #ifdef wxLONGLONG_TEST_MODE
388 #endif // wxLONGLONG_TEST_MODE
393 // convert to long with range checking in the debug mode (only!)
396 wxASSERT_MSG( (m_hi
== 0l) || (m_hi
== -1l),
397 _T("wxLongLong to long conversion loss of precision") );
404 wxLongLongWx
operator+(const wxLongLongWx
& ll
) const;
405 wxLongLongWx
& operator+=(const wxLongLongWx
& ll
);
406 wxLongLongWx
operator+(long l
) const;
407 wxLongLongWx
& operator+=(long l
);
409 // pre increment operator
410 wxLongLongWx
& operator++();
412 // post increment operator
413 wxLongLongWx
& operator++(int) { return ++(*this); }
416 wxLongLongWx
operator-() const;
417 wxLongLongWx
& Negate();
420 wxLongLongWx
operator-(const wxLongLongWx
& ll
) const;
421 wxLongLongWx
& operator-=(const wxLongLongWx
& ll
);
423 // pre decrement operator
424 wxLongLongWx
& operator--();
426 // post decrement operator
427 wxLongLongWx
& operator--(int) { return --(*this); }
431 wxLongLongWx
operator<<(int shift
) const;
432 wxLongLongWx
& operator<<=(int shift
);
435 wxLongLongWx
operator>>(int shift
) const;
436 wxLongLongWx
& operator>>=(int shift
);
439 wxLongLongWx
operator&(const wxLongLongWx
& ll
) const;
440 wxLongLongWx
& operator&=(const wxLongLongWx
& ll
);
441 wxLongLongWx
operator|(const wxLongLongWx
& ll
) const;
442 wxLongLongWx
& operator|=(const wxLongLongWx
& ll
);
443 wxLongLongWx
operator^(const wxLongLongWx
& ll
) const;
444 wxLongLongWx
& operator^=(const wxLongLongWx
& ll
);
445 wxLongLongWx
operator~() const;
448 bool operator==(const wxLongLongWx
& ll
) const
449 { return m_lo
== ll
.m_lo
&& m_hi
== ll
.m_hi
; }
450 bool operator!=(const wxLongLongWx
& ll
) const
451 { return !(*this == ll
); }
452 bool operator<(const wxLongLongWx
& ll
) const;
453 bool operator>(const wxLongLongWx
& ll
) const;
454 bool operator<=(const wxLongLongWx
& ll
) const
455 { return *this < ll
|| *this == ll
; }
456 bool operator>=(const wxLongLongWx
& ll
) const
457 { return *this > ll
|| *this == ll
; }
459 bool operator<(long l
) const { return *this < wxLongLongWx(l
); }
460 bool operator>(long l
) const { return *this > wxLongLongWx(l
); }
461 bool operator==(long l
) const
463 return l
>= 0 ? (m_hi
== 0 && m_lo
== (unsigned long)l
)
464 : (m_hi
== -1 && m_lo
== (unsigned long)l
);
467 bool operator<=(long l
) const { return *this < l
|| *this == l
; }
468 bool operator>=(long l
) const { return *this > l
|| *this == l
; }
471 wxLongLongWx
operator*(const wxLongLongWx
& ll
) const;
472 wxLongLongWx
& operator*=(const wxLongLongWx
& ll
);
475 wxLongLongWx
operator/(const wxLongLongWx
& ll
) const;
476 wxLongLongWx
& operator/=(const wxLongLongWx
& ll
);
478 wxLongLongWx
operator%(const wxLongLongWx
& ll
) const;
480 void Divide(const wxLongLongWx
& divisor
,
481 wxLongLongWx
& quotient
,
482 wxLongLongWx
& remainder
) const;
485 #if wxUSE_STD_IOSTREAM
486 friend ostream
& operator<<(ostream
&, const wxLongLongWx
&);
487 #endif // wxUSE_STD_IOSTREAM
489 void *asArray() const;
492 // long is at least 32 bits, so represent our 64bit number as 2 longs
494 long m_hi
; // signed bit is in the high part
497 #ifdef wxLONGLONG_TEST_MODE
500 wxASSERT( (m_ll
>> 32) == m_hi
&& (unsigned long)m_ll
== m_lo
);
504 #endif // wxLONGLONG_TEST_MODE
507 #endif // wxUSE_LONGLONG_WX
509 // ----------------------------------------------------------------------------
511 // ----------------------------------------------------------------------------
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
; }
518 inline bool WXDLLEXPORT
operator!=(long l
, const wxLongLong
& ll
) { return ll
> l
; }
520 inline wxLongLong WXDLLEXPORT
operator+(long l
, const wxLongLong
& ll
) { return ll
+ l
; }
521 inline wxLongLong WXDLLEXPORT
operator-(long l
, const wxLongLong
& ll
) { return ll
- l
; }
523 #endif // _WX_LONGLONG_H