| 1 | /* java.math.BigInteger -- Arbitary precision integers
|
|---|
| 2 | Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 | package java.math;
|
|---|
| 39 |
|
|---|
| 40 | import gnu.java.math.MPN;
|
|---|
| 41 | import java.util.Random;
|
|---|
| 42 | import java.io.ObjectInputStream;
|
|---|
| 43 | import java.io.ObjectOutputStream;
|
|---|
| 44 | import java.io.IOException;
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * @author Warren Levy <[email protected]>
|
|---|
| 48 | * @date December 20, 1999.
|
|---|
| 49 | */
|
|---|
| 50 |
|
|---|
| 51 | /**
|
|---|
| 52 | * Written using on-line Java Platform 1.2 API Specification, as well
|
|---|
| 53 | * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998) and
|
|---|
| 54 | * "Applied Cryptography, Second Edition" by Bruce Schneier (Wiley, 1996).
|
|---|
| 55 | *
|
|---|
| 56 | * Based primarily on IntNum.java BitOps.java by Per Bothner <[email protected]>
|
|---|
| 57 | * (found in Kawa 1.6.62).
|
|---|
| 58 | *
|
|---|
| 59 | * Status: Believed complete and correct.
|
|---|
| 60 | */
|
|---|
| 61 |
|
|---|
| 62 | public class BigInteger extends Number implements Comparable
|
|---|
| 63 | {
|
|---|
| 64 | /** All integers are stored in 2's-complement form.
|
|---|
| 65 | * If words == null, the ival is the value of this BigInteger.
|
|---|
| 66 | * Otherwise, the first ival elements of words make the value
|
|---|
| 67 | * of this BigInteger, stored in little-endian order, 2's-complement form. */
|
|---|
| 68 | transient private int ival;
|
|---|
| 69 | transient private int[] words;
|
|---|
| 70 |
|
|---|
| 71 | // Serialization fields.
|
|---|
| 72 | private int bitCount = -1;
|
|---|
| 73 | private int bitLength = -1;
|
|---|
| 74 | private int firstNonzeroByteNum = -2;
|
|---|
| 75 | private int lowestSetBit = -2;
|
|---|
| 76 | private byte[] magnitude;
|
|---|
| 77 | private int signum;
|
|---|
| 78 | private static final long serialVersionUID = -8287574255936472291L;
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | /** We pre-allocate integers in the range minFixNum..maxFixNum. */
|
|---|
| 82 | private static final int minFixNum = -100;
|
|---|
| 83 | private static final int maxFixNum = 1024;
|
|---|
| 84 | private static final int numFixNum = maxFixNum-minFixNum+1;
|
|---|
| 85 | private static final BigInteger[] smallFixNums = new BigInteger[numFixNum];
|
|---|
| 86 |
|
|---|
| 87 | static {
|
|---|
| 88 | for (int i = numFixNum; --i >= 0; )
|
|---|
| 89 | smallFixNums[i] = new BigInteger(i + minFixNum);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | // JDK1.2
|
|---|
| 93 | public static final BigInteger ZERO = smallFixNums[-minFixNum];
|
|---|
| 94 |
|
|---|
| 95 | // JDK1.2
|
|---|
| 96 | public static final BigInteger ONE = smallFixNums[1 - minFixNum];
|
|---|
| 97 |
|
|---|
| 98 | /* Rounding modes: */
|
|---|
| 99 | private static final int FLOOR = 1;
|
|---|
| 100 | private static final int CEILING = 2;
|
|---|
| 101 | private static final int TRUNCATE = 3;
|
|---|
| 102 | private static final int ROUND = 4;
|
|---|
| 103 |
|
|---|
| 104 | /** When checking the probability of primes, it is most efficient to
|
|---|
| 105 | * first check the factoring of small primes, so we'll use this array.
|
|---|
| 106 | */
|
|---|
| 107 | private static final int[] primes =
|
|---|
| 108 | { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
|
|---|
| 109 | 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107,
|
|---|
| 110 | 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181,
|
|---|
| 111 | 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251 };
|
|---|
| 112 |
|
|---|
| 113 | private BigInteger()
|
|---|
| 114 | {
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /* Create a new (non-shared) BigInteger, and initialize to an int. */
|
|---|
| 118 | private BigInteger(int value)
|
|---|
| 119 | {
|
|---|
| 120 | ival = value;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | public BigInteger(String val, int radix)
|
|---|
| 124 | {
|
|---|
| 125 | BigInteger result = valueOf(val, radix);
|
|---|
| 126 | this.ival = result.ival;
|
|---|
| 127 | this.words = result.words;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | public BigInteger(String val)
|
|---|
| 131 | {
|
|---|
| 132 | this(val, 10);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /* Create a new (non-shared) BigInteger, and initialize from a byte array. */
|
|---|
| 136 | public BigInteger(byte[] val)
|
|---|
| 137 | {
|
|---|
| 138 | if (val == null || val.length < 1)
|
|---|
| 139 | throw new NumberFormatException();
|
|---|
| 140 |
|
|---|
| 141 | words = byteArrayToIntArray(val, val[0] < 0 ? -1 : 0);
|
|---|
| 142 | BigInteger result = make(words, words.length);
|
|---|
| 143 | this.ival = result.ival;
|
|---|
| 144 | this.words = result.words;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | public BigInteger(int signum, byte[] magnitude)
|
|---|
| 148 | {
|
|---|
| 149 | if (magnitude == null || signum > 1 || signum < -1)
|
|---|
| 150 | throw new NumberFormatException();
|
|---|
| 151 |
|
|---|
| 152 | if (signum == 0)
|
|---|
| 153 | {
|
|---|
| 154 | int i;
|
|---|
| 155 | for (i = magnitude.length - 1; i >= 0 && magnitude[i] == 0; --i)
|
|---|
| 156 | ;
|
|---|
| 157 | if (i >= 0)
|
|---|
| 158 | throw new NumberFormatException();
|
|---|
| 159 | return;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | // Magnitude is always positive, so don't ever pass a sign of -1.
|
|---|
| 163 | words = byteArrayToIntArray(magnitude, 0);
|
|---|
| 164 | BigInteger result = make(words, words.length);
|
|---|
| 165 | this.ival = result.ival;
|
|---|
| 166 | this.words = result.words;
|
|---|
| 167 |
|
|---|
| 168 | if (signum < 0)
|
|---|
| 169 | setNegative();
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | public BigInteger(int numBits, Random rnd)
|
|---|
| 173 | {
|
|---|
| 174 | if (numBits < 0)
|
|---|
| 175 | throw new IllegalArgumentException();
|
|---|
| 176 |
|
|---|
| 177 | init(numBits, rnd);
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | private void init(int numBits, Random rnd)
|
|---|
| 181 | {
|
|---|
| 182 | int highbits = numBits & 31;
|
|---|
| 183 | if (highbits > 0)
|
|---|
| 184 | highbits = rnd.nextInt() >>> (32 - highbits);
|
|---|
| 185 | int nwords = numBits / 32;
|
|---|
| 186 |
|
|---|
| 187 | while (highbits == 0 && nwords > 0)
|
|---|
| 188 | {
|
|---|
| 189 | highbits = rnd.nextInt();
|
|---|
| 190 | --nwords;
|
|---|
| 191 | }
|
|---|
| 192 | if (nwords == 0 && highbits >= 0)
|
|---|
| 193 | {
|
|---|
| 194 | ival = highbits;
|
|---|
| 195 | }
|
|---|
| 196 | else
|
|---|
| 197 | {
|
|---|
| 198 | ival = highbits < 0 ? nwords + 2 : nwords + 1;
|
|---|
| 199 | words = new int[ival];
|
|---|
| 200 | words[nwords] = highbits;
|
|---|
| 201 | while (--nwords >= 0)
|
|---|
| 202 | words[nwords] = rnd.nextInt();
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | public BigInteger(int bitLength, int certainty, Random rnd)
|
|---|
| 207 | {
|
|---|
| 208 | this(bitLength, rnd);
|
|---|
| 209 |
|
|---|
| 210 | // Keep going until we find a probable prime.
|
|---|
| 211 | while (true)
|
|---|
| 212 | {
|
|---|
| 213 | if (isProbablePrime(certainty))
|
|---|
| 214 | return;
|
|---|
| 215 |
|
|---|
| 216 | init(bitLength, rnd);
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | /** Return a (possibly-shared) BigInteger with a given long value. */
|
|---|
| 221 | private static BigInteger make(long value)
|
|---|
| 222 | {
|
|---|
| 223 | if (value >= minFixNum && value <= maxFixNum)
|
|---|
| 224 | return smallFixNums[(int)value - minFixNum];
|
|---|
| 225 | int i = (int) value;
|
|---|
| 226 | if ((long)i == value)
|
|---|
| 227 | return new BigInteger(i);
|
|---|
| 228 | BigInteger result = alloc(2);
|
|---|
| 229 | result.ival = 2;
|
|---|
| 230 | result.words[0] = i;
|
|---|
| 231 | result.words[1] = (int) (value >> 32);
|
|---|
| 232 | return result;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | // FIXME: Could simply rename 'make' method above as valueOf while
|
|---|
| 236 | // changing all instances of 'make'. Don't do this until this class
|
|---|
| 237 | // is done as the Kawa class this is based on has 'make' methods
|
|---|
| 238 | // with other parameters; wait to see if they are used in BigInteger.
|
|---|
| 239 | public static BigInteger valueOf(long val)
|
|---|
| 240 | {
|
|---|
| 241 | return make(val);
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | /** Make a canonicalized BigInteger from an array of words.
|
|---|
| 245 | * The array may be reused (without copying). */
|
|---|
| 246 | private static BigInteger make(int[] words, int len)
|
|---|
| 247 | {
|
|---|
| 248 | if (words == null)
|
|---|
| 249 | return make(len);
|
|---|
| 250 | len = BigInteger.wordsNeeded(words, len);
|
|---|
| 251 | if (len <= 1)
|
|---|
| 252 | return len == 0 ? ZERO : make(words[0]);
|
|---|
| 253 | BigInteger num = new BigInteger();
|
|---|
| 254 | num.words = words;
|
|---|
| 255 | num.ival = len;
|
|---|
| 256 | return num;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | /** Convert a big-endian byte array to a little-endian array of words. */
|
|---|
| 260 | private static int[] byteArrayToIntArray(byte[] bytes, int sign)
|
|---|
| 261 | {
|
|---|
| 262 | // Determine number of words needed.
|
|---|
| 263 | int[] words = new int[bytes.length/4 + 1];
|
|---|
| 264 | int nwords = words.length;
|
|---|
| 265 |
|
|---|
| 266 | // Create a int out of modulo 4 high order bytes.
|
|---|
| 267 | int bptr = 0;
|
|---|
| 268 | int word = sign;
|
|---|
| 269 | for (int i = bytes.length % 4; i > 0; --i, bptr++)
|
|---|
| 270 | word = (word << 8) | (((int) bytes[bptr]) & 0xff);
|
|---|
| 271 | words[--nwords] = word;
|
|---|
| 272 |
|
|---|
| 273 | // Elements remaining in byte[] are a multiple of 4.
|
|---|
| 274 | while (nwords > 0)
|
|---|
| 275 | words[--nwords] = bytes[bptr++] << 24 |
|
|---|
| 276 | (((int) bytes[bptr++]) & 0xff) << 16 |
|
|---|
| 277 | (((int) bytes[bptr++]) & 0xff) << 8 |
|
|---|
| 278 | (((int) bytes[bptr++]) & 0xff);
|
|---|
| 279 | return words;
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | /** Allocate a new non-shared BigInteger.
|
|---|
| 283 | * @param nwords number of words to allocate
|
|---|
| 284 | */
|
|---|
| 285 | private static BigInteger alloc(int nwords)
|
|---|
| 286 | {
|
|---|
| 287 | if (nwords <= 1)
|
|---|
| 288 | return new BigInteger();
|
|---|
| 289 | BigInteger result = new BigInteger();
|
|---|
| 290 | result.words = new int[nwords];
|
|---|
| 291 | return result;
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | /** Change words.length to nwords.
|
|---|
| 295 | * We allow words.length to be upto nwords+2 without reallocating.
|
|---|
| 296 | */
|
|---|
| 297 | private void realloc(int nwords)
|
|---|
| 298 | {
|
|---|
| 299 | if (nwords == 0)
|
|---|
| 300 | {
|
|---|
| 301 | if (words != null)
|
|---|
| 302 | {
|
|---|
| 303 | if (ival > 0)
|
|---|
| 304 | ival = words[0];
|
|---|
| 305 | words = null;
|
|---|
| 306 | }
|
|---|
| 307 | }
|
|---|
| 308 | else if (words == null
|
|---|
| 309 | || words.length < nwords
|
|---|
| 310 | || words.length > nwords + 2)
|
|---|
| 311 | {
|
|---|
| 312 | int[] new_words = new int [nwords];
|
|---|
| 313 | if (words == null)
|
|---|
| 314 | {
|
|---|
| 315 | new_words[0] = ival;
|
|---|
| 316 | ival = 1;
|
|---|
| 317 | }
|
|---|
| 318 | else
|
|---|
| 319 | {
|
|---|
| 320 | if (nwords < ival)
|
|---|
| 321 | ival = nwords;
|
|---|
| 322 | System.arraycopy(words, 0, new_words, 0, ival);
|
|---|
| 323 | }
|
|---|
| 324 | words = new_words;
|
|---|
| 325 | }
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | private final boolean isNegative()
|
|---|
| 329 | {
|
|---|
| 330 | return (words == null ? ival : words[ival - 1]) < 0;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | public int signum()
|
|---|
| 334 | {
|
|---|
| 335 | int top = words == null ? ival : words[ival-1];
|
|---|
| 336 | if (top == 0 && words == null)
|
|---|
| 337 | return 0;
|
|---|
| 338 | return top < 0 ? -1 : 1;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | private static int compareTo(BigInteger x, BigInteger y)
|
|---|
| 342 | {
|
|---|
| 343 | if (x.words == null && y.words == null)
|
|---|
| 344 | return x.ival < y.ival ? -1 : x.ival > y.ival ? 1 : 0;
|
|---|
| 345 | boolean x_negative = x.isNegative();
|
|---|
| 346 | boolean y_negative = y.isNegative();
|
|---|
| 347 | if (x_negative != y_negative)
|
|---|
| 348 | return x_negative ? -1 : 1;
|
|---|
| 349 | int x_len = x.words == null ? 1 : x.ival;
|
|---|
| 350 | int y_len = y.words == null ? 1 : y.ival;
|
|---|
| 351 | if (x_len != y_len)
|
|---|
| 352 | return (x_len > y_len) != x_negative ? 1 : -1;
|
|---|
| 353 | return MPN.cmp(x.words, y.words, x_len);
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | // JDK1.2
|
|---|
| 357 | public int compareTo(Object obj)
|
|---|
| 358 | {
|
|---|
| 359 | if (obj instanceof BigInteger)
|
|---|
| 360 | return compareTo(this, (BigInteger) obj);
|
|---|
| 361 | throw new ClassCastException();
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | public int compareTo(BigInteger val)
|
|---|
| 365 | {
|
|---|
| 366 | return compareTo(this, val);
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | public BigInteger min(BigInteger val)
|
|---|
| 370 | {
|
|---|
| 371 | return compareTo(this, val) < 0 ? this : val;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | public BigInteger max(BigInteger val)
|
|---|
| 375 | {
|
|---|
| 376 | return compareTo(this, val) > 0 ? this : val;
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | private final boolean isOdd()
|
|---|
| 380 | {
|
|---|
| 381 | int low = words == null ? ival : words[0];
|
|---|
| 382 | return (low & 1) != 0;
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | private final boolean isZero()
|
|---|
| 386 | {
|
|---|
| 387 | return words == null && ival == 0;
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | private final boolean isOne()
|
|---|
| 391 | {
|
|---|
| 392 | return words == null && ival == 1;
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | private final boolean isMinusOne()
|
|---|
| 396 | {
|
|---|
| 397 | return words == null && ival == -1;
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | /** Calculate how many words are significant in words[0:len-1].
|
|---|
| 401 | * Returns the least value x such that x>0 && words[0:x-1]==words[0:len-1],
|
|---|
| 402 | * when words is viewed as a 2's complement integer.
|
|---|
| 403 | */
|
|---|
| 404 | private static int wordsNeeded(int[] words, int len)
|
|---|
| 405 | {
|
|---|
| 406 | int i = len;
|
|---|
| 407 | if (i > 0)
|
|---|
| 408 | {
|
|---|
| 409 | int word = words[--i];
|
|---|
| 410 | if (word == -1)
|
|---|
| 411 | {
|
|---|
| 412 | while (i > 0 && (word = words[i - 1]) < 0)
|
|---|
| 413 | {
|
|---|
| 414 | i--;
|
|---|
| 415 | if (word != -1) break;
|
|---|
| 416 | }
|
|---|
| 417 | }
|
|---|
| 418 | else
|
|---|
| 419 | {
|
|---|
| 420 | while (word == 0 && i > 0 && (word = words[i - 1]) >= 0) i--;
|
|---|
| 421 | }
|
|---|
| 422 | }
|
|---|
| 423 | return i + 1;
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | private BigInteger canonicalize()
|
|---|
| 427 | {
|
|---|
| 428 | if (words != null
|
|---|
| 429 | && (ival = BigInteger.wordsNeeded(words, ival)) <= 1)
|
|---|
| 430 | {
|
|---|
| 431 | if (ival == 1)
|
|---|
| 432 | ival = words[0];
|
|---|
| 433 | words = null;
|
|---|
| 434 | }
|
|---|
| 435 | if (words == null && ival >= minFixNum && ival <= maxFixNum)
|
|---|
| 436 | return smallFixNums[(int) ival - minFixNum];
|
|---|
| 437 | return this;
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | /** Add two ints, yielding a BigInteger. */
|
|---|
| 441 | private static final BigInteger add(int x, int y)
|
|---|
| 442 | {
|
|---|
| 443 | return BigInteger.make((long) x + (long) y);
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | /** Add a BigInteger and an int, yielding a new BigInteger. */
|
|---|
| 447 | private static BigInteger add(BigInteger x, int y)
|
|---|
| 448 | {
|
|---|
| 449 | if (x.words == null)
|
|---|
| 450 | return BigInteger.add(x.ival, y);
|
|---|
| 451 | BigInteger result = new BigInteger(0);
|
|---|
| 452 | result.setAdd(x, y);
|
|---|
| 453 | return result.canonicalize();
|
|---|
| 454 | }
|
|---|
| 455 |
|
|---|
| 456 | /** Set this to the sum of x and y.
|
|---|
| 457 | * OK if x==this. */
|
|---|
| 458 | private void setAdd(BigInteger x, int y)
|
|---|
| 459 | {
|
|---|
| 460 | if (x.words == null)
|
|---|
| 461 | {
|
|---|
| 462 | set((long) x.ival + (long) y);
|
|---|
| 463 | return;
|
|---|
| 464 | }
|
|---|
| 465 | int len = x.ival;
|
|---|
| 466 | realloc(len + 1);
|
|---|
| 467 | long carry = y;
|
|---|
| 468 | for (int i = 0; i < len; i++)
|
|---|
| 469 | {
|
|---|
| 470 | carry += ((long) x.words[i] & 0xffffffffL);
|
|---|
| 471 | words[i] = (int) carry;
|
|---|
| 472 | carry >>= 32;
|
|---|
| 473 | }
|
|---|
| 474 | if (x.words[len - 1] < 0)
|
|---|
| 475 | carry--;
|
|---|
| 476 | words[len] = (int) carry;
|
|---|
| 477 | ival = wordsNeeded(words, len + 1);
|
|---|
| 478 | }
|
|---|
| 479 |
|
|---|
| 480 | /** Destructively add an int to this. */
|
|---|
| 481 | private final void setAdd(int y)
|
|---|
| 482 | {
|
|---|
| 483 | setAdd(this, y);
|
|---|
| 484 | }
|
|---|
| 485 |
|
|---|
| 486 | /** Destructively set the value of this to a long. */
|
|---|
| 487 | private final void set(long y)
|
|---|
| 488 | {
|
|---|
| 489 | int i = (int) y;
|
|---|
| 490 | if ((long) i == y)
|
|---|
| 491 | {
|
|---|
| 492 | ival = i;
|
|---|
| 493 | words = null;
|
|---|
| 494 | }
|
|---|
| 495 | else
|
|---|
| 496 | {
|
|---|
| 497 | realloc(2);
|
|---|
| 498 | words[0] = i;
|
|---|
| 499 | words[1] = (int) (y >> 32);
|
|---|
| 500 | ival = 2;
|
|---|
| 501 | }
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 | /** Destructively set the value of this to the given words.
|
|---|
| 505 | * The words array is reused, not copied. */
|
|---|
| 506 | private final void set(int[] words, int length)
|
|---|
| 507 | {
|
|---|
| 508 | this.ival = length;
|
|---|
| 509 | this.words = words;
|
|---|
| 510 | }
|
|---|
| 511 |
|
|---|
| 512 | /** Destructively set the value of this to that of y. */
|
|---|
| 513 | private final void set(BigInteger y)
|
|---|
| 514 | {
|
|---|
| 515 | if (y.words == null)
|
|---|
| 516 | set(y.ival);
|
|---|
| 517 | else if (this != y)
|
|---|
| 518 | {
|
|---|
| 519 | realloc(y.ival);
|
|---|
| 520 | System.arraycopy(y.words, 0, words, 0, y.ival);
|
|---|
| 521 | ival = y.ival;
|
|---|
| 522 | }
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 | /** Add two BigIntegers, yielding their sum as another BigInteger. */
|
|---|
| 526 | private static BigInteger add(BigInteger x, BigInteger y, int k)
|
|---|
| 527 | {
|
|---|
| 528 | if (x.words == null && y.words == null)
|
|---|
| 529 | return BigInteger.make((long) k * (long) y.ival + (long) x.ival);
|
|---|
| 530 | if (k != 1)
|
|---|
| 531 | {
|
|---|
| 532 | if (k == -1)
|
|---|
| 533 | y = BigInteger.neg(y);
|
|---|
| 534 | else
|
|---|
| 535 | y = BigInteger.times(y, BigInteger.make(k));
|
|---|
| 536 | }
|
|---|
| 537 | if (x.words == null)
|
|---|
| 538 | return BigInteger.add(y, x.ival);
|
|---|
| 539 | if (y.words == null)
|
|---|
| 540 | return BigInteger.add(x, y.ival);
|
|---|
| 541 | // Both are big
|
|---|
| 542 | int len;
|
|---|
| 543 | if (y.ival > x.ival)
|
|---|
| 544 | { // Swap so x is longer then y.
|
|---|
| 545 | BigInteger tmp = x; x = y; y = tmp;
|
|---|
| 546 | }
|
|---|
| 547 | BigInteger result = alloc(x.ival + 1);
|
|---|
| 548 | int i = y.ival;
|
|---|
| 549 | long carry = MPN.add_n(result.words, x.words, y.words, i);
|
|---|
| 550 | long y_ext = y.words[i - 1] < 0 ? 0xffffffffL : 0;
|
|---|
| 551 | for (; i < x.ival; i++)
|
|---|
| 552 | {
|
|---|
| 553 | carry += ((long) x.words[i] & 0xffffffffL) + y_ext;;
|
|---|
| 554 | result.words[i] = (int) carry;
|
|---|
| 555 | carry >>>= 32;
|
|---|
| 556 | }
|
|---|
| 557 | if (x.words[i - 1] < 0)
|
|---|
| 558 | y_ext--;
|
|---|
| 559 | result.words[i] = (int) (carry + y_ext);
|
|---|
| 560 | result.ival = i+1;
|
|---|
| 561 | return result.canonicalize();
|
|---|
| 562 | }
|
|---|
| 563 |
|
|---|
| 564 | public BigInteger add(BigInteger val)
|
|---|
| 565 | {
|
|---|
| 566 | return add(this, val, 1);
|
|---|
| 567 | }
|
|---|
| 568 |
|
|---|
| 569 | public BigInteger subtract(BigInteger val)
|
|---|
| 570 | {
|
|---|
| 571 | return add(this, val, -1);
|
|---|
| 572 | }
|
|---|
| 573 |
|
|---|
| 574 | private static final BigInteger times(BigInteger x, int y)
|
|---|
| 575 | {
|
|---|
| 576 | if (y == 0)
|
|---|
| 577 | return ZERO;
|
|---|
| 578 | if (y == 1)
|
|---|
| 579 | return x;
|
|---|
| 580 | int[] xwords = x.words;
|
|---|
| 581 | int xlen = x.ival;
|
|---|
| 582 | if (xwords == null)
|
|---|
| 583 | return BigInteger.make((long) xlen * (long) y);
|
|---|
| 584 | boolean negative;
|
|---|
| 585 | BigInteger result = BigInteger.alloc(xlen + 1);
|
|---|
| 586 | if (xwords[xlen - 1] < 0)
|
|---|
| 587 | {
|
|---|
| 588 | negative = true;
|
|---|
| 589 | negate(result.words, xwords, xlen);
|
|---|
| 590 | xwords = result.words;
|
|---|
| 591 | }
|
|---|
| 592 | else
|
|---|
| 593 | negative = false;
|
|---|
| 594 | if (y < 0)
|
|---|
| 595 | {
|
|---|
| 596 | negative = !negative;
|
|---|
| 597 | y = -y;
|
|---|
| 598 | }
|
|---|
| 599 | result.words[xlen] = MPN.mul_1(result.words, xwords, xlen, y);
|
|---|
| 600 | result.ival = xlen + 1;
|
|---|
| 601 | if (negative)
|
|---|
| 602 | result.setNegative();
|
|---|
| 603 | return result.canonicalize();
|
|---|
| 604 | }
|
|---|
| 605 |
|
|---|
| 606 | private static final BigInteger times(BigInteger x, BigInteger y)
|
|---|
| 607 | {
|
|---|
| 608 | if (y.words == null)
|
|---|
| 609 | return times(x, y.ival);
|
|---|
| 610 | if (x.words == null)
|
|---|
| 611 | return times(y, x.ival);
|
|---|
| 612 | boolean negative = false;
|
|---|
| 613 | int[] xwords;
|
|---|
| 614 | int[] ywords;
|
|---|
| 615 | int xlen = x.ival;
|
|---|
| 616 | int ylen = y.ival;
|
|---|
| 617 | if (x.isNegative())
|
|---|
| 618 | {
|
|---|
| 619 | negative = true;
|
|---|
| 620 | xwords = new int[xlen];
|
|---|
| 621 | negate(xwords, x.words, xlen);
|
|---|
| 622 | }
|
|---|
| 623 | else
|
|---|
| 624 | {
|
|---|
| 625 | negative = false;
|
|---|
| 626 | xwords = x.words;
|
|---|
| 627 | }
|
|---|
| 628 | if (y.isNegative())
|
|---|
| 629 | {
|
|---|
| 630 | negative = !negative;
|
|---|
| 631 | ywords = new int[ylen];
|
|---|
| 632 | negate(ywords, y.words, ylen);
|
|---|
| 633 | }
|
|---|
| 634 | else
|
|---|
| 635 | ywords = y.words;
|
|---|
| 636 | // Swap if x is shorter then y.
|
|---|
| 637 | if (xlen < ylen)
|
|---|
| 638 | {
|
|---|
| 639 | int[] twords = xwords; xwords = ywords; ywords = twords;
|
|---|
| 640 | int tlen = xlen; xlen = ylen; ylen = tlen;
|
|---|
| 641 | }
|
|---|
| 642 | BigInteger result = BigInteger.alloc(xlen+ylen);
|
|---|
| 643 | MPN.mul(result.words, xwords, xlen, ywords, ylen);
|
|---|
| 644 | result.ival = xlen+ylen;
|
|---|
| 645 | if (negative)
|
|---|
| 646 | result.setNegative();
|
|---|
| 647 | return result.canonicalize();
|
|---|
| 648 | }
|
|---|
| 649 |
|
|---|
| 650 | public BigInteger multiply(BigInteger y)
|
|---|
| 651 | {
|
|---|
| 652 | return times(this, y);
|
|---|
| 653 | }
|
|---|
| 654 |
|
|---|
| 655 | private static void divide(long x, long y,
|
|---|
| 656 | BigInteger quotient, BigInteger remainder,
|
|---|
| 657 | int rounding_mode)
|
|---|
| 658 | {
|
|---|
| 659 | boolean xNegative, yNegative;
|
|---|
| 660 | if (x < 0)
|
|---|
| 661 | {
|
|---|
| 662 | xNegative = true;
|
|---|
| 663 | if (x == Long.MIN_VALUE)
|
|---|
| 664 | {
|
|---|
| 665 | divide(BigInteger.make(x), BigInteger.make(y),
|
|---|
| 666 | quotient, remainder, rounding_mode);
|
|---|
| 667 | return;
|
|---|
| 668 | }
|
|---|
| 669 | x = -x;
|
|---|
| 670 | }
|
|---|
| 671 | else
|
|---|
| 672 | xNegative = false;
|
|---|
| 673 |
|
|---|
| 674 | if (y < 0)
|
|---|
| 675 | {
|
|---|
| 676 | yNegative = true;
|
|---|
| 677 | if (y == Long.MIN_VALUE)
|
|---|
| 678 | {
|
|---|
| 679 | if (rounding_mode == TRUNCATE)
|
|---|
| 680 | { // x != Long.Min_VALUE implies abs(x) < abs(y)
|
|---|
| 681 | if (quotient != null)
|
|---|
| 682 | quotient.set(0);
|
|---|
| 683 | if (remainder != null)
|
|---|
| 684 | remainder.set(x);
|
|---|
| 685 | }
|
|---|
| 686 | else
|
|---|
| 687 | divide(BigInteger.make(x), BigInteger.make(y),
|
|---|
| 688 | quotient, remainder, rounding_mode);
|
|---|
| 689 | return;
|
|---|
| 690 | }
|
|---|
| 691 | y = -y;
|
|---|
| 692 | }
|
|---|
| 693 | else
|
|---|
| 694 | yNegative = false;
|
|---|
| 695 |
|
|---|
| 696 | long q = x / y;
|
|---|
| 697 | long r = x % y;
|
|---|
| 698 | boolean qNegative = xNegative ^ yNegative;
|
|---|
| 699 |
|
|---|
| 700 | boolean add_one = false;
|
|---|
| 701 | if (r != 0)
|
|---|
| 702 | {
|
|---|
| 703 | switch (rounding_mode)
|
|---|
| 704 | {
|
|---|
| 705 | case TRUNCATE:
|
|---|
| 706 | break;
|
|---|
| 707 | case CEILING:
|
|---|
| 708 | case FLOOR:
|
|---|
| 709 | if (qNegative == (rounding_mode == FLOOR))
|
|---|
| 710 | add_one = true;
|
|---|
| 711 | break;
|
|---|
| 712 | case ROUND:
|
|---|
| 713 | add_one = r > ((y - (q & 1)) >> 1);
|
|---|
| 714 | break;
|
|---|
| 715 | }
|
|---|
| 716 | }
|
|---|
| 717 | if (quotient != null)
|
|---|
| 718 | {
|
|---|
| 719 | if (add_one)
|
|---|
| 720 | q++;
|
|---|
| 721 | if (qNegative)
|
|---|
| 722 | q = -q;
|
|---|
| 723 | quotient.set(q);
|
|---|
| 724 | }
|
|---|
| 725 | if (remainder != null)
|
|---|
| 726 | {
|
|---|
| 727 | // The remainder is by definition: X-Q*Y
|
|---|
| 728 | if (add_one)
|
|---|
| 729 | {
|
|---|
| 730 | // Subtract the remainder from Y.
|
|---|
| 731 | r = y - r;
|
|---|
| 732 | // In this case, abs(Q*Y) > abs(X).
|
|---|
| 733 | // So sign(remainder) = -sign(X).
|
|---|
| 734 | xNegative = ! xNegative;
|
|---|
| 735 | }
|
|---|
| 736 | else
|
|---|
| 737 | {
|
|---|
| 738 | // If !add_one, then: abs(Q*Y) <= abs(X).
|
|---|
| 739 | // So sign(remainder) = sign(X).
|
|---|
| 740 | }
|
|---|
| 741 | if (xNegative)
|
|---|
| 742 | r = -r;
|
|---|
| 743 | remainder.set(r);
|
|---|
| 744 | }
|
|---|
| 745 | }
|
|---|
| 746 |
|
|---|
| 747 | /** Divide two integers, yielding quotient and remainder.
|
|---|
| 748 | * @param x the numerator in the division
|
|---|
| 749 | * @param y the denominator in the division
|
|---|
| 750 | * @param quotient is set to the quotient of the result (iff quotient!=null)
|
|---|
| 751 | * @param remainder is set to the remainder of the result
|
|---|
| 752 | * (iff remainder!=null)
|
|---|
| 753 | * @param rounding_mode one of FLOOR, CEILING, TRUNCATE, or ROUND.
|
|---|
| 754 | */
|
|---|
| 755 | private static void divide(BigInteger x, BigInteger y,
|
|---|
| 756 | BigInteger quotient, BigInteger remainder,
|
|---|
| 757 | int rounding_mode)
|
|---|
| 758 | {
|
|---|
| 759 | if ((x.words == null || x.ival <= 2)
|
|---|
| 760 | && (y.words == null || y.ival <= 2))
|
|---|
| 761 | {
|
|---|
| 762 | long x_l = x.longValue();
|
|---|
| 763 | long y_l = y.longValue();
|
|---|
| 764 | if (x_l != Long.MIN_VALUE && y_l != Long.MIN_VALUE)
|
|---|
| 765 | {
|
|---|
| 766 | divide(x_l, y_l, quotient, remainder, rounding_mode);
|
|---|
| 767 | return;
|
|---|
| 768 | }
|
|---|
| 769 | }
|
|---|
| 770 |
|
|---|
| 771 | boolean xNegative = x.isNegative();
|
|---|
| 772 | boolean yNegative = y.isNegative();
|
|---|
| 773 | boolean qNegative = xNegative ^ yNegative;
|
|---|
| 774 |
|
|---|
| 775 | int ylen = y.words == null ? 1 : y.ival;
|
|---|
| 776 | int[] ywords = new int[ylen];
|
|---|
| 777 | y.getAbsolute(ywords);
|
|---|
| 778 | while (ylen > 1 && ywords[ylen - 1] == 0) ylen--;
|
|---|
| 779 |
|
|---|
| 780 | int xlen = x.words == null ? 1 : x.ival;
|
|---|
| 781 | int[] xwords = new int[xlen+2];
|
|---|
| 782 | x.getAbsolute(xwords);
|
|---|
| 783 | while (xlen > 1 && xwords[xlen-1] == 0) xlen--;
|
|---|
| 784 |
|
|---|
| 785 | int qlen, rlen;
|
|---|
| 786 |
|
|---|
| 787 | int cmpval = MPN.cmp(xwords, xlen, ywords, ylen);
|
|---|
| 788 | if (cmpval < 0) // abs(x) < abs(y)
|
|---|
| 789 | { // quotient = 0; remainder = num.
|
|---|
| 790 | int[] rwords = xwords; xwords = ywords; ywords = rwords;
|
|---|
| 791 | rlen = xlen; qlen = 1; xwords[0] = 0;
|
|---|
| 792 | }
|
|---|
| 793 | else if (cmpval == 0) // abs(x) == abs(y)
|
|---|
| 794 | {
|
|---|
| 795 | xwords[0] = 1; qlen = 1; // quotient = 1
|
|---|
| 796 | ywords[0] = 0; rlen = 1; // remainder = 0;
|
|---|
| 797 | }
|
|---|
| 798 | else if (ylen == 1)
|
|---|
| 799 | {
|
|---|
| 800 | qlen = xlen;
|
|---|
| 801 | // Need to leave room for a word of leading zeros if dividing by 1
|
|---|
| 802 | // and the dividend has the high bit set. It might be safe to
|
|---|
| 803 | // increment qlen in all cases, but it certainly is only necessary
|
|---|
| 804 | // in the following case.
|
|---|
| 805 | if (ywords[0] == 1 && xwords[xlen-1] < 0)
|
|---|
| 806 | qlen++;
|
|---|
| 807 | rlen = 1;
|
|---|
| 808 | ywords[0] = MPN.divmod_1(xwords, xwords, xlen, ywords[0]);
|
|---|
| 809 | }
|
|---|
| 810 | else // abs(x) > abs(y)
|
|---|
| 811 | {
|
|---|
| 812 | // Normalize the denominator, i.e. make its most significant bit set by
|
|---|
| 813 | // shifting it normalization_steps bits to the left. Also shift the
|
|---|
| 814 | // numerator the same number of steps (to keep the quotient the same!).
|
|---|
| 815 |
|
|---|
| 816 | int nshift = MPN.count_leading_zeros(ywords[ylen - 1]);
|
|---|
| 817 | if (nshift != 0)
|
|---|
| 818 | {
|
|---|
| 819 | // Shift up the denominator setting the most significant bit of
|
|---|
| 820 | // the most significant word.
|
|---|
| 821 | MPN.lshift(ywords, 0, ywords, ylen, nshift);
|
|---|
| 822 |
|
|---|
| 823 | // Shift up the numerator, possibly introducing a new most
|
|---|
| 824 | // significant word.
|
|---|
| 825 | int x_high = MPN.lshift(xwords, 0, xwords, xlen, nshift);
|
|---|
| 826 | xwords[xlen++] = x_high;
|
|---|
| 827 | }
|
|---|
| 828 |
|
|---|
| 829 | if (xlen == ylen)
|
|---|
| 830 | xwords[xlen++] = 0;
|
|---|
| 831 | MPN.divide(xwords, xlen, ywords, ylen);
|
|---|
| 832 | rlen = ylen;
|
|---|
| 833 | MPN.rshift0 (ywords, xwords, 0, rlen, nshift);
|
|---|
| 834 |
|
|---|
| 835 | qlen = xlen + 1 - ylen;
|
|---|
| 836 | if (quotient != null)
|
|---|
| 837 | {
|
|---|
| 838 | for (int i = 0; i < qlen; i++)
|
|---|
| 839 | xwords[i] = xwords[i+ylen];
|
|---|
| 840 | }
|
|---|
| 841 | }
|
|---|
| 842 |
|
|---|
| 843 | if (ywords[rlen-1] < 0)
|
|---|
| 844 | {
|
|---|
| 845 | ywords[rlen] = 0;
|
|---|
| 846 | rlen++;
|
|---|
| 847 | }
|
|---|
| 848 |
|
|---|
| 849 | // Now the quotient is in xwords, and the remainder is in ywords.
|
|---|
| 850 |
|
|---|
| 851 | boolean add_one = false;
|
|---|
| 852 | if (rlen > 1 || ywords[0] != 0)
|
|---|
| 853 | { // Non-zero remainder i.e. in-exact quotient.
|
|---|
| 854 | switch (rounding_mode)
|
|---|
| 855 | {
|
|---|
| 856 | case TRUNCATE:
|
|---|
| 857 | break;
|
|---|
| 858 | case CEILING:
|
|---|
| 859 | case FLOOR:
|
|---|
| 860 | if (qNegative == (rounding_mode == FLOOR))
|
|---|
| 861 | add_one = true;
|
|---|
| 862 | break;
|
|---|
| 863 | case ROUND:
|
|---|
| 864 | // int cmp = compareTo(remainder<<1, abs(y));
|
|---|
| 865 | BigInteger tmp = remainder == null ? new BigInteger() : remainder;
|
|---|
| 866 | tmp.set(ywords, rlen);
|
|---|
| 867 | tmp = shift(tmp, 1);
|
|---|
| 868 | if (yNegative)
|
|---|
| 869 | tmp.setNegative();
|
|---|
| 870 | int cmp = compareTo(tmp, y);
|
|---|
| 871 | // Now cmp == compareTo(sign(y)*(remainder<<1), y)
|
|---|
| 872 | if (yNegative)
|
|---|
| 873 | cmp = -cmp;
|
|---|
| 874 | add_one = (cmp == 1) || (cmp == 0 && (xwords[0]&1) != 0);
|
|---|
| 875 | }
|
|---|
| 876 | }
|
|---|
| 877 | if (quotient != null)
|
|---|
| 878 | {
|
|---|
| 879 | quotient.set(xwords, qlen);
|
|---|
| 880 | if (qNegative)
|
|---|
| 881 | {
|
|---|
| 882 | if (add_one) // -(quotient + 1) == ~(quotient)
|
|---|
| 883 | quotient.setInvert();
|
|---|
| 884 | else
|
|---|
| 885 | quotient.setNegative();
|
|---|
| 886 | }
|
|---|
| 887 | else if (add_one)
|
|---|
| 888 | quotient.setAdd(1);
|
|---|
| 889 | }
|
|---|
| 890 | if (remainder != null)
|
|---|
| 891 | {
|
|---|
| 892 | // The remainder is by definition: X-Q*Y
|
|---|
| 893 | remainder.set(ywords, rlen);
|
|---|
| 894 | if (add_one)
|
|---|
| 895 | {
|
|---|
| 896 | // Subtract the remainder from Y:
|
|---|
| 897 | // abs(R) = abs(Y) - abs(orig_rem) = -(abs(orig_rem) - abs(Y)).
|
|---|
| 898 | BigInteger tmp;
|
|---|
| 899 | if (y.words == null)
|
|---|
| 900 | {
|
|---|
| 901 | tmp = remainder;
|
|---|
| 902 | tmp.set(yNegative ? ywords[0] + y.ival : ywords[0] - y.ival);
|
|---|
| 903 | }
|
|---|
| 904 | else
|
|---|
| 905 | tmp = BigInteger.add(remainder, y, yNegative ? 1 : -1);
|
|---|
| 906 | // Now tmp <= 0.
|
|---|
| 907 | // In this case, abs(Q) = 1 + floor(abs(X)/abs(Y)).
|
|---|
| 908 | // Hence, abs(Q*Y) > abs(X).
|
|---|
| 909 | // So sign(remainder) = -sign(X).
|
|---|
| 910 | if (xNegative)
|
|---|
| 911 | remainder.setNegative(tmp);
|
|---|
| 912 | else
|
|---|
| 913 | remainder.set(tmp);
|
|---|
| 914 | }
|
|---|
| 915 | else
|
|---|
| 916 | {
|
|---|
| 917 | // If !add_one, then: abs(Q*Y) <= abs(X).
|
|---|
| 918 | // So sign(remainder) = sign(X).
|
|---|
| 919 | if (xNegative)
|
|---|
| 920 | remainder.setNegative();
|
|---|
| 921 | }
|
|---|
| 922 | }
|
|---|
| 923 | }
|
|---|
| 924 |
|
|---|
| 925 | public BigInteger divide(BigInteger val)
|
|---|
| 926 | {
|
|---|
| 927 | if (val.isZero())
|
|---|
| 928 | throw new ArithmeticException("divisor is zero");
|
|---|
| 929 |
|
|---|
| 930 | BigInteger quot = new BigInteger();
|
|---|
| 931 | divide(this, val, quot, null, TRUNCATE);
|
|---|
| 932 | return quot.canonicalize();
|
|---|
| 933 | }
|
|---|
| 934 |
|
|---|
| 935 | public BigInteger remainder(BigInteger val)
|
|---|
| 936 | {
|
|---|
| 937 | if (val.isZero())
|
|---|
| 938 | throw new ArithmeticException("divisor is zero");
|
|---|
| 939 |
|
|---|
| 940 | BigInteger rem = new BigInteger();
|
|---|
| 941 | divide(this, val, null, rem, TRUNCATE);
|
|---|
| 942 | return rem.canonicalize();
|
|---|
| 943 | }
|
|---|
| 944 |
|
|---|
| 945 | public BigInteger[] divideAndRemainder(BigInteger val)
|
|---|
| 946 | {
|
|---|
| 947 | if (val.isZero())
|
|---|
| 948 | throw new ArithmeticException("divisor is zero");
|
|---|
| 949 |
|
|---|
| 950 | BigInteger[] result = new BigInteger[2];
|
|---|
| 951 | result[0] = new BigInteger();
|
|---|
| 952 | result[1] = new BigInteger();
|
|---|
| 953 | divide(this, val, result[0], result[1], TRUNCATE);
|
|---|
| 954 | result[0].canonicalize();
|
|---|
| 955 | result[1].canonicalize();
|
|---|
| 956 | return result;
|
|---|
| 957 | }
|
|---|
| 958 |
|
|---|
| 959 | public BigInteger mod(BigInteger m)
|
|---|
| 960 | {
|
|---|
| 961 | if (m.isNegative() || m.isZero())
|
|---|
| 962 | throw new ArithmeticException("non-positive modulus");
|
|---|
| 963 |
|
|---|
| 964 | BigInteger rem = new BigInteger();
|
|---|
| 965 | divide(this, m, null, rem, FLOOR);
|
|---|
| 966 | return rem.canonicalize();
|
|---|
| 967 | }
|
|---|
| 968 |
|
|---|
| 969 | /** Calculate power for BigInteger exponents.
|
|---|
| 970 | * @param y exponent assumed to be non-negative. */
|
|---|
| 971 | private BigInteger pow(BigInteger y)
|
|---|
| 972 | {
|
|---|
| 973 | if (isOne())
|
|---|
| 974 | return this;
|
|---|
| 975 | if (isMinusOne())
|
|---|
| 976 | return y.isOdd () ? this : ONE;
|
|---|
| 977 | if (y.words == null && y.ival >= 0)
|
|---|
| 978 | return pow(y.ival);
|
|---|
| 979 |
|
|---|
| 980 | // Assume exponent is non-negative.
|
|---|
| 981 | if (isZero())
|
|---|
| 982 | return this;
|
|---|
| 983 |
|
|---|
| 984 | // Implemented by repeated squaring and multiplication.
|
|---|
| 985 | BigInteger pow2 = this;
|
|---|
| 986 | BigInteger r = null;
|
|---|
| 987 | for (;;) // for (i = 0; ; i++)
|
|---|
| 988 | {
|
|---|
| 989 | // pow2 == x**(2**i)
|
|---|
| 990 | // prod = x**(sum(j=0..i-1, (y>>j)&1))
|
|---|
| 991 | if (y.isOdd())
|
|---|
| 992 | r = r == null ? pow2 : times(r, pow2); // r *= pow2
|
|---|
| 993 | y = BigInteger.shift(y, -1);
|
|---|
| 994 | if (y.isZero())
|
|---|
| 995 | break;
|
|---|
| 996 | // pow2 *= pow2;
|
|---|
| 997 | pow2 = times(pow2, pow2);
|
|---|
| 998 | }
|
|---|
| 999 | return r == null ? ONE : r;
|
|---|
| 1000 | }
|
|---|
| 1001 |
|
|---|
| 1002 | /** Calculate the integral power of a BigInteger.
|
|---|
| 1003 | * @param exponent the exponent (must be non-negative)
|
|---|
| 1004 | */
|
|---|
| 1005 | public BigInteger pow(int exponent)
|
|---|
| 1006 | {
|
|---|
| 1007 | if (exponent <= 0)
|
|---|
| 1008 | {
|
|---|
| 1009 | if (exponent == 0)
|
|---|
| 1010 | return ONE;
|
|---|
| 1011 | else
|
|---|
| 1012 | throw new ArithmeticException("negative exponent");
|
|---|
| 1013 | }
|
|---|
| 1014 | if (isZero())
|
|---|
| 1015 | return this;
|
|---|
| 1016 | int plen = words == null ? 1 : ival; // Length of pow2.
|
|---|
| 1017 | int blen = ((bitLength() * exponent) >> 5) + 2 * plen;
|
|---|
| 1018 | boolean negative = isNegative() && (exponent & 1) != 0;
|
|---|
| 1019 | int[] pow2 = new int [blen];
|
|---|
| 1020 | int[] rwords = new int [blen];
|
|---|
| 1021 | int[] work = new int [blen];
|
|---|
| 1022 | getAbsolute(pow2); // pow2 = abs(this);
|
|---|
| 1023 | int rlen = 1;
|
|---|
| 1024 | rwords[0] = 1; // rwords = 1;
|
|---|
| 1025 | for (;;) // for (i = 0; ; i++)
|
|---|
| 1026 | {
|
|---|
| 1027 | // pow2 == this**(2**i)
|
|---|
| 1028 | // prod = this**(sum(j=0..i-1, (exponent>>j)&1))
|
|---|
| 1029 | if ((exponent & 1) != 0)
|
|---|
| 1030 | { // r *= pow2
|
|---|
| 1031 | MPN.mul(work, pow2, plen, rwords, rlen);
|
|---|
| 1032 | int[] temp = work; work = rwords; rwords = temp;
|
|---|
| 1033 | rlen += plen;
|
|---|
| 1034 | while (rwords[rlen - 1] == 0) rlen--;
|
|---|
| 1035 | }
|
|---|
| 1036 | exponent >>= 1;
|
|---|
| 1037 | if (exponent == 0)
|
|---|
| 1038 | break;
|
|---|
| 1039 | // pow2 *= pow2;
|
|---|
| 1040 | MPN.mul(work, pow2, plen, pow2, plen);
|
|---|
| 1041 | int[] temp = work; work = pow2; pow2 = temp; // swap to avoid a copy
|
|---|
| 1042 | plen *= 2;
|
|---|
| 1043 | while (pow2[plen - 1] == 0) plen--;
|
|---|
| 1044 | }
|
|---|
| 1045 | if (rwords[rlen - 1] < 0)
|
|---|
| 1046 | rlen++;
|
|---|
| 1047 | if (negative)
|
|---|
| 1048 | negate(rwords, rwords, rlen);
|
|---|
| 1049 | return BigInteger.make(rwords, rlen);
|
|---|
| 1050 | }
|
|---|
| 1051 |
|
|---|
| 1052 | private static final int[] euclidInv(int a, int b, int prevDiv)
|
|---|
| 1053 | {
|
|---|
| 1054 | // Storage for return values, plus one slot for a temp int (see below).
|
|---|
| 1055 | int[] xy;
|
|---|
| 1056 |
|
|---|
| 1057 | if (b == 0)
|
|---|
| 1058 | throw new ArithmeticException("not invertible");
|
|---|
| 1059 | else if (b == 1)
|
|---|
| 1060 | {
|
|---|
| 1061 | // Success: values are indeed invertible!
|
|---|
| 1062 | // Bottom of the recursion reached; start unwinding.
|
|---|
| 1063 | xy = new int[3];
|
|---|
| 1064 | xy[0] = -prevDiv;
|
|---|
| 1065 | xy[1] = 1;
|
|---|
| 1066 | return xy;
|
|---|
| 1067 | }
|
|---|
| 1068 |
|
|---|
| 1069 | xy = euclidInv(b, a % b, a / b); // Recursion happens here.
|
|---|
| 1070 |
|
|---|
| 1071 | // xy[2] is just temp storage for intermediate results in the following
|
|---|
| 1072 | // calculation. This saves us a bit of space over having an int
|
|---|
| 1073 | // allocated at every level of this recursive method.
|
|---|
| 1074 | xy[2] = xy[0];
|
|---|
| 1075 | xy[0] = xy[2] * -prevDiv + xy[1];
|
|---|
| 1076 | xy[1] = xy[2];
|
|---|
| 1077 | return xy;
|
|---|
| 1078 | }
|
|---|
| 1079 |
|
|---|
| 1080 | private static final BigInteger[]
|
|---|
| 1081 | euclidInv(BigInteger a, BigInteger b, BigInteger prevDiv)
|
|---|
| 1082 | {
|
|---|
| 1083 | // FIXME: This method could be more efficient memory-wise and should be
|
|---|
| 1084 | // modified as such since it is recursive.
|
|---|
| 1085 |
|
|---|
| 1086 | // Storage for return values, plus one slot for a temp int (see below).
|
|---|
| 1087 | BigInteger[] xy;
|
|---|
| 1088 |
|
|---|
| 1089 | if (b.isZero())
|
|---|
| 1090 | throw new ArithmeticException("not invertible");
|
|---|
| 1091 | else if (b.isOne())
|
|---|
| 1092 | {
|
|---|
| 1093 | // Success: values are indeed invertible!
|
|---|
| 1094 | // Bottom of the recursion reached; start unwinding.
|
|---|
| 1095 | xy = new BigInteger[3];
|
|---|
| 1096 | xy[0] = neg(prevDiv);
|
|---|
| 1097 | xy[1] = ONE;
|
|---|
| 1098 | return xy;
|
|---|
| 1099 | }
|
|---|
| 1100 |
|
|---|
| 1101 | // Recursion happens in the following conditional!
|
|---|
| 1102 |
|
|---|
| 1103 | // If a just contains an int, then use integer math for the rest.
|
|---|
| 1104 | if (a.words == null)
|
|---|
| 1105 | {
|
|---|
| 1106 | int[] xyInt = euclidInv(b.ival, a.ival % b.ival, a.ival / b.ival);
|
|---|
| 1107 | xy = new BigInteger[3];
|
|---|
| 1108 | xy[0] = new BigInteger(xyInt[0]);
|
|---|
| 1109 | xy[1] = new BigInteger(xyInt[1]);
|
|---|
| 1110 | }
|
|---|
| 1111 | else
|
|---|
| 1112 | {
|
|---|
| 1113 | BigInteger rem = new BigInteger();
|
|---|
| 1114 | BigInteger quot = new BigInteger();
|
|---|
| 1115 | divide(a, b, quot, rem, FLOOR);
|
|---|
| 1116 | xy = euclidInv(b, rem, quot);
|
|---|
| 1117 | }
|
|---|
| 1118 |
|
|---|
| 1119 | // xy[2] is just temp storage for intermediate results in the following
|
|---|
| 1120 | // calculation. This saves us a bit of space over having a BigInteger
|
|---|
| 1121 | // allocated at every level of this recursive method.
|
|---|
| 1122 | xy[2] = xy[0];
|
|---|
| 1123 | xy[0] = add(xy[1], times(xy[2], prevDiv), -1);
|
|---|
| 1124 | xy[1] = xy[2];
|
|---|
| 1125 | return xy;
|
|---|
| 1126 | }
|
|---|
| 1127 |
|
|---|
| 1128 | public BigInteger modInverse(BigInteger y)
|
|---|
| 1129 | {
|
|---|
| 1130 | if (y.isNegative() || y.isZero())
|
|---|
| 1131 | throw new ArithmeticException("non-positive modulo");
|
|---|
| 1132 |
|
|---|
| 1133 | // Degenerate cases.
|
|---|
| 1134 | if (y.isOne())
|
|---|
| 1135 | return ZERO;
|
|---|
| 1136 | else if (isOne())
|
|---|
| 1137 | return ONE;
|
|---|
| 1138 |
|
|---|
| 1139 | // Use Euclid's algorithm as in gcd() but do this recursively
|
|---|
| 1140 | // rather than in a loop so we can use the intermediate results as we
|
|---|
| 1141 | // unwind from the recursion.
|
|---|
| 1142 | // Used http://www.math.nmsu.edu/~crypto/EuclideanAlgo.html as reference.
|
|---|
| 1143 | BigInteger result = new BigInteger();
|
|---|
| 1144 | int xval = ival;
|
|---|
| 1145 | int yval = y.ival;
|
|---|
| 1146 | boolean swapped = false;
|
|---|
| 1147 |
|
|---|
| 1148 | if (y.words == null)
|
|---|
| 1149 | {
|
|---|
| 1150 | // The result is guaranteed to be less than the modulus, y (which is
|
|---|
| 1151 | // an int), so simplify this by working with the int result of this
|
|---|
| 1152 | // modulo y. Also, if this is negative, make it positive via modulo
|
|---|
| 1153 | // math. Note that BigInteger.mod() must be used even if this is
|
|---|
| 1154 | // already an int as the % operator would provide a negative result if
|
|---|
| 1155 | // this is negative, BigInteger.mod() never returns negative values.
|
|---|
| 1156 | if (words != null || isNegative())
|
|---|
| 1157 | xval = mod(y).ival;
|
|---|
| 1158 |
|
|---|
| 1159 | // Swap values so x > y.
|
|---|
| 1160 | if (yval > xval)
|
|---|
| 1161 | {
|
|---|
| 1162 | int tmp = xval; xval = yval; yval = tmp;
|
|---|
| 1163 | swapped = true;
|
|---|
| 1164 | }
|
|---|
| 1165 | // Normally, the result is in the 2nd element of the array, but
|
|---|
| 1166 | // if originally x < y, then x and y were swapped and the result
|
|---|
| 1167 | // is in the 1st element of the array.
|
|---|
| 1168 | result.ival =
|
|---|
| 1169 | euclidInv(yval, xval % yval, xval / yval)[swapped ? 0 : 1];
|
|---|
| 1170 |
|
|---|
| 1171 | // Result can't be negative, so make it positive by adding the
|
|---|
| 1172 | // original modulus, y.ival (not the possibly "swapped" yval).
|
|---|
| 1173 | if (result.ival < 0)
|
|---|
| 1174 | result.ival += y.ival;
|
|---|
| 1175 | }
|
|---|
| 1176 | else
|
|---|
| 1177 | {
|
|---|
| 1178 | BigInteger x = this;
|
|---|
| 1179 |
|
|---|
| 1180 | // As above, force this to be a positive value via modulo math.
|
|---|
| 1181 | if (isNegative())
|
|---|
| 1182 | x = mod(y);
|
|---|
| 1183 |
|
|---|
| 1184 | // Swap values so x > y.
|
|---|
| 1185 | if (x.compareTo(y) < 0)
|
|---|
| 1186 | {
|
|---|
| 1187 | BigInteger tmp = x; x = y; y = tmp;
|
|---|
| 1188 | swapped = true;
|
|---|
| 1189 | }
|
|---|
| 1190 | // As above (for ints), result will be in the 2nd element unless
|
|---|
| 1191 | // the original x and y were swapped.
|
|---|
| 1192 | BigInteger rem = new BigInteger();
|
|---|
| 1193 | BigInteger quot = new BigInteger();
|
|---|
| 1194 | divide(x, y, quot, rem, FLOOR);
|
|---|
| 1195 | result = euclidInv(y, rem, quot)[swapped ? 0 : 1];
|
|---|
| 1196 |
|
|---|
| 1197 | // Result can't be negative, so make it positive by adding the
|
|---|
| 1198 | // original modulus, y (which is now x if they were swapped).
|
|---|
| 1199 | if (result.isNegative())
|
|---|
| 1200 | result = add(result, swapped ? x : y, 1);
|
|---|
| 1201 | }
|
|---|
| 1202 |
|
|---|
| 1203 | return result;
|
|---|
| 1204 | }
|
|---|
| 1205 |
|
|---|
| 1206 | public BigInteger modPow(BigInteger exponent, BigInteger m)
|
|---|
| 1207 | {
|
|---|
| 1208 | if (m.isNegative() || m.isZero())
|
|---|
| 1209 | throw new ArithmeticException("non-positive modulo");
|
|---|
| 1210 |
|
|---|
| 1211 | if (exponent.isNegative())
|
|---|
| 1212 | return modInverse(m);
|
|---|
| 1213 | if (exponent.isOne())
|
|---|
| 1214 | return mod(m);
|
|---|
| 1215 |
|
|---|
| 1216 | // To do this naively by first raising this to the power of exponent
|
|---|
| 1217 | // and then performing modulo m would be extremely expensive, especially
|
|---|
| 1218 | // for very large numbers. The solution is found in Number Theory
|
|---|
| 1219 | // where a combination of partial powers and modulos can be done easily.
|
|---|
| 1220 | //
|
|---|
| 1221 | // We'll use the algorithm for Additive Chaining which can be found on
|
|---|
| 1222 | // p. 244 of "Applied Cryptography, Second Edition" by Bruce Schneier.
|
|---|
| 1223 | BigInteger s, t, u;
|
|---|
| 1224 | int i;
|
|---|
| 1225 |
|
|---|
| 1226 | s = ONE;
|
|---|
| 1227 | t = this;
|
|---|
| 1228 | u = exponent;
|
|---|
| 1229 |
|
|---|
| 1230 | while (!u.isZero())
|
|---|
| 1231 | {
|
|---|
| 1232 | if (u.and(ONE).isOne())
|
|---|
| 1233 | s = times(s, t).mod(m);
|
|---|
| 1234 | u = u.shiftRight(1);
|
|---|
| 1235 | t = times(t, t).mod(m);
|
|---|
| 1236 | }
|
|---|
| 1237 |
|
|---|
| 1238 | return s;
|
|---|
| 1239 | }
|
|---|
| 1240 |
|
|---|
| 1241 | /** Calculate Greatest Common Divisor for non-negative ints. */
|
|---|
| 1242 | private static final int gcd(int a, int b)
|
|---|
| 1243 | {
|
|---|
| 1244 | // Euclid's algorithm, copied from libg++.
|
|---|
| 1245 | if (b > a)
|
|---|
| 1246 | {
|
|---|
| 1247 | int tmp = a; a = b; b = tmp;
|
|---|
| 1248 | }
|
|---|
| 1249 | for(;;)
|
|---|
| 1250 | {
|
|---|
| 1251 | if (b == 0)
|
|---|
| 1252 | return a;
|
|---|
| 1253 | else if (b == 1)
|
|---|
| 1254 | return b;
|
|---|
| 1255 | else
|
|---|
| 1256 | {
|
|---|
| 1257 | int tmp = b;
|
|---|
| 1258 | b = a % b;
|
|---|
| 1259 | a = tmp;
|
|---|
| 1260 | }
|
|---|
| 1261 | }
|
|---|
| 1262 | }
|
|---|
| 1263 |
|
|---|
| 1264 | public BigInteger gcd(BigInteger y)
|
|---|
| 1265 | {
|
|---|
| 1266 | int xval = ival;
|
|---|
| 1267 | int yval = y.ival;
|
|---|
| 1268 | if (words == null)
|
|---|
| 1269 | {
|
|---|
| 1270 | if (xval == 0)
|
|---|
| 1271 | return BigInteger.abs(y);
|
|---|
| 1272 | if (y.words == null
|
|---|
| 1273 | && xval != Integer.MIN_VALUE && yval != Integer.MIN_VALUE)
|
|---|
| 1274 | {
|
|---|
| 1275 | if (xval < 0)
|
|---|
| 1276 | xval = -xval;
|
|---|
| 1277 | if (yval < 0)
|
|---|
| 1278 | yval = -yval;
|
|---|
| 1279 | return BigInteger.make(BigInteger.gcd(xval, yval));
|
|---|
| 1280 | }
|
|---|
| 1281 | xval = 1;
|
|---|
| 1282 | }
|
|---|
| 1283 | if (y.words == null)
|
|---|
| 1284 | {
|
|---|
| 1285 | if (yval == 0)
|
|---|
| 1286 | return BigInteger.abs(this);
|
|---|
| 1287 | yval = 1;
|
|---|
| 1288 | }
|
|---|
| 1289 | int len = (xval > yval ? xval : yval) + 1;
|
|---|
| 1290 | int[] xwords = new int[len];
|
|---|
| 1291 | int[] ywords = new int[len];
|
|---|
| 1292 | getAbsolute(xwords);
|
|---|
| 1293 | y.getAbsolute(ywords);
|
|---|
| 1294 | len = MPN.gcd(xwords, ywords, len);
|
|---|
| 1295 | BigInteger result = new BigInteger(0);
|
|---|
| 1296 | result.ival = len;
|
|---|
| 1297 | result.words = xwords;
|
|---|
| 1298 | return result.canonicalize();
|
|---|
| 1299 | }
|
|---|
| 1300 |
|
|---|
| 1301 | public boolean isProbablePrime(int certainty)
|
|---|
| 1302 | {
|
|---|
| 1303 | /** We'll use the Rabin-Miller algorithm for doing a probabilistic
|
|---|
| 1304 | * primality test. It is fast, easy and has faster decreasing odds of a
|
|---|
| 1305 | * composite passing than with other tests. This means that this
|
|---|
| 1306 | * method will actually have a probability much greater than the
|
|---|
| 1307 | * 1 - .5^certainty specified in the JCL (p. 117), but I don't think
|
|---|
| 1308 | * anyone will complain about better performance with greater certainty.
|
|---|
| 1309 | *
|
|---|
| 1310 | * The Rabin-Miller algorithm can be found on pp. 259-261 of "Applied
|
|---|
| 1311 | * Cryptography, Second Edition" by Bruce Schneier.
|
|---|
| 1312 | */
|
|---|
| 1313 |
|
|---|
| 1314 | // First rule out small prime factors and assure the number is odd.
|
|---|
| 1315 | for (int i = 0; i < primes.length; i++)
|
|---|
| 1316 | {
|
|---|
| 1317 | if (words == null && ival == primes[i])
|
|---|
| 1318 | return true;
|
|---|
| 1319 | if (remainder(make(primes[i])).isZero())
|
|---|
| 1320 | return false;
|
|---|
| 1321 | }
|
|---|
| 1322 |
|
|---|
| 1323 | // Now perform the Rabin-Miller test.
|
|---|
| 1324 | // NB: I know that this can be simplified programatically, but
|
|---|
| 1325 | // I have tried to keep it as close as possible to the algorithm
|
|---|
| 1326 | // as written in the Schneier book for reference purposes.
|
|---|
| 1327 |
|
|---|
| 1328 | // Set b to the number of times 2 evenly divides (this - 1).
|
|---|
| 1329 | // I.e. 2^b is the largest power of 2 that divides (this - 1).
|
|---|
| 1330 | BigInteger pMinus1 = add(this, -1);
|
|---|
| 1331 | int b = pMinus1.getLowestSetBit();
|
|---|
| 1332 |
|
|---|
| 1333 | // Set m such that this = 1 + 2^b * m.
|
|---|
| 1334 | BigInteger m = pMinus1.divide(make(2L << b - 1));
|
|---|
| 1335 |
|
|---|
| 1336 | Random rand = new Random();
|
|---|
| 1337 | while (certainty-- > 0)
|
|---|
| 1338 | {
|
|---|
| 1339 | // Pick a random number greater than 1 and less than this.
|
|---|
| 1340 | // The algorithm says to pick a small number to make the calculations
|
|---|
| 1341 | // go faster, but it doesn't say how small; we'll use 2 to 1024.
|
|---|
| 1342 | int a = rand.nextInt();
|
|---|
| 1343 | a = (a < 0 ? -a : a) % 1023 + 2;
|
|---|
| 1344 |
|
|---|
| 1345 | BigInteger z = make(a).modPow(m, this);
|
|---|
| 1346 | if (z.isOne() || z.equals(pMinus1))
|
|---|
| 1347 | continue; // Passes the test; may be prime.
|
|---|
| 1348 |
|
|---|
| 1349 | int i;
|
|---|
| 1350 | for (i = 0; i < b; )
|
|---|
| 1351 | {
|
|---|
| 1352 | if (z.isOne())
|
|---|
| 1353 | return false;
|
|---|
| 1354 | i++;
|
|---|
| 1355 | if (z.equals(pMinus1))
|
|---|
| 1356 | break; // Passes the test; may be prime.
|
|---|
| 1357 |
|
|---|
| 1358 | z = z.modPow(make(2), this);
|
|---|
| 1359 | }
|
|---|
| 1360 |
|
|---|
| 1361 | if (i == b && !z.equals(pMinus1))
|
|---|
| 1362 | return false;
|
|---|
| 1363 | }
|
|---|
| 1364 | return true;
|
|---|
| 1365 | }
|
|---|
| 1366 |
|
|---|
| 1367 | private void setInvert()
|
|---|
| 1368 | {
|
|---|
| 1369 | if (words == null)
|
|---|
| 1370 | ival = ~ival;
|
|---|
| 1371 | else
|
|---|
| 1372 | {
|
|---|
| 1373 | for (int i = ival; --i >= 0; )
|
|---|
| 1374 | words[i] = ~words[i];
|
|---|
| 1375 | }
|
|---|
| 1376 | }
|
|---|
| 1377 |
|
|---|
| 1378 | private void setShiftLeft(BigInteger x, int count)
|
|---|
| 1379 | {
|
|---|
| 1380 | int[] xwords;
|
|---|
| 1381 | int xlen;
|
|---|
| 1382 | if (x.words == null)
|
|---|
| 1383 | {
|
|---|
| 1384 | if (count < 32)
|
|---|
| 1385 | {
|
|---|
| 1386 | set((long) x.ival << count);
|
|---|
| 1387 | return;
|
|---|
| 1388 | }
|
|---|
| 1389 | xwords = new int[1];
|
|---|
| 1390 | xwords[0] = x.ival;
|
|---|
| 1391 | xlen = 1;
|
|---|
| 1392 | }
|
|---|
| 1393 | else
|
|---|
| 1394 | {
|
|---|
| 1395 | xwords = x.words;
|
|---|
| 1396 | xlen = x.ival;
|
|---|
| 1397 | }
|
|---|
| 1398 | int word_count = count >> 5;
|
|---|
| 1399 | count &= 31;
|
|---|
| 1400 | int new_len = xlen + word_count;
|
|---|
| 1401 | if (count == 0)
|
|---|
| 1402 | {
|
|---|
| 1403 | realloc(new_len);
|
|---|
| 1404 | for (int i = xlen; --i >= 0; )
|
|---|
| 1405 | words[i+word_count] = xwords[i];
|
|---|
| 1406 | }
|
|---|
| 1407 | else
|
|---|
| 1408 | {
|
|---|
| 1409 | new_len++;
|
|---|
| 1410 | realloc(new_len);
|
|---|
| 1411 | int shift_out = MPN.lshift(words, word_count, xwords, xlen, count);
|
|---|
| 1412 | count = 32 - count;
|
|---|
| 1413 | words[new_len-1] = (shift_out << count) >> count; // sign-extend.
|
|---|
| 1414 | }
|
|---|
| 1415 | ival = new_len;
|
|---|
| 1416 | for (int i = word_count; --i >= 0; )
|
|---|
| 1417 | words[i] = 0;
|
|---|
| 1418 | }
|
|---|
| 1419 |
|
|---|
| 1420 | private void setShiftRight(BigInteger x, int count)
|
|---|
| 1421 | {
|
|---|
| 1422 | if (x.words == null)
|
|---|
| 1423 | set(count < 32 ? x.ival >> count : x.ival < 0 ? -1 : 0);
|
|---|
| 1424 | else if (count == 0)
|
|---|
| 1425 | set(x);
|
|---|
| 1426 | else
|
|---|
| 1427 | {
|
|---|
| 1428 | boolean neg = x.isNegative();
|
|---|
| 1429 | int word_count = count >> 5;
|
|---|
| 1430 | count &= 31;
|
|---|
| 1431 | int d_len = x.ival - word_count;
|
|---|
| 1432 | if (d_len <= 0)
|
|---|
| 1433 | set(neg ? -1 : 0);
|
|---|
| 1434 | else
|
|---|
| 1435 | {
|
|---|
| 1436 | if (words == null || words.length < d_len)
|
|---|
| 1437 | realloc(d_len);
|
|---|
| 1438 | MPN.rshift0 (words, x.words, word_count, d_len, count);
|
|---|
| 1439 | ival = d_len;
|
|---|
| 1440 | if (neg)
|
|---|
| 1441 | words[d_len-1] |= -2 << (31 - count);
|
|---|
| 1442 | }
|
|---|
| 1443 | }
|
|---|
| 1444 | }
|
|---|
| 1445 |
|
|---|
| 1446 | private void setShift(BigInteger x, int count)
|
|---|
| 1447 | {
|
|---|
| 1448 | if (count > 0)
|
|---|
| 1449 | setShiftLeft(x, count);
|
|---|
| 1450 | else
|
|---|
| 1451 | setShiftRight(x, -count);
|
|---|
| 1452 | }
|
|---|
| 1453 |
|
|---|
| 1454 | private static BigInteger shift(BigInteger x, int count)
|
|---|
| 1455 | {
|
|---|
| 1456 | if (x.words == null)
|
|---|
| 1457 | {
|
|---|
| 1458 | if (count <= 0)
|
|---|
| 1459 | return make(count > -32 ? x.ival >> (-count) : x.ival < 0 ? -1 : 0);
|
|---|
| 1460 | if (count < 32)
|
|---|
| 1461 | return make((long) x.ival << count);
|
|---|
| 1462 | }
|
|---|
| 1463 | if (count == 0)
|
|---|
| 1464 | return x;
|
|---|
| 1465 | BigInteger result = new BigInteger(0);
|
|---|
| 1466 | result.setShift(x, count);
|
|---|
| 1467 | return result.canonicalize();
|
|---|
| 1468 | }
|
|---|
| 1469 |
|
|---|
| 1470 | public BigInteger shiftLeft(int n)
|
|---|
| 1471 | {
|
|---|
| 1472 | return shift(this, n);
|
|---|
| 1473 | }
|
|---|
| 1474 |
|
|---|
| 1475 | public BigInteger shiftRight(int n)
|
|---|
| 1476 | {
|
|---|
| 1477 | return shift(this, -n);
|
|---|
| 1478 | }
|
|---|
| 1479 |
|
|---|
| 1480 | private void format(int radix, StringBuffer buffer)
|
|---|
| 1481 | {
|
|---|
| 1482 | if (words == null)
|
|---|
| 1483 | buffer.append(Integer.toString(ival, radix));
|
|---|
| 1484 | else if (ival <= 2)
|
|---|
| 1485 | buffer.append(Long.toString(longValue(), radix));
|
|---|
| 1486 | else
|
|---|
| 1487 | {
|
|---|
| 1488 | boolean neg = isNegative();
|
|---|
| 1489 | int[] work;
|
|---|
| 1490 | if (neg || radix != 16)
|
|---|
| 1491 | {
|
|---|
| 1492 | work = new int[ival];
|
|---|
| 1493 | getAbsolute(work);
|
|---|
| 1494 | }
|
|---|
| 1495 | else
|
|---|
| 1496 | work = words;
|
|---|
| 1497 | int len = ival;
|
|---|
| 1498 |
|
|---|
| 1499 | int buf_size = len * (MPN.chars_per_word(radix) + 1);
|
|---|
| 1500 | if (radix == 16)
|
|---|
| 1501 | {
|
|---|
| 1502 | if (neg)
|
|---|
| 1503 | buffer.append('-');
|
|---|
| 1504 | int buf_start = buffer.length();
|
|---|
| 1505 | for (int i = len; --i >= 0; )
|
|---|
| 1506 | {
|
|---|
| 1507 | int word = work[i];
|
|---|
| 1508 | for (int j = 8; --j >= 0; )
|
|---|
| 1509 | {
|
|---|
| 1510 | int hex_digit = (word >> (4 * j)) & 0xF;
|
|---|
| 1511 | // Suppress leading zeros:
|
|---|
| 1512 | if (hex_digit > 0 || buffer.length() > buf_start)
|
|---|
| 1513 | buffer.append(Character.forDigit(hex_digit, 16));
|
|---|
| 1514 | }
|
|---|
| 1515 | }
|
|---|
| 1516 | }
|
|---|
| 1517 | else
|
|---|
| 1518 | {
|
|---|
| 1519 | int i = buffer.length();
|
|---|
| 1520 | for (;;)
|
|---|
| 1521 | {
|
|---|
| 1522 | int digit = MPN.divmod_1(work, work, len, radix);
|
|---|
| 1523 | buffer.append(Character.forDigit(digit, radix));
|
|---|
| 1524 | while (len > 0 && work[len-1] == 0) len--;
|
|---|
| 1525 | if (len == 0)
|
|---|
| 1526 | break;
|
|---|
| 1527 | }
|
|---|
| 1528 | if (neg)
|
|---|
| 1529 | buffer.append('-');
|
|---|
| 1530 | /* Reverse buffer. */
|
|---|
| 1531 | int j = buffer.length() - 1;
|
|---|
| 1532 | while (i < j)
|
|---|
| 1533 | {
|
|---|
| 1534 | char tmp = buffer.charAt(i);
|
|---|
| 1535 | buffer.setCharAt(i, buffer.charAt(j));
|
|---|
| 1536 | buffer.setCharAt(j, tmp);
|
|---|
| 1537 | i++; j--;
|
|---|
| 1538 | }
|
|---|
| 1539 | }
|
|---|
| 1540 | }
|
|---|
| 1541 | }
|
|---|
| 1542 |
|
|---|
| 1543 | public String toString()
|
|---|
| 1544 | {
|
|---|
| 1545 | return toString(10);
|
|---|
| 1546 | }
|
|---|
| 1547 |
|
|---|
| 1548 | public String toString(int radix)
|
|---|
| 1549 | {
|
|---|
| 1550 | if (words == null)
|
|---|
| 1551 | return Integer.toString(ival, radix);
|
|---|
| 1552 | else if (ival <= 2)
|
|---|
| 1553 | return Long.toString(longValue(), radix);
|
|---|
| 1554 | int buf_size = ival * (MPN.chars_per_word(radix) + 1);
|
|---|
| 1555 | StringBuffer buffer = new StringBuffer(buf_size);
|
|---|
| 1556 | format(radix, buffer);
|
|---|
| 1557 | return buffer.toString();
|
|---|
| 1558 | }
|
|---|
| 1559 |
|
|---|
| 1560 | public int intValue()
|
|---|
| 1561 | {
|
|---|
| 1562 | if (words == null)
|
|---|
| 1563 | return ival;
|
|---|
| 1564 | return words[0];
|
|---|
| 1565 | }
|
|---|
| 1566 |
|
|---|
| 1567 | public long longValue()
|
|---|
| 1568 | {
|
|---|
| 1569 | if (words == null)
|
|---|
| 1570 | return ival;
|
|---|
| 1571 | if (ival == 1)
|
|---|
| 1572 | return words[0];
|
|---|
| 1573 | return ((long)words[1] << 32) + ((long)words[0] & 0xffffffffL);
|
|---|
| 1574 | }
|
|---|
| 1575 |
|
|---|
| 1576 | public int hashCode()
|
|---|
| 1577 | {
|
|---|
| 1578 | // FIXME: May not match hashcode of JDK.
|
|---|
| 1579 | return words == null ? ival : (words[0] + words[ival - 1]);
|
|---|
| 1580 | }
|
|---|
| 1581 |
|
|---|
| 1582 | /* Assumes x and y are both canonicalized. */
|
|---|
| 1583 | private static boolean equals(BigInteger x, BigInteger y)
|
|---|
| 1584 | {
|
|---|
| 1585 | if (x.words == null && y.words == null)
|
|---|
| 1586 | return x.ival == y.ival;
|
|---|
| 1587 | if (x.words == null || y.words == null || x.ival != y.ival)
|
|---|
| 1588 | return false;
|
|---|
| 1589 | for (int i = x.ival; --i >= 0; )
|
|---|
| 1590 | {
|
|---|
| 1591 | if (x.words[i] != y.words[i])
|
|---|
| 1592 | return false;
|
|---|
| 1593 | }
|
|---|
| 1594 | return true;
|
|---|
| 1595 | }
|
|---|
| 1596 |
|
|---|
| 1597 | /* Assumes this and obj are both canonicalized. */
|
|---|
| 1598 | public boolean equals(Object obj)
|
|---|
| 1599 | {
|
|---|
| 1600 | if (obj == null || ! (obj instanceof BigInteger))
|
|---|
| 1601 | return false;
|
|---|
| 1602 | return BigInteger.equals(this, (BigInteger) obj);
|
|---|
| 1603 | }
|
|---|
| 1604 |
|
|---|
| 1605 | private static BigInteger valueOf(String s, int radix)
|
|---|
| 1606 | throws NumberFormatException
|
|---|
| 1607 | {
|
|---|
| 1608 | int len = s.length();
|
|---|
| 1609 | // Testing (len < MPN.chars_per_word(radix)) would be more accurate,
|
|---|
| 1610 | // but slightly more expensive, for little practical gain.
|
|---|
| 1611 | if (len <= 15 && radix <= 16)
|
|---|
| 1612 | return BigInteger.make(Long.parseLong(s, radix));
|
|---|
| 1613 |
|
|---|
| 1614 | int byte_len = 0;
|
|---|
| 1615 | byte[] bytes = new byte[len];
|
|---|
| 1616 | boolean negative = false;
|
|---|
| 1617 | for (int i = 0; i < len; i++)
|
|---|
| 1618 | {
|
|---|
| 1619 | char ch = s.charAt(i);
|
|---|
| 1620 | if (ch == '-')
|
|---|
| 1621 | negative = true;
|
|---|
| 1622 | else if (ch == '_' || (byte_len == 0 && (ch == ' ' || ch == '\t')))
|
|---|
| 1623 | continue;
|
|---|
| 1624 | else
|
|---|
| 1625 | {
|
|---|
| 1626 | int digit = Character.digit(ch, radix);
|
|---|
| 1627 | if (digit < 0)
|
|---|
| 1628 | break;
|
|---|
| 1629 | bytes[byte_len++] = (byte) digit;
|
|---|
| 1630 | }
|
|---|
| 1631 | }
|
|---|
| 1632 | return valueOf(bytes, byte_len, negative, radix);
|
|---|
| 1633 | }
|
|---|
| 1634 |
|
|---|
| 1635 | private static BigInteger valueOf(byte[] digits, int byte_len,
|
|---|
| 1636 | boolean negative, int radix)
|
|---|
| 1637 | {
|
|---|
| 1638 | int chars_per_word = MPN.chars_per_word(radix);
|
|---|
| 1639 | int[] words = new int[byte_len / chars_per_word + 1];
|
|---|
| 1640 | int size = MPN.set_str(words, digits, byte_len, radix);
|
|---|
| 1641 | if (size == 0)
|
|---|
| 1642 | return ZERO;
|
|---|
| 1643 | if (words[size-1] < 0)
|
|---|
| 1644 | words[size++] = 0;
|
|---|
| 1645 | if (negative)
|
|---|
| 1646 | negate(words, words, size);
|
|---|
| 1647 | return make(words, size);
|
|---|
| 1648 | }
|
|---|
| 1649 |
|
|---|
| 1650 | public double doubleValue()
|
|---|
| 1651 | {
|
|---|
| 1652 | if (words == null)
|
|---|
| 1653 | return (double) ival;
|
|---|
| 1654 | if (ival <= 2)
|
|---|
| 1655 | return (double) longValue();
|
|---|
| 1656 | if (isNegative())
|
|---|
| 1657 | return BigInteger.neg(this).roundToDouble(0, true, false);
|
|---|
| 1658 | else
|
|---|
| 1659 | return roundToDouble(0, false, false);
|
|---|
| 1660 | }
|
|---|
| 1661 |
|
|---|
| 1662 | public float floatValue()
|
|---|
| 1663 | {
|
|---|
| 1664 | return (float) doubleValue();
|
|---|
| 1665 | }
|
|---|
| 1666 |
|
|---|
| 1667 | /** Return true if any of the lowest n bits are one.
|
|---|
| 1668 | * (false if n is negative). */
|
|---|
| 1669 | private boolean checkBits(int n)
|
|---|
| 1670 | {
|
|---|
| 1671 | if (n <= 0)
|
|---|
| 1672 | return false;
|
|---|
| 1673 | if (words == null)
|
|---|
| 1674 | return n > 31 || ((ival & ((1 << n) - 1)) != 0);
|
|---|
| 1675 | int i;
|
|---|
| 1676 | for (i = 0; i < (n >> 5) ; i++)
|
|---|
| 1677 | if (words[i] != 0)
|
|---|
| 1678 | return true;
|
|---|
| 1679 | return (n & 31) != 0 && (words[i] & ((1 << (n & 31)) - 1)) != 0;
|
|---|
| 1680 | }
|
|---|
| 1681 |
|
|---|
| 1682 | /** Convert a semi-processed BigInteger to double.
|
|---|
| 1683 | * Number must be non-negative. Multiplies by a power of two, applies sign,
|
|---|
| 1684 | * and converts to double, with the usual java rounding.
|
|---|
| 1685 | * @param exp power of two, positive or negative, by which to multiply
|
|---|
| 1686 | * @param neg true if negative
|
|---|
| 1687 | * @param remainder true if the BigInteger is the result of a truncating
|
|---|
| 1688 | * division that had non-zero remainder. To ensure proper rounding in
|
|---|
| 1689 | * this case, the BigInteger must have at least 54 bits. */
|
|---|
| 1690 | private double roundToDouble(int exp, boolean neg, boolean remainder)
|
|---|
| 1691 | {
|
|---|
| 1692 | // Compute length.
|
|---|
| 1693 | int il = bitLength();
|
|---|
| 1694 |
|
|---|
| 1695 | // Exponent when normalized to have decimal point directly after
|
|---|
| 1696 | // leading one. This is stored excess 1023 in the exponent bit field.
|
|---|
| 1697 | exp += il - 1;
|
|---|
| 1698 |
|
|---|
| 1699 | // Gross underflow. If exp == -1075, we let the rounding
|
|---|
| 1700 | // computation determine whether it is minval or 0 (which are just
|
|---|
| 1701 | // 0x0000 0000 0000 0001 and 0x0000 0000 0000 0000 as bit
|
|---|
| 1702 | // patterns).
|
|---|
| 1703 | if (exp < -1075)
|
|---|
| 1704 | return neg ? -0.0 : 0.0;
|
|---|
| 1705 |
|
|---|
| 1706 | // gross overflow
|
|---|
| 1707 | if (exp > 1023)
|
|---|
| 1708 | return neg ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
|
|---|
| 1709 |
|
|---|
| 1710 | // number of bits in mantissa, including the leading one.
|
|---|
| 1711 | // 53 unless it's denormalized
|
|---|
| 1712 | int ml = (exp >= -1022 ? 53 : 53 + exp + 1022);
|
|---|
| 1713 |
|
|---|
| 1714 | // Get top ml + 1 bits. The extra one is for rounding.
|
|---|
| 1715 | long m;
|
|---|
| 1716 | int excess_bits = il - (ml + 1);
|
|---|
| 1717 | if (excess_bits > 0)
|
|---|
| 1718 | m = ((words == null) ? ival >> excess_bits
|
|---|
| 1719 | : MPN.rshift_long(words, ival, excess_bits));
|
|---|
| 1720 | else
|
|---|
| 1721 | m = longValue() << (- excess_bits);
|
|---|
| 1722 |
|
|---|
| 1723 | // Special rounding for maxval. If the number exceeds maxval by
|
|---|
| 1724 | // any amount, even if it's less than half a step, it overflows.
|
|---|
| 1725 | if (exp == 1023 && ((m >> 1) == (1L << 53) - 1))
|
|---|
| 1726 | {
|
|---|
| 1727 | if (remainder || checkBits(il - ml))
|
|---|
| 1728 | return neg ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
|
|---|
| 1729 | else
|
|---|
| 1730 | return neg ? - Double.MAX_VALUE : Double.MAX_VALUE;
|
|---|
| 1731 | }
|
|---|
| 1732 |
|
|---|
| 1733 | // Normal round-to-even rule: round up if the bit dropped is a one, and
|
|---|
| 1734 | // the bit above it or any of the bits below it is a one.
|
|---|
| 1735 | if ((m & 1) == 1
|
|---|
| 1736 | && ((m & 2) == 2 || remainder || checkBits(excess_bits)))
|
|---|
| 1737 | {
|
|---|
| 1738 | m += 2;
|
|---|
| 1739 | // Check if we overflowed the mantissa
|
|---|
| 1740 | if ((m & (1L << 54)) != 0)
|
|---|
| 1741 | {
|
|---|
| 1742 | exp++;
|
|---|
| 1743 | // renormalize
|
|---|
| 1744 | m >>= 1;
|
|---|
| 1745 | }
|
|---|
| 1746 | // Check if a denormalized mantissa was just rounded up to a
|
|---|
| 1747 | // normalized one.
|
|---|
| 1748 | else if (ml == 52 && (m & (1L << 53)) != 0)
|
|---|
| 1749 | exp++;
|
|---|
| 1750 | }
|
|---|
| 1751 |
|
|---|
| 1752 | // Discard the rounding bit
|
|---|
| 1753 | m >>= 1;
|
|---|
| 1754 |
|
|---|
| 1755 | long bits_sign = neg ? (1L << 63) : 0;
|
|---|
| 1756 | exp += 1023;
|
|---|
| 1757 | long bits_exp = (exp <= 0) ? 0 : ((long)exp) << 52;
|
|---|
| 1758 | long bits_mant = m & ~(1L << 52);
|
|---|
| 1759 | return Double.longBitsToDouble(bits_sign | bits_exp | bits_mant);
|
|---|
| 1760 | }
|
|---|
| 1761 |
|
|---|
| 1762 | /** Copy the abolute value of this into an array of words.
|
|---|
| 1763 | * Assumes words.length >= (this.words == null ? 1 : this.ival).
|
|---|
| 1764 | * Result is zero-extended, but need not be a valid 2's complement number.
|
|---|
| 1765 | */
|
|---|
| 1766 |
|
|---|
| 1767 | private void getAbsolute(int[] words)
|
|---|
| 1768 | {
|
|---|
| 1769 | int len;
|
|---|
| 1770 | if (this.words == null)
|
|---|
| 1771 | {
|
|---|
| 1772 | len = 1;
|
|---|
| 1773 | words[0] = this.ival;
|
|---|
| 1774 | }
|
|---|
| 1775 | else
|
|---|
| 1776 | {
|
|---|
| 1777 | len = this.ival;
|
|---|
| 1778 | for (int i = len; --i >= 0; )
|
|---|
| 1779 | words[i] = this.words[i];
|
|---|
| 1780 | }
|
|---|
| 1781 | if (words[len - 1] < 0)
|
|---|
| 1782 | negate(words, words, len);
|
|---|
| 1783 | for (int i = words.length; --i > len; )
|
|---|
| 1784 | words[i] = 0;
|
|---|
| 1785 | }
|
|---|
| 1786 |
|
|---|
| 1787 | /** Set dest[0:len-1] to the negation of src[0:len-1].
|
|---|
| 1788 | * Return true if overflow (i.e. if src is -2**(32*len-1)).
|
|---|
| 1789 | * Ok for src==dest. */
|
|---|
| 1790 | private static boolean negate(int[] dest, int[] src, int len)
|
|---|
| 1791 | {
|
|---|
| 1792 | long carry = 1;
|
|---|
| 1793 | boolean negative = src[len-1] < 0;
|
|---|
| 1794 | for (int i = 0; i < len; i++)
|
|---|
| 1795 | {
|
|---|
| 1796 | carry += ((long) (~src[i]) & 0xffffffffL);
|
|---|
| 1797 | dest[i] = (int) carry;
|
|---|
| 1798 | carry >>= 32;
|
|---|
| 1799 | }
|
|---|
| 1800 | return (negative && dest[len-1] < 0);
|
|---|
| 1801 | }
|
|---|
| 1802 |
|
|---|
| 1803 | /** Destructively set this to the negative of x.
|
|---|
| 1804 | * It is OK if x==this.*/
|
|---|
| 1805 | private void setNegative(BigInteger x)
|
|---|
| 1806 | {
|
|---|
| 1807 | int len = x.ival;
|
|---|
| 1808 | if (x.words == null)
|
|---|
| 1809 | {
|
|---|
| 1810 | if (len == Integer.MIN_VALUE)
|
|---|
| 1811 | set(- (long) len);
|
|---|
| 1812 | else
|
|---|
| 1813 | set(-len);
|
|---|
| 1814 | return;
|
|---|
| 1815 | }
|
|---|
| 1816 | realloc(len + 1);
|
|---|
| 1817 | if (BigInteger.negate(words, x.words, len))
|
|---|
| 1818 | words[len++] = 0;
|
|---|
| 1819 | ival = len;
|
|---|
| 1820 | }
|
|---|
| 1821 |
|
|---|
| 1822 | /** Destructively negate this. */
|
|---|
| 1823 | private final void setNegative()
|
|---|
| 1824 | {
|
|---|
| 1825 | setNegative(this);
|
|---|
| 1826 | }
|
|---|
| 1827 |
|
|---|
| 1828 | private static BigInteger abs(BigInteger x)
|
|---|
| 1829 | {
|
|---|
| 1830 | return x.isNegative() ? neg(x) : x;
|
|---|
| 1831 | }
|
|---|
| 1832 |
|
|---|
| 1833 | public BigInteger abs()
|
|---|
| 1834 | {
|
|---|
| 1835 | return abs(this);
|
|---|
| 1836 | }
|
|---|
| 1837 |
|
|---|
| 1838 | private static BigInteger neg(BigInteger x)
|
|---|
| 1839 | {
|
|---|
| 1840 | if (x.words == null && x.ival != Integer.MIN_VALUE)
|
|---|
| 1841 | return make(- x.ival);
|
|---|
| 1842 | BigInteger result = new BigInteger(0);
|
|---|
| 1843 | result.setNegative(x);
|
|---|
| 1844 | return result.canonicalize();
|
|---|
| 1845 | }
|
|---|
| 1846 |
|
|---|
| 1847 | public BigInteger negate()
|
|---|
| 1848 | {
|
|---|
| 1849 | return BigInteger.neg(this);
|
|---|
| 1850 | }
|
|---|
| 1851 |
|
|---|
| 1852 | /** Calculates ceiling(log2(this < 0 ? -this : this+1))
|
|---|
| 1853 | * See Common Lisp: the Language, 2nd ed, p. 361.
|
|---|
| 1854 | */
|
|---|
| 1855 | public int bitLength()
|
|---|
| 1856 | {
|
|---|
| 1857 | if (words == null)
|
|---|
| 1858 | return MPN.intLength(ival);
|
|---|
| 1859 | else
|
|---|
| 1860 | return MPN.intLength(words, ival);
|
|---|
| 1861 | }
|
|---|
| 1862 |
|
|---|
| 1863 | public byte[] toByteArray()
|
|---|
| 1864 | {
|
|---|
| 1865 | // Determine number of bytes needed. The method bitlength returns
|
|---|
| 1866 | // the size without the sign bit, so add one bit for that and then
|
|---|
| 1867 | // add 7 more to emulate the ceil function using integer math.
|
|---|
| 1868 | byte[] bytes = new byte[(bitLength() + 1 + 7) / 8];
|
|---|
| 1869 | int nbytes = bytes.length;
|
|---|
| 1870 |
|
|---|
| 1871 | int wptr = 0;
|
|---|
| 1872 | int word;
|
|---|
| 1873 |
|
|---|
| 1874 | // Deal with words array until one word or less is left to process.
|
|---|
| 1875 | // If BigInteger is an int, then it is in ival and nbytes will be <= 4.
|
|---|
| 1876 | while (nbytes > 4)
|
|---|
| 1877 | {
|
|---|
| 1878 | word = words[wptr++];
|
|---|
| 1879 | for (int i = 4; i > 0; --i, word >>= 8)
|
|---|
| 1880 | bytes[--nbytes] = (byte) word;
|
|---|
| 1881 | }
|
|---|
| 1882 |
|
|---|
| 1883 | // Deal with the last few bytes. If BigInteger is an int, use ival.
|
|---|
| 1884 | word = (words == null) ? ival : words[wptr];
|
|---|
| 1885 | for ( ; nbytes > 0; word >>= 8)
|
|---|
| 1886 | bytes[--nbytes] = (byte) word;
|
|---|
| 1887 |
|
|---|
| 1888 | return bytes;
|
|---|
| 1889 | }
|
|---|
| 1890 |
|
|---|
| 1891 | /** Return the boolean opcode (for bitOp) for swapped operands.
|
|---|
| 1892 | * I.e. bitOp(swappedOp(op), x, y) == bitOp(op, y, x).
|
|---|
| 1893 | */
|
|---|
| 1894 | private static int swappedOp(int op)
|
|---|
| 1895 | {
|
|---|
| 1896 | return
|
|---|
| 1897 | "\000\001\004\005\002\003\006\007\010\011\014\015\012\013\016\017"
|
|---|
| 1898 | .charAt(op);
|
|---|
| 1899 | }
|
|---|
| 1900 |
|
|---|
| 1901 | /** Do one the the 16 possible bit-wise operations of two BigIntegers. */
|
|---|
| 1902 | private static BigInteger bitOp(int op, BigInteger x, BigInteger y)
|
|---|
| 1903 | {
|
|---|
| 1904 | switch (op)
|
|---|
| 1905 | {
|
|---|
| 1906 | case 0: return ZERO;
|
|---|
| 1907 | case 1: return x.and(y);
|
|---|
| 1908 | case 3: return x;
|
|---|
| 1909 | case 5: return y;
|
|---|
| 1910 | case 15: return make(-1);
|
|---|
| 1911 | }
|
|---|
| 1912 | BigInteger result = new BigInteger();
|
|---|
| 1913 | setBitOp(result, op, x, y);
|
|---|
| 1914 | return result.canonicalize();
|
|---|
| 1915 | }
|
|---|
| 1916 |
|
|---|
| 1917 | /** Do one the the 16 possible bit-wise operations of two BigIntegers. */
|
|---|
| 1918 | private static void setBitOp(BigInteger result, int op,
|
|---|
| 1919 | BigInteger x, BigInteger y)
|
|---|
| 1920 | {
|
|---|
| 1921 | if (y.words == null) ;
|
|---|
| 1922 | else if (x.words == null || x.ival < y.ival)
|
|---|
| 1923 | {
|
|---|
| 1924 | BigInteger temp = x; x = y; y = temp;
|
|---|
| 1925 | op = swappedOp(op);
|
|---|
| 1926 | }
|
|---|
| 1927 | int xi;
|
|---|
| 1928 | int yi;
|
|---|
| 1929 | int xlen, ylen;
|
|---|
| 1930 | if (y.words == null)
|
|---|
| 1931 | {
|
|---|
| 1932 | yi = y.ival;
|
|---|
| 1933 | ylen = 1;
|
|---|
| 1934 | }
|
|---|
| 1935 | else
|
|---|
| 1936 | {
|
|---|
| 1937 | yi = y.words[0];
|
|---|
| 1938 | ylen = y.ival;
|
|---|
| 1939 | }
|
|---|
| 1940 | if (x.words == null)
|
|---|
| 1941 | {
|
|---|
| 1942 | xi = x.ival;
|
|---|
| 1943 | xlen = 1;
|
|---|
| 1944 | }
|
|---|
| 1945 | else
|
|---|
| 1946 | {
|
|---|
| 1947 | xi = x.words[0];
|
|---|
| 1948 | xlen = x.ival;
|
|---|
| 1949 | }
|
|---|
| 1950 | if (xlen > 1)
|
|---|
| 1951 | result.realloc(xlen);
|
|---|
| 1952 | int[] w = result.words;
|
|---|
| 1953 | int i = 0;
|
|---|
| 1954 | // Code for how to handle the remainder of x.
|
|---|
| 1955 | // 0: Truncate to length of y.
|
|---|
| 1956 | // 1: Copy rest of x.
|
|---|
| 1957 | // 2: Invert rest of x.
|
|---|
| 1958 | int finish = 0;
|
|---|
| 1959 | int ni;
|
|---|
| 1960 | switch (op)
|
|---|
| 1961 | {
|
|---|
| 1962 | case 0: // clr
|
|---|
| 1963 | ni = 0;
|
|---|
| 1964 | break;
|
|---|
| 1965 | case 1: // and
|
|---|
| 1966 | for (;;)
|
|---|
| 1967 | {
|
|---|
| 1968 | ni = xi & yi;
|
|---|
| 1969 | if (i+1 >= ylen) break;
|
|---|
| 1970 | w[i++] = ni; xi = x.words[i]; yi = y.words[i];
|
|---|
| 1971 | }
|
|---|
| 1972 | if (yi < 0) finish = 1;
|
|---|
| 1973 | break;
|
|---|
| 1974 | case 2: // andc2
|
|---|
| 1975 | for (;;)
|
|---|
| 1976 | {
|
|---|
| 1977 | ni = xi & ~yi;
|
|---|
| 1978 | if (i+1 >= ylen) break;
|
|---|
| 1979 | w[i++] = ni; xi = x.words[i]; yi = y.words[i];
|
|---|
| 1980 | }
|
|---|
| 1981 | if (yi >= 0) finish = 1;
|
|---|
| 1982 | break;
|
|---|
| 1983 | case 3: // copy x
|
|---|
| 1984 | ni = xi;
|
|---|
| 1985 | finish = 1; // Copy rest
|
|---|
| 1986 | break;
|
|---|
| 1987 | case 4: // andc1
|
|---|
| 1988 | for (;;)
|
|---|
| 1989 | {
|
|---|
| 1990 | ni = ~xi & yi;
|
|---|
| 1991 | if (i+1 >= ylen) break;
|
|---|
| 1992 | w[i++] = ni; xi = x.words[i]; yi = y.words[i];
|
|---|
| 1993 | }
|
|---|
| 1994 | if (yi < 0) finish = 2;
|
|---|
| 1995 | break;
|
|---|
| 1996 | case 5: // copy y
|
|---|
| 1997 | for (;;)
|
|---|
| 1998 | {
|
|---|
| 1999 | ni = yi;
|
|---|
| 2000 | if (i+1 >= ylen) break;
|
|---|
| 2001 | w[i++] = ni; xi = x.words[i]; yi = y.words[i];
|
|---|
| 2002 | }
|
|---|
| 2003 | break;
|
|---|
| 2004 | case 6: // xor
|
|---|
| 2005 | for (;;)
|
|---|
| 2006 | {
|
|---|
| 2007 | ni = xi ^ yi;
|
|---|
| 2008 | if (i+1 >= ylen) break;
|
|---|
| 2009 | w[i++] = ni; xi = x.words[i]; yi = y.words[i];
|
|---|
| 2010 | }
|
|---|
| 2011 | finish = yi < 0 ? 2 : 1;
|
|---|
| 2012 | break;
|
|---|
| 2013 | case 7: // ior
|
|---|
| 2014 | for (;;)
|
|---|
| 2015 | {
|
|---|
| 2016 | ni = xi | yi;
|
|---|
| 2017 | if (i+1 >= ylen) break;
|
|---|
| 2018 | w[i++] = ni; xi = x.words[i]; yi = y.words[i];
|
|---|
| 2019 | }
|
|---|
| 2020 | if (yi >= 0) finish = 1;
|
|---|
| 2021 | break;
|
|---|
| 2022 | case 8: // nor
|
|---|
| 2023 | for (;;)
|
|---|
| 2024 | {
|
|---|
| 2025 | ni = ~(xi | yi);
|
|---|
| 2026 | if (i+1 >= ylen) break;
|
|---|
| 2027 | w[i++] = ni; xi = x.words[i]; yi = y.words[i];
|
|---|
| 2028 | }
|
|---|
| 2029 | if (yi >= 0) finish = 2;
|
|---|
| 2030 | break;
|
|---|
| 2031 | case 9: // eqv [exclusive nor]
|
|---|
| 2032 | for (;;)
|
|---|
| 2033 | {
|
|---|
| 2034 | ni = ~(xi ^ yi);
|
|---|
| 2035 | if (i+1 >= ylen) break;
|
|---|
| 2036 | w[i++] = ni; xi = x.words[i]; yi = y.words[i];
|
|---|
| 2037 | }
|
|---|
| 2038 | finish = yi >= 0 ? 2 : 1;
|
|---|
| 2039 | break;
|
|---|
| 2040 | case 10: // c2
|
|---|
| 2041 | for (;;)
|
|---|
| 2042 | {
|
|---|
| 2043 | ni = ~yi;
|
|---|
| 2044 | if (i+1 >= ylen) break;
|
|---|
| 2045 | w[i++] = ni; xi = x.words[i]; yi = y.words[i];
|
|---|
| 2046 | }
|
|---|
| 2047 | break;
|
|---|
| 2048 | case 11: // orc2
|
|---|
| 2049 | for (;;)
|
|---|
| 2050 | {
|
|---|
| 2051 | ni = xi | ~yi;
|
|---|
| 2052 | if (i+1 >= ylen) break;
|
|---|
| 2053 | w[i++] = ni; xi = x.words[i]; yi = y.words[i];
|
|---|
| 2054 | }
|
|---|
| 2055 | if (yi < 0) finish = 1;
|
|---|
| 2056 | break;
|
|---|
| 2057 | case 12: // c1
|
|---|
| 2058 | ni = ~xi;
|
|---|
| 2059 | finish = 2;
|
|---|
| 2060 | break;
|
|---|
| 2061 | case 13: // orc1
|
|---|
| 2062 | for (;;)
|
|---|
| 2063 | {
|
|---|
| 2064 | ni = ~xi | yi;
|
|---|
| 2065 | if (i+1 >= ylen) break;
|
|---|
| 2066 | w[i++] = ni; xi = x.words[i]; yi = y.words[i];
|
|---|
| 2067 | }
|
|---|
| 2068 | if (yi >= 0) finish = 2;
|
|---|
| 2069 | break;
|
|---|
| 2070 | case 14: // nand
|
|---|
| 2071 | for (;;)
|
|---|
| 2072 | {
|
|---|
| 2073 | ni = ~(xi & yi);
|
|---|
| 2074 | if (i+1 >= ylen) break;
|
|---|
| 2075 | w[i++] = ni; xi = x.words[i]; yi = y.words[i];
|
|---|
| 2076 | }
|
|---|
| 2077 | if (yi < 0) finish = 2;
|
|---|
| 2078 | break;
|
|---|
| 2079 | default:
|
|---|
| 2080 | case 15: // set
|
|---|
| 2081 | ni = -1;
|
|---|
| 2082 | break;
|
|---|
| 2083 | }
|
|---|
| 2084 | // Here i==ylen-1; w[0]..w[i-1] have the correct result;
|
|---|
| 2085 | // and ni contains the correct result for w[i+1].
|
|---|
| 2086 | if (i+1 == xlen)
|
|---|
| 2087 | finish = 0;
|
|---|
| 2088 | switch (finish)
|
|---|
| 2089 | {
|
|---|
| 2090 | case 0:
|
|---|
| 2091 | if (i == 0 && w == null)
|
|---|
| 2092 | {
|
|---|
| 2093 | result.ival = ni;
|
|---|
| 2094 | return;
|
|---|
| 2095 | }
|
|---|
| 2096 | w[i++] = ni;
|
|---|
| 2097 | break;
|
|---|
| 2098 | case 1: w[i] = ni; while (++i < xlen) w[i] = x.words[i]; break;
|
|---|
| 2099 | case 2: w[i] = ni; while (++i < xlen) w[i] = ~x.words[i]; break;
|
|---|
| 2100 | }
|
|---|
| 2101 | result.ival = i;
|
|---|
| 2102 | }
|
|---|
| 2103 |
|
|---|
| 2104 | /** Return the logical (bit-wise) "and" of a BigInteger and an int. */
|
|---|
| 2105 | private static BigInteger and(BigInteger x, int y)
|
|---|
| 2106 | {
|
|---|
| 2107 | if (x.words == null)
|
|---|
| 2108 | return BigInteger.make(x.ival & y);
|
|---|
| 2109 | if (y >= 0)
|
|---|
| 2110 | return BigInteger.make(x.words[0] & y);
|
|---|
| 2111 | int len = x.ival;
|
|---|
| 2112 | int[] words = new int[len];
|
|---|
| 2113 | words[0] = x.words[0] & y;
|
|---|
| 2114 | while (--len > 0)
|
|---|
| 2115 | words[len] = x.words[len];
|
|---|
| 2116 | return BigInteger.make(words, x.ival);
|
|---|
| 2117 | }
|
|---|
| 2118 |
|
|---|
| 2119 | /** Return the logical (bit-wise) "and" of two BigIntegers. */
|
|---|
| 2120 | public BigInteger and(BigInteger y)
|
|---|
| 2121 | {
|
|---|
| 2122 | if (y.words == null)
|
|---|
| 2123 | return and(this, y.ival);
|
|---|
| 2124 | else if (words == null)
|
|---|
| 2125 | return and(y, ival);
|
|---|
| 2126 |
|
|---|
| 2127 | BigInteger x = this;
|
|---|
| 2128 | if (ival < y.ival)
|
|---|
| 2129 | {
|
|---|
| 2130 | BigInteger temp = this; x = y; y = temp;
|
|---|
| 2131 | }
|
|---|
| 2132 | int i;
|
|---|
| 2133 | int len = y.isNegative() ? x.ival : y.ival;
|
|---|
| 2134 | int[] words = new int[len];
|
|---|
| 2135 | for (i = 0; i < y.ival; i++)
|
|---|
| 2136 | words[i] = x.words[i] & y.words[i];
|
|---|
| 2137 | for ( ; i < len; i++)
|
|---|
| 2138 | words[i] = x.words[i];
|
|---|
| 2139 | return BigInteger.make(words, len);
|
|---|
| 2140 | }
|
|---|
| 2141 |
|
|---|
| 2142 | /** Return the logical (bit-wise) "(inclusive) or" of two BigIntegers. */
|
|---|
| 2143 | public BigInteger or(BigInteger y)
|
|---|
| 2144 | {
|
|---|
| 2145 | return bitOp(7, this, y);
|
|---|
| 2146 | }
|
|---|
| 2147 |
|
|---|
| 2148 | /** Return the logical (bit-wise) "exclusive or" of two BigIntegers. */
|
|---|
| 2149 | public BigInteger xor(BigInteger y)
|
|---|
| 2150 | {
|
|---|
| 2151 | return bitOp(6, this, y);
|
|---|
| 2152 | }
|
|---|
| 2153 |
|
|---|
| 2154 | /** Return the logical (bit-wise) negation of a BigInteger. */
|
|---|
| 2155 | public BigInteger not()
|
|---|
| 2156 | {
|
|---|
| 2157 | return bitOp(12, this, ZERO);
|
|---|
| 2158 | }
|
|---|
| 2159 |
|
|---|
| 2160 | public BigInteger andNot(BigInteger val)
|
|---|
| 2161 | {
|
|---|
| 2162 | return and(val.not());
|
|---|
| 2163 | }
|
|---|
| 2164 |
|
|---|
| 2165 | public BigInteger clearBit(int n)
|
|---|
| 2166 | {
|
|---|
| 2167 | if (n < 0)
|
|---|
| 2168 | throw new ArithmeticException();
|
|---|
| 2169 |
|
|---|
| 2170 | return and(ONE.shiftLeft(n).not());
|
|---|
| 2171 | }
|
|---|
| 2172 |
|
|---|
| 2173 | public BigInteger setBit(int n)
|
|---|
| 2174 | {
|
|---|
| 2175 | if (n < 0)
|
|---|
| 2176 | throw new ArithmeticException();
|
|---|
| 2177 |
|
|---|
| 2178 | return or(ONE.shiftLeft(n));
|
|---|
| 2179 | }
|
|---|
| 2180 |
|
|---|
| 2181 | public boolean testBit(int n)
|
|---|
| 2182 | {
|
|---|
| 2183 | if (n < 0)
|
|---|
| 2184 | throw new ArithmeticException();
|
|---|
| 2185 |
|
|---|
| 2186 | return !and(ONE.shiftLeft(n)).isZero();
|
|---|
| 2187 | }
|
|---|
| 2188 |
|
|---|
| 2189 | public BigInteger flipBit(int n)
|
|---|
| 2190 | {
|
|---|
| 2191 | if (n < 0)
|
|---|
| 2192 | throw new ArithmeticException();
|
|---|
| 2193 |
|
|---|
| 2194 | return xor(ONE.shiftLeft(n));
|
|---|
| 2195 | }
|
|---|
| 2196 |
|
|---|
| 2197 | public int getLowestSetBit()
|
|---|
| 2198 | {
|
|---|
| 2199 | if (isZero())
|
|---|
| 2200 | return -1;
|
|---|
| 2201 |
|
|---|
| 2202 | if (words == null)
|
|---|
| 2203 | return MPN.findLowestBit(ival);
|
|---|
| 2204 | else
|
|---|
| 2205 | return MPN.findLowestBit(words);
|
|---|
| 2206 | }
|
|---|
| 2207 |
|
|---|
| 2208 | // bit4count[I] is number of '1' bits in I.
|
|---|
| 2209 | private static final byte[] bit4_count = { 0, 1, 1, 2, 1, 2, 2, 3,
|
|---|
| 2210 | 1, 2, 2, 3, 2, 3, 3, 4};
|
|---|
| 2211 |
|
|---|
| 2212 | private static int bitCount(int i)
|
|---|
| 2213 | {
|
|---|
| 2214 | int count = 0;
|
|---|
| 2215 | while (i != 0)
|
|---|
| 2216 | {
|
|---|
| 2217 | count += bit4_count[i & 15];
|
|---|
| 2218 | i >>>= 4;
|
|---|
| 2219 | }
|
|---|
| 2220 | return count;
|
|---|
| 2221 | }
|
|---|
| 2222 |
|
|---|
| 2223 | private static int bitCount(int[] x, int len)
|
|---|
| 2224 | {
|
|---|
| 2225 | int count = 0;
|
|---|
| 2226 | while (--len >= 0)
|
|---|
| 2227 | count += bitCount(x[len]);
|
|---|
| 2228 | return count;
|
|---|
| 2229 | }
|
|---|
| 2230 |
|
|---|
| 2231 | /** Count one bits in a BigInteger.
|
|---|
| 2232 | * If argument is negative, count zero bits instead. */
|
|---|
| 2233 | public int bitCount()
|
|---|
| 2234 | {
|
|---|
| 2235 | int i, x_len;
|
|---|
| 2236 | int[] x_words = words;
|
|---|
| 2237 | if (x_words == null)
|
|---|
| 2238 | {
|
|---|
| 2239 | x_len = 1;
|
|---|
| 2240 | i = bitCount(ival);
|
|---|
| 2241 | }
|
|---|
| 2242 | else
|
|---|
| 2243 | {
|
|---|
| 2244 | x_len = ival;
|
|---|
| 2245 | i = bitCount(x_words, x_len);
|
|---|
| 2246 | }
|
|---|
| 2247 | return isNegative() ? x_len * 32 - i : i;
|
|---|
| 2248 | }
|
|---|
| 2249 |
|
|---|
| 2250 | private void readObject(ObjectInputStream s)
|
|---|
| 2251 | throws IOException, ClassNotFoundException
|
|---|
| 2252 | {
|
|---|
| 2253 | s.defaultReadObject();
|
|---|
| 2254 | words = byteArrayToIntArray(magnitude, signum < 0 ? -1 : 0);
|
|---|
| 2255 | BigInteger result = make(words, words.length);
|
|---|
| 2256 | this.ival = result.ival;
|
|---|
| 2257 | this.words = result.words;
|
|---|
| 2258 | }
|
|---|
| 2259 |
|
|---|
| 2260 | private void writeObject(ObjectOutputStream s)
|
|---|
| 2261 | throws IOException, ClassNotFoundException
|
|---|
| 2262 | {
|
|---|
| 2263 | signum = signum();
|
|---|
| 2264 | magnitude = toByteArray();
|
|---|
| 2265 | s.defaultWriteObject();
|
|---|
| 2266 | }
|
|---|
| 2267 | }
|
|---|