diff --git a/src/libgap-api.c b/src/libgap-api.c index 36dea6e2a0..a7f3d25d2e 100644 --- a/src/libgap-api.c +++ b/src/libgap-api.c @@ -4,12 +4,13 @@ #include "ariths.h" #include "bool.h" -#include "opers.h" #include "calls.h" #include "gap.h" #include "gapstate.h" #include "gvars.h" +#include "integer.h" #include "lists.h" +#include "opers.h" #include "plist.h" #include "streams.h" #include "stringobj.h" @@ -193,6 +194,47 @@ Obj GAP_CallFuncArray(Obj func, UInt narg, Obj args[]) } +//// +//// integers +//// + +int GAP_IsInt(Obj obj) +{ + return obj && IS_INT(obj); +} + +int GAP_IsSmallInt(Obj obj) +{ + return obj && IS_INTOBJ(obj); +} + +int GAP_IsLargeInt(Obj obj) +{ + return obj && IS_LARGEINT(obj); +} + +Obj GAP_MakeObjInt(const UInt * limbs, Int size) +{ + return MakeObjInt(limbs, size); +} + +Int GAP_SizeInt(Obj obj) +{ + RequireInt("GAP_SizeInt", obj, "obj"); + if (obj == INTOBJ_INT(0)) + return 0; + Int size = (IS_INTOBJ(obj) ? 1 : SIZE_INT(obj)); + return IS_POS_INT(obj) ? size : -size; +} + +const UInt * GAP_AddrInt(Obj obj) +{ + if (obj && IS_LARGEINT(obj)) + return CONST_ADDR_INT(obj); + else + return 0; +} + //// //// lists //// diff --git a/src/libgap-api.h b/src/libgap-api.h index e08dc41f69..27a10a9c97 100644 --- a/src/libgap-api.h +++ b/src/libgap-api.h @@ -82,6 +82,49 @@ extern Obj GAP_CallFuncList(Obj func, Obj args); extern Obj GAP_CallFuncArray(Obj func, UInt narg, Obj args[]); +//// +//// integers +//// + +// Returns 1 if is a GAP integer, 0 if not. +extern int GAP_IsInt(Obj obj); + +// Returns 1 if is a GAP small (aka immediate) integer, 0 if not. +extern int GAP_IsSmallInt(Obj obj); + +// Returns 1 if is a GAP large integer, 0 if not. +extern int GAP_IsLargeInt(Obj obj); + + +// Construct an integer object from the limbs at which points (for a +// definition of "limbs", please consult the comment at the top of +// `integer.c`). The absolute value of determines the number of limbs. +// If is zero, then `INTOBJ_INT(0)` is returned. Otherwise, the sign +// of the returned integer object is determined by the sign of . +// // +// Note that GAP automatically reduces and normalized the integer object, +// i.e., it will discard any leading zeros; and if the integer fits into a +// small integer, it will be returned as such. +extern Obj GAP_MakeObjInt(const UInt * limbs, Int size); + +// If is a GAP integer, returns the number of limbs needed to store the +// integer, times the sign. If is the integer 0, then 0 is returned. If +// is any other small integer, then 1 or -1 is returned, depending on +// its sign. +// +// If is not a GAP integer, an error is raised. +extern Int GAP_SizeInt(Obj obj); + +// Returns a pointer to the limbs of a the GAP large integer . +// If is not a GAP large integer, then NULL is returned. +// +// Note: The pointer returned by this function is only valid until the next +// GAP garbage collection. In particular, if you use any GAP APIs, then you +// should assume that the pointer became stale. Barring that, you may safely +// copy, inspect, or even modify the content of the string buffer. +extern const UInt * GAP_AddrInt(Obj obj); + + //// //// lists ////