2008-11-28 15:54:08 +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: gmp_support.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: bignum code *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "Yap.h"
|
2009-04-25 17:01:33 +01:00
|
|
|
#include "Yatom.h"
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2008-11-28 15:54:08 +00:00
|
|
|
#include "eval.h"
|
|
|
|
|
|
|
|
#if USE_GMP
|
|
|
|
|
2009-04-25 17:01:33 +01:00
|
|
|
static inline Term
|
|
|
|
MkBigAndClose(MP_INT *new)
|
|
|
|
{
|
|
|
|
Term t = Yap_MkBigIntTerm(new);
|
|
|
|
mpz_clear(new);
|
|
|
|
if (t == TermNil) {
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(RESOURCE_ERROR_STACK, t, ">>/2");
|
2009-04-25 17:01:33 +01:00
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2008-11-28 15:54:08 +00:00
|
|
|
/* add i + j using temporary bigint new */
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_add_ints(Int i, Int j)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set_si(&new,i);
|
2008-11-28 15:54:08 +00:00
|
|
|
if (j > 0) {
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_add_ui(&new, &new, j);
|
2008-11-28 15:54:08 +00:00
|
|
|
} else {
|
|
|
|
if (j-1 > 0) { /* negative overflow */
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_sub_ui(&new, &new, -(j+1));
|
|
|
|
mpz_sub_ui(&new, &new, 1);
|
2008-11-28 15:54:08 +00:00
|
|
|
} else {
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_sub_ui(&new, &new, -j);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_sub_ints(Int i, Int j)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2009-04-25 01:10:53 +01:00
|
|
|
Term t;
|
2008-12-04 23:33:32 +00:00
|
|
|
|
|
|
|
mpz_init_set_si(&new,i);
|
2008-11-28 15:54:08 +00:00
|
|
|
if (j > 0) {
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_sub_ui(&new, &new, j);
|
2008-11-28 15:54:08 +00:00
|
|
|
} else {
|
|
|
|
if (j-1 > 0) { /* negative overflow */
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_add_ui(&new, &new, -(j+1));
|
|
|
|
mpz_add_ui(&new, &new, 1);
|
2008-11-28 15:54:08 +00:00
|
|
|
} else {
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_add_ui(&new, &new, -j);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2009-04-25 01:10:53 +01:00
|
|
|
t = Yap_MkBigIntTerm(&new);
|
|
|
|
mpz_clear(&new);
|
|
|
|
return t;
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_mul_ints(Int i, Int j)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set_si(&new,i);
|
|
|
|
mpz_mul_si(&new, &new, j);
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_sll_ints(Int i, Int j)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set_si(&new,i);
|
|
|
|
mpz_mul_2exp(&new, &new, j);
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add i + b using temporary bigint new */
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_add_int_big(Int i, MP_INT *b)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set_si(&new, i);
|
|
|
|
mpz_add(&new, &new, b);
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sub i - b using temporary bigint new */
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_sub_int_big(Int i, MP_INT *b)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set_si(&new, i);
|
|
|
|
mpz_sub(&new, &new, b);
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add i + b using temporary bigint new */
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_mul_int_big(Int i, MP_INT *b)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set_si(&new, i);
|
|
|
|
mpz_mul(&new, &new, b);
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sub i - b using temporary bigint new */
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_sub_big_int(MP_INT *b, Int i)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set_si(&new, i);
|
|
|
|
mpz_neg(&new, &new);
|
|
|
|
mpz_add(&new, &new, b);
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* div i / b using temporary bigint new */
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_div_big_int(MP_INT *b, Int i)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2008-11-28 15:54:08 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, b);
|
2008-11-28 15:54:08 +00:00
|
|
|
if (yap_flags[INTEGER_ROUNDING_FLAG] == 0) {
|
|
|
|
if (i > 0) {
|
2009-02-09 21:56:40 +00:00
|
|
|
mpz_tdiv_q_ui(&new, &new, i);
|
2008-11-28 15:54:08 +00:00
|
|
|
} else if (i == 0) {
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(EVALUATION_ERROR_ZERO_DIVISOR, MkIntTerm(0), "// /2");
|
2008-11-28 15:54:08 +00:00
|
|
|
} else {
|
|
|
|
/* we do not handle MIN_INT */
|
2009-02-09 21:56:40 +00:00
|
|
|
mpz_tdiv_q_ui(&new, &new, -i);
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_neg(&new, &new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (i > 0) {
|
2009-02-09 21:56:40 +00:00
|
|
|
mpz_fdiv_q_ui(&new, &new, i);
|
2008-11-28 15:54:08 +00:00
|
|
|
} else if (i == 0) {
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(EVALUATION_ERROR_ZERO_DIVISOR, MkIntTerm(0), "// /2");
|
2008-11-28 15:54:08 +00:00
|
|
|
} else {
|
|
|
|
/* we do not handle MIN_INT */
|
2009-02-09 21:56:40 +00:00
|
|
|
mpz_fdiv_q_ui(&new, &new, -i);
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_neg(&new, &new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sub i - b using temporary bigint new */
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_and_int_big(Int i, MP_INT *b)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set_si(&new, i);
|
|
|
|
mpz_and(&new, &new, b);
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sub i - b using temporary bigint new */
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_ior_int_big(Int i, MP_INT *b)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set_si(&new, i);
|
|
|
|
mpz_ior(&new, &new, b);
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add i + b using temporary bigint new */
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_sll_big_int(MP_INT *b, Int i)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
|
|
|
|
2008-11-28 15:54:08 +00:00
|
|
|
if (i > 0) {
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, b);
|
|
|
|
mpz_mul_2exp(&new, &new, i);
|
2008-11-28 15:54:08 +00:00
|
|
|
} else if (i == 0) {
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, b);
|
2008-11-28 15:54:08 +00:00
|
|
|
} else {
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, b);
|
2008-11-28 15:54:08 +00:00
|
|
|
if (i == Int_MIN) {
|
2009-05-22 17:21:39 +01:00
|
|
|
return Yap_ArithError(RESOURCE_ERROR_HUGE_INT, MkIntegerTerm(i), "<</2");
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_tdiv_q_2exp(&new, &new, -i);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_add_big_big(MP_INT *b1, MP_INT *b2)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set(&new, b1);
|
|
|
|
mpz_add(&new, &new, b2);
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_sub_big_big(MP_INT *b1, MP_INT *b2)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set(&new, b1);
|
|
|
|
mpz_sub(&new, &new, b2);
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_mul_big_big(MP_INT *b1, MP_INT *b2)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set(&new, b1);
|
|
|
|
mpz_mul(&new, &new, b2);
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* div i / b using temporary bigint new */
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_div_big_big(MP_INT *b1, MP_INT *b2)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
2008-11-28 15:54:08 +00:00
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_init_set(&new, b1);
|
2008-11-28 15:54:08 +00:00
|
|
|
if (yap_flags[INTEGER_ROUNDING_FLAG] == 0) {
|
2008-12-04 23:33:32 +00:00
|
|
|
mpz_tdiv_q(&new, &new, b2);
|
2009-02-09 21:56:40 +00:00
|
|
|
} else {
|
|
|
|
mpz_fdiv_q(&new, &new, b2);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_and_big_big(MP_INT *b1, MP_INT *b2)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set(&new, b1);
|
|
|
|
mpz_and(&new, &new, b2);
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 23:33:32 +00:00
|
|
|
Term
|
|
|
|
Yap_gmp_ior_big_big(MP_INT *b1, MP_INT *b2)
|
2008-11-28 15:54:08 +00:00
|
|
|
{
|
2008-12-04 23:33:32 +00:00
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set(&new, b1);
|
|
|
|
mpz_ior(&new, &new, b2);
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
2009-06-05 16:55:28 +01:00
|
|
|
Term
|
|
|
|
Yap_gmp_mod_big_big(MP_INT *b1, MP_INT *b2)
|
|
|
|
{
|
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init(&new);
|
|
|
|
mpz_fdiv_r(&new, b1, b2);
|
|
|
|
return MkBigAndClose(&new);
|
|
|
|
}
|
|
|
|
|
|
|
|
Term
|
|
|
|
Yap_gmp_mod_big_int(MP_INT *b, Int i2)
|
|
|
|
{
|
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set_si(&new, i2);
|
|
|
|
mpz_fdiv_r(&new, b, &new);
|
|
|
|
return MkBigAndClose(&new);
|
|
|
|
}
|
|
|
|
|
|
|
|
Term
|
|
|
|
Yap_gmp_mod_int_big(Int i1, MP_INT *b)
|
|
|
|
{
|
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set_si(&new, i1);
|
|
|
|
mpz_fdiv_r(&new, &new, b);
|
|
|
|
RBIG(&new);
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:56:40 +00:00
|
|
|
Term
|
2008-11-28 15:54:08 +00:00
|
|
|
Yap_gmp_add_float_big(Float d, MP_INT *b)
|
|
|
|
{
|
2009-02-09 21:56:40 +00:00
|
|
|
return MkFloatTerm(d+mpz_get_d(b));
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
2009-02-09 21:56:40 +00:00
|
|
|
Term
|
2008-11-28 15:54:08 +00:00
|
|
|
Yap_gmp_sub_float_big(Float d, MP_INT *b)
|
|
|
|
{
|
2009-02-09 21:56:40 +00:00
|
|
|
return MkFloatTerm(d-mpz_get_d(b));
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
2009-02-09 21:56:40 +00:00
|
|
|
Term
|
2008-11-28 15:54:08 +00:00
|
|
|
Yap_gmp_sub_big_float(MP_INT *b, Float d)
|
|
|
|
{
|
2009-02-09 21:56:40 +00:00
|
|
|
return MkFloatTerm(mpz_get_d(b)-d);
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
2009-02-09 21:56:40 +00:00
|
|
|
Term
|
2008-11-28 15:54:08 +00:00
|
|
|
Yap_gmp_mul_float_big(Float d, MP_INT *b)
|
|
|
|
{
|
2009-02-09 21:56:40 +00:00
|
|
|
return MkFloatTerm(d*mpz_get_d(b));
|
2008-11-28 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
2009-04-25 01:03:00 +01:00
|
|
|
Term
|
|
|
|
Yap_gmp_exp_ints(Int i1, Int i2)
|
|
|
|
{
|
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
mpz_init_set_si(&new, i1);
|
|
|
|
mpz_pow_ui (&new, &new, (unsigned long int)i2);
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2009-04-25 01:03:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Term
|
|
|
|
Yap_gmp_exp_big_int(MP_INT *b, Int i)
|
|
|
|
{
|
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
if (b > 0) {
|
|
|
|
mpz_init(&new);
|
|
|
|
mpz_pow_ui (&new, b, (unsigned long int)i);
|
|
|
|
} else {
|
|
|
|
MP_INT new;
|
|
|
|
if (b==0) return MkIntTerm(1);
|
|
|
|
|
|
|
|
mpz_init_set_si(&new, i);
|
|
|
|
mpz_powm (&new, b, &new, b);
|
|
|
|
}
|
2009-04-25 17:01:33 +01:00
|
|
|
return MkBigAndClose(&new);
|
2009-04-25 01:03:00 +01:00
|
|
|
}
|
|
|
|
|
2009-06-05 18:18:10 +01:00
|
|
|
Term
|
|
|
|
Yap_gmp_big_from_64bits(YAP_LONG_LONG i)
|
|
|
|
{
|
|
|
|
char s[64];
|
|
|
|
MP_INT new;
|
|
|
|
|
|
|
|
sprintf(s, "%lld", (long long int)i);
|
|
|
|
mpz_init_set_str (&new, s, 10);
|
|
|
|
return MkBigAndClose(&new);
|
|
|
|
}
|
|
|
|
|
2008-11-28 15:54:08 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|