]>
git.saurik.com Git - wxWidgets.git/blob - 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 wxWindows 2.0! It is intentionally
6 // not documented and is for private use only.
10 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
11 // Licence: wxWindows license
12 /////////////////////////////////////////////////////////////////////////////
14 // ============================================================================
16 // ============================================================================
19 #pragma implementation "longlong.h"
22 #include "wx/wxprec.h"
29 #include "wx/longlong.h"
31 #include <memory.h> // for memset()
32 #include <math.h> // for fabs()
34 // ============================================================================
36 // ============================================================================
38 #if wxUSE_LONGLONG_NATIVE
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 void *wxLongLongNative::asArray(void) const
46 static unsigned char temp
[8];
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;
60 #if wxUSE_STD_IOSTREAM
63 ostream
& operator<< (ostream
& o
, const wxLongLongNative
& ll
)
67 memset(result
, 'A', 64);
71 for (int i
= 0; i
< 64; i
++)
73 result
[63 - i
] = '0' + (char) ((ll
.m_ll
>> i
) & 1);
79 #endif // wxUSE_STD_IOSTREAM
81 #endif // wxUSE_LONGLONG_NATIVE
83 // ============================================================================
84 // wxLongLongWx: emulation of 'long long' using 2 longs
85 // ============================================================================
90 wxLongLongWx
& wxLongLongWx::Assign(double d
)
92 if ( fabs(d
) <= LONG_MAX
)
94 m_hi
= d
< 0 ? 1 << (8*sizeof(long) - 1) : 0l;
99 wxFAIL_MSG(_T("TODO"));
105 wxLongLongWx
wxLongLongWx::operator<<(int shift
) const
111 return wxLongLongWx((m_hi
<< shift
) | (m_lo
>> (32 - shift
)),
114 return wxLongLongWx(m_lo
<< (shift
- 32),
118 wxLongLongWx
& wxLongLongWx::operator<<=(int shift
)
126 m_hi
|= m_lo
>> (32 - shift
);
131 m_hi
= m_lo
<< (shift
- 32);
138 wxLongLongWx
wxLongLongWx::operator>>(int shift
) const
144 return wxLongLongWx(m_hi
>> shift
,
145 (m_lo
>> shift
) | (m_hi
<< (32 - shift
)));
147 return wxLongLongWx((m_hi
< 0 ? -1l : 0),
148 m_hi
>> (shift
- 32));
151 wxLongLongWx
& wxLongLongWx::operator>>=(int shift
)
159 m_lo
|= m_hi
<< (32 - shift
);
164 m_lo
= m_hi
>> (shift
- 32);
165 m_hi
= (m_hi
< 0 ? -1L : 0);
171 wxLongLongWx
wxLongLongWx::operator+(const wxLongLongWx
& ll
) const
175 temp
.m_lo
= m_lo
+ ll
.m_lo
;
176 temp
.m_hi
= m_hi
+ ll
.m_hi
;
177 if ((temp
.m_lo
< m_lo
) || (temp
.m_lo
< ll
.m_lo
))
183 wxLongLongWx
wxLongLongWx::operator+(long l
) const
187 temp
.m_lo
= m_lo
+ l
;
192 if ((temp
.m_lo
< m_lo
) || (temp
.m_lo
< (unsigned long)l
))
198 wxLongLongWx
& wxLongLongWx::operator+=(const wxLongLongWx
& ll
)
200 unsigned long previous
= m_lo
;
205 if ((m_lo
< previous
) || (m_lo
< ll
.m_lo
))
211 wxLongLongWx
& wxLongLongWx::operator+=(long l
)
213 unsigned long previous
= m_lo
;
219 if ((m_lo
< previous
) || (m_lo
< (unsigned long)l
))
226 wxLongLongWx
& wxLongLongWx::operator++()
236 wxLongLongWx
& wxLongLongWx::operator++(int)
246 wxLongLongWx
wxLongLongWx::operator-() const
248 wxLongLongWx
temp(~m_hi
, ~m_lo
);
259 wxLongLongWx
wxLongLongWx::operator-(const wxLongLongWx
& ll
) const
263 temp
.m_lo
= m_lo
- ll
.m_lo
;
264 temp
.m_hi
= m_hi
- ll
.m_hi
;
272 wxLongLongWx
& wxLongLongWx::operator-=(const wxLongLongWx
& ll
)
274 unsigned long previous
= m_lo
;
279 if (previous
< ll
.m_lo
)
286 wxLongLongWx
& wxLongLongWx::operator--()
289 if (m_lo
== 0xFFFFFFFF)
296 wxLongLongWx
& wxLongLongWx::operator--(int)
299 if (m_lo
== 0xFFFFFFFF)
305 // comparison operators
307 bool wxLongLongWx::operator<(const wxLongLongWx
& ll
) const
309 if ( m_hi
< ll
.m_hi
)
311 else if ( m_hi
== ll
.m_hi
)
312 return m_lo
< ll
.m_lo
;
317 bool wxLongLongWx::operator>(const wxLongLongWx
& ll
) const
319 if ( m_hi
> ll
.m_hi
)
321 else if ( m_hi
== ll
.m_hi
)
322 return m_lo
> ll
.m_lo
;
329 wxLongLongWx
wxLongLongWx::operator&(const wxLongLongWx
& ll
) const
331 return wxLongLongWx(m_hi
& ll
.m_hi
, m_lo
& ll
.m_lo
);
334 wxLongLongWx
wxLongLongWx::operator|(const wxLongLongWx
& ll
) const
336 return wxLongLongWx(m_hi
| ll
.m_hi
, m_lo
| ll
.m_lo
);
339 wxLongLongWx
wxLongLongWx::operator^(const wxLongLongWx
& ll
) const
341 return wxLongLongWx(m_hi
^ ll
.m_hi
, m_lo
^ ll
.m_lo
);
344 wxLongLongWx
& wxLongLongWx::operator&=(const wxLongLongWx
& ll
)
352 wxLongLongWx
& wxLongLongWx::operator|=(const wxLongLongWx
& ll
)
360 wxLongLongWx
& wxLongLongWx::operator^=(const wxLongLongWx
& ll
)
368 wxLongLongWx
wxLongLongWx::operator~() const
370 return wxLongLongWx(~m_hi
, ~m_lo
);
375 wxLongLongWx
wxLongLongWx::operator*(const wxLongLongWx
& ll
) const
377 wxLongLongWx
t(m_hi
, m_lo
);
378 wxLongLongWx
q(ll
.m_hi
, ll
.m_lo
);
384 if ((q
.m_lo
& 1) != 0)
390 while ((counter
< 64) && ((q
.m_hi
!= 0) || (q
.m_lo
!= 0)));
394 wxLongLongWx
& wxLongLongWx::operator*=(const wxLongLongWx
& ll
)
396 wxLongLongWx
t(m_hi
, m_lo
);
397 wxLongLongWx
q(ll
.m_hi
, ll
.m_lo
);
402 if ((q
.m_lo
& 1) != 0)
408 while ((counter
< 64) && ((q
.m_hi
!= 0) || (q
.m_lo
!= 0)));
414 void wxLongLongWx::Divide(const wxLongLongWx
& divisorIn
,
415 wxLongLongWx
& quotient
,
416 wxLongLongWx
& remainder
) const
418 if ((divisorIn
.m_lo
== 0) && (divisorIn
.m_hi
== 0))
420 // provoke division by zero error and silence the compilers warnings
421 // about an expression without effect and unused variable
422 long dummy
= divisorIn
.m_lo
/divisorIn
.m_hi
;
426 // VZ: I'm writing this in a hurry and it's surely not the fastest way to
427 // do this - any improvements are more than welcome
429 // code inspired by the snippet at
430 // http://www.bearcave.com/software/divide.htm
434 // Use of this program, for any purpose, is granted the author, Ian
435 // Kaplan, as long as this copyright notice is included in the source
436 // code or any source code derived from this program. The user assumes
437 // all responsibility for using this code.
440 wxLongLongWx dividend
= *this,
446 // check for some particular cases
447 if ( divisor
> dividend
)
449 remainder
= dividend
;
454 if ( divisor
== dividend
)
461 // always do unsigned division and adjust the signs later: in C integer
462 // division, the sign of the remainder is the same as the sign of the
463 // dividend, while the sign of the quotient is the product of the signs of
464 // the dividend and divisor. Of course, we also always have
466 // dividend = quotient*divisor + remainder
468 // with 0 <= abs(remainder) < abs(divisor)
469 bool negRemainder
= dividend
.m_hi
< 0;
470 bool negQuotient
= FALSE
; // assume positive
471 if ( dividend
.m_hi
< 0 )
473 negQuotient
= !negQuotient
;
474 dividend
= -dividend
;
476 if ( divisor
.m_hi
< 0 )
478 negQuotient
= !negQuotient
;
482 // here: dividend > divisor and both are positibe: do unsigned division
486 #define IS_MSB_SET(ll) ((ll.m_hi) & (1 << (8*sizeof(long) - 1)))
488 while ( remainder
< divisor
)
491 if ( IS_MSB_SET(dividend
) )
502 // undo the last loop iteration
507 for ( size_t i
= 0; i
< nBits
; i
++ )
510 if ( IS_MSB_SET(dividend
) )
515 wxLongLongWx t
= remainder
- divisor
;
518 if ( !IS_MSB_SET(t
) )
529 remainder
= -remainder
;
534 quotient
= -quotient
;
538 wxLongLongWx
wxLongLongWx::operator/(const wxLongLongWx
& ll
) const
540 wxLongLongWx quotient
, remainder
;
542 Divide(ll
, quotient
, remainder
);
547 wxLongLongWx
& wxLongLongWx::operator/=(const wxLongLongWx
& ll
)
549 wxLongLongWx quotient
, remainder
;
551 Divide(ll
, quotient
, remainder
);
553 return *this = quotient
;
556 wxLongLongWx
wxLongLongWx::operator%(const wxLongLongWx
& ll
) const
558 wxLongLongWx quotient
, remainder
;
560 Divide(ll
, quotient
, remainder
);
565 // ----------------------------------------------------------------------------
567 // ----------------------------------------------------------------------------
569 // temporary - just for testing
570 void *wxLongLongWx::asArray(void) const
572 static unsigned char temp
[8];
574 temp
[0] = (char)((m_hi
>> 24) & 0xFF);
575 temp
[1] = (char)((m_hi
>> 16) & 0xFF);
576 temp
[2] = (char)((m_hi
>> 8) & 0xFF);
577 temp
[3] = (char)((m_hi
>> 0) & 0xFF);
578 temp
[4] = (char)((m_lo
>> 24) & 0xFF);
579 temp
[5] = (char)((m_lo
>> 16) & 0xFF);
580 temp
[6] = (char)((m_lo
>> 8) & 0xFF);
581 temp
[7] = (char)((m_lo
>> 0) & 0xFF);
586 #if wxUSE_STD_IOSTREAM
589 ostream
& operator<< (ostream
& o
, const wxLongLongWx
& ll
)
593 memset(result
, 'A', 64);
597 for (int i
= 0; i
< 32; i
++)
599 result
[31 - i
] = (char) ('0' + (int) ((ll
.m_hi
>> i
) & 1));
600 result
[63 - i
] = (char) ('0' + (int) ((ll
.m_lo
>> i
) & 1));
605 #endif // wxUSE_STD_IOSTREAM
607 #endif // wxUSE_LONGLONG_NATIVE
609 #endif // wxUSE_LONGLONG