From db14462a82ae5f772cfe88ee44dd444eb48096c1 Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Sat, 6 Apr 2013 10:33:43 +0200 Subject: [PATCH] add GMP specific isl_val functions Signed-off-by: Sven Verdoolaege --- Makefile.am | 2 ++ doc/user.pod | 21 +++++++++++++++++ include/isl/val_gmp.h | 13 +++++++++++ isl_val_gmp.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 include/isl/val_gmp.h create mode 100644 isl_val_gmp.c diff --git a/Makefile.am b/Makefile.am index be498c7..bb25f1d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -138,6 +138,7 @@ libisl_la_SOURCES = \ isl_union_map.c \ isl_union_map_private.h \ isl_val.c \ + isl_val_gmp.c \ isl_vec.c \ isl_version.c \ isl_vertices_private.h \ @@ -252,6 +253,7 @@ pkginclude_HEADERS = \ include/isl/union_set.h \ include/isl/union_set_type.h \ include/isl/val.h \ + include/isl/val_gmp.h \ include/isl/vec.h \ include/isl/version.h \ include/isl/vertices.h diff --git a/doc/user.pod b/doc/user.pod index 29bf606..70405ae 100644 --- a/doc/user.pod +++ b/doc/user.pod @@ -499,6 +499,27 @@ A value can be printed using __isl_give isl_printer *isl_printer_print_val( __isl_take isl_printer *p, __isl_keep isl_val *v); +=head3 GMP specific functions + +These functions are only available if C has been compiled with C +support. + +Specific integer and rational values can be created from C values using +the following functions. + + #include + __isl_give isl_val *isl_val_int_from_gmp(isl_ctx *ctx, + mpz_t z); + __isl_give isl_val *isl_val_from_gmp(isl_ctx *ctx, + const mpz_t n, const mpz_t d); + +The numerator and denominator of a rational value can be extracted as +C values using the following functions. + + #include + int isl_val_get_num_gmp(__isl_keep isl_val *v, mpz_t z); + int isl_val_get_den_gmp(__isl_keep isl_val *v, mpz_t z); + =head2 Integers (obsolescent) All operations on integers, mainly the coefficients diff --git a/include/isl/val_gmp.h b/include/isl/val_gmp.h new file mode 100644 index 0000000..1dd4da8 --- /dev/null +++ b/include/isl/val_gmp.h @@ -0,0 +1,13 @@ +#ifndef ISL_VAL_GMP_H +#define ISL_VAL_GMP_H + +#include +#include + +__isl_give isl_val *isl_val_int_from_gmp(isl_ctx *ctx, mpz_t z); +__isl_give isl_val *isl_val_from_gmp(isl_ctx *ctx, + const mpz_t n, const mpz_t d); +int isl_val_get_num_gmp(__isl_keep isl_val *v, mpz_t z); +int isl_val_get_den_gmp(__isl_keep isl_val *v, mpz_t z); + +#endif diff --git a/isl_val_gmp.c b/isl_val_gmp.c new file mode 100644 index 0000000..aed8bb1 --- /dev/null +++ b/isl_val_gmp.c @@ -0,0 +1,64 @@ +#include +#include + +/* Return a reference to an isl_val representing the integer "z". + */ +__isl_give isl_val *isl_val_int_from_gmp(isl_ctx *ctx, mpz_t z) +{ + isl_val *v; + + v = isl_val_alloc(ctx); + if (!v) + return NULL; + + isl_int_set(v->n, z); + isl_int_set_si(v->d, 1); + + return v; +} + +/* Return a reference to an isl_val representing the rational value "n"/"d". + */ +__isl_give isl_val *isl_val_from_gmp(isl_ctx *ctx, const mpz_t n, const mpz_t d) +{ + isl_val *v; + + v = isl_val_alloc(ctx); + if (!v) + return NULL; + + isl_int_set(v->n, n); + isl_int_set(v->d, d); + + return isl_val_normalize(v); +} + +/* Extract the numerator of a rational value "v" in "z". + * + * If "v" is not a rational value, then the result is undefined. + */ +int isl_val_get_num_gmp(__isl_keep isl_val *v, mpz_t z) +{ + if (!v) + return -1; + if (!isl_val_is_rat(v)) + isl_die(isl_val_get_ctx(v), isl_error_invalid, + "expecting rational value", return -1); + mpz_set(z, v->n); + return 0; +} + +/* Extract the denominator of a rational value "v" in "z". + * + * If "v" is not a rational value, then the result is undefined. + */ +int isl_val_get_den_gmp(__isl_keep isl_val *v, mpz_t z) +{ + if (!v) + return -1; + if (!isl_val_is_rat(v)) + isl_die(isl_val_get_ctx(v), isl_error_invalid, + "expecting rational value", return -1); + mpz_set(z, v->d); + return 0; +} -- 2.7.4