On Tuesday 03 July 2007 02:21:11 Pascal S. de Kloe wrote: > On Tuesday 03 July 2007 02:16:11 Pascal S. de Kloe wrote: > > The patch introduces arraycopy and a little cleanup in > > BigInteger#add(int[], int[]). > > > > I started reading the source today with BigInteger and this was the > > first thing I noticed. Would you be interested in more of these > > very small patches? > > > > Cheers, > > > > Pascal > > Sorry, wrong attachment. It's getting late. ;)
Once more.
--- BigInteger.java-old 2007-07-02 23:59:52.000000000 +0200
+++ BigInteger.java 2007-07-03 01:51:58.000000000 +0200
@@ -1095,30 +1095,32 @@
long sum = 0;
// Add common parts of both numbers
- while(yIndex > 0) {
- sum = (x[--xIndex] & LONG_MASK) +
- (y[--yIndex] & LONG_MASK) + (sum >>> 32);
- result[xIndex] = (int)sum;
+ while (yIndex != 0) {
+ long xl = x[--xIndex] & LONG_MASK;
+ long yl = y[--yIndex] & LONG_MASK;
+ sum = xl + yl + (sum >>> 32);
+ result[xIndex] = (int) sum;
}
// Copy remainder of longer number while carry propagation is required
- boolean carry = (sum >>> 32 != 0);
- while (xIndex > 0 && carry)
- carry = ((result[--xIndex] = x[xIndex] + 1) == 0);
+ boolean carry = (sum >>> 32) != 0;
+ while (carry) {
+ if (xIndex == 0) {
+ int[] larger = new int[result.length + 1];
+ larger[0] = 1;
+ System.arraycopy(result, 0, larger, 1, result.length);
+ return larger;
+ }
+ --xIndex;
+ int flow = x[xIndex] + 1;
+ result[xIndex] = flow;
+ carry = flow == 0;
+ }
// Copy remainder of longer number
- while (xIndex > 0)
- result[--xIndex] = x[xIndex];
+ if (xIndex != 0)
+ System.arraycopy(x, 0, result, 0, xIndex);
- // Grow result if necessary
- if (carry) {
- int newLen = result.length + 1;
- int temp[] = new int[newLen];
- for (int i = 1; i<newLen; i++)
- temp[i] = result[i-1];
- temp[0] = 0x01;
- result = temp;
- }
return result;
}
signature.asc
Description: This is a digitally signed message part.
