22#include <osl/diagnose.h>
46 memcpy(
static_cast<void*
>(
this),
static_cast<const void*
>(&rVal),
sizeof(
BigInt ) );
57 nTmp = -
static_cast<sal_Int64
>(
nVal);
65 nNum[0] =
static_cast<sal_uInt16
>(nTmp & 0xffffL);
66 nNum[1] =
static_cast<sal_uInt16
>(nTmp >> 16);
67 if ( nTmp & 0xffff0000L )
86 else if (
nNum[1] & 0x8000 )
89 newVal = (
static_cast<sal_Int32
>(
nNum[1]) << 16) +
nNum[0];
100 else if (
nVal & 0xFFFF0000L )
109 for (
int i = 0;
i < rVal.
nLen;
i++ )
111 sal_uInt32 nTmp =
static_cast<sal_uInt32
>(rVal.
nNum[
i]) *
static_cast<sal_uInt32
>(nMul) + nK;
112 nK =
static_cast<sal_uInt16
>(nTmp >> 16);
113 nNum[
i] =
static_cast<sal_uInt16
>(nTmp);
130 for (
int i =
nLen - 1;
i >= 0;
i-- )
132 sal_uInt32 nTmp =
static_cast<sal_uInt32
>(
nNum[
i]) + (nK << 16);
133 nNum[
i] =
static_cast<sal_uInt16
>(nTmp / nDiv);
136 rRem =
static_cast<sal_uInt16
>(nK);
168 for (
i = rB.
nLen;
i < len;
i++)
181 for (
i = 0, k = 0;
i < len;
i++) {
182 nZ =
static_cast<sal_Int32
>(
nNum[
i]) +
static_cast<sal_Int32
>(rB.
nNum[
i]) + k;
187 rErg.
nNum[
i] =
static_cast<sal_uInt16
>(nZ & 0xffffL);
227 for (
i = rB.
nLen;
i < len;
i++)
239 for (
i = 0, k = 0;
i < len;
i++)
241 nZ =
static_cast<sal_Int32
>(
nNum[
i]) -
static_cast<sal_Int32
>(rB.
nNum[
i]) + k;
246 rErg.
nNum[
i] =
static_cast<sal_uInt16
>(nZ & 0xffffL);
252 for (
i = 0, k = 0;
i < len;
i++)
254 nZ =
static_cast<sal_Int32
>(rB.
nNum[
i]) -
static_cast<sal_Int32
>(
nNum[
i]) + k;
259 rErg.
nNum[
i] =
static_cast<sal_uInt16
>(nZ & 0xffffL);
291 for (
i = 0;
i < rErg.
nLen;
i++)
294 for (j = 0; j < rB.
nLen; j++)
296 for (
i = 0, k = 0;
i <
nLen;
i++)
298 nZ =
static_cast<sal_uInt32
>(
nNum[
i]) *
static_cast<sal_uInt32
>(rB.
nNum[j]) +
299 static_cast<sal_uInt32
>(rErg.
nNum[
i + j]) + k;
300 rErg.
nNum[
i + j] =
static_cast<sal_uInt16
>(nZ & 0xffffU);
303 rErg.
nNum[
i + j] =
static_cast<sal_uInt16
>(k);
310 sal_uInt16 nK, nQ, nMult;
311 sal_uInt16 nLenB = rB.
nLen;
312 sal_uInt16 nLenB1 = rB.
nLen - 1;
315 nMult =
static_cast<sal_uInt16
>(0x10000L / (
static_cast<sal_Int32
>(rB.
nNum[nLenB1]) + 1));
317 aTmpA.
Mult( *
this, nMult );
324 aTmpB.
Mult( rB, nMult );
326 for (j = aTmpA.
nLen - 1; j >= nLenB; j--)
328 sal_uInt32 nTmp = (
static_cast<sal_uInt32
>(aTmpA.
nNum[j]) << 16 ) + aTmpA.
nNum[j - 1];
329 if (aTmpA.
nNum[j] == aTmpB.
nNum[nLenB1])
332 nQ =
static_cast<sal_uInt16
>(nTmp / aTmpB.
nNum[nLenB1]);
334 if ( (
static_cast<sal_uInt32
>(aTmpB.
nNum[nLenB1 - 1]) * nQ) >
335 ((nTmp -
static_cast<sal_uInt32
>(aTmpB.
nNum[nLenB1]) * nQ) << 16) + aTmpA.
nNum[j - 2])
339 for (
i = 0;
i < nLenB;
i++)
341 nTmp =
static_cast<sal_uInt32
>(aTmpA.
nNum[j - nLenB +
i])
342 - (
static_cast<sal_uInt32
>(aTmpB.
nNum[
i]) * nQ)
344 aTmpA.
nNum[j - nLenB +
i] =
static_cast<sal_uInt16
>(nTmp);
345 nK =
static_cast<sal_uInt16
>(nTmp >> 16);
347 nK =
static_cast<sal_uInt16
>(0x10000U - nK);
349 sal_uInt16& rNum( aTmpA.
nNum[j - nLenB +
i] );
351 if (aTmpA.
nNum[j - nLenB +
i] == 0)
352 rErg.
nNum[j - nLenB] = nQ;
355 rErg.
nNum[j - nLenB] = nQ - 1;
357 for (
i = 0;
i < nLenB;
i++)
359 nTmp = aTmpA.
nNum[j - nLenB +
i] + aTmpB.
nNum[
i] + nK;
360 aTmpA.
nNum[j - nLenB +
i] =
static_cast<sal_uInt16
>(nTmp & 0xFFFFL);
361 if (nTmp & 0xFFFF0000L)
376 sal_uInt16 nK, nQ, nMult;
377 sal_Int16 nLenB = rB.
nLen;
378 sal_Int16 nLenB1 = rB.
nLen - 1;
381 nMult =
static_cast<sal_uInt16
>(0x10000L / (
static_cast<sal_Int32
>(rB.
nNum[nLenB1]) + 1));
383 aTmpA.
Mult( *
this, nMult);
390 aTmpB.
Mult( rB, nMult);
392 for (j = aTmpA.
nLen - 1; j >= nLenB; j--)
394 sal_uInt32 nTmp = (
static_cast<sal_uInt32
>(aTmpA.
nNum[j]) << 16 ) + aTmpA.
nNum[j - 1];
395 if (aTmpA.
nNum[j] == aTmpB.
nNum[nLenB1])
398 nQ =
static_cast<sal_uInt16
>(nTmp / aTmpB.
nNum[nLenB1]);
400 if ( (
static_cast<sal_uInt32
>(aTmpB.
nNum[nLenB1 - 1]) * nQ) >
401 ((nTmp - aTmpB.
nNum[nLenB1] * nQ) << 16) + aTmpA.
nNum[j - 2])
405 for (
i = 0;
i < nLenB;
i++)
407 nTmp =
static_cast<sal_uInt32
>(aTmpA.
nNum[j - nLenB +
i])
408 - (
static_cast<sal_uInt32
>(aTmpB.
nNum[
i]) * nQ)
410 aTmpA.
nNum[j - nLenB +
i] =
static_cast<sal_uInt16
>(nTmp);
411 nK =
static_cast<sal_uInt16
>(nTmp >> 16);
413 nK =
static_cast<sal_uInt16
>(0x10000U - nK);
415 sal_uInt16& rNum( aTmpA.
nNum[j - nLenB +
i] );
417 if (aTmpA.
nNum[j - nLenB +
i] == 0)
418 rErg.
nNum[j - nLenB] = nQ;
421 rErg.
nNum[j - nLenB] = nQ - 1;
423 for (
i = 0;
i < nLenB;
i++) {
424 nTmp = aTmpA.
nNum[j - nLenB +
i] + aTmpB.
nNum[
i] + nK;
425 aTmpA.
nNum[j - nLenB +
i] =
static_cast<sal_uInt16
>(nTmp & 0xFFFFL);
426 if (nTmp & 0xFFFF0000L)
435 rErg.
Div( nMult, nQ );
472 if ( rBigInt.
nLen != 0 )
473 memcpy(
static_cast<void*
>(
this),
static_cast<const void*
>(&rBigInt),
sizeof(
BigInt ) );
485 auto p = rString.begin();
486 auto pEnd = rString.end();
496 while(
p != pEnd && *
p >=
'0' && *
p <=
'9' )
532 nNum[
i] =
static_cast<sal_uInt16
>(fmod(
nValue, 65536.0 ));
550 if (
nValue & 0x80000000U )
553 nNum[0] =
static_cast<sal_uInt16
>(
nValue & 0xffffU);
554 nNum[1] =
static_cast<sal_uInt16
>(
nValue >> 16);
578 for (
int i = 0; (
i !=
sizeof(sal_uInt64) / 2) && (nUValue != 0); ++
i)
580 nNum[
i] =
static_cast<sal_uInt16
>(nUValue & 0xffffUL);
581 nUValue = nUValue >> 16;
587BigInt::operator double()
const
590 return static_cast<double>(nVal);
594 double nRet =
static_cast<double>(
static_cast<sal_uInt32
>(nNum[
i]));
600 nRet +=
static_cast<double>(
static_cast<sal_uInt32
>(nNum[
i]));
612 if (
this == &rBigInt)
615 if ( rBigInt.
nLen != 0 )
616 memcpy(
static_cast<void*
>(
this),
static_cast<const void*
>(&rBigInt),
sizeof(
BigInt ) );
662 if ( (
nVal < 0) == (rVal.
nVal < 0) )
679 static const sal_Int32 MY_MAXSHORT = 0x00007fff;
680 static const sal_Int32 MY_MINSHORT = -MY_MAXSHORT;
683 &&
nVal <= MY_MAXSHORT && rVal.
nVal <= MY_MAXSHORT
684 &&
nVal >= MY_MINSHORT && rVal.
nVal >= MY_MINSHORT )
702 if ( rVal.
nLen == 0 )
704 if ( rVal.
nVal == 0 )
706 OSL_FAIL(
"BigInt::operator/ --> divide by zero" );
717 if ( rVal.
nVal == 1 )
720 if ( rVal.
nVal == -1 )
726 if ( rVal.
nVal <= 0xFFFF && rVal.
nVal >= -0xFFFF )
732 nTmp =
static_cast<sal_uInt16
>(-rVal.
nVal);
736 nTmp =
static_cast<sal_uInt16
>(rVal.
nVal);
761 if ( rVal.
nLen == 0 )
763 if ( rVal.
nVal == 0 )
765 OSL_FAIL(
"BigInt::operator/ --> divide by zero" );
776 if ( rVal.
nVal <= 0xFFFF && rVal.
nVal >= -0xFFFF )
782 nTmp =
static_cast<sal_uInt16
>(-rVal.
nVal);
786 nTmp =
static_cast<sal_uInt16
>(rVal.
nVal);
808 if (rVal1.
nLen == 0 && rVal2.
nLen == 0)
820 if (rVal1.
nLen == 0 && rVal2.
nLen == 0)
842 if ( aVal.
IsNeg() != ( nDiv < 0 ) )
const sal_Int32 MY_MINLONG
bool operator==(const BigInt &rVal1, const BigInt &rVal2)
bool operator<(const BigInt &rVal1, const BigInt &rVal2)
const sal_Int32 MY_MAXLONG
The range in which we can perform add/sub without fear of overflow.
TOOLS_DLLPRIVATE void MultLong(BigInt const &, BigInt &) const
static tools::Long Scale(tools::Long nVal, tools::Long nMult, tools::Long nDiv)
TOOLS_DLLPRIVATE void Normalize()
BigInt & operator*=(const BigInt &rVal)
TOOLS_DLLPRIVATE void Mult(BigInt const &, sal_uInt16)
TOOLS_DLLPRIVATE void MakeBigInt(BigInt const &)
TOOLS_DLLPRIVATE void DivLong(BigInt const &, BigInt &) const
TOOLS_DLLPRIVATE bool ABS_IsLess(BigInt const &) const
TOOLS_DLLPRIVATE void Div(sal_uInt16, sal_uInt16 &)
TOOLS_DLLPRIVATE bool IsLess(BigInt const &) const
BigInt & operator+=(const BigInt &rVal)
BigInt & operator=(const BigInt &rVal)
sal_uInt16 nNum[MAX_DIGITS]
TOOLS_DLLPRIVATE void AddLong(BigInt &, BigInt &)
TOOLS_DLLPRIVATE void ModLong(BigInt const &, BigInt &) const
BigInt & operator-=(const BigInt &rVal)
BigInt & operator%=(const BigInt &rVal)
BigInt & operator/=(const BigInt &rVal)
TOOLS_DLLPRIVATE void SubLong(BigInt &, BigInt &)