fix comparison between terms when rationals come up.

This commit is contained in:
Vítor Santos Costa 2010-05-28 12:07:01 +01:00
parent 4a76bde7cb
commit ae2421951b
11 changed files with 96 additions and 100 deletions

View File

@ -4185,7 +4185,7 @@ Yap_absmi(int inp)
{
FAIL();
}
if (mpz_cmp(Yap_BigIntOfTerm(d0),Yap_BigIntOfTerm(PREG->u.xc.c)))
if (Yap_gmp_tcmp_big_big(d0,PREG->u.xc.c))
FAIL();
PREG = NEXTOP(PREG, xc);
ENDP(pt0);
@ -6274,7 +6274,7 @@ Yap_absmi(int inp)
FAIL();
}
ENDD(d1);
if (mpz_cmp(Yap_BigIntOfTerm(d0),Yap_BigIntOfTerm(PREG->u.oc.c)))
if (Yap_gmp_tcmp_big_big(d0,PREG->u.oc.c))
FAIL();
PREG = NEXTOP(PREG, oc);
ENDP(pt0);
@ -6319,7 +6319,7 @@ Yap_absmi(int inp)
FAIL();
}
ENDD(d0);
if (mpz_cmp(Yap_BigIntOfTerm(d0),Yap_BigIntOfTerm(PREG->u.oc.c)))
if (Yap_gmp_tcmp_big_big(d0,PREG->u.oc.c))
FAIL();
PREG = NEXTOP(PREG, oc);
ENDP(pt0);
@ -11992,7 +11992,7 @@ Yap_absmi(int inp)
PREG = PREG->u.l.l;
GONext();
}
if (mpz_cmp(Yap_BigIntOfTerm(d0), Yap_BigIntOfTerm(d1)) == 0) {
if (Yap_gmp_tcmp_big_big(d0,d1) == 0) {
PREG = NEXTOP(PREG, l);
GONext();
}

View File

@ -120,12 +120,7 @@ static int compare_complex(register CELL *pt0, register CELL *pt0_end, register
out = IntOfTerm(d0) - LongIntOfTerm(d1);
#ifdef USE_GMP
} else if (IsBigIntTerm(d1)) {
MP_INT *b1 = Yap_BigIntOfTerm(d1);
if (!mpz_size(b1)) {
out = -1;
} else {
out = -mpz_cmp_si(b1, IntOfTerm(d0));
}
out = Yap_gmp_tcmp_int_big(IntOfTerm(d0), d1);
#endif
} else if (IsRefTerm(d1))
out = 1 ;
@ -151,12 +146,7 @@ static int compare_complex(register CELL *pt0, register CELL *pt0_end, register
out = LongIntOfTerm(d0) - LongIntOfTerm(d1);
#ifdef USE_GMP
} else if (IsBigIntTerm(d1)) {
MP_INT *b1 = Yap_BigIntOfTerm(d1);
if (!mpz_size(b1)) {
out = -1;
} else {
out = -mpz_cmp_si(b1, LongIntOfTerm(d0));
}
out = Yap_gmp_tcmp_int_big(LongIntOfTerm(d0), d1);
#endif
} else if (IsRefTerm(d1)) {
out = 1 ;
@ -168,29 +158,14 @@ static int compare_complex(register CELL *pt0, register CELL *pt0_end, register
}
#ifdef USE_GMP
else if (IsBigIntTerm(d0)) {
MP_INT *b0 = Yap_BigIntOfTerm(d0);
if (!mpz_size(b0)) {
if (IsBigIntTerm(d1)) {
MP_INT *b1 = Yap_BigIntOfTerm(d1);
out = b0-b1;
} else {
out = 1;
}
} else if (IsIntTerm(d1))
out = mpz_cmp_si(b0, IntOfTerm(d1));
else if (IsFloatTerm(d1)) {
if (IsIntTerm(d1)) {
out = Yap_gmp_tcmp_int_big(d0, IntOfTerm(d1));
} else if (IsFloatTerm(d1)) {
out = 1;
} else if (IsLongIntTerm(d1))
out = mpz_cmp_si(b0, LongIntOfTerm(d1));
else if (IsBigIntTerm(d1)) {
MP_INT *b1 = Yap_BigIntOfTerm(d1);
if (!mpz_size(b1)) {
out = -1;
} else {
out = mpz_cmp(b0, b1);
}
} else if (IsLongIntTerm(d1)) {
out = Yap_gmp_tcmp_int_big(d0, LongIntOfTerm(d1));
} else if (IsBigIntTerm(d1)) {
out = Yap_gmp_tcmp_big_big(d1, d1);
} else if (IsRefTerm(d1))
out = 1 ;
else out = -1;
@ -352,12 +327,7 @@ compare(Term t1, Term t2) /* compare terms t1 and t2 */
}
#ifdef USE_GMP
if (IsBigIntTerm(t2)) {
MP_INT *b1 = Yap_BigIntOfTerm(t2);
if (!mpz_size(b1)) {
return -1;
} else {
return -mpz_cmp_si(b1,IntOfTerm(t1));
}
return Yap_gmp_tcmp_int_big(IntOfTerm(t1), t2);
}
#endif
if (IsRefTerm(t2))
@ -408,12 +378,7 @@ compare(Term t1, Term t2) /* compare terms t1 and t2 */
return LongIntOfTerm(t1) - LongIntOfTerm(t2);
#ifdef USE_GMP
if (IsBigIntTerm(t2)) {
MP_INT *b1 = Yap_BigIntOfTerm(t2);
if (!mpz_size(b1)) {
return -1;
} else {
return -mpz_cmp_si(b1, LongIntOfTerm(t1));
}
return Yap_gmp_tcmp_int_big(LongIntOfTerm(t1), t2);
}
#endif
if (IsRefTerm(t2))
@ -423,30 +388,15 @@ compare(Term t1, Term t2) /* compare terms t1 and t2 */
#ifdef USE_GMP
case big_int_e:
{
MP_INT *b0 = Yap_BigIntOfTerm(t1);
if (!mpz_size(b0)) {
if (IsBigIntTerm(t2)) {
MP_INT *b1 = Yap_BigIntOfTerm(t2);
return b0-b1;
} else {
return 1;
}
} else if (IsIntTerm(t2))
return mpz_cmp_si(Yap_BigIntOfTerm(t1), IntOfTerm(t2));
if (IsIntTerm(t2))
return Yap_gmp_tcmp_big_int(t1, IntOfTerm(t2));
if (IsFloatTerm(t2)) {
return 1;
}
if (IsLongIntTerm(t2))
return mpz_cmp_si(Yap_BigIntOfTerm(t1), LongIntOfTerm(t2));
return Yap_gmp_tcmp_big_int(t1, LongIntOfTerm(t2));
if (IsBigIntTerm(t2)) {
MP_INT *b1 = Yap_BigIntOfTerm(t2);
if (!mpz_size(b1)) {
return -1;
} else {
return mpz_cmp(b0, b1);
}
return Yap_gmp_tcmp_big_big(t1, t2);
}
if (IsRefTerm(t2))
return 1;
@ -572,8 +522,7 @@ a_cmp(Term t1, Term t2)
return flt_cmp(i1-f2);
#ifdef USE_GMP
} else if (IsBigIntTerm(t2)) {
MP_INT *b2 = Yap_BigIntOfTerm(t2);
return int_cmp(-mpz_cmp_si(b2,i1));
return Yap_gmp_cmp_int_big(i1,t2);
#endif
} else {
return FALSE;
@ -610,8 +559,7 @@ a_cmp(Term t1, Term t2)
return flt_cmp(f1-f2);
#ifdef USE_GMP
} else if (IsBigIntTerm(t2)) {
MP_INT *b2 = Yap_BigIntOfTerm(t2);
return flt_cmp(f1-mpz_get_d(b2));
return Yap_gmp_cmp_float_big(f1,t2);
#endif
} else {
return FALSE;
@ -619,12 +567,8 @@ a_cmp(Term t1, Term t2)
#ifdef USE_GMP
} else if (IsBigIntTerm(t1)) {
{
MP_INT *b1 = Yap_BigIntOfTerm(t1);
t2 = Yap_Eval(t2);
if (IsIntegerTerm(t2)) {
Int i2 = IntegerOfTerm(t2);
return int_cmp(mpz_cmp_si(b1,i2));
return Yap_gmp_cmp_big_int(t1, IntegerOfTerm(t2));
} else if (IsFloatTerm(t2)) {
Float f2 = FloatOfTerm(t2);
#if HAVE_ISNAN
@ -635,10 +579,9 @@ a_cmp(Term t1, Term t2)
ArithError = TRUE;
}
#endif
return flt_cmp(mpz_get_d(b1)-f2);
return Yap_gmp_cmp_big_float(t1, f2);
} else if (IsBigIntTerm(t2)) {
MP_INT *b2 = Yap_BigIntOfTerm(t2);
return int_cmp(mpz_cmp(b1,b2));
return Yap_gmp_cmp_big_big(t1, t2);
} else {
return FALSE;
}

View File

@ -154,7 +154,7 @@ static int can_unify_complex(register CELL *pt0,
goto comparison_failed;
#ifdef USE_GMP
case (CELL)FunctorBigInt:
if (mpz_cmp(Yap_BigIntOfTerm(d0),Yap_BigIntOfTerm(d1)) == 0) continue;
if (Yap_gmp_tcmp_big_big(d0,d1) == 0) continue;
goto comparison_failed;
#endif /* USE_GMP */
default:
@ -293,7 +293,7 @@ can_unify(Term t1, Term t2, Term *Vars)
return FALSE;
#ifdef USE_GMP
case (CELL)FunctorBigInt:
if (mpz_cmp(Yap_BigIntOfTerm(t1),Yap_BigIntOfTerm(t2)) == 0) return(TRUE);
if (Yap_gmp_tcmp_big_big(t1,t2) == 0) return(TRUE);
return(FALSE);
#endif /* USE_GMP */
default:

View File

@ -1166,9 +1166,12 @@ Yap_gmp_cmp_big_big(Term t1, Term t2)
return mpz_cmp(b1, b2);
} else {
MP_RAT *b1 = NULL, bb1;
int f1 = FALSE;
MP_RAT *b2 = NULL, bb2;
int f2 = FALSE;
if (pt1[1] == BIG_INT) {
b1 = &bb1;
f1 = TRUE;
mpq_init(b1);
mpq_set_z(b1, Yap_BigIntOfTerm(t1));
} else {
@ -1176,11 +1179,62 @@ Yap_gmp_cmp_big_big(Term t1, Term t2)
}
if (pt2[1] == BIG_INT) {
b2 = &bb2;
f2 = TRUE;
mpq_init(b2);
mpq_set_z(b2, Yap_BigIntOfTerm(t2));
} else {
b2 = Yap_BigRatOfTerm(t2);
}
if (f1)
mpq_clear(b1);
if (f2)
mpq_clear(b2);
return mpq_cmp(b1, b2);
}
}
int
Yap_gmp_tcmp_big_int(Term t, Int i)
{
CELL *pt = RepAppl(t);
if (pt[1] == BIG_INT) {
MP_INT *b = Yap_BigIntOfTerm(t);
return mpz_cmp_si(b,i);
} else {
return -1;
}
}
int
Yap_gmp_tcmp_big_float(Term t, Float d)
{
return 1;
}
int
Yap_gmp_tcmp_big_big(Term t1, Term t2)
{
CELL *pt1 = RepAppl(t1);
CELL *pt2 = RepAppl(t2);
if (pt1[1] == BIG_INT && pt2[1] == BIG_INT) {
MP_INT *b1 = Yap_BigIntOfTerm(t1);
MP_INT *b2 = Yap_BigIntOfTerm(t2);
return mpz_cmp(b1, b2);
} else {
MP_RAT *b1, *b2;
if (pt1[1] == BIG_INT) {
return 1;
} else {
b1 = Yap_BigRatOfTerm(t1);
}
if (pt2[1] == BIG_INT) {
return -1;
} else {
b2 = Yap_BigRatOfTerm(t2);
}
return mpq_cmp(b1, b2);
}
}
@ -1480,7 +1534,6 @@ Yap_gmp_popcount(Term t)
return Yap_ArithError(TYPE_ERROR_INTEGER, t, "popcount");
}
}
#endif

View File

@ -321,7 +321,7 @@ p_eq(void)
return(LongIntOfTerm(d0) == LongIntOfTerm(d1));
#ifdef USE_GMP
case (CELL)FunctorBigInt:
return (mpz_cmp(Yap_BigIntOfTerm(d0), Yap_BigIntOfTerm(d1)) == 0);
return (Yap_gmp_tcmp_big_big(d0, d1) == 0);
#endif
case (CELL)FunctorDouble:
return(FloatOfTerm(d0) == FloatOfTerm(d1));

View File

@ -5128,7 +5128,7 @@ format(volatile Term otail, volatile Term oargs, int sno)
fl = (Float)IntegerOfTerm(t);
#ifdef USE_GMP
} else if (IsBigIntTerm(t)) {
fl = mpz_get_d(Yap_BigIntOfTerm(t));
fl = Yap_gmp_to_float(t);
#endif
} else {
fl = FloatOfTerm(t);

View File

@ -385,7 +385,7 @@ oc_unify_nvar_nvar:
return(FloatOfTerm(AbsAppl(pt0)) == FloatOfTerm(AbsAppl(pt1)));
#ifdef USE_GMP
case (CELL)FunctorBigInt:
return(mpz_cmp(Yap_BigIntOfTerm(AbsAppl(pt0)),Yap_BigIntOfTerm(AbsAppl(pt0))) == 0);
return(Yap_gmp_tcmp_big_big(AbsAppl(pt0),AbsAppl(pt0)) == 0);
#endif /* USE_GMP */
default:
return(FALSE);
@ -532,7 +532,7 @@ unify_nvar_nvar:
return(FloatOfTerm(AbsAppl(pt0)) == FloatOfTerm(AbsAppl(pt1)));
#ifdef USE_GMP
case (CELL)FunctorBigInt:
return(mpz_cmp(Yap_BigIntOfTerm(AbsAppl(pt0)),Yap_BigIntOfTerm(AbsAppl(pt0))) == 0);
return(Yap_gmp_tcmp_big_big(AbsAppl(pt0),AbsAppl(pt0)) == 0);
#endif /* USE_GMP */
default:
return(FALSE);
@ -920,7 +920,7 @@ unifiable_nvar_nvar:
return(FloatOfTerm(AbsAppl(pt0)) == FloatOfTerm(AbsAppl(pt1)));
#ifdef USE_GMP
case (CELL)FunctorBigInt:
return(mpz_cmp(Yap_BigIntOfTerm(AbsAppl(pt0)),Yap_BigIntOfTerm(AbsAppl(pt0))) == 0);
return(Yap_gmp_tcmp_big_big(AbsAppl(pt0),AbsAppl(pt0)) == 0);
#endif /* USE_GMP */
default:
return(FALSE);

View File

@ -526,6 +526,8 @@ inline EXTERN int STD_PROTO (unify_extension, (Functor, CELL, CELL *, CELL));
EXTERN int STD_PROTO (unify_extension, (Functor, CELL, CELL *, CELL));
int STD_PROTO(Yap_gmp_tcmp_big_big,(Term, Term));
inline EXTERN int
unify_extension (Functor f, CELL d0, CELL * pt0, CELL d1)
{
@ -539,11 +541,7 @@ unify_extension (Functor f, CELL d0, CELL * pt0, CELL d1)
return (pt0[1] == RepAppl (d1)[1]);
case big_int_e:
#ifdef USE_GMP
{
MP_INT *m0 = Yap_BigIntOfTerm (d0);
MP_INT *m1 = Yap_BigIntOfTerm (d1);
return mpz_cmp (m0, m1) == 0;
}
return (Yap_gmp_tcmp_big_big(d0,d1) == 0);
#else
return d0 == d1;
#endif /* USE_GMP */

View File

@ -1380,7 +1380,7 @@ loop:
return (FALSE);
#ifdef USE_GMP
case (CELL)FunctorBigInt:
if (IsBigIntTerm(d1) && mpz_cmp(Yap_BigIntOfTerm(d0),Yap_BigIntOfTerm(d1)) == 0) continue;
if (IsBigIntTerm(d1) && Yap_gmp_tcmp_big_big(d0,d1) == 0) continue;
UNWIND_CUNIF();
return (FALSE);
#endif /* USE_GMP */

View File

@ -520,11 +520,7 @@ Yap_unify_constant(register Term a, register Term cons)
}
case (CELL)FunctorBigInt:
#ifdef USE_GMP
{
MP_INT *d0 = Yap_BigIntOfTerm(a);
MP_INT *d1 = Yap_BigIntOfTerm(cons);
return mpz_cmp(d0,d1) == 0;
}
return (Yap_gmp_tcmp_big_big(a, cons) == 0);
#endif /* USE_GMP */
default:
return FALSE;

View File

@ -282,6 +282,12 @@ int STD_PROTO(Yap_gmp_cmp_big_float,(Term, Float));
#define Yap_gmp_cmp_float_big(D, T) (-Yap_gmp_cmp_big_float(T, D))
int STD_PROTO(Yap_gmp_cmp_big_big,(Term, Term));
int STD_PROTO(Yap_gmp_tcmp_big_int,(Term, Int));
#define Yap_gmp_tcmp_int_big(I, T) (-Yap_gmp_tcmp_big_int(T, I))
int STD_PROTO(Yap_gmp_tcmp_big_float,(Term, Float));
#define Yap_gmp_tcmp_float_big(D, T) (-Yap_gmp_tcmp_big_float(T, D))
int STD_PROTO(Yap_gmp_tcmp_big_big,(Term, Term));
Term STD_PROTO(Yap_gmp_neg_int,(Int));
Term STD_PROTO(Yap_gmp_abs_big,(Term));
Term STD_PROTO(Yap_gmp_neg_big,(Term));