Update contrib.
1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\math\largeint.cpp
20 TLargeIntBase::TLargeIntBase(TInt n)
23 Mem::FillZ(iX,n*sizeof(TUint32));
26 TLargeIntBase::TLargeIntBase(TInt n, TInt32 aSigned32)
29 TUint f = (aSigned32<0) ? 0xff : 0;
30 Mem::Fill(iX,n*sizeof(TUint32),f);
34 TLargeIntBase::TLargeIntBase(TInt n, TUint32 aUnsigned32)
37 Mem::FillZ(iX,n*sizeof(TUint32));
41 TLargeIntBase::TLargeIntBase(TInt n, const TLargeIntBase& aSrc, TMode aMode)
44 __ASSERT(aMode==ETruncate||n>=aSrc.iC); // if not truncating, dest can't be shorter than source
45 __ASSERT(aMode!=ETruncate||n<=aSrc.iC); // if truncating, dest can't be longer than source
46 TInt min = Min(n,aSrc.iC);
50 if (aMode==ETruncate || n==aSrc.iC)
52 TUint32 f = (aMode==ESignExtend && (iX[i-1] & 0x80000000u)) ? 0xffffffffu : 0;
57 TLargeIntBase::TLargeIntBase(TInt n, const TUint32* aPtr)
60 Mem::Copy(iX, aPtr, n*sizeof(TUint32));
63 TLargeIntBase::TLargeIntBase(TInt n, const Int64& aSigned64)
67 Mem::Copy(iX, &aSigned64, 8);
68 TUint f = (iX[1] & 0x80000000u) ? 0xff : 0;
69 Mem::Fill(iX+2,(n-2)*sizeof(TUint32),f);
72 TLargeIntBase::TLargeIntBase(TInt n, const Uint64& aUnsigned64)
76 Mem::Copy(iX, &aUnsigned64, 8);
77 Mem::FillZ(iX+2,(n-2)*sizeof(TUint32));
80 void TLargeIntBase::Not()
87 void TLargeIntBase::Neg()
93 void TLargeIntBase::Abs()
95 if (iX[iC-1] & 0x80000000u)
99 void TLargeIntBase::Inc()
102 for (i=0; i<iC && ++iX[i]==0; ++i) {}
105 void TLargeIntBase::Dec()
108 for (i=0; i<iC && --iX[i]==0xffffffffu; ++i) {}
111 TUint32 TLargeIntBase::Lsl()
117 TUint32 x = (iX[i]<<1) | (c>>31);
124 TUint32 TLargeIntBase::Lsr()
128 for (i=iC-1; i>=0; --i)
130 TUint32 x = (iX[i]>>1) | (c<<31);
137 TUint32 TLargeIntBase::Asr()
140 TUint32 c = iX[i]>>31;
143 TUint32 x = (iX[i]>>1) | (c<<31);
150 void TLargeIntBase::Lsl(TInt aCount)
156 void TLargeIntBase::Lsr(TInt aCount)
162 void TLargeIntBase::Asr(TInt aCount)
168 void TLargeIntBase::Add(const TLargeIntBase& a)
178 iX[i] = (s + (c>>31));
179 TUint32 g = (x & y) | ((x | y) &~ s);
180 TUint32 p = ~s ? 0 : s;
185 void TLargeIntBase::Sub(const TLargeIntBase& a)
189 TUint32 c = 0x80000000u;
193 TUint32 y = ~a.iX[i];
195 iX[i] = (s + (c>>31));
196 TUint32 g = (x & y) | ((x | y) &~ s);
197 TUint32 p = ~s ? 0 : s;
202 void TLargeIntBase::Mul(const TLargeIntBase& a)
205 TUint32 temp[64]; // HACK!!
206 Mem::Copy(temp, this, (iC+1)*sizeof(TUint32));
207 TLargeIntBase& b = *(TLargeIntBase*)temp;
208 new (this) TLargeIntBase(iC,TUint32(0u));
210 for (i=0; i<32*iC; ++i)
218 void TLargeIntBase::DivU(const TLargeIntBase& aDivisor, TLargeIntBase& aRem)
220 __ASSERT(aDivisor.iC==iC);
221 __ASSERT(aRem.iC==iC);
222 new (&aRem) TLargeIntBase(iC,TUint32(0u));
224 for (i=0; i<iC*32; ++i)
229 if (aRem.Hs(aDivisor))
230 aRem.Sub(aDivisor), Inc();
234 void TLargeIntBase::DivS(const TLargeIntBase& aDivisor, TLargeIntBase& aRem)
236 __ASSERT(aDivisor.iC==iC);
237 __ASSERT(aRem.iC==iC);
238 TUint32 temp[64]; // HACK!!
239 Mem::Copy(temp, &aDivisor, (iC+1)*sizeof(TUint32));
240 TLargeIntBase& divisor = *(TLargeIntBase*)temp;
241 TUint32 rs = iX[iC-1];
242 TUint32 qs = divisor.iX[iC-1] ^ rs;
246 if (rs & 0x80000000u)
248 if (qs & 0x80000000u)
252 TInt TLargeIntBase::CompareU(const TLargeIntBase& a) const
256 for (i=iC-1; i>=0; --i)
268 TInt TLargeIntBase::CompareS(const TLargeIntBase& a) const
272 TUint32 m = 0x80000000u;
273 for (i=iC-1; i>=0; --i)
275 TUint32 x = iX[i] ^ m;
276 TUint32 y = a.iX[i] ^ m;