From 33b8f7c10baead5fdd24d9b68caab54052bd00ba Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Wed, 6 Jan 2021 10:41:22 +0200 Subject: bIsBig member is redundant we can just use nLen != 0 to get the same information Change-Id: I2406322aa8b7cfc5e276818df763c6de08397454 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/108834 Tested-by: Jenkins Reviewed-by: Noel Grandin --- include/tools/bigint.hxx | 30 +++++++++------------ tools/source/generic/bigint.cxx | 59 +++++++++++++++-------------------------- 2 files changed, 34 insertions(+), 55 deletions(-) diff --git a/include/tools/bigint.hxx b/include/tools/bigint.hxx index 3299d56c5374..14efb7e69248 100644 --- a/include/tools/bigint.hxx +++ b/include/tools/bigint.hxx @@ -33,9 +33,8 @@ private: sal_Int32 nVal; sal_uInt16 nNum[MAX_DIGITS]; }; - sal_uInt8 nLen : 5; // current length - bool bIsNeg : 1, // Is Sign negative? - bIsBig : 1; // if true , value is in nNum array + sal_uInt8 nLen : 5; // current length, if 0, data is in nVal, otherwise data is in nNum + bool bIsNeg : 1; // Is Sign negative? TOOLS_DLLPRIVATE void MakeBigInt(BigInt const &); TOOLS_DLLPRIVATE void Normalize(); @@ -54,7 +53,6 @@ public: : nVal(0) , nLen(0) , bIsNeg(false) - , bIsBig(false) { } @@ -62,7 +60,6 @@ public: : nVal(nValue) , nLen(0) , bIsNeg(false) - , bIsBig(false) { } @@ -71,7 +68,6 @@ public: : nVal(nValue) , nLen(0) , bIsNeg(false) - , bIsBig(false) { } #endif @@ -93,7 +89,7 @@ public: bool IsNeg() const; bool IsZero() const; - bool IsLong() const { return !bIsBig; } + bool IsLong() const { return nLen == 0; } void Abs(); @@ -127,7 +123,7 @@ public: inline BigInt::operator sal_Int16() const { - if ( !bIsBig && nVal >= SAL_MIN_INT16 && nVal <= SAL_MAX_INT16 ) + if ( nLen == 0 && nVal >= SAL_MIN_INT16 && nVal <= SAL_MAX_INT16 ) return static_cast(nVal); assert(false && "out of range"); return 0; @@ -135,7 +131,7 @@ inline BigInt::operator sal_Int16() const inline BigInt::operator sal_uInt16() const { - if ( !bIsBig && nVal >= 0 && nVal <= SAL_MAX_UINT16 ) + if ( nLen == 0 && nVal >= 0 && nVal <= SAL_MAX_UINT16 ) return static_cast(nVal); assert(false && "out of range"); return 0; @@ -143,7 +139,7 @@ inline BigInt::operator sal_uInt16() const inline BigInt::operator sal_Int32() const { - if (!bIsBig) + if (nLen == 0) return nVal; assert(false && "out of range"); return 0; @@ -151,7 +147,7 @@ inline BigInt::operator sal_Int32() const inline BigInt::operator sal_uInt32() const { - if ( !bIsBig && nVal >= 0 ) + if ( nLen == 0 && nVal >= 0 ) return static_cast(nVal); assert(false && "out of range"); return 0; @@ -161,7 +157,7 @@ inline BigInt::operator sal_uInt32() const inline BigInt::operator tools::Long() const { // Clamp to int32 since long is int32 on Windows. - if (!bIsBig) + if (nLen == 0) return nVal; assert(false && "out of range"); return 0; @@ -170,15 +166,15 @@ inline BigInt::operator tools::Long() const inline BigInt& BigInt::operator =( sal_Int32 nValue ) { - bIsBig = false; - nVal = nValue; + nLen = 0; + nVal = nValue; return *this; } inline bool BigInt::IsNeg() const { - if ( !bIsBig ) + if ( nLen == 0 ) return (nVal < 0); else return bIsNeg; @@ -186,7 +182,7 @@ inline bool BigInt::IsNeg() const inline bool BigInt::IsZero() const { - if ( bIsBig ) + if ( nLen != 0 ) return false; else return (nVal == 0); @@ -194,7 +190,7 @@ inline bool BigInt::IsZero() const inline void BigInt::Abs() { - if ( bIsBig ) + if ( nLen != 0 ) bIsNeg = false; else if ( nVal < 0 ) nVal = -nVal; diff --git a/tools/source/generic/bigint.cxx b/tools/source/generic/bigint.cxx index 6616ef76f423..5c8c7771a2c5 100644 --- a/tools/source/generic/bigint.cxx +++ b/tools/source/generic/bigint.cxx @@ -41,7 +41,7 @@ const sal_Int32 MY_MINLONG = -MY_MAXLONG; // TODO: Needs conversion to sal_uInt16/INT16/sal_uInt32/sal_Int32 void BigInt::MakeBigInt( const BigInt& rVal ) { - if ( rVal.bIsBig ) + if ( rVal.nLen != 0 ) { memcpy( static_cast(this), static_cast(&rVal), sizeof( BigInt ) ); while ( nLen > 1 && nNum[nLen-1] == 0 ) @@ -50,7 +50,6 @@ void BigInt::MakeBigInt( const BigInt& rVal ) else { nVal = rVal.nVal; - bIsBig = true; sal_uInt32 nTmp; if (nVal < 0) { @@ -74,7 +73,7 @@ void BigInt::MakeBigInt( const BigInt& rVal ) void BigInt::Normalize() { - if ( bIsBig ) + if ( nLen != 0 ) { while ( nLen > 1 && nNum[nLen-1] == 0 ) nLen--; @@ -88,7 +87,7 @@ void BigInt::Normalize() else nVal = (static_cast(nNum[1]) << 16) + nNum[0]; - bIsBig = false; + nLen = 0; if ( bIsNeg ) nVal = -nVal; @@ -120,7 +119,6 @@ void BigInt::Mult( const BigInt &rVal, sal_uInt16 nMul ) else nLen = rVal.nLen; - bIsBig = true; bIsNeg = rVal.bIsNeg; } @@ -195,7 +193,6 @@ void BigInt::AddLong( BigInt& rB, BigInt& rErg ) // Set length and sign rErg.nLen = len; rErg.bIsNeg = bIsNeg && rB.bIsNeg; - rErg.bIsBig = true; } // If one of the values is negative, perform subtraction instead else if (bIsNeg) @@ -263,7 +260,6 @@ void BigInt::SubLong( BigInt& rB, BigInt& rErg ) rErg.bIsNeg = !bIsNeg; } rErg.nLen = len; - rErg.bIsBig = true; } // If one of the values is negative, perform addition instead else if (bIsNeg) @@ -288,7 +284,6 @@ void BigInt::MultLong( const BigInt& rB, BigInt& rErg ) const sal_uInt32 nZ, k; rErg.bIsNeg = bIsNeg != rB.bIsNeg; - rErg.bIsBig = true; rErg.nLen = nLen + rB.nLen; for (i = 0; i < rErg.nLen; i++) @@ -370,7 +365,6 @@ void BigInt::DivLong( const BigInt& rB, BigInt& rErg ) const } rErg.bIsNeg = bIsNeg != rB.bIsNeg; - rErg.bIsBig = true; rErg.nLen = nLen - rB.nLen + 1; } @@ -441,7 +435,7 @@ void BigInt::ModLong( const BigInt& rB, BigInt& rErg ) const bool BigInt::ABS_IsLess( const BigInt& rB ) const { - if (bIsBig || rB.bIsBig) + if (nLen != 0 || rB.nLen != 0) { BigInt nA, nB; nA.MakeBigInt( *this ); @@ -473,20 +467,16 @@ BigInt::BigInt( const BigInt& rBigInt ) : nLen(0) , bIsNeg(false) { - if ( rBigInt.bIsBig ) + if ( rBigInt.nLen != 0 ) memcpy( static_cast(this), static_cast(&rBigInt), sizeof( BigInt ) ); else - { - bIsBig = false; - nVal = rBigInt.nVal; - } + nVal = rBigInt.nVal; } BigInt::BigInt( const OUString& rString ) : nLen(0) { bIsNeg = false; - bIsBig = false; nVal = 0; bool bNeg = false; @@ -502,7 +492,7 @@ BigInt::BigInt( const OUString& rString ) *this += *p - '0'; p++; } - if ( bIsBig ) + if ( nLen != 0 ) bIsNeg = bNeg; else if( bNeg ) nVal = -nVal; @@ -523,14 +513,11 @@ BigInt::BigInt( double nValue ) if ( nValue < 1 ) { - bIsBig = false; nVal = 0; nLen = 0; } else { - bIsBig = true; - int i=0; while ( ( nValue > 65536.0 ) && ( i < MAX_DIGITS ) ) @@ -555,7 +542,6 @@ BigInt::BigInt( sal_uInt32 nValue ) { if ( nValue & 0x80000000U ) { - bIsBig = true; bIsNeg = false; nNum[0] = static_cast(nValue & 0xffffU); nNum[1] = static_cast(nValue >> 16); @@ -563,7 +549,6 @@ BigInt::BigInt( sal_uInt32 nValue ) } else { - bIsBig = false; bIsNeg = false; nVal = nValue; nLen = 0; @@ -578,12 +563,10 @@ BigInt::BigInt( sal_Int64 nValue ) if ((nValue >= SAL_MIN_INT32) && (nValue <= SAL_MAX_INT32)) { - bIsBig = false; - nVal = static_cast(nValue); + nVal = static_cast(nValue); } else { - bIsBig = true; sal_uInt64 nUValue = static_cast(bIsNeg ? -nValue : nValue); for (int i = 0; (i != sizeof(sal_uInt64) / 2) && (nUValue != 0); ++i) { @@ -596,7 +579,7 @@ BigInt::BigInt( sal_Int64 nValue ) BigInt::operator double() const { - if ( !bIsBig ) + if ( nLen == 0 ) return static_cast(nVal); else { @@ -622,19 +605,19 @@ BigInt& BigInt::operator=( const BigInt& rBigInt ) if (this == &rBigInt) return *this; - if ( rBigInt.bIsBig ) + if ( rBigInt.nLen != 0 ) memcpy( static_cast(this), static_cast(&rBigInt), sizeof( BigInt ) ); else { - bIsBig = false; - nVal = rBigInt.nVal; + nLen = 0; + nVal = rBigInt.nVal; } return *this; } BigInt& BigInt::operator+=( const BigInt& rVal ) { - if ( !bIsBig && !rVal.bIsBig ) + if ( nLen == 0 && rVal.nLen == 0 ) { if( nVal <= MY_MAXLONG && rVal.nVal <= MY_MAXLONG && nVal >= MY_MINLONG && rVal.nVal >= MY_MINLONG ) @@ -660,7 +643,7 @@ BigInt& BigInt::operator+=( const BigInt& rVal ) BigInt& BigInt::operator-=( const BigInt& rVal ) { - if ( !bIsBig && !rVal.bIsBig ) + if ( nLen == 0 && rVal.nLen == 0 ) { if ( nVal <= MY_MAXLONG && rVal.nVal <= MY_MAXLONG && nVal >= MY_MINLONG && rVal.nVal >= MY_MINLONG ) @@ -689,7 +672,7 @@ BigInt& BigInt::operator*=( const BigInt& rVal ) static const sal_Int32 MY_MAXSHORT = 0x00007fff; static const sal_Int32 MY_MINSHORT = -MY_MAXSHORT; - if ( !bIsBig && !rVal.bIsBig + if ( nLen == 0 && rVal.nLen == 0 && nVal <= MY_MAXSHORT && rVal.nVal <= MY_MAXSHORT && nVal >= MY_MINSHORT && rVal.nVal >= MY_MINSHORT ) // TODO: not optimal !!! W.P. @@ -709,7 +692,7 @@ BigInt& BigInt::operator*=( const BigInt& rVal ) BigInt& BigInt::operator/=( const BigInt& rVal ) { - if ( !rVal.bIsBig ) + if ( rVal.nLen == 0 ) { if ( rVal.nVal == 0 ) { @@ -717,7 +700,7 @@ BigInt& BigInt::operator/=( const BigInt& rVal ) return *this; } - if ( !bIsBig ) + if ( nLen == 0 ) { // No overflows may occur here nVal /= rVal.nVal; @@ -768,7 +751,7 @@ BigInt& BigInt::operator/=( const BigInt& rVal ) BigInt& BigInt::operator%=( const BigInt& rVal ) { - if ( !rVal.bIsBig ) + if ( rVal.nLen == 0 ) { if ( rVal.nVal == 0 ) { @@ -776,7 +759,7 @@ BigInt& BigInt::operator%=( const BigInt& rVal ) return *this; } - if ( !bIsBig ) + if ( nLen == 0 ) { // No overflows may occur here nVal %= rVal.nVal; @@ -815,7 +798,7 @@ BigInt& BigInt::operator%=( const BigInt& rVal ) bool operator==( const BigInt& rVal1, const BigInt& rVal2 ) { - if (!rVal1.bIsBig && !rVal2.bIsBig) + if (rVal1.nLen == 0 && rVal2.nLen == 0) return rVal1.nVal == rVal2.nVal; BigInt nA, nB; @@ -827,7 +810,7 @@ bool operator==( const BigInt& rVal1, const BigInt& rVal2 ) bool operator<( const BigInt& rVal1, const BigInt& rVal2 ) { - if (!rVal1.bIsBig && !rVal2.bIsBig) + if (rVal1.nLen == 0 && rVal2.nLen == 0) return rVal1.nVal < rVal2.nVal; BigInt nA, nB; -- cgit v1.2.3