X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=isl_val_gmp.c;h=42e8d44467186be64a5c5d9d4f452626628b2105;hb=19596bc4e5cd282b2e75d17077b1aaaeacbfd6f9;hp=aed8bb1824b88f31d0d9693a89a1d4b9988a1f45;hpb=4fff6cf84785a942c0676848ea2ae4115820b327;p=platform%2Fupstream%2Fisl.git diff --git a/isl_val_gmp.c b/isl_val_gmp.c index aed8bb1..42e8d44 100644 --- a/isl_val_gmp.c +++ b/isl_val_gmp.c @@ -1,3 +1,4 @@ +#include #include #include @@ -62,3 +63,66 @@ int isl_val_get_den_gmp(__isl_keep isl_val *v, mpz_t z) mpz_set(z, v->d); return 0; } + +/* Return a reference to an isl_val representing the unsigned + * integer value stored in the "n" chunks of size "size" at "chunks". + * The least significant chunk is assumed to be stored first. + */ +__isl_give isl_val *isl_val_int_from_chunks(isl_ctx *ctx, size_t n, + size_t size, const void *chunks) +{ + isl_val *v; + + v = isl_val_alloc(ctx); + if (!v) + return NULL; + + mpz_import(v->n, n, -1, size, 0, 0, chunks); + isl_int_set_si(v->d, 1); + + return v; +} + +/* Return the number of chunks of size "size" required to + * store the absolute value of the numerator of "v". + */ +size_t isl_val_n_abs_num_chunks(__isl_keep isl_val *v, size_t size) +{ + if (!v) + return 0; + + if (!isl_val_is_rat(v)) + isl_die(isl_val_get_ctx(v), isl_error_invalid, + "expecting rational value", return 0); + + size *= 8; + return (mpz_sizeinbase(v->n, 2) + size - 1) / size; +} + +/* Store a representation of the absolute value of the numerator of "v" + * in terms of chunks of size "size" at "chunks". + * The least significant chunk is stored first. + * The number of chunks in the result can be obtained by calling + * isl_val_n_abs_num_chunks. The user is responsible for allocating + * enough memory to store the results. + * + * In the special case of a zero value, isl_val_n_abs_num_chunks will + * return one, while mpz_export will not fill in any chunks. We therefore + * do it ourselves. + */ +int isl_val_get_abs_num_chunks(__isl_keep isl_val *v, size_t size, + void *chunks) +{ + if (!v || !chunks) + return -1; + + if (!isl_val_is_rat(v)) + isl_die(isl_val_get_ctx(v), isl_error_invalid, + "expecting rational value", return -1); + + mpz_export(chunks, NULL, -1, size, 0, 0, v->n); + if (isl_val_is_zero(v)) + memset(chunks, 0, size); + + return 0; +}