Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GMP support to ForeignFunctions #2906

Merged
merged 4 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions M2/Macaulay2/d/ffi.d
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ setupconst("nullPointer", toExpr(nullPointer()));

getMem(n:int):voidPointer := Ccode(voidPointer, "getmem(", n, ")");
getMemAtomic(n:int):voidPointer := Ccode(voidPointer, "getmem_atomic(", n, ")");
pointerSize := Ccode(int, "sizeof(void *)");

dlerror0():Expr:= buildErrorPacket(tostring(Ccode(charstar, "dlerror()")));

Expand Down Expand Up @@ -244,6 +245,14 @@ setupfun("ffiIntegerType", ffiIntegerType);

ffiIntegerAddress(e:Expr):Expr := (
when e
is x:ZZcell do (
y := newZZmutable();
set(y, x.v);
ptr := getMem(pointerSize);
Ccode(void, "*(void **)", ptr, " = ", y);
Ccode(void, "GC_REGISTER_FINALIZER(", ptr,
", (GC_finalization_proc)mpz_clear, ", y, ", 0, 0)");
toExpr(ptr))
is a:Sequence do (
if length(a) == 3 then (
when a.0
Expand Down Expand Up @@ -295,6 +304,7 @@ setupfun("ffiIntegerAddress", ffiIntegerAddress);

ffiIntegerValue(e:Expr):Expr := (
when e
is x:pointerCell do toExpr(moveToZZ(Ccode(ZZmutable, "*(mpz_ptr*)", x.v)))
is a:Sequence do (
if length(a) == 3 then (
when a.0
Expand Down Expand Up @@ -397,8 +407,6 @@ setupfun("ffiRealValue", ffiRealValue);
-- pointer types --
-------------------

pointerSize := Ccode(int, "sizeof(void *)");

setupconst("ffiPointerType", toExpr(Ccode(voidPointer, "&ffi_type_pointer")));

ffiPointerAddress(e:Expr):Expr := (
Expand Down
2 changes: 1 addition & 1 deletion M2/Macaulay2/d/gmp.d
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export moveToRRiandclear(z:RRimutable):RRi := (
clear(z);
w);

set(x:ZZmutable, y:ZZ ) ::= Ccode( void, "mpz_set (", x, ",", y, ")" );
export set(x:ZZmutable, y:ZZ ) ::= Ccode( void, "mpz_set (", x, ",", y, ")" );
set(x:ZZmutable, n:int ) ::= Ccode( void, "mpz_set_si(", x, ",", n, ")" );
set(x:ZZmutable, n:uint ) ::= Ccode( void, "mpz_set_ui(", x, ",", n, ")" );
set(x:ZZmutable, n:long ) ::= Ccode( void, "mpz_set_si(", x, ",", n, ")" );
Expand Down
34 changes: 33 additions & 1 deletion M2/Macaulay2/packages/ForeignFunctions.m2
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export {
"uint",
"long",
"ulong",
"mpzT",
"float",
"double",
"voidstar",
Expand Down Expand Up @@ -247,10 +248,16 @@ if version#"pointer size" == 4 then (
long = int64;
ulong = uint64)

mpzT = new ForeignIntegerType;
mpzT.Name = "mpz_t";
mpzT.Address = ffiPointerType
new mpzT from ZZ := (T, n) -> new T from {Address => ffiIntegerAddress n}
value mpzT := ffiIntegerValue @@ address

ForeignIntegerType Number :=
ForeignIntegerType Constant := (T, x) -> new T from truncate x

isAtomic ForeignIntegerType := T -> true
isAtomic ForeignIntegerType := T -> if T === mpzT then false else true

-----------------------
-- foreign real type --
Expand Down Expand Up @@ -844,6 +851,8 @@ doc ///
uint
long
ulong
SeeAlso
mpzT
///

doc ///
Expand Down Expand Up @@ -877,6 +886,28 @@ doc ///
short(-2.71828)
///

doc ///
Key
mpzT
(NewFromMethod, mpzT, ZZ)
(value, mpzT)
Headline
GMP arbitrary-precision integer type
Description
Text
Macaulay2's native @TO ZZ@ integer type wraps around @TT "mpz_t"@ from
@HREF{"https://gmplib.org/", "GMP"}@. This type (which is an instance
of @TO ForeignIntegerType@) allows for conversion between Macaulay2
integers and GMP integers without loss of precision.
Example
mpzT 2^100
value oo
mpzAdd = foreignFunction("__gmpz_add", void, {mpzT, mpzT, mpzT})
x = mpzT 0
mpzAdd(x, 2, 3)
x
///

doc ///
Key
ForeignRealType
Expand Down Expand Up @@ -1859,6 +1890,7 @@ longexp = 8 * version#"pointer size"
assert Equation(value ulong(2^longexp - 1), 2^longexp - 1)
assert Equation(value long(2^(longexp - 1) - 1), 2^(longexp - 1) - 1)
assert Equation(value long(-2^(longexp - 1)), -2^(longexp - 1))
assert Equation(value mpzT 10^100, 10^100)

-- real types
assert Equation(value float 3.14159, 3.14159p24)
Expand Down
Loading