isl_mat_swap_cols: extract out check_col_range
authorSven Verdoolaege <sven.verdoolaege@gmail.com>
Fri, 8 Sep 2017 10:34:38 +0000 (8 12:34 +0200)
committerSven Verdoolaege <sven.verdoolaege@gmail.com>
Sat, 16 Sep 2017 08:42:56 +0000 (16 10:42 +0200)
This function will be reused in the next commit.

Signed-off-by: Sven Verdoolaege <sven.verdoolaege@gmail.com>
isl_mat.c

index 545a8a2..b0ea0e0 100644 (file)
--- a/isl_mat.c
+++ b/isl_mat.c
@@ -271,6 +271,20 @@ static isl_stat check_col(__isl_keep isl_mat *mat, int col)
        return isl_stat_ok;
 }
 
+/* Check that there are "n" columns starting at position "first" in "mat".
+ */
+static isl_stat check_col_range(__isl_keep isl_mat *mat, unsigned first,
+       unsigned n)
+{
+       if (!mat)
+               return isl_stat_error;
+       if (first + n > mat->n_col || first + n < first)
+               isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
+                       "column position or range out of bounds",
+                       return isl_stat_error);
+       return isl_stat_ok;
+}
+
 int isl_mat_get_element(__isl_keep isl_mat *mat, int row, int col, isl_int *v)
 {
        if (!mat)
@@ -1061,17 +1075,13 @@ struct isl_mat *isl_mat_swap_cols(struct isl_mat *mat, unsigned i, unsigned j)
        int r;
 
        mat = isl_mat_cow(mat);
-       if (!mat)
-               return NULL;
-       isl_assert(mat->ctx, i < mat->n_col, goto error);
-       isl_assert(mat->ctx, j < mat->n_col, goto error);
+       if (check_col_range(mat, i, 1) < 0 ||
+           check_col_range(mat, j, 1) < 0)
+               return isl_mat_free(mat);
 
        for (r = 0; r < mat->n_row; ++r)
                isl_int_swap(mat->row[r][i], mat->row[r][j]);
        return mat;
-error:
-       isl_mat_free(mat);
-       return NULL;
 }
 
 struct isl_mat *isl_mat_swap_rows(struct isl_mat *mat, unsigned i, unsigned j)