]>
Commit | Line | Data |
---|---|---|
8b81872f VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/longlong.cpp | |
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 | ||
14f355c2 | 18 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
8b81872f VZ |
19 | #pragma implementation "longlong.h" |
20 | #endif | |
21 | ||
22 | #include "wx/wxprec.h" | |
23 | ||
24 | #ifdef __BORLANDC__ | |
25 | #pragma hdrstop | |
26 | #endif | |
27 | ||
4e57b0d4 | 28 | #if wxUSE_LONGLONG |
8b81872f VZ |
29 | #include "wx/longlong.h" |
30 | ||
de85a884 VZ |
31 | #if defined(__MWERKS__) && defined(__WXMSW__) |
32 | #include <string.h> // for memset() | |
33 | #else | |
8b81872f | 34 | #include <memory.h> // for memset() |
de85a884 VZ |
35 | #endif |
36 | ||
41acf5c0 | 37 | #include <math.h> // for fabs() |
8b81872f VZ |
38 | |
39 | // ============================================================================ | |
40 | // implementation | |
41 | // ============================================================================ | |
42 | ||
43 | #if wxUSE_LONGLONG_NATIVE | |
44 | ||
45 | // ---------------------------------------------------------------------------- | |
46 | // misc | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
2a310492 | 49 | void *wxLongLongNative::asArray() const |
8b81872f VZ |
50 | { |
51 | static unsigned char temp[8]; | |
52 | ||
0203c8cd WS |
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); | |
8b81872f VZ |
61 | |
62 | return temp; | |
63 | } | |
64 | ||
8e38fd1f RL |
65 | void *wxULongLongNative::asArray() const |
66 | { | |
67 | static unsigned char temp[8]; | |
68 | ||
0203c8cd WS |
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); | |
8e38fd1f RL |
77 | |
78 | return temp; | |
79 | } | |
80 | ||
842215bb WS |
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 | ||
8b81872f VZ |
100 | #endif // wxUSE_LONGLONG_NATIVE |
101 | ||
41acf5c0 VZ |
102 | // ============================================================================ |
103 | // wxLongLongWx: emulation of 'long long' using 2 longs | |
104 | // ============================================================================ | |
105 | ||
8b81872f VZ |
106 | #if wxUSE_LONGLONG_WX |
107 | ||
41acf5c0 VZ |
108 | // assignment |
109 | wxLongLongWx& wxLongLongWx::Assign(double d) | |
110 | { | |
2a310492 VZ |
111 | bool positive = d >= 0; |
112 | d = fabs(d); | |
617ec456 | 113 | if ( d <= ULONG_MAX ) |
41acf5c0 | 114 | { |
2a310492 | 115 | m_hi = 0; |
41acf5c0 VZ |
116 | m_lo = (long)d; |
117 | } | |
118 | else | |
119 | { | |
617ec456 VZ |
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))); | |
41acf5c0 VZ |
122 | } |
123 | ||
2a310492 VZ |
124 | #ifdef wxLONGLONG_TEST_MODE |
125 | m_ll = (wxLongLong_t)d; | |
126 | ||
127 | Check(); | |
128 | #endif // wxLONGLONG_TEST_MODE | |
129 | ||
617ec456 VZ |
130 | if ( !positive ) |
131 | Negate(); | |
132 | ||
41acf5c0 VZ |
133 | return *this; |
134 | } | |
135 | ||
8b81872f VZ |
136 | wxLongLongWx wxLongLongWx::operator<<(int shift) const |
137 | { | |
2a310492 VZ |
138 | wxLongLongWx ll(*this); |
139 | ll <<= shift; | |
8b81872f | 140 | |
2a310492 | 141 | return ll; |
8b81872f VZ |
142 | } |
143 | ||
8e38fd1f RL |
144 | wxULongLongWx wxULongLongWx::operator<<(int shift) const |
145 | { | |
146 | wxULongLongWx ll(*this); | |
147 | ll <<= shift; | |
148 | ||
149 | return ll; | |
150 | } | |
151 | ||
8b81872f VZ |
152 | wxLongLongWx& wxLongLongWx::operator<<=(int shift) |
153 | { | |
2a310492 | 154 | if (shift != 0) |
8b81872f | 155 | { |
2a310492 VZ |
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 | } | |
8b81872f VZ |
167 | } |
168 | ||
2a310492 VZ |
169 | #ifdef wxLONGLONG_TEST_MODE |
170 | m_ll <<= shift; | |
171 | ||
172 | Check(); | |
173 | #endif // wxLONGLONG_TEST_MODE | |
174 | ||
8b81872f VZ |
175 | return *this; |
176 | } | |
177 | ||
8e38fd1f RL |
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 | ||
8b81872f VZ |
204 | wxLongLongWx wxLongLongWx::operator>>(int shift) const |
205 | { | |
2a310492 VZ |
206 | wxLongLongWx ll(*this); |
207 | ll >>= shift; | |
8b81872f | 208 | |
2a310492 | 209 | return ll; |
8b81872f VZ |
210 | } |
211 | ||
8e38fd1f RL |
212 | wxULongLongWx wxULongLongWx::operator>>(int shift) const |
213 | { | |
214 | wxULongLongWx ll(*this); | |
215 | ll >>= shift; | |
216 | ||
217 | return ll; | |
218 | } | |
219 | ||
8b81872f VZ |
220 | wxLongLongWx& wxLongLongWx::operator>>=(int shift) |
221 | { | |
2a310492 | 222 | if (shift != 0) |
8b81872f | 223 | { |
2a310492 VZ |
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 | } | |
8b81872f VZ |
235 | } |
236 | ||
2a310492 VZ |
237 | #ifdef wxLONGLONG_TEST_MODE |
238 | m_ll >>= shift; | |
239 | ||
240 | Check(); | |
241 | #endif // wxLONGLONG_TEST_MODE | |
242 | ||
8b81872f VZ |
243 | return *this; |
244 | } | |
245 | ||
8e38fd1f RL |
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 | ||
8b81872f VZ |
272 | wxLongLongWx wxLongLongWx::operator+(const wxLongLongWx& ll) const |
273 | { | |
2a310492 VZ |
274 | wxLongLongWx res(*this); |
275 | res += ll; | |
8b81872f | 276 | |
2a310492 | 277 | return res; |
8b81872f VZ |
278 | } |
279 | ||
8e38fd1f RL |
280 | wxULongLongWx wxULongLongWx::operator+(const wxULongLongWx& ll) const |
281 | { | |
282 | wxULongLongWx res(*this); | |
283 | res += ll; | |
284 | ||
285 | return res; | |
286 | } | |
287 | ||
8b81872f VZ |
288 | wxLongLongWx wxLongLongWx::operator+(long l) const |
289 | { | |
2a310492 VZ |
290 | wxLongLongWx res(*this); |
291 | res += l; | |
8b81872f | 292 | |
2a310492 | 293 | return res; |
8b81872f VZ |
294 | } |
295 | ||
8e38fd1f RL |
296 | wxULongLongWx wxULongLongWx::operator+(unsigned long l) const |
297 | { | |
298 | wxULongLongWx res(*this); | |
299 | res += l; | |
300 | ||
301 | return res; | |
302 | } | |
303 | ||
8b81872f VZ |
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 | ||
2a310492 VZ |
314 | #ifdef wxLONGLONG_TEST_MODE |
315 | m_ll += ll.m_ll; | |
316 | ||
317 | Check(); | |
318 | #endif // wxLONGLONG_TEST_MODE | |
319 | ||
8b81872f VZ |
320 | return *this; |
321 | } | |
322 | ||
8e38fd1f RL |
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 | ||
8b81872f VZ |
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 | ||
2a310492 VZ |
353 | #ifdef wxLONGLONG_TEST_MODE |
354 | m_ll += l; | |
355 | ||
356 | Check(); | |
357 | #endif // wxLONGLONG_TEST_MODE | |
358 | ||
8b81872f VZ |
359 | return *this; |
360 | } | |
361 | ||
8e38fd1f RL |
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 | ||
8b81872f VZ |
380 | // pre increment |
381 | wxLongLongWx& wxLongLongWx::operator++() | |
382 | { | |
383 | m_lo++; | |
384 | if (m_lo == 0) | |
385 | m_hi++; | |
386 | ||
2a310492 VZ |
387 | #ifdef wxLONGLONG_TEST_MODE |
388 | m_ll++; | |
8b81872f | 389 | |
2a310492 VZ |
390 | Check(); |
391 | #endif // wxLONGLONG_TEST_MODE | |
8b81872f VZ |
392 | |
393 | return *this; | |
394 | } | |
395 | ||
8e38fd1f RL |
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 | ||
8b81872f VZ |
411 | // negation |
412 | wxLongLongWx wxLongLongWx::operator-() const | |
413 | { | |
2a310492 VZ |
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++; | |
8b81872f | 428 | |
2a310492 VZ |
429 | #ifdef wxLONGLONG_TEST_MODE |
430 | m_ll = -m_ll; | |
8b81872f | 431 | |
2a310492 VZ |
432 | Check(); |
433 | #endif // wxLONGLONG_TEST_MODE | |
434 | ||
435 | return *this; | |
8b81872f VZ |
436 | } |
437 | ||
438 | // subtraction | |
439 | ||
440 | wxLongLongWx wxLongLongWx::operator-(const wxLongLongWx& ll) const | |
441 | { | |
2a310492 VZ |
442 | wxLongLongWx res(*this); |
443 | res -= ll; | |
8b81872f | 444 | |
2a310492 | 445 | return res; |
8b81872f VZ |
446 | } |
447 | ||
842215bb | 448 | wxLongLongWx wxULongLongWx::operator-(const wxULongLongWx& ll) const |
8e38fd1f | 449 | { |
842215bb WS |
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; | |
8e38fd1f RL |
456 | |
457 | return res; | |
458 | } | |
459 | ||
8b81872f VZ |
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 | ||
2a310492 VZ |
470 | #ifdef wxLONGLONG_TEST_MODE |
471 | m_ll -= ll.m_ll; | |
472 | ||
473 | Check(); | |
474 | #endif // wxLONGLONG_TEST_MODE | |
475 | ||
13111b2a | 476 | return *this; |
8b81872f VZ |
477 | } |
478 | ||
8e38fd1f RL |
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 | ||
8b81872f VZ |
498 | // pre decrement |
499 | wxLongLongWx& wxLongLongWx::operator--() | |
500 | { | |
501 | m_lo--; | |
502 | if (m_lo == 0xFFFFFFFF) | |
503 | m_hi--; | |
504 | ||
2a310492 VZ |
505 | #ifdef wxLONGLONG_TEST_MODE |
506 | m_ll--; | |
8b81872f | 507 | |
2a310492 VZ |
508 | Check(); |
509 | #endif // wxLONGLONG_TEST_MODE | |
8b81872f VZ |
510 | |
511 | return *this; | |
512 | } | |
513 | ||
8e38fd1f RL |
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 | ||
8b81872f VZ |
529 | // comparison operators |
530 | ||
531 | bool wxLongLongWx::operator<(const wxLongLongWx& ll) const | |
532 | { | |
2ea24d9f | 533 | if ( m_hi < ll.m_hi ) |
f644b28c | 534 | return true; |
2ea24d9f VZ |
535 | else if ( m_hi == ll.m_hi ) |
536 | return m_lo < ll.m_lo; | |
537 | else | |
f644b28c | 538 | return false; |
8b81872f VZ |
539 | } |
540 | ||
8e38fd1f RL |
541 | bool wxULongLongWx::operator<(const wxULongLongWx& ll) const |
542 | { | |
543 | if ( m_hi < ll.m_hi ) | |
f644b28c | 544 | return true; |
8e38fd1f RL |
545 | else if ( m_hi == ll.m_hi ) |
546 | return m_lo < ll.m_lo; | |
547 | else | |
f644b28c | 548 | return false; |
8e38fd1f RL |
549 | } |
550 | ||
8b81872f VZ |
551 | bool wxLongLongWx::operator>(const wxLongLongWx& ll) const |
552 | { | |
2ea24d9f | 553 | if ( m_hi > ll.m_hi ) |
f644b28c | 554 | return true; |
2ea24d9f VZ |
555 | else if ( m_hi == ll.m_hi ) |
556 | return m_lo > ll.m_lo; | |
557 | else | |
f644b28c | 558 | return false; |
8b81872f VZ |
559 | } |
560 | ||
8e38fd1f RL |
561 | bool wxULongLongWx::operator>(const wxULongLongWx& ll) const |
562 | { | |
563 | if ( m_hi > ll.m_hi ) | |
f644b28c | 564 | return true; |
8e38fd1f RL |
565 | else if ( m_hi == ll.m_hi ) |
566 | return m_lo > ll.m_lo; | |
567 | else | |
f644b28c | 568 | return false; |
8e38fd1f RL |
569 | } |
570 | ||
8b81872f VZ |
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 | ||
8e38fd1f RL |
578 | wxULongLongWx wxULongLongWx::operator&(const wxULongLongWx& ll) const |
579 | { | |
580 | return wxULongLongWx(m_hi & ll.m_hi, m_lo & ll.m_lo); | |
581 | } | |
582 | ||
8b81872f VZ |
583 | wxLongLongWx wxLongLongWx::operator|(const wxLongLongWx& ll) const |
584 | { | |
585 | return wxLongLongWx(m_hi | ll.m_hi, m_lo | ll.m_lo); | |
586 | } | |
587 | ||
8e38fd1f RL |
588 | wxULongLongWx wxULongLongWx::operator|(const wxULongLongWx& ll) const |
589 | { | |
590 | return wxULongLongWx(m_hi | ll.m_hi, m_lo | ll.m_lo); | |
591 | } | |
592 | ||
8b81872f VZ |
593 | wxLongLongWx wxLongLongWx::operator^(const wxLongLongWx& ll) const |
594 | { | |
595 | return wxLongLongWx(m_hi ^ ll.m_hi, m_lo ^ ll.m_lo); | |
596 | } | |
597 | ||
8e38fd1f RL |
598 | wxULongLongWx wxULongLongWx::operator^(const wxULongLongWx& ll) const |
599 | { | |
600 | return wxULongLongWx(m_hi ^ ll.m_hi, m_lo ^ ll.m_lo); | |
601 | } | |
602 | ||
8b81872f VZ |
603 | wxLongLongWx& wxLongLongWx::operator&=(const wxLongLongWx& ll) |
604 | { | |
605 | m_lo &= ll.m_lo; | |
606 | m_hi &= ll.m_hi; | |
607 | ||
2a310492 VZ |
608 | #ifdef wxLONGLONG_TEST_MODE |
609 | m_ll &= ll.m_ll; | |
610 | ||
611 | Check(); | |
612 | #endif // wxLONGLONG_TEST_MODE | |
613 | ||
8b81872f VZ |
614 | return *this; |
615 | } | |
616 | ||
8e38fd1f RL |
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 | ||
8b81872f VZ |
631 | wxLongLongWx& wxLongLongWx::operator|=(const wxLongLongWx& ll) |
632 | { | |
633 | m_lo |= ll.m_lo; | |
634 | m_hi |= ll.m_hi; | |
635 | ||
2a310492 VZ |
636 | #ifdef wxLONGLONG_TEST_MODE |
637 | m_ll |= ll.m_ll; | |
638 | ||
639 | Check(); | |
640 | #endif // wxLONGLONG_TEST_MODE | |
641 | ||
8b81872f VZ |
642 | return *this; |
643 | } | |
644 | ||
8e38fd1f RL |
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 | ||
8b81872f VZ |
659 | wxLongLongWx& wxLongLongWx::operator^=(const wxLongLongWx& ll) |
660 | { | |
661 | m_lo ^= ll.m_lo; | |
662 | m_hi ^= ll.m_hi; | |
663 | ||
2a310492 VZ |
664 | #ifdef wxLONGLONG_TEST_MODE |
665 | m_ll ^= ll.m_ll; | |
666 | ||
667 | Check(); | |
668 | #endif // wxLONGLONG_TEST_MODE | |
669 | ||
8b81872f VZ |
670 | return *this; |
671 | } | |
672 | ||
8e38fd1f RL |
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 | ||
8b81872f VZ |
687 | wxLongLongWx wxLongLongWx::operator~() const |
688 | { | |
689 | return wxLongLongWx(~m_hi, ~m_lo); | |
690 | } | |
691 | ||
8e38fd1f RL |
692 | wxULongLongWx wxULongLongWx::operator~() const |
693 | { | |
694 | return wxULongLongWx(~m_hi, ~m_lo); | |
695 | } | |
696 | ||
8b81872f VZ |
697 | // multiplication |
698 | ||
699 | wxLongLongWx wxLongLongWx::operator*(const wxLongLongWx& ll) const | |
700 | { | |
2a310492 VZ |
701 | wxLongLongWx res(*this); |
702 | res *= ll; | |
8b81872f | 703 | |
2a310492 | 704 | return res; |
8b81872f VZ |
705 | } |
706 | ||
8e38fd1f RL |
707 | wxULongLongWx wxULongLongWx::operator*(const wxULongLongWx& ll) const |
708 | { | |
709 | wxULongLongWx res(*this); | |
710 | res *= ll; | |
711 | ||
712 | return res; | |
713 | } | |
714 | ||
8b81872f VZ |
715 | wxLongLongWx& wxLongLongWx::operator*=(const wxLongLongWx& ll) |
716 | { | |
717 | wxLongLongWx t(m_hi, m_lo); | |
718 | wxLongLongWx q(ll.m_hi, ll.m_lo); | |
8b81872f | 719 | |
2a310492 VZ |
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; | |
8b81872f VZ |
728 | do |
729 | { | |
730 | if ((q.m_lo & 1) != 0) | |
2a310492 | 731 | *this += t; |
8b81872f VZ |
732 | q >>= 1; |
733 | t <<= 1; | |
734 | counter++; | |
2a310492 VZ |
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 | ||
8b81872f VZ |
744 | return *this; |
745 | } | |
746 | ||
8e38fd1f RL |
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 | ||
8b81872f VZ |
779 | // division |
780 | ||
5e6a0e83 | 781 | void wxLongLongWx::Divide(const wxLongLongWx& divisorIn, |
cd0b1709 VZ |
782 | wxLongLongWx& quotient, |
783 | wxLongLongWx& remainder) const | |
8b81872f | 784 | { |
5e6a0e83 | 785 | if ((divisorIn.m_lo == 0) && (divisorIn.m_hi == 0)) |
8b81872f VZ |
786 | { |
787 | // provoke division by zero error and silence the compilers warnings | |
788 | // about an expression without effect and unused variable | |
5e6a0e83 | 789 | long dummy = divisorIn.m_lo/divisorIn.m_hi; |
8b81872f VZ |
790 | dummy += 0; |
791 | } | |
792 | ||
2ea24d9f VZ |
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 | |
5e6a0e83 VZ |
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 | |
3a994742 | 804 | // all responsibility for using this code. |
5e6a0e83 VZ |
805 | |
806 | // init everything | |
807 | wxLongLongWx dividend = *this, | |
808 | divisor = divisorIn; | |
809 | ||
810 | quotient = 0l; | |
811 | remainder = 0l; | |
812 | ||
5e6a0e83 VZ |
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; | |
f644b28c | 822 | bool negQuotient = false; // assume positive |
5e6a0e83 VZ |
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 | } | |
2ea24d9f | 833 | |
2a310492 VZ |
834 | // check for some particular cases |
835 | if ( divisor > dividend ) | |
2ea24d9f | 836 | { |
2a310492 VZ |
837 | remainder = dividend; |
838 | } | |
839 | else if ( divisor == dividend ) | |
840 | { | |
841 | quotient = 1l; | |
5e6a0e83 | 842 | } |
2a310492 VZ |
843 | else |
844 | { | |
842215bb | 845 | // here: dividend > divisor and both are positive: do unsigned division |
2a310492 VZ |
846 | size_t nBits = 64u; |
847 | wxLongLongWx d; | |
5e6a0e83 | 848 | |
2a310492 | 849 | #define IS_MSB_SET(ll) ((ll.m_hi) & (1 << (8*sizeof(long) - 1))) |
2ea24d9f | 850 | |
2a310492 | 851 | while ( remainder < divisor ) |
2ea24d9f | 852 | { |
2a310492 VZ |
853 | remainder <<= 1; |
854 | if ( IS_MSB_SET(dividend) ) | |
855 | { | |
856 | remainder |= 1; | |
857 | } | |
858 | ||
859 | d = dividend; | |
860 | dividend <<= 1; | |
861 | ||
862 | nBits--; | |
2ea24d9f VZ |
863 | } |
864 | ||
2a310492 VZ |
865 | // undo the last loop iteration |
866 | dividend = d; | |
867 | remainder >>= 1; | |
868 | nBits++; | |
5e6a0e83 | 869 | |
2a310492 VZ |
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 | } | |
5e6a0e83 | 887 | } |
2ea24d9f VZ |
888 | } |
889 | ||
5e6a0e83 VZ |
890 | // adjust signs |
891 | if ( negRemainder ) | |
2ea24d9f | 892 | { |
5e6a0e83 | 893 | remainder = -remainder; |
2ea24d9f VZ |
894 | } |
895 | ||
5e6a0e83 VZ |
896 | if ( negQuotient ) |
897 | { | |
898 | quotient = -quotient; | |
899 | } | |
2ea24d9f VZ |
900 | } |
901 | ||
8e38fd1f RL |
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 | ||
2ea24d9f VZ |
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 | ||
8e38fd1f RL |
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 | ||
2ea24d9f VZ |
1009 | wxLongLongWx& wxLongLongWx::operator/=(const wxLongLongWx& ll) |
1010 | { | |
1011 | wxLongLongWx quotient, remainder; | |
1012 | ||
1013 | Divide(ll, quotient, remainder); | |
1014 | ||
55dfa8d3 | 1015 | *this = quotient; |
3a994742 | 1016 | |
55dfa8d3 | 1017 | return *this; |
8b81872f VZ |
1018 | } |
1019 | ||
8e38fd1f RL |
1020 | wxULongLongWx& wxULongLongWx::operator/=(const wxULongLongWx& ll) |
1021 | { | |
c35c7d01 | 1022 | wxULongLongWx quotient, remainder; |
8e38fd1f RL |
1023 | |
1024 | Divide(ll, quotient, remainder); | |
1025 | ||
1026 | *this = quotient; | |
1027 | ||
1028 | return *this; | |
1029 | } | |
1030 | ||
2ea24d9f VZ |
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 | ||
8e38fd1f RL |
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 | ||
2ea24d9f VZ |
1049 | // ---------------------------------------------------------------------------- |
1050 | // misc | |
1051 | // ---------------------------------------------------------------------------- | |
1052 | ||
8b81872f VZ |
1053 | // temporary - just for testing |
1054 | void *wxLongLongWx::asArray(void) const | |
1055 | { | |
1056 | static unsigned char temp[8]; | |
1057 | ||
13111b2a VZ |
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); | |
8b81872f VZ |
1066 | |
1067 | return temp; | |
1068 | } | |
1069 | ||
8e38fd1f RL |
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 | ||
3a994742 | 1086 | #endif // wxUSE_LONGLONG_WX |
8b81872f | 1087 | |
842215bb WS |
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; \ | |
3a994742 | 1119 | } |
8b81872f | 1120 | |
842215bb WS |
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; \ | |
8b81872f VZ |
1139 | } |
1140 | ||
8e38fd1f | 1141 | #if wxUSE_LONGLONG_NATIVE |
842215bb WS |
1142 | LL_TO_STRING(wxLongLongNative) |
1143 | ULL_TO_STRING(wxULongLongNative) | |
8e38fd1f | 1144 | #endif |
8e38fd1f | 1145 | |
842215bb WS |
1146 | #if wxUSE_LONGLONG_WX |
1147 | LL_TO_STRING(wxLongLongWx) | |
1148 | ULL_TO_STRING(wxULongLongWx) | |
1149 | #endif | |
8e38fd1f | 1150 | |
3a994742 VZ |
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 | ||
8e38fd1f RL |
1159 | wxSTD ostream& operator<< (wxSTD ostream& o, const wxULongLong& ll) |
1160 | { | |
1161 | return o << ll.ToString(); | |
1162 | } | |
1163 | ||
3a994742 | 1164 | #endif // wxUSE_STD_IOSTREAM |
8b81872f | 1165 | |
4e57b0d4 | 1166 | #endif // wxUSE_LONGLONG |