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