isl_mat_extend: properly extend matrices with reordered rows
[platform/upstream/isl.git] / isl_mat.c
index 1230a18..10b81e3 100644 (file)
--- a/isl_mat.c
+++ b/isl_mat.c
@@ -36,6 +36,55 @@ error:
        return NULL;
 }
 
+struct isl_mat *isl_mat_extend(struct isl_ctx *ctx, struct isl_mat *mat,
+       unsigned n_row, unsigned n_col)
+{
+       int i;
+       isl_int *old;
+
+       if (!mat)
+               return NULL;
+
+       if (mat->n_col >= n_col && mat->n_row >= n_row)
+               return mat;
+
+       if (mat->n_col < n_col) {
+               struct isl_mat *new_mat;
+
+               new_mat = isl_mat_alloc(ctx, n_row, n_col);
+               if (!new_mat)
+                       goto error;
+               for (i = 0; i < mat->n_row; ++i)
+                       isl_seq_cpy(new_mat->row[i], mat->row[i], mat->n_col);
+               isl_mat_free(ctx, mat);
+               return new_mat;
+       }
+
+       mat = isl_mat_cow(ctx, mat);
+       if (!mat)
+               goto error;
+
+       assert(mat->ref == 1);
+       old = mat->block.data;
+       mat->block = isl_blk_extend(ctx, mat->block, n_row * mat->n_col);
+       if (isl_blk_is_error(mat->block))
+               goto error;
+       mat->row = isl_realloc_array(ctx, mat->row, isl_int *, n_row);
+       if (!mat->row)
+               goto error;
+
+       for (i = 0; i < mat->n_row; ++i)
+               mat->row[i] = mat->block.data + (mat->row[i] - old);
+       for (i = mat->n_row; i < n_row; ++i)
+               mat->row[i] = mat->block.data + i * mat->n_col;
+       mat->n_row = n_row;
+
+       return mat;
+error:
+       isl_mat_free(ctx, mat);
+       return NULL;
+}
+
 struct isl_mat *isl_mat_sub_alloc(struct isl_ctx *ctx, isl_int **row,
        unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
 {
@@ -655,16 +704,26 @@ error:
 
 struct isl_mat *isl_mat_transpose(struct isl_ctx *ctx, struct isl_mat *mat)
 {
+       struct isl_mat *transpose = NULL;
        int i, j;
 
-       mat = isl_mat_cow(ctx, mat);
-       if (!mat)
-               return NULL;
-       isl_assert(ctx, mat->n_col == mat->n_row, goto error);
+       if (mat->n_col == mat->n_row) {
+               mat = isl_mat_cow(ctx, mat);
+               if (!mat)
+                       return NULL;
+               for (i = 0; i < mat->n_row; ++i)
+                       for (j = i + 1; j < mat->n_col; ++j)
+                               isl_int_swap(mat->row[i][j], mat->row[j][i]);
+               return mat;
+       }
+       transpose = isl_mat_alloc(ctx, mat->n_col, mat->n_row);
+       if (!transpose)
+               goto error;
        for (i = 0; i < mat->n_row; ++i)
-               for (j = i + 1; j < mat->n_col; ++j)
-                       isl_int_swap(mat->row[i][j], mat->row[j][i]);
-       return mat;
+               for (j = 0; j < mat->n_col; ++j)
+                       isl_int_set(transpose->row[j][i], mat->row[i][j]);
+       isl_mat_free(ctx, mat);
+       return transpose;
 error:
        isl_mat_free(ctx, mat);
        return NULL;
@@ -799,6 +858,12 @@ struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
        }
        isl_mat_free(ctx, t);
 
+       ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
+       ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
+       ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
+       ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
+       ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
+
        bset = isl_basic_set_simplify(bset);
        bset = isl_basic_set_finalize(bset);