add isl_set_dim_residue_class
[platform/upstream/isl.git] / isl_equalities.c
1 #include "isl_mat.h"
2 #include "isl_seq.h"
3 #include "isl_map_private.h"
4 #include "isl_equalities.h"
5
6 /* Given a set of modulo constraints
7  *
8  *              c + A y = 0 mod d
9  *
10  * this function computes a particular solution y_0
11  *
12  * The input is given as a matrix B = [ c A ] and a vector d.
13  *
14  * The output is matrix containing the solution y_0 or
15  * a zero-column matrix if the constraints admit no integer solution.
16  *
17  * The given set of constrains is equivalent to
18  *
19  *              c + A y = -D x
20  *
21  * with D = diag d and x a fresh set of variables.
22  * Reducing both c and A modulo d does not change the
23  * value of y in the solution and may lead to smaller coefficients.
24  * Let M = [ D A ] and [ H 0 ] = M U, the Hermite normal form of M.
25  * Then
26  *                [ x ]
27  *              M [ y ] = - c
28  * and so
29  *                             [ x ]
30  *              [ H 0 ] U^{-1} [ y ] = - c
31  * Let
32  *              [ A ]          [ x ]
33  *              [ B ] = U^{-1} [ y ]
34  * then
35  *              H A + 0 B = -c
36  *
37  * so B may be chosen arbitrarily, e.g., B = 0, and then
38  *
39  *                     [ x ] = [ -c ]
40  *              U^{-1} [ y ] = [  0 ]
41  * or
42  *              [ x ]     [ -c ]
43  *              [ y ] = U [  0 ]
44  * specifically,
45  *
46  *              y = U_{2,1} (-c)
47  *
48  * If any of the coordinates of this y are non-integer
49  * then the constraints admit no integer solution and
50  * a zero-column matrix is returned.
51  */
52 static struct isl_mat *particular_solution(struct isl_mat *B, struct isl_vec *d)
53 {
54         int i, j;
55         struct isl_mat *M = NULL;
56         struct isl_mat *C = NULL;
57         struct isl_mat *U = NULL;
58         struct isl_mat *H = NULL;
59         struct isl_mat *cst = NULL;
60         struct isl_mat *T = NULL;
61
62         M = isl_mat_alloc(B->ctx, B->n_row, B->n_row + B->n_col - 1);
63         C = isl_mat_alloc(B->ctx, 1 + B->n_row, 1);
64         if (!M || !C)
65                 goto error;
66         isl_int_set_si(C->row[0][0], 1);
67         for (i = 0; i < B->n_row; ++i) {
68                 isl_seq_clr(M->row[i], B->n_row);
69                 isl_int_set(M->row[i][i], d->block.data[i]);
70                 isl_int_neg(C->row[1 + i][0], B->row[i][0]);
71                 isl_int_fdiv_r(C->row[1+i][0], C->row[1+i][0], M->row[i][i]);
72                 for (j = 0; j < B->n_col - 1; ++j)
73                         isl_int_fdiv_r(M->row[i][B->n_row + j],
74                                         B->row[i][1 + j], M->row[i][i]);
75         }
76         M = isl_mat_left_hermite(M, 0, &U, NULL);
77         if (!M || !U)
78                 goto error;
79         H = isl_mat_sub_alloc(B->ctx, M->row, 0, B->n_row, 0, B->n_row);
80         H = isl_mat_lin_to_aff(H);
81         C = isl_mat_inverse_product(H, C);
82         if (!C)
83                 goto error;
84         for (i = 0; i < B->n_row; ++i) {
85                 if (!isl_int_is_divisible_by(C->row[1+i][0], C->row[0][0]))
86                         break;
87                 isl_int_divexact(C->row[1+i][0], C->row[1+i][0], C->row[0][0]);
88         }
89         if (i < B->n_row)
90                 cst = isl_mat_alloc(B->ctx, B->n_row, 0);
91         else
92                 cst = isl_mat_sub_alloc(C->ctx, C->row, 1, B->n_row, 0, 1);
93         T = isl_mat_sub_alloc(U->ctx, U->row, B->n_row, B->n_col - 1, 0, B->n_row);
94         cst = isl_mat_product(T, cst);
95         isl_mat_free(M);
96         isl_mat_free(C);
97         isl_mat_free(U);
98         return cst;
99 error:
100         isl_mat_free(M);
101         isl_mat_free(C);
102         isl_mat_free(U);
103         return NULL;
104 }
105
106 /* Compute and return the matrix
107  *
108  *              U_1^{-1} diag(d_1, 1, ..., 1)
109  *
110  * with U_1 the unimodular completion of the first (and only) row of B.
111  * The columns of this matrix generate the lattice that satisfies
112  * the single (linear) modulo constraint.
113  */
114 static struct isl_mat *parameter_compression_1(
115                         struct isl_mat *B, struct isl_vec *d)
116 {
117         struct isl_mat *U;
118
119         U = isl_mat_alloc(B->ctx, B->n_col - 1, B->n_col - 1);
120         if (!U)
121                 return NULL;
122         isl_seq_cpy(U->row[0], B->row[0] + 1, B->n_col - 1);
123         U = isl_mat_unimodular_complete(U, 1);
124         U = isl_mat_right_inverse(U);
125         if (!U)
126                 return NULL;
127         isl_mat_col_mul(U, 0, d->block.data[0], 0);
128         U = isl_mat_lin_to_aff(U);
129         return U;
130 }
131
132 /* Compute a common lattice of solutions to the linear modulo
133  * constraints specified by B and d.
134  * See also the documentation of isl_mat_parameter_compression.
135  * We put the matrix
136  * 
137  *              A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ]
138  *
139  * on a common denominator.  This denominator D is the lcm of modulos d.
140  * Since L_i = U_i^{-1} diag(d_i, 1, ... 1), we have
141  * L_i^{-T} = U_i^T diag(d_i, 1, ... 1)^{-T} = U_i^T diag(1/d_i, 1, ..., 1).
142  * Putting this on the common denominator, we have
143  * D * L_i^{-T} = U_i^T diag(D/d_i, D, ..., D).
144  */
145 static struct isl_mat *parameter_compression_multi(
146                         struct isl_mat *B, struct isl_vec *d)
147 {
148         int i, j, k;
149         isl_int D;
150         struct isl_mat *A = NULL, *U = NULL;
151         struct isl_mat *T;
152         unsigned size;
153
154         isl_int_init(D);
155
156         isl_vec_lcm(d, &D);
157
158         size = B->n_col - 1;
159         A = isl_mat_alloc(B->ctx, size, B->n_row * size);
160         U = isl_mat_alloc(B->ctx, size, size);
161         if (!U || !A)
162                 goto error;
163         for (i = 0; i < B->n_row; ++i) {
164                 isl_seq_cpy(U->row[0], B->row[i] + 1, size);
165                 U = isl_mat_unimodular_complete(U, 1);
166                 if (!U)
167                         goto error;
168                 isl_int_divexact(D, D, d->block.data[i]);
169                 for (k = 0; k < U->n_col; ++k)
170                         isl_int_mul(A->row[k][i*size+0], D, U->row[0][k]);
171                 isl_int_mul(D, D, d->block.data[i]);
172                 for (j = 1; j < U->n_row; ++j)
173                         for (k = 0; k < U->n_col; ++k)
174                                 isl_int_mul(A->row[k][i*size+j],
175                                                 D, U->row[j][k]);
176         }
177         A = isl_mat_left_hermite(A, 0, NULL, NULL);
178         T = isl_mat_sub_alloc(A->ctx, A->row, 0, A->n_row, 0, A->n_row);
179         T = isl_mat_lin_to_aff(T);
180         isl_int_set(T->row[0][0], D);
181         T = isl_mat_right_inverse(T);
182         isl_assert(T->ctx, isl_int_is_one(T->row[0][0]), goto error);
183         T = isl_mat_transpose(T);
184         isl_mat_free(A);
185         isl_mat_free(U);
186
187         isl_int_clear(D);
188         return T;
189 error:
190         isl_mat_free(A);
191         isl_mat_free(U);
192         isl_int_clear(D);
193         return NULL;
194 }
195
196 /* Given a set of modulo constraints
197  *
198  *              c + A y = 0 mod d
199  *
200  * this function returns an affine transformation T,
201  *
202  *              y = T y'
203  *
204  * that bijectively maps the integer vectors y' to integer
205  * vectors y that satisfy the modulo constraints.
206  *
207  * This function is inspired by Section 2.5.3
208  * of B. Meister, "Stating and Manipulating Periodicity in the Polytope
209  * Model.  Applications to Program Analysis and Optimization".
210  * However, the implementation only follows the algorithm of that
211  * section for computing a particular solution and not for computing
212  * a general homogeneous solution.  The latter is incomplete and
213  * may remove some valid solutions.
214  * Instead, we use an adaptation of the algorithm in Section 7 of
215  * B. Meister, S. Verdoolaege, "Polynomial Approximations in the Polytope
216  * Model: Bringing the Power of Quasi-Polynomials to the Masses".
217  *
218  * The input is given as a matrix B = [ c A ] and a vector d.
219  * Each element of the vector d corresponds to a row in B.
220  * The output is a lower triangular matrix.
221  * If no integer vector y satisfies the given constraints then
222  * a matrix with zero columns is returned.
223  *
224  * We first compute a particular solution y_0 to the given set of
225  * modulo constraints in particular_solution.  If no such solution
226  * exists, then we return a zero-columned transformation matrix.
227  * Otherwise, we compute the generic solution to
228  *
229  *              A y = 0 mod d
230  *
231  * That is we want to compute G such that
232  *
233  *              y = G y''
234  *
235  * with y'' integer, describes the set of solutions.
236  *
237  * We first remove the common factors of each row.
238  * In particular if gcd(A_i,d_i) != 1, then we divide the whole
239  * row i (including d_i) by this common factor.  If afterwards gcd(A_i) != 1,
240  * then we divide this row of A by the common factor, unless gcd(A_i) = 0.
241  * In the later case, we simply drop the row (in both A and d).
242  *
243  * If there are no rows left in A, the G is the identity matrix. Otherwise,
244  * for each row i, we now determine the lattice of integer vectors
245  * that satisfies this row.  Let U_i be the unimodular extension of the
246  * row A_i.  This unimodular extension exists because gcd(A_i) = 1.
247  * The first component of
248  *
249  *              y' = U_i y
250  *
251  * needs to be a multiple of d_i.  Let y' = diag(d_i, 1, ..., 1) y''.
252  * Then,
253  *
254  *              y = U_i^{-1} diag(d_i, 1, ..., 1) y''
255  *
256  * for arbitrary integer vectors y''.  That is, y belongs to the lattice
257  * generated by the columns of L_i = U_i^{-1} diag(d_i, 1, ..., 1).
258  * If there is only one row, then G = L_1.
259  *
260  * If there is more than one row left, we need to compute the intersection
261  * of the lattices.  That is, we need to compute an L such that
262  *
263  *              L = L_i L_i'    for all i
264  *
265  * with L_i' some integer matrices.  Let A be constructed as follows
266  *
267  *              A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ]
268  *
269  * and computed the Hermite Normal Form of A = [ H 0 ] U
270  * Then,
271  *
272  *              L_i^{-T} = H U_{1,i}
273  *
274  * or
275  *
276  *              H^{-T} = L_i U_{1,i}^T
277  *
278  * In other words G = L = H^{-T}.
279  * To ensure that G is lower triangular, we compute and use its Hermite
280  * normal form.
281  *
282  * The affine transformation matrix returned is then
283  *
284  *              [  1   0  ]
285  *              [ y_0  G  ]
286  *
287  * as any y = y_0 + G y' with y' integer is a solution to the original
288  * modulo constraints.
289  */
290 struct isl_mat *isl_mat_parameter_compression(
291                         struct isl_mat *B, struct isl_vec *d)
292 {
293         int i;
294         struct isl_mat *cst = NULL;
295         struct isl_mat *T = NULL;
296         isl_int D;
297
298         if (!B || !d)
299                 goto error;
300         isl_assert(B->ctx, B->n_row == d->size, goto error);
301         cst = particular_solution(B, d);
302         if (!cst)
303                 goto error;
304         if (cst->n_col == 0) {
305                 T = isl_mat_alloc(B->ctx, B->n_col, 0);
306                 isl_mat_free(cst);
307                 isl_mat_free(B);
308                 isl_vec_free(d);
309                 return T;
310         }
311         isl_int_init(D);
312         /* Replace a*g*row = 0 mod g*m by row = 0 mod m */
313         for (i = 0; i < B->n_row; ++i) {
314                 isl_seq_gcd(B->row[i] + 1, B->n_col - 1, &D);
315                 if (isl_int_is_one(D))
316                         continue;
317                 if (isl_int_is_zero(D)) {
318                         B = isl_mat_drop_rows(B, i, 1);
319                         d = isl_vec_cow(d);
320                         if (!B || !d)
321                                 goto error2;
322                         isl_seq_cpy(d->block.data+i, d->block.data+i+1,
323                                                         d->size - (i+1));
324                         d->size--;
325                         i--;
326                         continue;
327                 }
328                 B = isl_mat_cow(B);
329                 if (!B)
330                         goto error2;
331                 isl_seq_scale_down(B->row[i] + 1, B->row[i] + 1, D, B->n_col-1);
332                 isl_int_gcd(D, D, d->block.data[i]);
333                 d = isl_vec_cow(d);
334                 if (!d)
335                         goto error2;
336                 isl_int_divexact(d->block.data[i], d->block.data[i], D);
337         }
338         isl_int_clear(D);
339         if (B->n_row == 0)
340                 T = isl_mat_identity(B->ctx, B->n_col);
341         else if (B->n_row == 1)
342                 T = parameter_compression_1(B, d);
343         else
344                 T = parameter_compression_multi(B, d);
345         T = isl_mat_left_hermite(T, 0, NULL, NULL);
346         if (!T)
347                 goto error;
348         isl_mat_sub_copy(T->ctx, T->row + 1, cst->row, cst->n_row, 0, 0, 1);
349         isl_mat_free(cst);
350         isl_mat_free(B);
351         isl_vec_free(d);
352         return T;
353 error2:
354         isl_int_clear(D);
355 error:
356         isl_mat_free(cst);
357         isl_mat_free(B);
358         isl_vec_free(d);
359         return NULL;
360 }
361
362 /* Given a set of equalities
363  *
364  *              M x - c = 0
365  *
366  * this function computes unimodular transformation from a lower-dimensional
367  * space to the original space that bijectively maps the integer points x'
368  * in the lower-dimensional space to the integer points x in the original
369  * space that satisfy the equalities.
370  *
371  * The input is given as a matrix B = [ -c M ] and the out is a
372  * matrix that maps [1 x'] to [1 x].
373  * If T2 is not NULL, then *T2 is set to a matrix mapping [1 x] to [1 x'].
374  *
375  * First compute the (left) Hermite normal form of M,
376  *
377  *              M [U1 U2] = M U = H = [H1 0]
378  * or
379  *                            M = H Q = [H1 0] [Q1]
380  *                                             [Q2]
381  *
382  * with U, Q unimodular, Q = U^{-1} (and H lower triangular).
383  * Define the transformed variables as
384  *
385  *              x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x
386  *                          [ x2' ]           [Q2]
387  *
388  * The equalities then become
389  *
390  *              H1 x1' - c = 0   or   x1' = H1^{-1} c = c'
391  *
392  * If any of the c' is non-integer, then the original set has no
393  * integer solutions (since the x' are a unimodular transformation
394  * of the x).
395  * Otherwise, the transformation is given by
396  *
397  *              x = U1 H1^{-1} c + U2 x2'
398  *
399  * The inverse transformation is simply
400  *
401  *              x2' = Q2 x
402  */
403 struct isl_mat *isl_mat_variable_compression(struct isl_mat *B,
404         struct isl_mat **T2)
405 {
406         int i;
407         struct isl_mat *H = NULL, *C = NULL, *H1, *U = NULL, *U1, *U2, *TC;
408         unsigned dim;
409
410         if (T2)
411                 *T2 = NULL;
412         if (!B)
413                 goto error;
414
415         dim = B->n_col - 1;
416         H = isl_mat_sub_alloc(B->ctx, B->row, 0, B->n_row, 1, dim);
417         H = isl_mat_left_hermite(H, 0, &U, T2);
418         if (!H || !U || (T2 && !*T2))
419                 goto error;
420         if (T2) {
421                 *T2 = isl_mat_drop_rows(*T2, 0, B->n_row);
422                 *T2 = isl_mat_lin_to_aff(*T2);
423                 if (!*T2)
424                         goto error;
425         }
426         C = isl_mat_alloc(B->ctx, 1+B->n_row, 1);
427         if (!C)
428                 goto error;
429         isl_int_set_si(C->row[0][0], 1);
430         isl_mat_sub_neg(C->ctx, C->row+1, B->row, B->n_row, 0, 0, 1);
431         H1 = isl_mat_sub_alloc(H->ctx, H->row, 0, H->n_row, 0, H->n_row);
432         H1 = isl_mat_lin_to_aff(H1);
433         TC = isl_mat_inverse_product(H1, C);
434         if (!TC)
435                 goto error;
436         isl_mat_free(H);
437         if (!isl_int_is_one(TC->row[0][0])) {
438                 for (i = 0; i < B->n_row; ++i) {
439                         if (!isl_int_is_divisible_by(TC->row[1+i][0], TC->row[0][0])) {
440                                 struct isl_ctx *ctx = B->ctx;
441                                 isl_mat_free(B);
442                                 isl_mat_free(TC);
443                                 isl_mat_free(U);
444                                 if (T2) {
445                                         isl_mat_free(*T2);
446                                         *T2 = NULL;
447                                 }
448                                 return isl_mat_alloc(ctx, 1 + dim, 0);
449                         }
450                         isl_seq_scale_down(TC->row[1+i], TC->row[1+i], TC->row[0][0], 1);
451                 }
452                 isl_int_set_si(TC->row[0][0], 1);
453         }
454         U1 = isl_mat_sub_alloc(U->ctx, U->row, 0, U->n_row, 0, B->n_row);
455         U1 = isl_mat_lin_to_aff(U1);
456         U2 = isl_mat_sub_alloc(U->ctx, U->row, 0, U->n_row,
457                                 B->n_row, U->n_row - B->n_row);
458         U2 = isl_mat_lin_to_aff(U2);
459         isl_mat_free(U);
460         TC = isl_mat_product(U1, TC);
461         TC = isl_mat_aff_direct_sum(TC, U2);
462
463         isl_mat_free(B);
464
465         return TC;
466 error:
467         isl_mat_free(B);
468         isl_mat_free(H);
469         isl_mat_free(U);
470         if (T2) {
471                 isl_mat_free(*T2);
472                 *T2 = NULL;
473         }
474         return NULL;
475 }
476
477 /* Use the n equalities of bset to unimodularly transform the
478  * variables x such that n transformed variables x1' have a constant value
479  * and rewrite the constraints of bset in terms of the remaining
480  * transformed variables x2'.  The matrix pointed to by T maps
481  * the new variables x2' back to the original variables x, while T2
482  * maps the original variables to the new variables.
483  */
484 static struct isl_basic_set *compress_variables(
485         struct isl_basic_set *bset, struct isl_mat **T, struct isl_mat **T2)
486 {
487         struct isl_mat *B, *TC;
488         unsigned dim;
489
490         if (T)
491                 *T = NULL;
492         if (T2)
493                 *T2 = NULL;
494         if (!bset)
495                 goto error;
496         isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
497         isl_assert(bset->ctx, bset->n_div == 0, goto error);
498         dim = isl_basic_set_n_dim(bset);
499         isl_assert(bset->ctx, bset->n_eq <= dim, goto error);
500         if (bset->n_eq == 0)
501                 return bset;
502
503         B = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq, 0, 1 + dim);
504         TC = isl_mat_variable_compression(B, T2);
505         if (!TC)
506                 goto error;
507         if (TC->n_col == 0) {
508                 isl_mat_free(TC);
509                 if (T2) {
510                         isl_mat_free(*T2);
511                         *T2 = NULL;
512                 }
513                 return isl_basic_set_set_to_empty(bset);
514         }
515
516         bset = isl_basic_set_preimage(bset, T ? isl_mat_copy(TC) : TC);
517         if (T)
518                 *T = TC;
519         return bset;
520 error:
521         isl_basic_set_free(bset);
522         return NULL;
523 }
524
525 struct isl_basic_set *isl_basic_set_remove_equalities(
526         struct isl_basic_set *bset, struct isl_mat **T, struct isl_mat **T2)
527 {
528         if (T)
529                 *T = NULL;
530         if (T2)
531                 *T2 = NULL;
532         if (!bset)
533                 return NULL;
534         isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
535         bset = isl_basic_set_gauss(bset, NULL);
536         if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
537                 return bset;
538         bset = compress_variables(bset, T, T2);
539         return bset;
540 error:
541         isl_basic_set_free(bset);
542         *T = NULL;
543         return NULL;
544 }
545
546 /* Check if dimension dim belongs to a residue class
547  *              i_dim \equiv r mod m
548  * with m != 1 and if so return m in *modulo and r in *residue.
549  * As a special case, when i_dim has a fixed value v, then
550  * *modulo is set to 0 and *residue to v.
551  *
552  * If i_dim does not belong to such a residue class, then *modulo
553  * is set to 1 and *residue is set to 0.
554  */
555 int isl_basic_set_dim_residue_class(struct isl_basic_set *bset,
556         int pos, isl_int *modulo, isl_int *residue)
557 {
558         struct isl_ctx *ctx;
559         struct isl_mat *H = NULL, *U = NULL, *C, *H1, *U1;
560         unsigned total;
561         unsigned nparam;
562
563         if (!bset || !modulo || !residue)
564                 return -1;
565
566         if (isl_basic_set_fast_dim_is_fixed(bset, pos, residue)) {
567                 isl_int_set_si(*modulo, 0);
568                 return 0;
569         }
570
571         ctx = bset->ctx;
572         total = isl_basic_set_total_dim(bset);
573         nparam = isl_basic_set_n_param(bset);
574         H = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq, 1, total);
575         H = isl_mat_left_hermite(H, 0, &U, NULL);
576         if (!H)
577                 return -1;
578
579         isl_seq_gcd(U->row[nparam + pos]+bset->n_eq,
580                         total-bset->n_eq, modulo);
581         if (isl_int_is_zero(*modulo))
582                 isl_int_set_si(*modulo, 1);
583         if (isl_int_is_one(*modulo)) {
584                 isl_int_set_si(*residue, 0);
585                 isl_mat_free(H);
586                 isl_mat_free(U);
587                 return 0;
588         }
589
590         C = isl_mat_alloc(bset->ctx, 1+bset->n_eq, 1);
591         if (!C)
592                 goto error;
593         isl_int_set_si(C->row[0][0], 1);
594         isl_mat_sub_neg(C->ctx, C->row+1, bset->eq, bset->n_eq, 0, 0, 1);
595         H1 = isl_mat_sub_alloc(H->ctx, H->row, 0, H->n_row, 0, H->n_row);
596         H1 = isl_mat_lin_to_aff(H1);
597         C = isl_mat_inverse_product(H1, C);
598         isl_mat_free(H);
599         U1 = isl_mat_sub_alloc(U->ctx, U->row, nparam+pos, 1, 0, bset->n_eq);
600         U1 = isl_mat_lin_to_aff(U1);
601         isl_mat_free(U);
602         C = isl_mat_product(U1, C);
603         if (!C)
604                 goto error;
605         if (!isl_int_is_divisible_by(C->row[1][0], C->row[0][0])) {
606                 bset = isl_basic_set_copy(bset);
607                 bset = isl_basic_set_set_to_empty(bset);
608                 isl_basic_set_free(bset);
609                 isl_int_set_si(*modulo, 1);
610                 isl_int_set_si(*residue, 0);
611                 return 0;
612         }
613         isl_int_divexact(*residue, C->row[1][0], C->row[0][0]);
614         isl_int_fdiv_r(*residue, *residue, *modulo);
615         isl_mat_free(C);
616         return 0;
617 error:
618         isl_mat_free(H);
619         isl_mat_free(U);
620         return -1;
621 }
622
623 /* Check if dimension dim belongs to a residue class
624  *              i_dim \equiv r mod m
625  * with m != 1 and if so return m in *modulo and r in *residue.
626  * As a special case, when i_dim has a fixed value v, then
627  * *modulo is set to 0 and *residue to v.
628  *
629  * If i_dim does not belong to such a residue class, then *modulo
630  * is set to 1 and *residue is set to 0.
631  */
632 int isl_set_dim_residue_class(struct isl_set *set,
633         int pos, isl_int *modulo, isl_int *residue)
634 {
635         isl_int m;
636         isl_int r;
637         int i;
638
639         if (!set || !modulo || !residue)
640                 return -1;
641
642         if (set->n == 0) {
643                 isl_int_set_si(*modulo, 0);
644                 isl_int_set_si(*residue, 0);
645                 return 0;
646         }
647
648         if (isl_basic_set_dim_residue_class(set->p[0], pos, modulo, residue)<0)
649                 return -1;
650
651         if (set->n == 1)
652                 return 0;
653
654         if (isl_int_is_one(*modulo))
655                 return 0;
656
657         isl_int_init(m);
658         isl_int_init(r);
659
660         for (i = 1; i < set->n; ++i) {
661                 if (isl_basic_set_dim_residue_class(set->p[0], pos, &m, &r) < 0)
662                         goto error;
663                 isl_int_gcd(*modulo, *modulo, m);
664                 if (!isl_int_is_zero(*modulo))
665                         isl_int_fdiv_r(*residue, *residue, *modulo);
666                 if (isl_int_is_one(*modulo))
667                         break;
668                 if (!isl_int_is_zero(*modulo))
669                         isl_int_fdiv_r(r, r, *modulo);
670                 if (isl_int_ne(*residue, r)) {
671                         isl_int_set_si(*modulo, 1);
672                         isl_int_set_si(*residue, 0);
673                         break;
674                 }
675         }
676
677         isl_int_clear(m);
678         isl_int_clear(r);
679
680         return 0;
681 error:
682         isl_int_clear(m);
683         isl_int_clear(r);
684         return -1;
685 }