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
6 // Remarks: this class is not public in wxWindows 2.0! It is intentionally
7 // not documented and is for private use only.
11 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
12 // Licence: wxWindows license
13 /////////////////////////////////////////////////////////////////////////////
15 #ifndef _WX_LONGLONG_H
16 #define _WX_LONGLONG_H
19 #pragma interface "longlong.h"
23 #include "wx/wxchar.h"
26 #include <limits.h> // for LONG_MAX
28 // define this to compile wxLongLongWx in "test" mode: the results of all
29 // calculations will be compared with the real results taken from
30 // wxLongLongNative -- this is extremely useful to find the bugs in
31 // wxLongLongWx class!
33 //#define wxLONGLONG_TEST_MODE
35 #ifdef wxLONGLONG_TEST_MODE
36 #define wxUSE_LONGLONG_WX 1
37 #define wxUSE_LONGLONG_NATIVE 1
38 #endif // wxLONGLONG_TEST_MODE
40 // ----------------------------------------------------------------------------
41 // decide upon which class we will use
42 // ----------------------------------------------------------------------------
44 // to avoid compilation problems on 64bit machines with ambiguous method calls
45 // we will need to define this
46 #undef wxLongLongIsLong
48 // NB: we #define and not typedef wxLongLong_t because we want to be able to
49 // use 'unsigned wxLongLong_t' as well and because we use "#ifdef
50 // wxLongLong_t" below
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__) && (__BORLANDC__ >= 0x530)
57 #define wxLongLong_t __int64
58 #elif defined(__GNUG__)
59 #define wxLongLong_t long long
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.
72 // Err, actually, Watcom C++ doesn't like it.
73 // (well, if the compilers are _that_ broken, I'm removing it (VZ))
74 #if 0 //ndef __WATCOMC__
75 #pragma warning "Your compiler does not appear to support 64 bit "\
76 "integers, using emulation class instead."
79 #define wxUSE_LONGLONG_WX 1
82 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
83 // to disable automatic testing (useful for the test program which defines
84 // both classes) but by default we only use one class
85 #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
86 // don't use both classes unless wxUSE_LONGLONG_NATIVE was explicitly set:
87 // this is useful in test programs nad only there
88 #ifndef wxUSE_LONGLONG_NATIVE
89 #define wxUSE_LONGLONG_NATIVE 0
92 class WXDLLEXPORT wxLongLongWx
;
93 typedef wxLongLongWx wxLongLong
;
95 // if nothing is defined, use native implementation by default, of course
96 #ifndef wxUSE_LONGLONG_NATIVE
97 #define wxUSE_LONGLONG_NATIVE 1
101 #ifndef wxUSE_LONGLONG_WX
102 #define wxUSE_LONGLONG_WX 0
103 class WXDLLEXPORT wxLongLongNative
;
104 typedef wxLongLongNative wxLongLong
;
107 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
108 // typedef wxLongLong as it wants, we don't do it
110 // ----------------------------------------------------------------------------
111 // choose the appropriate class
112 // ----------------------------------------------------------------------------
114 // we use iostream for wxLongLong output
115 #include "wx/ioswrap.h"
117 #if wxUSE_LONGLONG_NATIVE
119 class WXDLLEXPORT wxLongLongNative
123 // default ctor initializes to 0
124 wxLongLongNative() { m_ll
= 0; }
126 wxLongLongNative(wxLongLong_t ll
) { m_ll
= ll
; }
128 wxLongLongNative(long hi
, unsigned long lo
)
130 // assign first to avoid precision loss!
131 m_ll
= ((wxLongLong_t
) hi
) << 32;
132 m_ll
|= (wxLongLong_t
) lo
;
135 // default copy ctor is ok
139 // assignment operators
140 // from native 64 bit integer
141 wxLongLongNative
& operator=(wxLongLong_t ll
)
142 { m_ll
= ll
; return *this; }
144 // from double: this one has an explicit name because otherwise we
145 // would have ambiguity with "ll = int" and also because we don't want
146 // to have implicit conversions between doubles and wxLongLongs
147 wxLongLongNative
& Assign(double d
)
148 { m_ll
= (wxLongLong_t
)d
; return *this; }
150 // assignment operators from wxLongLongNative is ok
155 { return (long)(m_ll
>> 32); }
157 unsigned long GetLo() const
158 { return (unsigned long)m_ll
; }
160 // get absolute value
161 wxLongLongNative
Abs() const { return wxLongLongNative(*this).Abs(); }
162 wxLongLongNative
& Abs() { if ( m_ll
< 0 ) m_ll
= -m_ll
; return *this; }
164 // convert to native long long
165 wxLongLong_t
GetValue() const { return m_ll
; }
167 // convert to long with range checking in the debug mode (only!)
170 wxASSERT_MSG( (m_ll
>= LONG_MIN
) && (m_ll
<= LONG_MAX
),
171 _T("wxLongLong to long conversion loss of precision") );
176 // don't provide implicit conversion to wxLongLong_t or we will have an
177 // ambiguity for all arithmetic operations
178 //operator wxLongLong_t() const { return m_ll; }
182 wxLongLongNative
operator+(const wxLongLongNative
& ll
) const
183 { return wxLongLongNative(m_ll
+ ll
.m_ll
); }
184 wxLongLongNative
& operator+=(const wxLongLongNative
& ll
)
185 { m_ll
+= ll
.m_ll
; return *this; }
187 wxLongLongNative
operator+(const wxLongLong_t ll
) const
188 { return wxLongLongNative(m_ll
+ ll
); }
189 wxLongLongNative
& operator+=(const wxLongLong_t ll
)
190 { m_ll
+= ll
; return *this; }
193 wxLongLongNative
& operator++()
194 { m_ll
++; return *this; }
197 wxLongLongNative
& operator++(int)
198 { m_ll
++; return *this; }
201 wxLongLongNative
operator-() const
202 { return wxLongLongNative(-m_ll
); }
205 wxLongLongNative
operator-(const wxLongLongNative
& ll
) const
206 { return wxLongLongNative(m_ll
- ll
.m_ll
); }
207 wxLongLongNative
& operator-=(const wxLongLongNative
& ll
)
208 { m_ll
-= ll
.m_ll
; return *this; }
210 wxLongLongNative
operator-(const wxLongLong_t ll
) const
211 { return wxLongLongNative(m_ll
- ll
); }
212 wxLongLongNative
& operator-=(const wxLongLong_t ll
)
213 { m_ll
-= ll
; return *this; }
216 wxLongLongNative
& operator--()
217 { m_ll
--; return *this; }
220 wxLongLongNative
& operator--(int)
221 { m_ll
--; return *this; }
225 wxLongLongNative
operator<<(int shift
) const
226 { return wxLongLongNative(m_ll
<< shift
);; }
227 wxLongLongNative
& operator<<=(int shift
)
228 { m_ll
<<= shift
; return *this; }
231 wxLongLongNative
operator>>(int shift
) const
232 { return wxLongLongNative(m_ll
>> shift
);; }
233 wxLongLongNative
& operator>>=(int shift
)
234 { m_ll
>>= shift
; 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 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 // multiplication/division
253 wxLongLongNative
operator*(const wxLongLongNative
& ll
) const
254 { return wxLongLongNative(m_ll
* ll
.m_ll
); }
255 wxLongLongNative
operator*(long l
) const
256 { return wxLongLongNative(m_ll
* l
); }
257 wxLongLongNative
& operator*=(const wxLongLongNative
& ll
)
258 { m_ll
*= ll
.m_ll
; return *this; }
259 wxLongLongNative
& operator*=(long l
)
260 { m_ll
*= l
; return *this; }
262 wxLongLongNative
operator/(const wxLongLongNative
& ll
) const
263 { return wxLongLongNative(m_ll
/ ll
.m_ll
); }
264 wxLongLongNative
operator/(long l
) const
265 { return wxLongLongNative(m_ll
/ l
); }
266 wxLongLongNative
& operator/=(const wxLongLongNative
& ll
)
267 { m_ll
/= ll
.m_ll
; return *this; }
268 wxLongLongNative
& operator/=(long l
)
269 { m_ll
/= l
; return *this; }
271 wxLongLongNative
operator%(const wxLongLongNative
& ll
) const
272 { return wxLongLongNative(m_ll
% ll
.m_ll
); }
273 wxLongLongNative
operator%(long l
) const
274 { return wxLongLongNative(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
284 { return m_ll
!= l
; }
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
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
; }
297 bool operator>=(const wxLongLongNative
& ll
) const
298 { return m_ll
>= ll
.m_ll
; }
299 bool operator>=(long l
) const
300 { return m_ll
>= l
; }
303 // conversion to byte array: returns a pointer to static buffer!
304 void *asArray() const;
306 #if wxUSE_STD_IOSTREAM
308 friend ostream
& operator<<(ostream
&, const wxLongLongNative
&);
315 #endif // wxUSE_LONGLONG_NATIVE
317 #if wxUSE_LONGLONG_WX
319 class WXDLLEXPORT wxLongLongWx
323 // default ctor initializes to 0
328 #ifdef wxLONGLONG_TEST_MODE
332 #endif // wxLONGLONG_TEST_MODE
335 wxLongLongWx(long l
) { *this = l
; }
337 wxLongLongWx(long hi
, unsigned long lo
)
342 #ifdef wxLONGLONG_TEST_MODE
348 #endif // wxLONGLONG_TEST_MODE
351 // default copy ctor is ok in both cases
355 // assignment operators
357 wxLongLongWx
& operator=(long l
)
360 m_hi
= (l
< 0 ? -1l : 0l);
362 #ifdef wxLONGLONG_TEST_MODE
366 #endif // wxLONGLONG_TEST_MODE
371 wxLongLongWx
& Assign(double d
);
372 // can't have assignment operator from 2 longs
376 long GetHi() const { return m_hi
; }
378 unsigned long GetLo() const { return m_lo
; }
380 // get absolute value
381 wxLongLongWx
Abs() const { return wxLongLongWx(*this).Abs(); }
387 #ifdef wxLONGLONG_TEST_MODE
392 #endif // wxLONGLONG_TEST_MODE
397 // convert to long with range checking in the debug mode (only!)
400 wxASSERT_MSG( (m_hi
== 0l) || (m_hi
== -1l),
401 _T("wxLongLong to long conversion loss of precision") );
408 wxLongLongWx
operator+(const wxLongLongWx
& ll
) const;
409 wxLongLongWx
& operator+=(const wxLongLongWx
& ll
);
410 wxLongLongWx
operator+(long l
) const;
411 wxLongLongWx
& operator+=(long l
);
413 // pre increment operator
414 wxLongLongWx
& operator++();
416 // post increment operator
417 wxLongLongWx
& operator++(int) { return ++(*this); }
420 wxLongLongWx
operator-() const;
421 wxLongLongWx
& Negate();
424 wxLongLongWx
operator-(const wxLongLongWx
& ll
) const;
425 wxLongLongWx
& operator-=(const wxLongLongWx
& ll
);
427 // pre decrement operator
428 wxLongLongWx
& operator--();
430 // post decrement operator
431 wxLongLongWx
& operator--(int) { return --(*this); }
435 wxLongLongWx
operator<<(int shift
) const;
436 wxLongLongWx
& operator<<=(int shift
);
439 wxLongLongWx
operator>>(int shift
) const;
440 wxLongLongWx
& operator>>=(int shift
);
443 wxLongLongWx
operator&(const wxLongLongWx
& ll
) const;
444 wxLongLongWx
& operator&=(const wxLongLongWx
& ll
);
445 wxLongLongWx
operator|(const wxLongLongWx
& ll
) const;
446 wxLongLongWx
& operator|=(const wxLongLongWx
& ll
);
447 wxLongLongWx
operator^(const wxLongLongWx
& ll
) const;
448 wxLongLongWx
& operator^=(const wxLongLongWx
& ll
);
449 wxLongLongWx
operator~() const;
452 bool operator==(const wxLongLongWx
& ll
) const
453 { return m_lo
== ll
.m_lo
&& m_hi
== ll
.m_hi
; }
454 bool operator!=(const wxLongLongWx
& ll
) const
455 { return !(*this == ll
); }
456 bool operator<(const wxLongLongWx
& ll
) const;
457 bool operator>(const wxLongLongWx
& ll
) const;
458 bool operator<=(const wxLongLongWx
& ll
) const
459 { return *this < ll
|| *this == ll
; }
460 bool operator>=(const wxLongLongWx
& ll
) const
461 { return *this > ll
|| *this == ll
; }
464 wxLongLongWx
operator*(const wxLongLongWx
& ll
) const;
465 wxLongLongWx
& operator*=(const wxLongLongWx
& ll
);
468 wxLongLongWx
operator/(const wxLongLongWx
& ll
) const;
469 wxLongLongWx
& operator/=(const wxLongLongWx
& ll
);
471 wxLongLongWx
operator%(const wxLongLongWx
& ll
) const;
473 void Divide(const wxLongLongWx
& divisor
,
474 wxLongLongWx
& quotient
,
475 wxLongLongWx
& remainder
) const;
478 #if wxUSE_STD_IOSTREAM
479 friend ostream
& operator<<(ostream
&, const wxLongLongWx
&);
480 #endif // wxUSE_STD_IOSTREAM
482 void *asArray() const;
485 // long is at least 32 bits, so represent our 64bit number as 2 longs
487 long m_hi
; // signed bit is in the high part
490 #ifdef wxLONGLONG_TEST_MODE
493 wxASSERT( (m_ll
>> 32) == m_hi
&& (unsigned long)m_ll
== m_lo
);
497 #endif // wxLONGLONG_TEST_MODE
500 #endif // wxUSE_LONGLONG_WX
502 // ----------------------------------------------------------------------------
504 // ----------------------------------------------------------------------------
506 inline bool WXDLLEXPORT
operator<(long l
, const wxLongLong
& ll
) { return ll
> l
; }
507 inline bool WXDLLEXPORT
operator>(long l
, const wxLongLong
& ll
) { return ll
> l
; }
508 inline bool WXDLLEXPORT
operator<=(long l
, const wxLongLong
& ll
) { return ll
> l
; }
509 inline bool WXDLLEXPORT
operator>=(long l
, const wxLongLong
& ll
) { return ll
> l
; }
510 inline bool WXDLLEXPORT
operator==(long l
, const wxLongLong
& ll
) { return ll
> l
; }
511 inline bool WXDLLEXPORT
operator!=(long l
, const wxLongLong
& ll
) { return ll
> l
; }
513 inline wxLongLong WXDLLEXPORT
operator+(long l
, const wxLongLong
& ll
) { return ll
+ l
; }
514 inline wxLongLong WXDLLEXPORT
operator-(long l
, const wxLongLong
& ll
) { return ll
- l
; }
516 #endif // _WX_LONGLONG_H