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
31 #define wxLONGLONG_TEST_MODE
33 #ifdef wxLONGLONG_TEST_MODE
34 #define wxUSE_LONGLONG_WX 1
35 #define wxUSE_LONGLONG_NATIVE 1
36 #endif // wxLONGLONG_TEST_MODE
38 // ----------------------------------------------------------------------------
39 // decide upon which class we will use
40 // ----------------------------------------------------------------------------
42 // to avoid compilation problems on 64bit machines with ambiguous method calls
43 // we will need to define this
44 #undef wxLongLongIsLong
46 // NB: we #define and not typedef wxLongLong_t because we want to be able to
47 // use 'unsigned wxLongLong_t' as well and because we use "#ifdef
48 // wxLongLong_t" below
49 #if defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
50 #define wxLongLong_t long
51 #define wxLongLongIsLong
52 #elif (defined(__VISUALC__) && defined(__WIN32__)) || defined( __VMS__ )
53 #define wxLongLong_t __int64
54 #elif defined(__BORLANDC__) && (__BORLANDC__ >= 0x530)
55 #define wxLongLong_t __int64
56 #elif defined(__GNUG__)
57 #define wxLongLong_t long long
58 #elif defined(__MWERKS__)
59 #if __option(longlong)
60 #define wxLongLong_t long long
62 #error "The 64 bit integer support in CodeWarrior has been disabled."
63 #error "See the documentation on the 'longlong' pragma."
65 #elif defined(__VISAGECPP__) && __IBMCPP__ >= 400
66 #define wxLongLong_t long long
67 #else // no native long long type
68 // both warning and pragma warning are not portable, but at least an
69 // unknown pragma should never be an error.
70 // Err, actually, Watcom C++ doesn't like it.
71 // (well, if the compilers are _that_ broken, I'm removing it (VZ))
72 #if 0 //ndef __WATCOMC__
73 #pragma warning "Your compiler does not appear to support 64 bit "\
74 "integers, using emulation class instead."
77 #define wxUSE_LONGLONG_WX 1
80 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
81 // to disable automatic testing (useful for the test program which defines
82 // both classes) but by default we only use one class
83 #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
84 // don't use both classes unless wxUSE_LONGLONG_NATIVE was explicitly set:
85 // this is useful in test programs nad only there
86 #ifndef wxUSE_LONGLONG_NATIVE
87 #define wxUSE_LONGLONG_NATIVE 0
90 class WXDLLEXPORT wxLongLongWx
;
91 typedef wxLongLongWx wxLongLong
;
93 // if nothing is defined, use native implementation by default, of course
94 #ifndef wxUSE_LONGLONG_NATIVE
95 #define wxUSE_LONGLONG_NATIVE 1
99 #ifndef wxUSE_LONGLONG_WX
100 #define wxUSE_LONGLONG_WX 0
101 class WXDLLEXPORT wxLongLongNative
;
102 typedef wxLongLongNative wxLongLong
;
105 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
106 // typedef wxLongLong as it wants, we don't do it
108 // ----------------------------------------------------------------------------
109 // choose the appropriate class
110 // ----------------------------------------------------------------------------
112 // we use iostream for wxLongLong output
113 #include "wx/ioswrap.h"
115 #if wxUSE_LONGLONG_NATIVE
117 class WXDLLEXPORT wxLongLongNative
121 // default ctor initializes to 0
122 wxLongLongNative() { m_ll
= 0; }
124 wxLongLongNative(wxLongLong_t ll
) { m_ll
= ll
; }
126 wxLongLongNative(long hi
, unsigned long lo
)
128 // assign first to avoid precision loss!
129 m_ll
= ((wxLongLong_t
) hi
) << 32;
130 m_ll
|= (wxLongLong_t
) lo
;
133 // default copy ctor is ok
137 // assignment operators
138 // from native 64 bit integer
139 wxLongLongNative
& operator=(wxLongLong_t ll
)
140 { m_ll
= ll
; return *this; }
142 // from double: this one has an explicit name because otherwise we
143 // would have ambiguity with "ll = int" and also because we don't want
144 // to have implicit conversions between doubles and wxLongLongs
145 wxLongLongNative
& Assign(double d
)
146 { m_ll
= (wxLongLong_t
)d
; return *this; }
148 // assignment operators from wxLongLongNative is ok
153 { return (long)(m_ll
>> 32); }
155 unsigned long GetLo() const
156 { return (unsigned long)m_ll
; }
158 // get absolute value
159 wxLongLongNative
Abs() const { return wxLongLongNative(*this).Abs(); }
160 wxLongLongNative
& Abs() { if ( m_ll
< 0 ) m_ll
= -m_ll
; return *this; }
162 // convert to native long long
163 wxLongLong_t
GetValue() const { return m_ll
; }
165 // convert to long with range checking in the debug mode (only!)
168 wxASSERT_MSG( (m_ll
>= LONG_MIN
) && (m_ll
<= LONG_MAX
),
169 _T("wxLongLong to long conversion loss of precision") );
174 // don't provide implicit conversion to wxLongLong_t or we will have an
175 // ambiguity for all arithmetic operations
176 //operator wxLongLong_t() const { return m_ll; }
180 wxLongLongNative
operator+(const wxLongLongNative
& ll
) const
181 { return wxLongLongNative(m_ll
+ ll
.m_ll
); }
182 wxLongLongNative
& operator+=(const wxLongLongNative
& ll
)
183 { m_ll
+= ll
.m_ll
; return *this; }
185 wxLongLongNative
operator+(const wxLongLong_t ll
) const
186 { return wxLongLongNative(m_ll
+ ll
); }
187 wxLongLongNative
& operator+=(const wxLongLong_t ll
)
188 { m_ll
+= ll
; return *this; }
191 wxLongLongNative
& operator++()
192 { m_ll
++; return *this; }
195 wxLongLongNative
& operator++(int)
196 { m_ll
++; return *this; }
199 wxLongLongNative
operator-() const
200 { return wxLongLongNative(-m_ll
); }
203 wxLongLongNative
operator-(const wxLongLongNative
& ll
) const
204 { return wxLongLongNative(m_ll
- ll
.m_ll
); }
205 wxLongLongNative
& operator-=(const wxLongLongNative
& ll
)
206 { m_ll
-= ll
.m_ll
; return *this; }
208 wxLongLongNative
operator-(const wxLongLong_t ll
) const
209 { return wxLongLongNative(m_ll
- ll
); }
210 wxLongLongNative
& operator-=(const wxLongLong_t ll
)
211 { m_ll
-= ll
; return *this; }
214 wxLongLongNative
& operator--()
215 { m_ll
--; return *this; }
218 wxLongLongNative
& operator--(int)
219 { m_ll
--; return *this; }
223 wxLongLongNative
operator<<(int shift
) const
224 { return wxLongLongNative(m_ll
<< shift
);; }
225 wxLongLongNative
& operator<<=(int shift
)
226 { m_ll
<<= shift
; return *this; }
229 wxLongLongNative
operator>>(int shift
) const
230 { return wxLongLongNative(m_ll
>> shift
);; }
231 wxLongLongNative
& operator>>=(int shift
)
232 { m_ll
>>= shift
; return *this; }
235 wxLongLongNative
operator&(const wxLongLongNative
& ll
) const
236 { return wxLongLongNative(m_ll
& ll
.m_ll
); }
237 wxLongLongNative
& operator&=(const wxLongLongNative
& ll
)
238 { m_ll
&= ll
.m_ll
; return *this; }
240 wxLongLongNative
operator|(const wxLongLongNative
& ll
) const
241 { return wxLongLongNative(m_ll
| ll
.m_ll
); }
242 wxLongLongNative
& operator|=(const wxLongLongNative
& ll
)
243 { m_ll
|= ll
.m_ll
; 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 // multiplication/division
251 wxLongLongNative
operator*(const wxLongLongNative
& ll
) const
252 { return wxLongLongNative(m_ll
* ll
.m_ll
); }
253 wxLongLongNative
operator*(long l
) const
254 { return wxLongLongNative(m_ll
* l
); }
255 wxLongLongNative
& operator*=(const wxLongLongNative
& ll
)
256 { m_ll
*= ll
.m_ll
; return *this; }
257 wxLongLongNative
& operator*=(long l
)
258 { m_ll
*= l
; return *this; }
260 wxLongLongNative
operator/(const wxLongLongNative
& ll
) const
261 { return wxLongLongNative(m_ll
/ ll
.m_ll
); }
262 wxLongLongNative
operator/(long l
) const
263 { return wxLongLongNative(m_ll
/ l
); }
264 wxLongLongNative
& operator/=(const wxLongLongNative
& ll
)
265 { m_ll
/= ll
.m_ll
; return *this; }
266 wxLongLongNative
& operator/=(long l
)
267 { m_ll
/= l
; return *this; }
269 wxLongLongNative
operator%(const wxLongLongNative
& ll
) const
270 { return wxLongLongNative(m_ll
% ll
.m_ll
); }
271 wxLongLongNative
operator%(long l
) const
272 { return wxLongLongNative(m_ll
% l
); }
275 bool operator==(const wxLongLongNative
& ll
) const
276 { return m_ll
== ll
.m_ll
; }
277 bool operator==(long l
) const
278 { return m_ll
== l
; }
279 bool operator!=(const wxLongLongNative
& ll
) const
280 { return m_ll
!= ll
.m_ll
; }
281 bool operator!=(long l
) const
282 { return m_ll
!= l
; }
283 bool operator<(const wxLongLongNative
& ll
) const
284 { return m_ll
< ll
.m_ll
; }
285 bool operator<(long l
) const
287 bool operator>(const wxLongLongNative
& ll
) const
288 { return m_ll
> ll
.m_ll
; }
289 bool operator>(long l
) const
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
298 { return m_ll
>= l
; }
301 // conversion to byte array: returns a pointer to static buffer!
302 void *asArray() const;
304 #if wxUSE_STD_IOSTREAM
306 friend ostream
& operator<<(ostream
&, const wxLongLongNative
&);
313 #endif // wxUSE_LONGLONG_NATIVE
315 #if wxUSE_LONGLONG_WX
317 class WXDLLEXPORT wxLongLongWx
321 // default ctor initializes to 0
326 #ifdef wxLONGLONG_TEST_MODE
330 #endif // wxLONGLONG_TEST_MODE
333 wxLongLongWx(long l
) { *this = l
; }
335 wxLongLongWx(long hi
, unsigned long lo
)
340 #ifdef wxLONGLONG_TEST_MODE
346 #endif // wxLONGLONG_TEST_MODE
349 // default copy ctor is ok in both cases
353 // assignment operators
355 wxLongLongWx
& operator=(long l
)
358 m_hi
= (l
< 0 ? -1l : 0l);
360 #ifdef wxLONGLONG_TEST_MODE
364 #endif // wxLONGLONG_TEST_MODE
369 wxLongLongWx
& Assign(double d
);
370 // can't have assignment operator from 2 longs
374 long GetHi() const { return m_hi
; }
376 unsigned long GetLo() const { return m_lo
; }
378 // get absolute value
379 wxLongLongWx
Abs() const { return wxLongLongWx(*this).Abs(); }
385 #ifdef wxLONGLONG_TEST_MODE
390 #endif // wxLONGLONG_TEST_MODE
395 // convert to long with range checking in the debug mode (only!)
398 wxASSERT_MSG( (m_hi
== 0l) || (m_hi
== -1l),
399 _T("wxLongLong to long conversion loss of precision") );
406 wxLongLongWx
operator+(const wxLongLongWx
& ll
) const;
407 wxLongLongWx
& operator+=(const wxLongLongWx
& ll
);
408 wxLongLongWx
operator+(long l
) const;
409 wxLongLongWx
& operator+=(long l
);
411 // pre increment operator
412 wxLongLongWx
& operator++();
414 // post increment operator
415 wxLongLongWx
& operator++(int) { return ++(*this); }
418 wxLongLongWx
operator-() const;
419 wxLongLongWx
& Negate();
422 wxLongLongWx
operator-(const wxLongLongWx
& ll
) const;
423 wxLongLongWx
& operator-=(const wxLongLongWx
& ll
);
425 // pre decrement operator
426 wxLongLongWx
& operator--();
428 // post decrement operator
429 wxLongLongWx
& operator--(int) { return --(*this); }
433 wxLongLongWx
operator<<(int shift
) const;
434 wxLongLongWx
& operator<<=(int shift
);
437 wxLongLongWx
operator>>(int shift
) const;
438 wxLongLongWx
& operator>>=(int shift
);
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 wxLongLongWx
& ll
) const;
446 wxLongLongWx
& operator^=(const wxLongLongWx
& ll
);
447 wxLongLongWx
operator~() const;
450 bool operator==(const wxLongLongWx
& ll
) const
451 { return m_lo
== ll
.m_lo
&& m_hi
== ll
.m_hi
; }
452 bool operator!=(const wxLongLongWx
& ll
) const
453 { return !(*this == ll
); }
454 bool operator<(const wxLongLongWx
& ll
) const;
455 bool operator>(const wxLongLongWx
& ll
) const;
456 bool operator<=(const wxLongLongWx
& ll
) const
457 { return *this < ll
|| *this == ll
; }
458 bool operator>=(const wxLongLongWx
& ll
) const
459 { return *this > ll
|| *this == ll
; }
462 wxLongLongWx
operator*(const wxLongLongWx
& ll
) const;
463 wxLongLongWx
& operator*=(const wxLongLongWx
& ll
);
466 wxLongLongWx
operator/(const wxLongLongWx
& ll
) const;
467 wxLongLongWx
& operator/=(const wxLongLongWx
& ll
);
469 wxLongLongWx
operator%(const wxLongLongWx
& ll
) const;
471 void Divide(const wxLongLongWx
& divisor
,
472 wxLongLongWx
& quotient
,
473 wxLongLongWx
& remainder
) const;
476 #if wxUSE_STD_IOSTREAM
477 friend ostream
& operator<<(ostream
&, const wxLongLongWx
&);
478 #endif // wxUSE_STD_IOSTREAM
480 void *asArray() const;
483 // long is at least 32 bits, so represent our 64bit number as 2 longs
485 long m_hi
; // signed bit is in the high part
488 #ifdef wxLONGLONG_TEST_MODE
491 wxASSERT( (m_ll
>> 32) == m_hi
&& (unsigned long)m_ll
== m_lo
);
495 #endif // wxLONGLONG_TEST_MODE
498 #endif // wxUSE_LONGLONG_WX
500 // ----------------------------------------------------------------------------
502 // ----------------------------------------------------------------------------
504 inline bool WXDLLEXPORT
operator<(long l
, const wxLongLong
& ll
) { return ll
> l
; }
505 inline bool WXDLLEXPORT
operator>(long l
, const wxLongLong
& ll
) { return ll
> l
; }
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
; }
511 inline wxLongLong WXDLLEXPORT
operator+(long l
, const wxLongLong
& ll
) { return ll
+ l
; }
512 inline wxLongLong WXDLLEXPORT
operator-(long l
, const wxLongLong
& ll
) { return ll
- l
; }
514 #endif // _WX_LONGLONG_H