diff options
author | Per Bothner <per@bothner.com> | 2001-08-28 15:16:11 -0700 |
---|---|---|
committer | Per Bothner <bothner@gcc.gnu.org> | 2001-08-28 15:16:11 -0700 |
commit | 4046760603306f262092ca9bbf62bdf3e3f9c0e7 (patch) | |
tree | 9006eb39ecab92e379bbc4ea2f566084f034734f /libjava/java/math | |
parent | 967603efc9e5f6772e57ae5d1375e6cb79e17a34 (diff) | |
download | gcc-4046760603306f262092ca9bbf62bdf3e3f9c0e7.zip gcc-4046760603306f262092ca9bbf62bdf3e3f9c0e7.tar.gz gcc-4046760603306f262092ca9bbf62bdf3e3f9c0e7.tar.bz2 |
BigInteger.java (init(int,Random)): New method.
* java/math/BigInteger.java (init(int,Random)): New method.
Move body of constructor <init>(int,Random)) here.
Re-write it to avoid constructing unneeded temporaries.
(<init>(int,int,Random)): Use new init method to avoid constructing
extra temporary BigIntegers.
From-SVN: r45240
Diffstat (limited to 'libjava/java/math')
-rw-r--r-- | libjava/java/math/BigInteger.java | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/libjava/java/math/BigInteger.java b/libjava/java/math/BigInteger.java index b9bfee6..f4a03f9 100644 --- a/libjava/java/math/BigInteger.java +++ b/libjava/java/math/BigInteger.java @@ -144,20 +144,36 @@ public class BigInteger extends Number implements Comparable public BigInteger(int numBits, Random rnd) { - this(1, randBytes(numBits, rnd)); + if (numBits < 0) + throw new IllegalArgumentException(); + + init(numBits, rnd); } - private static byte[] randBytes(int numBits, Random rnd) + private void init(int numBits, Random rnd) { - if (numBits < 0) - throw new IllegalArgumentException(); + int highbits = numBits & 31; + if (highbits > 0) + highbits = rnd.nextInt() >>> (32 - highbits); + int nwords = numBits / 32; - 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; + 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); } } |