fix gmp support to give out of resource error if the bignum is too big.

This commit is contained in:
Vitor Santos Costa
2009-04-25 11:01:33 -05:00
parent 09d33c6cad
commit c132e7e6ec
10 changed files with 73 additions and 87 deletions

View File

@@ -16,17 +16,30 @@
*************************************************************************/
#include "Yap.h"
#include "Yatom.h"
#include "Heap.h"
#include "eval.h"
#if USE_GMP
static inline Term
MkBigAndClose(MP_INT *new)
{
Term t = Yap_MkBigIntTerm(new);
mpz_clear(new);
if (t == TermNil) {
Yap_Error(RESOURCE_ERROR_STACK, t, ">>/2");
P = (yamop *)FAILCODE;
RERROR();
}
return t;
}
/* add i + j using temporary bigint new */
Term
Yap_gmp_add_ints(Int i, Int j)
{
MP_INT new;
Term t;
mpz_init_set_si(&new,i);
if (j > 0) {
@@ -39,9 +52,7 @@ Yap_gmp_add_ints(Int i, Int j)
mpz_sub_ui(&new, &new, -j);
}
}
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
Term
@@ -61,6 +72,7 @@ Yap_gmp_sub_ints(Int i, Int j)
mpz_add_ui(&new, &new, -j);
}
}
return MkBigAndClose(&new);
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
@@ -70,26 +82,20 @@ Term
Yap_gmp_mul_ints(Int i, Int j)
{
MP_INT new;
Term t;
mpz_init_set_si(&new,i);
mpz_mul_si(&new, &new, j);
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
Term
Yap_gmp_sll_ints(Int i, Int j)
{
MP_INT new;
Term t;
mpz_init_set_si(&new,i);
mpz_mul_2exp(&new, &new, j);
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
/* add i + b using temporary bigint new */
@@ -97,13 +103,10 @@ Term
Yap_gmp_add_int_big(Int i, MP_INT *b)
{
MP_INT new;
Term t;
mpz_init_set_si(&new, i);
mpz_add(&new, &new, b);
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
/* sub i - b using temporary bigint new */
@@ -111,13 +114,10 @@ Term
Yap_gmp_sub_int_big(Int i, MP_INT *b)
{
MP_INT new;
Term t;
mpz_init_set_si(&new, i);
mpz_sub(&new, &new, b);
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
/* add i + b using temporary bigint new */
@@ -125,13 +125,10 @@ Term
Yap_gmp_mul_int_big(Int i, MP_INT *b)
{
MP_INT new;
Term t;
mpz_init_set_si(&new, i);
mpz_mul(&new, &new, b);
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
/* sub i - b using temporary bigint new */
@@ -139,14 +136,11 @@ Term
Yap_gmp_sub_big_int(MP_INT *b, Int i)
{
MP_INT new;
Term t;
mpz_init_set_si(&new, i);
mpz_neg(&new, &new);
mpz_add(&new, &new, b);
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
/* div i / b using temporary bigint new */
@@ -154,7 +148,6 @@ Term
Yap_gmp_div_big_int(MP_INT *b, Int i)
{
MP_INT new;
Term t;
mpz_init_set(&new, b);
if (yap_flags[INTEGER_ROUNDING_FLAG] == 0) {
@@ -180,9 +173,7 @@ Yap_gmp_div_big_int(MP_INT *b, Int i)
mpz_neg(&new, &new);
}
}
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
/* sub i - b using temporary bigint new */
@@ -190,13 +181,10 @@ Term
Yap_gmp_and_int_big(Int i, MP_INT *b)
{
MP_INT new;
Term t;
mpz_init_set_si(&new, i);
mpz_and(&new, &new, b);
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
/* sub i - b using temporary bigint new */
@@ -204,13 +192,10 @@ Term
Yap_gmp_ior_int_big(Int i, MP_INT *b)
{
MP_INT new;
Term t;
mpz_init_set_si(&new, i);
mpz_ior(&new, &new, b);
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
/* add i + b using temporary bigint new */
@@ -218,7 +203,6 @@ Term
Yap_gmp_sll_big_int(MP_INT *b, Int i)
{
MP_INT new;
Term t;
if (i > 0) {
mpz_init_set(&new, b);
@@ -232,48 +216,37 @@ Yap_gmp_sll_big_int(MP_INT *b, Int i)
}
mpz_tdiv_q_2exp(&new, &new, -i);
}
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
Term
Yap_gmp_add_big_big(MP_INT *b1, MP_INT *b2)
{
MP_INT new;
Term t;
mpz_init_set(&new, b1);
mpz_add(&new, &new, b2);
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
Term
Yap_gmp_sub_big_big(MP_INT *b1, MP_INT *b2)
{
MP_INT new;
Term t;
mpz_init_set(&new, b1);
mpz_sub(&new, &new, b2);
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
Term
Yap_gmp_mul_big_big(MP_INT *b1, MP_INT *b2)
{
MP_INT new;
Term t;
mpz_init_set(&new, b1);
mpz_mul(&new, &new, b2);
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
/* div i / b using temporary bigint new */
@@ -281,7 +254,6 @@ Term
Yap_gmp_div_big_big(MP_INT *b1, MP_INT *b2)
{
MP_INT new;
Term t;
mpz_init_set(&new, b1);
if (yap_flags[INTEGER_ROUNDING_FLAG] == 0) {
@@ -289,35 +261,27 @@ Yap_gmp_div_big_big(MP_INT *b1, MP_INT *b2)
} else {
mpz_fdiv_q(&new, &new, b2);
}
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
Term
Yap_gmp_and_big_big(MP_INT *b1, MP_INT *b2)
{
MP_INT new;
Term t;
mpz_init_set(&new, b1);
mpz_and(&new, &new, b2);
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
Term
Yap_gmp_ior_big_big(MP_INT *b1, MP_INT *b2)
{
MP_INT new;
Term t;
mpz_init_set(&new, b1);
mpz_ior(&new, &new, b2);
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
Term
@@ -348,20 +312,16 @@ Term
Yap_gmp_exp_ints(Int i1, Int i2)
{
MP_INT new;
Term t;
mpz_init_set_si(&new, i1);
mpz_pow_ui (&new, &new, (unsigned long int)i2);
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
Term
Yap_gmp_exp_big_int(MP_INT *b, Int i)
{
MP_INT new;
Term t;
if (b > 0) {
mpz_init(&new);
@@ -373,9 +333,7 @@ Yap_gmp_exp_big_int(MP_INT *b, Int i)
mpz_init_set_si(&new, i);
mpz_powm (&new, b, &new, b);
}
t = Yap_MkBigIntTerm(&new);
mpz_clear(&new);
return t;
return MkBigAndClose(&new);
}
#endif