isl_basic_set_opt: avoid invalid access on error path
[platform/upstream/isl.git] / isl_mat.c
index 7a0d1a2..cff8123 100644 (file)
--- a/isl_mat.c
+++ b/isl_mat.c
@@ -1,7 +1,7 @@
 /*
  * Copyright 2008-2009 Katholieke Universiteit Leuven
  *
- * Use of this software is governed by the GNU LGPLv2.1 license
+ * Use of this software is governed by the MIT license
  *
  * Written by Sven Verdoolaege, K.U.Leuven, Departement
  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
@@ -281,7 +281,7 @@ error:
        return NULL;
 }
 
-struct isl_mat *isl_mat_identity(struct isl_ctx *ctx, unsigned n_row)
+__isl_give isl_mat *isl_mat_diag(isl_ctx *ctx, unsigned n_row, isl_int d)
 {
        int i;
        struct isl_mat *mat;
@@ -291,13 +291,20 @@ struct isl_mat *isl_mat_identity(struct isl_ctx *ctx, unsigned n_row)
                return NULL;
        for (i = 0; i < n_row; ++i) {
                isl_seq_clr(mat->row[i], i);
-               isl_int_set_si(mat->row[i][i], 1);
+               isl_int_set(mat->row[i][i], d);
                isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
        }
 
        return mat;
 }
 
+__isl_give isl_mat *isl_mat_identity(isl_ctx *ctx, unsigned n_row)
+{
+       if (!ctx)
+               return NULL;
+       return isl_mat_diag(ctx, n_row, ctx->one);
+}
+
 struct isl_vec *isl_mat_vec_product(struct isl_mat *mat, struct isl_vec *vec)
 {
        int i;
@@ -902,6 +909,9 @@ struct isl_mat *isl_mat_transpose(struct isl_mat *mat)
        struct isl_mat *transpose = NULL;
        int i, j;
 
+       if (!mat)
+               return NULL;
+
        if (mat->n_col == mat->n_row) {
                mat = isl_mat_cow(mat);
                if (!mat)
@@ -1582,6 +1592,21 @@ __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
        return mat;
 }
 
+__isl_give isl_mat *isl_mat_scale_down_row(__isl_take isl_mat *mat, int row,
+       isl_int m)
+{
+       if (isl_int_is_one(m))
+               return mat;
+
+       mat = isl_mat_cow(mat);
+       if (!mat)
+               return NULL;
+
+       isl_seq_scale_down(mat->row[row], mat->row[row], m, mat->n_col);
+
+       return mat;
+}
+
 __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
 {
        isl_int gcd;