2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: arith1.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: bignum support through gmp *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
#include "Yatom.h"
|
|
|
|
|
2010-07-19 22:42:47 +01:00
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2011-07-22 12:09:33 +01:00
|
|
|
#include "YapHeap.h"
|
2015-09-21 23:05:36 +01:00
|
|
|
#include "YapText.h"
|
2011-07-22 12:09:33 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef USE_GMP
|
|
|
|
|
|
|
|
#include "eval.h"
|
|
|
|
#include "alloc.h"
|
|
|
|
|
|
|
|
Term
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_MkBigIntTerm(MP_INT *big)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2006-01-02 02:16:19 +00:00
|
|
|
Int nlimbs;
|
2014-01-19 21:15:05 +00:00
|
|
|
MP_INT *dst = (MP_INT *)(HR+2);
|
|
|
|
CELL *ret = HR;
|
2012-02-06 15:10:48 +00:00
|
|
|
Int bytes;
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2006-01-16 02:57:52 +00:00
|
|
|
if (mpz_fits_slong_p(big)) {
|
2006-02-01 13:28:57 +00:00
|
|
|
long int out = mpz_get_si(big);
|
|
|
|
return MkIntegerTerm((Int)out);
|
2006-01-02 02:25:45 +00:00
|
|
|
}
|
2012-02-06 15:10:48 +00:00
|
|
|
// bytes = big->_mp_alloc * sizeof(mp_limb_t);
|
|
|
|
// nlimbs = ALIGN_YAPTYPE(bytes,CELL)/CellSize;
|
|
|
|
// this works, but it shouldn't need to do this...
|
|
|
|
nlimbs = big->_mp_alloc;
|
|
|
|
bytes = nlimbs*sizeof(CELL);
|
2006-01-02 02:25:45 +00:00
|
|
|
if (nlimbs > (ASP-ret)-1024) {
|
|
|
|
return TermNil;
|
|
|
|
}
|
2014-01-19 21:15:05 +00:00
|
|
|
HR[0] = (CELL)FunctorBigInt;
|
|
|
|
HR[1] = BIG_INT;
|
2006-01-02 02:25:45 +00:00
|
|
|
|
|
|
|
dst->_mp_size = big->_mp_size;
|
2012-02-06 15:10:48 +00:00
|
|
|
dst->_mp_alloc = nlimbs*(CellSize/sizeof(mp_limb_t));
|
|
|
|
memmove((void *)(dst+1), (const void *)(big->_mp_d), bytes);
|
2014-01-19 21:15:05 +00:00
|
|
|
HR = (CELL *)(dst+1)+nlimbs;
|
|
|
|
HR[0] = EndSpecials;
|
|
|
|
HR++;
|
2011-07-21 10:24:21 +01:00
|
|
|
return AbsAppl(ret);
|
|
|
|
}
|
|
|
|
|
2006-01-02 02:25:45 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
MP_INT *
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_BigIntOfTerm(Term t)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2008-11-28 15:54:46 +00:00
|
|
|
MP_INT *new = (MP_INT *)(RepAppl(t)+2);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
new->_mp_d = (mp_limb_t *)(new+1);
|
|
|
|
return(new);
|
|
|
|
}
|
|
|
|
|
2010-05-27 12:24:15 +01:00
|
|
|
Term
|
|
|
|
Yap_MkBigRatTerm(MP_RAT *big)
|
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2010-05-27 12:24:15 +01:00
|
|
|
Int nlimbs;
|
2014-01-19 21:15:05 +00:00
|
|
|
MP_INT *dst = (MP_INT *)(HR+2);
|
2010-05-27 12:24:15 +01:00
|
|
|
MP_INT *num = mpq_numref(big);
|
|
|
|
MP_INT *den = mpq_denref(big);
|
|
|
|
MP_RAT *rat;
|
2014-01-19 21:15:05 +00:00
|
|
|
CELL *ret = HR;
|
2010-05-27 12:24:15 +01:00
|
|
|
|
|
|
|
if (mpz_cmp_si(den, 1) == 0)
|
|
|
|
return Yap_MkBigIntTerm(num);
|
|
|
|
if ((num->_mp_alloc+den->_mp_alloc)*(sizeof(mp_limb_t)/CellSize) > (ASP-ret)-1024) {
|
|
|
|
return TermNil;
|
|
|
|
}
|
2014-01-19 21:15:05 +00:00
|
|
|
HR[0] = (CELL)FunctorBigInt;
|
|
|
|
HR[1] = BIG_RATIONAL;
|
2010-05-28 09:53:56 +01:00
|
|
|
dst->_mp_size = 0;
|
2010-05-27 12:24:15 +01:00
|
|
|
rat = (MP_RAT *)(dst+1);
|
|
|
|
rat->_mp_num._mp_size = num->_mp_size;
|
|
|
|
rat->_mp_num._mp_alloc = num->_mp_alloc;
|
|
|
|
nlimbs = (num->_mp_alloc)*(sizeof(mp_limb_t)/CellSize);
|
|
|
|
memmove((void *)(rat+1), (const void *)(num->_mp_d), nlimbs*CellSize);
|
|
|
|
rat->_mp_den._mp_size = den->_mp_size;
|
|
|
|
rat->_mp_den._mp_alloc = den->_mp_alloc;
|
2014-01-19 21:15:05 +00:00
|
|
|
HR = (CELL *)(rat+1)+nlimbs;
|
2010-05-27 12:24:15 +01:00
|
|
|
nlimbs = (den->_mp_alloc)*(sizeof(mp_limb_t)/CellSize);
|
2014-01-19 21:15:05 +00:00
|
|
|
memmove((void *)(HR), (const void *)(den->_mp_d), nlimbs*CellSize);
|
|
|
|
HR += nlimbs;
|
|
|
|
dst->_mp_alloc = (HR-(CELL *)(dst+1));
|
|
|
|
HR[0] = EndSpecials;
|
|
|
|
HR++;
|
2010-05-27 12:24:15 +01:00
|
|
|
return AbsAppl(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
MP_RAT *
|
|
|
|
Yap_BigRatOfTerm(Term t)
|
|
|
|
{
|
|
|
|
MP_RAT *new = (MP_RAT *)(RepAppl(t)+2+sizeof(MP_INT)/sizeof(CELL));
|
|
|
|
mp_limb_t *nt;
|
|
|
|
|
|
|
|
nt = new->_mp_num._mp_d = (mp_limb_t *)(new+1);
|
|
|
|
nt += new->_mp_num._mp_alloc;
|
|
|
|
new->_mp_den._mp_d = nt;
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2015-04-13 13:15:30 +01:00
|
|
|
Term
|
2010-05-28 09:53:56 +01:00
|
|
|
Yap_RatTermToApplTerm(Term t)
|
|
|
|
{
|
|
|
|
Term ts[2];
|
|
|
|
MP_RAT *rat = Yap_BigRatOfTerm(t);
|
|
|
|
|
|
|
|
ts[0] = Yap_MkBigIntTerm(mpq_numref(rat));
|
|
|
|
ts[1] = Yap_MkBigIntTerm(mpq_denref(rat));
|
|
|
|
return Yap_MkApplTerm(FunctorRDiv,2,ts);
|
|
|
|
}
|
|
|
|
|
2011-07-22 12:09:33 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
Term
|
|
|
|
Yap_AllocExternalDataInStack(CELL tag, size_t bytes)
|
|
|
|
{
|
|
|
|
CACHE_REGS
|
|
|
|
Int nlimbs;
|
2014-01-19 21:15:05 +00:00
|
|
|
MP_INT *dst = (MP_INT *)(HR+2);
|
|
|
|
CELL *ret = HR;
|
2011-07-22 12:09:33 +01:00
|
|
|
|
2015-01-20 03:00:42 +00:00
|
|
|
nlimbs = ALIGN_BY_TYPE(bytes,CELL)/CellSize;
|
2011-07-22 12:09:33 +01:00
|
|
|
if (nlimbs > (ASP-ret)-1024) {
|
|
|
|
return TermNil;
|
|
|
|
}
|
2014-01-19 21:15:05 +00:00
|
|
|
HR[0] = (CELL)FunctorBigInt;
|
|
|
|
HR[1] = tag;
|
2011-07-22 12:09:33 +01:00
|
|
|
dst->_mp_size = 0;
|
|
|
|
dst->_mp_alloc = nlimbs;
|
2014-01-19 21:15:05 +00:00
|
|
|
HR = (CELL *)(dst+1)+nlimbs;
|
|
|
|
HR[0] = EndSpecials;
|
|
|
|
HR++;
|
2011-07-22 12:09:33 +01:00
|
|
|
if (tag != EXTERNAL_BLOB) {
|
|
|
|
TrailTerm(TR) = AbsPair(ret);
|
|
|
|
TR++;
|
|
|
|
}
|
|
|
|
return AbsAppl(ret);
|
|
|
|
}
|
2010-05-27 12:24:15 +01:00
|
|
|
|
2011-07-22 12:09:33 +01:00
|
|
|
int Yap_CleanOpaqueVariable(CELL *pt)
|
|
|
|
{
|
|
|
|
CELL blob_info, blob_tag;
|
|
|
|
MP_INT *blobp;
|
|
|
|
#ifdef DEBUG
|
|
|
|
/* sanity checking */
|
|
|
|
if (pt[0] != (CELL)FunctorBigInt) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "CleanOpaqueVariable bad call");
|
2011-07-22 12:09:33 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2011-07-22 12:09:33 +01:00
|
|
|
blob_tag = pt[1];
|
|
|
|
if (blob_tag < USER_BLOB_START ||
|
|
|
|
blob_tag >= USER_BLOB_END) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_INTERNAL, AbsAppl(pt), "clean opaque: bad blob with tag " UInt_FORMAT ,blob_tag);
|
2011-07-22 12:09:33 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
blob_info = blob_tag - USER_BLOB_START;
|
|
|
|
if (!GLOBAL_OpaqueHandlers)
|
|
|
|
return FALSE;
|
|
|
|
blobp = (MP_INT *)(pt+2);
|
2011-07-22 12:21:21 +01:00
|
|
|
if (!GLOBAL_OpaqueHandlers[blob_info].fail_handler)
|
|
|
|
return TRUE;
|
2011-07-22 12:09:33 +01:00
|
|
|
return (GLOBAL_OpaqueHandlers[blob_info].fail_handler)((void *)(blobp+1));
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-07-27 16:50:14 +01:00
|
|
|
Opaque_CallOnWrite
|
2015-06-18 00:22:32 +01:00
|
|
|
Yap_blob_write_handler(Term t)
|
2011-07-27 16:50:14 +01:00
|
|
|
{
|
|
|
|
CELL blob_info, blob_tag;
|
|
|
|
CELL *pt = RepAppl(t);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
/* sanity checking */
|
|
|
|
if (pt[0] != (CELL)FunctorBigInt) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "CleanOpaqueVariable bad call");
|
2011-07-27 16:50:14 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
blob_tag = pt[1];
|
|
|
|
if (blob_tag < USER_BLOB_START ||
|
|
|
|
blob_tag >= USER_BLOB_END) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_INTERNAL, AbsAppl(pt), "clean opaque: bad blob with tag " UInt_FORMAT ,blob_tag);
|
2011-07-27 16:50:14 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
blob_info = blob_tag - USER_BLOB_START;
|
2011-12-13 10:01:51 +00:00
|
|
|
if (!GLOBAL_OpaqueHandlers) {
|
2011-07-27 16:50:14 +01:00
|
|
|
return NULL;
|
2011-12-13 10:01:51 +00:00
|
|
|
}
|
2011-07-27 16:50:14 +01:00
|
|
|
return GLOBAL_OpaqueHandlers[blob_info].write_handler;
|
|
|
|
}
|
|
|
|
|
2011-12-13 10:01:51 +00:00
|
|
|
Opaque_CallOnGCMark
|
|
|
|
Yap_blob_gc_mark_handler(Term t)
|
|
|
|
{
|
|
|
|
CELL blob_info, blob_tag;
|
|
|
|
CELL *pt = RepAppl(t);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
/* sanity checking */
|
|
|
|
if (pt[0] != (CELL)FunctorBigInt) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "CleanOpaqueVariable bad call");
|
2011-12-13 10:01:51 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
blob_tag = pt[1];
|
|
|
|
if (blob_tag < USER_BLOB_START ||
|
|
|
|
blob_tag >= USER_BLOB_END) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
blob_info = blob_tag - USER_BLOB_START;
|
|
|
|
if (!GLOBAL_OpaqueHandlers)
|
|
|
|
return NULL;
|
|
|
|
return GLOBAL_OpaqueHandlers[blob_info].gc_mark_handler;
|
|
|
|
}
|
|
|
|
|
2011-12-13 18:14:33 +00:00
|
|
|
Opaque_CallOnGCRelocate
|
|
|
|
Yap_blob_gc_relocate_handler(Term t)
|
2011-12-13 10:01:51 +00:00
|
|
|
{
|
|
|
|
CELL blob_info, blob_tag;
|
|
|
|
CELL *pt = RepAppl(t);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
/* sanity checking */
|
|
|
|
if (pt[0] != (CELL)FunctorBigInt) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "CleanOpaqueVariable bad call");
|
2011-12-13 10:01:51 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
blob_tag = pt[1];
|
|
|
|
if (blob_tag < USER_BLOB_START ||
|
|
|
|
blob_tag >= USER_BLOB_END) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_INTERNAL, AbsAppl(pt), "clean opaque: bad blob with tag " UInt_FORMAT ,blob_tag);
|
2011-12-13 10:01:51 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
blob_info = blob_tag - USER_BLOB_START;
|
|
|
|
if (!GLOBAL_OpaqueHandlers)
|
|
|
|
return NULL;
|
2011-12-13 18:14:33 +00:00
|
|
|
return GLOBAL_OpaqueHandlers[blob_info].gc_relocate_handler;
|
2011-12-13 10:01:51 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 00:22:32 +01:00
|
|
|
extern Int Yap_blob_tag(Term t)
|
2011-07-27 16:50:14 +01:00
|
|
|
{
|
|
|
|
CELL *pt = RepAppl(t);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
/* sanity checking */
|
|
|
|
if (pt[0] != (CELL)FunctorBigInt) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "CleanOpaqueVariable bad call");
|
2011-07-27 16:50:14 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return pt[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2015-06-18 00:22:32 +01:00
|
|
|
Yap_blob_info(Term t)
|
2011-07-27 16:50:14 +01:00
|
|
|
{
|
|
|
|
MP_INT *blobp;
|
|
|
|
CELL *pt = RepAppl(t);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
/* sanity checking */
|
|
|
|
if (pt[0] != (CELL)FunctorBigInt) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "CleanOpaqueVariable bad call");
|
2011-07-27 16:50:14 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (!GLOBAL_OpaqueHandlers)
|
|
|
|
return FALSE;
|
|
|
|
blobp = (MP_INT *)(pt+2);
|
|
|
|
return (void *)(blobp+1);
|
|
|
|
}
|
|
|
|
|
2003-03-20 15:10:18 +00:00
|
|
|
Term
|
|
|
|
Yap_MkULLIntTerm(YAP_ULONG_LONG n)
|
|
|
|
{
|
2005-11-16 02:01:09 +00:00
|
|
|
#if __GNUC__ && USE_GMP
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_t new;
|
2005-11-15 02:05:49 +00:00
|
|
|
char tmp[256];
|
2006-01-02 02:16:19 +00:00
|
|
|
Term t;
|
2005-11-15 02:05:49 +00:00
|
|
|
|
2010-02-10 09:03:03 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
snprintf(tmp,256,"%I64u",n);
|
|
|
|
#elif HAVE_SNPRINTF
|
2005-11-16 01:55:03 +00:00
|
|
|
snprintf(tmp,256,"%llu",n);
|
2015-04-13 13:15:30 +01:00
|
|
|
#else
|
2005-11-15 02:05:49 +00:00
|
|
|
sprintf(tmp,"%llu",n);
|
|
|
|
#endif
|
|
|
|
/* try to scan it as a bignum */
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_init_set_str (new, tmp, 10);
|
2005-11-15 02:05:49 +00:00
|
|
|
if (mpz_fits_slong_p(new)) {
|
2013-03-26 20:01:52 +00:00
|
|
|
CACHE_REGS
|
2005-11-15 02:05:49 +00:00
|
|
|
return MkIntegerTerm(mpz_get_si(new));
|
|
|
|
}
|
2006-01-02 02:16:19 +00:00
|
|
|
t = Yap_MkBigIntTerm(new);
|
2006-01-18 15:34:54 +00:00
|
|
|
mpz_clear(new);
|
2006-01-02 02:16:19 +00:00
|
|
|
return t;
|
2003-03-20 15:10:18 +00:00
|
|
|
#else
|
2013-06-04 04:03:59 +01:00
|
|
|
CACHE_REGS
|
2003-03-20 15:10:18 +00:00
|
|
|
return MkIntegerTerm(n);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-01-17 09:39:29 +00:00
|
|
|
CELL *
|
|
|
|
Yap_HeapStoreOpaqueTerm(Term t)
|
|
|
|
{
|
|
|
|
CELL *ptr = RepAppl(t);
|
|
|
|
size_t sz;
|
|
|
|
void *new;
|
|
|
|
|
|
|
|
if (ptr[0] == (CELL)FunctorBigInt) {
|
|
|
|
sz = sizeof(MP_INT)+2*CellSize+
|
|
|
|
((MP_INT *)(ptr+2))->_mp_alloc*sizeof(mp_limb_t);
|
|
|
|
} else { /* string */
|
|
|
|
sz = sizeof(CELL)*(2+ptr[1]);
|
|
|
|
}
|
|
|
|
new = Yap_AllocCodeSpace(sz);
|
|
|
|
if (!new) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(RESOURCE_ERROR_HEAP, TermNil, "subgoal_search_loop: no space for %s", StringOfTerm(t) );
|
2014-01-17 09:39:29 +00:00
|
|
|
} else {
|
|
|
|
if (ptr[0] == (CELL)FunctorBigInt) {
|
|
|
|
MP_INT *new = (MP_INT *)(RepAppl(t)+2);
|
|
|
|
|
|
|
|
new->_mp_d = (mp_limb_t *)(new+1);
|
|
|
|
}
|
|
|
|
memmove(new, ptr, sz);
|
|
|
|
}
|
2015-04-13 13:15:30 +01:00
|
|
|
return new;
|
2014-01-17 09:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t
|
|
|
|
Yap_OpaqueTermToString(Term t, char *str, size_t max)
|
|
|
|
{
|
|
|
|
size_t str_index = 0;
|
|
|
|
CELL * li = RepAppl(t);
|
|
|
|
if (li[0] == (CELL)FunctorString) {
|
|
|
|
str_index += sprintf(& str[str_index], "\"");
|
|
|
|
do {
|
2015-09-21 23:05:36 +01:00
|
|
|
utf8proc_int32_t chr;
|
|
|
|
unsigned char *ptr = (unsigned char *)StringOfTerm(AbsAppl(li));
|
|
|
|
ptr += get_utf8(ptr, &chr);
|
2014-01-17 09:39:29 +00:00
|
|
|
if (chr == '\0') break;
|
2015-09-21 23:05:36 +01:00
|
|
|
str_index += sprintf(str+str_index, "%C", chr);
|
2014-01-17 09:39:29 +00:00
|
|
|
} while (TRUE);
|
2015-09-21 23:05:36 +01:00
|
|
|
str_index += sprintf(str+str_index, "\"");
|
2014-01-17 09:39:29 +00:00
|
|
|
} else {
|
|
|
|
CELL big_tag = li[1];
|
|
|
|
|
|
|
|
if (big_tag == ARRAY_INT || big_tag == ARRAY_FLOAT) {
|
|
|
|
str_index += sprintf(& str[str_index], "{...}");
|
|
|
|
#ifdef USE_GMP
|
|
|
|
} else if (big_tag == BIG_INT) {
|
|
|
|
MP_INT *big = Yap_BigIntOfTerm(AbsAppl(li));
|
|
|
|
char *s = mpz_get_str(&str[str_index], 10, big);
|
|
|
|
str_index += strlen(&s[str_index]);
|
|
|
|
} else if (big_tag == BIG_RATIONAL) {
|
|
|
|
MP_RAT *big = Yap_BigRatOfTerm(AbsAppl(li));
|
|
|
|
char *s = mpq_get_str(&str[str_index], 10, big);
|
|
|
|
str_index += strlen(&s[str_index]);
|
|
|
|
#endif
|
2015-04-13 13:15:30 +01:00
|
|
|
}
|
2014-01-17 09:39:29 +00:00
|
|
|
/*
|
|
|
|
else if (big_tag >= USER_BLOB_START && big_tag < USER_BLOB_END) {
|
|
|
|
Opaque_CallOnWrite f;
|
|
|
|
CELL blob_info;
|
2015-04-13 13:15:30 +01:00
|
|
|
|
2014-01-17 09:39:29 +00:00
|
|
|
blob_info = big_tag - USER_BLOB_START;
|
|
|
|
if (GLOBAL_OpaqueHandlers &&
|
|
|
|
(f= GLOBAL_OpaqueHandlers[blob_info].write_handler)) {
|
|
|
|
(f)(wglb->stream, big_tag, ExternalBlobFromTerm(t), 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} */
|
|
|
|
str_index += sprintf(& str[str_index], "0");
|
|
|
|
}
|
|
|
|
return str_index;
|
|
|
|
}
|
|
|
|
|
2015-04-13 13:15:30 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_is_bignum( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
#ifdef USE_GMP
|
|
|
|
Term t = Deref(ARG1);
|
2008-11-28 15:54:46 +00:00
|
|
|
return(
|
2015-04-13 13:15:30 +01:00
|
|
|
IsNonVarTerm(t) &&
|
|
|
|
IsApplTerm(t) &&
|
2010-05-28 09:53:56 +01:00
|
|
|
FunctorOfTerm(t) == FunctorBigInt &&
|
|
|
|
RepAppl(t)[1] == BIG_INT
|
|
|
|
);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2007-08-02 18:22:00 +01:00
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-04-13 13:15:30 +01:00
|
|
|
static Int
|
2013-12-06 23:24:01 +00:00
|
|
|
p_is_string( USES_REGS1 )
|
|
|
|
{
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
return(
|
2015-04-13 13:15:30 +01:00
|
|
|
IsNonVarTerm(t) &&
|
|
|
|
IsApplTerm(t) &&
|
2013-12-06 23:24:01 +00:00
|
|
|
FunctorOfTerm(t) == FunctorString
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-13 13:15:30 +01:00
|
|
|
static Int
|
2013-03-26 20:01:52 +00:00
|
|
|
p_nb_set_bit( USES_REGS1 )
|
|
|
|
{
|
|
|
|
#ifdef USE_GMP
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
Term ti = Deref(ARG2);
|
|
|
|
Int i;
|
|
|
|
|
|
|
|
if (!(
|
2015-04-13 13:15:30 +01:00
|
|
|
IsNonVarTerm(t) &&
|
|
|
|
IsApplTerm(t) &&
|
2013-03-26 20:01:52 +00:00
|
|
|
FunctorOfTerm(t) == FunctorBigInt &&
|
|
|
|
RepAppl(t)[1] == BIG_INT
|
|
|
|
))
|
|
|
|
return FALSE;
|
|
|
|
if (!IsIntegerTerm(ti)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!IsIntegerTerm(ti)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
i = IntegerOfTerm(ti);
|
|
|
|
if (i < 0) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
Yap_gmp_set_bit(i, t);
|
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-04-13 13:15:30 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_has_bignums( USES_REGS1 )
|
2007-08-02 18:22:00 +01:00
|
|
|
{
|
|
|
|
#ifdef USE_GMP
|
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-04-13 13:15:30 +01:00
|
|
|
static Int
|
2013-09-19 14:23:10 +01:00
|
|
|
p_is_opaque( USES_REGS1 )
|
|
|
|
{
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
if (IsVarTerm(t))
|
|
|
|
return FALSE;
|
|
|
|
if (IsApplTerm(t)) {
|
|
|
|
Functor f = FunctorOfTerm(t);
|
|
|
|
CELL *pt;
|
|
|
|
|
|
|
|
if (f != FunctorBigInt)
|
|
|
|
return FALSE;
|
|
|
|
pt = RepAppl(t);
|
|
|
|
return ( pt[1] != BIG_RATIONAL || pt[1] != BIG_INT );
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-04-13 13:15:30 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_is_rational( USES_REGS1 )
|
2010-05-28 09:53:56 +01:00
|
|
|
{
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
if (IsVarTerm(t))
|
|
|
|
return FALSE;
|
|
|
|
if (IsIntTerm(t))
|
|
|
|
return TRUE;
|
|
|
|
if (IsApplTerm(t)) {
|
|
|
|
Functor f = FunctorOfTerm(t);
|
|
|
|
CELL *pt;
|
|
|
|
|
|
|
|
if (f == FunctorLongInt)
|
|
|
|
return TRUE;
|
|
|
|
if (f != FunctorBigInt)
|
|
|
|
return FALSE;
|
|
|
|
pt = RepAppl(t);
|
|
|
|
return ( pt[1] == BIG_RATIONAL || pt[1] == BIG_INT );
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-04-13 13:15:30 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_rational( USES_REGS1 )
|
2010-05-28 09:53:56 +01:00
|
|
|
{
|
|
|
|
#ifdef USE_GMP
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
Functor f;
|
|
|
|
CELL *pt;
|
|
|
|
MP_RAT *rat;
|
|
|
|
Term t1, t2;
|
|
|
|
|
|
|
|
if (IsVarTerm(t))
|
|
|
|
return FALSE;
|
|
|
|
if (!IsApplTerm(t))
|
|
|
|
return FALSE;
|
|
|
|
f = FunctorOfTerm(t);
|
|
|
|
if (f != FunctorBigInt)
|
|
|
|
return FALSE;
|
|
|
|
pt = RepAppl(t);
|
|
|
|
if (pt[1] != BIG_RATIONAL)
|
|
|
|
return FALSE;
|
|
|
|
rat = Yap_BigRatOfTerm(t);
|
|
|
|
while ((t1 = Yap_MkBigIntTerm(mpq_numref(rat))) == TermNil ||
|
|
|
|
(t2 = Yap_MkBigIntTerm(mpq_denref(rat))) == TermNil) {
|
|
|
|
UInt size =
|
|
|
|
(mpq_numref(rat)->_mp_alloc)*(sizeof(mp_limb_t)/CellSize) +
|
|
|
|
(mpq_denref(rat)->_mp_alloc)*(sizeof(mp_limb_t)/CellSize);
|
|
|
|
if (!Yap_gcl(size, 3, ENV, P)) {
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(RESOURCE_ERROR_STACK, t, LOCAL_ErrorMessage);
|
2010-05-28 09:53:56 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2015-04-13 13:15:30 +01:00
|
|
|
return
|
2010-05-28 09:53:56 +01:00
|
|
|
Yap_unify(ARG2, t1) &&
|
|
|
|
Yap_unify(ARG3, t2);
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitBigNums(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2012-10-19 18:10:48 +01:00
|
|
|
Yap_InitCPred("$has_bignums", 0, p_has_bignums, SafePredFlag);
|
|
|
|
Yap_InitCPred("$bignum", 1, p_is_bignum, SafePredFlag);
|
2010-05-28 09:53:56 +01:00
|
|
|
Yap_InitCPred("rational", 3, p_rational, 0);
|
|
|
|
Yap_InitCPred("rational", 1, p_is_rational, SafePredFlag);
|
2015-04-13 13:15:30 +01:00
|
|
|
/** @pred rational( _T_)
|
2014-09-11 20:06:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
Checks whether `T` is a rational number.
|
|
|
|
|
2015-04-13 13:15:30 +01:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
*/
|
2013-12-06 23:24:01 +00:00
|
|
|
Yap_InitCPred("string", 1, p_is_string, SafePredFlag);
|
2013-09-19 14:23:10 +01:00
|
|
|
Yap_InitCPred("opaque", 1, p_is_opaque, SafePredFlag);
|
2013-03-26 20:01:52 +00:00
|
|
|
Yap_InitCPred("nb_set_bit", 2, p_nb_set_bit, SafePredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|