gnu.java.math

Class MPN


public class MPN
extends Object

This contains various low-level routines for unsigned bigints. The interfaces match the mpn interfaces in gmp, so it should be easy to replace them with fast native functions that are trivial wrappers around the mpn_ functions in gmp (at least on platforms that use 32-bit "limbs").

Method Summary

static int
add_1(int[] dest, int[] x, int size, int y)
Add x[0:size-1] and y, and write the size least significant words of the result to dest.
static int
add_n(dest[] , int[] x, int[] y, int len)
Add x[0:len-1] and y[0:len-1] and write the len least significant words of the result to dest[0:len-1].
static int
chars_per_word(int radix)
Number of digits in the conversion base that always fits in a word.
static int
cmp(int[] x, int xlen, int[] y, int ylen)
Compare x[0:xlen-1] with y[0:ylen-1], treating them as unsigned integers.
static int
cmp(int[] x, int[] y, int size)
Compare x[0:size-1] with y[0:size-1], treating them as unsigned integers.
static int
count_leading_zeros(int i)
Count the number of leading zero bits in an int.
static void
divide(int[] zds, int nx, int[] y, int ny)
Divide zds[0:nx] by y[0:ny-1].
static int
divmod_1(int[] quotient, int[] dividend, int len, int divisor)
Divide divident[0:len-1] by (unsigned int)divisor.
static int
findLowestBit(int word)
Return least i such that word & (1<<i).
static int
findLowestBit(int[] words)
Return least i such that words & (1<<i).
static int
gcd(int[] x, int[] y, int len)
Calculate Greatest Common Divisior of x[0:len-1] and y[0:len-1].
static int
intLength(int i)
static int
intLength(int[] words, int len)
Calcaulte the Common Lisp "integer-length" function.
static int
lshift(int[] dest, int d_offset, int[] x, int len, int count)
static void
mul(int[] dest, int[] x, int xlen, int[] y, int ylen)
Multiply x[0:xlen-1] and y[0:ylen-1], and write the result to dest[0:xlen+ylen-1].
static int
mul_1(int[] dest, int[] x, int len, int y)
Multiply x[0:len-1] by y, and write the len least significant words of the product to dest[0:len-1].
static int
rshift(int[] dest, int[] x, int x_start, int len, int count)
Shift x[x_start:x_start+len-1] count bits to the "right" (i.e. divide by 2**count).
static void
rshift0(int[] dest, int[] x, int x_start, int len, int count)
Shift x[x_start:x_start+len-1] count bits to the "right" (i.e. divide by 2**count).
static long
rshift_long(int[] x, int len, int count)
Return the long-truncated value of right shifting.
static int
set_str(dest[] , byte[] str, int str_len, int base)
static int
sub_n(int[] dest, int[] X, int[] Y, int size)
Subtract Y[0:size-1] from X[0:size-1], and write the size least significant words of the result to dest[0:size-1].
static int
submul_1(int[] dest, int offset, int[] x, int len, int y)
static long
udiv_qrnnd(long N, int D)

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Method Details

add_1

public static int add_1(int[] dest,
                        int[] x,
                        int size,
                        int y)
Add x[0:size-1] and y, and write the size least significant words of the result to dest. Return carry, either 0 or 1. All values are unsigned. This is basically the same as gmp's mpn_add_1.

add_n

public static int add_n(dest[] ,
                        int[] x,
                        int[] y,
                        int len)
Add x[0:len-1] and y[0:len-1] and write the len least significant words of the result to dest[0:len-1]. All words are treated as unsigned.
Returns:
the carry, either 0 or 1 This function is basically the same as gmp's mpn_add_n.

chars_per_word

public static int chars_per_word(int radix)
Number of digits in the conversion base that always fits in a word. For example, for base 10 this is 9, since 10**9 is the largest number that fits into a words (assuming 32-bit words). This is the same as gmp's __mp_bases[radix].chars_per_limb.
Parameters:
radix - the base
Returns:
number of digits

cmp

public static int cmp(int[] x,
                      int xlen,
                      int[] y,
                      int ylen)
Compare x[0:xlen-1] with y[0:ylen-1], treating them as unsigned integers.
Returns:
-1, 0, or 1 depending on if x<y, x==y, or x>y.

cmp

