BigInteger patch for efficient multiplication and division (bug #4837946) (original) (raw)

--- jdk8/jdk/src/share/classes/java/math/BigInteger.java 2012-12-28 20:43:25.314218154 +0100

+++ src/main/java/java/math/BigInteger.java 2012-12-28 14:59:56.049558654 +0100

@@ -94,6 +94,9 @@

* @see BigDecimal

* @author Josh Bloch

* @author Michael McCloskey

* @since JDK1.1

*/

@@ -174,6 +177,30 @@

*/

final static long LONG_MASK = 0xffffffffL;

//Constructors

/**

@@ -978,6 +1005,19 @@

return (val[0]>0 ? new BigInteger(val, 1) : new BigInteger(val));

}

// Constants

/**

@@ -1290,17 +1330,28 @@

public BigInteger multiply(BigInteger val) {

if (val.signum == 0 || signum == 0)

return ZERO;

- int resultSign = signum == val.signum ? 1 : -1;

- if (val.mag.length == 1) {

- return multiplyByInt(mag,val.mag[0], resultSign);

- }

- if(mag.length == 1) {

- return multiplyByInt(val.mag,mag[0], resultSign);

- }

- int[] result = multiplyToLen(mag, mag.length,

- val.mag, val.mag.length, null);

- result = trustedStripLeadingZeroInts(result);

- return new BigInteger(result, resultSign);

}

private static BigInteger multiplyByInt(int[] x, int y, int sign) {

@@ -1402,6 +1453,265 @@

}

/**

* Returns a BigInteger whose value is {@code (this2)}.

*

* @return {@code this2}

@@ -1488,6 +1798,14 @@

* @throws ArithmeticException if {@code val} is zero.

*/

public BigInteger divide(BigInteger val) {

MutableBigInteger q = new MutableBigInteger(),

a = new MutableBigInteger(this.mag),

b = new MutableBigInteger(val.mag);

@@ -1508,6 +1826,14 @@

* @throws ArithmeticException if {@code val} is zero.

*/

public BigInteger[] divideAndRemainder(BigInteger val) {

BigInteger[] result = new BigInteger[2];

MutableBigInteger q = new MutableBigInteger(),

a = new MutableBigInteger(this.mag),

@@ -1527,6 +1853,14 @@

* @throws ArithmeticException if {@code val} is zero.

*/

public BigInteger remainder(BigInteger val) {

MutableBigInteger q = new MutableBigInteger(),

a = new MutableBigInteger(this.mag),

b = new MutableBigInteger(val.mag);

@@ -1535,6 +1869,207 @@

}

/**

* Returns a BigInteger whose value is (thisexponent).

* Note that {@code exponent} is an integer rather than a BigInteger.

*

@@ -2323,6 +2858,28 @@

return val;

}

// Bitwise Operations

/**