1 /* |
1 /* |
2 * Copyright (c) 1996, 2007, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
351 } |
351 } |
352 // Required for cases where the array was overallocated. |
352 // Required for cases where the array was overallocated. |
353 mag = trustedStripLeadingZeroInts(magnitude); |
353 mag = trustedStripLeadingZeroInts(magnitude); |
354 } |
354 } |
355 |
355 |
356 // Constructs a new BigInteger using a char array with radix=10 |
356 /* |
357 BigInteger(char[] val) { |
357 * Constructs a new BigInteger using a char array with radix=10. |
|
358 * Sign is precalculated outside and not allowed in the val. |
|
359 */ |
|
360 BigInteger(char[] val, int sign, int len) { |
358 int cursor = 0, numDigits; |
361 int cursor = 0, numDigits; |
359 int len = val.length; |
|
360 |
|
361 // Check for leading minus sign |
|
362 int sign = 1; |
|
363 if (val[0] == '-') { |
|
364 if (len == 1) |
|
365 throw new NumberFormatException("Zero length BigInteger"); |
|
366 sign = -1; |
|
367 cursor = 1; |
|
368 } else if (val[0] == '+') { |
|
369 if (len == 1) |
|
370 throw new NumberFormatException("Zero length BigInteger"); |
|
371 cursor = 1; |
|
372 } |
|
373 |
362 |
374 // Skip leading zeros and compute number of digits in magnitude |
363 // Skip leading zeros and compute number of digits in magnitude |
375 while (cursor < len && Character.digit(val[cursor], 10) == 0) |
364 while (cursor < len && Character.digit(val[cursor], 10) == 0) { |
376 cursor++; |
365 cursor++; |
|
366 } |
377 if (cursor == len) { |
367 if (cursor == len) { |
378 signum = 0; |
368 signum = 0; |
379 mag = ZERO.mag; |
369 mag = ZERO.mag; |
380 return; |
370 return; |
381 } |
371 } |
382 |
372 |
383 numDigits = len - cursor; |
373 numDigits = len - cursor; |
384 signum = sign; |
374 signum = sign; |
385 |
|
386 // Pre-allocate array of expected size |
375 // Pre-allocate array of expected size |
387 int numWords; |
376 int numWords; |
388 if (len < 10) { |
377 if (len < 10) { |
389 numWords = 1; |
378 numWords = 1; |
390 } else { |
379 } else { |
1056 |
1045 |
1057 return new BigInteger(resultMag, cmp == signum ? 1 : -1); |
1046 return new BigInteger(resultMag, cmp == signum ? 1 : -1); |
1058 } |
1047 } |
1059 |
1048 |
1060 /** |
1049 /** |
|
1050 * Package private methods used by BigDecimal code to add a BigInteger |
|
1051 * with a long. Assumes val is not equal to INFLATED. |
|
1052 */ |
|
1053 BigInteger add(long val) { |
|
1054 if (val == 0) |
|
1055 return this; |
|
1056 if (signum == 0) |
|
1057 return valueOf(val); |
|
1058 if (Long.signum(val) == signum) |
|
1059 return new BigInteger(add(mag, Math.abs(val)), signum); |
|
1060 int cmp = compareMagnitude(val); |
|
1061 if (cmp == 0) |
|
1062 return ZERO; |
|
1063 int[] resultMag = (cmp > 0 ? subtract(mag, Math.abs(val)) : subtract(Math.abs(val), mag)); |
|
1064 resultMag = trustedStripLeadingZeroInts(resultMag); |
|
1065 return new BigInteger(resultMag, cmp == signum ? 1 : -1); |
|
1066 } |
|
1067 |
|
1068 /** |
|
1069 * Adds the contents of the int array x and long value val. This |
|
1070 * method allocates a new int array to hold the answer and returns |
|
1071 * a reference to that array. Assumes x.length > 0 and val is |
|
1072 * non-negative |
|
1073 */ |
|
1074 private static int[] add(int[] x, long val) { |
|
1075 int[] y; |
|
1076 long sum = 0; |
|
1077 int xIndex = x.length; |
|
1078 int[] result; |
|
1079 int highWord = (int)(val >>> 32); |
|
1080 if (highWord==0) { |
|
1081 result = new int[xIndex]; |
|
1082 sum = (x[--xIndex] & LONG_MASK) + val; |
|
1083 result[xIndex] = (int)sum; |
|
1084 } else { |
|
1085 if (xIndex == 1) { |
|
1086 result = new int[2]; |
|
1087 sum = val + (x[0] & LONG_MASK); |
|
1088 result[1] = (int)sum; |
|
1089 result[0] = (int)(sum >>> 32); |
|
1090 return result; |
|
1091 } else { |
|
1092 result = new int[xIndex]; |
|
1093 sum = (x[--xIndex] & LONG_MASK) + (val & LONG_MASK); |
|
1094 result[xIndex] = (int)sum; |
|
1095 sum = (x[--xIndex] & LONG_MASK) + (highWord & LONG_MASK) + (sum >>> 32); |
|
1096 result[xIndex] = (int)sum; |
|
1097 } |
|
1098 } |
|
1099 // Copy remainder of longer number while carry propagation is required |
|
1100 boolean carry = (sum >>> 32 != 0); |
|
1101 while (xIndex > 0 && carry) |
|
1102 carry = ((result[--xIndex] = x[xIndex] + 1) == 0); |
|
1103 // Copy remainder of longer number |
|
1104 while (xIndex > 0) |
|
1105 result[--xIndex] = x[xIndex]; |
|
1106 // Grow result if necessary |
|
1107 if (carry) { |
|
1108 int bigger[] = new int[result.length + 1]; |
|
1109 System.arraycopy(result, 0, bigger, 1, result.length); |
|
1110 bigger[0] = 0x01; |
|
1111 return bigger; |
|
1112 } |
|
1113 return result; |
|
1114 } |
|
1115 |
|
1116 /** |
1061 * Adds the contents of the int arrays x and y. This method allocates |
1117 * Adds the contents of the int arrays x and y. This method allocates |
1062 * a new int array to hold the answer and returns a reference to that |
1118 * a new int array to hold the answer and returns a reference to that |
1063 * array. |
1119 * array. |
1064 */ |
1120 */ |
1065 private static int[] add(int[] x, int[] y) { |
1121 private static int[] add(int[] x, int[] y) { |
1072 |
1128 |
1073 int xIndex = x.length; |
1129 int xIndex = x.length; |
1074 int yIndex = y.length; |
1130 int yIndex = y.length; |
1075 int result[] = new int[xIndex]; |
1131 int result[] = new int[xIndex]; |
1076 long sum = 0; |
1132 long sum = 0; |
1077 |
1133 if(yIndex==1) { |
1078 // Add common parts of both numbers |
1134 sum = (x[--xIndex] & LONG_MASK) + (y[0] & LONG_MASK) ; |
1079 while(yIndex > 0) { |
|
1080 sum = (x[--xIndex] & LONG_MASK) + |
|
1081 (y[--yIndex] & LONG_MASK) + (sum >>> 32); |
|
1082 result[xIndex] = (int)sum; |
1135 result[xIndex] = (int)sum; |
1083 } |
1136 } else { |
1084 |
1137 // Add common parts of both numbers |
|
1138 while(yIndex > 0) { |
|
1139 sum = (x[--xIndex] & LONG_MASK) + |
|
1140 (y[--yIndex] & LONG_MASK) + (sum >>> 32); |
|
1141 result[xIndex] = (int)sum; |
|
1142 } |
|
1143 } |
1085 // Copy remainder of longer number while carry propagation is required |
1144 // Copy remainder of longer number while carry propagation is required |
1086 boolean carry = (sum >>> 32 != 0); |
1145 boolean carry = (sum >>> 32 != 0); |
1087 while (xIndex > 0 && carry) |
1146 while (xIndex > 0 && carry) |
1088 carry = ((result[--xIndex] = x[xIndex] + 1) == 0); |
1147 carry = ((result[--xIndex] = x[xIndex] + 1) == 0); |
1089 |
1148 |
1096 int bigger[] = new int[result.length + 1]; |
1155 int bigger[] = new int[result.length + 1]; |
1097 System.arraycopy(result, 0, bigger, 1, result.length); |
1156 System.arraycopy(result, 0, bigger, 1, result.length); |
1098 bigger[0] = 0x01; |
1157 bigger[0] = 0x01; |
1099 return bigger; |
1158 return bigger; |
1100 } |
1159 } |
|
1160 return result; |
|
1161 } |
|
1162 |
|
1163 private static int[] subtract(long val, int[] little) { |
|
1164 int highWord = (int)(val >>> 32); |
|
1165 if (highWord==0) { |
|
1166 int result[] = new int[1]; |
|
1167 result[0] = (int)(val - (little[0] & LONG_MASK)); |
|
1168 return result; |
|
1169 } else { |
|
1170 int result[] = new int[2]; |
|
1171 if(little.length==1) { |
|
1172 long difference = ((int)val & LONG_MASK) - (little[0] & LONG_MASK); |
|
1173 result[1] = (int)difference; |
|
1174 // Subtract remainder of longer number while borrow propagates |
|
1175 boolean borrow = (difference >> 32 != 0); |
|
1176 if(borrow) { |
|
1177 result[0] = highWord - 1; |
|
1178 } else { // Copy remainder of longer number |
|
1179 result[0] = highWord; |
|
1180 } |
|
1181 return result; |
|
1182 } else { // little.length==2 |
|
1183 long difference = ((int)val & LONG_MASK) - (little[1] & LONG_MASK); |
|
1184 result[1] = (int)difference; |
|
1185 difference = (highWord & LONG_MASK) - (little[0] & LONG_MASK) + (difference >> 32); |
|
1186 result[0] = (int)difference; |
|
1187 return result; |
|
1188 } |
|
1189 } |
|
1190 } |
|
1191 |
|
1192 /** |
|
1193 * Subtracts the contents of the second argument (val) from the |
|
1194 * first (big). The first int array (big) must represent a larger number |
|
1195 * than the second. This method allocates the space necessary to hold the |
|
1196 * answer. |
|
1197 * assumes val >= 0 |
|
1198 */ |
|
1199 private static int[] subtract(int[] big, long val) { |
|
1200 int highWord = (int)(val >>> 32); |
|
1201 int bigIndex = big.length; |
|
1202 int result[] = new int[bigIndex]; |
|
1203 long difference = 0; |
|
1204 |
|
1205 if (highWord==0) { |
|
1206 difference = (big[--bigIndex] & LONG_MASK) - val; |
|
1207 result[bigIndex] = (int)difference; |
|
1208 } else { |
|
1209 difference = (big[--bigIndex] & LONG_MASK) - (val & LONG_MASK); |
|
1210 result[bigIndex] = (int)difference; |
|
1211 difference = (big[--bigIndex] & LONG_MASK) - (highWord & LONG_MASK) + (difference >> 32); |
|
1212 result[bigIndex] = (int)difference; |
|
1213 } |
|
1214 |
|
1215 |
|
1216 // Subtract remainder of longer number while borrow propagates |
|
1217 boolean borrow = (difference >> 32 != 0); |
|
1218 while (bigIndex > 0 && borrow) |
|
1219 borrow = ((result[--bigIndex] = big[bigIndex] - 1) == -1); |
|
1220 |
|
1221 // Copy remainder of longer number |
|
1222 while (bigIndex > 0) |
|
1223 result[--bigIndex] = big[bigIndex]; |
|
1224 |
1101 return result; |
1225 return result; |
1102 } |
1226 } |
1103 |
1227 |
1104 /** |
1228 /** |
1105 * Returns a BigInteger whose value is {@code (this - val)}. |
1229 * Returns a BigInteger whose value is {@code (this - val)}. |
1163 * @return {@code this * val} |
1287 * @return {@code this * val} |
1164 */ |
1288 */ |
1165 public BigInteger multiply(BigInteger val) { |
1289 public BigInteger multiply(BigInteger val) { |
1166 if (val.signum == 0 || signum == 0) |
1290 if (val.signum == 0 || signum == 0) |
1167 return ZERO; |
1291 return ZERO; |
1168 |
1292 int resultSign = signum == val.signum ? 1 : -1; |
|
1293 if (val.mag.length == 1) { |
|
1294 return multiplyByInt(mag,val.mag[0], resultSign); |
|
1295 } |
|
1296 if(mag.length == 1) { |
|
1297 return multiplyByInt(val.mag,mag[0], resultSign); |
|
1298 } |
1169 int[] result = multiplyToLen(mag, mag.length, |
1299 int[] result = multiplyToLen(mag, mag.length, |
1170 val.mag, val.mag.length, null); |
1300 val.mag, val.mag.length, null); |
1171 result = trustedStripLeadingZeroInts(result); |
1301 result = trustedStripLeadingZeroInts(result); |
1172 return new BigInteger(result, signum == val.signum ? 1 : -1); |
1302 return new BigInteger(result, resultSign); |
|
1303 } |
|
1304 |
|
1305 private static BigInteger multiplyByInt(int[] x, int y, int sign) { |
|
1306 if(Integer.bitCount(y)==1) { |
|
1307 return new BigInteger(shiftLeft(x,Integer.numberOfTrailingZeros(y)), sign); |
|
1308 } |
|
1309 int xlen = x.length; |
|
1310 int[] rmag = new int[xlen + 1]; |
|
1311 long carry = 0; |
|
1312 long yl = y & LONG_MASK; |
|
1313 int rstart = rmag.length - 1; |
|
1314 for (int i = xlen - 1; i >= 0; i--) { |
|
1315 long product = (x[i] & LONG_MASK) * yl + carry; |
|
1316 rmag[rstart--] = (int)product; |
|
1317 carry = product >>> 32; |
|
1318 } |
|
1319 if (carry == 0L) { |
|
1320 rmag = java.util.Arrays.copyOfRange(rmag, 1, rmag.length); |
|
1321 } else { |
|
1322 rmag[rstart] = (int)carry; |
|
1323 } |
|
1324 return new BigInteger(rmag, sign); |
1173 } |
1325 } |
1174 |
1326 |
1175 /** |
1327 /** |
1176 * Package private methods used by BigDecimal code to multiply a BigInteger |
1328 * Package private methods used by BigDecimal code to multiply a BigInteger |
1177 * with a long. Assumes v is not equal to INFLATED. |
1329 * with a long. Assumes v is not equal to INFLATED. |
1337 public BigInteger divide(BigInteger val) { |
1489 public BigInteger divide(BigInteger val) { |
1338 MutableBigInteger q = new MutableBigInteger(), |
1490 MutableBigInteger q = new MutableBigInteger(), |
1339 a = new MutableBigInteger(this.mag), |
1491 a = new MutableBigInteger(this.mag), |
1340 b = new MutableBigInteger(val.mag); |
1492 b = new MutableBigInteger(val.mag); |
1341 |
1493 |
1342 a.divide(b, q); |
1494 a.divide(b, q, false); |
1343 return q.toBigInteger(this.signum == val.signum ? 1 : -1); |
1495 return q.toBigInteger(this.signum * val.signum); |
1344 } |
1496 } |
1345 |
1497 |
1346 /** |
1498 /** |
1347 * Returns an array of two BigIntegers containing {@code (this / val)} |
1499 * Returns an array of two BigIntegers containing {@code (this / val)} |
1348 * followed by {@code (this % val)}. |
1500 * followed by {@code (this % val)}. |
2067 throw new ArithmeticException("Shift distance of Integer.MIN_VALUE not supported."); |
2219 throw new ArithmeticException("Shift distance of Integer.MIN_VALUE not supported."); |
2068 } else { |
2220 } else { |
2069 return shiftRight(-n); |
2221 return shiftRight(-n); |
2070 } |
2222 } |
2071 } |
2223 } |
2072 |
2224 int[] newMag = shiftLeft(mag,n); |
|
2225 |
|
2226 return new BigInteger(newMag, signum); |
|
2227 } |
|
2228 |
|
2229 private static int[] shiftLeft(int[] mag, int n) { |
2073 int nInts = n >>> 5; |
2230 int nInts = n >>> 5; |
2074 int nBits = n & 0x1f; |
2231 int nBits = n & 0x1f; |
2075 int magLen = mag.length; |
2232 int magLen = mag.length; |
2076 int newMag[] = null; |
2233 int newMag[] = null; |
2077 |
2234 |
2092 int j=0; |
2249 int j=0; |
2093 while (j < magLen-1) |
2250 while (j < magLen-1) |
2094 newMag[i++] = mag[j++] << nBits | mag[j] >>> nBits2; |
2251 newMag[i++] = mag[j++] << nBits | mag[j] >>> nBits2; |
2095 newMag[i] = mag[j] << nBits; |
2252 newMag[i] = mag[j] << nBits; |
2096 } |
2253 } |
2097 |
2254 return newMag; |
2098 return new BigInteger(newMag, signum); |
|
2099 } |
2255 } |
2100 |
2256 |
2101 /** |
2257 /** |
2102 * Returns a BigInteger whose value is {@code (this >> n)}. Sign |
2258 * Returns a BigInteger whose value is {@code (this >> n)}. Sign |
2103 * extension is performed. The shift distance, {@code n}, may be |
2259 * extension is performed. The shift distance, {@code n}, may be |
2525 int b = m2[i]; |
2681 int b = m2[i]; |
2526 if (a != b) |
2682 if (a != b) |
2527 return ((a & LONG_MASK) < (b & LONG_MASK)) ? -1 : 1; |
2683 return ((a & LONG_MASK) < (b & LONG_MASK)) ? -1 : 1; |
2528 } |
2684 } |
2529 return 0; |
2685 return 0; |
|
2686 } |
|
2687 |
|
2688 /** |
|
2689 * Version of compareMagnitude that compares magnitude with long value. |
|
2690 * val can't be Long.MIN_VALUE. |
|
2691 */ |
|
2692 final int compareMagnitude(long val) { |
|
2693 assert val != Long.MIN_VALUE; |
|
2694 int[] m1 = mag; |
|
2695 int len = m1.length; |
|
2696 if(len > 2) { |
|
2697 return 1; |
|
2698 } |
|
2699 if (val < 0) { |
|
2700 val = -val; |
|
2701 } |
|
2702 int highWord = (int)(val >>> 32); |
|
2703 if (highWord==0) { |
|
2704 if (len < 1) |
|
2705 return -1; |
|
2706 if (len > 1) |
|
2707 return 1; |
|
2708 int a = m1[0]; |
|
2709 int b = (int)val; |
|
2710 if (a != b) { |
|
2711 return ((a & LONG_MASK) < (b & LONG_MASK))? -1 : 1; |
|
2712 } |
|
2713 return 0; |
|
2714 } else { |
|
2715 if (len < 2) |
|
2716 return -1; |
|
2717 int a = m1[0]; |
|
2718 int b = highWord; |
|
2719 if (a != b) { |
|
2720 return ((a & LONG_MASK) < (b & LONG_MASK))? -1 : 1; |
|
2721 } |
|
2722 a = m1[1]; |
|
2723 b = (int)val; |
|
2724 if (a != b) { |
|
2725 return ((a & LONG_MASK) < (b & LONG_MASK))? -1 : 1; |
|
2726 } |
|
2727 return 0; |
|
2728 } |
2530 } |
2729 } |
2531 |
2730 |
2532 /** |
2731 /** |
2533 * Compares this BigInteger with the specified Object for equality. |
2732 * Compares this BigInteger with the specified Object for equality. |
2534 * |
2733 * |