-static struct isl_map *add_cut_constraint(struct isl_map *dst,
- struct isl_basic_map *src, isl_int *c,
- unsigned len, int oppose)
-{
- struct isl_basic_map *copy = NULL;
- int is_empty;
- int k;
- unsigned total;
-
- copy = isl_basic_map_copy(src);
- copy = isl_basic_map_cow(copy);
- if (!copy)
- goto error;
- copy = isl_basic_map_extend_constraints(copy, 0, 1);
- k = isl_basic_map_alloc_inequality(copy);
- if (k < 0)
- goto error;
- if (oppose)
- isl_seq_neg(copy->ineq[k], c, len);
- else
- isl_seq_cpy(copy->ineq[k], c, len);
- total = 1 + isl_basic_map_total_dim(copy);
- isl_seq_clr(copy->ineq[k]+len, total - len);
- isl_inequality_negate(copy, k);
- copy = isl_basic_map_simplify(copy);
- copy = isl_basic_map_finalize(copy);
- is_empty = isl_basic_map_is_empty(copy);
- if (is_empty < 0)
- goto error;
- if (!is_empty)
- dst = isl_map_add(dst, copy);
- else
- isl_basic_map_free(copy);
- return dst;
-error:
- isl_basic_map_free(copy);
- isl_map_free(dst);
- return NULL;
-}
-
-static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
-{
- int i, j, k;
- unsigned flags = 0;
- struct isl_map *rest = NULL;
- unsigned max;
- unsigned total = isl_basic_map_total_dim(bmap);
-
- assert(bmap);
-
- if (!map)
- goto error;
-
- if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
- ISL_FL_SET(flags, ISL_MAP_DISJOINT);
-
- max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
- rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
- if (!rest)
- goto error;
-
- for (i = 0; i < map->n; ++i) {
- map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
- if (!map->p[i])
- goto error;
- }
-
- for (j = 0; j < map->n; ++j)
- map->p[j] = isl_basic_map_cow(map->p[j]);
-
- for (i = 0; i < bmap->n_eq; ++i) {
- for (j = 0; j < map->n; ++j) {
- rest = add_cut_constraint(rest,
- map->p[j], bmap->eq[i], 1+total, 0);
- if (!rest)
- goto error;
-
- rest = add_cut_constraint(rest,
- map->p[j], bmap->eq[i], 1+total, 1);
- if (!rest)
- goto error;
-
- map->p[j] = isl_basic_map_extend_constraints(map->p[j],
- 1, 0);
- if (!map->p[j])
- goto error;
- k = isl_basic_map_alloc_equality(map->p[j]);
- if (k < 0)
- goto error;
- isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
- isl_seq_clr(map->p[j]->eq[k]+1+total,
- map->p[j]->n_div - bmap->n_div);
- }
- }
-
- for (i = 0; i < bmap->n_ineq; ++i) {
- for (j = 0; j < map->n; ++j) {
- rest = add_cut_constraint(rest,
- map->p[j], bmap->ineq[i], 1+total, 0);
- if (!rest)
- goto error;
-
- map->p[j] = isl_basic_map_extend_constraints(map->p[j],
- 0, 1);
- if (!map->p[j])
- goto error;
- k = isl_basic_map_alloc_inequality(map->p[j]);
- if (k < 0)
- goto error;
- isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
- isl_seq_clr(map->p[j]->ineq[k]+1+total,
- map->p[j]->n_div - bmap->n_div);
- }
- }
-
- isl_map_free(map);
- return rest;
-error:
- isl_map_free(map);
- isl_map_free(rest);
- return NULL;
-}
-
-struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
-{
- int i;
- if (!map1 || !map2)
- goto error;
-
- isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
-
- if (isl_map_is_empty(map2)) {
- isl_map_free(map2);
- return map1;
- }
-
- map1 = isl_map_compute_divs(map1);
- map2 = isl_map_compute_divs(map2);
- if (!map1 || !map2)
- goto error;
-
- for (i = 0; map1 && i < map2->n; ++i)
- map1 = subtract(map1, map2->p[i]);
-
- isl_map_free(map2);
- return map1;
-error:
- isl_map_free(map1);
- isl_map_free(map2);
- return NULL;
-}
-
-struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
-{
- return (struct isl_set *)
- isl_map_subtract(
- (struct isl_map *)set1, (struct isl_map *)set2);
-}
-