isl_mat_scale_down: avoid trampling over aliased matrices
[platform/upstream/isl.git] / isl_mat.c
index 1c56388..4e6315c 100644 (file)
--- a/isl_mat.c
+++ b/isl_mat.c
@@ -7,9 +7,10 @@
  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  */
 
-#include "isl_dim.h"
-#include "isl_seq.h"
-#include "isl_mat.h"
+#include <isl_ctx_private.h>
+#include <isl/dim.h>
+#include <isl/seq.h>
+#include <isl_mat_private.h>
 #include "isl_map_private.h"
 #include <isl_dim_private.h>
 
@@ -54,6 +55,7 @@ struct isl_mat *isl_mat_extend(struct isl_mat *mat,
 {
        int i;
        isl_int *old;
+       isl_int **row;
 
        if (!mat)
                return NULL;
@@ -86,9 +88,10 @@ struct isl_mat *isl_mat_extend(struct isl_mat *mat,
        mat->block = isl_blk_extend(mat->ctx, mat->block, n_row * mat->max_col);
        if (isl_blk_is_error(mat->block))
                goto error;
-       mat->row = isl_realloc_array(mat->ctx, mat->row, isl_int *, n_row);
-       if (!mat->row)
+       row = isl_realloc_array(mat->ctx, mat->row, isl_int *, n_row);
+       if (!row)
                goto error;
+       mat->row = row;
 
        for (i = 0; i < mat->n_row; ++i)
                mat->row[i] = mat->block.data + (mat->row[i] - old);
@@ -202,6 +205,49 @@ void isl_mat_free(struct isl_mat *mat)
        free(mat);
 }
 
+int isl_mat_rows(__isl_keep isl_mat *mat)
+{
+       return mat ? mat->n_row : -1;
+}
+
+int isl_mat_cols(__isl_keep isl_mat *mat)
+{
+       return mat ? mat->n_col : -1;
+}
+
+int isl_mat_get_element(__isl_keep isl_mat *mat, int row, int col, isl_int *v)
+{
+       if (!mat)
+               return -1;
+       if (row < 0 || row >= mat->n_row)
+               isl_die(mat->ctx, isl_error_invalid, "row out of range",
+                       return -1);
+       if (col < 0 || col >= mat->n_col)
+               isl_die(mat->ctx, isl_error_invalid, "column out of range",
+                       return -1);
+       isl_int_set(*v, mat->row[row][col]);
+       return 0;
+}
+
+__isl_give isl_mat *isl_mat_set_element(__isl_take isl_mat *mat,
+       int row, int col, isl_int v)
+{
+       mat = isl_mat_cow(mat);
+       if (!mat)
+               return NULL;
+       if (row < 0 || row >= mat->n_row)
+               isl_die(mat->ctx, isl_error_invalid, "row out of range",
+                       goto error);
+       if (col < 0 || col >= mat->n_col)
+               isl_die(mat->ctx, isl_error_invalid, "column out of range",
+                       goto error);
+       isl_int_set(mat->row[row][col], v);
+       return mat;
+error:
+       isl_mat_free(mat);
+       return NULL;
+}
+
 struct isl_mat *isl_mat_identity(struct isl_ctx *ctx, unsigned n_row)
 {
        int i;
@@ -482,6 +528,7 @@ error:
                isl_mat_free(*U);
                *U = NULL;
        }
+       isl_mat_free(M);
        return NULL;
 }
 
@@ -813,6 +860,7 @@ struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
        return inv;
 error:
        isl_mat_free(mat);
+       isl_mat_free(inv);
        return NULL;
 }
 
@@ -890,6 +938,8 @@ struct isl_mat *isl_mat_product(struct isl_mat *left, struct isl_mat *right)
        if (left->n_col == 0) {
                for (i = 0; i < prod->n_row; ++i)
                        isl_seq_clr(prod->row[i], prod->n_col);
+               isl_mat_free(left);
+               isl_mat_free(right);
                return prod;
        }
        for (i = 0; i < prod->n_row; ++i) {
@@ -1200,6 +1250,18 @@ void isl_mat_col_submul(struct isl_mat *mat,
                isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
 }
 
+void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
+{
+       int i;
+
+       if (!mat)
+               return;
+
+       for (i = 0; i < mat->n_row; ++i)
+               isl_int_add(mat->row[i][dst_col],
+                           mat->row[i][dst_col], mat->row[i][src_col]);
+}
+
 void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
 {
        int i;
@@ -1379,6 +1441,10 @@ __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
 {
        int i;
 
+       if (isl_int_is_one(m))
+               return mat;
+
+       mat = isl_mat_cow(mat);
        if (!mat)
                return NULL;