OSDN Git Service

* java/math/BigInteger.java (init(int,Random)): New method.
authorbothner <bothner@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 28 Aug 2001 22:16:11 +0000 (22:16 +0000)
committerbothner <bothner@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 28 Aug 2001 22:16:11 +0000 (22:16 +0000)
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.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@45240 138bc75d-0d04-0410-961f-82ee72b054a4

libjava/ChangeLog
libjava/java/math/BigInteger.java

index 350efc0..8fe5f92 100644 (file)
@@ -1,3 +1,11 @@
+2001-08-28  Per Bothner  <per@bothner.com>
+
+       * 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.
+       
 2001-08-27  Tom Tromey  <tromey@redhat.com>
 
        * java/rmi/activation/Activatable.java,
index b9bfee6..f4a03f9 100644 (file)
@@ -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);
       }
   }