Fixed old FIXME about unsigned result of sunstraction + a few changes to be able...
[wxWidgets.git] / src / common / longlong.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
19 #pragma implementation "longlong.h"
20 #endif
21
22 #include "wx/wxprec.h"
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #if wxUSE_LONGLONG
29 #include "wx/longlong.h"
30
31 #if defined(__MWERKS__) && defined(__WXMSW__)
32 #include <string.h> // for memset()
33 #else
34 #include <memory.h> // for memset()
35 #endif
36
37 #include <math.h> // for fabs()
38
39 // ============================================================================
40 // implementation
41 // ============================================================================
42
43 #if wxUSE_LONGLONG_NATIVE
44
45 // ----------------------------------------------------------------------------
46 // misc
47 // ----------------------------------------------------------------------------
48
49 void *wxLongLongNative::asArray() const
50 {
51 static unsigned char temp[8];
52
53 temp[0] = (unsigned char)((m_ll >> 56) & 0xFF);
54 temp[1] = (unsigned char)((m_ll >> 48) & 0xFF);
55 temp[2] = (unsigned char)((m_ll >> 40) & 0xFF);
56 temp[3] = (unsigned char)((m_ll >> 32) & 0xFF);
57 temp[4] = (unsigned char)((m_ll >> 24) & 0xFF);
58 temp[5] = (unsigned char)((m_ll >> 16) & 0xFF);
59 temp[6] = (unsigned char)((m_ll >> 8) & 0xFF);
60 temp[7] = (unsigned char)((m_ll >> 0) & 0xFF);
61
62 return temp;
63 }
64
65 void *wxULongLongNative::asArray() const
66 {
67 static unsigned char temp[8];
68
69 temp[0] = (unsigned char)((m_ll >> 56) & 0xFF);
70 temp[1] = (unsigned char)((m_ll >> 48) & 0xFF);
71 temp[2] = (unsigned char)((m_ll >> 40) & 0xFF);
72 temp[3] = (unsigned char)((m_ll >> 32) & 0xFF);
73 temp[4] = (unsigned char)((m_ll >> 24) & 0xFF);
74 temp[5] = (unsigned char)((m_ll >> 16) & 0xFF);
75 temp[6] = (unsigned char)((m_ll >> 8) & 0xFF);
76 temp[7] = (unsigned char)((m_ll >> 0) & 0xFF);
77
78 return temp;
79 }
80
81 #if wxUSE_LONGLONG_WX
82 wxLongLongNative::wxLongLongNative(wxLongLongWx ll)
83 {
84 // assign first to avoid precision loss!
85 m_ll = ll.GetHi();
86 m_ll <<= 32;
87 m_ll |= ll.GetLo();
88 }
89
90 wxLongLongNative& wxLongLongNative::operator=(wxLongLongWx ll)
91 {
92 // assign first to avoid precision loss!
93 m_ll = ll.GetHi();
94 m_ll <<= 32;
95 m_ll |= ll.GetLo();
96 return *this;
97 }
98 #endif
99
100 #endif // wxUSE_LONGLONG_NATIVE
101
102 // ============================================================================
103 // wxLongLongWx: emulation of 'long long' using 2 longs
104 // ============================================================================
105
106 #if wxUSE_LONGLONG_WX
107
108 // assignment
109 wxLongLongWx& wxLongLongWx::Assign(double d)
110 {
111 bool positive = d >= 0;
112 d = fabs(d);
113 if ( d <= ULONG_MAX )
114 {
115 m_hi = 0;
116 m_lo = (long)d;
117 }
118 else
119 {
120 m_hi = (unsigned long)(d / (1.0 + (double)ULONG_MAX));
121 m_lo = (unsigned long)(d - ((double)m_hi * (1.0 + (double)ULONG_MAX)));
122 }
123
124 #ifdef wxLONGLONG_TEST_MODE
125 m_ll = (wxLongLong_t)d;
126
127 Check();
128 #endif // wxLONGLONG_TEST_MODE
129
130 if ( !positive )
131 Negate();
132
133 return *this;
134 }
135
136 wxLongLongWx wxLongLongWx::operator<<(int shift) const
137 {
138 wxLongLongWx ll(*this);
139 ll <<= shift;
140
141 return ll;
142 }
143
144 wxULongLongWx wxULongLongWx::operator<<(int shift) const
145 {
146 wxULongLongWx ll(*this);
147 ll <<= shift;
148
149 return ll;
150 }
151
152 wxLongLongWx& wxLongLongWx::operator<<=(int shift)
153 {
154 if (shift != 0)
155 {
156 if (shift < 32)
157 {
158 m_hi <<= shift;
159 m_hi |= m_lo >> (32 - shift);
160 m_lo <<= shift;
161 }
162 else
163 {
164 m_hi = m_lo << (shift - 32);
165 m_lo = 0;
166 }
167 }
168
169 #ifdef wxLONGLONG_TEST_MODE
170 m_ll <<= shift;
171
172 Check();
173 #endif // wxLONGLONG_TEST_MODE
174
175 return *this;
176 }
177
178 wxULongLongWx& wxULongLongWx::operator<<=(int shift)
179 {
180 if (shift != 0)
181 {
182 if (shift < 32)
183 {
184 m_hi <<= shift;
185 m_hi |= m_lo >> (32 - shift);
186 m_lo <<= shift;
187 }
188 else
189 {
190 m_hi = m_lo << (shift - 32);
191 m_lo = 0;
192 }
193 }
194
195 #ifdef wxLONGLONG_TEST_MODE
196 m_ll <<= shift;
197
198 Check();
199 #endif // wxLONGLONG_TEST_MODE
200
201 return *this;
202 }
203
204 wxLongLongWx wxLongLongWx::operator>>(int shift) const
205 {
206 wxLongLongWx ll(*this);
207 ll >>= shift;
208
209 return ll;
210 }
211
212 wxULongLongWx wxULongLongWx::operator>>(int shift) const
213 {
214 wxULongLongWx ll(*this);
215 ll >>= shift;
216
217 return ll;
218 }
219
220 wxLongLongWx& wxLongLongWx::operator>>=(int shift)
221 {
222 if (shift != 0)
223 {
224 if (shift < 32)
225 {
226 m_lo >>= shift;
227 m_lo |= m_hi << (32 - shift);
228 m_hi >>= shift;
229 }
230 else
231 {
232 m_lo = m_hi >> (shift - 32);
233 m_hi = (m_hi < 0 ? -1L : 0);
234 }
235 }
236
237 #ifdef wxLONGLONG_TEST_MODE
238 m_ll >>= shift;
239
240 Check();
241 #endif // wxLONGLONG_TEST_MODE
242
243 return *this;
244 }
245
246 wxULongLongWx& wxULongLongWx::operator>>=(int shift)
247 {
248 if (shift != 0)
249 {
250 if (shift < 32)
251 {
252 m_lo >>= shift;
253 m_lo |= m_hi << (32 - shift);
254 m_hi >>= shift;
255 }
256 else
257 {
258 m_lo = m_hi >> (shift - 32);
259 m_hi = 0;
260 }
261 }
262
263 #ifdef wxLONGLONG_TEST_MODE
264 m_ll >>= shift;
265
266 Check();
267 #endif // wxLONGLONG_TEST_MODE
268
269 return *this;
270 }
271
272 wxLongLongWx wxLongLongWx::operator+(const wxLongLongWx& ll) const
273 {
274 wxLongLongWx res(*this);
275 res += ll;
276
277 return res;
278 }
279
280 wxULongLongWx wxULongLongWx::operator+(const wxULongLongWx& ll) const
281 {
282 wxULongLongWx res(*this);
283 res += ll;
284
285 return res;
286 }
287
288 wxLongLongWx wxLongLongWx::operator+(long l) const
289 {
290 wxLongLongWx res(*this);
291 res += l;
292
293 return res;
294 }
295
296 wxULongLongWx wxULongLongWx::operator+(unsigned long l) const
297 {
298 wxULongLongWx res(*this);
299 res += l;
300
301 return res;
302 }
303
304 wxLongLongWx& wxLongLongWx::operator+=(const wxLongLongWx& ll)
305 {
306 unsigned long previous = m_lo;
307
308 m_lo += ll.m_lo;
309 m_hi += ll.m_hi;
310
311 if ((m_lo < previous) || (m_lo < ll.m_lo))
312 m_hi++;
313
314 #ifdef wxLONGLONG_TEST_MODE
315 m_ll += ll.m_ll;
316
317 Check();
318 #endif // wxLONGLONG_TEST_MODE
319
320 return *this;
321 }
322
323 wxULongLongWx& wxULongLongWx::operator+=(const wxULongLongWx& ll)
324 {
325 unsigned long previous = m_lo;
326
327 m_lo += ll.m_lo;
328 m_hi += ll.m_hi;
329
330 if ((m_lo < previous) || (m_lo < ll.m_lo))
331 m_hi++;
332
333 #ifdef wxLONGLONG_TEST_MODE
334 m_ll += ll.m_ll;
335
336 Check();
337 #endif // wxLONGLONG_TEST_MODE
338
339 return *this;
340 }
341
342 wxLongLongWx& wxLongLongWx::operator+=(long l)
343 {
344 unsigned long previous = m_lo;
345
346 m_lo += l;
347 if (l < 0)
348 m_hi += -1l;
349
350 if ((m_lo < previous) || (m_lo < (unsigned long)l))
351 m_hi++;
352
353 #ifdef wxLONGLONG_TEST_MODE
354 m_ll += l;
355
356 Check();
357 #endif // wxLONGLONG_TEST_MODE
358
359 return *this;
360 }
361
362 wxULongLongWx& wxULongLongWx::operator+=(unsigned long l)
363 {
364 unsigned long previous = m_lo;
365
366 m_lo += l;
367
368 if ((m_lo < previous) || (m_lo < l))
369 m_hi++;
370
371 #ifdef wxLONGLONG_TEST_MODE
372 m_ll += l;
373
374 Check();
375 #endif // wxLONGLONG_TEST_MODE
376
377 return *this;
378 }
379
380 // pre increment
381 wxLongLongWx& wxLongLongWx::operator++()
382 {
383 m_lo++;
384 if (m_lo == 0)
385 m_hi++;
386
387 #ifdef wxLONGLONG_TEST_MODE
388 m_ll++;
389
390 Check();
391 #endif // wxLONGLONG_TEST_MODE
392
393 return *this;
394 }
395
396 wxULongLongWx& wxULongLongWx::operator++()
397 {
398 m_lo++;
399 if (m_lo == 0)
400 m_hi++;
401
402 #ifdef wxLONGLONG_TEST_MODE
403 m_ll++;
404
405 Check();
406 #endif // wxLONGLONG_TEST_MODE
407
408 return *this;
409 }
410
411 // negation
412 wxLongLongWx wxLongLongWx::operator-() const
413 {
414 wxLongLongWx res(*this);
415 res.Negate();
416
417 return res;
418 }
419
420 wxLongLongWx& wxLongLongWx::Negate()
421 {
422 m_hi = ~m_hi;
423 m_lo = ~m_lo;
424
425 m_lo++;
426 if ( m_lo == 0 )
427 m_hi++;
428
429 #ifdef wxLONGLONG_TEST_MODE
430 m_ll = -m_ll;
431
432 Check();
433 #endif // wxLONGLONG_TEST_MODE
434
435 return *this;
436 }
437
438 // subtraction
439
440 wxLongLongWx wxLongLongWx::operator-(const wxLongLongWx& ll) const
441 {
442 wxLongLongWx res(*this);
443 res -= ll;
444
445 return res;
446 }
447
448 wxLongLongWx wxULongLongWx::operator-(const wxULongLongWx& ll) const
449 {
450 wxASSERT(m_hi <= LONG_MAX );
451 wxASSERT(ll.m_hi <= LONG_MAX );
452
453 wxLongLongWx res( (long)m_hi , m_lo );
454 wxLongLongWx op( (long)ll.m_hi , ll.m_lo );
455 res -= op;
456
457 return res;
458 }
459
460 wxLongLongWx& wxLongLongWx::operator-=(const wxLongLongWx& ll)
461 {
462 unsigned long previous = m_lo;
463
464 m_lo -= ll.m_lo;
465 m_hi -= ll.m_hi;
466
467 if (previous < ll.m_lo)
468 m_hi--;
469
470 #ifdef wxLONGLONG_TEST_MODE
471 m_ll -= ll.m_ll;
472
473 Check();
474 #endif // wxLONGLONG_TEST_MODE
475
476 return *this;
477 }
478
479 wxULongLongWx& wxULongLongWx::operator-=(const wxULongLongWx& ll)
480 {
481 unsigned long previous = m_lo;
482
483 m_lo -= ll.m_lo;
484 m_hi -= ll.m_hi;
485
486 if (previous < ll.m_lo)
487 m_hi--;
488
489 #ifdef wxLONGLONG_TEST_MODE
490 m_ll -= ll.m_ll;
491
492 Check();
493 #endif // wxLONGLONG_TEST_MODE
494
495 return *this;
496 }
497
498 // pre decrement
499 wxLongLongWx& wxLongLongWx::operator--()
500 {
501 m_lo--;
502 if (m_lo == 0xFFFFFFFF)
503 m_hi--;
504
505 #ifdef wxLONGLONG_TEST_MODE
506 m_ll--;
507
508 Check();
509 #endif // wxLONGLONG_TEST_MODE
510
511 return *this;
512 }
513
514 wxULongLongWx& wxULongLongWx::operator--()
515 {
516 m_lo--;
517 if (m_lo == 0xFFFFFFFF)
518 m_hi--;
519
520 #ifdef wxLONGLONG_TEST_MODE
521 m_ll--;
522
523 Check();
524 #endif // wxLONGLONG_TEST_MODE
525
526 return *this;
527 }
528
529 // comparison operators
530
531 bool wxLongLongWx::operator<(const wxLongLongWx& ll) const
532 {
533 if ( m_hi < ll.m_hi )
534 return true;
535 else if ( m_hi == ll.m_hi )
536 return m_lo < ll.m_lo;
537 else
538 return false;
539 }
540
541 bool wxULongLongWx::operator<(const wxULongLongWx& ll) const
542 {
543 if ( m_hi < ll.m_hi )
544 return true;
545 else if ( m_hi == ll.m_hi )
546 return m_lo < ll.m_lo;
547 else
548 return false;
549 }
550
551 bool wxLongLongWx::operator>(const wxLongLongWx& ll) const
552 {
553 if ( m_hi > ll.m_hi )
554 return true;
555 else if ( m_hi == ll.m_hi )
556 return m_lo > ll.m_lo;
557 else
558 return false;
559 }
560
561 bool wxULongLongWx::operator>(const wxULongLongWx& ll) const
562 {
563 if ( m_hi > ll.m_hi )
564 return true;
565 else if ( m_hi == ll.m_hi )
566 return m_lo > ll.m_lo;
567 else
568 return false;
569 }
570
571 // bitwise operators
572
573 wxLongLongWx wxLongLongWx::operator&(const wxLongLongWx& ll) const
574 {
575 return wxLongLongWx(m_hi & ll.m_hi, m_lo & ll.m_lo);
576 }
577
578 wxULongLongWx wxULongLongWx::operator&(const wxULongLongWx& ll) const
579 {
580 return wxULongLongWx(m_hi & ll.m_hi, m_lo & ll.m_lo);
581 }
582
583 wxLongLongWx wxLongLongWx::operator|(const wxLongLongWx& ll) const
584 {
585 return wxLongLongWx(m_hi | ll.m_hi, m_lo | ll.m_lo);
586 }
587
588 wxULongLongWx wxULongLongWx::operator|(const wxULongLongWx& ll) const
589 {
590 return wxULongLongWx(m_hi | ll.m_hi, m_lo | ll.m_lo);
591 }
592
593 wxLongLongWx wxLongLongWx::operator^(const wxLongLongWx& ll) const
594 {
595 return wxLongLongWx(m_hi ^ ll.m_hi, m_lo ^ ll.m_lo);
596 }
597
598 wxULongLongWx wxULongLongWx::operator^(const wxULongLongWx& ll) const
599 {
600 return wxULongLongWx(m_hi ^ ll.m_hi, m_lo ^ ll.m_lo);
601 }
602
603 wxLongLongWx& wxLongLongWx::operator&=(const wxLongLongWx& ll)
604 {
605 m_lo &= ll.m_lo;
606 m_hi &= ll.m_hi;
607
608 #ifdef wxLONGLONG_TEST_MODE
609 m_ll &= ll.m_ll;
610
611 Check();
612 #endif // wxLONGLONG_TEST_MODE
613
614 return *this;
615 }
616
617 wxULongLongWx& wxULongLongWx::operator&=(const wxULongLongWx& ll)
618 {
619 m_lo &= ll.m_lo;
620 m_hi &= ll.m_hi;
621
622 #ifdef wxLONGLONG_TEST_MODE
623 m_ll &= ll.m_ll;
624
625 Check();
626 #endif // wxLONGLONG_TEST_MODE
627
628 return *this;
629 }
630
631 wxLongLongWx& wxLongLongWx::operator|=(const wxLongLongWx& ll)
632 {
633 m_lo |= ll.m_lo;
634 m_hi |= ll.m_hi;
635
636 #ifdef wxLONGLONG_TEST_MODE
637 m_ll |= ll.m_ll;
638
639 Check();
640 #endif // wxLONGLONG_TEST_MODE
641
642 return *this;
643 }
644
645 wxULongLongWx& wxULongLongWx::operator|=(const wxULongLongWx& ll)
646 {
647 m_lo |= ll.m_lo;
648 m_hi |= ll.m_hi;
649
650 #ifdef wxLONGLONG_TEST_MODE
651 m_ll |= ll.m_ll;
652
653 Check();
654 #endif // wxLONGLONG_TEST_MODE
655
656 return *this;
657 }
658
659 wxLongLongWx& wxLongLongWx::operator^=(const wxLongLongWx& ll)
660 {
661 m_lo ^= ll.m_lo;
662 m_hi ^= ll.m_hi;
663
664 #ifdef wxLONGLONG_TEST_MODE
665 m_ll ^= ll.m_ll;
666
667 Check();
668 #endif // wxLONGLONG_TEST_MODE
669
670 return *this;
671 }
672
673 wxULongLongWx& wxULongLongWx::operator^=(const wxULongLongWx& ll)
674 {
675 m_lo ^= ll.m_lo;
676 m_hi ^= ll.m_hi;
677
678 #ifdef wxLONGLONG_TEST_MODE
679 m_ll ^= ll.m_ll;
680
681 Check();
682 #endif // wxLONGLONG_TEST_MODE
683
684 return *this;
685 }
686
687 wxLongLongWx wxLongLongWx::operator~() const
688 {
689 return wxLongLongWx(~m_hi, ~m_lo);
690 }
691
692 wxULongLongWx wxULongLongWx::operator~() const
693 {
694 return wxULongLongWx(~m_hi, ~m_lo);
695 }
696
697 // multiplication
698
699 wxLongLongWx wxLongLongWx::operator*(const wxLongLongWx& ll) const
700 {
701 wxLongLongWx res(*this);
702 res *= ll;
703
704 return res;
705 }
706
707 wxULongLongWx wxULongLongWx::operator*(const wxULongLongWx& ll) const
708 {
709 wxULongLongWx res(*this);
710 res *= ll;
711
712 return res;
713 }
714
715 wxLongLongWx& wxLongLongWx::operator*=(const wxLongLongWx& ll)
716 {
717 wxLongLongWx t(m_hi, m_lo);
718 wxLongLongWx q(ll.m_hi, ll.m_lo);
719
720 m_hi = m_lo = 0;
721
722 #ifdef wxLONGLONG_TEST_MODE
723 wxLongLong_t llOld = m_ll;
724 m_ll = 0;
725 #endif // wxLONGLONG_TEST_MODE
726
727 int counter = 0;
728 do
729 {
730 if ((q.m_lo & 1) != 0)
731 *this += t;
732 q >>= 1;
733 t <<= 1;
734 counter++;
735 }
736 while ((counter < 64) && ((q.m_hi != 0) || (q.m_lo != 0)));
737
738 #ifdef wxLONGLONG_TEST_MODE
739 m_ll = llOld * ll.m_ll;
740
741 Check();
742 #endif // wxLONGLONG_TEST_MODE
743
744 return *this;
745 }
746
747 wxULongLongWx& wxULongLongWx::operator*=(const wxULongLongWx& ll)
748 {
749 wxULongLongWx t(m_hi, m_lo);
750 wxULongLongWx q(ll.m_hi, ll.m_lo);
751
752 m_hi = m_lo = 0;
753
754 #ifdef wxLONGLONG_TEST_MODE
755 unsigned wxLongLong_t llOld = m_ll;
756 m_ll = 0;
757 #endif // wxLONGLONG_TEST_MODE
758
759 int counter = 0;
760 do
761 {
762 if ((q.m_lo & 1) != 0)
763 *this += t;
764 q >>= 1;
765 t <<= 1;
766 counter++;
767 }
768 while ((counter < 64) && ((q.m_hi != 0) || (q.m_lo != 0)));
769
770 #ifdef wxLONGLONG_TEST_MODE
771 m_ll = llOld * ll.m_ll;
772
773 Check();
774 #endif // wxLONGLONG_TEST_MODE
775
776 return *this;
777 }
778
779 // division
780
781 void wxLongLongWx::Divide(const wxLongLongWx& divisorIn,
782 wxLongLongWx& quotient,
783 wxLongLongWx& remainder) const
784 {
785 if ((divisorIn.m_lo == 0) && (divisorIn.m_hi == 0))
786 {
787 // provoke division by zero error and silence the compilers warnings
788 // about an expression without effect and unused variable
789 long dummy = divisorIn.m_lo/divisorIn.m_hi;
790 dummy += 0;
791 }
792
793 // VZ: I'm writing this in a hurry and it's surely not the fastest way to
794 // do this - any improvements are more than welcome
795 //
796 // code inspired by the snippet at
797 // http://www.bearcave.com/software/divide.htm
798 //
799 // Copyright notice:
800 //
801 // Use of this program, for any purpose, is granted the author, Ian
802 // Kaplan, as long as this copyright notice is included in the source
803 // code or any source code derived from this program. The user assumes
804 // all responsibility for using this code.
805
806 // init everything
807 wxLongLongWx dividend = *this,
808 divisor = divisorIn;
809
810 quotient = 0l;
811 remainder = 0l;
812
813 // always do unsigned division and adjust the signs later: in C integer
814 // division, the sign of the remainder is the same as the sign of the
815 // dividend, while the sign of the quotient is the product of the signs of
816 // the dividend and divisor. Of course, we also always have
817 //
818 // dividend = quotient*divisor + remainder
819 //
820 // with 0 <= abs(remainder) < abs(divisor)
821 bool negRemainder = dividend.m_hi < 0;
822 bool negQuotient = false; // assume positive
823 if ( dividend.m_hi < 0 )
824 {
825 negQuotient = !negQuotient;
826 dividend = -dividend;
827 }
828 if ( divisor.m_hi < 0 )
829 {
830 negQuotient = !negQuotient;
831 divisor = -divisor;
832 }
833
834 // check for some particular cases
835 if ( divisor > dividend )
836 {
837 remainder = dividend;
838 }
839 else if ( divisor == dividend )
840 {
841 quotient = 1l;
842 }
843 else
844 {
845 // here: dividend > divisor and both are positive: do unsigned division
846 size_t nBits = 64u;
847 wxLongLongWx d;
848
849 #define IS_MSB_SET(ll) ((ll.m_hi) & (1 << (8*sizeof(long) - 1)))
850
851 while ( remainder < divisor )
852 {
853 remainder <<= 1;
854 if ( IS_MSB_SET(dividend) )
855 {
856 remainder |= 1;
857 }
858
859 d = dividend;
860 dividend <<= 1;
861
862 nBits--;
863 }
864
865 // undo the last loop iteration
866 dividend = d;
867 remainder >>= 1;
868 nBits++;
869
870 for ( size_t i = 0; i < nBits; i++ )
871 {
872 remainder <<= 1;
873 if ( IS_MSB_SET(dividend) )
874 {
875 remainder |= 1;
876 }
877
878 wxLongLongWx t = remainder - divisor;
879 dividend <<= 1;
880 quotient <<= 1;
881 if ( !IS_MSB_SET(t) )
882 {
883 quotient |= 1;
884
885 remainder = t;
886 }
887 }
888 }
889
890 // adjust signs
891 if ( negRemainder )
892 {
893 remainder = -remainder;
894 }
895
896 if ( negQuotient )
897 {
898 quotient = -quotient;
899 }
900 }
901
902 void wxULongLongWx::Divide(const wxULongLongWx& divisorIn,
903 wxULongLongWx& quotient,
904 wxULongLongWx& remainder) const
905 {
906 if ((divisorIn.m_lo == 0) && (divisorIn.m_hi == 0))
907 {
908 // provoke division by zero error and silence the compilers warnings
909 // about an expression without effect and unused variable
910 unsigned long dummy = divisorIn.m_lo/divisorIn.m_hi;
911 dummy += 0;
912 }
913
914 // VZ: I'm writing this in a hurry and it's surely not the fastest way to
915 // do this - any improvements are more than welcome
916 //
917 // code inspired by the snippet at
918 // http://www.bearcave.com/software/divide.htm
919 //
920 // Copyright notice:
921 //
922 // Use of this program, for any purpose, is granted the author, Ian
923 // Kaplan, as long as this copyright notice is included in the source
924 // code or any source code derived from this program. The user assumes
925 // all responsibility for using this code.
926
927 // init everything
928 wxULongLongWx dividend = *this,
929 divisor = divisorIn;
930
931 quotient = 0l;
932 remainder = 0l;
933
934 // check for some particular cases
935 if ( divisor > dividend )
936 {
937 remainder = dividend;
938 }
939 else if ( divisor == dividend )
940 {
941 quotient = 1l;
942 }
943 else
944 {
945 // here: dividend > divisor
946 size_t nBits = 64u;
947 wxULongLongWx d;
948
949 #define IS_MSB_SET(ll) ((ll.m_hi) & (1 << (8*sizeof(long) - 1)))
950
951 while ( remainder < divisor )
952 {
953 remainder <<= 1;
954 if ( IS_MSB_SET(dividend) )
955 {
956 remainder |= 1;
957 }
958
959 d = dividend;
960 dividend <<= 1;
961
962 nBits--;
963 }
964
965 // undo the last loop iteration
966 dividend = d;
967 remainder >>= 1;
968 nBits++;
969
970 for ( size_t i = 0; i < nBits; i++ )
971 {
972 remainder <<= 1;
973 if ( IS_MSB_SET(dividend) )
974 {
975 remainder |= 1;
976 }
977
978 wxULongLongWx t = remainder - divisor;
979 dividend <<= 1;
980 quotient <<= 1;
981 if ( !IS_MSB_SET(t) )
982 {
983 quotient |= 1;
984
985 remainder = t;
986 }
987 }
988 }
989 }
990
991 wxLongLongWx wxLongLongWx::operator/(const wxLongLongWx& ll) const
992 {
993 wxLongLongWx quotient, remainder;
994
995 Divide(ll, quotient, remainder);
996
997 return quotient;
998 }
999
1000 wxULongLongWx wxULongLongWx::operator/(const wxULongLongWx& ll) const
1001 {
1002 wxULongLongWx quotient, remainder;
1003
1004 Divide(ll, quotient, remainder);
1005
1006 return quotient;
1007 }
1008
1009 wxLongLongWx& wxLongLongWx::operator/=(const wxLongLongWx& ll)
1010 {
1011 wxLongLongWx quotient, remainder;
1012
1013 Divide(ll, quotient, remainder);
1014
1015 *this = quotient;
1016
1017 return *this;
1018 }
1019
1020 wxULongLongWx& wxULongLongWx::operator/=(const wxULongLongWx& ll)
1021 {
1022 wxULongLongWx quotient, remainder;
1023
1024 Divide(ll, quotient, remainder);
1025
1026 *this = quotient;
1027
1028 return *this;
1029 }
1030
1031 wxLongLongWx wxLongLongWx::operator%(const wxLongLongWx& ll) const
1032 {
1033 wxLongLongWx quotient, remainder;
1034
1035 Divide(ll, quotient, remainder);
1036
1037 return remainder;
1038 }
1039
1040 wxULongLongWx wxULongLongWx::operator%(const wxULongLongWx& ll) const
1041 {
1042 wxULongLongWx quotient, remainder;
1043
1044 Divide(ll, quotient, remainder);
1045
1046 return remainder;
1047 }
1048
1049 // ----------------------------------------------------------------------------
1050 // misc
1051 // ----------------------------------------------------------------------------
1052
1053 // temporary - just for testing
1054 void *wxLongLongWx::asArray(void) const
1055 {
1056 static unsigned char temp[8];
1057
1058 temp[0] = (char)((m_hi >> 24) & 0xFF);
1059 temp[1] = (char)((m_hi >> 16) & 0xFF);
1060 temp[2] = (char)((m_hi >> 8) & 0xFF);
1061 temp[3] = (char)((m_hi >> 0) & 0xFF);
1062 temp[4] = (char)((m_lo >> 24) & 0xFF);
1063 temp[5] = (char)((m_lo >> 16) & 0xFF);
1064 temp[6] = (char)((m_lo >> 8) & 0xFF);
1065 temp[7] = (char)((m_lo >> 0) & 0xFF);
1066
1067 return temp;
1068 }
1069
1070 void *wxULongLongWx::asArray(void) const
1071 {
1072 static unsigned char temp[8];
1073
1074 temp[0] = (char)((m_hi >> 24) & 0xFF);
1075 temp[1] = (char)((m_hi >> 16) & 0xFF);
1076 temp[2] = (char)((m_hi >> 8) & 0xFF);
1077 temp[3] = (char)((m_hi >> 0) & 0xFF);
1078 temp[4] = (char)((m_lo >> 24) & 0xFF);
1079 temp[5] = (char)((m_lo >> 16) & 0xFF);
1080 temp[6] = (char)((m_lo >> 8) & 0xFF);
1081 temp[7] = (char)((m_lo >> 0) & 0xFF);
1082
1083 return temp;
1084 }
1085
1086 #endif // wxUSE_LONGLONG_WX
1087
1088 #define LL_TO_STRING(name) \
1089 wxString name::ToString() const \
1090 { \
1091 /* TODO: this is awfully inefficient, anything better? */ \
1092 wxString result; \
1093 \
1094 name ll = *this; \
1095 \
1096 bool neg; \
1097 if ( ll < 0 ) \
1098 { \
1099 ll.Negate(); \
1100 neg = true; \
1101 } \
1102 else \
1103 { \
1104 neg = false; \
1105 } \
1106 \
1107 while ( ll != 0 ) \
1108 { \
1109 result.Prepend((wxChar)(_T('0') + (ll % 10).ToLong())); \
1110 ll /= 10; \
1111 } \
1112 \
1113 if ( result.empty() ) \
1114 result = _T('0'); \
1115 else if ( neg ) \
1116 result.Prepend(_T('-')); \
1117 \
1118 return result; \
1119 }
1120
1121 #define ULL_TO_STRING(name) \
1122 wxString name::ToString() const \
1123 { \
1124 /* TODO: this is awfully inefficient, anything better? */ \
1125 wxString result; \
1126 \
1127 name ll = *this; \
1128 \
1129 while ( ll != 0 ) \
1130 { \
1131 result.Prepend((wxChar)(_T('0') + (ll % 10).ToULong())); \
1132 ll /= 10; \
1133 } \
1134 \
1135 if ( result.empty() ) \
1136 result = _T('0'); \
1137 \
1138 return result; \
1139 }
1140
1141 #if wxUSE_LONGLONG_NATIVE
1142 LL_TO_STRING(wxLongLongNative)
1143 ULL_TO_STRING(wxULongLongNative)
1144 #endif
1145
1146 #if wxUSE_LONGLONG_WX
1147 LL_TO_STRING(wxLongLongWx)
1148 ULL_TO_STRING(wxULongLongWx)
1149 #endif
1150
1151 #if wxUSE_STD_IOSTREAM
1152
1153 // input/output
1154 wxSTD ostream& operator<< (wxSTD ostream& o, const wxLongLong& ll)
1155 {
1156 return o << ll.ToString();
1157 }
1158
1159 wxSTD ostream& operator<< (wxSTD ostream& o, const wxULongLong& ll)
1160 {
1161 return o << ll.ToString();
1162 }
1163
1164 #endif // wxUSE_STD_IOSTREAM
1165
1166 #endif // wxUSE_LONGLONG