add isl_aff_mod_val
[platform/upstream/isl.git] / isl_val_gmp.c
1 #include <isl/val_gmp.h>
2 #include <isl_val_private.h>
3
4 /* Return a reference to an isl_val representing the integer "z".
5  */
6 __isl_give isl_val *isl_val_int_from_gmp(isl_ctx *ctx, mpz_t z)
7 {
8         isl_val *v;
9
10         v = isl_val_alloc(ctx);
11         if (!v)
12                 return NULL;
13
14         isl_int_set(v->n, z);
15         isl_int_set_si(v->d, 1);
16
17         return v;
18 }
19
20 /* Return a reference to an isl_val representing the rational value "n"/"d".
21  */
22 __isl_give isl_val *isl_val_from_gmp(isl_ctx *ctx, const mpz_t n, const mpz_t d)
23 {
24         isl_val *v;
25
26         v = isl_val_alloc(ctx);
27         if (!v)
28                 return NULL;
29
30         isl_int_set(v->n, n);
31         isl_int_set(v->d, d);
32
33         return isl_val_normalize(v);
34 }
35
36 /* Extract the numerator of a rational value "v" in "z".
37  *
38  * If "v" is not a rational value, then the result is undefined.
39  */
40 int isl_val_get_num_gmp(__isl_keep isl_val *v, mpz_t z)
41 {
42         if (!v)
43                 return -1;
44         if (!isl_val_is_rat(v))
45                 isl_die(isl_val_get_ctx(v), isl_error_invalid,
46                         "expecting rational value", return -1);
47         mpz_set(z, v->n);
48         return 0;
49 }
50
51 /* Extract the denominator of a rational value "v" in "z".
52  *
53  * If "v" is not a rational value, then the result is undefined.
54  */
55 int isl_val_get_den_gmp(__isl_keep isl_val *v, mpz_t z)
56 {
57         if (!v)
58                 return -1;
59         if (!isl_val_is_rat(v))
60                 isl_die(isl_val_get_ctx(v), isl_error_invalid,
61                         "expecting rational value", return -1);
62         mpz_set(z, v->d);
63         return 0;
64 }