private uint DivByULong(uint iDivisor)
{
ulong dwlDivisor = iDivisor;
ulong dwlAccum = 0; //Accumulated sum
uint ulQuotientCur = 0; // Value of the current UI4 of the quotient
bool fAllZero = true; // All of the quotient (so far) has been 0
int iData; //Which UI4 currently on
// Check for zero divisor.
if (dwlDivisor == 0)
throw new DivideByZeroException(SQLResource.s_divideByZeroMessage);
// Copy into array, so that we can iterate through the data
uint[] rguiData = new uint[4] { _data1, _data2, _data3, _data4 };
// Start from the MS UI4 of quotient, divide by divisor, placing result
// in quotient and carrying the remainder.
// DWORDLONG sufficient accumulator since:
// Accum < Divisor <= 2^32 - 1 at start each loop
// initially,and mod end previous loop
// Accum*2^32 < 2^64 - 2^32
// multiply both side by 2^32 (x_dwlBaseUI4)
// Accum*2^32 + m_rgulData < 2^64
// rglData < 2^32
for (iData = _bLen; iData > 0; iData--)
{
Debug.Assert(dwlAccum < dwlDivisor);
dwlAccum = (dwlAccum << 32) + rguiData[iData - 1]; // dwlA*x_dwlBaseUI4 + rglData
Debug.Assert((dwlAccum / dwlDivisor) < s_ulInt32Base);
//Update dividend to the quotient.
ulQuotientCur = (uint)(dwlAccum / dwlDivisor);
rguiData[iData - 1] = ulQuotientCur;
//Remainder to be carried to the next lower significant byte.
dwlAccum = dwlAccum % dwlDivisor;
// While current part of quotient still 0, reduce length
if (fAllZero && (ulQuotientCur == 0))
{
_bLen--;
}
else
{
fAllZero = false;
}
}
StoreFromWorkingArray(rguiData);
// If result is 0, preserve sign but set length to 5
if (fAllZero)
_bLen = 1;
AssertValid();
// return the remainder
Debug.Assert(dwlAccum < s_ulInt32Base);
return (uint)dwlAccum;
}