2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
2008-12-04 23:33:32 +00: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: arith2.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: arithmetical expression evaluation *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file implements unary arithmetic operations in YAP
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
#include "Yatom.h"
|
|
|
|
#include "Heap.h"
|
|
|
|
#include "eval.h"
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
#include "arith2.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
typedef struct init_un_eval {
|
|
|
|
char *OpName;
|
|
|
|
arith2_op f;
|
|
|
|
} InitBinEntry;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
#ifdef USE_GMP
|
|
|
|
static Float
|
|
|
|
fdiv_bigint(MP_INT *b1,MP_INT *b2)
|
|
|
|
{
|
|
|
|
Float f1 = mpz_get_d(b1);
|
|
|
|
Float f2 = mpz_get_d(b2);
|
|
|
|
if (1) {
|
|
|
|
mpf_t f1,f2;
|
|
|
|
Float res;
|
|
|
|
|
|
|
|
mpf_init(f1);
|
|
|
|
mpf_init(f2);
|
|
|
|
mpf_set_z(f1, b1);
|
|
|
|
mpf_set_z(f2, b2);
|
|
|
|
mpf_div(f1, f1, f2);
|
|
|
|
res = mpf_get_d(f1);
|
|
|
|
mpf_clear(f1);
|
|
|
|
mpf_clear(f2);
|
|
|
|
return(res);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2008-12-04 23:33:32 +00:00
|
|
|
return(f1/f2);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
static Term
|
|
|
|
p_mod(Term t1, Term t2) {
|
|
|
|
switch (ETypeOfTerm(t1)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case (CELL)long_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case (CELL)long_int_e:
|
|
|
|
/* two integers */
|
|
|
|
{
|
2006-01-02 02:16:19 +00:00
|
|
|
Int i1 = IntegerOfTerm(t1);
|
2001-04-09 20:54:03 +01:00
|
|
|
Int i2 = IntegerOfTerm(t2);
|
2006-01-02 02:16:19 +00:00
|
|
|
Int mod;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
if (i2 == 0) goto zero_divisor;
|
2009-02-09 21:56:40 +00:00
|
|
|
if (i1 == Int_MIN && i2 == -1) {
|
|
|
|
return Yap_gmp_add_ints(Int_MAX, 1);
|
|
|
|
}
|
2006-01-02 02:16:19 +00:00
|
|
|
mod = i1%i2;
|
|
|
|
if (mod && (mod ^ i2) < 0)
|
|
|
|
mod += i2;
|
|
|
|
RINT(mod);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
case (CELL)double_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t2, "mod/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
/* make GCC happy */
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case (CELL)big_int_e:
|
|
|
|
/* I know the term is much larger, so: */
|
2006-01-02 02:16:19 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2006-01-02 02:16:19 +00:00
|
|
|
Int i1 = IntegerOfTerm(t1);
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set_si(&new, i1);
|
|
|
|
mpz_fdiv_r(&new, &new, Yap_BigIntOfTerm(t2));
|
|
|
|
RBIG(&new);
|
2006-01-02 02:16:19 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
|
|
|
break;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
case (CELL)double_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t2, "mod/2");
|
|
|
|
/* make GCC happy */
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
case (CELL)big_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
#ifdef USE_GMP
|
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
/* modulo between bignum and integer */
|
|
|
|
{
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_t tmp;
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2001-04-09 20:54:03 +01:00
|
|
|
Int i2 = IntegerOfTerm(t2);
|
|
|
|
|
2006-01-02 02:16:19 +00:00
|
|
|
if (i2 == 0) goto zero_divisor;
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init(&new);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_init_set_si(tmp, i2);
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_fdiv_r(&new, Yap_BigIntOfTerm(t1), tmp);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(tmp);
|
2008-12-04 23:33:32 +00:00
|
|
|
RBIG(&new);
|
2006-01-02 02:16:19 +00:00
|
|
|
}
|
|
|
|
case (CELL)big_int_e:
|
|
|
|
/* two bignums */
|
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init(&new);
|
|
|
|
mpz_fdiv_r(&new, Yap_BigIntOfTerm(t1), Yap_BigIntOfTerm(t2));
|
|
|
|
RBIG(&new);
|
2006-01-02 02:16:19 +00:00
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t2, "mod/2");
|
|
|
|
/* make GCC happy */
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
2006-01-02 02:16:19 +00:00
|
|
|
RERROR();
|
|
|
|
}
|
2006-01-02 23:19:10 +00:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
2006-01-02 02:16:19 +00:00
|
|
|
RERROR();
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
|
|
|
zero_divisor:
|
2006-01-02 02:16:19 +00:00
|
|
|
Yap_Error(EVALUATION_ERROR_ZERO_DIVISOR, t2, "X is mod 0");
|
|
|
|
/* make GCC happy */
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
static Term
|
|
|
|
p_rem(Term t1, Term t2) {
|
|
|
|
switch (ETypeOfTerm(t1)) {
|
2006-01-02 02:16:19 +00:00
|
|
|
case (CELL)long_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2006-01-02 02:16:19 +00:00
|
|
|
case (CELL)long_int_e:
|
|
|
|
/* two integers */
|
|
|
|
{
|
|
|
|
Int i1 = IntegerOfTerm(t1);
|
|
|
|
Int i2 = IntegerOfTerm(t2);
|
|
|
|
Int mod;
|
|
|
|
|
|
|
|
if (i2 == 0) goto zero_divisor;
|
2009-02-09 21:56:40 +00:00
|
|
|
if (i1 == Int_MIN && i2 == -1) {
|
|
|
|
return Yap_gmp_add_ints(Int_MAX, 1);
|
|
|
|
}
|
2006-01-02 02:16:19 +00:00
|
|
|
mod = i1%i2;
|
|
|
|
RINT(i1%i2);
|
|
|
|
}
|
|
|
|
case (CELL)double_e:
|
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t2, "mod/2");
|
|
|
|
/* make GCC happy */
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case (CELL)big_int_e:
|
|
|
|
/* I know the term is much larger, so: */
|
|
|
|
RINT(IntegerOfTerm(t1));
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
2006-01-02 02:16:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case (CELL)double_e:
|
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t1, "mod/2");
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case (CELL)big_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2006-01-02 02:16:19 +00:00
|
|
|
case long_int_e:
|
|
|
|
/* modulo between bignum and integer */
|
|
|
|
{
|
|
|
|
mpz_t tmp;
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2006-01-02 02:16:19 +00:00
|
|
|
Int i2 = IntegerOfTerm(t2);
|
|
|
|
|
|
|
|
if (i2 == 0) goto zero_divisor;
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init(&new);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_init_set_si(tmp, i2);
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_tdiv_r(&new, Yap_BigIntOfTerm(t1), tmp);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(tmp);
|
2008-12-04 23:33:32 +00:00
|
|
|
RBIG(&new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
case (CELL)big_int_e:
|
|
|
|
/* two bignums */
|
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init(&new);
|
|
|
|
mpz_tdiv_r(&new, Yap_BigIntOfTerm(t1), Yap_BigIntOfTerm(t2));
|
|
|
|
RBIG(&new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
case double_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t2, "mod/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
/* make GCC happy */
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
2006-01-02 23:19:10 +00:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
zero_divisor:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(EVALUATION_ERROR_ZERO_DIVISOR, t2, "X is mod 0");
|
2001-04-09 20:54:03 +01:00
|
|
|
/* make GCC happy */
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Floating point division: /
|
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
static Term
|
|
|
|
p_fdiv(Term t1, Term t2)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t1)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
{
|
|
|
|
Int i2 = IntegerOfTerm(t2);
|
|
|
|
|
|
|
|
/* two integers */
|
|
|
|
RFLOAT((((Float)IntegerOfTerm(t1))/(Float)i2));
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
{
|
|
|
|
/* integer, double */
|
|
|
|
Float fl1 = (Float)IntegerOfTerm(t1);
|
|
|
|
Float fl2 = FloatOfTerm(t2);
|
|
|
|
RFLOAT(fl1/fl2);
|
|
|
|
}
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case (CELL)big_int_e:
|
|
|
|
{
|
|
|
|
Int i1 = IntegerOfTerm(t1);
|
2002-11-18 18:18:05 +00:00
|
|
|
Float f2 = mpz_get_d(Yap_BigIntOfTerm(t2));
|
2003-02-24 11:01:01 +00:00
|
|
|
RFLOAT(((Float)i1/f2));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case double_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
/* float / integer */
|
|
|
|
{
|
|
|
|
Int i2 = IntegerOfTerm(t2);
|
|
|
|
RFLOAT(FloatOfTerm(t1)/(Float)i2);
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
{
|
|
|
|
Float f2 = FloatOfTerm(t2);
|
|
|
|
RFLOAT(FloatOfTerm(t1)/f2);
|
|
|
|
}
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
{
|
2002-11-18 18:18:05 +00:00
|
|
|
RFLOAT(FloatOfTerm(t1)/mpz_get_d(Yap_BigIntOfTerm(t2)));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
{
|
|
|
|
Int i = IntegerOfTerm(t2);
|
2002-11-18 18:18:05 +00:00
|
|
|
RFLOAT(mpz_get_d(Yap_BigIntOfTerm(t1))/(Float)i);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
case big_int_e:
|
|
|
|
/* two bignums*/
|
2002-11-18 18:18:05 +00:00
|
|
|
RFLOAT(fdiv_bigint(Yap_BigIntOfTerm(t1),Yap_BigIntOfTerm(t2)));
|
|
|
|
// RFLOAT(mpz_get_d(Yap_BigIntOfTerm(t1))/mpz_get_d(Yap_BigIntOfTerm(t2)));
|
2001-04-09 20:54:03 +01:00
|
|
|
case double_e:
|
|
|
|
{
|
|
|
|
Float dbl = FloatOfTerm(t2);
|
2002-11-18 18:18:05 +00:00
|
|
|
RFLOAT(mpz_get_d(Yap_BigIntOfTerm(t1))/dbl);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if USE_GMP
|
2008-04-01 09:42:46 +01:00
|
|
|
#if !defined(HAVE_MPZ_XOR)
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
|
|
|
mpz_xor(MP_INT *new, MP_INT *r1, MP_INT *r2)
|
|
|
|
{
|
2006-01-02 02:16:19 +00:00
|
|
|
MP_INT *n2, *n3;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_new(n2);
|
|
|
|
mpz_new(n3);
|
2001-04-09 20:54:03 +01:00
|
|
|
mpz_ior(new, r1, r2);
|
|
|
|
mpz_com(n2, r1);
|
|
|
|
mpz_and(n2, n2, new);
|
|
|
|
mpz_com(n3, r2);
|
|
|
|
mpz_and(n3, n3, new);
|
|
|
|
mpz_ior(new, n2, n3);
|
2006-01-02 02:16:19 +00:00
|
|
|
mpz_clear(n2);
|
|
|
|
mpz_clear(n3);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
xor #
|
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
static Term
|
|
|
|
p_xor(Term t1, Term t2)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t1)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
/* two integers */
|
|
|
|
RINT(IntegerOfTerm(t1) ^ IntegerOfTerm(t2));
|
|
|
|
case double_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t2, "#/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2009-02-09 21:56:40 +00:00
|
|
|
mpz_init_set_si(&new,IntegerOfTerm(t1));
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_xor(&new, &new, Yap_BigIntOfTerm(t2));
|
|
|
|
RBIG(&new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case double_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t1, "#/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set_si(&new,IntegerOfTerm(t2));
|
|
|
|
mpz_xor(&new, Yap_BigIntOfTerm(t1), &new);
|
|
|
|
RBIG(&new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
case big_int_e:
|
|
|
|
/* two bignums */
|
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, Yap_BigIntOfTerm(t1));
|
|
|
|
mpz_xor(&new, &new, Yap_BigIntOfTerm(t2));
|
|
|
|
RBIG(&new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
case double_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t2, "#/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
/* make GCC happy */
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
2006-01-02 23:19:10 +00:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
atan2: arc tangent x/y
|
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
static Term
|
|
|
|
p_atan2(Term t1, Term t2)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t1)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
/* two integers */
|
|
|
|
RFLOAT(atan2(IntegerOfTerm(t1),IntegerOfTerm(t2)));
|
2001-04-09 20:54:03 +01:00
|
|
|
case double_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
RFLOAT(atan2(IntegerOfTerm(t1),FloatOfTerm(t2)));
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
{
|
|
|
|
Int i1 = IntegerOfTerm(t1);
|
2002-11-18 18:18:05 +00:00
|
|
|
Float f2 = mpz_get_d(Yap_BigIntOfTerm(t2));
|
2001-04-09 20:54:03 +01:00
|
|
|
RFLOAT(atan2(i1,f2));
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
|
|
|
break;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
case double_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
/* float / integer */
|
|
|
|
{
|
|
|
|
Int i2 = IntegerOfTerm(t2);
|
|
|
|
RFLOAT(atan2(FloatOfTerm(t1),i2));
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
{
|
|
|
|
Float f2 = FloatOfTerm(t2);
|
|
|
|
RFLOAT(atan2(FloatOfTerm(t1),f2));
|
|
|
|
}
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
{
|
2002-11-18 18:18:05 +00:00
|
|
|
RFLOAT(atan2(FloatOfTerm(t1),mpz_get_d(Yap_BigIntOfTerm(t2))));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
{
|
|
|
|
Int i = IntegerOfTerm(t2);
|
2002-11-18 18:18:05 +00:00
|
|
|
RFLOAT(atan2(mpz_get_d(Yap_BigIntOfTerm(t1)),i));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
case big_int_e:
|
|
|
|
/* two bignums */
|
2002-11-18 18:18:05 +00:00
|
|
|
RFLOAT(atan2(mpz_get_d(Yap_BigIntOfTerm(t1)),mpz_get_d(Yap_BigIntOfTerm(t2))));
|
2001-04-09 20:54:03 +01:00
|
|
|
case double_e:
|
|
|
|
{
|
|
|
|
Float dbl = FloatOfTerm(t2);
|
2002-11-18 18:18:05 +00:00
|
|
|
RFLOAT(atan2(mpz_get_d(Yap_BigIntOfTerm(t1)),dbl));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/*
|
|
|
|
power: x^y
|
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
static Term
|
|
|
|
p_power(Term t1, Term t2)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t1)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
Int i2 = IntegerOfTerm(t2);
|
|
|
|
|
|
|
|
/* two integers */
|
|
|
|
RFLOAT(pow(IntegerOfTerm(t1),i2));
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
case double_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
{
|
|
|
|
/* integer, double */
|
|
|
|
Float fl1 = (Float)IntegerOfTerm(t1);
|
|
|
|
Float fl2 = FloatOfTerm(t2);
|
|
|
|
RFLOAT(pow(fl1,fl2));
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2006-01-02 02:16:19 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
Int i1 = IntegerOfTerm(t1);
|
|
|
|
Float f2 = mpz_get_d(Yap_BigIntOfTerm(t2));
|
|
|
|
RFLOAT(pow(i1,f2));
|
2006-01-02 02:16:19 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
break;
|
2001-04-09 20:54:03 +01:00
|
|
|
case double_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
/* float / integer */
|
2008-12-04 23:33:32 +00:00
|
|
|
{
|
|
|
|
Int i2 = IntegerOfTerm(t2);
|
|
|
|
RFLOAT(pow(FloatOfTerm(t1),i2));
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
case double_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
{
|
|
|
|
Float f2 = FloatOfTerm(t2);
|
|
|
|
RFLOAT(pow(FloatOfTerm(t1),f2));
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2006-01-02 02:16:19 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
RFLOAT(pow(FloatOfTerm(t1),mpz_get_d(Yap_BigIntOfTerm(t2))));
|
2006-01-02 02:16:19 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
break;
|
2001-04-09 20:54:03 +01:00
|
|
|
case big_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
#ifdef USE_GMP
|
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
2006-01-02 02:16:19 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
Int i = IntegerOfTerm(t2);
|
|
|
|
RFLOAT(pow(mpz_get_d(Yap_BigIntOfTerm(t1)),i));
|
2006-01-02 02:16:19 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
case big_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
/* two bignums */
|
|
|
|
RFLOAT(pow(mpz_get_d(Yap_BigIntOfTerm(t1)),mpz_get_d(Yap_BigIntOfTerm(t2))));
|
|
|
|
case double_e:
|
2006-01-02 02:16:19 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
Float dbl = FloatOfTerm(t2);
|
|
|
|
RFLOAT(pow(mpz_get_d(Yap_BigIntOfTerm(t1)),dbl));
|
2006-01-02 02:16:19 +00:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2009-04-25 01:03:00 +01:00
|
|
|
/* next function is adapted from:
|
|
|
|
Inline C++ integer exponentiation routines
|
|
|
|
Version 1.01
|
|
|
|
Copyright (C) 1999-2004 John C. Bowman <bowman@math.ualberta.ca>
|
|
|
|
*/
|
|
|
|
static inline Int
|
|
|
|
ipow(Int x, Int p)
|
|
|
|
{
|
|
|
|
if (p == 0) return 1L;
|
|
|
|
if (x == 0 && p > 0) return 0L;
|
|
|
|
if(p < 0)
|
|
|
|
return (-p % 2) ? x : 1L;
|
|
|
|
|
|
|
|
Int r = 1L;
|
|
|
|
for(;;) {
|
|
|
|
if(p & 1) {
|
|
|
|
if (mul_overflow((r*x), r, x)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
r *= x;
|
|
|
|
}
|
|
|
|
if((p >>= 1) == 0) return r;
|
|
|
|
if (mul_overflow((x*x), x, x)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
x *= x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
power: x^y
|
|
|
|
*/
|
|
|
|
static Term
|
|
|
|
p_exp(Term t1, Term t2)
|
|
|
|
{
|
|
|
|
switch (ETypeOfTerm(t1)) {
|
|
|
|
case long_int_e:
|
|
|
|
switch (ETypeOfTerm(t2)) {
|
|
|
|
case long_int_e:
|
|
|
|
{
|
|
|
|
Int i1 = IntegerOfTerm(t1);
|
|
|
|
Int i2 = IntegerOfTerm(t2);
|
|
|
|
Int pow = ipow(i1,i2);
|
|
|
|
|
|
|
|
#ifdef USE_GMP
|
|
|
|
/* two integers */
|
|
|
|
if (i1 && !pow) {
|
|
|
|
/* overflow */
|
|
|
|
return Yap_gmp_exp_ints(i1, i2);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
RINT(pow);
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
{
|
|
|
|
/* integer, double */
|
|
|
|
Float fl1 = (Float)IntegerOfTerm(t1);
|
|
|
|
Float fl2 = FloatOfTerm(t2);
|
|
|
|
RFLOAT(pow(fl1,fl2));
|
|
|
|
}
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
{
|
|
|
|
Yap_Error(RESOURCE_ERROR_HUGE_INT, t2, "^/2");
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case double_e:
|
|
|
|
switch (ETypeOfTerm(t2)) {
|
|
|
|
case long_int_e:
|
|
|
|
/* float / integer */
|
|
|
|
{
|
|
|
|
Int i2 = IntegerOfTerm(t2);
|
|
|
|
RFLOAT(pow(FloatOfTerm(t1),i2));
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
{
|
|
|
|
Float f2 = FloatOfTerm(t2);
|
|
|
|
RFLOAT(pow(FloatOfTerm(t1),f2));
|
|
|
|
}
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
{
|
|
|
|
Yap_Error(RESOURCE_ERROR_HUGE_INT, t2, "^/2");
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case big_int_e:
|
|
|
|
#ifdef USE_GMP
|
|
|
|
switch (ETypeOfTerm(t2)) {
|
|
|
|
case long_int_e:
|
|
|
|
{
|
|
|
|
Int i = IntegerOfTerm(t2);
|
|
|
|
return Yap_gmp_exp_big_int(Yap_BigIntOfTerm(t1),i);
|
|
|
|
}
|
|
|
|
case big_int_e:
|
|
|
|
/* two bignums, makes no sense */
|
|
|
|
//
|
|
|
|
Yap_Error(RESOURCE_ERROR_HUGE_INT, t1, "^/2");
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
case double_e:
|
|
|
|
{
|
|
|
|
Float dbl = FloatOfTerm(t2);
|
|
|
|
RFLOAT(pow(mpz_get_d(Yap_BigIntOfTerm(t1)),dbl));
|
|
|
|
}
|
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Int
|
|
|
|
gcd(Int m11,Int m21)
|
|
|
|
{
|
|
|
|
/* Blankinship algorithm, provided by Miguel Filgueiras */
|
|
|
|
Int m12=1, m22=0, k;
|
|
|
|
|
|
|
|
while (m11>0 && m21>0)
|
|
|
|
if (m11<m21) {
|
|
|
|
k = m21/m11; m21 -= k*m11; m22 -= k*m12;
|
|
|
|
} else {
|
|
|
|
k=m11/m21; m11 -= k*m21; m12 -= k*m22;
|
|
|
|
}
|
|
|
|
if (m11<0 || m21<0) { /* overflow? */
|
2008-12-04 23:33:32 +00:00
|
|
|
/* Oflow = 1; */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(EVALUATION_ERROR_INT_OVERFLOW, MkIntegerTerm(m11),
|
2008-12-04 23:33:32 +00:00
|
|
|
"gcd/2 with %d and %d", m11, m21);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
if (m11) return(m11);
|
|
|
|
return(m21);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef GCD_MULT
|
|
|
|
Int gcdmult(Int m11,Int m21,Int *pm11) /* *pm11 gets multiplier of m11 */
|
|
|
|
{
|
|
|
|
Int m12=1, m22=0, k;
|
|
|
|
|
|
|
|
while (m11 && m21)
|
|
|
|
if (m11<m21) {
|
|
|
|
k = m21/m11; m21 -= k*m11; m22 -= k*m12;
|
|
|
|
} else {
|
|
|
|
k=m11/m21; m11 -= k*m21; m12 -= k*m22;
|
|
|
|
}
|
|
|
|
if (m11<0 || m21<0) { /* overflow? */
|
2008-12-04 23:33:32 +00:00
|
|
|
/* Oflow = 1; */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(EVALUATION_ERROR_INT_OVERFLOW, MkIntegerTerm(m11),
|
2008-12-04 23:33:32 +00:00
|
|
|
"gcdmult/2 with %d and %d", m11, m21);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
if (m11) {
|
|
|
|
*pm11 = m12; return(m11);
|
|
|
|
}
|
|
|
|
*pm11 = m22;
|
|
|
|
return(m21);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
module gcd
|
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
static Term
|
|
|
|
p_gcd(Term t1, Term t2)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t1)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
/* two integers */
|
|
|
|
{
|
|
|
|
Int i1 = IntegerOfTerm(t1), i2 = IntegerOfTerm(t2);
|
|
|
|
i1 = (i1 >= 0 ? i1 : -i1);
|
|
|
|
i2 = (i2 >= 0 ? i2 : -i2);
|
|
|
|
|
|
|
|
RINT(gcd(i1,i2));
|
|
|
|
}
|
|
|
|
case double_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t2, "gcd/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
/* make GCC happy */
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
/* I know the term is much larger, so: */
|
|
|
|
{
|
|
|
|
Int i = IntegerOfTerm(t1);
|
|
|
|
|
|
|
|
if (i > 0) {
|
2002-11-18 18:18:05 +00:00
|
|
|
RINT(mpz_gcd_ui(NULL,Yap_BigIntOfTerm(t2),i));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (i == 0) {
|
2006-01-02 02:16:19 +00:00
|
|
|
RINT(0);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
RINT(mpz_gcd_ui(NULL,Yap_BigIntOfTerm(t2),-i));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case double_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t1, "gcd/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
/* modulo between bignum and integer */
|
|
|
|
{
|
|
|
|
Int i = IntegerOfTerm(t2);
|
|
|
|
|
|
|
|
if (i > 0) {
|
2002-11-18 18:18:05 +00:00
|
|
|
RINT(mpz_gcd_ui(NULL,Yap_BigIntOfTerm(t1),i));
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (i == 0) {
|
2006-01-02 02:16:19 +00:00
|
|
|
RINT(0);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
RINT(mpz_gcd_ui(NULL,Yap_BigIntOfTerm(t1),-i));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case big_int_e:
|
|
|
|
/* two bignums */
|
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, Yap_BigIntOfTerm(t1));
|
|
|
|
mpz_gcd(&new, &new, Yap_BigIntOfTerm(t2));
|
|
|
|
RBIG(&new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
case double_e:
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER, t2, "gcd/2");
|
2001-04-09 20:54:03 +01:00
|
|
|
/* make GCC happy */
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
RERROR();
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
minimum: min(x,y)
|
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
static Term
|
|
|
|
p_min(Term t1, Term t2)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t1)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
{
|
|
|
|
Int i1 = IntegerOfTerm(t1);
|
|
|
|
Int i2 = IntegerOfTerm(t2);
|
|
|
|
RINT((i1 < i2 ? i1 : i2));
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
{
|
|
|
|
/* integer, double */
|
|
|
|
Int i = IntegerOfTerm(t1);
|
|
|
|
Float fl = FloatOfTerm(t2);
|
|
|
|
if (i <= fl) {
|
|
|
|
RINT(i);
|
|
|
|
}
|
|
|
|
RFLOAT(fl);
|
|
|
|
}
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
{
|
|
|
|
Int i = IntegerOfTerm(t1);
|
2002-11-18 18:18:05 +00:00
|
|
|
MP_INT *b = Yap_BigIntOfTerm(t2);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (mpz_cmp_si(b,i) < 0) {
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, b);
|
|
|
|
RBIG(&new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
RINT(i);
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case double_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
/* float / integer */
|
|
|
|
{
|
|
|
|
Int i = IntegerOfTerm(t2);
|
|
|
|
Float fl = FloatOfTerm(t1);
|
|
|
|
if (i <= fl) {
|
|
|
|
RINT(i);
|
|
|
|
}
|
|
|
|
RFLOAT(fl);
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
{
|
|
|
|
Float fl1 = FloatOfTerm(t1);
|
|
|
|
Float fl2 = FloatOfTerm(t2);
|
|
|
|
if (fl1 <= fl2) {
|
|
|
|
RFLOAT(fl1);
|
|
|
|
}
|
|
|
|
RFLOAT(fl2);
|
|
|
|
}
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
{
|
|
|
|
Float fl1 = FloatOfTerm(t1);
|
2002-11-18 18:18:05 +00:00
|
|
|
Float fl2 = mpz_get_d(Yap_BigIntOfTerm(t2));
|
2001-04-09 20:54:03 +01:00
|
|
|
if (fl1 <= fl2) {
|
|
|
|
RFLOAT(fl1);
|
2006-01-02 02:16:19 +00:00
|
|
|
} else {
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, Yap_BigIntOfTerm(t2));
|
|
|
|
RBIG(&new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
{
|
|
|
|
Int i = IntegerOfTerm(t2);
|
2002-11-18 18:18:05 +00:00
|
|
|
MP_INT *b = Yap_BigIntOfTerm(t1);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (mpz_cmp_si(b,i) < 0) {
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, b);
|
|
|
|
RBIG(&new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
RINT(i);
|
|
|
|
}
|
|
|
|
case big_int_e:
|
|
|
|
/* two bignums */
|
|
|
|
{
|
2002-11-18 18:18:05 +00:00
|
|
|
MP_INT *b1 = Yap_BigIntOfTerm(t1);
|
|
|
|
MP_INT *b2 = Yap_BigIntOfTerm(t2);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (mpz_cmp(b1,b2) < 0) {
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, b1);
|
|
|
|
RBIG(&new);
|
2006-01-02 02:16:19 +00:00
|
|
|
} else {
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, b2);
|
|
|
|
RBIG(&new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
{
|
|
|
|
Float fl1 = FloatOfTerm(t2);
|
2002-11-18 18:18:05 +00:00
|
|
|
Float fl2 = mpz_get_d(Yap_BigIntOfTerm(t1));
|
2001-04-09 20:54:03 +01:00
|
|
|
if (fl1 <= fl2) {
|
|
|
|
RFLOAT(fl1);
|
2006-01-02 02:16:19 +00:00
|
|
|
} else {
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, Yap_BigIntOfTerm(t1));
|
|
|
|
RBIG(&new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
maximum: max(x,y)
|
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
static Term
|
|
|
|
p_max(Term t1, Term t2)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t1)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
{
|
|
|
|
Int i1 = IntegerOfTerm(t1);
|
|
|
|
Int i2 = IntegerOfTerm(t2);
|
|
|
|
RINT((i1 > i2 ? i1 : i2));
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
{
|
|
|
|
/* integer, double */
|
|
|
|
Int i = IntegerOfTerm(t1);
|
|
|
|
Float fl = FloatOfTerm(t2);
|
|
|
|
if (i >= fl) {
|
|
|
|
RINT(i);
|
|
|
|
}
|
|
|
|
RFLOAT(fl);
|
|
|
|
}
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
{
|
|
|
|
Int i = IntegerOfTerm(t1);
|
2002-11-18 18:18:05 +00:00
|
|
|
MP_INT *b = Yap_BigIntOfTerm(t2);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (mpz_cmp_si(b,i) > 0) {
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, b);
|
|
|
|
RBIG(&new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
RINT(i);
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case double_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
/* float / integer */
|
|
|
|
{
|
|
|
|
Int i = IntegerOfTerm(t2);
|
|
|
|
Float fl = FloatOfTerm(t1);
|
|
|
|
if (i >= fl) {
|
|
|
|
RINT(i);
|
|
|
|
}
|
|
|
|
RFLOAT(fl);
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
{
|
|
|
|
Float fl1 = FloatOfTerm(t1);
|
|
|
|
Float fl2 = FloatOfTerm(t2);
|
|
|
|
if (fl1 >= fl2) {
|
|
|
|
RFLOAT(fl1);
|
|
|
|
}
|
|
|
|
RFLOAT(fl2);
|
|
|
|
}
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
|
|
|
{
|
|
|
|
Float fl1 = FloatOfTerm(t1);
|
2002-11-18 18:18:05 +00:00
|
|
|
Float fl2 = mpz_get_d(Yap_BigIntOfTerm(t2));
|
2001-04-09 20:54:03 +01:00
|
|
|
if (fl1 >= fl2) {
|
|
|
|
RFLOAT(fl1);
|
2006-01-02 02:16:19 +00:00
|
|
|
} else {
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, Yap_BigIntOfTerm(t2));
|
|
|
|
RBIG(&new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifdef USE_GMP
|
|
|
|
case big_int_e:
|
2008-12-04 23:33:32 +00:00
|
|
|
switch (ETypeOfTerm(t2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
case long_int_e:
|
|
|
|
{
|
|
|
|
Int i = IntegerOfTerm(t2);
|
2002-11-18 18:18:05 +00:00
|
|
|
MP_INT *b = Yap_BigIntOfTerm(t1);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (mpz_cmp_si(b,i) > 0) {
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, b);
|
|
|
|
RBIG(&new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
RINT(i);
|
|
|
|
}
|
|
|
|
case big_int_e:
|
|
|
|
/* two bignums */
|
|
|
|
{
|
2002-11-18 18:18:05 +00:00
|
|
|
MP_INT *b1 = Yap_BigIntOfTerm(t1);
|
|
|
|
MP_INT *b2 = Yap_BigIntOfTerm(t2);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (mpz_cmp(b1,b2) > 0) {
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, b1);
|
|
|
|
RBIG(&new);
|
2006-01-02 02:16:19 +00:00
|
|
|
} else {
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, b1);
|
|
|
|
RBIG(&new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
{
|
|
|
|
Float fl1 = FloatOfTerm(t2);
|
2002-11-18 18:18:05 +00:00
|
|
|
Float fl2 = mpz_get_d(Yap_BigIntOfTerm(t1));
|
2001-04-09 20:54:03 +01:00
|
|
|
if (fl1 >= fl2) {
|
|
|
|
RFLOAT(fl1);
|
2006-01-02 02:16:19 +00:00
|
|
|
} else {
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, Yap_BigIntOfTerm(t1));
|
|
|
|
RBIG(&new);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
2001-04-09 20:54:03 +01:00
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-04 23:33:32 +00:00
|
|
|
case db_ref_e:
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
RERROR();
|
|
|
|
}
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
static Term
|
|
|
|
eval2(Int fi, Term t1, Term t2) {
|
|
|
|
arith2_op f = fi;
|
|
|
|
switch (f) {
|
|
|
|
case op_plus:
|
|
|
|
return p_plus(t1, t2);
|
|
|
|
case op_minus:
|
|
|
|
return p_minus(t1, t2);
|
|
|
|
case op_times:
|
|
|
|
return p_times(t1, t2);
|
|
|
|
case op_div:
|
|
|
|
return p_div(t1, t2);
|
|
|
|
case op_and:
|
|
|
|
return p_and(t1, t2);
|
|
|
|
case op_or:
|
|
|
|
return p_or(t1, t2);
|
|
|
|
case op_sll:
|
|
|
|
return p_sll(t1, t2);
|
|
|
|
case op_slr:
|
|
|
|
return p_slr(t1, t2);
|
|
|
|
case op_mod:
|
|
|
|
return p_mod(t1, t2);
|
|
|
|
case op_rem:
|
|
|
|
return p_rem(t1, t2);
|
|
|
|
case op_fdiv:
|
|
|
|
return p_fdiv(t1, t2);
|
|
|
|
case op_xor:
|
|
|
|
return p_xor(t1, t2);
|
|
|
|
case op_atan2:
|
|
|
|
return p_atan2(t1, t2);
|
2009-04-25 01:03:00 +01:00
|
|
|
case op_power2:
|
2008-12-04 23:33:32 +00:00
|
|
|
return p_power(t1, t2);
|
2009-04-25 01:03:00 +01:00
|
|
|
case op_power:
|
|
|
|
return p_exp(t1, t2);
|
2008-12-04 23:33:32 +00:00
|
|
|
case op_gcd:
|
|
|
|
return p_gcd(t1, t2);
|
|
|
|
case op_min:
|
|
|
|
return p_min(t1, t2);
|
|
|
|
case op_max:
|
|
|
|
return p_max(t1, t2);
|
|
|
|
}
|
|
|
|
RERROR();
|
|
|
|
}
|
2006-01-02 02:16:19 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
Term Yap_eval_binary(Int f, Term t1, Term t2)
|
|
|
|
{
|
|
|
|
return eval2(f,t1,t2);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static InitBinEntry InitBinTab[] = {
|
2008-12-04 23:33:32 +00:00
|
|
|
{"+", op_plus},
|
|
|
|
{"-", op_minus},
|
|
|
|
{"*", op_times},
|
|
|
|
{"/", op_fdiv},
|
|
|
|
{"mod", op_mod},
|
|
|
|
{"rem", op_rem},
|
|
|
|
{"//", op_div},
|
|
|
|
{"<<", op_sll},
|
|
|
|
{">>", op_slr},
|
|
|
|
{"/\\", op_and},
|
|
|
|
{"\\/", op_or},
|
|
|
|
{"#", op_xor},
|
|
|
|
{"atan2", op_atan2},
|
2001-04-09 20:54:03 +01:00
|
|
|
/* C-Prolog exponentiation */
|
2008-12-04 23:33:32 +00:00
|
|
|
{"^", op_power},
|
2001-04-09 20:54:03 +01:00
|
|
|
/* ISO-Prolog exponentiation */
|
2009-04-25 01:03:00 +01:00
|
|
|
{"**", op_power2},
|
2001-04-09 20:54:03 +01:00
|
|
|
/* Quintus exponentiation */
|
2009-04-25 01:03:00 +01:00
|
|
|
{"exp", op_power2},
|
2008-12-04 23:33:32 +00:00
|
|
|
{"gcd", op_gcd},
|
|
|
|
{"min", op_min},
|
|
|
|
{"max", op_max}
|
2001-04-09 20:54:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_binary_is(void)
|
|
|
|
{ /* X is Y */
|
|
|
|
Term t = Deref(ARG2);
|
2008-12-04 23:33:32 +00:00
|
|
|
Term t1, t2;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (IsVarTerm(t)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR,t, "X is Y");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
t1 = Yap_Eval(Deref(ARG3));
|
|
|
|
if (t1 == 0L)
|
|
|
|
return FALSE;
|
|
|
|
t2 = Yap_Eval(Deref(ARG4));
|
|
|
|
if (t2 == 0L)
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsIntTerm(t)) {
|
2009-02-09 21:56:40 +00:00
|
|
|
return Yap_unify_constant(ARG1,eval2(IntegerOfTerm(t), t1, t2));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (IsAtomTerm(t)) {
|
|
|
|
Atom name = AtomOfTerm(t);
|
|
|
|
ExpEntry *p;
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
if (EndOfPAEntr(p = RepExpProp(Yap_GetExpProp(name, 2)))) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Term ti[2];
|
|
|
|
|
|
|
|
/* error */
|
|
|
|
ti[0] = t;
|
2008-12-04 23:33:32 +00:00
|
|
|
ti[1] = MkIntTerm(1);
|
2008-12-23 01:53:52 +00:00
|
|
|
t = Yap_MkApplTerm(FunctorSlash, 2, ti);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_EVALUABLE, t,
|
2008-12-04 23:33:32 +00:00
|
|
|
"functor %s/%d for arithmetic expression",
|
|
|
|
RepAtom(name)->StrOfAE,2);
|
2001-04-09 20:54:03 +01:00
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
return(FALSE);
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
return Yap_unify_constant(ARG1,eval2(p->FOfEE, t1, t2));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
|
2009-02-25 00:12:36 +00:00
|
|
|
static Int
|
|
|
|
p_binary_op_as_integer(void)
|
|
|
|
{ /* X is Y */
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR,t, "X is Y");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (IsIntTerm(t)) {
|
|
|
|
return Yap_unify_constant(ARG2,t);
|
|
|
|
}
|
|
|
|
if (IsAtomTerm(t)) {
|
|
|
|
Atom name = AtomOfTerm(t);
|
|
|
|
ExpEntry *p;
|
|
|
|
|
|
|
|
if (EndOfPAEntr(p = RepExpProp(Yap_GetExpProp(name, 2)))) {
|
|
|
|
Term ti[2];
|
|
|
|
|
|
|
|
/* error */
|
|
|
|
ti[0] = t;
|
|
|
|
ti[1] = MkIntTerm(1);
|
|
|
|
t = Yap_MkApplTerm(FunctorSlash, 2, ti);
|
|
|
|
Yap_Error(TYPE_ERROR_EVALUABLE, t,
|
|
|
|
"functor %s/%d for arithmetic expression",
|
|
|
|
RepAtom(name)->StrOfAE,2);
|
|
|
|
P = (yamop *)FAILCODE;
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
return Yap_unify_constant(ARG2,MkIntTerm(p->FOfEE));
|
|
|
|
}
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitBinaryExps(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
ExpEntry *p;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(InitBinTab)/sizeof(InitBinEntry); ++i) {
|
2002-11-18 18:18:05 +00:00
|
|
|
AtomEntry *ae = RepAtom(Yap_LookupAtom(InitBinTab[i].OpName));
|
2001-04-09 20:54:03 +01:00
|
|
|
WRITE_LOCK(ae->ARWLock);
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_GetExpPropHavingLock(ae, 2)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
WRITE_UNLOCK(ae->ARWLock);
|
|
|
|
break;
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
p = (ExpEntry *) Yap_AllocAtomSpace(sizeof(ExpEntry));
|
2001-04-09 20:54:03 +01:00
|
|
|
p->KindOfPE = ExpProperty;
|
|
|
|
p->ArityOfEE = 2;
|
|
|
|
p->ENoOfEE = 2;
|
2008-12-04 23:33:32 +00:00
|
|
|
p->FOfEE = InitBinTab[i].f;
|
2001-10-30 16:42:05 +00:00
|
|
|
p->NextOfPE = ae->PropsOfAE;
|
|
|
|
ae->PropsOfAE = AbsExpProp(p);
|
2001-04-09 20:54:03 +01:00
|
|
|
WRITE_UNLOCK(ae->ARWLock);
|
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitCPred("is", 4, p_binary_is, TestPredFlag | SafePredFlag);
|
2009-02-25 00:12:36 +00:00
|
|
|
Yap_InitCPred("$binary_op_as_integer", 2, p_binary_op_as_integer, TestPredFlag|SafePredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This routine is called from Restore to make sure we have the same arithmetic operators */
|
|
|
|
int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ReInitBinaryExps(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|