Remove dependency on GMP, so that it is easier to run the bench suite.
This commit is contained in:
1 parent
18710567f2
commit
bc83fe43e5
9 files changed
+142
-74
No files matched your search
+83
-14
@@ -1,35 +1,104 @@
|
||||
#ifndef MY_MPZ_T
|
||||
#define MY_MPZ_T
|
||||
|
||||
#include <gmp.h>
|
||||
#include "m-string.h"
|
||||
|
||||
static void my_mpz_out_str(FILE *f, const mpz_t z)
|
||||
/* This is a trivial encapsulation of an opquae structure.
|
||||
Used for test purpose only.*/
|
||||
typedef struct my_mpz_s {
|
||||
unsigned int n;
|
||||
unsigned int a;
|
||||
unsigned int *ptr;
|
||||
} my_mpz_t[1];
|
||||
|
||||
static void my_mpz_init(my_mpz_t z)
|
||||
{
|
||||
mpz_out_str (f, 10, z);
|
||||
z->n = 1;
|
||||
z->a = 1;
|
||||
z->ptr = calloc(1, sizeof(unsigned int));
|
||||
if (!z->ptr) abort();
|
||||
}
|
||||
|
||||
static bool my_mpz_in_str(mpz_t z, FILE *f)
|
||||
static void my_mpz_clear(my_mpz_t z)
|
||||
{
|
||||
return mpz_inp_str (z, f, 10) > 0;
|
||||
free(z->ptr);
|
||||
z->ptr = NULL;
|
||||
}
|
||||
|
||||
static bool my_mpz_equal_p(const mpz_t z1, const mpz_t z2)
|
||||
static void my_mpz_init_set(my_mpz_t d, const my_mpz_t s)
|
||||
{
|
||||
return mpz_cmp (z1, z2) == 0;
|
||||
d->n = s->n;
|
||||
d->a = s->n;
|
||||
d->ptr = calloc (s->n, sizeof(unsigned int));
|
||||
memcpy(d->ptr, s->ptr, sizeof(unsigned int) * s->n);
|
||||
}
|
||||
|
||||
static void my_mpz_str(string_t str, const mpz_t z, bool append)
|
||||
static void my_mpz_set(my_mpz_t d, const my_mpz_t s)
|
||||
{
|
||||
char *s = mpz_get_str(NULL, 10, z);
|
||||
assert (s != NULL);
|
||||
if (append) string_cat_str (str, s);
|
||||
else string_set_str (str, s);
|
||||
free(s);
|
||||
my_mpz_clear(d);
|
||||
my_mpz_init_set(d, s);
|
||||
}
|
||||
|
||||
static void my_mpz_set_ui(my_mpz_t d, unsigned int v)
|
||||
{
|
||||
d->n = 1;
|
||||
d->ptr[0] = v;
|
||||
}
|
||||
|
||||
static void my_mpz_init_set_ui(my_mpz_t d, unsigned int v)
|
||||
{
|
||||
my_mpz_init(d);
|
||||
my_mpz_set_ui(d, v);
|
||||
}
|
||||
|
||||
static unsigned int my_mpz_get_ui(const my_mpz_t z)
|
||||
{
|
||||
return z->ptr[0];
|
||||
}
|
||||
|
||||
static void my_mpz_add(my_mpz_t d, const my_mpz_t a, const my_mpz_t b)
|
||||
{
|
||||
d->ptr[0] = a->ptr[0] + b->ptr[0];
|
||||
}
|
||||
|
||||
static void my_mpz_out_str(FILE *f, const my_mpz_t z)
|
||||
{
|
||||
assert(z->n == 1);
|
||||
fprintf(f, "%u", z->ptr[0]);
|
||||
}
|
||||
|
||||
static bool my_mpz_in_str(my_mpz_t z, FILE *f)
|
||||
{
|
||||
z->n = 1;
|
||||
return fscanf(f, "%u", &z->ptr[0]) == 1;
|
||||
}
|
||||
|
||||
static bool my_mpz_equal_p(const my_mpz_t z1, const my_mpz_t z2)
|
||||
{
|
||||
if (z1->n != z2->n) return false;
|
||||
return memcmp(z1->ptr, z2->ptr, z1->n*sizeof(unsigned int)) == 0;
|
||||
}
|
||||
|
||||
static int my_mpz_cmp(const my_mpz_t z1, const my_mpz_t z2)
|
||||
{
|
||||
if (z1->n != z2->n) return z1-> n < z2->n ? -1 : 1;
|
||||
return memcmp(z1->ptr, z2->ptr, z1->n*sizeof(unsigned int));
|
||||
}
|
||||
|
||||
static int my_mpz_cmp_ui(const my_mpz_t z1, unsigned int z2)
|
||||
{
|
||||
if (z1->n != 1) return z1-> n < 1 ? -1 : 1;
|
||||
return memcmp(z1->ptr, &z2, sizeof(unsigned int));
|
||||
}
|
||||
|
||||
static void my_mpz_str(string_t str, const my_mpz_t z, bool append)
|
||||
{
|
||||
if (append) string_cat_printf (str, "%u", z->ptr[0]);
|
||||
else string_printf (str, "%u", z->ptr[0]);
|
||||
}
|
||||
|
||||
#define MYMPZ_OPLIST \
|
||||
(INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear), \
|
||||
(INIT(my_mpz_init), INIT_SET(my_mpz_init_set), SET(my_mpz_set), CLEAR(my_mpz_clear), \
|
||||
OUT_STR(my_mpz_out_str), IN_STR(my_mpz_in_str), EQUAL(my_mpz_equal_p), \
|
||||
GET_STR(my_mpz_str) )
|
||||
|
||||
|
||||
Reference in new issue
Block a user