sl@0: // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32test\math\largeint.cpp sl@0: // sl@0: // sl@0: sl@0: #include "largeint.h" sl@0: sl@0: TLargeIntBase::TLargeIntBase(TInt n) sl@0: : iC(n) sl@0: { sl@0: Mem::FillZ(iX,n*sizeof(TUint32)); sl@0: } sl@0: sl@0: TLargeIntBase::TLargeIntBase(TInt n, TInt32 aSigned32) sl@0: : iC(n) sl@0: { sl@0: TUint f = (aSigned32<0) ? 0xff : 0; sl@0: Mem::Fill(iX,n*sizeof(TUint32),f); sl@0: iX[0]=aSigned32; sl@0: } sl@0: sl@0: TLargeIntBase::TLargeIntBase(TInt n, TUint32 aUnsigned32) sl@0: : iC(n) sl@0: { sl@0: Mem::FillZ(iX,n*sizeof(TUint32)); sl@0: iX[0]=aUnsigned32; sl@0: } sl@0: sl@0: TLargeIntBase::TLargeIntBase(TInt n, const TLargeIntBase& aSrc, TMode aMode) sl@0: : iC(n) sl@0: { sl@0: __ASSERT(aMode==ETruncate||n>=aSrc.iC); // if not truncating, dest can't be shorter than source sl@0: __ASSERT(aMode!=ETruncate||n<=aSrc.iC); // if truncating, dest can't be longer than source sl@0: TInt min = Min(n,aSrc.iC); sl@0: TInt i; sl@0: for (i=0; i=2); sl@0: Mem::Copy(iX, &aSigned64, 8); sl@0: TUint f = (iX[1] & 0x80000000u) ? 0xff : 0; sl@0: Mem::Fill(iX+2,(n-2)*sizeof(TUint32),f); sl@0: } sl@0: sl@0: TLargeIntBase::TLargeIntBase(TInt n, const Uint64& aUnsigned64) sl@0: : iC(n) sl@0: { sl@0: __ASSERT(n>=2); sl@0: Mem::Copy(iX, &aUnsigned64, 8); sl@0: Mem::FillZ(iX+2,(n-2)*sizeof(TUint32)); sl@0: } sl@0: sl@0: void TLargeIntBase::Not() sl@0: { sl@0: TInt i; sl@0: for (i=0; i>31); sl@0: c = iX[i]; sl@0: iX[i] = x; sl@0: } sl@0: return c>>31; sl@0: } sl@0: sl@0: TUint32 TLargeIntBase::Lsr() sl@0: { sl@0: TInt i; sl@0: TUint32 c = 0; sl@0: for (i=iC-1; i>=0; --i) sl@0: { sl@0: TUint32 x = (iX[i]>>1) | (c<<31); sl@0: c = iX[i]; sl@0: iX[i] = x; sl@0: } sl@0: return c&1; sl@0: } sl@0: sl@0: TUint32 TLargeIntBase::Asr() sl@0: { sl@0: TInt i=iC-1; sl@0: TUint32 c = iX[i]>>31; sl@0: for (; i>=0; --i) sl@0: { sl@0: TUint32 x = (iX[i]>>1) | (c<<31); sl@0: c = iX[i]; sl@0: iX[i] = x; sl@0: } sl@0: return c&1; sl@0: } sl@0: sl@0: void TLargeIntBase::Lsl(TInt aCount) sl@0: { sl@0: while(--aCount>=0) sl@0: Lsl(); sl@0: } sl@0: sl@0: void TLargeIntBase::Lsr(TInt aCount) sl@0: { sl@0: while(--aCount>=0) sl@0: Lsr(); sl@0: } sl@0: sl@0: void TLargeIntBase::Asr(TInt aCount) sl@0: { sl@0: while(--aCount>=0) sl@0: Asr(); sl@0: } sl@0: sl@0: void TLargeIntBase::Add(const TLargeIntBase& a) sl@0: { sl@0: __ASSERT(a.iC==iC); sl@0: TInt i; sl@0: TUint32 c = 0; sl@0: for (i=0; i>31)); sl@0: TUint32 g = (x & y) | ((x | y) &~ s); sl@0: TUint32 p = ~s ? 0 : s; sl@0: c = g | (c & p); sl@0: } sl@0: } sl@0: sl@0: void TLargeIntBase::Sub(const TLargeIntBase& a) sl@0: { sl@0: __ASSERT(a.iC==iC); sl@0: TInt i; sl@0: TUint32 c = 0x80000000u; sl@0: for (i=0; i>31)); sl@0: TUint32 g = (x & y) | ((x | y) &~ s); sl@0: TUint32 p = ~s ? 0 : s; sl@0: c = g | (c & p); sl@0: } sl@0: } sl@0: sl@0: void TLargeIntBase::Mul(const TLargeIntBase& a) sl@0: { sl@0: __ASSERT(a.iC==iC); sl@0: TUint32 temp[64]; // HACK!! sl@0: Mem::Copy(temp, this, (iC+1)*sizeof(TUint32)); sl@0: TLargeIntBase& b = *(TLargeIntBase*)temp; sl@0: new (this) TLargeIntBase(iC,TUint32(0u)); sl@0: TInt i; sl@0: for (i=0; i<32*iC; ++i) sl@0: { sl@0: Lsl(); sl@0: if (b.Lsl()) sl@0: Add(a); sl@0: } sl@0: } sl@0: sl@0: void TLargeIntBase::DivU(const TLargeIntBase& aDivisor, TLargeIntBase& aRem) sl@0: { sl@0: __ASSERT(aDivisor.iC==iC); sl@0: __ASSERT(aRem.iC==iC); sl@0: new (&aRem) TLargeIntBase(iC,TUint32(0u)); sl@0: TInt i; sl@0: for (i=0; i=0; --i) sl@0: { sl@0: TUint32 x = iX[i]; sl@0: TUint32 y = a.iX[i]; sl@0: if (x>y) sl@0: return 1; sl@0: if (x=0; --i) sl@0: { sl@0: TUint32 x = iX[i] ^ m; sl@0: TUint32 y = a.iX[i] ^ m; sl@0: m = 0; sl@0: if (x>y) sl@0: return 1; sl@0: if (x