Bugfix: handle added edges correctly if src_index and tgt_index are in the wrong...
[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 pbqp_matrix *pbqp_matrix_copy_and_transpose(pbqp *pbqp, pbqp_matrix *m)
34 {
35         unsigned     i;
36         unsigned     j;
37         unsigned     cols = m->cols;
38         unsigned     rows = m->rows;
39         unsigned     len  = rows * cols;
40         pbqp_matrix *copy = obstack_alloc(&pbqp->obstack, sizeof(*copy) + sizeof(*copy->entries) * len);
41         assert(copy);
42
43         for (i = 0; i < rows; ++i) {
44                 for (j = 0; j < cols; ++j) {
45                         copy->entries[j*rows+i] = m->entries[i*cols+j];
46                 }
47         }
48
49         copy->cols = rows;
50         copy->rows = cols;
51
52         return copy;
53 }
54
55 void pbqp_matrix_transpose(pbqp *pbqp, pbqp_matrix *mat)
56 {
57         unsigned len;
58
59         assert(mat);
60         len = mat->rows * mat->cols;
61
62         pbqp_matrix *tmp = pbqp_matrix_copy_and_transpose(pbqp, mat);
63
64         memcpy(mat, tmp, sizeof(*mat) + sizeof(*mat->entries) * len);
65
66         obstack_free(&pbqp->obstack, tmp);
67 }
68
69 void pbqp_matrix_add(pbqp_matrix *sum, pbqp_matrix *summand)
70 {
71         int i;
72         int len;
73
74         assert(sum);
75         assert(summand);
76         assert(sum->cols == summand->cols);
77         assert(sum->rows == summand->rows);
78
79         len = sum->rows * sum->cols;
80
81         for (i = 0; i < len; ++i) {
82                 sum->entries[i] = pbqp_add(sum->entries[i], summand->entries[i]);
83         }
84 }
85
86 void pbqp_matrix_set(pbqp_matrix *mat, unsigned row, unsigned col, num value)
87 {
88         assert(mat);
89         assert(col < mat->cols);
90         assert(row < mat->rows);
91
92         mat->entries[row * mat->cols + col] = value;
93 }
94
95 num pbqp_matrix_get_col_min(pbqp_matrix *matrix, unsigned col_index, vector *flags)
96 {
97         unsigned row_index;
98         num min = INF_COSTS;
99
100         assert(matrix);
101         assert(flags);
102         assert(matrix->rows == flags->len);
103
104         unsigned col_len = matrix->cols;
105         unsigned row_len = matrix->rows;
106
107         for (row_index = 0; row_index < row_len; ++row_index) {
108                 /* Ignore virtual deleted columns. */
109                 if (flags->entries[row_index].data == INF_COSTS) continue;
110
111                 num elem = matrix->entries[row_index * col_len + col_index];
112
113                 if (elem < min) {
114                         min = elem;
115                 }
116         }
117
118         return min;
119 }
120
121 void pbqp_matrix_sub_col_value(pbqp_matrix *matrix, unsigned col_index,
122                 vector *flags, num value)
123 {
124         unsigned row_index;
125
126         assert(matrix);
127         assert(flags);
128         assert(matrix->rows == flags->len);
129
130         unsigned col_len = matrix->cols;
131         unsigned row_len = matrix->rows;
132
133         for (row_index = 0; row_index < row_len; ++row_index) {
134                 /* inf - x = inf if x < inf */
135                 if (matrix->entries[row_index * col_len + col_index] == INF_COSTS && value
136                                 != INF_COSTS)
137                         continue;
138                 matrix->entries[row_index * col_len + col_index] -= value;
139         }
140 }
141
142 num pbqp_matrix_get_row_min(pbqp_matrix *matrix, unsigned row_index, vector *flags)
143 {
144         unsigned col_index;
145         num min = INF_COSTS;
146
147         assert(matrix);
148         assert(flags);
149         assert(matrix->cols == flags->len);
150
151         unsigned len = flags->len;
152
153         for (col_index = 0; col_index < len; ++col_index) {
154                 /* Ignore virtual deleted columns. */
155                 if (flags->entries[col_index].data == INF_COSTS) continue;
156
157                 num elem = matrix->entries[row_index * len + col_index];
158
159                 if (elem < min) {
160                         min = elem;
161                 }
162         }
163
164         return min;
165 }
166
167 void pbqp_matrix_sub_row_value(pbqp_matrix *matrix, unsigned row_index,
168                 vector *flags, num value)
169 {
170         unsigned col_index;
171
172         assert(matrix);
173         assert(flags);
174         assert(matrix->cols == flags->len);
175
176         unsigned len = flags->len;
177
178         for (col_index = 0; col_index < len; ++col_index) {
179                 /* inf - x = inf if x < inf */
180                 if (matrix->entries[row_index * len + col_index] == INF_COSTS && value
181                                 != INF_COSTS)
182                         continue;
183                 matrix->entries[row_index * len + col_index] -= value;
184         }
185 }
186
187 int pbqp_matrix_is_zero(pbqp_matrix *mat, vector *src_vec, vector *tgt_vec)
188 {
189         unsigned col_index;
190         unsigned col_len;
191         unsigned row_index;
192         unsigned row_len;
193
194         assert(mat);
195         assert(src_vec);
196         assert(tgt_vec);
197         assert(mat->cols = tgt_vec->len);
198         assert(mat->rows = src_vec->len);
199
200         col_len = mat->cols;
201         row_len = mat->rows;
202
203         for (row_index = 0; row_index < row_len; ++row_index) {
204                 if (src_vec->entries[row_index].data == INF_COSTS) continue;
205                 for (col_index = 0; col_index < col_len; ++col_index) {
206                         if (tgt_vec->entries[col_index].data == INF_COSTS) continue;
207
208                         if (mat->entries[row_index * col_len + col_index] != 0) {
209                                 return 0;
210                         }
211                 }
212         }
213
214         return 1;
215 }
216
217 void pbqp_matrix_add_to_all_cols(pbqp_matrix *mat, vector *vec)
218 {
219         unsigned col_index;
220         unsigned col_len;
221         unsigned row_index;
222         unsigned row_len;
223
224         assert(mat);
225         assert(vec);
226         assert(mat->rows == vec->len);
227
228         col_len = mat->cols;
229         row_len = mat->rows;
230
231         for (row_index = 0; row_index < row_len; ++row_index) {
232                 num value = vec->entries[row_index].data;
233                 for (col_index = 0; col_index < col_len; ++col_index) {
234                         mat->entries[row_index * col_len + col_index] = pbqp_add(
235                                         mat->entries[row_index * col_len + col_index], value);
236                 }
237         }
238 }
239
240 void pbqp_matrix_add_to_all_rows(pbqp_matrix *mat, vector *vec)
241 {
242         unsigned col_index;
243         unsigned col_len;
244         unsigned row_index;
245         unsigned row_len;
246
247         assert(mat);
248         assert(vec);
249         assert(mat->cols == vec->len);
250
251         col_len = mat->cols;
252         row_len = mat->rows;
253
254         for (row_index = 0; row_index < row_len; ++row_index) {
255                 for (col_index = 0; col_index < col_len; ++col_index) {
256                         num value = vec->entries[col_index].data;
257
258                         mat->entries[row_index * col_len + col_index] = pbqp_add(mat->entries[row_index * col_len + col_index], value);
259                 }
260         }
261 }