diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 350efc02b081..8fe5f923cbda 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,11 @@ +2001-08-28 Per Bothner + + * java/math/BigInteger.java (init(int,Random)): New method. + Move body of constructor (int,Random)) here. + Re-write it to avoid constructing unneeded temporaries. + ((int,int,Random)): Use new init method to avoid constructing + extra temporary BigIntegers. + 2001-08-27 Tom Tromey * java/rmi/activation/Activatable.java, diff --git a/libjava/java/math/BigInteger.java b/libjava/java/math/BigInteger.java index b9bfee695b50..f4a03f901238 100644 --- a/libjava/java/math/BigInteger.java +++ b/libjava/java/math/BigInteger.java @@ -143,21 +143,37 @@ public class BigInteger extends Number implements Comparable } public BigInteger(int numBits, Random rnd) - { - this(1, randBytes(numBits, rnd)); - } - - private static byte[] randBytes(int numBits, Random rnd) { if (numBits < 0) throw new IllegalArgumentException(); - int extra = numBits % 8; - byte[] b = new byte[numBits / 8 + (extra > 0 ? 1 : 0)]; - rnd.nextBytes(b); - if (extra > 0) - b[0] &= ~((~0) << (8 - extra)); - return b; + init(numBits, rnd); + } + + private void init(int numBits, Random rnd) + { + int highbits = numBits & 31; + if (highbits > 0) + highbits = rnd.nextInt() >>> (32 - highbits); + int nwords = numBits / 32; + + while (highbits == 0 && nwords > 0) + { + highbits = rnd.nextInt(); + --nwords; + } + if (nwords == 0 && highbits >= 0) + { + ival = highbits; + } + else + { + ival = highbits < 0 ? nwords + 2 : nwords + 1; + words = new int[ival]; + words[nwords] = highbits; + while (--nwords >= 0) + words[nwords] = rnd.nextInt(); + } } public BigInteger(int bitLength, int certainty, Random rnd) @@ -170,9 +186,7 @@ public class BigInteger extends Number implements Comparable if (isProbablePrime(certainty)) return; - BigInteger next = new BigInteger(bitLength, rnd); - this.ival = next.ival; - this.words = next.words; + init(bitLength, rnd); } }