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
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef USE_GMP
|
|
|
|
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
#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
|
|
|
{
|
2006-01-02 02:16:19 +00:00
|
|
|
Int nlimbs;
|
2008-11-28 15:54:46 +00:00
|
|
|
MP_INT *dst = (MP_INT *)(H+2);
|
2006-01-02 02:16:19 +00:00
|
|
|
CELL *ret = H;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
nlimbs = (big->_mp_alloc)*(sizeof(mp_limb_t)/CellSize);
|
|
|
|
if (nlimbs > (ASP-ret)-1024) {
|
|
|
|
return TermNil;
|
|
|
|
}
|
|
|
|
H[0] = (CELL)FunctorBigInt;
|
2008-11-28 15:54:46 +00:00
|
|
|
H[1] = BIG_INT;
|
2006-01-02 02:25:45 +00:00
|
|
|
|
|
|
|
dst->_mp_size = big->_mp_size;
|
|
|
|
dst->_mp_alloc = big->_mp_alloc;
|
|
|
|
memmove((void *)(dst+1), (const void *)(big->_mp_d), nlimbs*CellSize);
|
|
|
|
H = (CELL *)(dst+1)+nlimbs;
|
2006-08-22 17:12:46 +01:00
|
|
|
H[0] = EndSpecials;
|
2006-01-02 02:25:45 +00:00
|
|
|
H++;
|
|
|
|
return AbsAppl(ret);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
Int nlimbs;
|
|
|
|
MP_INT *dst = (MP_INT *)(H+2);
|
|
|
|
MP_INT *num = mpq_numref(big);
|
|
|
|
MP_INT *den = mpq_denref(big);
|
|
|
|
MP_RAT *rat;
|
|
|
|
CELL *ret = H;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
H[0] = (CELL)FunctorBigInt;
|
|
|
|
H[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;
|
|
|
|
H = (CELL *)(rat+1)+nlimbs;
|
|
|
|
nlimbs = (den->_mp_alloc)*(sizeof(mp_limb_t)/CellSize);
|
|
|
|
memmove((void *)(H), (const void *)(den->_mp_d), nlimbs*CellSize);
|
|
|
|
H += nlimbs;
|
2010-05-28 09:53:56 +01:00
|
|
|
dst->_mp_alloc = (H-(CELL *)(dst+1));
|
2010-05-27 12:24:15 +01:00
|
|
|
H[0] = EndSpecials;
|
|
|
|
H++;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-05-28 09:53:56 +01:00
|
|
|
Term
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-05-27 12:24:15 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
|
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);
|
2005-11-15 02:05:49 +00:00
|
|
|
#else
|
|
|
|
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)) {
|
|
|
|
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
|
|
|
|
return MkIntegerTerm(n);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Int
|
|
|
|
p_is_bignum(void)
|
|
|
|
{
|
|
|
|
#ifdef USE_GMP
|
|
|
|
Term t = Deref(ARG1);
|
2008-11-28 15:54:46 +00:00
|
|
|
return(
|
2010-05-28 09:53:56 +01:00
|
|
|
IsNonVarTerm(t) &&
|
|
|
|
IsApplTerm(t) &&
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_has_bignums(void)
|
|
|
|
{
|
|
|
|
#ifdef USE_GMP
|
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-05-28 09:53:56 +01:00
|
|
|
static Int
|
|
|
|
p_is_rational(void)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_rational(void)
|
|
|
|
{
|
|
|
|
#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)) {
|
|
|
|
Yap_Error(OUT_OF_STACK_ERROR, t, Yap_ErrorMessage);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
Yap_unify(ARG2, t1) &&
|
|
|
|
Yap_unify(ARG3, t2);
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-06-19 00:38:49 +01:00
|
|
|
Term
|
|
|
|
Yap_MkBlobStringTerm(const char *s, size_t len)
|
|
|
|
{
|
|
|
|
CELL *ret = H;
|
|
|
|
size_t sz;
|
|
|
|
MP_INT *dst = (MP_INT *)(H+2);
|
|
|
|
blob_string_t *sp;
|
|
|
|
size_t siz;
|
|
|
|
|
|
|
|
sz = strlen(s);
|
|
|
|
if (len > 0 && sz > len) sz = len;
|
|
|
|
if (len/sizeof(CELL) > (ASP-ret)-1024) {
|
|
|
|
return TermNil;
|
|
|
|
}
|
|
|
|
H[0] = (CELL)FunctorBigInt;
|
|
|
|
H[1] = BLOB_STRING;
|
|
|
|
|
|
|
|
siz = (sizeof(size_t)+len+sizeof(CELL))/sizeof(CELL);
|
|
|
|
dst->_mp_size = siz;
|
|
|
|
dst->_mp_alloc = 0L;
|
|
|
|
sp = (blob_string_t *)(dst+1);
|
|
|
|
sp->len = sz;
|
|
|
|
strncpy((char *)(sp+1), s, sz);
|
|
|
|
H += siz;
|
|
|
|
H[0] = EndSpecials;
|
|
|
|
H++;
|
|
|
|
return AbsAppl(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
Term
|
|
|
|
Yap_MkBlobWideStringTerm(const wchar_t *s, size_t len)
|
|
|
|
{
|
|
|
|
CELL *ret = H;
|
|
|
|
size_t sz;
|
|
|
|
MP_INT *dst = (MP_INT *)(H+2);
|
|
|
|
blob_string_t *sp;
|
|
|
|
size_t siz;
|
|
|
|
|
|
|
|
sz = wcslen(s);
|
|
|
|
if (len > 0 && sz > len) sz = len;
|
|
|
|
if (len/sizeof(CELL) > (ASP-ret)-1024) {
|
|
|
|
return TermNil;
|
|
|
|
}
|
|
|
|
H[0] = (CELL)FunctorBigInt;
|
|
|
|
H[1] = BLOB_WIDE_STRING;
|
|
|
|
|
|
|
|
siz = (sizeof(size_t)+(len+2)*sizeof(wchar_t))/sizeof(CELL);
|
|
|
|
dst->_mp_size = siz;
|
|
|
|
dst->_mp_alloc = 0L;
|
|
|
|
sp = (blob_string_t *)(dst+1);
|
|
|
|
sp->len = sz;
|
|
|
|
wcsncpy((wchar_t *)(sp+1), s, sz);
|
|
|
|
H += siz;
|
|
|
|
H[0] = EndSpecials;
|
|
|
|
H++;
|
|
|
|
return AbsAppl(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
Yap_BlobStringOfTerm(Term t)
|
|
|
|
{
|
|
|
|
blob_string_t *new = (blob_string_t *)(RepAppl(t)+2+sizeof(MP_INT)/sizeof(CELL));
|
|
|
|
return (char *)(new+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t *
|
|
|
|
Yap_BlobWideStringOfTerm(Term t)
|
|
|
|
{
|
|
|
|
blob_string_t *new = (blob_string_t *)(RepAppl(t)+2+sizeof(MP_INT)/sizeof(CELL));
|
|
|
|
return (wchar_t *)(new+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
Yap_BlobStringOfTermAndLength(Term t, size_t *sp)
|
|
|
|
{
|
|
|
|
blob_string_t *new = (blob_string_t *)(RepAppl(t)+2+sizeof(MP_INT)/sizeof(CELL));
|
|
|
|
*sp = new->len;
|
|
|
|
return (char *)(new+1);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2007-08-02 18:22:00 +01:00
|
|
|
Yap_InitCPred("$has_bignums", 0, p_has_bignums, SafePredFlag|HiddenPredFlag);
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$bignum", 1, p_is_bignum, SafePredFlag|HiddenPredFlag);
|
2010-05-28 09:53:56 +01:00
|
|
|
Yap_InitCPred("rational", 3, p_rational, 0);
|
|
|
|
Yap_InitCPred("rational", 1, p_is_rational, SafePredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|