Created
May 20, 2012 10:14
-
-
Save tell/2757564 to your computer and use it in GitHub Desktop.
GMP Wrapper by JNA, but this code is broken
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.tell.gmp; | |
import java.util.logging.ConsoleHandler; | |
import java.util.logging.Handler; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import com.github.tell.gmp.JNAGMP.GMPLib.mpz_struct; | |
import com.sun.jna.Library; | |
import com.sun.jna.Native; | |
import com.sun.jna.NativeLibrary; | |
import com.sun.jna.Structure; | |
import com.sun.jna.ptr.NativeLongByReference; | |
public class JNAGMP { | |
static public final Logger logger = Logger.getLogger("gmp"); | |
public interface CLibrary extends Library { | |
CLibrary INSTANCE = (CLibrary) Native.loadLibrary("c", CLibrary.class); | |
void printf(String format, Object... args); | |
} | |
static private final NativeLibrary libgmp; | |
static public final String gmpVersion; | |
public interface GMPLib extends Library { | |
public class mpz_struct extends Structure { | |
public int _mp_alloc; | |
public int _mp_size; | |
public NativeLongByReference _mp_d; | |
protected void initFieldOrder() { | |
setFieldOrder(new String[] { "_mp_alloc", "_mp_size", "_mp_d" }); | |
} | |
public mpz_struct() { | |
super(); | |
initFieldOrder(); | |
allocateMemory(); | |
ensureAllocated(); | |
logger.fine(String.format("constructed at: %s", getPointer())); | |
} | |
public static class ByReference extends mpz_struct implements | |
Structure.ByReference { | |
}; | |
public static class ByValue extends mpz_struct implements | |
Structure.ByValue { | |
}; | |
} | |
/* | |
* public class mpz_t extends PointerType { public mpz_t() { super(); | |
* mpz_struct x = new mpz_struct(); setPointer(x.getPointer()); | |
* logger.fine(String.format("constructed, it has pointer at: %s", | |
* getPointer())); } | |
* | |
* public mpz_t(Pointer ptr) { super(ptr); | |
* setPointer(ptr.getPointer(0)); | |
* logger.fine(String.format("constructed, it has pointer at: %s", | |
* getPointer())); } } | |
*/ | |
public void __gmpz_init(mpz_struct x); | |
public void __gmpz_clear(mpz_struct x); | |
public void __gmpz_set_ui(mpz_struct x, long y); | |
public void __gmpz_set_str(mpz_struct x, String numStr, int base); | |
public long __gmpz_get_ui(mpz_struct x); | |
public String __gmpz_get_str(String repStr, int base, mpz_struct x); | |
} | |
static public final GMPLib ifgmp; | |
static { | |
logger.addHandler(new ConsoleHandler()); | |
logger.setLevel(Level.FINE); | |
for (final Handler h : logger.getHandlers()) { | |
h.setLevel(Level.FINE); | |
} | |
GMPLib INSTANCE = (GMPLib) Native.loadLibrary("gmp", GMPLib.class); | |
ifgmp = (GMPLib) Native.synchronizedLibrary(INSTANCE); | |
libgmp = NativeLibrary.getInstance("gmp"); | |
gmpVersion = libgmp.getGlobalVariableAddress("__gmp_version") | |
.getStringArray(0, 1)[0]; | |
} | |
static public void main(String[] args) { | |
CLibrary i = CLibrary.INSTANCE; | |
i.printf("Hello World\n"); | |
i.printf("GMP version is %s\n", gmpVersion); | |
mpz_struct x = new mpz_struct(); | |
final int numOfLoop = 1000; | |
for (int j = 0; j < numOfLoop; j++) { | |
ifgmp.__gmpz_init(x); | |
System.out.printf("init: x = %d%n", ifgmp.__gmpz_get_ui(x)); | |
System.out.printf("init: x (str) = %s%n", | |
ifgmp.__gmpz_get_str(new String(), 10, x)); | |
ifgmp.__gmpz_set_ui(x, 10); | |
System.out.printf("set 10: x = %d%n", ifgmp.__gmpz_get_ui(x)); | |
System.out.printf("set 10: x (str) = %s%n", | |
ifgmp.__gmpz_get_str(new String(), 10, x)); | |
ifgmp.__gmpz_set_str( | |
x, | |
"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", | |
10); | |
System.out.printf("set str: x (str) = %s%n", | |
ifgmp.__gmpz_get_str(new String(), 10, x)); | |
ifgmp.__gmpz_clear(x); | |
System.out.println("next"); | |
} | |
System.out.println("finished"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment