+#define IS_MSB_SET(ll) ((ll.GetHi()) & (1 << (8*sizeof(long) - 1)))
+
+void wxLongLongWx::Divide(const wxLongLongWx& divisorIn,
+ wxLongLongWx& quotient,
+ wxLongLongWx& remainderIO) const
+{
+ if ((divisorIn.m_lo == 0) && (divisorIn.m_hi == 0))
+ {
+ // provoke division by zero error and silence the compilers warnings
+ // about an expression without effect and unused variable
+ long dummy = divisorIn.m_lo/divisorIn.m_hi;
+ dummy += 0;
+ }
+
+ // VZ: I'm writing this in a hurry and it's surely not the fastest way to
+ // do this - any improvements are more than welcome
+ //
+ // code inspired by the snippet at
+ // http://www.bearcave.com/software/divide.htm
+ //
+ // Copyright notice:
+ //
+ // Use of this program, for any purpose, is granted the author, Ian
+ // Kaplan, as long as this copyright notice is included in the source
+ // code or any source code derived from this program. The user assumes
+ // all responsibility for using this code.
+
+ // init everything
+ wxULongLongWx dividend, divisor, remainder;
+
+ quotient = 0l;
+ remainder = 0l;
+
+ // always do unsigned division and adjust the signs later: in C integer
+ // division, the sign of the remainder is the same as the sign of the
+ // dividend, while the sign of the quotient is the product of the signs of
+ // the dividend and divisor. Of course, we also always have
+ //
+ // dividend = quotient*divisor + remainder
+ //
+ // with 0 <= abs(remainder) < abs(divisor)
+ bool negRemainder = GetHi() < 0;
+ bool negQuotient = false; // assume positive
+ if ( GetHi() < 0 )
+ {
+ negQuotient = !negQuotient;
+ dividend = -*this;
+ } else {
+ dividend = *this;
+ }
+ if ( divisorIn.GetHi() < 0 )
+ {
+ negQuotient = !negQuotient;
+ divisor = -divisorIn;
+ } else {
+ divisor = divisorIn;
+ }
+
+ // check for some particular cases
+ if ( divisor > dividend )
+ {
+ remainder = dividend;
+ }
+ else if ( divisor == dividend )
+ {
+ quotient = 1l;
+ }
+ else
+ {
+ // here: dividend > divisor and both are positive: do unsigned division
+ size_t nBits = 64u;
+ wxLongLongWx d;
+
+ while ( remainder < divisor )
+ {
+ remainder <<= 1;
+ if ( IS_MSB_SET(dividend) )
+ {
+ remainder |= 1;
+ }
+
+ d = dividend;
+ dividend <<= 1;
+
+ nBits--;
+ }
+
+ // undo the last loop iteration
+ dividend = d;
+ remainder >>= 1;
+ nBits++;
+
+ for ( size_t i = 0; i < nBits; i++ )
+ {
+ remainder <<= 1;
+ if ( IS_MSB_SET(dividend) )
+ {
+ remainder |= 1;
+ }
+
+ wxLongLongWx t = remainder - divisor;
+ dividend <<= 1;
+ quotient <<= 1;
+ if ( !IS_MSB_SET(t) )
+ {
+ quotient |= 1;
+
+ remainder = t;
+ }
+ }
+ }
+
+ remainderIO = remainder;
+
+ // adjust signs
+ if ( negRemainder )
+ {
+ remainderIO = -remainderIO;
+ }
+
+ if ( negQuotient )
+ {
+ quotient = -quotient;
+ }
+}
+
+void wxULongLongWx::Divide(const wxULongLongWx& divisorIn,
+ wxULongLongWx& quotient,
+ wxULongLongWx& remainder) const