First public contribution.
2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
24 #ifndef __BIGINT_WORDS_H__
25 #define __BIGINT_WORDS_H__
27 #include "../common/inlines.h"
29 inline void ArraySetBit(TUint* aS, TUint aJ)
31 aS[aJ/WORD_BITS] |= (1L << (aJ%WORD_BITS));
34 inline TUint CountWords(const TUint *X, TUint N)
36 while (N && X[N-1]==0)
41 inline void SetWords(TUint *r, TUint a, TUint n)
43 Mem::Fill(r, WordsToBytes(n), a);
46 inline void CopyWords(TUint *r, const TUint *a, TUint n)
48 Mem::Copy(r, a, WordsToBytes(n));
51 inline TUint ShiftWordsLeftByBits(TUint *r, TUint n, TUint shiftBits)
53 assert (shiftBits<WORD_BITS);
56 for (TUint i=0; i<n; i++)
59 r[i] = (u << shiftBits) | carry;
60 carry = u >> (WORD_BITS-shiftBits);
65 inline TUint ShiftWordsRightByBits(TUint *r, TUint n, TUint shiftBits)
67 assert (shiftBits<WORD_BITS);
70 for (int i=n-1; i>=0; i--)
73 r[i] = (u >> shiftBits) | carry;
74 carry = u << (WORD_BITS-shiftBits);
79 inline TUint CryptoMin(TUint aLeft, TUint aRight)
81 return(aLeft<aRight ? aLeft : aRight);
84 inline TUint CryptoMax(TUint aLeft, TUint aRight)
86 return(aLeft<aRight ? aRight : aLeft);
89 inline void ShiftWordsLeftByWords(TUint *r, TUint n, TUint shiftWords)
91 shiftWords = CryptoMin(shiftWords, n);
94 for (TUint i=n-1; i>=shiftWords; i--)
95 r[i] = r[i-shiftWords];
96 SetWords(r, 0, shiftWords);
100 inline void ShiftWordsRightByWords(TUint *r, TUint n, TUint shiftWords)
102 shiftWords = CryptoMin(shiftWords, n);
105 for (TUint i=0; i+shiftWords<n; i++)
106 r[i] = r[i+shiftWords];
107 SetWords(r+n-shiftWords, 0, shiftWords);
111 inline TUint BytePrecision(TUint aValue)
114 if(aValue < 0x10000) aValue <<=16, x-=2;
115 if(aValue < 0x1000000) x--;
120 inline TUint BitPrecision(TUint aValue)
122 if(!aValue) return 0;
124 if(aValue < 0x10000) aValue<<=16, x-=16;
125 if(aValue < 0x1000000) aValue<<=8, x-=8;
126 if(aValue < 0x10000000) aValue<<=4, x-=4;
127 if(aValue < 0x40000000) aValue<<=2, x-=2;
128 if(aValue < 0x80000000) --x;
132 static const TUint RoundupSizeTable[] = {2, 2, 2, 4, 4, 8, 8, 8, 8};
134 static inline TUint RoundupSize(TUint n)
137 return RoundupSizeTable[n];
144 else return 1U << BitPrecision(n-1);