doc: update supported versions of clang
[isl.git] / isl_affine_hull.c
index 84fcee9..7d1d2b2 100644 (file)
 #include <isl_mat_private.h>
 #include <isl_vec_private.h>
 
-struct isl_basic_map *isl_basic_map_implicit_equalities(
-                                               struct isl_basic_map *bmap)
+#include <bset_to_bmap.c>
+#include <bset_from_bmap.c>
+#include <set_to_map.c>
+#include <set_from_map.c>
+
+__isl_give isl_basic_map *isl_basic_map_implicit_equalities(
+       __isl_take isl_basic_map *bmap)
 {
        struct isl_tab *tab;
 
@@ -54,30 +59,11 @@ error:
        return NULL;
 }
 
-struct isl_basic_set *isl_basic_set_implicit_equalities(
-                                               struct isl_basic_set *bset)
-{
-       return (struct isl_basic_set *)
-               isl_basic_map_implicit_equalities((struct isl_basic_map*)bset);
-}
-
-struct isl_map *isl_map_implicit_equalities(struct isl_map *map)
+__isl_give isl_basic_set *isl_basic_set_implicit_equalities(
+       __isl_take isl_basic_set *bset)
 {
-       int i;
-
-       if (!map)
-               return map;
-
-       for (i = 0; i < map->n; ++i) {
-               map->p[i] = isl_basic_map_implicit_equalities(map->p[i]);
-               if (!map->p[i])
-                       goto error;
-       }
-
-       return map;
-error:
-       isl_map_free(map);
-       return NULL;
+       return bset_from_bmap(
+               isl_basic_map_implicit_equalities(bset_to_bmap(bset)));
 }
 
 /* Make eq[row][col] of both bmaps equal so we can add the row
@@ -107,7 +93,7 @@ static void set_common_multiple(
 
 /* Delete a given equality, moving all the following equalities one up.
  */
-static void delete_row(struct isl_basic_set *bset, unsigned row)
+static void delete_row(__isl_keep isl_basic_set *bset, unsigned row)
 {
        isl_int *t;
        int r;
@@ -128,18 +114,21 @@ static void delete_row(struct isl_basic_set *bset, unsigned row)
  * so that
  *             A[i][col] = B[i][col] = a * old(B[i][col])
  */
-static void construct_column(
-       struct isl_basic_set *bset1, struct isl_basic_set *bset2,
+static isl_stat construct_column(
+       __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
        unsigned row, unsigned col)
 {
        int r;
        isl_int a;
        isl_int b;
-       unsigned total;
+       isl_size total;
+
+       total = isl_basic_set_dim(bset1, isl_dim_set);
+       if (total < 0)
+               return isl_stat_error;
 
        isl_int_init(a);
        isl_int_init(b);
-       total = 1 + isl_basic_set_n_dim(bset1);
        for (r = 0; r < row; ++r) {
                if (isl_int_is_zero(bset2->eq[r][col]))
                        continue;
@@ -147,12 +136,14 @@ static void construct_column(
                isl_int_divexact(a, bset1->eq[row][col], b);
                isl_int_divexact(b, bset2->eq[r][col], b);
                isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
-                                             b, bset1->eq[row], total);
-               isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
+                                             b, bset1->eq[row], 1 + total);
+               isl_seq_scale(bset2->eq[r], bset2->eq[r], a, 1 + total);
        }
        isl_int_clear(a);
        isl_int_clear(b);
        delete_row(bset1, row);
+
+       return isl_stat_ok;
 }
 
 /* Make first row entries in column col of bset1 identical to
@@ -164,21 +155,23 @@ static void construct_column(
  * so that
  *     A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
  */
-static int transform_column(
-       struct isl_basic_set *bset1, struct isl_basic_set *bset2,
+static isl_bool transform_column(
+       __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
        unsigned row, unsigned col)
 {
        int i, t;
        isl_int a, b, g;
-       unsigned total;
+       isl_size total;
 
        for (t = row-1; t >= 0; --t)
                if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
                        break;
        if (t < 0)
-               return 0;
+               return isl_bool_false;
 
-       total = 1 + isl_basic_set_n_dim(bset1);
+       total = isl_basic_set_dim(bset1, isl_dim_set);
+       if (total < 0)
+               return isl_bool_error;
        isl_int_init(a);
        isl_int_init(b);
        isl_int_init(g);
@@ -189,16 +182,16 @@ static int transform_column(
                isl_int_divexact(a, a, g);
                isl_int_divexact(g, b, g);
                isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
-                               total);
+                               1 + total);
                isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
-                               total);
+                               1 + total);
        }
        isl_int_clear(a);
        isl_int_clear(b);
        isl_int_clear(g);
        delete_row(bset1, t);
        delete_row(bset2, t);
