]>
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 | |
5 | // Remarks: this class is not public in wxWindows 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 license | |
12 | ///////////////////////////////////////////////////////////////////////////// | |
13 | ||
14 | // ============================================================================ | |
15 | // headers | |
16 | // ============================================================================ | |
17 | ||
18 | #ifdef __GNUG__ | |
19 | #pragma implementation "longlong.h" | |
20 | #endif | |
21 | ||
22 | #include "wx/wxprec.h" | |
23 | ||
24 | #ifdef __BORLANDC__ | |
25 | #pragma hdrstop | |
26 | #endif | |
27 | ||
4e57b0d4 VZ |
28 | #if wxUSE_LONGLONG |
29 | ||
8b81872f VZ |
30 | #include "wx/longlong.h" |
31 | ||
32 | #include <memory.h> // for memset() | |
33 | ||
34 | // ============================================================================ | |
35 | // implementation | |
36 | // ============================================================================ | |
37 | ||
38 | #if wxUSE_LONGLONG_NATIVE | |
39 | ||
40 | // ---------------------------------------------------------------------------- | |
41 | // misc | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
44 | void *wxLongLongNative::asArray(void) const | |
45 | { | |
46 | static unsigned char temp[8]; | |
47 | ||
48 | temp[0] = (m_ll >> 56) & 0xFF; | |
49 | temp[1] = (m_ll >> 48) & 0xFF; | |
50 | temp[2] = (m_ll >> 40) & 0xFF; | |
51 | temp[3] = (m_ll >> 32) & 0xFF; | |
52 | temp[4] = (m_ll >> 24) & 0xFF; | |
53 | temp[5] = (m_ll >> 16) & 0xFF; | |
54 | temp[6] = (m_ll >> 8) & 0xFF; | |
55 | temp[7] = (m_ll >> 0) & 0xFF; | |
56 | ||
57 | return temp; | |
58 | } | |
59 | ||
fcc3d7cb VZ |
60 | #if wxUSE_STD_IOSTREAM |
61 | ||
8b81872f | 62 | // input/output |
162b0c3b | 63 | ostream& operator<< (ostream& o, const wxLongLongNative& ll) |
8b81872f VZ |
64 | { |
65 | char result[65]; | |
66 | ||
67 | memset(result, 'A', 64); | |
68 | ||
69 | result[64] = '\0'; | |
70 | ||
71 | for (int i = 0; i < 64; i++) | |
72 | { | |
73 | result[63 - i] = '0' + (char) ((ll.m_ll >> i) & 1); | |
74 | } | |
75 | ||
76 | return o << result; | |
77 | } | |
78 | ||
fcc3d7cb VZ |
79 | #endif // wxUSE_STD_IOSTREAM |
80 | ||
8b81872f VZ |
81 | #endif // wxUSE_LONGLONG_NATIVE |
82 | ||
83 | #if wxUSE_LONGLONG_WX | |
84 | ||
85 | wxLongLongWx wxLongLongWx::operator<<(int shift) const | |
86 | { | |
87 | if (shift == 0) | |
88 | return *this; | |
89 | ||
90 | if (shift < 32) | |
91 | return wxLongLongWx((m_hi << shift) | (m_lo >> (32 - shift)), | |
92 | m_lo << shift); | |
93 | else | |
94 | return wxLongLongWx(m_lo << (shift - 32), | |
95 | 0); | |
96 | } | |
97 | ||
98 | wxLongLongWx& wxLongLongWx::operator<<=(int shift) | |
99 | { | |
100 | if (shift == 0) | |
101 | return *this; | |
102 | ||
103 | if (shift < 32) | |
104 | { | |
105 | m_hi <<= shift; | |
106 | m_hi |= m_lo >> (32 - shift); | |
107 | m_lo <<= shift; | |
108 | } | |
109 | else | |
110 | { | |
111 | m_hi = m_lo << (shift - 32); | |
112 | m_lo = 0; | |
113 | } | |
114 | ||
115 | return *this; | |
116 | } | |
117 | ||
118 | wxLongLongWx wxLongLongWx::operator>>(int shift) const | |
119 | { | |
120 | if (shift == 0) | |
121 | return *this; | |
122 | ||
123 | if (shift < 32) | |
124 | return wxLongLongWx(m_hi >> shift, | |
125 | (m_lo >> shift) | (m_hi << (32 - shift))); | |
126 | else | |
127 | return wxLongLongWx((m_hi < 0 ? -1l : 0), | |
128 | m_hi >> (shift - 32)); | |
129 | } | |
130 | ||
131 | wxLongLongWx& wxLongLongWx::operator>>=(int shift) | |
132 | { | |
133 | if (shift == 0) | |
134 | return *this; | |
135 | ||
136 | if (shift < 32) | |
137 | { | |
138 | m_lo >>= shift; | |
139 | m_lo |= m_hi << (32 - shift); | |
140 | m_hi >>= shift; | |
141 | } | |
142 | else | |
143 | { | |
144 | m_lo = m_hi >> (shift - 32); | |
145 | m_hi = (m_hi < 0 ? -1L : 0); | |
146 | } | |
147 | ||
148 | return *this; | |
149 | } | |
150 | ||
151 | wxLongLongWx wxLongLongWx::operator+(const wxLongLongWx& ll) const | |
152 | { | |
153 | wxLongLongWx temp; | |
154 | ||
155 | temp.m_lo = m_lo + ll.m_lo; | |
156 | temp.m_hi = m_hi + ll.m_hi; | |
157 | if ((temp.m_lo < m_lo) || (temp.m_lo < ll.m_lo)) | |
158 | temp.m_hi++; | |
159 | ||
160 | return temp; | |
161 | } | |
162 | ||
163 | wxLongLongWx wxLongLongWx::operator+(long l) const | |
164 | { | |
165 | wxLongLongWx temp; | |
166 | ||
167 | temp.m_lo = m_lo + l; | |
168 | ||
169 | if (l < 0) | |
170 | temp.m_hi += -1l; | |
171 | ||
172 | if ((temp.m_lo < m_lo) || (temp.m_lo < (unsigned long)l)) | |
173 | temp.m_hi++; | |
174 | ||
175 | return temp; | |
176 | } | |
177 | ||
178 | wxLongLongWx& wxLongLongWx::operator+=(const wxLongLongWx& ll) | |
179 | { | |
180 | unsigned long previous = m_lo; | |
181 | ||
182 | m_lo += ll.m_lo; | |
183 | m_hi += ll.m_hi; | |
184 | ||
185 | if ((m_lo < previous) || (m_lo < ll.m_lo)) | |
186 | m_hi++; | |
187 | ||
188 | return *this; | |
189 | } | |
190 | ||
191 | wxLongLongWx& wxLongLongWx::operator+=(long l) | |
192 | { | |
193 | unsigned long previous = m_lo; | |
194 | ||
195 | m_lo += l; | |
196 | if (l < 0) | |
197 | m_hi += -1l; | |
198 | ||
199 | if ((m_lo < previous) || (m_lo < (unsigned long)l)) | |
200 | m_hi++; | |
201 | ||
202 | return *this; | |
203 | } | |
204 | ||
205 | // pre increment | |
206 | wxLongLongWx& wxLongLongWx::operator++() | |
207 | { | |
208 | m_lo++; | |
209 | if (m_lo == 0) | |
210 | m_hi++; | |
211 | ||
212 | return *this; | |
213 | } | |
214 | ||
215 | // post increment | |
216 | wxLongLongWx& wxLongLongWx::operator++(int) | |
217 | { | |
218 | m_lo++; | |
219 | if (m_lo == 0) | |
220 | m_hi++; | |
221 | ||
222 | return *this; | |
223 | } | |
224 | ||
225 | // negation | |
226 | wxLongLongWx wxLongLongWx::operator-() const | |
227 | { | |
228 | wxLongLongWx temp(~m_hi, ~m_lo); | |
229 | ||
230 | temp.m_lo++; | |
231 | if (temp.m_lo == 0) | |
232 | temp.m_hi++; | |
233 | ||
234 | return temp; | |
235 | } | |
236 | ||
237 | // subtraction | |
238 | ||
239 | wxLongLongWx wxLongLongWx::operator-(const wxLongLongWx& ll) const | |
240 | { | |
241 | wxLongLongWx temp; | |
242 | ||
243 | temp.m_lo = m_lo - ll.m_lo; | |
244 | temp.m_hi = m_hi - ll.m_hi; | |
245 | ||
246 | if (m_lo < ll.m_lo) | |
247 | temp.m_hi--; | |
248 | ||
249 | return temp; | |
250 | } | |
251 | ||
252 | wxLongLongWx& wxLongLongWx::operator-=(const wxLongLongWx& ll) | |
253 | { | |
254 | unsigned long previous = m_lo; | |
255 | ||
256 | m_lo -= ll.m_lo; | |
257 | m_hi -= ll.m_hi; | |
258 | ||
259 | if (previous < ll.m_lo) | |
260 | m_hi--; | |
261 | ||
262 | return *this;; | |
263 | } | |
264 | ||
265 | // pre decrement | |
266 | wxLongLongWx& wxLongLongWx::operator--() | |
267 | { | |
268 | m_lo--; | |
269 | if (m_lo == 0xFFFFFFFF) | |
270 | m_hi--; | |
271 | ||
272 | return *this; | |
273 | } | |
274 | ||
275 | // post decrement | |
276 | wxLongLongWx& wxLongLongWx::operator--(int) | |
277 | { | |
278 | m_lo--; | |
279 | if (m_lo == 0xFFFFFFFF) | |
280 | m_hi--; | |
281 | ||
282 | return *this; | |
283 | } | |
284 | ||
285 | // comparison operators | |
286 | ||
287 | bool wxLongLongWx::operator<(const wxLongLongWx& ll) const | |
288 | { | |
289 | if (m_lo < ll.m_lo) | |
290 | return 1; | |
291 | if (m_lo > ll.m_lo) | |
292 | return 0; | |
293 | if (m_hi < ll.m_hi) | |
294 | return 1; | |
295 | if (m_hi > ll.m_hi) | |
296 | return 0; | |
297 | return 0; | |
298 | } | |
299 | ||
300 | bool wxLongLongWx::operator>(const wxLongLongWx& ll) const | |
301 | { | |
302 | if (m_lo < ll.m_lo) | |
303 | return 0; | |
304 | if (m_lo > ll.m_lo) | |
305 | return 1; | |
306 | if (m_hi < ll.m_hi) | |
307 | return 0; | |
308 | if (m_hi > ll.m_hi) | |
309 | return 1; | |
310 | return 0; | |
311 | } | |
312 | ||
313 | bool wxLongLongWx::operator<=(const wxLongLongWx& ll) const | |
314 | { | |
315 | if (m_lo < ll.m_lo) | |
316 | return 1; | |
317 | if (m_lo > ll.m_lo) | |
318 | return 0; | |
319 | if (m_hi < ll.m_hi) | |
320 | return 1; | |
321 | if (m_hi > ll.m_hi) | |
322 | return 0; | |
323 | return 1; | |
324 | } | |
325 | ||
326 | bool wxLongLongWx::operator>=(const wxLongLongWx& ll) const | |
327 | { | |
328 | if (m_lo < ll.m_lo) | |
329 | return 0; | |
330 | if (m_lo > ll.m_lo) | |
331 | return 1; | |
332 | if (m_hi < ll.m_hi) | |
333 | return 0; | |
334 | if (m_hi > ll.m_hi) | |
335 | return 1; | |
336 | return 1; | |
337 | } | |
338 | ||
339 | // bitwise operators | |
340 | ||
341 | wxLongLongWx wxLongLongWx::operator&(const wxLongLongWx& ll) const | |
342 | { | |
343 | return wxLongLongWx(m_hi & ll.m_hi, m_lo & ll.m_lo); | |
344 | } | |
345 | ||
346 | wxLongLongWx wxLongLongWx::operator|(const wxLongLongWx& ll) const | |
347 | { | |
348 | return wxLongLongWx(m_hi | ll.m_hi, m_lo | ll.m_lo); | |
349 | } | |
350 | ||
351 | wxLongLongWx wxLongLongWx::operator^(const wxLongLongWx& ll) const | |
352 | { | |
353 | return wxLongLongWx(m_hi ^ ll.m_hi, m_lo ^ ll.m_lo); | |
354 | } | |
355 | ||
356 | wxLongLongWx& wxLongLongWx::operator&=(const wxLongLongWx& ll) | |
357 | { | |
358 | m_lo &= ll.m_lo; | |
359 | m_hi &= ll.m_hi; | |
360 | ||
361 | return *this; | |
362 | } | |
363 | ||
364 | wxLongLongWx& wxLongLongWx::operator|=(const wxLongLongWx& ll) | |
365 | { | |
366 | m_lo |= ll.m_lo; | |
367 | m_hi |= ll.m_hi; | |
368 | ||
369 | return *this; | |
370 | } | |
371 | ||
372 | wxLongLongWx& wxLongLongWx::operator^=(const wxLongLongWx& ll) | |
373 | { | |
374 | m_lo ^= ll.m_lo; | |
375 | m_hi ^= ll.m_hi; | |
376 | ||
377 | return *this; | |
378 | } | |
379 | ||
380 | wxLongLongWx wxLongLongWx::operator~() const | |
381 | { | |
382 | return wxLongLongWx(~m_hi, ~m_lo); | |
383 | } | |
384 | ||
385 | // multiplication | |
386 | ||
387 | wxLongLongWx wxLongLongWx::operator*(const wxLongLongWx& ll) const | |
388 | { | |
389 | wxLongLongWx t(m_hi, m_lo); | |
390 | wxLongLongWx q(ll.m_hi, ll.m_lo); | |
391 | wxLongLongWx p; | |
392 | int counter = 0; | |
393 | ||
394 | do | |
395 | { | |
396 | if ((q.m_lo & 1) != 0) | |
397 | p += t; | |
398 | q >>= 1; | |
399 | t <<= 1; | |
400 | counter++; | |
401 | } | |
402 | while ((counter < 64) && ((q.m_hi != 0) || (q.m_lo != 0))); | |
403 | return p; | |
404 | } | |
405 | ||
406 | wxLongLongWx& wxLongLongWx::operator*=(const wxLongLongWx& ll) | |
407 | { | |
408 | wxLongLongWx t(m_hi, m_lo); | |
409 | wxLongLongWx q(ll.m_hi, ll.m_lo); | |
410 | int counter = 0; | |
411 | ||
412 | do | |
413 | { | |
414 | if ((q.m_lo & 1) != 0) | |
415 | *this += t; | |
416 | q >>= 1; | |
417 | t <<= 1; | |
418 | counter++; | |
419 | } | |
420 | while ((counter < 64) && ((q.m_hi != 0) || (q.m_lo != 0))); | |
421 | return *this; | |
422 | } | |
423 | ||
424 | // division | |
425 | ||
426 | void wxLongLongWx::Divide(const wxLongLongWx& divisor, wxLongLongWx& quotient, wxLongLongWx& remainder) const | |
427 | { | |
428 | if ((divisor.m_lo == 0) && (divisor.m_hi == 0)) | |
429 | { | |
430 | // provoke division by zero error and silence the compilers warnings | |
431 | // about an expression without effect and unused variable | |
432 | long dummy = divisor.m_lo/divisor.m_hi; | |
433 | dummy += 0; | |
434 | } | |
435 | ||
436 | wxFAIL_MSG("not implemented"); | |
437 | } | |
438 | ||
439 | // temporary - just for testing | |
440 | void *wxLongLongWx::asArray(void) const | |
441 | { | |
442 | static unsigned char temp[8]; | |
443 | ||
444 | temp[0] = (m_hi >> 24) & 0xFF; | |
445 | temp[1] = (m_hi >> 16) & 0xFF; | |
446 | temp[2] = (m_hi >> 8) & 0xFF; | |
447 | temp[3] = (m_hi >> 0) & 0xFF; | |
448 | temp[4] = (m_lo >> 24) & 0xFF; | |
449 | temp[5] = (m_lo >> 16) & 0xFF; | |
450 | temp[6] = (m_lo >> 8) & 0xFF; | |
451 | temp[7] = (m_lo >> 0) & 0xFF; | |
452 | ||
453 | return temp; | |
454 | } | |
455 | ||
fcc3d7cb | 456 | #if wxUSE_STD_IOSTREAM |
8b81872f | 457 | |
fcc3d7cb | 458 | // input/output |
162b0c3b | 459 | ostream& operator<< (ostream& o, const wxLongLongWx& ll) |
8b81872f VZ |
460 | { |
461 | char result[65]; | |
462 | ||
463 | memset(result, 'A', 64); | |
464 | ||
465 | result[64] = '\0'; | |
466 | ||
467 | for (int i = 0; i < 32; i++) | |
468 | { | |
469 | result[31 - i] = (char) ('0' + (int) ((ll.m_hi >> i) & 1)); | |
470 | result[63 - i] = (char) ('0' + (int) ((ll.m_lo >> i) & 1)); | |
471 | } | |
472 | ||
473 | return o << result; | |
474 | } | |
fcc3d7cb VZ |
475 | #endif // wxUSE_STD_IOSTREAM |
476 | ||
477 | #endif // wxUSE_LONGLONG_NATIVE | |
8b81872f | 478 | |
4e57b0d4 | 479 | #endif // wxUSE_LONGLONG |