Line data Source code
1 : #include <string.h>
2 : #include <isl/val_gmp.h>
3 : #include <isl_val_private.h>
4 :
5 : /* Return a reference to an isl_val representing the integer "z".
6 : */
7 0 : __isl_give isl_val *isl_val_int_from_gmp(isl_ctx *ctx, mpz_t z)
8 : {
9 : isl_val *v;
10 :
11 0 : v = isl_val_alloc(ctx);
12 0 : if (!v)
13 0 : return NULL;
14 :
15 0 : isl_int_set(v->n, z);
16 0 : isl_int_set_si(v->d, 1);
17 :
18 0 : return v;
19 : }
20 :
21 : /* Return a reference to an isl_val representing the rational value "n"/"d".
22 : */
23 0 : __isl_give isl_val *isl_val_from_gmp(isl_ctx *ctx, const mpz_t n, const mpz_t d)
24 : {
25 : isl_val *v;
26 :
27 0 : v = isl_val_alloc(ctx);
28 0 : if (!v)
29 0 : return NULL;
30 :
31 0 : isl_int_set(v->n, n);
32 0 : isl_int_set(v->d, d);
33 :
34 0 : return isl_val_normalize(v);
35 : }
36 :
37 : /* Extract the numerator of a rational value "v" in "z".
38 : *
39 : * If "v" is not a rational value, then the result is undefined.
40 : */
41 0 : int isl_val_get_num_gmp(__isl_keep isl_val *v, mpz_t z)
42 : {
43 0 : if (!v)
44 0 : return -1;
45 0 : if (!isl_val_is_rat(v))
46 0 : isl_die(isl_val_get_ctx(v), isl_error_invalid,
47 : "expecting rational value", return -1);
48 0 : mpz_set(z, v->n);
49 0 : return 0;
50 : }
51 :
52 : /* Extract the denominator of a rational value "v" in "z".
53 : *
54 : * If "v" is not a rational value, then the result is undefined.
55 : */
56 0 : int isl_val_get_den_gmp(__isl_keep isl_val *v, mpz_t z)
57 : {
58 0 : if (!v)
59 0 : return -1;
60 0 : if (!isl_val_is_rat(v))
61 0 : isl_die(isl_val_get_ctx(v), isl_error_invalid,
62 : "expecting rational value", return -1);
63 0 : mpz_set(z, v->d);
64 0 : return 0;
65 : }
66 :
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.
70 : */
71 0 : __isl_give isl_val *isl_val_int_from_chunks(isl_ctx *ctx, size_t n,
72 : size_t size, const void *chunks)
73 : {
74 : isl_val *v;
75 :
76 0 : v = isl_val_alloc(ctx);
77 0 : if (!v)
78 0 : return NULL;
79 :
80 0 : mpz_import(v->n, n, -1, size, 0, 0, chunks);
81 0 : isl_int_set_si(v->d, 1);
82 :
83 0 : return v;
84 : }
85 :
86 : /* Return the number of chunks of size "size" required to
87 : * store the absolute value of the numerator of "v".
88 : */
89 0 : size_t isl_val_n_abs_num_chunks(__isl_keep isl_val *v, size_t size)
90 : {
91 0 : if (!v)
92 0 : return 0;
93 :
94 0 : if (!isl_val_is_rat(v))
95 0 : isl_die(isl_val_get_ctx(v), isl_error_invalid,
96 : "expecting rational value", return 0);
97 :
98 0 : size *= 8;
99 0 : return (mpz_sizeinbase(v->n, 2) + size - 1) / size;
100 : }
101 :
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.
108 : *
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
111 : * do it ourselves.
112 : */
113 0 : int isl_val_get_abs_num_chunks(__isl_keep isl_val *v, size_t size,
114 : void *chunks)
115 : {
116 0 : if (!v || !chunks)
117 0 : return -1;
118 :
119 0 : if (!isl_val_is_rat(v))
120 0 : isl_die(isl_val_get_ctx(v), isl_error_invalid,
121 : "expecting rational value", return -1);
122 :
123 0 : mpz_export(chunks, NULL, -1, size, 0, 0, v->n);
124 0 : if (isl_val_is_zero(v))
125 0 : memset(chunks, 0, size);
126 :
127 0 : return 0;
128 : }
|