- integrated new PBQP solver into existing PBQP transformation, so it can be easily...
[libfirm] / matrix.c
1 #include "assert.h"
2
3 #include "pbqp_t.h"
4 #include "matrix.h"
5
6 pbqp_matrix *pbqp_matrix_alloc(pbqp *pbqp, unsigned rows, unsigned cols)
7 {
8         pbqp_matrix *mat;
9         unsigned index;
10
11         assert(cols> 0);
12         assert(rows> 0);
13
14         unsigned length = rows * cols;
15
16         mat = obstack_alloc(&pbqp->obstack, sizeof(*mat) + sizeof(num) * (length - 1));
17         assert(mat);
18
19         mat->cols = cols;
20         mat->rows = rows;
21         for (index = 0; index < length; ++index) {
22                 mat->entries[index] = 0;
23         }
24
25         return mat;
26 }
27
28 pbqp_matrix *pbqp_matrix_copy(pbqp *pbqp, pbqp_matrix *m)
29 {
30         int i;
31         int len;
32         pbqp_matrix *copy = obstack_alloc(&pbqp->obstack, sizeof(*copy));
33
34         assert(copy);
35
36         len = m->rows * m->cols;
37
38         for (i = 0; i < len; ++i) {
39                 copy->entries[i] = m->entries[i];
40         }
41
42         return copy;
43 }
44
45 void pbqp_matrix_add(pbqp_matrix *sum, pbqp_matrix *summand)
46 {
47         int i;
48         int len;
49
50         assert(sum);
51         assert(summand);
52         assert(sum->cols == summand->cols);
53         assert(sum->rows == summand->rows);
54
55         len = sum->rows * sum->cols;
56
57         for (i = 0; i < len; ++i) {
58                 sum->entries[i] += summand->entries[i];
59         }
60 }
61
62 void pbqp_matrix_set(pbqp_matrix *mat, unsigned row, unsigned col, num value)
63 {
64         assert(mat);
65         assert(col < mat->cols);
66         assert(row < mat->rows);
67
68         mat->entries[row * mat->cols + col] = value;
69 }