xref: /aosp_15_r20/external/pdfium/third_party/bigint/BigUnsigned.cc (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1*3ac0a46fSAndroid Build Coastguard Worker // Copyright 2014 The PDFium Authors
2*3ac0a46fSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*3ac0a46fSAndroid Build Coastguard Worker // found in the LICENSE file.
4*3ac0a46fSAndroid Build Coastguard Worker 
5*3ac0a46fSAndroid Build Coastguard Worker // Original code by Matt McCutchen, see the LICENSE file.
6*3ac0a46fSAndroid Build Coastguard Worker 
7*3ac0a46fSAndroid Build Coastguard Worker #include "BigUnsigned.hh"
8*3ac0a46fSAndroid Build Coastguard Worker 
9*3ac0a46fSAndroid Build Coastguard Worker // Memory management definitions have moved to the bottom of NumberlikeArray.hh.
10*3ac0a46fSAndroid Build Coastguard Worker 
11*3ac0a46fSAndroid Build Coastguard Worker // The templates used by these constructors and converters are at the bottom of
12*3ac0a46fSAndroid Build Coastguard Worker // BigUnsigned.hh.
13*3ac0a46fSAndroid Build Coastguard Worker 
BigUnsigned(unsigned long x)14*3ac0a46fSAndroid Build Coastguard Worker BigUnsigned::BigUnsigned(unsigned long  x) { initFromPrimitive      (x); }
BigUnsigned(unsigned int x)15*3ac0a46fSAndroid Build Coastguard Worker BigUnsigned::BigUnsigned(unsigned int   x) { initFromPrimitive      (x); }
BigUnsigned(unsigned short x)16*3ac0a46fSAndroid Build Coastguard Worker BigUnsigned::BigUnsigned(unsigned short x) { initFromPrimitive      (x); }
BigUnsigned(long x)17*3ac0a46fSAndroid Build Coastguard Worker BigUnsigned::BigUnsigned(         long  x) { initFromSignedPrimitive(x); }
BigUnsigned(int x)18*3ac0a46fSAndroid Build Coastguard Worker BigUnsigned::BigUnsigned(         int   x) { initFromSignedPrimitive(x); }
BigUnsigned(short x)19*3ac0a46fSAndroid Build Coastguard Worker BigUnsigned::BigUnsigned(         short x) { initFromSignedPrimitive(x); }
20*3ac0a46fSAndroid Build Coastguard Worker 
toUnsignedLong() const21*3ac0a46fSAndroid Build Coastguard Worker unsigned long  BigUnsigned::toUnsignedLong () const { return convertToPrimitive      <unsigned long >(); }
toUnsignedInt() const22*3ac0a46fSAndroid Build Coastguard Worker unsigned int   BigUnsigned::toUnsignedInt  () const { return convertToPrimitive      <unsigned int  >(); }
toUnsignedShort() const23*3ac0a46fSAndroid Build Coastguard Worker unsigned short BigUnsigned::toUnsignedShort() const { return convertToPrimitive      <unsigned short>(); }
toLong() const24*3ac0a46fSAndroid Build Coastguard Worker long           BigUnsigned::toLong         () const { return convertToSignedPrimitive<         long >(); }
toInt() const25*3ac0a46fSAndroid Build Coastguard Worker int            BigUnsigned::toInt          () const { return convertToSignedPrimitive<         int  >(); }
toShort() const26*3ac0a46fSAndroid Build Coastguard Worker short          BigUnsigned::toShort        () const { return convertToSignedPrimitive<         short>(); }
27*3ac0a46fSAndroid Build Coastguard Worker 
28*3ac0a46fSAndroid Build Coastguard Worker // BIT/BLOCK ACCESSORS
29*3ac0a46fSAndroid Build Coastguard Worker 
setBlock(Index i,Blk newBlock)30*3ac0a46fSAndroid Build Coastguard Worker void BigUnsigned::setBlock(Index i, Blk newBlock) {
31*3ac0a46fSAndroid Build Coastguard Worker 	if (newBlock == 0) {
32*3ac0a46fSAndroid Build Coastguard Worker 		if (i < len) {
33*3ac0a46fSAndroid Build Coastguard Worker 			blk[i] = 0;
34*3ac0a46fSAndroid Build Coastguard Worker 			zapLeadingZeros();
35*3ac0a46fSAndroid Build Coastguard Worker 		}
36*3ac0a46fSAndroid Build Coastguard Worker 		// If i >= len, no effect.
37*3ac0a46fSAndroid Build Coastguard Worker 	} else {
38*3ac0a46fSAndroid Build Coastguard Worker 		if (i >= len) {
39*3ac0a46fSAndroid Build Coastguard Worker 			// The nonzero block extends the number.
40*3ac0a46fSAndroid Build Coastguard Worker 			allocateAndCopy(i+1);
41*3ac0a46fSAndroid Build Coastguard Worker 			// Zero any added blocks that we aren't setting.
42*3ac0a46fSAndroid Build Coastguard Worker 			for (Index j = len; j < i; j++)
43*3ac0a46fSAndroid Build Coastguard Worker 				blk[j] = 0;
44*3ac0a46fSAndroid Build Coastguard Worker 			len = i+1;
45*3ac0a46fSAndroid Build Coastguard Worker 		}
46*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = newBlock;
47*3ac0a46fSAndroid Build Coastguard Worker 	}
48*3ac0a46fSAndroid Build Coastguard Worker }
49*3ac0a46fSAndroid Build Coastguard Worker 
50*3ac0a46fSAndroid Build Coastguard Worker /* Evidently the compiler wants BigUnsigned:: on the return type because, at
51*3ac0a46fSAndroid Build Coastguard Worker  * that point, it hasn't yet parsed the BigUnsigned:: on the name to get the
52*3ac0a46fSAndroid Build Coastguard Worker  * proper scope. */
bitLength() const53*3ac0a46fSAndroid Build Coastguard Worker BigUnsigned::Index BigUnsigned::bitLength() const {
54*3ac0a46fSAndroid Build Coastguard Worker 	if (isZero())
55*3ac0a46fSAndroid Build Coastguard Worker 		return 0;
56*3ac0a46fSAndroid Build Coastguard Worker 	else {
57*3ac0a46fSAndroid Build Coastguard Worker 		Blk leftmostBlock = getBlock(len - 1);
58*3ac0a46fSAndroid Build Coastguard Worker 		Index leftmostBlockLen = 0;
59*3ac0a46fSAndroid Build Coastguard Worker 		while (leftmostBlock != 0) {
60*3ac0a46fSAndroid Build Coastguard Worker 			leftmostBlock >>= 1;
61*3ac0a46fSAndroid Build Coastguard Worker 			leftmostBlockLen++;
62*3ac0a46fSAndroid Build Coastguard Worker 		}
63*3ac0a46fSAndroid Build Coastguard Worker 		return leftmostBlockLen + (len - 1) * N;
64*3ac0a46fSAndroid Build Coastguard Worker 	}
65*3ac0a46fSAndroid Build Coastguard Worker }
66*3ac0a46fSAndroid Build Coastguard Worker 
setBit(Index bi,bool newBit)67*3ac0a46fSAndroid Build Coastguard Worker void BigUnsigned::setBit(Index bi, bool newBit) {
68*3ac0a46fSAndroid Build Coastguard Worker 	Index blockI = bi / N;
69*3ac0a46fSAndroid Build Coastguard Worker 	Blk block = getBlock(blockI), mask = Blk(1) << (bi % N);
70*3ac0a46fSAndroid Build Coastguard Worker 	block = newBit ? (block | mask) : (block & ~mask);
71*3ac0a46fSAndroid Build Coastguard Worker 	setBlock(blockI, block);
72*3ac0a46fSAndroid Build Coastguard Worker }
73*3ac0a46fSAndroid Build Coastguard Worker 
74*3ac0a46fSAndroid Build Coastguard Worker // COMPARISON
compareTo(const BigUnsigned & x) const75*3ac0a46fSAndroid Build Coastguard Worker BigUnsigned::CmpRes BigUnsigned::compareTo(const BigUnsigned &x) const {
76*3ac0a46fSAndroid Build Coastguard Worker 	// A bigger length implies a bigger number.
77*3ac0a46fSAndroid Build Coastguard Worker 	if (len < x.len)
78*3ac0a46fSAndroid Build Coastguard Worker 		return less;
79*3ac0a46fSAndroid Build Coastguard Worker 	else if (len > x.len)
80*3ac0a46fSAndroid Build Coastguard Worker 		return greater;
81*3ac0a46fSAndroid Build Coastguard Worker 	else {
82*3ac0a46fSAndroid Build Coastguard Worker 		// Compare blocks one by one from left to right.
83*3ac0a46fSAndroid Build Coastguard Worker 		Index i = len;
84*3ac0a46fSAndroid Build Coastguard Worker 		while (i > 0) {
85*3ac0a46fSAndroid Build Coastguard Worker 			i--;
86*3ac0a46fSAndroid Build Coastguard Worker 			if (blk[i] == x.blk[i])
87*3ac0a46fSAndroid Build Coastguard Worker 				continue;
88*3ac0a46fSAndroid Build Coastguard Worker 			else if (blk[i] > x.blk[i])
89*3ac0a46fSAndroid Build Coastguard Worker 				return greater;
90*3ac0a46fSAndroid Build Coastguard Worker 			else
91*3ac0a46fSAndroid Build Coastguard Worker 				return less;
92*3ac0a46fSAndroid Build Coastguard Worker 		}
93*3ac0a46fSAndroid Build Coastguard Worker 		// If no blocks differed, the numbers are equal.
94*3ac0a46fSAndroid Build Coastguard Worker 		return equal;
95*3ac0a46fSAndroid Build Coastguard Worker 	}
96*3ac0a46fSAndroid Build Coastguard Worker }
97*3ac0a46fSAndroid Build Coastguard Worker 
98*3ac0a46fSAndroid Build Coastguard Worker // COPY-LESS OPERATIONS
99*3ac0a46fSAndroid Build Coastguard Worker 
100*3ac0a46fSAndroid Build Coastguard Worker /*
101*3ac0a46fSAndroid Build Coastguard Worker  * On most calls to copy-less operations, it's safe to read the inputs little by
102*3ac0a46fSAndroid Build Coastguard Worker  * little and write the outputs little by little.  However, if one of the
103*3ac0a46fSAndroid Build Coastguard Worker  * inputs is coming from the same variable into which the output is to be
104*3ac0a46fSAndroid Build Coastguard Worker  * stored (an "aliased" call), we risk overwriting the input before we read it.
105*3ac0a46fSAndroid Build Coastguard Worker  * In this case, we first compute the result into a temporary BigUnsigned
106*3ac0a46fSAndroid Build Coastguard Worker  * variable and then copy it into the requested output variable *this.
107*3ac0a46fSAndroid Build Coastguard Worker  * Each put-here operation uses the DTRT_ALIASED macro (Do The Right Thing on
108*3ac0a46fSAndroid Build Coastguard Worker  * aliased calls) to generate code for this check.
109*3ac0a46fSAndroid Build Coastguard Worker  *
110*3ac0a46fSAndroid Build Coastguard Worker  * I adopted this approach on 2007.02.13 (see Assignment Operators in
111*3ac0a46fSAndroid Build Coastguard Worker  * BigUnsigned.hh).  Before then, put-here operations rejected aliased calls
112*3ac0a46fSAndroid Build Coastguard Worker  * with an exception.  I think doing the right thing is better.
113*3ac0a46fSAndroid Build Coastguard Worker  *
114*3ac0a46fSAndroid Build Coastguard Worker  * Some of the put-here operations can probably handle aliased calls safely
115*3ac0a46fSAndroid Build Coastguard Worker  * without the extra copy because (for example) they process blocks strictly
116*3ac0a46fSAndroid Build Coastguard Worker  * right-to-left.  At some point I might determine which ones don't need the
117*3ac0a46fSAndroid Build Coastguard Worker  * copy, but my reasoning would need to be verified very carefully.  For now
118*3ac0a46fSAndroid Build Coastguard Worker  * I'll leave in the copy.
119*3ac0a46fSAndroid Build Coastguard Worker  */
120*3ac0a46fSAndroid Build Coastguard Worker #define DTRT_ALIASED(cond, op) \
121*3ac0a46fSAndroid Build Coastguard Worker 	if (cond) { \
122*3ac0a46fSAndroid Build Coastguard Worker 		BigUnsigned tmpThis; \
123*3ac0a46fSAndroid Build Coastguard Worker 		tmpThis.op; \
124*3ac0a46fSAndroid Build Coastguard Worker 		*this = tmpThis; \
125*3ac0a46fSAndroid Build Coastguard Worker 		return; \
126*3ac0a46fSAndroid Build Coastguard Worker 	}
127*3ac0a46fSAndroid Build Coastguard Worker 
128*3ac0a46fSAndroid Build Coastguard Worker 
129*3ac0a46fSAndroid Build Coastguard Worker 
add(const BigUnsigned & a,const BigUnsigned & b)130*3ac0a46fSAndroid Build Coastguard Worker void BigUnsigned::add(const BigUnsigned &a, const BigUnsigned &b) {
131*3ac0a46fSAndroid Build Coastguard Worker 	DTRT_ALIASED(this == &a || this == &b, add(a, b));
132*3ac0a46fSAndroid Build Coastguard Worker 	// If one argument is zero, copy the other.
133*3ac0a46fSAndroid Build Coastguard Worker 	if (a.len == 0) {
134*3ac0a46fSAndroid Build Coastguard Worker 		operator =(b);
135*3ac0a46fSAndroid Build Coastguard Worker 		return;
136*3ac0a46fSAndroid Build Coastguard Worker 	} else if (b.len == 0) {
137*3ac0a46fSAndroid Build Coastguard Worker 		operator =(a);
138*3ac0a46fSAndroid Build Coastguard Worker 		return;
139*3ac0a46fSAndroid Build Coastguard Worker 	}
140*3ac0a46fSAndroid Build Coastguard Worker 	// Some variables...
141*3ac0a46fSAndroid Build Coastguard Worker 	// Carries in and out of an addition stage
142*3ac0a46fSAndroid Build Coastguard Worker 	bool carryIn, carryOut;
143*3ac0a46fSAndroid Build Coastguard Worker 	Blk temp;
144*3ac0a46fSAndroid Build Coastguard Worker 	Index i;
145*3ac0a46fSAndroid Build Coastguard Worker 	// a2 points to the longer input, b2 points to the shorter
146*3ac0a46fSAndroid Build Coastguard Worker 	const BigUnsigned *a2, *b2;
147*3ac0a46fSAndroid Build Coastguard Worker 	if (a.len >= b.len) {
148*3ac0a46fSAndroid Build Coastguard Worker 		a2 = &a;
149*3ac0a46fSAndroid Build Coastguard Worker 		b2 = &b;
150*3ac0a46fSAndroid Build Coastguard Worker 	} else {
151*3ac0a46fSAndroid Build Coastguard Worker 		a2 = &b;
152*3ac0a46fSAndroid Build Coastguard Worker 		b2 = &a;
153*3ac0a46fSAndroid Build Coastguard Worker 	}
154*3ac0a46fSAndroid Build Coastguard Worker 	// Set prelimiary length and make room in this BigUnsigned
155*3ac0a46fSAndroid Build Coastguard Worker 	len = a2->len + 1;
156*3ac0a46fSAndroid Build Coastguard Worker 	allocate(len);
157*3ac0a46fSAndroid Build Coastguard Worker 	// For each block index that is present in both inputs...
158*3ac0a46fSAndroid Build Coastguard Worker 	for (i = 0, carryIn = false; i < b2->len; i++) {
159*3ac0a46fSAndroid Build Coastguard Worker 		// Add input blocks
160*3ac0a46fSAndroid Build Coastguard Worker 		temp = a2->blk[i] + b2->blk[i];
161*3ac0a46fSAndroid Build Coastguard Worker 		// If a rollover occurred, the result is less than either input.
162*3ac0a46fSAndroid Build Coastguard Worker 		// This test is used many times in the BigUnsigned code.
163*3ac0a46fSAndroid Build Coastguard Worker 		carryOut = (temp < a2->blk[i]);
164*3ac0a46fSAndroid Build Coastguard Worker 		// If a carry was input, handle it
165*3ac0a46fSAndroid Build Coastguard Worker 		if (carryIn) {
166*3ac0a46fSAndroid Build Coastguard Worker 			temp++;
167*3ac0a46fSAndroid Build Coastguard Worker 			carryOut |= (temp == 0);
168*3ac0a46fSAndroid Build Coastguard Worker 		}
169*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = temp; // Save the addition result
170*3ac0a46fSAndroid Build Coastguard Worker 		carryIn = carryOut; // Pass the carry along
171*3ac0a46fSAndroid Build Coastguard Worker 	}
172*3ac0a46fSAndroid Build Coastguard Worker 	// If there is a carry left over, increase blocks until
173*3ac0a46fSAndroid Build Coastguard Worker 	// one does not roll over.
174*3ac0a46fSAndroid Build Coastguard Worker 	for (; i < a2->len && carryIn; i++) {
175*3ac0a46fSAndroid Build Coastguard Worker 		temp = a2->blk[i] + 1;
176*3ac0a46fSAndroid Build Coastguard Worker 		carryIn = (temp == 0);
177*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = temp;
178*3ac0a46fSAndroid Build Coastguard Worker 	}
179*3ac0a46fSAndroid Build Coastguard Worker 	// If the carry was resolved but the larger number
180*3ac0a46fSAndroid Build Coastguard Worker 	// still has blocks, copy them over.
181*3ac0a46fSAndroid Build Coastguard Worker 	for (; i < a2->len; i++)
182*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = a2->blk[i];
183*3ac0a46fSAndroid Build Coastguard Worker 	// Set the extra block if there's still a carry, decrease length otherwise
184*3ac0a46fSAndroid Build Coastguard Worker 	if (carryIn)
185*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = 1;
186*3ac0a46fSAndroid Build Coastguard Worker 	else
187*3ac0a46fSAndroid Build Coastguard Worker 		len--;
188*3ac0a46fSAndroid Build Coastguard Worker }
189*3ac0a46fSAndroid Build Coastguard Worker 
subtract(const BigUnsigned & a,const BigUnsigned & b)190*3ac0a46fSAndroid Build Coastguard Worker void BigUnsigned::subtract(const BigUnsigned &a, const BigUnsigned &b) {
191*3ac0a46fSAndroid Build Coastguard Worker 	DTRT_ALIASED(this == &a || this == &b, subtract(a, b));
192*3ac0a46fSAndroid Build Coastguard Worker 	if (b.len == 0) {
193*3ac0a46fSAndroid Build Coastguard Worker 		// If b is zero, copy a.
194*3ac0a46fSAndroid Build Coastguard Worker 		operator =(a);
195*3ac0a46fSAndroid Build Coastguard Worker 		return;
196*3ac0a46fSAndroid Build Coastguard Worker 	} else if (a.len < b.len)
197*3ac0a46fSAndroid Build Coastguard Worker 		// If a is shorter than b, the result is negative.
198*3ac0a46fSAndroid Build Coastguard Worker         abort();
199*3ac0a46fSAndroid Build Coastguard Worker 	// Some variables...
200*3ac0a46fSAndroid Build Coastguard Worker 	bool borrowIn, borrowOut;
201*3ac0a46fSAndroid Build Coastguard Worker 	Blk temp;
202*3ac0a46fSAndroid Build Coastguard Worker 	Index i;
203*3ac0a46fSAndroid Build Coastguard Worker 	// Set preliminary length and make room
204*3ac0a46fSAndroid Build Coastguard Worker 	len = a.len;
205*3ac0a46fSAndroid Build Coastguard Worker 	allocate(len);
206*3ac0a46fSAndroid Build Coastguard Worker 	// For each block index that is present in both inputs...
207*3ac0a46fSAndroid Build Coastguard Worker 	for (i = 0, borrowIn = false; i < b.len; i++) {
208*3ac0a46fSAndroid Build Coastguard Worker 		temp = a.blk[i] - b.blk[i];
209*3ac0a46fSAndroid Build Coastguard Worker 		// If a reverse rollover occurred,
210*3ac0a46fSAndroid Build Coastguard Worker 		// the result is greater than the block from a.
211*3ac0a46fSAndroid Build Coastguard Worker 		borrowOut = (temp > a.blk[i]);
212*3ac0a46fSAndroid Build Coastguard Worker 		// Handle an incoming borrow
213*3ac0a46fSAndroid Build Coastguard Worker 		if (borrowIn) {
214*3ac0a46fSAndroid Build Coastguard Worker 			borrowOut |= (temp == 0);
215*3ac0a46fSAndroid Build Coastguard Worker 			temp--;
216*3ac0a46fSAndroid Build Coastguard Worker 		}
217*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = temp; // Save the subtraction result
218*3ac0a46fSAndroid Build Coastguard Worker 		borrowIn = borrowOut; // Pass the borrow along
219*3ac0a46fSAndroid Build Coastguard Worker 	}
220*3ac0a46fSAndroid Build Coastguard Worker 	// If there is a borrow left over, decrease blocks until
221*3ac0a46fSAndroid Build Coastguard Worker 	// one does not reverse rollover.
222*3ac0a46fSAndroid Build Coastguard Worker 	for (; i < a.len && borrowIn; i++) {
223*3ac0a46fSAndroid Build Coastguard Worker 		borrowIn = (a.blk[i] == 0);
224*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = a.blk[i] - 1;
225*3ac0a46fSAndroid Build Coastguard Worker 	}
226*3ac0a46fSAndroid Build Coastguard Worker 	/* If there's still a borrow, the result is negative.
227*3ac0a46fSAndroid Build Coastguard Worker 	 * Throw an exception, but zero out this object so as to leave it in a
228*3ac0a46fSAndroid Build Coastguard Worker 	 * predictable state. */
229*3ac0a46fSAndroid Build Coastguard Worker 	if (borrowIn) {
230*3ac0a46fSAndroid Build Coastguard Worker 		len = 0;
231*3ac0a46fSAndroid Build Coastguard Worker         abort();
232*3ac0a46fSAndroid Build Coastguard Worker 	} else
233*3ac0a46fSAndroid Build Coastguard Worker 		// Copy over the rest of the blocks
234*3ac0a46fSAndroid Build Coastguard Worker 		for (; i < a.len; i++)
235*3ac0a46fSAndroid Build Coastguard Worker 			blk[i] = a.blk[i];
236*3ac0a46fSAndroid Build Coastguard Worker 	// Zap leading zeros
237*3ac0a46fSAndroid Build Coastguard Worker 	zapLeadingZeros();
238*3ac0a46fSAndroid Build Coastguard Worker }
239*3ac0a46fSAndroid Build Coastguard Worker 
240*3ac0a46fSAndroid Build Coastguard Worker /*
241*3ac0a46fSAndroid Build Coastguard Worker  * About the multiplication and division algorithms:
242*3ac0a46fSAndroid Build Coastguard Worker  *
243*3ac0a46fSAndroid Build Coastguard Worker  * I searched unsucessfully for fast C++ built-in operations like the `b_0'
244*3ac0a46fSAndroid Build Coastguard Worker  * and `c_0' Knuth describes in Section 4.3.1 of ``The Art of Computer
245*3ac0a46fSAndroid Build Coastguard Worker  * Programming'' (replace `place' by `Blk'):
246*3ac0a46fSAndroid Build Coastguard Worker  *
247*3ac0a46fSAndroid Build Coastguard Worker  *    ``b_0[:] multiplication of a one-place integer by another one-place
248*3ac0a46fSAndroid Build Coastguard Worker  *      integer, giving a two-place answer;
249*3ac0a46fSAndroid Build Coastguard Worker  *
250*3ac0a46fSAndroid Build Coastguard Worker  *    ``c_0[:] division of a two-place integer by a one-place integer,
251*3ac0a46fSAndroid Build Coastguard Worker  *      provided that the quotient is a one-place integer, and yielding
252*3ac0a46fSAndroid Build Coastguard Worker  *      also a one-place remainder.''
253*3ac0a46fSAndroid Build Coastguard Worker  *
254*3ac0a46fSAndroid Build Coastguard Worker  * I also missed his note that ``[b]y adjusting the word size, if
255*3ac0a46fSAndroid Build Coastguard Worker  * necessary, nearly all computers will have these three operations
256*3ac0a46fSAndroid Build Coastguard Worker  * available'', so I gave up on trying to use algorithms similar to his.
257*3ac0a46fSAndroid Build Coastguard Worker  * A future version of the library might include such algorithms; I
258*3ac0a46fSAndroid Build Coastguard Worker  * would welcome contributions from others for this.
259*3ac0a46fSAndroid Build Coastguard Worker  *
260*3ac0a46fSAndroid Build Coastguard Worker  * I eventually decided to use bit-shifting algorithms.  To multiply `a'
261*3ac0a46fSAndroid Build Coastguard Worker  * and `b', we zero out the result.  Then, for each `1' bit in `a', we
262*3ac0a46fSAndroid Build Coastguard Worker  * shift `b' left the appropriate amount and add it to the result.
263*3ac0a46fSAndroid Build Coastguard Worker  * Similarly, to divide `a' by `b', we shift `b' left varying amounts,
264*3ac0a46fSAndroid Build Coastguard Worker  * repeatedly trying to subtract it from `a'.  When we succeed, we note
265*3ac0a46fSAndroid Build Coastguard Worker  * the fact by setting a bit in the quotient.  While these algorithms
266*3ac0a46fSAndroid Build Coastguard Worker  * have the same O(n^2) time complexity as Knuth's, the ``constant factor''
267*3ac0a46fSAndroid Build Coastguard Worker  * is likely to be larger.
268*3ac0a46fSAndroid Build Coastguard Worker  *
269*3ac0a46fSAndroid Build Coastguard Worker  * Because I used these algorithms, which require single-block addition
270*3ac0a46fSAndroid Build Coastguard Worker  * and subtraction rather than single-block multiplication and division,
271*3ac0a46fSAndroid Build Coastguard Worker  * the innermost loops of all four routines are very similar.  Study one
272*3ac0a46fSAndroid Build Coastguard Worker  * of them and all will become clear.
273*3ac0a46fSAndroid Build Coastguard Worker  */
274*3ac0a46fSAndroid Build Coastguard Worker 
275*3ac0a46fSAndroid Build Coastguard Worker /*
276*3ac0a46fSAndroid Build Coastguard Worker  * This is a little inline function used by both the multiplication
277*3ac0a46fSAndroid Build Coastguard Worker  * routine and the division routine.
278*3ac0a46fSAndroid Build Coastguard Worker  *
279*3ac0a46fSAndroid Build Coastguard Worker  * `getShiftedBlock' returns the `x'th block of `num << y'.
280*3ac0a46fSAndroid Build Coastguard Worker  * `y' may be anything from 0 to N - 1, and `x' may be anything from
281*3ac0a46fSAndroid Build Coastguard Worker  * 0 to `num.len'.
282*3ac0a46fSAndroid Build Coastguard Worker  *
283*3ac0a46fSAndroid Build Coastguard Worker  * Two things contribute to this block:
284*3ac0a46fSAndroid Build Coastguard Worker  *
285*3ac0a46fSAndroid Build Coastguard Worker  * (1) The `N - y' low bits of `num.blk[x]', shifted `y' bits left.
286*3ac0a46fSAndroid Build Coastguard Worker  *
287*3ac0a46fSAndroid Build Coastguard Worker  * (2) The `y' high bits of `num.blk[x-1]', shifted `N - y' bits right.
288*3ac0a46fSAndroid Build Coastguard Worker  *
289*3ac0a46fSAndroid Build Coastguard Worker  * But we must be careful if `x == 0' or `x == num.len', in
290*3ac0a46fSAndroid Build Coastguard Worker  * which case we should use 0 instead of (2) or (1), respectively.
291*3ac0a46fSAndroid Build Coastguard Worker  *
292*3ac0a46fSAndroid Build Coastguard Worker  * If `y == 0', then (2) contributes 0, as it should.  However,
293*3ac0a46fSAndroid Build Coastguard Worker  * in some computer environments, for a reason I cannot understand,
294*3ac0a46fSAndroid Build Coastguard Worker  * `a >> b' means `a >> (b % N)'.  This means `num.blk[x-1] >> (N - y)'
295*3ac0a46fSAndroid Build Coastguard Worker  * will return `num.blk[x-1]' instead of the desired 0 when `y == 0';
296*3ac0a46fSAndroid Build Coastguard Worker  * the test `y == 0' handles this case specially.
297*3ac0a46fSAndroid Build Coastguard Worker  */
getShiftedBlock(const BigUnsigned & num,BigUnsigned::Index x,unsigned int y)298*3ac0a46fSAndroid Build Coastguard Worker inline BigUnsigned::Blk getShiftedBlock(const BigUnsigned &num,
299*3ac0a46fSAndroid Build Coastguard Worker 	BigUnsigned::Index x, unsigned int y) {
300*3ac0a46fSAndroid Build Coastguard Worker 	BigUnsigned::Blk part1 = (x == 0 || y == 0) ? 0 : (num.blk[x - 1] >> (BigUnsigned::N - y));
301*3ac0a46fSAndroid Build Coastguard Worker 	BigUnsigned::Blk part2 = (x == num.len) ? 0 : (num.blk[x] << y);
302*3ac0a46fSAndroid Build Coastguard Worker 	return part1 | part2;
303*3ac0a46fSAndroid Build Coastguard Worker }
304*3ac0a46fSAndroid Build Coastguard Worker 
multiply(const BigUnsigned & a,const BigUnsigned & b)305*3ac0a46fSAndroid Build Coastguard Worker void BigUnsigned::multiply(const BigUnsigned &a, const BigUnsigned &b) {
306*3ac0a46fSAndroid Build Coastguard Worker 	DTRT_ALIASED(this == &a || this == &b, multiply(a, b));
307*3ac0a46fSAndroid Build Coastguard Worker 	// If either a or b is zero, set to zero.
308*3ac0a46fSAndroid Build Coastguard Worker 	if (a.len == 0 || b.len == 0) {
309*3ac0a46fSAndroid Build Coastguard Worker 		len = 0;
310*3ac0a46fSAndroid Build Coastguard Worker 		return;
311*3ac0a46fSAndroid Build Coastguard Worker 	}
312*3ac0a46fSAndroid Build Coastguard Worker 	/*
313*3ac0a46fSAndroid Build Coastguard Worker 	 * Overall method:
314*3ac0a46fSAndroid Build Coastguard Worker 	 *
315*3ac0a46fSAndroid Build Coastguard Worker 	 * Set this = 0.
316*3ac0a46fSAndroid Build Coastguard Worker 	 * For each 1-bit of `a' (say the `i2'th bit of block `i'):
317*3ac0a46fSAndroid Build Coastguard Worker 	 *    Add `b << (i blocks and i2 bits)' to *this.
318*3ac0a46fSAndroid Build Coastguard Worker 	 */
319*3ac0a46fSAndroid Build Coastguard Worker 	// Variables for the calculation
320*3ac0a46fSAndroid Build Coastguard Worker 	Index i, j, k;
321*3ac0a46fSAndroid Build Coastguard Worker 	unsigned int i2;
322*3ac0a46fSAndroid Build Coastguard Worker 	Blk temp;
323*3ac0a46fSAndroid Build Coastguard Worker 	bool carryIn, carryOut;
324*3ac0a46fSAndroid Build Coastguard Worker 	// Set preliminary length and make room
325*3ac0a46fSAndroid Build Coastguard Worker 	len = a.len + b.len;
326*3ac0a46fSAndroid Build Coastguard Worker 	allocate(len);
327*3ac0a46fSAndroid Build Coastguard Worker 	// Zero out this object
328*3ac0a46fSAndroid Build Coastguard Worker 	for (i = 0; i < len; i++)
329*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = 0;
330*3ac0a46fSAndroid Build Coastguard Worker 	// For each block of the first number...
331*3ac0a46fSAndroid Build Coastguard Worker 	for (i = 0; i < a.len; i++) {
332*3ac0a46fSAndroid Build Coastguard Worker 		// For each 1-bit of that block...
333*3ac0a46fSAndroid Build Coastguard Worker 		for (i2 = 0; i2 < N; i2++) {
334*3ac0a46fSAndroid Build Coastguard Worker 			if ((a.blk[i] & (Blk(1) << i2)) == 0)
335*3ac0a46fSAndroid Build Coastguard Worker 				continue;
336*3ac0a46fSAndroid Build Coastguard Worker 			/*
337*3ac0a46fSAndroid Build Coastguard Worker 			 * Add b to this, shifted left i blocks and i2 bits.
338*3ac0a46fSAndroid Build Coastguard Worker 			 * j is the index in b, and k = i + j is the index in this.
339*3ac0a46fSAndroid Build Coastguard Worker 			 *
340*3ac0a46fSAndroid Build Coastguard Worker 			 * `getShiftedBlock', a short inline function defined above,
341*3ac0a46fSAndroid Build Coastguard Worker 			 * is now used for the bit handling.  It replaces the more
342*3ac0a46fSAndroid Build Coastguard Worker 			 * complex `bHigh' code, in which each run of the loop dealt
343*3ac0a46fSAndroid Build Coastguard Worker 			 * immediately with the low bits and saved the high bits to
344*3ac0a46fSAndroid Build Coastguard Worker 			 * be picked up next time.  The last run of the loop used to
345*3ac0a46fSAndroid Build Coastguard Worker 			 * leave leftover high bits, which were handled separately.
346*3ac0a46fSAndroid Build Coastguard Worker 			 * Instead, this loop runs an additional time with j == b.len.
347*3ac0a46fSAndroid Build Coastguard Worker 			 * These changes were made on 2005.01.11.
348*3ac0a46fSAndroid Build Coastguard Worker 			 */
349*3ac0a46fSAndroid Build Coastguard Worker 			for (j = 0, k = i, carryIn = false; j <= b.len; j++, k++) {
350*3ac0a46fSAndroid Build Coastguard Worker 				/*
351*3ac0a46fSAndroid Build Coastguard Worker 				 * The body of this loop is very similar to the body of the first loop
352*3ac0a46fSAndroid Build Coastguard Worker 				 * in `add', except that this loop does a `+=' instead of a `+'.
353*3ac0a46fSAndroid Build Coastguard Worker 				 */
354*3ac0a46fSAndroid Build Coastguard Worker 				temp = blk[k] + getShiftedBlock(b, j, i2);
355*3ac0a46fSAndroid Build Coastguard Worker 				carryOut = (temp < blk[k]);
356*3ac0a46fSAndroid Build Coastguard Worker 				if (carryIn) {
357*3ac0a46fSAndroid Build Coastguard Worker 					temp++;
358*3ac0a46fSAndroid Build Coastguard Worker 					carryOut |= (temp == 0);
359*3ac0a46fSAndroid Build Coastguard Worker 				}
360*3ac0a46fSAndroid Build Coastguard Worker 				blk[k] = temp;
361*3ac0a46fSAndroid Build Coastguard Worker 				carryIn = carryOut;
362*3ac0a46fSAndroid Build Coastguard Worker 			}
363*3ac0a46fSAndroid Build Coastguard Worker 			// No more extra iteration to deal with `bHigh'.
364*3ac0a46fSAndroid Build Coastguard Worker 			// Roll-over a carry as necessary.
365*3ac0a46fSAndroid Build Coastguard Worker 			for (; carryIn; k++) {
366*3ac0a46fSAndroid Build Coastguard Worker 				blk[k]++;
367*3ac0a46fSAndroid Build Coastguard Worker 				carryIn = (blk[k] == 0);
368*3ac0a46fSAndroid Build Coastguard Worker 			}
369*3ac0a46fSAndroid Build Coastguard Worker 		}
370*3ac0a46fSAndroid Build Coastguard Worker 	}
371*3ac0a46fSAndroid Build Coastguard Worker 	// Zap possible leading zero
372*3ac0a46fSAndroid Build Coastguard Worker 	if (blk[len - 1] == 0)
373*3ac0a46fSAndroid Build Coastguard Worker 		len--;
374*3ac0a46fSAndroid Build Coastguard Worker }
375*3ac0a46fSAndroid Build Coastguard Worker 
376*3ac0a46fSAndroid Build Coastguard Worker /*
377*3ac0a46fSAndroid Build Coastguard Worker  * DIVISION WITH REMAINDER
378*3ac0a46fSAndroid Build Coastguard Worker  * This monstrous function mods *this by the given divisor b while storing the
379*3ac0a46fSAndroid Build Coastguard Worker  * quotient in the given object q; at the end, *this contains the remainder.
380*3ac0a46fSAndroid Build Coastguard Worker  * The seemingly bizarre pattern of inputs and outputs was chosen so that the
381*3ac0a46fSAndroid Build Coastguard Worker  * function copies as little as possible (since it is implemented by repeated
382*3ac0a46fSAndroid Build Coastguard Worker  * subtraction of multiples of b from *this).
383*3ac0a46fSAndroid Build Coastguard Worker  *
384*3ac0a46fSAndroid Build Coastguard Worker  * "modWithQuotient" might be a better name for this function, but I would
385*3ac0a46fSAndroid Build Coastguard Worker  * rather not change the name now.
386*3ac0a46fSAndroid Build Coastguard Worker  */
divideWithRemainder(const BigUnsigned & b,BigUnsigned & q)387*3ac0a46fSAndroid Build Coastguard Worker void BigUnsigned::divideWithRemainder(const BigUnsigned &b, BigUnsigned &q) {
388*3ac0a46fSAndroid Build Coastguard Worker 	/* Defending against aliased calls is more complex than usual because we
389*3ac0a46fSAndroid Build Coastguard Worker 	 * are writing to both *this and q.
390*3ac0a46fSAndroid Build Coastguard Worker 	 *
391*3ac0a46fSAndroid Build Coastguard Worker 	 * It would be silly to try to write quotient and remainder to the
392*3ac0a46fSAndroid Build Coastguard Worker 	 * same variable.  Rule that out right away. */
393*3ac0a46fSAndroid Build Coastguard Worker 	if (this == &q)
394*3ac0a46fSAndroid Build Coastguard Worker         abort();
395*3ac0a46fSAndroid Build Coastguard Worker 	/* Now *this and q are separate, so the only concern is that b might be
396*3ac0a46fSAndroid Build Coastguard Worker 	 * aliased to one of them.  If so, use a temporary copy of b. */
397*3ac0a46fSAndroid Build Coastguard Worker 	if (this == &b || &q == &b) {
398*3ac0a46fSAndroid Build Coastguard Worker 		BigUnsigned tmpB(b);
399*3ac0a46fSAndroid Build Coastguard Worker 		divideWithRemainder(tmpB, q);
400*3ac0a46fSAndroid Build Coastguard Worker 		return;
401*3ac0a46fSAndroid Build Coastguard Worker 	}
402*3ac0a46fSAndroid Build Coastguard Worker 
403*3ac0a46fSAndroid Build Coastguard Worker 	/*
404*3ac0a46fSAndroid Build Coastguard Worker 	 * Knuth's definition of mod (which this function uses) is somewhat
405*3ac0a46fSAndroid Build Coastguard Worker 	 * different from the C++ definition of % in case of division by 0.
406*3ac0a46fSAndroid Build Coastguard Worker 	 *
407*3ac0a46fSAndroid Build Coastguard Worker 	 * We let a / 0 == 0 (it doesn't matter much) and a % 0 == a, no
408*3ac0a46fSAndroid Build Coastguard Worker 	 * exceptions thrown.  This allows us to preserve both Knuth's demand
409*3ac0a46fSAndroid Build Coastguard Worker 	 * that a mod 0 == a and the useful property that
410*3ac0a46fSAndroid Build Coastguard Worker 	 * (a / b) * b + (a % b) == a.
411*3ac0a46fSAndroid Build Coastguard Worker 	 */
412*3ac0a46fSAndroid Build Coastguard Worker 	if (b.len == 0) {
413*3ac0a46fSAndroid Build Coastguard Worker 		q.len = 0;
414*3ac0a46fSAndroid Build Coastguard Worker 		return;
415*3ac0a46fSAndroid Build Coastguard Worker 	}
416*3ac0a46fSAndroid Build Coastguard Worker 
417*3ac0a46fSAndroid Build Coastguard Worker 	/*
418*3ac0a46fSAndroid Build Coastguard Worker 	 * If *this.len < b.len, then *this < b, and we can be sure that b doesn't go into
419*3ac0a46fSAndroid Build Coastguard Worker 	 * *this at all.  The quotient is 0 and *this is already the remainder (so leave it alone).
420*3ac0a46fSAndroid Build Coastguard Worker 	 */
421*3ac0a46fSAndroid Build Coastguard Worker 	if (len < b.len) {
422*3ac0a46fSAndroid Build Coastguard Worker 		q.len = 0;
423*3ac0a46fSAndroid Build Coastguard Worker 		return;
424*3ac0a46fSAndroid Build Coastguard Worker 	}
425*3ac0a46fSAndroid Build Coastguard Worker 
426*3ac0a46fSAndroid Build Coastguard Worker 	// At this point we know (*this).len >= b.len > 0.  (Whew!)
427*3ac0a46fSAndroid Build Coastguard Worker 
428*3ac0a46fSAndroid Build Coastguard Worker 	/*
429*3ac0a46fSAndroid Build Coastguard Worker 	 * Overall method:
430*3ac0a46fSAndroid Build Coastguard Worker 	 *
431*3ac0a46fSAndroid Build Coastguard Worker 	 * For each appropriate i and i2, decreasing:
432*3ac0a46fSAndroid Build Coastguard Worker 	 *    Subtract (b << (i blocks and i2 bits)) from *this, storing the
433*3ac0a46fSAndroid Build Coastguard Worker 	 *      result in subtractBuf.
434*3ac0a46fSAndroid Build Coastguard Worker 	 *    If the subtraction succeeds with a nonnegative result:
435*3ac0a46fSAndroid Build Coastguard Worker 	 *        Turn on bit i2 of block i of the quotient q.
436*3ac0a46fSAndroid Build Coastguard Worker 	 *        Copy subtractBuf back into *this.
437*3ac0a46fSAndroid Build Coastguard Worker 	 *    Otherwise bit i2 of block i remains off, and *this is unchanged.
438*3ac0a46fSAndroid Build Coastguard Worker 	 *
439*3ac0a46fSAndroid Build Coastguard Worker 	 * Eventually q will contain the entire quotient, and *this will
440*3ac0a46fSAndroid Build Coastguard Worker 	 * be left with the remainder.
441*3ac0a46fSAndroid Build Coastguard Worker 	 *
442*3ac0a46fSAndroid Build Coastguard Worker 	 * subtractBuf[x] corresponds to blk[x], not blk[x+i], since 2005.01.11.
443*3ac0a46fSAndroid Build Coastguard Worker 	 * But on a single iteration, we don't touch the i lowest blocks of blk
444*3ac0a46fSAndroid Build Coastguard Worker 	 * (and don't use those of subtractBuf) because these blocks are
445*3ac0a46fSAndroid Build Coastguard Worker 	 * unaffected by the subtraction: we are subtracting
446*3ac0a46fSAndroid Build Coastguard Worker 	 * (b << (i blocks and i2 bits)), which ends in at least `i' zero
447*3ac0a46fSAndroid Build Coastguard Worker 	 * blocks. */
448*3ac0a46fSAndroid Build Coastguard Worker 	// Variables for the calculation
449*3ac0a46fSAndroid Build Coastguard Worker 	Index i, j, k;
450*3ac0a46fSAndroid Build Coastguard Worker 	unsigned int i2;
451*3ac0a46fSAndroid Build Coastguard Worker 	Blk temp;
452*3ac0a46fSAndroid Build Coastguard Worker 	bool borrowIn, borrowOut;
453*3ac0a46fSAndroid Build Coastguard Worker 
454*3ac0a46fSAndroid Build Coastguard Worker 	/*
455*3ac0a46fSAndroid Build Coastguard Worker 	 * Make sure we have an extra zero block just past the value.
456*3ac0a46fSAndroid Build Coastguard Worker 	 *
457*3ac0a46fSAndroid Build Coastguard Worker 	 * When we attempt a subtraction, we might shift `b' so
458*3ac0a46fSAndroid Build Coastguard Worker 	 * its first block begins a few bits left of the dividend,
459*3ac0a46fSAndroid Build Coastguard Worker 	 * and then we'll try to compare these extra bits with
460*3ac0a46fSAndroid Build Coastguard Worker 	 * a nonexistent block to the left of the dividend.  The
461*3ac0a46fSAndroid Build Coastguard Worker 	 * extra zero block ensures sensible behavior; we need
462*3ac0a46fSAndroid Build Coastguard Worker 	 * an extra block in `subtractBuf' for exactly the same reason.
463*3ac0a46fSAndroid Build Coastguard Worker 	 */
464*3ac0a46fSAndroid Build Coastguard Worker 	Index origLen = len; // Save real length.
465*3ac0a46fSAndroid Build Coastguard Worker 	/* To avoid an out-of-bounds access in case of reallocation, allocate
466*3ac0a46fSAndroid Build Coastguard Worker 	 * first and then increment the logical length. */
467*3ac0a46fSAndroid Build Coastguard Worker 	allocateAndCopy(len + 1);
468*3ac0a46fSAndroid Build Coastguard Worker 	len++;
469*3ac0a46fSAndroid Build Coastguard Worker 	blk[origLen] = 0; // Zero the added block.
470*3ac0a46fSAndroid Build Coastguard Worker 
471*3ac0a46fSAndroid Build Coastguard Worker 	// subtractBuf holds part of the result of a subtraction; see above.
472*3ac0a46fSAndroid Build Coastguard Worker 	Blk *subtractBuf = new Blk[len];
473*3ac0a46fSAndroid Build Coastguard Worker 
474*3ac0a46fSAndroid Build Coastguard Worker 	// Set preliminary length for quotient and make room
475*3ac0a46fSAndroid Build Coastguard Worker 	q.len = origLen - b.len + 1;
476*3ac0a46fSAndroid Build Coastguard Worker 	q.allocate(q.len);
477*3ac0a46fSAndroid Build Coastguard Worker 	// Zero out the quotient
478*3ac0a46fSAndroid Build Coastguard Worker 	for (i = 0; i < q.len; i++)
479*3ac0a46fSAndroid Build Coastguard Worker 		q.blk[i] = 0;
480*3ac0a46fSAndroid Build Coastguard Worker 
481*3ac0a46fSAndroid Build Coastguard Worker 	// For each possible left-shift of b in blocks...
482*3ac0a46fSAndroid Build Coastguard Worker 	i = q.len;
483*3ac0a46fSAndroid Build Coastguard Worker 	while (i > 0) {
484*3ac0a46fSAndroid Build Coastguard Worker 		i--;
485*3ac0a46fSAndroid Build Coastguard Worker 		// For each possible left-shift of b in bits...
486*3ac0a46fSAndroid Build Coastguard Worker 		// (Remember, N is the number of bits in a Blk.)
487*3ac0a46fSAndroid Build Coastguard Worker 		q.blk[i] = 0;
488*3ac0a46fSAndroid Build Coastguard Worker 		i2 = N;
489*3ac0a46fSAndroid Build Coastguard Worker 		while (i2 > 0) {
490*3ac0a46fSAndroid Build Coastguard Worker 			i2--;
491*3ac0a46fSAndroid Build Coastguard Worker 			/*
492*3ac0a46fSAndroid Build Coastguard Worker 			 * Subtract b, shifted left i blocks and i2 bits, from *this,
493*3ac0a46fSAndroid Build Coastguard Worker 			 * and store the answer in subtractBuf.  In the for loop, `k == i + j'.
494*3ac0a46fSAndroid Build Coastguard Worker 			 *
495*3ac0a46fSAndroid Build Coastguard Worker 			 * Compare this to the middle section of `multiply'.  They
496*3ac0a46fSAndroid Build Coastguard Worker 			 * are in many ways analogous.  See especially the discussion
497*3ac0a46fSAndroid Build Coastguard Worker 			 * of `getShiftedBlock'.
498*3ac0a46fSAndroid Build Coastguard Worker 			 */
499*3ac0a46fSAndroid Build Coastguard Worker 			for (j = 0, k = i, borrowIn = false; j <= b.len; j++, k++) {
500*3ac0a46fSAndroid Build Coastguard Worker 				temp = blk[k] - getShiftedBlock(b, j, i2);
501*3ac0a46fSAndroid Build Coastguard Worker 				borrowOut = (temp > blk[k]);
502*3ac0a46fSAndroid Build Coastguard Worker 				if (borrowIn) {
503*3ac0a46fSAndroid Build Coastguard Worker 					borrowOut |= (temp == 0);
504*3ac0a46fSAndroid Build Coastguard Worker 					temp--;
505*3ac0a46fSAndroid Build Coastguard Worker 				}
506*3ac0a46fSAndroid Build Coastguard Worker 				// Since 2005.01.11, indices of `subtractBuf' directly match those of `blk', so use `k'.
507*3ac0a46fSAndroid Build Coastguard Worker 				subtractBuf[k] = temp;
508*3ac0a46fSAndroid Build Coastguard Worker 				borrowIn = borrowOut;
509*3ac0a46fSAndroid Build Coastguard Worker 			}
510*3ac0a46fSAndroid Build Coastguard Worker 			// No more extra iteration to deal with `bHigh'.
511*3ac0a46fSAndroid Build Coastguard Worker 			// Roll-over a borrow as necessary.
512*3ac0a46fSAndroid Build Coastguard Worker 			for (; k < origLen && borrowIn; k++) {
513*3ac0a46fSAndroid Build Coastguard Worker 				borrowIn = (blk[k] == 0);
514*3ac0a46fSAndroid Build Coastguard Worker 				subtractBuf[k] = blk[k] - 1;
515*3ac0a46fSAndroid Build Coastguard Worker 			}
516*3ac0a46fSAndroid Build Coastguard Worker 			/*
517*3ac0a46fSAndroid Build Coastguard Worker 			 * If the subtraction was performed successfully (!borrowIn),
518*3ac0a46fSAndroid Build Coastguard Worker 			 * set bit i2 in block i of the quotient.
519*3ac0a46fSAndroid Build Coastguard Worker 			 *
520*3ac0a46fSAndroid Build Coastguard Worker 			 * Then, copy the portion of subtractBuf filled by the subtraction
521*3ac0a46fSAndroid Build Coastguard Worker 			 * back to *this.  This portion starts with block i and ends--
522*3ac0a46fSAndroid Build Coastguard Worker 			 * where?  Not necessarily at block `i + b.len'!  Well, we
523*3ac0a46fSAndroid Build Coastguard Worker 			 * increased k every time we saved a block into subtractBuf, so
524*3ac0a46fSAndroid Build Coastguard Worker 			 * the region of subtractBuf we copy is just [i, k).
525*3ac0a46fSAndroid Build Coastguard Worker 			 */
526*3ac0a46fSAndroid Build Coastguard Worker 			if (!borrowIn) {
527*3ac0a46fSAndroid Build Coastguard Worker 				q.blk[i] |= (Blk(1) << i2);
528*3ac0a46fSAndroid Build Coastguard Worker 				while (k > i) {
529*3ac0a46fSAndroid Build Coastguard Worker 					k--;
530*3ac0a46fSAndroid Build Coastguard Worker 					blk[k] = subtractBuf[k];
531*3ac0a46fSAndroid Build Coastguard Worker 				}
532*3ac0a46fSAndroid Build Coastguard Worker 			}
533*3ac0a46fSAndroid Build Coastguard Worker 		}
534*3ac0a46fSAndroid Build Coastguard Worker 	}
535*3ac0a46fSAndroid Build Coastguard Worker 	// Zap possible leading zero in quotient
536*3ac0a46fSAndroid Build Coastguard Worker 	if (q.blk[q.len - 1] == 0)
537*3ac0a46fSAndroid Build Coastguard Worker 		q.len--;
538*3ac0a46fSAndroid Build Coastguard Worker 	// Zap any/all leading zeros in remainder
539*3ac0a46fSAndroid Build Coastguard Worker 	zapLeadingZeros();
540*3ac0a46fSAndroid Build Coastguard Worker 	// Deallocate subtractBuf.
541*3ac0a46fSAndroid Build Coastguard Worker 	// (Thanks to Brad Spencer for noticing my accidental omission of this!)
542*3ac0a46fSAndroid Build Coastguard Worker 	delete [] subtractBuf;
543*3ac0a46fSAndroid Build Coastguard Worker }
544*3ac0a46fSAndroid Build Coastguard Worker 
545*3ac0a46fSAndroid Build Coastguard Worker /* BITWISE OPERATORS
546*3ac0a46fSAndroid Build Coastguard Worker  * These are straightforward blockwise operations except that they differ in
547*3ac0a46fSAndroid Build Coastguard Worker  * the output length and the necessity of zapLeadingZeros. */
548*3ac0a46fSAndroid Build Coastguard Worker 
bitAnd(const BigUnsigned & a,const BigUnsigned & b)549*3ac0a46fSAndroid Build Coastguard Worker void BigUnsigned::bitAnd(const BigUnsigned &a, const BigUnsigned &b) {
550*3ac0a46fSAndroid Build Coastguard Worker 	DTRT_ALIASED(this == &a || this == &b, bitAnd(a, b));
551*3ac0a46fSAndroid Build Coastguard Worker 	// The bitwise & can't be longer than either operand.
552*3ac0a46fSAndroid Build Coastguard Worker 	len = (a.len >= b.len) ? b.len : a.len;
553*3ac0a46fSAndroid Build Coastguard Worker 	allocate(len);
554*3ac0a46fSAndroid Build Coastguard Worker 	Index i;
555*3ac0a46fSAndroid Build Coastguard Worker 	for (i = 0; i < len; i++)
556*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = a.blk[i] & b.blk[i];
557*3ac0a46fSAndroid Build Coastguard Worker 	zapLeadingZeros();
558*3ac0a46fSAndroid Build Coastguard Worker }
559*3ac0a46fSAndroid Build Coastguard Worker 
bitOr(const BigUnsigned & a,const BigUnsigned & b)560*3ac0a46fSAndroid Build Coastguard Worker void BigUnsigned::bitOr(const BigUnsigned &a, const BigUnsigned &b) {
561*3ac0a46fSAndroid Build Coastguard Worker 	DTRT_ALIASED(this == &a || this == &b, bitOr(a, b));
562*3ac0a46fSAndroid Build Coastguard Worker 	Index i;
563*3ac0a46fSAndroid Build Coastguard Worker 	const BigUnsigned *a2, *b2;
564*3ac0a46fSAndroid Build Coastguard Worker 	if (a.len >= b.len) {
565*3ac0a46fSAndroid Build Coastguard Worker 		a2 = &a;
566*3ac0a46fSAndroid Build Coastguard Worker 		b2 = &b;
567*3ac0a46fSAndroid Build Coastguard Worker 	} else {
568*3ac0a46fSAndroid Build Coastguard Worker 		a2 = &b;
569*3ac0a46fSAndroid Build Coastguard Worker 		b2 = &a;
570*3ac0a46fSAndroid Build Coastguard Worker 	}
571*3ac0a46fSAndroid Build Coastguard Worker 	allocate(a2->len);
572*3ac0a46fSAndroid Build Coastguard Worker 	for (i = 0; i < b2->len; i++)
573*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = a2->blk[i] | b2->blk[i];
574*3ac0a46fSAndroid Build Coastguard Worker 	for (; i < a2->len; i++)
575*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = a2->blk[i];
576*3ac0a46fSAndroid Build Coastguard Worker 	len = a2->len;
577*3ac0a46fSAndroid Build Coastguard Worker 	// Doesn't need zapLeadingZeros.
578*3ac0a46fSAndroid Build Coastguard Worker }
579*3ac0a46fSAndroid Build Coastguard Worker 
bitXor(const BigUnsigned & a,const BigUnsigned & b)580*3ac0a46fSAndroid Build Coastguard Worker void BigUnsigned::bitXor(const BigUnsigned &a, const BigUnsigned &b) {
581*3ac0a46fSAndroid Build Coastguard Worker 	DTRT_ALIASED(this == &a || this == &b, bitXor(a, b));
582*3ac0a46fSAndroid Build Coastguard Worker 	Index i;
583*3ac0a46fSAndroid Build Coastguard Worker 	const BigUnsigned *a2, *b2;
584*3ac0a46fSAndroid Build Coastguard Worker 	if (a.len >= b.len) {
585*3ac0a46fSAndroid Build Coastguard Worker 		a2 = &a;
586*3ac0a46fSAndroid Build Coastguard Worker 		b2 = &b;
587*3ac0a46fSAndroid Build Coastguard Worker 	} else {
588*3ac0a46fSAndroid Build Coastguard Worker 		a2 = &b;
589*3ac0a46fSAndroid Build Coastguard Worker 		b2 = &a;
590*3ac0a46fSAndroid Build Coastguard Worker 	}
591*3ac0a46fSAndroid Build Coastguard Worker 	allocate(a2->len);
592*3ac0a46fSAndroid Build Coastguard Worker 	for (i = 0; i < b2->len; i++)
593*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = a2->blk[i] ^ b2->blk[i];
594*3ac0a46fSAndroid Build Coastguard Worker 	for (; i < a2->len; i++)
595*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = a2->blk[i];
596*3ac0a46fSAndroid Build Coastguard Worker 	len = a2->len;
597*3ac0a46fSAndroid Build Coastguard Worker 	zapLeadingZeros();
598*3ac0a46fSAndroid Build Coastguard Worker }
599*3ac0a46fSAndroid Build Coastguard Worker 
bitShiftLeft(const BigUnsigned & a,int b)600*3ac0a46fSAndroid Build Coastguard Worker void BigUnsigned::bitShiftLeft(const BigUnsigned &a, int b) {
601*3ac0a46fSAndroid Build Coastguard Worker 	DTRT_ALIASED(this == &a, bitShiftLeft(a, b));
602*3ac0a46fSAndroid Build Coastguard Worker 	if (b < 0) {
603*3ac0a46fSAndroid Build Coastguard Worker 		if (b << 1 == 0)
604*3ac0a46fSAndroid Build Coastguard Worker             abort();
605*3ac0a46fSAndroid Build Coastguard Worker 		else {
606*3ac0a46fSAndroid Build Coastguard Worker 			bitShiftRight(a, -b);
607*3ac0a46fSAndroid Build Coastguard Worker 			return;
608*3ac0a46fSAndroid Build Coastguard Worker 		}
609*3ac0a46fSAndroid Build Coastguard Worker 	}
610*3ac0a46fSAndroid Build Coastguard Worker 	Index shiftBlocks = b / N;
611*3ac0a46fSAndroid Build Coastguard Worker 	unsigned int shiftBits = b % N;
612*3ac0a46fSAndroid Build Coastguard Worker 	// + 1: room for high bits nudged left into another block
613*3ac0a46fSAndroid Build Coastguard Worker 	len = a.len + shiftBlocks + 1;
614*3ac0a46fSAndroid Build Coastguard Worker 	allocate(len);
615*3ac0a46fSAndroid Build Coastguard Worker 	Index i, j;
616*3ac0a46fSAndroid Build Coastguard Worker 	for (i = 0; i < shiftBlocks; i++)
617*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = 0;
618*3ac0a46fSAndroid Build Coastguard Worker 	for (j = 0, i = shiftBlocks; j <= a.len; j++, i++)
619*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = getShiftedBlock(a, j, shiftBits);
620*3ac0a46fSAndroid Build Coastguard Worker 	// Zap possible leading zero
621*3ac0a46fSAndroid Build Coastguard Worker 	if (blk[len - 1] == 0)
622*3ac0a46fSAndroid Build Coastguard Worker 		len--;
623*3ac0a46fSAndroid Build Coastguard Worker }
624*3ac0a46fSAndroid Build Coastguard Worker 
bitShiftRight(const BigUnsigned & a,int b)625*3ac0a46fSAndroid Build Coastguard Worker void BigUnsigned::bitShiftRight(const BigUnsigned &a, int b) {
626*3ac0a46fSAndroid Build Coastguard Worker 	DTRT_ALIASED(this == &a, bitShiftRight(a, b));
627*3ac0a46fSAndroid Build Coastguard Worker 	if (b < 0) {
628*3ac0a46fSAndroid Build Coastguard Worker 		if (b << 1 == 0)
629*3ac0a46fSAndroid Build Coastguard Worker             abort();
630*3ac0a46fSAndroid Build Coastguard Worker 		else {
631*3ac0a46fSAndroid Build Coastguard Worker 			bitShiftLeft(a, -b);
632*3ac0a46fSAndroid Build Coastguard Worker 			return;
633*3ac0a46fSAndroid Build Coastguard Worker 		}
634*3ac0a46fSAndroid Build Coastguard Worker 	}
635*3ac0a46fSAndroid Build Coastguard Worker 	// This calculation is wacky, but expressing the shift as a left bit shift
636*3ac0a46fSAndroid Build Coastguard Worker 	// within each block lets us use getShiftedBlock.
637*3ac0a46fSAndroid Build Coastguard Worker 	Index rightShiftBlocks = (b + N - 1) / N;
638*3ac0a46fSAndroid Build Coastguard Worker 	unsigned int leftShiftBits = N * rightShiftBlocks - b;
639*3ac0a46fSAndroid Build Coastguard Worker 	// Now (N * rightShiftBlocks - leftShiftBits) == b
640*3ac0a46fSAndroid Build Coastguard Worker 	// and 0 <= leftShiftBits < N.
641*3ac0a46fSAndroid Build Coastguard Worker 	if (rightShiftBlocks >= a.len + 1) {
642*3ac0a46fSAndroid Build Coastguard Worker 		// All of a is guaranteed to be shifted off, even considering the left
643*3ac0a46fSAndroid Build Coastguard Worker 		// bit shift.
644*3ac0a46fSAndroid Build Coastguard Worker 		len = 0;
645*3ac0a46fSAndroid Build Coastguard Worker 		return;
646*3ac0a46fSAndroid Build Coastguard Worker 	}
647*3ac0a46fSAndroid Build Coastguard Worker 	// Now we're allocating a positive amount.
648*3ac0a46fSAndroid Build Coastguard Worker 	// + 1: room for high bits nudged left into another block
649*3ac0a46fSAndroid Build Coastguard Worker 	len = a.len + 1 - rightShiftBlocks;
650*3ac0a46fSAndroid Build Coastguard Worker 	allocate(len);
651*3ac0a46fSAndroid Build Coastguard Worker 	Index i, j;
652*3ac0a46fSAndroid Build Coastguard Worker 	for (j = rightShiftBlocks, i = 0; j <= a.len; j++, i++)
653*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = getShiftedBlock(a, j, leftShiftBits);
654*3ac0a46fSAndroid Build Coastguard Worker 	// Zap possible leading zero
655*3ac0a46fSAndroid Build Coastguard Worker 	if (blk[len - 1] == 0)
656*3ac0a46fSAndroid Build Coastguard Worker 		len--;
657*3ac0a46fSAndroid Build Coastguard Worker }
658*3ac0a46fSAndroid Build Coastguard Worker 
659*3ac0a46fSAndroid Build Coastguard Worker // INCREMENT/DECREMENT OPERATORS
660*3ac0a46fSAndroid Build Coastguard Worker 
661*3ac0a46fSAndroid Build Coastguard Worker // Prefix increment
operator ++()662*3ac0a46fSAndroid Build Coastguard Worker BigUnsigned& BigUnsigned::operator ++() {
663*3ac0a46fSAndroid Build Coastguard Worker 	Index i;
664*3ac0a46fSAndroid Build Coastguard Worker 	bool carry = true;
665*3ac0a46fSAndroid Build Coastguard Worker 	for (i = 0; i < len && carry; i++) {
666*3ac0a46fSAndroid Build Coastguard Worker 		blk[i]++;
667*3ac0a46fSAndroid Build Coastguard Worker 		carry = (blk[i] == 0);
668*3ac0a46fSAndroid Build Coastguard Worker 	}
669*3ac0a46fSAndroid Build Coastguard Worker 	if (carry) {
670*3ac0a46fSAndroid Build Coastguard Worker 		// Allocate and then increase length, as in divideWithRemainder
671*3ac0a46fSAndroid Build Coastguard Worker 		allocateAndCopy(len + 1);
672*3ac0a46fSAndroid Build Coastguard Worker 		len++;
673*3ac0a46fSAndroid Build Coastguard Worker 		blk[i] = 1;
674*3ac0a46fSAndroid Build Coastguard Worker 	}
675*3ac0a46fSAndroid Build Coastguard Worker 	return *this;
676*3ac0a46fSAndroid Build Coastguard Worker }
677*3ac0a46fSAndroid Build Coastguard Worker 
678*3ac0a46fSAndroid Build Coastguard Worker // Postfix increment
operator ++(int)679*3ac0a46fSAndroid Build Coastguard Worker BigUnsigned BigUnsigned::operator ++(int) {
680*3ac0a46fSAndroid Build Coastguard Worker 	BigUnsigned temp(*this);
681*3ac0a46fSAndroid Build Coastguard Worker 	operator ++();
682*3ac0a46fSAndroid Build Coastguard Worker 	return temp;
683*3ac0a46fSAndroid Build Coastguard Worker }
684*3ac0a46fSAndroid Build Coastguard Worker 
685*3ac0a46fSAndroid Build Coastguard Worker // Prefix decrement
operator --()686*3ac0a46fSAndroid Build Coastguard Worker BigUnsigned& BigUnsigned::operator --() {
687*3ac0a46fSAndroid Build Coastguard Worker 	if (len == 0)
688*3ac0a46fSAndroid Build Coastguard Worker         abort();
689*3ac0a46fSAndroid Build Coastguard Worker 	Index i;
690*3ac0a46fSAndroid Build Coastguard Worker 	bool borrow = true;
691*3ac0a46fSAndroid Build Coastguard Worker 	for (i = 0; borrow; i++) {
692*3ac0a46fSAndroid Build Coastguard Worker 		borrow = (blk[i] == 0);
693*3ac0a46fSAndroid Build Coastguard Worker 		blk[i]--;
694*3ac0a46fSAndroid Build Coastguard Worker 	}
695*3ac0a46fSAndroid Build Coastguard Worker 	// Zap possible leading zero (there can only be one)
696*3ac0a46fSAndroid Build Coastguard Worker 	if (blk[len - 1] == 0)
697*3ac0a46fSAndroid Build Coastguard Worker 		len--;
698*3ac0a46fSAndroid Build Coastguard Worker 	return *this;
699*3ac0a46fSAndroid Build Coastguard Worker }
700*3ac0a46fSAndroid Build Coastguard Worker 
701*3ac0a46fSAndroid Build Coastguard Worker // Postfix decrement
operator --(int)702*3ac0a46fSAndroid Build Coastguard Worker BigUnsigned BigUnsigned::operator --(int) {
703*3ac0a46fSAndroid Build Coastguard Worker 	BigUnsigned temp(*this);
704*3ac0a46fSAndroid Build Coastguard Worker 	operator --();
705*3ac0a46fSAndroid Build Coastguard Worker 	return temp;
706*3ac0a46fSAndroid Build Coastguard Worker }
707