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 some integer related functions to libgap API #2998

Merged
merged 2 commits into from
Nov 15, 2018
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
44 changes: 43 additions & 1 deletion src/libgap-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
////
Expand Down
43 changes: 43 additions & 0 deletions src/libgap-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <obj> is a GAP integer, 0 if not.
extern int GAP_IsInt(Obj obj);

// Returns 1 if <obj> is a GAP small (aka immediate) integer, 0 if not.
extern int GAP_IsSmallInt(Obj obj);

// Returns 1 if <obj> is a GAP large integer, 0 if not.
extern int GAP_IsLargeInt(Obj obj);


// Construct an integer object from the limbs at which <limbs> points (for a
// definition of "limbs", please consult the comment at the top of
// `integer.c`). The absolute value of <size> determines the number of limbs.
// If <size> is zero, then `INTOBJ_INT(0)` is returned. Otherwise, the sign
// of the returned integer object is determined by the sign of <size>.
// //
// 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 <obj> is a GAP integer, returns the number of limbs needed to store the
// integer, times the sign. If <obj> is the integer 0, then 0 is returned. If
// <obj> is any other small integer, then 1 or -1 is returned, depending on
// its sign.
//
// If <obj> 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 <obj>.
// If <obj> 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
////
Expand Down