< prev index next >
src/java.base/share/classes/java/math/BigInteger.java
Print this page
*** 3945,3970 ****
(signum < 0 ? 1 : 0);
StringBuilder sb = new StringBuilder(numChars);
// If it's small enough, use smallToString.
if (mag.length <= SCHOENHAGE_BASE_CONVERSION_THRESHOLD) {
! smallToString(radix, sb);
return sb.toString();
}
// Otherwise use recursive toString.
// The results will be concatenated into this StringBuilder
toString(this, sb, radix, 0);
return sb.toString();
}
! /** This method is used to perform toString when arguments are small. */
! private int smallToString(int radix, StringBuilder buf) {
if (signum == 0) {
buf.append('0');
! return 1;
}
// Compute upper bound on number of digit groups and allocate space
int maxNumDigitGroups = (4*mag.length + 6)/7;
long[] digitGroups = new long[maxNumDigitGroups];
--- 3945,3986 ----
(signum < 0 ? 1 : 0);
StringBuilder sb = new StringBuilder(numChars);
// If it's small enough, use smallToString.
if (mag.length <= SCHOENHAGE_BASE_CONVERSION_THRESHOLD) {
! // smallToString() will prepend a negative sign if this < 0,
! // but will not pad with any leading zeros.
! smallToString(radix, sb, -1);
return sb.toString();
}
// Otherwise use recursive toString.
// The results will be concatenated into this StringBuilder
toString(this, sb, radix, 0);
return sb.toString();
}
! /**
! * This method is used to perform toString when arguments are small.
! * A negative sign will be prepended if and only if {@code this < 0}.
! * If {@code digits <= 0} no padding (pre-pending with zeros) will be
! * effected.
! *
! * @param radix The base to convert to.
! * @param sb The StringBuilder that will be appended to in place.
! * @param digits The minimum number of digits to pad to.
! */
! private void smallToString(int radix, StringBuilder buf, int digits) {
if (signum == 0) {
buf.append('0');
! return;
! }
!
! // Put sign (if any) into result buffer
! if (signum < 0) {
! buf.append('-');
}
// Compute upper bound on number of digit groups and allocate space
int maxNumDigitGroups = (4*mag.length + 6)/7;
long[] digitGroups = new long[maxNumDigitGroups];
*** 3984,4018 ****
digitGroups[numGroups++] = r2.longValue();
tmp = q2;
}
! int count = 0;
! // Put sign (if any) and first digit group into result buffer
! if (signum < 0) {
! buf.append('-');
! count++;
}
! String s = Long.toString(digitGroups[numGroups-1], radix);
buf.append(s);
- count += s.length();
! // Append remaining digit groups padded with leading zeros
for (int i=numGroups-2; i >= 0; i--) {
// Prepend (any) leading zeros for this digit group
s = Long.toString(digitGroups[i], radix);
int numLeadingZeros = digitsPerLong[radix] - s.length();
if (numLeadingZeros != 0) {
buf.append(zeros, 0, numLeadingZeros);
- count += numLeadingZeros;
}
buf.append(s);
- count += s.length();
}
-
- return count;
}
/**
* Converts the specified BigInteger to a string and appends to
* {@code sb}. This implements the recursive Schoenhage algorithm
--- 4000,4040 ----
digitGroups[numGroups++] = r2.longValue();
tmp = q2;
}
! // Get string version of first digit group
! String s = Long.toString(digitGroups[numGroups-1], radix);
! // Pad with internal zeros if necessary.
! if (digits > 0) {
! int len = s.length() + (numGroups - 1)*digitsPerLong[radix];
! if (len < digits) {
! int m = digits - len;
! while (m >= NUM_ZEROS) {
! buf.append(zeros);
! m -= NUM_ZEROS;
}
! if (m > 0) {
! buf.append(zeros, 0, m);
! }
! }
! }
!
! // Put first digit group into result buffer
buf.append(s);
! // Append remaining digit groups each padded with leading zeros
for (int i=numGroups-2; i >= 0; i--) {
// Prepend (any) leading zeros for this digit group
s = Long.toString(digitGroups[i], radix);
int numLeadingZeros = digitsPerLong[radix] - s.length();
if (numLeadingZeros != 0) {
buf.append(zeros, 0, numLeadingZeros);
}
buf.append(s);
}
}
/**
* Converts the specified BigInteger to a string and appends to
* {@code sb}. This implements the recursive Schoenhage algorithm
*** 4026,4072 ****
* @param radix The base to convert to.
* @param digits The minimum number of digits to pad to.
*/
private static void toString(BigInteger u, StringBuilder sb,
int radix, int digits) {
boolean atBeginning = sb.length() == 0;
if (u.signum() < 0) {
u = u.negate();
sb.append('-');
}
// If we're smaller than a certain threshold, use the smallToString
! // method, padding with leading zeroes when necessary.
if (u.mag.length <= SCHOENHAGE_BASE_CONVERSION_THRESHOLD) {
! // Save current position.
! int pos = sb.length();
! int len = u.smallToString(radix, sb);
!
! // Pad with internal zeros if necessary.
! // Don't pad if we're at the beginning of the string.
! if (!atBeginning && len < digits) {
! int m = digits - len;
! while (m >= NUM_ZEROS) {
! sb.insert(pos, zeros, 0, NUM_ZEROS);
! pos += NUM_ZEROS;
! m -= NUM_ZEROS;
! }
! if (m > 0) {
! sb.insert(pos, zeros, 0, m);
! }
! }
!
return;
}
- int b = u.bitLength();
-
// Calculate a value for n in the equation radix^(2^n) = u
// and subtract 1 from that value. This is used to find the
// cache index that contains the best value to divide u.
int n = (int) Math.round(Math.log(b * LOG_TWO / logCache[radix]) /
LOG_TWO - 1.0);
BigInteger v = getRadixConversionCache(radix, n);
BigInteger[] results;
results = u.divideAndRemainder(v);
int expectedDigits = 1 << n;
--- 4048,4082 ----
* @param radix The base to convert to.
* @param digits The minimum number of digits to pad to.
*/
private static void toString(BigInteger u, StringBuilder sb,
int radix, int digits) {
+ // We're at the beginning if nothing is in the StringBuilder.
boolean atBeginning = sb.length() == 0;
+
+ // Make negative values positive and prepend a negative sign.
if (u.signum() < 0) {
u = u.negate();
sb.append('-');
}
// If we're smaller than a certain threshold, use the smallToString
! // method, padding with leading zeroes when necessary unless we're
! // at the beginning of the string or digits <= 0. As u.signum() >= 0,
! // smallToString() will not prepend a negative sign.
if (u.mag.length <= SCHOENHAGE_BASE_CONVERSION_THRESHOLD) {
! u.smallToString(radix, sb, atBeginning ? -1 : digits);
return;
}
// Calculate a value for n in the equation radix^(2^n) = u
// and subtract 1 from that value. This is used to find the
// cache index that contains the best value to divide u.
+ int b = u.bitLength();
int n = (int) Math.round(Math.log(b * LOG_TWO / logCache[radix]) /
LOG_TWO - 1.0);
+
BigInteger v = getRadixConversionCache(radix, n);
BigInteger[] results;
results = u.divideAndRemainder(v);
int expectedDigits = 1 << n;
< prev index next >