-       return 1;
+       return isl_bool_true;
 }
 
 /* The implementation is based on Section 5.2 of Michael Karr,
@@ -206,17 +199,19 @@ static int transform_column(
  * except that the echelon form we use starts from the last column
  * and that we are dealing with integer coefficients.
  */
-static struct isl_basic_set *affine_hull(
-       struct isl_basic_set *bset1, struct isl_basic_set *bset2)
+static __isl_give isl_basic_set *affine_hull(
+       __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
 {
+       isl_size dim;
        unsigned total;
        int col;
        int row;
 
-       if (!bset1 || !bset2)
+       dim = isl_basic_set_dim(bset1, isl_dim_set);
+       if (dim < 0 || !bset2)
                goto error;
 
-       total = 1 + isl_basic_set_n_dim(bset1);
+       total = 1 + dim;
 
        row = 0;
        for (col = total-1; col >= 0; --col) {
@@ -228,11 +223,18 @@ static struct isl_basic_set *affine_hull(
                        set_common_multiple(bset1, bset2, row, col);
                        ++row;
                } else if (!is_zero1 && is_zero2) {
-                       construct_column(bset1, bset2, row, col);
+                       if (construct_column(bset1, bset2, row, col) < 0)
+                               goto error;
                } else if (is_zero1 && !is_zero2) {
-                       construct_column(bset2, bset1, row, col);
+                       if (construct_column(bset2, bset1, row, col) < 0)
+                               goto error;
                } else {
-                       if (transform_column(bset1, bset2, row, col))
+                       isl_bool transform;
+
+                       transform = transform_column(bset1, bset2, row, col);
+                       if (transform < 0)
+                               goto error;
+                       if (transform)
                                --row;
                }
        }
@@ -261,7 +263,8 @@ error:
  * The caller of this function ensures that the tableau is bounded or
  * that tab->basis and tab->n_unbounded have been set appropriately.
  */
-static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
+static __isl_give isl_vec *outside_point(struct isl_tab *tab, isl_int *eq,
+       int up)
 {
        struct isl_ctx *ctx;
        struct isl_vec *sample = NULL;
@@ -311,14 +314,21 @@ error:
        return NULL;
 }
 
-struct isl_basic_set *isl_basic_set_recession_cone(struct isl_basic_set *bset)
+__isl_give isl_basic_set *isl_basic_set_recession_cone(
+       __isl_take isl_basic_set *bset)
 {
        int i;
+       isl_bool empty;
+
+       empty = isl_basic_set_plain_is_empty(bset);
+       if (empty < 0)
+               return isl_basic_set_free(bset);
+       if (empty)
+               return bset;
 
        bset = isl_basic_set_cow(bset);
-       if (!bset)
-               return NULL;
-       isl_assert(bset->ctx, bset->n_div == 0, goto error);
+       if (isl_basic_set_check_no_locals(bset) < 0)
+               return isl_basic_set_free(bset);
 
        for (i = 0; i < bset->n_eq; ++i)
                isl_int_set_si(bset->eq[i][0], 0);
@@ -328,35 +338,6 @@ struct isl_basic_set *isl_basic_set_recession_cone(struct isl_basic_set *bset)
 
        ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
        return isl_basic_set_implicit_equalities(bset);
-error:
-       isl_basic_set_free(bset);
-       return NULL;
-}
-
-__isl_give isl_set *isl_set_recession_cone(__isl_take isl_set *set)
-{
-       int i;
-
-       if (!set)
-               return NULL;
-       if (set->n == 0)
-               return set;
-
-       set = isl_set_remove_divs(set);
-       set = isl_set_cow(set);
-       if (!set)
-               return NULL;
-
-       for (i = 0; i < set->n; ++i) {
-               set->p[i] = isl_basic_set_recession_cone(set->p[i]);
-               if (!set->p[i])
-                       goto error;
-       }
-
-       return set;
-error:
-       isl_set_free(set);
-       return NULL;
 }
 
 /* Move "sample" to a point that is one up (or down) from the original
@@ -382,12 +363,11 @@ static __isl_give isl_basic_set *add_adjacent_points(
        __isl_keep isl_basic_set *bset)
 {
        int i, up;
-       int dim;
-
-       if (!sample)
-               goto error;
+       isl_size dim;
 
        dim = isl_basic_set_dim(hull, isl_dim_set);
+       if (!sample || dim < 0)
+               goto error;
 
        for (i = 0; i < dim; ++i) {
                for (up = 0; up <= 1; ++up) {
@@ -492,175 +472,7 @@ error:
        return NULL;
 }
 
-/* Drop all constraints in bmap that involve any of the dimensions
- * first to first+n-1.
- */
-static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
-       __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
-{
-       int i;
-
-       if (n == 0)
-               return bmap;
-
-       bmap = isl_basic_map_cow(bmap);
-
-       if (!bmap)
-               return NULL;
-
-       for (i = bmap->n_eq - 1; i >= 0; --i) {
-               if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
-                       continue;
-               isl_basic_map_drop_equality(bmap, i);
-       }
-
-       for (i = bmap->n_ineq - 1; i >= 0; --i) {
-               if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
-                       continue;
-               isl_basic_map_drop_inequality(bmap, i);
-       }
-
-       return bmap;
-}
-
-/* Drop all constraints in bset that involve any of the dimensions
- * first to first+n-1.
- */
-__isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
-       __isl_take isl_basic_set *bset, unsigned first, unsigned n)
-{
-       return isl_basic_map_drop_constraints_involving(bset, first, n);
-}
-
-/* Drop all constraints in bmap that do not involve any of the dimensions
- * first to first + n - 1 of the given type.
- */
-__isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
-       __isl_take isl_basic_map *bmap,
-       enum isl_dim_type type, unsigned first, unsigned n)
-{
-       int i;
-       unsigned dim;
-
-       if (n == 0)
-               return isl_basic_map_set_to_empty(bmap);
-       bmap = isl_basic_map_cow(bmap);
-       if (!bmap)
-               return NULL;
-
-       dim = isl_basic_map_dim(bmap, type);
-       if (first + n > dim || first + n < first)
-               isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
-                       "index out of bounds", return isl_basic_map_free(bmap));
-
-       first += isl_basic_map_offset(bmap, type) - 1;
-
-       for (i = bmap->n_eq - 1; i >= 0; --i) {
-               if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
-                       continue;
-               isl_basic_map_drop_equality(bmap, i);
-       }
-
-       for (i = bmap->n_ineq - 1; i >= 0; --i) {
-               if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
-                       continue;
-               isl_basic_map_drop_inequality(bmap, i);
-       }
-
-       return bmap;
-}
-
-/* Drop all constraints in bset that do not involve any of the dimensions
- * first to first + n - 1 of the given type.
- */
-__isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
-       __isl_take isl_basic_set *bset,
-       enum isl_dim_type type, unsigned first, unsigned n)
-{
-       return isl_basic_map_drop_constraints_not_involving_dims(bset,
-                                                           type, first, n);
-}
-
-/* Drop all constraints in bmap that involve any of the dimensions
- * first to first + n - 1 of the given type.
- */
-__isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
-       __isl_take isl_basic_map *bmap,
-       enum isl_dim_type type, unsigned first, unsigned n)
-{
-       unsigned dim;
-
-       if (!bmap)
-               return NULL;
-       if (n == 0)
-               return bmap;
-
-       dim = isl_basic_map_dim(bmap, type);
-       if (first + n > dim || first + n < first)
-               isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
-                       "index out of bounds", return isl_basic_map_free(bmap));
-
-       bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
-       first += isl_basic_map_offset(bmap, type) - 1;
-       return isl_basic_map_drop_constraints_involving(bmap, first, n);
-}
-
-/* Drop all constraints in bset that involve any of the dimensions
- * first to first + n - 1 of the given type.
- */
-__isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
-       __isl_take isl_basic_set *bset,
-       enum isl_dim_type type, unsigned first, unsigned n)
-{
-       return isl_basic_map_drop_constraints_involving_dims(bset,
-                                                           type, first, n);
-}
-
-/* Drop all constraints in map that involve any of the dimensions
- * first to first + n - 1 of the given type.
- */
-__isl_give isl_map *isl_map_drop_constraints_involving_dims(
-       __isl_take isl_map *map,
-       enum isl_dim_type type, unsigned first, unsigned n)
-{
-       int i;
-       unsigned dim;
-
-       if (!map)
-               return NULL;
-       if (n == 0)
-               return map;
-
-       dim = isl_map_dim(map, type);
-       if (first + n > dim || first + n < first)
-               isl_die(isl_map_get_ctx(map), isl_error_invalid,
-                       "index out of bounds", return isl_map_free(map));
-
-       map = isl_map_cow(map);
-       if (!map)
-               return NULL;
-
-       for (i = 0; i < map->n; ++i) {
-               map->p[i] = isl_basic_map_drop_constraints_involving_dims(
-                                                   map->p[i], type, first, n);
-               if (!map->p[i])
-                       return isl_map_free(map);
-       }
-
-       return map;
-}
-
-/* Drop all constraints in set that involve any of the dimensions
- * first to first + n - 1 of the given type.
- */
-__isl_give isl_set *isl_set_drop_constraints_involving_dims(
-       __isl_take isl_set *set,
-       enum isl_dim_type type, unsigned first, unsigned n)
-{
-       return isl_map_drop_constraints_involving_dims(set, type, first, n);
-}
-
-/* Construct an initial underapproximatino of the hull of "bset"
+/* Construct an initial underapproximation of the hull of "bset"
  * from "sample" and any of its adjacent points that also belong to "bset".
  */
 static __isl_give isl_basic_set *initialize_hull(__isl_keep isl_basic_set *bset,
@@ -683,17 +495,20 @@ static __isl_give isl_basic_set *initialize_hull(__isl_keep isl_basic_set *bset,
  * we check if there is any point on a hyperplane parallel to the
  * corresponding hyperplane shifted by at least one (in either direction).
  */
-static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset)
+static __isl_give isl_basic_set *uset_affine_hull_bounded(
+       __isl_take isl_basic_set *bset)
 {
        struct isl_vec *sample = NULL;
        struct isl_basic_set *hull;
        struct isl_tab *tab = NULL;
-       unsigned dim;
+       isl_size dim;
 
        if (isl_basic_set_plain_is_empty(bset))
                return bset;
 
-       dim = isl_basic_set_n_dim(bset);
+       dim = isl_basic_set_dim(bset, isl_dim_set);
+       if (dim < 0)
+               return isl_basic_set_free(bset);
 
        if (bset->sample && bset->sample->size == 1 + dim) {
                int contains = isl_basic_set_contains(bset, bset->sample);
@@ -764,7 +579,7 @@ static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab,
        int k;
        struct isl_basic_set *bset = NULL;
        struct isl_ctx *ctx;
-       unsigned dim;
+       isl_size dim;
 
        if (!vec || !tab)
                return NULL;
@@ -772,9 +587,10 @@ static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab,
        isl_assert(ctx, vec->size != 0, goto error);
 
        bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
-       if (!bset)
+       dim = isl_basic_set_dim(bset, isl_dim_set);
+       if (dim < 0)
                goto error;
-       dim = isl_basic_set_n_dim(bset) - tab->n_unbounded;
+       dim -= tab->n_unbounded;
        for (i = 0; i < dim; ++i) {
                k = isl_basic_set_alloc_equality(bset);
                if (k < 0)
@@ -911,18 +727,18 @@ error:
  * The affine hull in the original space is then obtained as
  * A = preimage(A'', Q_1).
  */
-static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
-       struct isl_basic_set *cone)
+static __isl_give isl_basic_set *affine_hull_with_cone(
+       __isl_take isl_basic_set *bset, __isl_take isl_basic_set *cone)
 {
-       unsigned total;
+       isl_size total;
        unsigned cone_dim;
        struct isl_basic_set *hull;
        struct isl_mat *M, *U, *Q;
 
-       if (!bset || !cone)
+       total = isl_basic_set_dim(cone, isl_dim_all);
+       if (!bset || total < 0)
                goto error;
 
-       total = isl_basic_set_total_dim(cone);
        cone_dim = total - cone->n_eq;
 
        M = isl_mat_sub_alloc6(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
@@ -989,9 +805,11 @@ error:
  * In particular, if the recession cone is full-dimensional, then
  * the affine hull is simply the whole universe.
  */
-static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
+static __isl_give isl_basic_set *uset_affine_hull(
+       __isl_take isl_basic_set *bset)
 {
        struct isl_basic_set *cone;
+       isl_size total;
 
        if (isl_basic_set_plain_is_empty(bset))
                return bset;
@@ -1000,14 +818,17 @@ static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
        if (!cone)
                goto error;
        if (cone->n_eq == 0) {
-               struct isl_basic_set *hull;
+               isl_space *space;
+               space = isl_basic_set_get_space(bset);
                isl_basic_set_free(cone);
-               hull = isl_basic_set_universe_like(bset);
                isl_basic_set_free(bset);
-               return hull;
+               return isl_basic_set_universe(space);
        }
 
-       if (cone->n_eq < isl_basic_set_total_dim(cone))
+       total = isl_basic_set_dim(cone, isl_dim_all);
+       if (total < 0)
+               bset = isl_basic_set_free(bset);
+       if (cone->n_eq < total)
                return affine_hull_with_cone(bset, cone);
 
        isl_basic_set_free(cone);
@@ -1030,8 +851,8 @@ error:
  * In particular, dimensions that correspond to existential variables
  * in bmap and that are found to be fixed are not removed.
  */
-static struct isl_basic_set *equalities_in_underlying_set(
-                                               struct isl_basic_map *bmap)
+static __isl_give isl_basic_set *equalities_in_underlying_set(
+       __isl_take isl_basic_map *bmap)
 {
        struct isl_mat *T1 = NULL;
        struct isl_mat *T2 = NULL;
@@ -1079,10 +900,11 @@ error:
 /* Detect and make explicit all equalities satisfied by the (integer)
  * points in bmap.
  */
-struct isl_basic_map *isl_basic_map_detect_equalities(
-                                               struct isl_basic_map *bmap)
+__isl_give isl_basic_map *isl_basic_map_detect_equalities(
+       __isl_take isl_basic_map *bmap)
 {
        int i, j;
+       isl_size total;
        struct isl_basic_set *hull = NULL;
 
        if (!bmap)
@@ -1103,14 +925,15 @@ struct isl_basic_map *isl_basic_map_detect_equalities(
                isl_basic_set_free(hull);
                return isl_basic_map_set_to_empty(bmap);
        }
-       bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), 0,
-                                       hull->n_eq, 0);
+       bmap = isl_basic_map_extend(bmap, 0, hull->n_eq, 0);
+       total = isl_basic_set_dim(hull, isl_dim_all);
+       if (total < 0)
+               goto error;
        for (i = 0; i < hull->n_eq; ++i) {
                j = isl_basic_map_alloc_equality(bmap);
                if (j < 0)
                        goto error;
-               isl_seq_cpy(bmap->eq[j], hull->eq[i],
-                               1 + isl_basic_set_total_dim(hull));
+               isl_seq_cpy(bmap->eq[j], hull->eq[i], 1 + total);
        }
        isl_vec_free(bmap->sample);
        bmap->sample = isl_vec_copy(hull->sample);
@@ -1127,8 +950,8 @@ error:
 __isl_give isl_basic_set *isl_basic_set_detect_equalities(
                                                __isl_take isl_basic_set *bset)
 {
-       return (isl_basic_set *)
-               isl_basic_map_detect_equalities((isl_basic_map *)bset);
+       return bset_from_bmap(
+               isl_basic_map_detect_equalities(bset_to_bmap(bset)));
 }
 
 __isl_give isl_map *isl_map_detect_equalities(__isl_take isl_map *map)
@@ -1139,16 +962,15 @@ __isl_give isl_map *isl_map_detect_equalities(__isl_take isl_map *map)
 
 __isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
 {
-       return (isl_set *)isl_map_detect_equalities((isl_map *)set);
+       return set_from_map(isl_map_detect_equalities(set_to_map(set)));
 }
 
-/* After computing the rational affine hull (by detecting the implicit
- * equalities), we compute the additional equalities satisfied by
- * the integer points (if any) and add the original equalities back in.
+/* Return the superset of "bmap" described by the equalities
+ * satisfied by "bmap" that are already known.
  */
-struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
+__isl_give isl_basic_map *isl_basic_map_plain_affine_hull(
+       __isl_take isl_basic_map *bmap)
 {
-       bmap = isl_basic_map_detect_equalities(bmap);
        bmap = isl_basic_map_cow(bmap);
        if (bmap)
                isl_basic_map_free_inequality(bmap, bmap->n_ineq);
@@ -1156,10 +978,31 @@ struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
        return bmap;
 }
 
-struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
+/* Return the superset of "bset" described by the equalities
+ * satisfied by "bset" that are already known.
+ */
+__isl_give isl_basic_set *isl_basic_set_plain_affine_hull(
+       __isl_take isl_basic_set *bset)
 {
-       return (struct isl_basic_set *)
-               isl_basic_map_affine_hull((struct isl_basic_map *)bset);
+       return isl_basic_map_plain_affine_hull(bset);
+}
+
+/* After computing the rational affine hull (by detecting the implicit
+ * equalities), we compute the additional equalities satisfied by
+ * the integer points (if any) and add the original equalities back in.
+ */
+__isl_give isl_basic_map *isl_basic_map_affine_hull(
+       __isl_take isl_basic_map *bmap)
+{
+       bmap = isl_basic_map_detect_equalities(bmap);
+       bmap = isl_basic_map_plain_affine_hull(bmap);
+       return bmap;
+}
+
+__isl_give isl_basic_set *isl_basic_set_affine_hull(
+       __isl_take isl_basic_set *bset)
+{
+       return bset_from_bmap(isl_basic_map_affine_hull(bset_to_bmap(bset)));
 }
 
 /* Given a rational affine matrix "M", add stride constraints to "bmap"
@@ -1189,8 +1032,7 @@ static __isl_give isl_basic_map *add_strides(__isl_take isl_basic_map *bmap,
        if (isl_int_is_one(M->row[0][0]))
                return bmap;
 
-       bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
-                                       M->n_row - 1, M->n_row - 1, 0);
+       bmap = isl_basic_map_extend(bmap, M->n_row - 1, M->n_row - 1, 0);
 
        isl_int_init(gcd);
        for (i = 1; i < M->n_row; ++i) {
@@ -1248,10 +1090,10 @@ error:
 static __isl_give isl_basic_map *isl_basic_map_make_strides_explicit(
        __isl_take isl_basic_map *bmap)
 {
-       int known;
+       isl_bool known;
        int n_known;
        int n, n_col;
-       int total;
+       isl_size v_div;
        isl_ctx *ctx;
        isl_mat *A, *B, *M;
 
@@ -1268,17 +1110,19 @@ static __isl_give isl_basic_map *isl_basic_map_make_strides_explicit(
        for (n_known = 0; n_known < bmap->n_div; ++n_known)
                if (isl_int_is_zero(bmap->div[n_known][0]))
                        break;
-       ctx = isl_basic_map_get_ctx(bmap);
-       total = isl_space_dim(bmap->dim, isl_dim_all);
+       v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
+       if (v_div < 0)
+               return isl_basic_map_free(bmap);
        for (n = 0; n < bmap->n_eq; ++n)
-               if (isl_seq_first_non_zero(bmap->eq[n] + 1 + total + n_known,
-                                           bmap->n_div - n_known) == -1)
+               if (!isl_seq_any_non_zero(bmap->eq[n] + 1 + v_div + n_known,
+                                           bmap->n_div - n_known))
                        break;
        if (n == 0)
                return bmap;
-       B = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 0, 1 + total + n_known);
+       ctx = isl_basic_map_get_ctx(bmap);
+       B = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 0, 1 + v_div + n_known);
        n_col = bmap->n_div - n_known;
-       A = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 1 + total + n_known, n_col);
+       A = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 1 + v_div + n_known, n_col);
        A = isl_mat_left_hermite(A, 0, NULL, NULL);
        A = isl_mat_drop_cols(A, n, n_col - n);
        A = isl_mat_lin_to_aff(A);
@@ -1331,6 +1175,18 @@ static __isl_give isl_set *isl_set_local_affine_hull(__isl_take isl_set *set)
        return isl_map_local_affine_hull(set);
 }
 
+/* Return an empty basic map living in the same space as "map".
+ */
+static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
+       __isl_take isl_map *map)
+{
+       isl_space *space;
+
+       space = isl_map_get_space(map);
+       isl_map_free(map);
+       return isl_basic_map_empty(space);
+}
+
 /* Compute the affine hull of "map".
  *
  * We first compute the affine hull of each basic map separately.
@@ -1361,16 +1217,13 @@ __isl_give isl_basic_map *isl_map_affine_hull(__isl_take isl_map *map)
        map = isl_map_local_affine_hull(map);
        map = isl_map_remove_empty_parts(map);
        map = isl_map_remove_unknown_divs(map);
-       map = isl_map_align_divs(map);
+       map = isl_map_align_divs_internal(map);
 
        if (!map)
                return NULL;
 
-       if (map->n == 0) {
-               hull = isl_basic_map_empty_like_map(map);
-               isl_map_free(map);
-               return hull;
-       }
+       if (map->n == 0)
+               return replace_map_by_empty_basic_map(map);
 
        model = isl_basic_map_copy(map->p[0]);
        set = isl_map_underlying_set(map);
@@ -1393,8 +1246,7 @@ error:
        return NULL;
 }
 
-struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
+__isl_give isl_basic_set *isl_set_affine_hull(__isl_take isl_set *set)
 {
-       return (struct isl_basic_set *)
-               isl_map_affine_hull((struct isl_map *)set);
+       return bset_from_bmap(isl_map_affine_hull(set_to_map(set)));
 }