public static int cmp(int[] x,
                      int[] y,
                      int size)
Compare x[0:size-1] with y[0:size-1], treating them as unsigned integers.

count_leading_zeros

public static int count_leading_zeros(int i)
Count the number of leading zero bits in an int.

divide

public static void divide(int[] zds,
                          int nx,
                          int[] y,
                          int ny)
Divide zds[0:nx] by y[0:ny-1]. The remainder ends up in zds[0:ny-1]. The quotient ends up in zds[ny:nx]. Assumes: nx>ny. (int)y[ny-1] <320 (i.e. most significant bit set)

divmod_1

public static int divmod_1(int[] quotient,
                           int[] dividend,
                           int len,
                           int divisor)
Divide divident[0:len-1] by (unsigned int)divisor. Write result into quotient[0:len-1. Return the one-word (unsigned) remainder. OK for quotient==dividend.

findLowestBit

public static int findLowestBit(int word)
Return least i such that word & (1<<i). Assumes word!=0.

findLowestBit

public static int findLowestBit(int[] words)
Return least i such that words & (1<<i). Assumes there is such an i.

gcd

public static int gcd(int[] x,
                      int[] y,
                      int len)
Calculate Greatest Common Divisior of x[0:len-1] and y[0:len-1]. Assumes both arguments are non-zero. Leaves result in x, and returns len of result. Also destroys y (actually sets it to a copy of the result).

intLength

public static int intLength(int i)

intLength

public static int intLength(int[] words,
                            int len)
Calcaulte the Common Lisp "integer-length" function. Assumes input is canonicalized: len==BigInteger.wordsNeeded(words,len)

lshift

public static int lshift(int[] dest,
                         int d_offset,
                         int[] x,
                         int len,
                         int count)

mul

public static void mul(int[] dest,
                       int[] x,
                       int xlen,
                       int[] y,
                       int ylen)
Multiply x[0:xlen-1] and y[0:ylen-1], and write the result to dest[0:xlen+ylen-1]. The destination has to have space for xlen+ylen words, even if the result might be one limb smaller. This function requires that xlen >= ylen. The destination must be distinct from either input operands. All operands are unsigned. This function is basically the same gmp's mpn_mul.

mul_1

public static int mul_1(int[] dest,
                        int[] x,
                        int len,
                        int y)
Multiply x[0:len-1] by y, and write the len least significant words of the product to dest[0:len-1]. Return the most significant word of the product. All values are treated as if they were unsigned (i.e. masked with 0xffffffffL). OK if dest==x (not sure if this is guaranteed for mpn_mul_1). This function is basically the same as gmp's mpn_mul_1.

rshift

public static int rshift(int[] dest,
                         int[] x,
                         int x_start,
                         int len,
                         int count)
Shift x[x_start:x_start+len-1] count bits to the "right" (i.e. divide by 2**count). Store the len least significant words of the result at dest. The bits shifted out to the right are returned. OK if dest==x. Assumes: 0 < count < 32

rshift0

public static void rshift0(int[] dest,
                           int[] x,
                           int x_start,
                           int len,
                           int count)
Shift x[x_start:x_start+len-1] count bits to the "right" (i.e. divide by 2**count). Store the len least significant words of the result at dest. OK if dest==x. Assumes: 0 <= count < 32 Same as rshift, but handles count==0 (and has no return value).

rshift_long

public static long rshift_long(int[] x,
                               int len,
                               int count)
Return the long-truncated value of right shifting.
Parameters:
x - a two's-complement "bignum"
len - the number of significant words in x
count - the shift count
Returns:
(long)(x[0..len-1] >> count).

set_str

public static int set_str(dest[] ,
                          byte[] str,
                          int str_len,
                          int base)

sub_n

public static int sub_n(int[] dest,
                        int[] X,
                        int[] Y,
                        int size)
Subtract Y[0:size-1] from X[0:size-1], and write the size least significant words of the result to dest[0:size-1]. Return borrow, either 0 or 1. This is basically the same as gmp's mpn_sub_n function.

submul_1

public static int submul_1(int[] dest,
                           int offset,
                           int[] x,
                           int len,
                           int y)

udiv_qrnnd

public static long udiv_qrnnd(long N,
                              int D)

gnu.java.math.MPN Copyright (C) 1999, 2000, 2001, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.