]> git.saurik.com Git - wxWidgets.git/blob - src/common/longlong.cpp
remove unnecessary class definitions and variables
[wxWidgets.git] / src / common / longlong.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/longlong.cpp
3 // Purpose: implementation of wxLongLongNative
4 // Author: Jeffrey C. Ollie <jeff@ollie.clive.ia.us>, Vadim Zeitlin
5 // Remarks: this class is not public in wxWidgets 2.0! It is intentionally
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>
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
13
14 // ============================================================================
15 // headers
16 // ============================================================================
17
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #if wxUSE_LONGLONG
25
26 #include "wx/longlong.h"
27
28 #ifndef WX_PRECOMP
29 #include "wx/math.h" // for fabs()
30 #endif
31
32 #if wxUSE_STREAMS
33 #include "wx/txtstrm.h"
34 #endif
35
36 #include <string.h> // for memset()
37
38 #include "wx/ioswrap.h"
39
40 // ============================================================================
41 // implementation
42 // ============================================================================
43
44 #if wxUSE_LONGLONG_NATIVE
45
46 // ----------------------------------------------------------------------------
47 // misc
48 // ----------------------------------------------------------------------------
49
50 void *wxLongLongNative::asArray() const
51 {
52 static unsigned char temp[8];
53
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));
62
63 return temp;
64 }
65
66 void *wxULongLongNative::asArray() const
67 {
68 static unsigned char temp[8];
69
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));
78
79 return temp;
80 }
81
82 #if wxUSE_LONGLONG_WX
83 wxLongLongNative::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
91 wxLongLongNative& 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 }
99
100 wxLongLongNative& 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
109 wxULongLongNative::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
117 wxULongLongNative& 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
126 wxULongLongNative& 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 }
134 #endif
135
136 #ifdef __VISUALC6__
137 double 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
152 #endif // wxUSE_LONGLONG_NATIVE
153
154 // ============================================================================
155 // wxLongLongWx: emulation of 'long long' using 2 longs
156 // ============================================================================
157
158 #if wxUSE_LONGLONG_WX
159
160 // Set value from unsigned wxULongLongWx
161 wxLongLongWx &wxLongLongWx::operator=(const class wxULongLongWx &ll)
162 {
163 m_hi = (unsigned long) ll.GetHi();
164 m_lo = ll.GetLo();
165 return *this;
166 }
167
168 // assignment
169 wxLongLongWx& wxLongLongWx::Assign(double d)
170 {
171 bool positive = d >= 0;
172 d = fabs(d);
173 if ( d <= ULONG_MAX )
174 {
175 m_hi = 0;
176 m_lo = (long)d;
177 }
178 else
179 {
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)));
182 }
183
184 #ifdef wxLONGLONG_TEST_MODE
185 m_ll = (wxLongLong_t)d;
186
187 Check();
188 #endif // wxLONGLONG_TEST_MODE
189
190 if ( !positive )
191 Negate();
192
193 return *this;
194 }
195
196 double 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
209 double 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
222 wxLongLongWx wxLongLongWx::operator<<(int shift) const
223 {
224 wxLongLongWx ll(*this);
225 ll <<= shift;
226
227 return ll;
228 }
229
230 wxULongLongWx wxULongLongWx::operator<<(int shift) const
231 {
232 wxULongLongWx ll(*this);
233 ll <<= shift;
234
235 return ll;
236 }
237
238 wxLongLongWx& wxLongLongWx::operator<<=(int shift)
239 {
240 if (shift != 0)
241 {
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 }
253 }
254
255 #ifdef wxLONGLONG_TEST_MODE
256 m_ll <<= shift;
257
258 Check();
259 #endif // wxLONGLONG_TEST_MODE
260
261 return *this;
262 }
263
264 wxULongLongWx& 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
290 wxLongLongWx wxLongLongWx::operator>>(int shift) const
291 {
292 wxLongLongWx ll(*this);
293 ll >>= shift;
294
295 return ll;
296 }
297
298 wxULongLongWx wxULongLongWx::operator>>(int shift) const
299 {
300 wxULongLongWx ll(*this);
301 ll >>= shift;
302
303 return ll;
304 }
305
306 wxLongLongWx& wxLongLongWx::operator>>=(int shift)
307 {
308 if (shift != 0)
309 {
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 }
321 }
322
323 #ifdef wxLONGLONG_TEST_MODE
324 m_ll >>= shift;
325
326 Check();
327 #endif // wxLONGLONG_TEST_MODE
328
329 return *this;
330 }
331
332 wxULongLongWx& 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
358 wxLongLongWx wxLongLongWx::operator+(const wxLongLongWx& ll) const
359 {
360 wxLongLongWx res(*this);
361 res += ll;
362
363 return res;
364 }
365
366 wxULongLongWx wxULongLongWx::operator+(const wxULongLongWx& ll) const
367 {
368 wxULongLongWx res(*this);
369 res += ll;
370
371 return res;
372 }
373
374 wxLongLongWx wxLongLongWx::operator+(long l) const
375 {
376 wxLongLongWx res(*this);
377 res += l;
378
379 return res;
380 }
381
382 wxULongLongWx wxULongLongWx::operator+(unsigned long l) const
383 {
384 wxULongLongWx res(*this);
385 res += l;
386
387 return res;
388 }
389
390 wxLongLongWx& 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
400 #ifdef wxLONGLONG_TEST_MODE
401 m_ll += ll.m_ll;
402
403 Check();
404 #endif // wxLONGLONG_TEST_MODE
405
406 return *this;
407 }
408
409 wxULongLongWx& 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
428 wxLongLongWx& 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
439 #ifdef wxLONGLONG_TEST_MODE
440 m_ll += l;
441
442 Check();
443 #endif // wxLONGLONG_TEST_MODE
444
445 return *this;
446 }
447
448 wxULongLongWx& 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
466 // pre increment
467 wxLongLongWx& wxLongLongWx::operator++()
468 {
469 m_lo++;
470 if (m_lo == 0)
471 m_hi++;
472
473 #ifdef wxLONGLONG_TEST_MODE
474 m_ll++;
475
476 Check();
477 #endif // wxLONGLONG_TEST_MODE
478
479 return *this;
480 }
481
482 wxULongLongWx& 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
497 // negation
498 wxLongLongWx wxLongLongWx::operator-() const
499 {
500 wxLongLongWx res(*this);
501 res.Negate();
502
503 return res;
504 }
505
506 wxLongLongWx& 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++;
514
515 #ifdef wxLONGLONG_TEST_MODE
516 m_ll = -m_ll;
517
518 Check();
519 #endif // wxLONGLONG_TEST_MODE
520
521 return *this;
522 }
523
524 // subtraction
525
526 wxLongLongWx wxLongLongWx::operator-(const wxLongLongWx& ll) const
527 {
528 wxLongLongWx res(*this);
529 res -= ll;
530
531 return res;
532 }
533
534 wxLongLongWx wxULongLongWx::operator-(const wxULongLongWx& ll) const
535 {
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;
542
543 return res;
544 }
545
546 wxLongLongWx& 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
556 #ifdef wxLONGLONG_TEST_MODE
557 m_ll -= ll.m_ll;
558
559 Check();
560 #endif // wxLONGLONG_TEST_MODE
561
562 return *this;
563 }
564
565 wxULongLongWx& 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
584 // pre decrement
585 wxLongLongWx& wxLongLongWx::operator--()
586 {
587 m_lo--;
588 if (m_lo == 0xFFFFFFFF)
589 m_hi--;
590
591 #ifdef wxLONGLONG_TEST_MODE
592 m_ll--;
593
594 Check();
595 #endif // wxLONGLONG_TEST_MODE
596
597 return *this;
598 }
599
600 wxULongLongWx& 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
615 // comparison operators
616
617 bool wxLongLongWx::operator<(const wxLongLongWx& ll) const
618 {
619 if ( m_hi < ll.m_hi )
620 return true;
621 else if ( m_hi == ll.m_hi )
622 return m_lo < ll.m_lo;
623 else
624 return false;
625 }
626
627 bool wxULongLongWx::operator<(const wxULongLongWx& ll) const
628 {
629 if ( m_hi < ll.m_hi )
630 return true;
631 else if ( m_hi == ll.m_hi )
632 return m_lo < ll.m_lo;
633 else
634 return false;
635 }
636
637 bool wxLongLongWx::operator>(const wxLongLongWx& ll) const
638 {
639 if ( m_hi > ll.m_hi )
640 return true;
641 else if ( m_hi == ll.m_hi )
642 return m_lo > ll.m_lo;
643 else
644 return false;
645 }
646
647 bool wxULongLongWx::operator>(const wxULongLongWx& ll) const
648 {
649 if ( m_hi > ll.m_hi )
650 return true;
651 else if ( m_hi == ll.m_hi )
652 return m_lo > ll.m_lo;
653 else
654 return false;
655 }
656
657 // bitwise operators
658
659 wxLongLongWx wxLongLongWx::operator&(const wxLongLongWx& ll) const
660 {
661 return wxLongLongWx(m_hi & ll.m_hi, m_lo & ll.m_lo);
662 }
663
664 wxULongLongWx wxULongLongWx::operator&(const wxULongLongWx& ll) const
665 {
666 return wxULongLongWx(m_hi & ll.m_hi, m_lo & ll.m_lo);
667 }
668
669 wxLongLongWx wxLongLongWx::operator|(const wxLongLongWx& ll) const
670 {
671 return wxLongLongWx(m_hi | ll.m_hi, m_lo | ll.m_lo);
672 }
673
674 wxULongLongWx wxULongLongWx::operator|(const wxULongLongWx& ll) const
675 {
676 return wxULongLongWx(m_hi | ll.m_hi, m_lo | ll.m_lo);
677 }
678
679 wxLongLongWx wxLongLongWx::operator^(const wxLongLongWx& ll) const
680 {
681 return wxLongLongWx(m_hi ^ ll.m_hi, m_lo ^ ll.m_lo);
682 }
683
684 wxULongLongWx wxULongLongWx::operator^(const wxULongLongWx& ll) const
685 {
686 return wxULongLongWx(m_hi ^ ll.m_hi, m_lo ^ ll.m_lo);
687 }
688
689 wxLongLongWx& wxLongLongWx::operator&=(const wxLongLongWx& ll)
690 {
691 m_lo &= ll.m_lo;
692 m_hi &= ll.m_hi;
693
694 #ifdef wxLONGLONG_TEST_MODE
695 m_ll &= ll.m_ll;
696
697 Check();
698 #endif // wxLONGLONG_TEST_MODE
699
700 return *this;
701 }
702
703 wxULongLongWx& 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
717 wxLongLongWx& wxLongLongWx::operator|=(const wxLongLongWx& ll)
718 {
719 m_lo |= ll.m_lo;
720 m_hi |= ll.m_hi;
721
722 #ifdef wxLONGLONG_TEST_MODE
723 m_ll |= ll.m_ll;
724
725 Check();
726 #endif // wxLONGLONG_TEST_MODE
727
728 return *this;
729 }
730
731 wxULongLongWx& 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
745 wxLongLongWx& wxLongLongWx::operator^=(const wxLongLongWx& ll)
746 {
747 m_lo ^= ll.m_lo;
748 m_hi ^= ll.m_hi;
749
750 #ifdef wxLONGLONG_TEST_MODE
751 m_ll ^= ll.m_ll;
752
753 Check();
754 #endif // wxLONGLONG_TEST_MODE
755
756 return *this;
757 }
758
759 wxULongLongWx& 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
773 wxLongLongWx wxLongLongWx::operator~() const
774 {
775 return wxLongLongWx(~m_hi, ~m_lo);
776 }
777
778 wxULongLongWx wxULongLongWx::operator~() const
779 {
780 return wxULongLongWx(~m_hi, ~m_lo);
781 }
782
783 // multiplication
784
785 wxLongLongWx wxLongLongWx::operator*(const wxLongLongWx& ll) const
786 {
787 wxLongLongWx res(*this);
788 res *= ll;
789
790 return res;
791 }
792
793 wxULongLongWx wxULongLongWx::operator*(const wxULongLongWx& ll) const
794 {
795 wxULongLongWx res(*this);
796 res *= ll;
797
798 return res;
799 }
800
801 wxLongLongWx& wxLongLongWx::operator*=(const wxLongLongWx& ll)
802 {
803 wxLongLongWx t(m_hi, m_lo);
804 wxLongLongWx q(ll.m_hi, ll.m_lo);
805
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;
814 do
815 {
816 if ((q.m_lo & 1) != 0)
817 *this += t;
818 q >>= 1;
819 t <<= 1;
820 counter++;
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
830 return *this;
831 }
832
833 wxULongLongWx& 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
841 wxULongLong_t llOld = m_ll;
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
865 // division
866
867 #define IS_MSB_SET(ll) ((ll.GetHi()) & (1 << (8*sizeof(long) - 1)))
868
869 void wxLongLongWx::Divide(const wxLongLongWx& divisorIn,
870 wxLongLongWx& quotient,
871 wxLongLongWx& remainderIO) const
872 {
873 if ((divisorIn.m_lo == 0) && (divisorIn.m_hi == 0))
874 {
875 // provoke division by zero error and silence the compilers warnings
876 // about an expression without effect and unused variable
877 long dummy = divisorIn.m_lo/divisorIn.m_hi;
878 dummy += 0;
879 }
880
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
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
892 // all responsibility for using this code.
893
894 // init everything
895 wxULongLongWx dividend, divisor, remainder;
896
897 quotient = 0l;
898 remainder = 0l;
899
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)
908 bool negRemainder = GetHi() < 0;
909 bool negQuotient = false; // assume positive
910 if ( GetHi() < 0 )
911 {
912 negQuotient = !negQuotient;
913 dividend = -*this;
914 } else {
915 dividend = *this;
916 }
917 if ( divisorIn.GetHi() < 0 )
918 {
919 negQuotient = !negQuotient;
920 divisor = -divisorIn;
921 } else {
922 divisor = divisorIn;
923 }
924
925 // check for some particular cases
926 if ( divisor > dividend )
927 {
928 remainder = dividend;
929 }
930 else if ( divisor == dividend )
931 {
932 quotient = 1l;
933 }
934 else
935 {
936 // here: dividend > divisor and both are positive: do unsigned division
937 size_t nBits = 64u;
938 wxLongLongWx d;
939
940 while ( remainder < divisor )
941 {
942 remainder <<= 1;
943 if ( IS_MSB_SET(dividend) )
944 {
945 remainder |= 1;
946 }
947
948 d = dividend;
949 dividend <<= 1;
950
951 nBits--;
952 }
953
954 // undo the last loop iteration
955 dividend = d;
956 remainder >>= 1;
957 nBits++;
958
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 }
976 }
977 }
978
979 remainderIO = remainder;
980
981 // adjust signs
982 if ( negRemainder )
983 {
984 remainderIO = -remainderIO;
985 }
986
987 if ( negQuotient )
988 {
989 quotient = -quotient;
990 }
991 }
992
993 void 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
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
1080 wxLongLongWx wxLongLongWx::operator/(const wxLongLongWx& ll) const
1081 {
1082 wxLongLongWx quotient, remainder;
1083
1084 Divide(ll, quotient, remainder);
1085
1086 return quotient;
1087 }
1088
1089 wxULongLongWx wxULongLongWx::operator/(const wxULongLongWx& ll) const
1090 {
1091 wxULongLongWx quotient, remainder;
1092
1093 Divide(ll, quotient, remainder);
1094
1095 return quotient;
1096 }
1097
1098 wxLongLongWx& wxLongLongWx::operator/=(const wxLongLongWx& ll)
1099 {
1100 wxLongLongWx quotient, remainder;
1101
1102 Divide(ll, quotient, remainder);
1103
1104 *this = quotient;
1105
1106 return *this;
1107 }
1108
1109 wxULongLongWx& wxULongLongWx::operator/=(const wxULongLongWx& ll)
1110 {
1111 wxULongLongWx quotient, remainder;
1112
1113 Divide(ll, quotient, remainder);
1114
1115 *this = quotient;
1116
1117 return *this;
1118 }
1119
1120 wxLongLongWx wxLongLongWx::operator%(const wxLongLongWx& ll) const
1121 {
1122 wxLongLongWx quotient, remainder;
1123
1124 Divide(ll, quotient, remainder);
1125
1126 return remainder;
1127 }
1128
1129 wxULongLongWx wxULongLongWx::operator%(const wxULongLongWx& ll) const
1130 {
1131 wxULongLongWx quotient, remainder;
1132
1133 Divide(ll, quotient, remainder);
1134
1135 return remainder;
1136 }
1137
1138 // ----------------------------------------------------------------------------
1139 // misc
1140 // ----------------------------------------------------------------------------
1141
1142 // temporary - just for testing
1143 void *wxLongLongWx::asArray(void) const
1144 {
1145 static unsigned char temp[8];
1146
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);
1155
1156 return temp;
1157 }
1158
1159 void *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
1175 #endif // wxUSE_LONGLONG_WX
1176
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 \
1185 bool neg = ll < 0; \
1186 if ( neg ) \
1187 { \
1188 while ( ll != 0 ) \
1189 { \
1190 long digit = (ll % 10).ToLong(); \
1191 result.Prepend((wxChar)(wxT('0') - digit)); \
1192 ll /= 10; \
1193 } \
1194 } \
1195 else \
1196 { \
1197 while ( ll != 0 ) \
1198 { \
1199 long digit = (ll % 10).ToLong(); \
1200 result.Prepend((wxChar)(wxT('0') + digit)); \
1201 ll /= 10; \
1202 } \
1203 } \
1204 \
1205 if ( result.empty() ) \
1206 result = wxT('0'); \
1207 else if ( neg ) \
1208 result.Prepend(wxT('-')); \
1209 \
1210 return result; \
1211 }
1212
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 { \
1223 result.Prepend((wxChar)(wxT('0') + (ll % 10).ToULong())); \
1224 ll /= 10; \
1225 } \
1226 \
1227 if ( result.empty() ) \
1228 result = wxT('0'); \
1229 \
1230 return result; \
1231 }
1232
1233 #if wxUSE_LONGLONG_NATIVE
1234 LL_TO_STRING(wxLongLongNative)
1235 ULL_TO_STRING(wxULongLongNative)
1236 #endif
1237
1238 #if wxUSE_LONGLONG_WX
1239 LL_TO_STRING(wxLongLongWx)
1240 ULL_TO_STRING(wxULongLongWx)
1241 #endif
1242
1243 #if wxUSE_STD_IOSTREAM
1244
1245 // input/output
1246 WXDLLIMPEXP_BASE
1247 wxSTD ostream& operator<< (wxSTD ostream& o, const wxLongLong& ll)
1248 {
1249 return o << ll.ToString();
1250 }
1251
1252 WXDLLIMPEXP_BASE
1253 wxSTD ostream& operator<< (wxSTD ostream& o, const wxULongLong& ll)
1254 {
1255 return o << ll.ToString();
1256 }
1257
1258 #endif // wxUSE_STD_IOSTREAM
1259
1260 WXDLLIMPEXP_BASE wxString& operator<< (wxString& s, const wxLongLong& ll)
1261 {
1262 return s << ll.ToString();
1263 }
1264
1265 WXDLLIMPEXP_BASE wxString& operator<< (wxString& s, const wxULongLong& ll)
1266 {
1267 return s << ll.ToString();
1268 }
1269
1270 #if wxUSE_STREAMS
1271
1272 WXDLLIMPEXP_BASE wxTextOutputStream& operator<< (wxTextOutputStream& o, const wxULongLong& ll)
1273 {
1274 return o << ll.ToString();
1275 }
1276
1277 WXDLLIMPEXP_BASE wxTextOutputStream& operator<< (wxTextOutputStream& o, const wxLongLong& ll)
1278 {
1279 return o << ll.ToString();
1280 }
1281
1282 #define READ_STRING_CHAR(s, idx, len) ((idx!=len) ? (wxChar)s[idx++] : wxT('\0'))
1283
1284 WXDLLIMPEXP_BASE class wxTextInputStream &operator>>(class wxTextInputStream &o, wxULongLong &ll)
1285 {
1286 wxString s = o.ReadWord();
1287
1288 ll = wxULongLong(0l, 0l);
1289 size_t length = s.length();
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
1309 WXDLLIMPEXP_BASE class wxTextInputStream &operator>>(class wxTextInputStream &o, wxLongLong &ll)
1310 {
1311 wxString s = o.ReadWord();
1312
1313 ll = wxLongLong(0l, 0l);
1314 size_t length = s.length();
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
1349 WXDLLIMPEXP_BASE class wxTextOutputStream &operator<<(class wxTextOutputStream &o, wxULongLong_t value)
1350 {
1351 return o << wxULongLong(value).ToString();
1352 }
1353
1354 WXDLLIMPEXP_BASE class wxTextOutputStream &operator<<(class wxTextOutputStream &o, wxLongLong_t value)
1355 {
1356 return o << wxLongLong(value).ToString();
1357 }
1358
1359 WXDLLIMPEXP_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
1367 WXDLLIMPEXP_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
1379 #endif // wxUSE_LONGLONG