2 #include <isl/val_gmp.h>
3 #include <isl_val_private.h>
5 /* Return a reference to an isl_val representing the integer "z".
7 __isl_give isl_val
*isl_val_int_from_gmp(isl_ctx
*ctx
, mpz_t z
)
11 v
= isl_val_alloc(ctx
);
16 isl_int_set_si(v
->d
, 1);
21 /* Return a reference to an isl_val representing the rational value "n"/"d".
23 __isl_give isl_val
*isl_val_from_gmp(isl_ctx
*ctx
, const mpz_t n
, const mpz_t d
)
27 v
= isl_val_alloc(ctx
);
34 return isl_val_normalize(v
);
37 /* Extract the numerator of a rational value "v" in "z".
39 * If "v" is not a rational value, then the result is undefined.
41 int isl_val_get_num_gmp(__isl_keep isl_val
*v
, mpz_t z
)
45 if (!isl_val_is_rat(v
))
46 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
47 "expecting rational value", return -1);
52 /* Extract the denominator of a rational value "v" in "z".
54 * If "v" is not a rational value, then the result is undefined.
56 int isl_val_get_den_gmp(__isl_keep isl_val
*v
, mpz_t z
)
60 if (!isl_val_is_rat(v
))
61 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
62 "expecting rational value", return -1);
67 /* Return a reference to an isl_val representing the unsigned
68 * integer value stored in the "n" chunks of size "size" at "chunks".
69 * The least significant chunk is assumed to be stored first.
71 __isl_give isl_val
*isl_val_int_from_chunks(isl_ctx
*ctx
, size_t n
,
72 size_t size
, const void *chunks
)
76 v
= isl_val_alloc(ctx
);
80 mpz_import(v
->n
, n
, -1, size
, 0, 0, chunks
);
81 isl_int_set_si(v
->d
, 1);
86 /* Return the number of chunks of size "size" required to
87 * store the absolute value of the numerator of "v".
89 isl_size
isl_val_n_abs_num_chunks(__isl_keep isl_val
*v
, size_t size
)
92 return isl_size_error
;
94 if (!isl_val_is_rat(v
))
95 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
96 "expecting rational value", return isl_size_error
);
99 return (mpz_sizeinbase(v
->n
, 2) + size
- 1) / size
;
102 /* Store a representation of the absolute value of the numerator of "v"
103 * in terms of chunks of size "size" at "chunks".
104 * The least significant chunk is stored first.
105 * The number of chunks in the result can be obtained by calling
106 * isl_val_n_abs_num_chunks. The user is responsible for allocating
107 * enough memory to store the results.
109 * In the special case of a zero value, isl_val_n_abs_num_chunks will
110 * return one, while mpz_export will not fill in any chunks. We therefore
113 isl_stat
isl_val_get_abs_num_chunks(__isl_keep isl_val
*v
, size_t size
,
117 return isl_stat_error
;
119 if (!isl_val_is_rat(v
))
120 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
121 "expecting rational value", return isl_stat_error
);
123 mpz_export(chunks
, NULL
, -1, size
, 0, 0, v
->n
);
124 if (isl_val_is_zero(v
))
125 memset(chunks
, 0, size
);