From b8653cd04a54d4bbb94b1e8c743cc7a411ccc85c Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 14 Nov 2018 15:45:51 +0100 Subject: [PATCH 1/2] Add some integer related functions to libgap API --- src/libgap-api.c | 44 +++++++++++++++++++++++++++++++++++++++++++- src/libgap-api.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/src/libgap-api.c b/src/libgap-api.c index 36dea6e2a0..eb04c4e232 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_IsLargInt(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..a572a78ff3 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_IsLargInt(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 //// From aad523ac4761cce6672b20992f574c2472deefa1 Mon Sep 17 00:00:00 2001 From: Sebastian Gutsche Date: Wed, 14 Nov 2018 15:49:24 +0100 Subject: [PATCH 2/2] Fix typo GAP_IsLargInt -> GAP_IsLargeInt Co-Authored-By: fingolfin --- src/libgap-api.c | 2 +- src/libgap-api.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libgap-api.c b/src/libgap-api.c index eb04c4e232..a7f3d25d2e 100644 --- a/src/libgap-api.c +++ b/src/libgap-api.c @@ -208,7 +208,7 @@ int GAP_IsSmallInt(Obj obj) return obj && IS_INTOBJ(obj); } -int GAP_IsLargInt(Obj obj) +int GAP_IsLargeInt(Obj obj) { return obj && IS_LARGEINT(obj); } diff --git a/src/libgap-api.h b/src/libgap-api.h index a572a78ff3..27a10a9c97 100644 --- a/src/libgap-api.h +++ b/src/libgap-api.h @@ -93,7 +93,7 @@ extern int GAP_IsInt(Obj obj); extern int GAP_IsSmallInt(Obj obj); // Returns 1 if is a GAP large integer, 0 if not. -extern int GAP_IsLargInt(Obj obj); +extern int GAP_IsLargeInt(Obj obj); // Construct an integer object from the limbs at which points (for a