Renamed function.
[libfirm] / matrix.c
1 #include <assert.h>
2 #include <string.h>
3
4 #include "pbqp_t.h"
5 #include "matrix.h"
6
7 pbqp_matrix *pbqp_matrix_alloc(pbqp *pbqp, unsigned rows, unsigned cols)
8 {
9         assert(cols> 0);
10         assert(rows> 0);
11
12         unsigned length = rows * cols;
13
14         pbqp_matrix *mat = obstack_alloc(&pbqp->obstack, sizeof(*mat) + sizeof(*mat->entries) * length);
15         assert(mat);
16
17         mat->cols = cols;
18         mat->rows = rows;
19         memset(mat->entries, 0, sizeof(*mat->entries) * length);
20
21         return mat;
22 }
23
24 pbqp_matrix *pbqp_matrix_copy(pbqp *pbqp, pbqp_matrix *m)
25 {
26         unsigned     len  = m->rows * m->cols;
27         pbqp_matrix *copy = obstack_copy(&pbqp->obstack, m, sizeof(*copy) + sizeof(*copy->entries) * len);
28         assert(copy);
29
30         return copy;
31 }
32
33 void pbqp_matrix_add(pbqp_matrix *sum, pbqp_matrix *summand)
34 {
35         int i;
36         int len;
37
38         assert(sum);
39         assert(summand);
40         assert(sum->cols == summand->cols);
41         assert(sum->rows == summand->rows);
42
43         len = sum->rows * sum->cols;
44
45         for (i = 0; i < len; ++i) {
46                 sum->entries[i] += summand->entries[i];
47         }
48 }
49
50 void pbqp_matrix_set(pbqp_matrix *mat, unsigned row, unsigned col, num value)
51 {
52         assert(mat);
53         assert(col < mat->cols);
54         assert(row < mat->rows);
55
56         mat->entries[row * mat->cols + col] = value;
57 }