- F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
-}
-
-static void eliminate_var_using_equality(struct isl_basic_map *bmap,
- unsigned pos, isl_int *eq, int *progress)
-{
- unsigned total;
- int k;
-
- total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
- for (k = 0; k < bmap->n_eq; ++k) {
- if (bmap->eq[k] == eq)
- continue;
- if (isl_int_is_zero(bmap->eq[k][1+pos]))
- continue;
- if (progress)
- *progress = 1;
- isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
- }
-
- for (k = 0; k < bmap->n_ineq; ++k) {
- if (isl_int_is_zero(bmap->ineq[k][1+pos]))
- continue;
- if (progress)
- *progress = 1;
- isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
- F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
- }
-
- for (k = 0; k < bmap->n_div; ++k) {
- if (isl_int_is_zero(bmap->div[k][0]))
- continue;
- if (isl_int_is_zero(bmap->div[k][1+1+pos]))
- continue;
- if (progress)
- *progress = 1;
- isl_seq_elim(bmap->div[k]+1, eq,
- 1+pos, 1+total, &bmap->div[k][0]);
- F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
- }
-}
-
-
-struct isl_basic_map *isl_basic_map_gauss(
- struct isl_basic_map *bmap, int *progress)
-{
- int k;
- int done;
- int last_var;
- unsigned total_var;
- unsigned total;
-
- if (!bmap)
- return NULL;
-
- total_var = bmap->nparam + bmap->n_in + bmap->n_out;
- total = total_var + bmap->n_div;
-
- last_var = total - 1;
- for (done = 0; done < bmap->n_eq; ++done) {
- for (; last_var >= 0; --last_var) {
- for (k = done; k < bmap->n_eq; ++k)
- if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
- break;
- if (k < bmap->n_eq)
- break;
- }
- if (last_var < 0)
- break;
- if (k != done)
- swap_equality(bmap, k, done);
- if (isl_int_is_neg(bmap->eq[done][1+last_var]))
- isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
-
- eliminate_var_using_equality(bmap, last_var, bmap->eq[done],
- progress);
-
- if (last_var >= total_var &&
- isl_int_is_zero(bmap->div[last_var - total_var][0])) {
- unsigned div = last_var - total_var;
- isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
- isl_int_set_si(bmap->div[div][1+1+last_var], 0);
- isl_int_set(bmap->div[div][0],
- bmap->eq[done][1+last_var]);
- F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
- }
- }
- if (done == bmap->n_eq)
- return bmap;
- for (k = done; k < bmap->n_eq; ++k) {
- if (isl_int_is_zero(bmap->eq[k][0]))
- continue;
- return isl_basic_map_set_to_empty(bmap);
- }
- isl_basic_map_free_equality(bmap, bmap->n_eq-done);
- return bmap;
-}
-
-struct isl_basic_set *isl_basic_set_gauss(
- struct isl_basic_set *bset, int *progress)
-{
- return (struct isl_basic_set*)isl_basic_map_gauss(
- (struct isl_basic_map *)bset, progress);
-}
-
-static unsigned int round_up(unsigned int v)
-{
- int old_v = v;
-
- while (v) {
- old_v = v;
- v ^= v & -v;
- }
- return old_v << 1;
-}
-
-static int hash_index(isl_int ***index, unsigned int size, int bits,
- struct isl_basic_map *bmap, int k)
-{
- int h;
- unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
- u_int32_t hash = isl_seq_hash(bmap->ineq[k]+1, total, bits);
- for (h = hash; index[h]; h = (h+1) % size)
- if (&bmap->ineq[k] != index[h] &&
- isl_seq_eq(bmap->ineq[k]+1, index[h][0]+1, total))
- break;
- return h;
-}
-
-static int set_hash_index(isl_int ***index, unsigned int size, int bits,
- struct isl_basic_set *bset, int k)
-{
- return hash_index(index, size, bits, (struct isl_basic_map *)bset, k);
-}
-
-static struct isl_basic_map *remove_duplicate_constraints(
- struct isl_basic_map *bmap, int *progress)
-{
- unsigned int size;
- isl_int ***index;
- int k, l, h;
- int bits;
- unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
- isl_int sum;
-
- if (bmap->n_ineq <= 1)
- return bmap;
-
- size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
- bits = ffs(size) - 1;
- index = isl_alloc_array(ctx, isl_int **, size);
- memset(index, 0, size * sizeof(isl_int **));
- if (!index)
- return bmap;
-
- index[isl_seq_hash(bmap->ineq[0]+1, total, bits)] = &bmap->ineq[0];
- for (k = 1; k < bmap->n_ineq; ++k) {
- h = hash_index(index, size, bits, bmap, k);
- if (!index[h]) {
- index[h] = &bmap->ineq[k];
- continue;
- }
- if (progress)
- *progress = 1;
- l = index[h] - &bmap->ineq[0];
- if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
- swap_inequality(bmap, k, l);
- isl_basic_map_drop_inequality(bmap, k);
- --k;
- }
- isl_int_init(sum);
- for (k = 0; k < bmap->n_ineq-1; ++k) {
- isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
- h = hash_index(index, size, bits, bmap, k);
- isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
- if (!index[h])
- continue;
- l = index[h] - &bmap->ineq[0];
- isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
- if (isl_int_is_pos(sum))
- continue;
- if (isl_int_is_zero(sum)) {
- /* We need to break out of the loop after these
- * changes since the contents of the hash
- * will no longer be valid.
- * Plus, we probably we want to regauss first.
- */
- isl_basic_map_drop_inequality(bmap, l);
- isl_basic_map_inequality_to_equality(bmap, k);
- } else
- bmap = isl_basic_map_set_to_empty(bmap);
- break;
- }
- isl_int_clear(sum);
-
- free(index);
- return bmap;
-}
-
-static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
-{
- int d, i;
- unsigned total;
-
- total = bmap->nparam + bmap->n_in + bmap->n_out;
- for (d = 0; d < total; ++d)
- elim[d] = -1;
- for (d = total - 1, i = 0; d >= 0 && i < bmap->n_eq; ++i) {
- for (; d >= 0; --d) {
- if (isl_int_is_zero(bmap->eq[i][1+d]))
- continue;
- elim[d] = i;
- break;
- }
- }
-}
-
-static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
-{
- return compute_elimination_index((struct isl_basic_map *)bset, elim);
-}
-
-static int reduced_using_equalities(isl_int *dst, isl_int *src,
- struct isl_basic_map *bmap, int *elim)
-{
- int d, i;
- int copied = 0;
- unsigned total;
-
- total = bmap->nparam + bmap->n_in + bmap->n_out;
- for (d = total - 1; d >= 0; --d) {
- if (isl_int_is_zero(src[1+d]))
- continue;
- if (elim[d] == -1)
- continue;
- if (!copied) {
- isl_seq_cpy(dst, src, 1 + total);
- copied = 1;
- }
- isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
- }
- return copied;
-}
-
-static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
- struct isl_basic_set *bset, int *elim)
-{
- return reduced_using_equalities(dst, src,
- (struct isl_basic_map *)bset, elim);
-}
-
-/* Quick check to see if two basic maps are disjoint.
- * In particular, we reduce the equalities and inequalities of
- * one basic map in the context of the equalities of the other
- * basic map and check if we get a contradiction.
- */
-int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
- struct isl_basic_map *bmap2)
-{
- struct isl_vec *v = NULL;
- int *elim = NULL;
- unsigned total;
- int d, i;
-
- if (!bmap1 || !bmap2)
- return -1;
- isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, return -1);
- isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, return -1);
- isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, return -1);
- if (bmap1->n_div || bmap2->n_div)
- return 0;
- if (!bmap1->n_eq && !bmap2->n_eq)
- return 0;
-
- total = bmap1->nparam + bmap1->n_in + bmap1->n_out;
- if (total == 0)
- return 0;
- v = isl_vec_alloc(bmap1->ctx, 1 + total);
- if (!v)
- goto error;
- elim = isl_alloc_array(bmap1->ctx, int, total);
- if (!elim)
- goto error;
- compute_elimination_index(bmap1, elim);
- for (i = 0; i < bmap2->n_eq; ++i) {
- int reduced;
- reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
- bmap1, elim);
- if (reduced && !isl_int_is_zero(v->block.data[0]) &&
- isl_seq_first_non_zero(v->block.data + 1, total) == -1)
- goto disjoint;
- }
- for (i = 0; i < bmap2->n_ineq; ++i) {
- int reduced;
- reduced = reduced_using_equalities(v->block.data,
- bmap2->ineq[i], bmap1, elim);
- if (reduced && isl_int_is_neg(v->block.data[0]) &&
- isl_seq_first_non_zero(v->block.data + 1, total) == -1)
- goto disjoint;
- }
- compute_elimination_index(bmap2, elim);
- for (i = 0; i < bmap1->n_ineq; ++i) {
- int reduced;
- reduced = reduced_using_equalities(v->block.data,
- bmap1->ineq[i], bmap2, elim);
- if (reduced && isl_int_is_neg(v->block.data[0]) &&
- isl_seq_first_non_zero(v->block.data + 1, total) == -1)
- goto disjoint;
- }
- isl_vec_free(bmap1->ctx, v);
- free(elim);
- return 0;
-disjoint:
- isl_vec_free(bmap1->ctx, v);
- free(elim);
- return 1;
-error:
- isl_vec_free(bmap1->ctx, v);
- free(elim);
- return -1;
-}
-
-int isl_basic_set_fast_is_disjoint(struct isl_basic_set *bset1,
- struct isl_basic_set *bset2)
-{
- return isl_basic_map_fast_is_disjoint((struct isl_basic_map *)bset1,
- (struct isl_basic_map *)bset2);
-}
-
-int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2)
-{
- int i, j;
-
- if (!map1 || !map2)
- return -1;
-
- if (isl_map_fast_is_equal(map1, map2))
- return 0;
-
- for (i = 0; i < map1->n; ++i) {
- for (j = 0; j < map2->n; ++j) {
- int d = isl_basic_map_fast_is_disjoint(map1->p[i],
- map2->p[j]);
- if (d != 1)
- return d;
- }
- }
- return 1;
-}
-
-int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
-{
- return isl_map_fast_is_disjoint((struct isl_map *)set1,
- (struct isl_map *)set2);
-}
-
-static struct isl_basic_map *remove_duplicate_divs(
- struct isl_basic_map *bmap, int *progress)
-{
- unsigned int size;
- int *index;
- int k, l, h;
- int bits;
- struct isl_blk eq;
- unsigned total_var = bmap->nparam + bmap->n_in + bmap->n_out;
- unsigned total = total_var + bmap->n_div;
- struct isl_ctx *ctx;
-
- if (bmap->n_div <= 1)
- return bmap;
-
- ctx = bmap->ctx;
- for (k = bmap->n_div - 1; k >= 0; --k)
- if (!isl_int_is_zero(bmap->div[k][0]))
- break;
- if (k <= 0)
- return bmap;
-
- size = round_up(4 * bmap->n_div / 3 - 1);
- bits = ffs(size) - 1;
- index = isl_alloc_array(ctx, int, size);
- memset(index, 0, size * sizeof(int));
- if (!index)
- return bmap;
- eq = isl_blk_alloc(ctx, 1+total);
- if (isl_blk_is_error(eq))
- goto out;
-
- isl_seq_clr(eq.data, 1+total);
- index[isl_seq_hash(bmap->div[k], 2+total, bits)] = k + 1;
- for (--k; k >= 0; --k) {
- u_int32_t hash;
-
- if (isl_int_is_zero(bmap->div[k][0]))
- continue;
-
- hash = isl_seq_hash(bmap->div[k], 2+total, bits);
- for (h = hash; index[h]; h = (h+1) % size)
- if (isl_seq_eq(bmap->div[k],
- bmap->div[index[h]-1], 2+total))
- break;
- if (index[h]) {
- *progress = 1;
- l = index[h] - 1;
- isl_int_set_si(eq.data[1+total_var+k], -1);
- isl_int_set_si(eq.data[1+total_var+l], 1);
- eliminate_div(bmap, eq.data, l);
- isl_int_set_si(eq.data[1+total_var+k], 0);
- isl_int_set_si(eq.data[1+total_var+l], 0);
- }
- index[h] = k+1;
- }
-
- isl_blk_free(ctx, eq);
-out:
- free(index);
- return bmap;
-}
-
-/* Elimininate divs based on equalities
- */
-static struct isl_basic_map *eliminate_divs_eq(
- struct isl_basic_map *bmap, int *progress)
-{
- int d;
- int i;
- int modified = 0;
- unsigned off;
-
- if (!bmap)
- return NULL;
-
- off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
-
- for (d = bmap->n_div - 1; d >= 0 ; --d) {
- for (i = 0; i < bmap->n_eq; ++i) {
- if (!isl_int_is_one(bmap->eq[i][off + d]) &&
- !isl_int_is_negone(bmap->eq[i][off + d]))
- continue;
- modified = 1;
- *progress = 1;
- eliminate_div(bmap, bmap->eq[i], d);
- isl_basic_map_drop_equality(bmap, i);
- break;
- }
- }
- if (modified)
- return eliminate_divs_eq(bmap, progress);
- return bmap;
-}
-
-static struct isl_basic_map *normalize_constraints(struct isl_basic_map *bmap)
-{
- int i;
- isl_int gcd;
- unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
-
- isl_int_init(gcd);
- for (i = bmap->n_eq - 1; i >= 0; --i) {
- isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
- if (isl_int_is_zero(gcd)) {
- if (!isl_int_is_zero(bmap->eq[i][0])) {
- bmap = isl_basic_map_set_to_empty(bmap);
- break;
- }
- isl_basic_map_drop_equality(bmap, i);
- continue;
- }
- if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
- isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
- if (isl_int_is_one(gcd))
- continue;
- if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
- bmap = isl_basic_map_set_to_empty(bmap);
- break;
- }
- isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
- }
-
- for (i = bmap->n_ineq - 1; i >= 0; --i) {
- isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
- if (isl_int_is_zero(gcd)) {
- if (isl_int_is_neg(bmap->ineq[i][0])) {
- bmap = isl_basic_map_set_to_empty(bmap);
- break;
- }
- isl_basic_map_drop_inequality(bmap, i);
- continue;
- }
- if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
- isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
- if (isl_int_is_one(gcd))
- continue;
- isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
- isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
- }
- isl_int_clear(gcd);
-
- return bmap;
-}
-
-
-/* Eliminate the specified variables from the constraints using
- * Fourier-Motzkin. The variables themselves are not removed.
- */
-struct isl_basic_map *isl_basic_map_eliminate_vars(
- struct isl_basic_map *bmap, int pos, unsigned n)
-{
- int d;
- int i, j, k;
- unsigned total;
- unsigned extra;
-
- if (!bmap)
- return NULL;
- total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
- extra = bmap->extra - bmap->n_div;
-
- bmap = isl_basic_map_cow(bmap);
- for (d = pos + n - 1; d >= pos; --d) {
- int n_lower, n_upper;
- if (!bmap)
- return NULL;
- for (i = 0; i < bmap->n_eq; ++i) {
- if (isl_int_is_zero(bmap->eq[i][1+d]))
- continue;
- eliminate_var_using_equality(bmap, d, bmap->eq[i], NULL);
- isl_basic_map_drop_equality(bmap, i);
- break;
- }
- if (i < bmap->n_eq)
- continue;
- n_lower = 0;
- n_upper = 0;
- for (i = 0; i < bmap->n_ineq; ++i) {
- if (isl_int_is_pos(bmap->ineq[i][1+d]))
- n_lower++;
- else if (isl_int_is_neg(bmap->ineq[i][1+d]))
- n_upper++;
- }
- bmap = isl_basic_map_extend(bmap,
- bmap->nparam, bmap->n_in, bmap->n_out, 0,
- 0, n_lower * n_upper);
- for (i = bmap->n_ineq - 1; i >= 0; --i) {
- int last;
- if (isl_int_is_zero(bmap->ineq[i][1+d]))
- continue;
- last = -1;
- for (j = 0; j < i; ++j) {
- if (isl_int_is_zero(bmap->ineq[j][1+d]))
- continue;
- last = j;
- if (isl_int_sgn(bmap->ineq[i][1+d]) ==
- isl_int_sgn(bmap->ineq[j][1+d]))
- continue;
- k = isl_basic_map_alloc_inequality(bmap);
- if (k < 0)
- goto error;
- isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
- 1+total);
- isl_seq_clr(bmap->ineq[k]+1+total, extra);
- isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
- 1+d, 1+total, NULL);
- }
- isl_basic_map_drop_inequality(bmap, i);
- i = last + 1;
- }
- if (n_lower > 0 && n_upper > 0) {
- bmap = normalize_constraints(bmap);
- bmap = remove_duplicate_constraints(bmap, NULL);
- bmap = isl_basic_map_gauss(bmap, NULL);
- bmap = isl_basic_map_convex_hull(bmap);
- if (!bmap)
- goto error;
- if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
- break;
- }
- }
- F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
- return bmap;
-error:
- isl_basic_map_free(bmap);
- return NULL;
-}
-
-struct isl_basic_set *isl_basic_set_eliminate_vars(
- struct isl_basic_set *bset, unsigned pos, unsigned n)
-{
- return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
- (struct isl_basic_map *)bset, pos, n);