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