1 /* gnu.java.math.GMP -- Arbitary precision integers using GMP
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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. */
39 package gnu.java.math;
41 import gnu.classpath.Pointer;
44 * Implement BigInteger using GMP
46 public final class GMP
48 private Pointer native_ptr;
49 private int refCount = 1;
58 private synchronized void acquireRef()
63 private synchronized void releaseRef()
73 protected void finalize()
79 public void fromByteArray(byte[] v)
86 public void fromBI(GMP x)
90 natFromBI(x.native_ptr);
95 public void fromLong(long n)
102 public int fromString(String s, int rdx)
105 int result = natFromString(s, rdx);
110 public void fromSignedMagnitude(byte[] m, boolean isNegative)
113 natFromSignedMagnitude(m, isNegative);
117 public String toString(int b)
120 String result = natToString(b);
125 public void toByteArray(byte[] r)
132 public double doubleValue()
135 double result = natDoubleValue();
140 public int absIntValue()
143 int result = natAbsIntValue();
148 public int compare(GMP x)
152 int result = natCompare(x.native_ptr);
158 public void add(GMP x, GMP r)
163 natAdd(x.native_ptr, r.native_ptr);
169 public void subtract(GMP x, GMP r)
174 natSubtract(x.native_ptr, r.native_ptr);
180 public void multiply(GMP x, GMP r)
185 natMultiply(x.native_ptr, r.native_ptr);
191 public void quotient(GMP x, GMP r)
196 natQuotient(x.native_ptr, r.native_ptr);
202 public void remainder(GMP x, GMP r)
207 natRemainder(x.native_ptr, r.native_ptr);
213 public void quotientAndRemainder(GMP x, GMP q, GMP r)
219 natQuotientAndRemainder(x.native_ptr, q.native_ptr, r.native_ptr);
226 public void modulo(GMP x, GMP r)
231 natModulo(x.native_ptr, r.native_ptr);
237 public void pow(int n, GMP r)
241 natPow(n, r.native_ptr);
246 public void modPow(GMP e, GMP m, GMP r)
252 natModPow(e.native_ptr, m.native_ptr, r.native_ptr);
259 public void modInverse(GMP m, GMP r)
264 natModInverse(m.native_ptr, r.native_ptr);
270 public void gcd(GMP x, GMP r)
275 natGCD(x.native_ptr, r.native_ptr);
281 public void shiftLeft(int n, GMP r)
285 natShiftLeft(n, r.native_ptr);
290 public void shiftRight(int n, GMP r)
294 natShiftRight(n, r.native_ptr);
299 public void abs(GMP r)
303 natAbs(r.native_ptr);
308 public void negate(GMP r)
312 natNegate(r.native_ptr);
317 public int bitLength()
320 int result = natBitLength();
325 public int bitCount()
328 int result = natSetBitCount();
333 public void and(GMP x, GMP r)
338 natAnd(x.native_ptr, r.native_ptr);
344 public void or(GMP x, GMP r)
349 natOr(x.native_ptr, r.native_ptr);
355 public void xor(GMP x, GMP r)
360 natXor(x.native_ptr, r.native_ptr);
366 public void andNot(GMP x, GMP r)
371 natAndNot(x.native_ptr, r.native_ptr);
377 public void not(GMP r)
381 natNot(r.native_ptr);
386 public void flipBit(int n, GMP r)
390 natFlipBit(n, r.native_ptr);
395 public int testBit(int n)
398 int result = natTestBit(n);
403 public void setBit(int n, boolean setIt, GMP r)
407 natSetBit(n, setIt, r.native_ptr);
412 public int testPrimality(int certainty)
415 int result = natTestPrimality(certainty);
420 public int lowestSetBit()
423 int result = natLowestSetBit();
428 // Native methods .........................................................
430 public static native void natInitializeLibrary();
432 private native void natInitialize();
433 private native void natFinalize();
435 private native void natFromLong(long n);
436 private native void natFromBI(Pointer x);
437 private native void natFromByteArray(byte[] v);
438 private native int natFromString(String s, int rdx);
439 private native void natFromSignedMagnitude(byte[] m, boolean isNegative);
441 private native String natToString(int base);
442 private native void natToByteArray(byte[] r);
443 private native int natAbsIntValue();
444 private native double natDoubleValue();
446 private native int natCompare(Pointer y);
447 private native void natAdd(Pointer x, Pointer r);
448 private native void natSubtract(Pointer x, Pointer r);
449 private native void natMultiply(Pointer x, Pointer r);
450 private native void natQuotient(Pointer x, Pointer r);
451 private native void natRemainder(Pointer x, Pointer r);
452 private native void natQuotientAndRemainder(Pointer x, Pointer q, Pointer r);
453 private native void natModulo(Pointer m, Pointer r);
454 private native void natPow(int n, Pointer r);
455 private native void natModPow(Pointer e, Pointer m, Pointer r);
456 private native void natModInverse(Pointer x, Pointer r);
457 private native void natGCD(Pointer x, Pointer r);
458 private native int natTestPrimality(int c);
459 private native void natShiftLeft(int n, Pointer r);
460 private native void natShiftRight(int n, Pointer r);
461 private native int natLowestSetBit();
462 private native void natAbs(Pointer r);
463 private native void natNegate(Pointer r);
464 private native int natBitLength();
465 private native int natSetBitCount();
466 private native void natXor(Pointer x, Pointer r);
467 private native void natOr(Pointer x, Pointer r);
468 private native void natAnd(Pointer x, Pointer r);
469 private native void natAndNot(Pointer x, Pointer r);
470 private native void natFlipBit(int n, Pointer r);
471 private native int natTestBit(int n);
472 private native void natSetBit(int n, boolean setIt, Pointer r);
473 private native void natNot(Pointer r);