]> git.saurik.com Git - wxWidgets.git/blame - src/common/longlong.cpp
wiring osx native notification during launch
[wxWidgets.git] / src / common / longlong.cpp
CommitLineData
8b81872f 1/////////////////////////////////////////////////////////////////////////////
18680f86 2// Name: src/common/longlong.cpp
8b81872f
VZ
3// Purpose: implementation of wxLongLongNative
4// Author: Jeffrey C. Ollie <jeff@ollie.clive.ia.us>, Vadim Zeitlin
77ffb593 5// Remarks: this class is not public in wxWidgets 2.0! It is intentionally
8b81872f
VZ
6// not documented and is for private use only.
7// Modified by:
8// Created: 10.02.99
9// RCS-ID: $Id$
10// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 11// Licence: wxWindows licence
8b81872f
VZ
12/////////////////////////////////////////////////////////////////////////////
13
14// ============================================================================
15// headers
16// ============================================================================
17
8b81872f
VZ
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
21 #pragma hdrstop
22#endif
23
4e57b0d4 24#if wxUSE_LONGLONG
18680f86 25
8b81872f 26#include "wx/longlong.h"
18680f86
WS
27
28#ifndef WX_PRECOMP
29 #include "wx/math.h" // for fabs()
30#endif
8b81872f 31
216a72f3 32#if wxUSE_STREAMS
18680f86 33 #include "wx/txtstrm.h"
216a72f3
VZ
34#endif
35
5b8ee5de 36#include <string.h> // for memset()
de85a884 37
19c5a468
VZ
38#include "wx/ioswrap.h"
39
8b81872f
VZ
40// ============================================================================
41// implementation
42// ============================================================================
43
44#if wxUSE_LONGLONG_NATIVE
45
46// ----------------------------------------------------------------------------
47// misc
48// ----------------------------------------------------------------------------
49
2a310492 50void *wxLongLongNative::asArray() const
8b81872f
VZ
51{
52 static unsigned char temp[8];
53
17a1ebd1
VZ
54 temp[0] = wx_truncate_cast(unsigned char, ((m_ll >> 56) & 0xFF));
55 temp[1] = wx_truncate_cast(unsigned char, ((m_ll >> 48) & 0xFF));
56 temp[2] = wx_truncate_cast(unsigned char, ((m_ll >> 40) & 0xFF));
57 temp[3] = wx_truncate_cast(unsigned char, ((m_ll >> 32) & 0xFF));
58 temp[4] = wx_truncate_cast(unsigned char, ((m_ll >> 24) & 0xFF));
59 temp[5] = wx_truncate_cast(unsigned char, ((m_ll >> 16) & 0xFF));
60 temp[6] = wx_truncate_cast(unsigned char, ((m_ll >> 8) & 0xFF));
61 temp[7] = wx_truncate_cast(unsigned char, ((m_ll >> 0) & 0xFF));
8b81872f
VZ
62
63 return temp;
64}
65
8e38fd1f
RL
66void *wxULongLongNative::asArray() const
67{
68 static unsigned char temp[8];
69
17a1ebd1
VZ
70 temp[0] = wx_truncate_cast(unsigned char, ((m_ll >> 56) & 0xFF));
71 temp[1] = wx_truncate_cast(unsigned char, ((m_ll >> 48) & 0xFF));
72 temp[2] = wx_truncate_cast(unsigned char, ((m_ll >> 40) & 0xFF));
73 temp[3] = wx_truncate_cast(unsigned char, ((m_ll >> 32) & 0xFF));
74 temp[4] = wx_truncate_cast(unsigned char, ((m_ll >> 24) & 0xFF));
75 temp[5] = wx_truncate_cast(unsigned char, ((m_ll >> 16) & 0xFF));
76 temp[6] = wx_truncate_cast(unsigned char, ((m_ll >> 8) & 0xFF));
77 temp[7] = wx_truncate_cast(unsigned char, ((m_ll >> 0) & 0xFF));
8e38fd1f
RL
78
79 return temp;
80}
81
842215bb
WS
82#if wxUSE_LONGLONG_WX
83wxLongLongNative::wxLongLongNative(wxLongLongWx ll)
84{
85 // assign first to avoid precision loss!
86 m_ll = ll.GetHi();
87 m_ll <<= 32;
88 m_ll |= ll.GetLo();
89}
90
91wxLongLongNative& wxLongLongNative::operator=(wxLongLongWx ll)
92{
93 // assign first to avoid precision loss!
94 m_ll = ll.GetHi();
95 m_ll <<= 32;
96 m_ll |= ll.GetLo();
97 return *this;
98}
216a72f3
VZ
99
100wxLongLongNative& wxLongLongNative::operator=(const class wxULongLongWx &ll)
101{
102 // assign first to avoid precision loss!
103 m_ll = ll.GetHi();
104 m_ll <<= 32;
105 m_ll |= ll.GetLo();
106 return *this;
107}
108
109wxULongLongNative::wxULongLongNative(const class wxULongLongWx &ll)
110{
111 // assign first to avoid precision loss!
112 m_ll = ll.GetHi();
113 m_ll <<= 32;
114 m_ll |= ((unsigned long) ll.GetLo());
115}
116
117wxULongLongNative& wxULongLongNative::operator=(wxLongLongWx ll)
118{
119 // assign first to avoid precision loss!
120 m_ll = ll.GetHi();
121 m_ll <<= 32;
122 m_ll |= ((unsigned long) ll.GetLo());
123 return *this;
124}
125
126wxULongLongNative& wxULongLongNative::operator=(const class wxULongLongWx &ll)
127{
128 // assign first to avoid precision loss!
129 m_ll = ll.GetHi();
130 m_ll <<= 32;
131 m_ll |= ((unsigned long) ll.GetLo());
132 return *this;
133}
842215bb
WS
134#endif
135
4c24ca50
VZ
136#ifdef __VISUALC6__
137double wxULongLongNative::ToDouble() const
138{
139 // Work around the problem of casting unsigned __int64 to double in VC6
140 // (which for unknown reasons only manifests itself in DLL builds, i.e.
141 // when using /MD).
142 static const __int64 int64_t_max = 9223372036854775807i64;
143 if ( m_ll <= int64_t_max )
144 return wx_truncate_cast(double, (wxLongLong_t)m_ll);
145
146 double d = wx_truncate_cast(double, int64_t_max);
147 d += (__int64)(m_ll - int64_t_max - 1); // The cast is safe because of -1
148 return d + 1;
149}
150#endif // __VISUALC6__
151
8b81872f
VZ
152#endif // wxUSE_LONGLONG_NATIVE
153
41acf5c0
VZ
154// ============================================================================
155// wxLongLongWx: emulation of 'long long' using 2 longs
156// ============================================================================
157
8b81872f
VZ
158#if wxUSE_LONGLONG_WX
159
216a72f3
VZ
160// Set value from unsigned wxULongLongWx
161wxLongLongWx &wxLongLongWx::operator=(const class wxULongLongWx &ll)
162{
163 m_hi = (unsigned long) ll.GetHi();
164 m_lo = ll.GetLo();
165 return *this;
166}
167
41acf5c0
VZ
168// assignment
169wxLongLongWx& wxLongLongWx::Assign(double d)
170{
2a310492
VZ
171 bool positive = d >= 0;
172 d = fabs(d);
617ec456 173 if ( d <= ULONG_MAX )
41acf5c0 174 {
2a310492 175 m_hi = 0;
41acf5c0
VZ
176 m_lo = (long)d;
177 }
178 else
179 {
617ec456
VZ
180 m_hi = (unsigned long)(d / (1.0 + (double)ULONG_MAX));
181 m_lo = (unsigned long)(d - ((double)m_hi * (1.0 + (double)ULONG_MAX)));
41acf5c0
VZ
182 }
183
2a310492
VZ
184#ifdef wxLONGLONG_TEST_MODE
185 m_ll = (wxLongLong_t)d;
186
187 Check();
188#endif // wxLONGLONG_TEST_MODE
189
617ec456
VZ
190 if ( !positive )
191 Negate();
192
41acf5c0
VZ
193 return *this;
194}
195
e7aaf2de
VZ
196double wxLongLongWx::ToDouble() const
197{
198 double d = m_hi;
199 d *= 1.0 + (double)ULONG_MAX;
200 d += m_lo;
201
202#ifdef wxLONGLONG_TEST_MODE
203 wxASSERT( d == m_ll );
204#endif // wxLONGLONG_TEST_MODE
205
206 return d;
207}
208
d870a030
VZ
209double wxULongLongWx::ToDouble() const
210{
211 unsigned double d = m_hi;
212 d *= 1.0 + (double)ULONG_MAX;
213 d += m_lo;
214
215#ifdef wxLONGLONG_TEST_MODE
216 wxASSERT( d == m_ll );
217#endif // wxLONGLONG_TEST_MODE
218
219 return d;
220}
221
8b81872f
VZ
222wxLongLongWx wxLongLongWx::operator<<(int shift) const
223{
2a310492
VZ
224 wxLongLongWx ll(*this);
225 ll <<= shift;
8b81872f 226
2a310492 227 return ll;
8b81872f
VZ
228}
229
8e38fd1f
RL
230wxULongLongWx wxULongLongWx::operator<<(int shift) const
231{
232 wxULongLongWx ll(*this);
233 ll <<= shift;
234
235 return ll;
236}
237
8b81872f
VZ
238wxLongLongWx& wxLongLongWx::operator<<=(int shift)
239{
2a310492 240 if (shift != 0)
8b81872f 241 {
2a310492
VZ
242 if (shift < 32)
243 {
244 m_hi <<= shift;
245 m_hi |= m_lo >> (32 - shift);
246 m_lo <<= shift;
247 }
248 else
249 {
250 m_hi = m_lo << (shift - 32);
251 m_lo = 0;
252 }
8b81872f
VZ
253 }
254
2a310492
VZ
255#ifdef wxLONGLONG_TEST_MODE
256 m_ll <<= shift;
257
258 Check();
259#endif // wxLONGLONG_TEST_MODE
260
8b81872f
VZ
261 return *this;
262}
263
8e38fd1f
RL
264wxULongLongWx& wxULongLongWx::operator<<=(int shift)
265{
266 if (shift != 0)
267 {
268 if (shift < 32)
269 {
270 m_hi <<= shift;
271 m_hi |= m_lo >> (32 - shift);
272 m_lo <<= shift;
273 }
274 else
275 {
276 m_hi = m_lo << (shift - 32);
277 m_lo = 0;
278 }
279 }
280
281#ifdef wxLONGLONG_TEST_MODE
282 m_ll <<= shift;
283
284 Check();
285#endif // wxLONGLONG_TEST_MODE
286
287 return *this;
288}
289
8b81872f
VZ
290wxLongLongWx wxLongLongWx::operator>>(int shift) const
291{
2a310492
VZ
292 wxLongLongWx ll(*this);
293 ll >>= shift;
8b81872f 294
2a310492 295 return ll;
8b81872f
VZ
296}
297
8e38fd1f
RL
298wxULongLongWx wxULongLongWx::operator>>(int shift) const
299{
300 wxULongLongWx ll(*this);
301 ll >>= shift;
302
303 return ll;
304}
305
8b81872f
VZ
306wxLongLongWx& wxLongLongWx::operator>>=(int shift)
307{
2a310492 308 if (shift != 0)
8b81872f 309 {
2a310492
VZ
310 if (shift < 32)
311 {
312 m_lo >>= shift;
313 m_lo |= m_hi << (32 - shift);
314 m_hi >>= shift;
315 }
316 else
317 {
318 m_lo = m_hi >> (shift - 32);
319 m_hi = (m_hi < 0 ? -1L : 0);
320 }
8b81872f
VZ
321 }
322
2a310492
VZ
323#ifdef wxLONGLONG_TEST_MODE
324 m_ll >>= shift;
325
326 Check();
327#endif // wxLONGLONG_TEST_MODE
328
8b81872f
VZ
329 return *this;
330}
331
8e38fd1f
RL
332wxULongLongWx& wxULongLongWx::operator>>=(int shift)
333{
334 if (shift != 0)
335 {
336 if (shift < 32)
337 {
338 m_lo >>= shift;
339 m_lo |= m_hi << (32 - shift);
340 m_hi >>= shift;
341 }
342 else
343 {
344 m_lo = m_hi >> (shift - 32);
345 m_hi = 0;
346 }
347 }
348
349#ifdef wxLONGLONG_TEST_MODE
350 m_ll >>= shift;
351
352 Check();
353#endif // wxLONGLONG_TEST_MODE
354
355 return *this;
356}
357
8b81872f
VZ
358wxLongLongWx wxLongLongWx::operator+(const wxLongLongWx& ll) const
359{
2a310492
VZ
360 wxLongLongWx res(*this);
361 res += ll;
8b81872f 362
2a310492 363 return res;
8b81872f
VZ
364}
365
8e38fd1f
RL
366wxULongLongWx wxULongLongWx::operator+(const wxULongLongWx& ll) const
367{
368 wxULongLongWx res(*this);
369 res += ll;
370
371 return res;
372}
373
8b81872f
VZ
374wxLongLongWx wxLongLongWx::operator+(long l) const
375{
2a310492
VZ
376 wxLongLongWx res(*this);
377 res += l;
8b81872f 378
2a310492 379 return res;
8b81872f
VZ
380}
381
8e38fd1f
RL
382wxULongLongWx wxULongLongWx::operator+(unsigned long l) const
383{
384 wxULongLongWx res(*this);
385 res += l;
386
387 return res;
388}
389
8b81872f
VZ
390wxLongLongWx& wxLongLongWx::operator+=(const wxLongLongWx& ll)
391{
392 unsigned long previous = m_lo;
393
394 m_lo += ll.m_lo;
395 m_hi += ll.m_hi;
396
397 if ((m_lo < previous) || (m_lo < ll.m_lo))
398 m_hi++;
399
2a310492
VZ
400#ifdef wxLONGLONG_TEST_MODE
401 m_ll += ll.m_ll;
402
403 Check();
404#endif // wxLONGLONG_TEST_MODE
405
8b81872f
VZ
406 return *this;
407}
408
8e38fd1f
RL
409wxULongLongWx& wxULongLongWx::operator+=(const wxULongLongWx& ll)
410{
411 unsigned long previous = m_lo;
412
413 m_lo += ll.m_lo;
414 m_hi += ll.m_hi;
415
416 if ((m_lo < previous) || (m_lo < ll.m_lo))
417 m_hi++;
418
419#ifdef wxLONGLONG_TEST_MODE
420 m_ll += ll.m_ll;
421
422 Check();
423#endif // wxLONGLONG_TEST_MODE
424
425 return *this;
426}
427
8b81872f
VZ
428wxLongLongWx& wxLongLongWx::operator+=(long l)
429{
430 unsigned long previous = m_lo;
431
432 m_lo += l;
433 if (l < 0)
434 m_hi += -1l;
435
436 if ((m_lo < previous) || (m_lo < (unsigned long)l))
437 m_hi++;
438
2a310492
VZ
439#ifdef wxLONGLONG_TEST_MODE
440 m_ll += l;
441
442 Check();
443#endif // wxLONGLONG_TEST_MODE
444
8b81872f
VZ
445 return *this;
446}
447
8e38fd1f
RL
448wxULongLongWx& wxULongLongWx::operator+=(unsigned long l)
449{
450 unsigned long previous = m_lo;
451
452 m_lo += l;
453
454 if ((m_lo < previous) || (m_lo < l))
455 m_hi++;
456
457#ifdef wxLONGLONG_TEST_MODE
458 m_ll += l;
459
460 Check();
461#endif // wxLONGLONG_TEST_MODE
462
463 return *this;
464}
465
8b81872f
VZ
466// pre increment
467wxLongLongWx& wxLongLongWx::operator++()
468{
469 m_lo++;
470 if (m_lo == 0)
471 m_hi++;
472
2a310492
VZ
473#ifdef wxLONGLONG_TEST_MODE
474 m_ll++;
8b81872f 475
2a310492
VZ
476 Check();
477#endif // wxLONGLONG_TEST_MODE
8b81872f
VZ
478
479 return *this;
480}
481
8e38fd1f
RL
482wxULongLongWx& wxULongLongWx::operator++()
483{
484 m_lo++;
485 if (m_lo == 0)
486 m_hi++;
487
488#ifdef wxLONGLONG_TEST_MODE
489 m_ll++;
490
491 Check();
492#endif // wxLONGLONG_TEST_MODE
493
494 return *this;
495}
496
8b81872f
VZ
497// negation
498wxLongLongWx wxLongLongWx::operator-() const
499{
2a310492
VZ
500 wxLongLongWx res(*this);
501 res.Negate();
502
503 return res;
504}
505
506wxLongLongWx& wxLongLongWx::Negate()
507{
508 m_hi = ~m_hi;
509 m_lo = ~m_lo;
510
511 m_lo++;
512 if ( m_lo == 0 )
513 m_hi++;
8b81872f 514
2a310492
VZ
515#ifdef wxLONGLONG_TEST_MODE
516 m_ll = -m_ll;
8b81872f 517
2a310492
VZ
518 Check();
519#endif // wxLONGLONG_TEST_MODE
520
521 return *this;
8b81872f
VZ
522}
523
524// subtraction
525
526wxLongLongWx wxLongLongWx::operator-(const wxLongLongWx& ll) const
527{
2a310492
VZ
528 wxLongLongWx res(*this);
529 res -= ll;
8b81872f 530
2a310492 531 return res;
8b81872f
VZ
532}
533
842215bb 534wxLongLongWx wxULongLongWx::operator-(const wxULongLongWx& ll) const
8e38fd1f 535{
842215bb
WS
536 wxASSERT(m_hi <= LONG_MAX );
537 wxASSERT(ll.m_hi <= LONG_MAX );
538
539 wxLongLongWx res( (long)m_hi , m_lo );
540 wxLongLongWx op( (long)ll.m_hi , ll.m_lo );
541 res -= op;
8e38fd1f
RL
542
543 return res;
544}
545
8b81872f
VZ
546wxLongLongWx& wxLongLongWx::operator-=(const wxLongLongWx& ll)
547{
548 unsigned long previous = m_lo;
549
550 m_lo -= ll.m_lo;
551 m_hi -= ll.m_hi;
552
553 if (previous < ll.m_lo)
554 m_hi--;
555
2a310492
VZ
556#ifdef wxLONGLONG_TEST_MODE
557 m_ll -= ll.m_ll;
558
559 Check();
560#endif // wxLONGLONG_TEST_MODE
561
13111b2a 562 return *this;
8b81872f
VZ
563}
564
8e38fd1f
RL
565wxULongLongWx& wxULongLongWx::operator-=(const wxULongLongWx& ll)
566{
567 unsigned long previous = m_lo;
568
569 m_lo -= ll.m_lo;
570 m_hi -= ll.m_hi;
571
572 if (previous < ll.m_lo)
573 m_hi--;
574
575#ifdef wxLONGLONG_TEST_MODE
576 m_ll -= ll.m_ll;
577
578 Check();
579#endif // wxLONGLONG_TEST_MODE
580
581 return *this;
582}
583
8b81872f
VZ
584// pre decrement
585wxLongLongWx& wxLongLongWx::operator--()
586{
587 m_lo--;
588 if (m_lo == 0xFFFFFFFF)
589 m_hi--;
590
2a310492
VZ
591#ifdef wxLONGLONG_TEST_MODE
592 m_ll--;
8b81872f 593
2a310492
VZ
594 Check();
595#endif // wxLONGLONG_TEST_MODE
8b81872f
VZ
596
597 return *this;
598}
599
8e38fd1f
RL
600wxULongLongWx& wxULongLongWx::operator--()
601{
602 m_lo--;
603 if (m_lo == 0xFFFFFFFF)
604 m_hi--;
605
606#ifdef wxLONGLONG_TEST_MODE
607 m_ll--;
608
609 Check();
610#endif // wxLONGLONG_TEST_MODE
611
612 return *this;
613}
614
8b81872f
VZ
615// comparison operators
616
617bool wxLongLongWx::operator<(const wxLongLongWx& ll) const
618{
2ea24d9f 619 if ( m_hi < ll.m_hi )
f644b28c 620 return true;
2ea24d9f
VZ
621 else if ( m_hi == ll.m_hi )
622 return m_lo < ll.m_lo;
623 else
f644b28c 624 return false;
8b81872f
VZ
625}
626
8e38fd1f
RL
627bool wxULongLongWx::operator<(const wxULongLongWx& ll) const
628{
629 if ( m_hi < ll.m_hi )
f644b28c 630 return true;
8e38fd1f
RL
631 else if ( m_hi == ll.m_hi )
632 return m_lo < ll.m_lo;
633 else
f644b28c 634 return false;
8e38fd1f
RL
635}
636
8b81872f
VZ
637bool wxLongLongWx::operator>(const wxLongLongWx& ll) const
638{
2ea24d9f 639 if ( m_hi > ll.m_hi )
f644b28c 640 return true;
2ea24d9f
VZ
641 else if ( m_hi == ll.m_hi )
642 return m_lo > ll.m_lo;
643 else
f644b28c 644 return false;
8b81872f
VZ
645}
646
8e38fd1f
RL
647bool wxULongLongWx::operator>(const wxULongLongWx& ll) const
648{
649 if ( m_hi > ll.m_hi )
f644b28c 650 return true;
8e38fd1f
RL
651 else if ( m_hi == ll.m_hi )
652 return m_lo > ll.m_lo;
653 else
f644b28c 654 return false;
8e38fd1f
RL
655}
656
8b81872f
VZ
657// bitwise operators
658
659wxLongLongWx wxLongLongWx::operator&(const wxLongLongWx& ll) const
660{
661 return wxLongLongWx(m_hi & ll.m_hi, m_lo & ll.m_lo);
662}
663
8e38fd1f
RL
664wxULongLongWx wxULongLongWx::operator&(const wxULongLongWx& ll) const
665{
666 return wxULongLongWx(m_hi & ll.m_hi, m_lo & ll.m_lo);
667}
668
8b81872f
VZ
669wxLongLongWx wxLongLongWx::operator|(const wxLongLongWx& ll) const
670{
671 return wxLongLongWx(m_hi | ll.m_hi, m_lo | ll.m_lo);
672}
673
8e38fd1f
RL
674wxULongLongWx wxULongLongWx::operator|(const wxULongLongWx& ll) const
675{
676 return wxULongLongWx(m_hi | ll.m_hi, m_lo | ll.m_lo);
677}
678
8b81872f
VZ
679wxLongLongWx wxLongLongWx::operator^(const wxLongLongWx& ll) const
680{
681 return wxLongLongWx(m_hi ^ ll.m_hi, m_lo ^ ll.m_lo);
682}
683
8e38fd1f
RL
684wxULongLongWx wxULongLongWx::operator^(const wxULongLongWx& ll) const
685{
686 return wxULongLongWx(m_hi ^ ll.m_hi, m_lo ^ ll.m_lo);
687}
688
8b81872f
VZ
689wxLongLongWx& wxLongLongWx::operator&=(const wxLongLongWx& ll)
690{
691 m_lo &= ll.m_lo;
692 m_hi &= ll.m_hi;
693
2a310492
VZ
694#ifdef wxLONGLONG_TEST_MODE
695 m_ll &= ll.m_ll;
696
697 Check();
698#endif // wxLONGLONG_TEST_MODE
699
8b81872f
VZ
700 return *this;
701}
702
8e38fd1f
RL
703wxULongLongWx& wxULongLongWx::operator&=(const wxULongLongWx& ll)
704{
705 m_lo &= ll.m_lo;
706 m_hi &= ll.m_hi;
707
708#ifdef wxLONGLONG_TEST_MODE
709 m_ll &= ll.m_ll;
710
711 Check();
712#endif // wxLONGLONG_TEST_MODE
713
714 return *this;
715}
716
8b81872f
VZ
717wxLongLongWx& wxLongLongWx::operator|=(const wxLongLongWx& ll)
718{
719 m_lo |= ll.m_lo;
720 m_hi |= ll.m_hi;
721
2a310492
VZ
722#ifdef wxLONGLONG_TEST_MODE
723 m_ll |= ll.m_ll;
724
725 Check();
726#endif // wxLONGLONG_TEST_MODE
727
8b81872f
VZ
728 return *this;
729}
730
8e38fd1f
RL
731wxULongLongWx& wxULongLongWx::operator|=(const wxULongLongWx& ll)
732{
733 m_lo |= ll.m_lo;
734 m_hi |= ll.m_hi;
735
736#ifdef wxLONGLONG_TEST_MODE
737 m_ll |= ll.m_ll;
738
739 Check();
740#endif // wxLONGLONG_TEST_MODE
741
742 return *this;
743}
744
8b81872f
VZ
745wxLongLongWx& wxLongLongWx::operator^=(const wxLongLongWx& ll)
746{
747 m_lo ^= ll.m_lo;
748 m_hi ^= ll.m_hi;
749
2a310492
VZ
750#ifdef wxLONGLONG_TEST_MODE
751 m_ll ^= ll.m_ll;
752
753 Check();
754#endif // wxLONGLONG_TEST_MODE
755
8b81872f
VZ
756 return *this;
757}
758
8e38fd1f
RL
759wxULongLongWx& wxULongLongWx::operator^=(const wxULongLongWx& ll)
760{
761 m_lo ^= ll.m_lo;
762 m_hi ^= ll.m_hi;
763
764#ifdef wxLONGLONG_TEST_MODE
765 m_ll ^= ll.m_ll;
766
767 Check();
768#endif // wxLONGLONG_TEST_MODE
769
770 return *this;
771}
772
8b81872f
VZ
773wxLongLongWx wxLongLongWx::operator~() const
774{
775 return wxLongLongWx(~m_hi, ~m_lo);
776}
777
8e38fd1f
RL
778wxULongLongWx wxULongLongWx::operator~() const
779{
780 return wxULongLongWx(~m_hi, ~m_lo);
781}
782
8b81872f
VZ
783// multiplication
784
785wxLongLongWx wxLongLongWx::operator*(const wxLongLongWx& ll) const
786{
2a310492
VZ
787 wxLongLongWx res(*this);
788 res *= ll;
8b81872f 789
2a310492 790 return res;
8b81872f
VZ
791}
792
8e38fd1f
RL
793wxULongLongWx wxULongLongWx::operator*(const wxULongLongWx& ll) const
794{
795 wxULongLongWx res(*this);
796 res *= ll;
797
798 return res;
799}
800
8b81872f
VZ
801wxLongLongWx& wxLongLongWx::operator*=(const wxLongLongWx& ll)
802{
803 wxLongLongWx t(m_hi, m_lo);
804 wxLongLongWx q(ll.m_hi, ll.m_lo);
8b81872f 805
2a310492
VZ
806 m_hi = m_lo = 0;
807
808#ifdef wxLONGLONG_TEST_MODE
809 wxLongLong_t llOld = m_ll;
810 m_ll = 0;
811#endif // wxLONGLONG_TEST_MODE
812
813 int counter = 0;
8b81872f
VZ
814 do
815 {
816 if ((q.m_lo & 1) != 0)
2a310492 817 *this += t;
8b81872f
VZ
818 q >>= 1;
819 t <<= 1;
820 counter++;
2a310492
VZ
821 }
822 while ((counter < 64) && ((q.m_hi != 0) || (q.m_lo != 0)));
823
824#ifdef wxLONGLONG_TEST_MODE
825 m_ll = llOld * ll.m_ll;
826
827 Check();
828#endif // wxLONGLONG_TEST_MODE
829
8b81872f
VZ
830 return *this;
831}
832
8e38fd1f
RL
833wxULongLongWx& wxULongLongWx::operator*=(const wxULongLongWx& ll)
834{
835 wxULongLongWx t(m_hi, m_lo);
836 wxULongLongWx q(ll.m_hi, ll.m_lo);
837
838 m_hi = m_lo = 0;
839
840#ifdef wxLONGLONG_TEST_MODE
60582201 841 wxULongLong_t llOld = m_ll;
8e38fd1f
RL
842 m_ll = 0;
843#endif // wxLONGLONG_TEST_MODE
844
845 int counter = 0;
846 do
847 {
848 if ((q.m_lo & 1) != 0)
849 *this += t;
850 q >>= 1;
851 t <<= 1;
852 counter++;
853 }
854 while ((counter < 64) && ((q.m_hi != 0) || (q.m_lo != 0)));
855
856#ifdef wxLONGLONG_TEST_MODE
857 m_ll = llOld * ll.m_ll;
858
859 Check();
860#endif // wxLONGLONG_TEST_MODE
861
862 return *this;
863}
864
8b81872f
VZ
865// division
866
6984e414
VZ
867#define IS_MSB_SET(ll) ((ll.GetHi()) & (1 << (8*sizeof(long) - 1)))
868
5e6a0e83 869void wxLongLongWx::Divide(const wxLongLongWx& divisorIn,
cd0b1709 870 wxLongLongWx& quotient,
6984e414 871 wxLongLongWx& remainderIO) const
8b81872f 872{
5e6a0e83 873 if ((divisorIn.m_lo == 0) && (divisorIn.m_hi == 0))
8b81872f
VZ
874 {
875 // provoke division by zero error and silence the compilers warnings
876 // about an expression without effect and unused variable
5e6a0e83 877 long dummy = divisorIn.m_lo/divisorIn.m_hi;
8b81872f
VZ
878 dummy += 0;
879 }
880
2ea24d9f
VZ
881 // VZ: I'm writing this in a hurry and it's surely not the fastest way to
882 // do this - any improvements are more than welcome
5e6a0e83
VZ
883 //
884 // code inspired by the snippet at
885 // http://www.bearcave.com/software/divide.htm
886 //
887 // Copyright notice:
888 //
889 // Use of this program, for any purpose, is granted the author, Ian
890 // Kaplan, as long as this copyright notice is included in the source
891 // code or any source code derived from this program. The user assumes
3a994742 892 // all responsibility for using this code.
5e6a0e83
VZ
893
894 // init everything
6984e414 895 wxULongLongWx dividend, divisor, remainder;
5e6a0e83
VZ
896
897 quotient = 0l;
898 remainder = 0l;
899
5e6a0e83
VZ
900 // always do unsigned division and adjust the signs later: in C integer
901 // division, the sign of the remainder is the same as the sign of the
902 // dividend, while the sign of the quotient is the product of the signs of
903 // the dividend and divisor. Of course, we also always have
904 //
905 // dividend = quotient*divisor + remainder
906 //
907 // with 0 <= abs(remainder) < abs(divisor)
6984e414 908 bool negRemainder = GetHi() < 0;
f644b28c 909 bool negQuotient = false; // assume positive
6984e414 910 if ( GetHi() < 0 )
5e6a0e83
VZ
911 {
912 negQuotient = !negQuotient;
6984e414
VZ
913 dividend = -*this;
914 } else {
915 dividend = *this;
5e6a0e83 916 }
6984e414 917 if ( divisorIn.GetHi() < 0 )
5e6a0e83
VZ
918 {
919 negQuotient = !negQuotient;
6984e414
VZ
920 divisor = -divisorIn;
921 } else {
922 divisor = divisorIn;
5e6a0e83 923 }
2ea24d9f 924
2a310492
VZ
925 // check for some particular cases
926 if ( divisor > dividend )
2ea24d9f 927 {
2a310492
VZ
928 remainder = dividend;
929 }
930 else if ( divisor == dividend )
931 {
932 quotient = 1l;
5e6a0e83 933 }
2a310492
VZ
934 else
935 {
842215bb 936 // here: dividend > divisor and both are positive: do unsigned division
2a310492
VZ
937 size_t nBits = 64u;
938 wxLongLongWx d;
5e6a0e83 939
2a310492 940 while ( remainder < divisor )
2ea24d9f 941 {
2a310492
VZ
942 remainder <<= 1;
943 if ( IS_MSB_SET(dividend) )
944 {
945 remainder |= 1;
946 }
947
948 d = dividend;
949 dividend <<= 1;
950
951 nBits--;
2ea24d9f
VZ
952 }
953
2a310492
VZ
954 // undo the last loop iteration
955 dividend = d;
956 remainder >>= 1;
957 nBits++;
5e6a0e83 958
2a310492
VZ
959 for ( size_t i = 0; i < nBits; i++ )
960 {
961 remainder <<= 1;
962 if ( IS_MSB_SET(dividend) )
963 {
964 remainder |= 1;
965 }
966
967 wxLongLongWx t = remainder - divisor;
968 dividend <<= 1;
969 quotient <<= 1;
970 if ( !IS_MSB_SET(t) )
971 {
972 quotient |= 1;
973
974 remainder = t;
975 }
5e6a0e83 976 }
2ea24d9f
VZ
977 }
978
6984e414
VZ
979 remainderIO = remainder;
980
5e6a0e83
VZ
981 // adjust signs
982 if ( negRemainder )
2ea24d9f 983 {
6984e414 984 remainderIO = -remainderIO;
2ea24d9f
VZ
985 }
986
5e6a0e83
VZ
987 if ( negQuotient )
988 {
989 quotient = -quotient;
990 }
2ea24d9f
VZ
991}
992
8e38fd1f
RL
993void wxULongLongWx::Divide(const wxULongLongWx& divisorIn,
994 wxULongLongWx& quotient,
995 wxULongLongWx& remainder) const
996{
997 if ((divisorIn.m_lo == 0) && (divisorIn.m_hi == 0))
998 {
999 // provoke division by zero error and silence the compilers warnings
1000 // about an expression without effect and unused variable
1001 unsigned long dummy = divisorIn.m_lo/divisorIn.m_hi;
1002 dummy += 0;
1003 }
1004
1005 // VZ: I'm writing this in a hurry and it's surely not the fastest way to
1006 // do this - any improvements are more than welcome
1007 //
1008 // code inspired by the snippet at
1009 // http://www.bearcave.com/software/divide.htm
1010 //
1011 // Copyright notice:
1012 //
1013 // Use of this program, for any purpose, is granted the author, Ian
1014 // Kaplan, as long as this copyright notice is included in the source
1015 // code or any source code derived from this program. The user assumes
1016 // all responsibility for using this code.
1017
1018 // init everything
1019 wxULongLongWx dividend = *this,
1020 divisor = divisorIn;
1021
1022 quotient = 0l;
1023 remainder = 0l;
1024
1025 // check for some particular cases
1026 if ( divisor > dividend )
1027 {
1028 remainder = dividend;
1029 }
1030 else if ( divisor == dividend )
1031 {
1032 quotient = 1l;
1033 }
1034 else
1035 {
1036 // here: dividend > divisor
1037 size_t nBits = 64u;
1038 wxULongLongWx d;
1039
8e38fd1f
RL
1040 while ( remainder < divisor )
1041 {
1042 remainder <<= 1;
1043 if ( IS_MSB_SET(dividend) )
1044 {
1045 remainder |= 1;
1046 }
1047
1048 d = dividend;
1049 dividend <<= 1;
1050
1051 nBits--;
1052 }
1053
1054 // undo the last loop iteration
1055 dividend = d;
1056 remainder >>= 1;
1057 nBits++;
1058
1059 for ( size_t i = 0; i < nBits; i++ )
1060 {
1061 remainder <<= 1;
1062 if ( IS_MSB_SET(dividend) )
1063 {
1064 remainder |= 1;
1065 }
1066
1067 wxULongLongWx t = remainder - divisor;
1068 dividend <<= 1;
1069 quotient <<= 1;
1070 if ( !IS_MSB_SET(t) )
1071 {
1072 quotient |= 1;
1073
1074 remainder = t;
1075 }
1076 }
1077 }
1078}
1079
2ea24d9f
VZ
1080wxLongLongWx wxLongLongWx::operator/(const wxLongLongWx& ll) const
1081{
1082 wxLongLongWx quotient, remainder;
1083
1084 Divide(ll, quotient, remainder);
1085
1086 return quotient;
1087}
1088
8e38fd1f
RL
1089wxULongLongWx wxULongLongWx::operator/(const wxULongLongWx& ll) const
1090{
1091 wxULongLongWx quotient, remainder;
1092
1093 Divide(ll, quotient, remainder);
1094
1095 return quotient;
1096}
1097
2ea24d9f
VZ
1098wxLongLongWx& wxLongLongWx::operator/=(const wxLongLongWx& ll)
1099{
1100 wxLongLongWx quotient, remainder;
1101
1102 Divide(ll, quotient, remainder);
1103
55dfa8d3 1104 *this = quotient;
3a994742 1105
55dfa8d3 1106 return *this;
8b81872f
VZ
1107}
1108
8e38fd1f
RL
1109wxULongLongWx& wxULongLongWx::operator/=(const wxULongLongWx& ll)
1110{
c35c7d01 1111 wxULongLongWx quotient, remainder;
8e38fd1f
RL
1112
1113 Divide(ll, quotient, remainder);
1114
1115 *this = quotient;
1116
1117 return *this;
1118}
1119
2ea24d9f
VZ
1120wxLongLongWx wxLongLongWx::operator%(const wxLongLongWx& ll) const
1121{
1122 wxLongLongWx quotient, remainder;
1123
1124 Divide(ll, quotient, remainder);
1125
1126 return remainder;
1127}
1128
8e38fd1f
RL
1129wxULongLongWx wxULongLongWx::operator%(const wxULongLongWx& ll) const
1130{
1131 wxULongLongWx quotient, remainder;
1132
1133 Divide(ll, quotient, remainder);
1134
1135 return remainder;
1136}
1137
2ea24d9f
VZ
1138// ----------------------------------------------------------------------------
1139// misc
1140// ----------------------------------------------------------------------------
1141
8b81872f
VZ
1142// temporary - just for testing
1143void *wxLongLongWx::asArray(void) const
1144{
1145 static unsigned char temp[8];
1146
13111b2a
VZ
1147 temp[0] = (char)((m_hi >> 24) & 0xFF);
1148 temp[1] = (char)((m_hi >> 16) & 0xFF);
1149 temp[2] = (char)((m_hi >> 8) & 0xFF);
1150 temp[3] = (char)((m_hi >> 0) & 0xFF);
1151 temp[4] = (char)((m_lo >> 24) & 0xFF);
1152 temp[5] = (char)((m_lo >> 16) & 0xFF);
1153 temp[6] = (char)((m_lo >> 8) & 0xFF);
1154 temp[7] = (char)((m_lo >> 0) & 0xFF);
8b81872f
VZ
1155
1156 return temp;
1157}
1158
8e38fd1f
RL
1159void *wxULongLongWx::asArray(void) const
1160{
1161 static unsigned char temp[8];
1162
1163 temp[0] = (char)((m_hi >> 24) & 0xFF);
1164 temp[1] = (char)((m_hi >> 16) & 0xFF);
1165 temp[2] = (char)((m_hi >> 8) & 0xFF);
1166 temp[3] = (char)((m_hi >> 0) & 0xFF);
1167 temp[4] = (char)((m_lo >> 24) & 0xFF);
1168 temp[5] = (char)((m_lo >> 16) & 0xFF);
1169 temp[6] = (char)((m_lo >> 8) & 0xFF);
1170 temp[7] = (char)((m_lo >> 0) & 0xFF);
1171
1172 return temp;
1173}
1174
3a994742 1175#endif // wxUSE_LONGLONG_WX
8b81872f 1176
842215bb
WS
1177#define LL_TO_STRING(name) \
1178 wxString name::ToString() const \
1179 { \
1180 /* TODO: this is awfully inefficient, anything better? */ \
1181 wxString result; \
1182 \
1183 name ll = *this; \
1184 \
21b9b5e2
VZ
1185 bool neg = ll < 0; \
1186 if ( neg ) \
842215bb 1187 { \
21b9b5e2
VZ
1188 while ( ll != 0 ) \
1189 { \
1190 long digit = (ll % 10).ToLong(); \
9a83f860 1191 result.Prepend((wxChar)(wxT('0') - digit)); \
21b9b5e2
VZ
1192 ll /= 10; \
1193 } \
842215bb
WS
1194 } \
1195 else \
1196 { \
21b9b5e2
VZ
1197 while ( ll != 0 ) \
1198 { \
1199 long digit = (ll % 10).ToLong(); \
9a83f860 1200 result.Prepend((wxChar)(wxT('0') + digit)); \
21b9b5e2
VZ
1201 ll /= 10; \
1202 } \
842215bb
WS
1203 } \
1204 \
1205 if ( result.empty() ) \
9a83f860 1206 result = wxT('0'); \
842215bb 1207 else if ( neg ) \
9a83f860 1208 result.Prepend(wxT('-')); \
842215bb
WS
1209 \
1210 return result; \
3a994742 1211 }
8b81872f 1212
842215bb
WS
1213#define ULL_TO_STRING(name) \
1214 wxString name::ToString() const \
1215 { \
1216 /* TODO: this is awfully inefficient, anything better? */ \
1217 wxString result; \
1218 \
1219 name ll = *this; \
1220 \
1221 while ( ll != 0 ) \
1222 { \
9a83f860 1223 result.Prepend((wxChar)(wxT('0') + (ll % 10).ToULong())); \
842215bb
WS
1224 ll /= 10; \
1225 } \
1226 \
1227 if ( result.empty() ) \
9a83f860 1228 result = wxT('0'); \
842215bb
WS
1229 \
1230 return result; \
8b81872f
VZ
1231 }
1232
8e38fd1f 1233#if wxUSE_LONGLONG_NATIVE
842215bb
WS
1234 LL_TO_STRING(wxLongLongNative)
1235 ULL_TO_STRING(wxULongLongNative)
8e38fd1f 1236#endif
8e38fd1f 1237
842215bb
WS
1238#if wxUSE_LONGLONG_WX
1239 LL_TO_STRING(wxLongLongWx)
1240 ULL_TO_STRING(wxULongLongWx)
1241#endif
8e38fd1f 1242
3a994742
VZ
1243#if wxUSE_STD_IOSTREAM
1244
1245// input/output
a353dc98 1246WXDLLIMPEXP_BASE
3a994742
VZ
1247wxSTD ostream& operator<< (wxSTD ostream& o, const wxLongLong& ll)
1248{
1249 return o << ll.ToString();
1250}
1251
a353dc98 1252WXDLLIMPEXP_BASE
8e38fd1f
RL
1253wxSTD ostream& operator<< (wxSTD ostream& o, const wxULongLong& ll)
1254{
1255 return o << ll.ToString();
1256}
1257
3a994742 1258#endif // wxUSE_STD_IOSTREAM
8b81872f 1259
059a684d 1260WXDLLIMPEXP_BASE wxString& operator<< (wxString& s, const wxLongLong& ll)
51496782
WS
1261{
1262 return s << ll.ToString();
1263}
1264
059a684d 1265WXDLLIMPEXP_BASE wxString& operator<< (wxString& s, const wxULongLong& ll)
51496782
WS
1266{
1267 return s << ll.ToString();
1268}
1269
216a72f3
VZ
1270#if wxUSE_STREAMS
1271
1272WXDLLIMPEXP_BASE wxTextOutputStream& operator<< (wxTextOutputStream& o, const wxULongLong& ll)
1273{
1274 return o << ll.ToString();
1275}
1276
1277WXDLLIMPEXP_BASE wxTextOutputStream& operator<< (wxTextOutputStream& o, const wxLongLong& ll)
1278{
1279 return o << ll.ToString();
1280}
1281
9a83f860 1282#define READ_STRING_CHAR(s, idx, len) ((idx!=len) ? (wxChar)s[idx++] : wxT('\0'))
216a72f3
VZ
1283
1284WXDLLIMPEXP_BASE class wxTextInputStream &operator>>(class wxTextInputStream &o, wxULongLong &ll)
1285{
1286 wxString s = o.ReadWord();
1287
1288 ll = wxULongLong(0l, 0l);
18680f86 1289 size_t length = s.length();
216a72f3
VZ
1290 size_t idx = 0;
1291
1292 wxChar ch = READ_STRING_CHAR(s, idx, length);
1293
1294 // Skip WS
1295 while (ch==wxT(' ') || ch==wxT('\t'))
1296 ch = READ_STRING_CHAR(s, idx, length);
1297
1298 // Read number
1299 wxULongLong multiplier(0l, 10l);
1300 while (ch>=wxT('0') && ch<=wxT('9')) {
1301 long lValue = (unsigned) (ch - wxT('0'));
1302 ll = ll * multiplier + wxULongLong(0l, lValue);
1303 ch = READ_STRING_CHAR(s, idx, length);
1304 }
1305
1306 return o;
1307}
1308
1309WXDLLIMPEXP_BASE class wxTextInputStream &operator>>(class wxTextInputStream &o, wxLongLong &ll)
1310{
1311 wxString s = o.ReadWord();
1312
1313 ll = wxLongLong(0l, 0l);
18680f86 1314 size_t length = s.length();
216a72f3
VZ
1315 size_t idx = 0;
1316
1317 wxChar ch = READ_STRING_CHAR(s, idx, length);
1318
1319 // Skip WS
1320 while (ch==wxT(' ') || ch==wxT('\t'))
1321 ch = READ_STRING_CHAR(s, idx, length);
1322
1323 // Ask for sign
1324 int iSign = 1;
1325 if (ch==wxT('-') || ch==wxT('+')) {
1326 iSign = ((ch==wxT('-')) ? -1 : 1);
1327 ch = READ_STRING_CHAR(s, idx, length);
1328 }
1329
1330 // Read number
1331 wxLongLong multiplier(0l, 10l);
1332 while (ch>=wxT('0') && ch<=wxT('9')) {
1333 long lValue = (unsigned) (ch - wxT('0'));
1334 ll = ll * multiplier + wxLongLong(0l, lValue);
1335 ch = READ_STRING_CHAR(s, idx, length);
1336 }
1337
1338#if wxUSE_LONGLONG_NATIVE
1339 ll = ll * wxLongLong((wxLongLong_t) iSign);
1340#else
1341 ll = ll * wxLongLong((long) iSign);
1342#endif
1343
1344 return o;
1345}
1346
1347#if wxUSE_LONGLONG_NATIVE
1348
1349WXDLLIMPEXP_BASE class wxTextOutputStream &operator<<(class wxTextOutputStream &o, wxULongLong_t value)
1350{
1351 return o << wxULongLong(value).ToString();
1352}
1353
1354WXDLLIMPEXP_BASE class wxTextOutputStream &operator<<(class wxTextOutputStream &o, wxLongLong_t value)
1355{
1356 return o << wxLongLong(value).ToString();
1357}
1358
1359WXDLLIMPEXP_BASE class wxTextInputStream &operator>>(class wxTextInputStream &o, wxULongLong_t &value)
1360{
1361 wxULongLong ll;
1362 o >> ll;
1363 value = ll.GetValue();
1364 return o;
1365}
1366
1367WXDLLIMPEXP_BASE class wxTextInputStream &operator>>(class wxTextInputStream &o, wxLongLong_t &value)
1368{
1369 wxLongLong ll;
1370 o >> ll;
1371 value = ll.GetValue();
1372 return o;
1373}
1374
1375#endif // wxUSE_LONGLONG_NATIVE
1376
1377#endif // wxUSE_STREAMS
1378
4e57b0d4 1379#endif // wxUSE_LONGLONG