2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * @author Sebastian Buchwald
31 #include "adt/array.h"
35 num pbqp_add(num x, num y)
37 if (x == INF_COSTS || y == INF_COSTS) return INF_COSTS;
41 #if !KAPS_USE_UNSIGNED
42 /* No positive overflow. */
43 assert(x < 0 || y < 0 || res >= x);
44 assert(x < 0 || y < 0 || res >= y);
47 /* No negative overflow. */
48 assert(x > 0 || y > 0 || res <= x);
49 assert(x > 0 || y > 0 || res <= y);
51 /* Result is not infinity.*/
52 assert(res < INF_COSTS);
57 vector_t *vector_alloc(pbqp_t *pbqp, unsigned length)
60 vector_t *vec = (vector_t*)obstack_alloc(&pbqp->obstack, sizeof(*vec) + sizeof(*vec->entries) * length);
63 memset(vec->entries, 0, sizeof(*vec->entries) * length);
68 vector_t *vector_copy(pbqp_t *pbqp, vector_t *v)
70 unsigned len = v->len;
71 vector_t *copy = (vector_t*)obstack_copy(&pbqp->obstack, v, sizeof(*copy) + sizeof(*copy->entries) * len);
77 void vector_add(vector_t *sum, vector_t *summand)
82 assert(sum->len == summand->len);
86 for (i = 0; i < len; ++i) {
87 sum->entries[i].data = pbqp_add(sum->entries[i].data,
88 summand->entries[i].data);
92 void vector_set(vector_t *vec, unsigned index, num value)
94 assert(index < vec->len);
95 vec->entries[index].data = value;
98 #if KAPS_ENABLE_VECTOR_NAMES
99 void vector_set_description(vector_t *vec, unsigned index, const char *name)
101 assert(index < vec->len);
102 vec->entries[index].name = name;
106 void vector_add_value(vector_t *vec, num value)
113 for (index = 0; index < len; ++index) {
114 vec->entries[index].data = pbqp_add(vec->entries[index].data, value);
118 void vector_add_matrix_col(vector_t *vec, pbqp_matrix_t *mat, unsigned col_index)
123 assert(vec->len == mat->rows);
124 assert(col_index < mat->cols);
128 for (index = 0; index < len; ++index) {
129 vec->entries[index].data = pbqp_add(vec->entries[index].data, mat->entries[index * mat->cols + col_index]);
133 void vector_add_matrix_row(vector_t *vec, pbqp_matrix_t *mat, unsigned row_index)
138 assert(vec->len == mat->cols);
139 assert(row_index < mat->rows);
143 for (index = 0; index < len; ++index) {
144 vec->entries[index].data = pbqp_add(vec->entries[index].data,
145 mat->entries[row_index * mat->cols + index]);
149 num vector_get_min(vector_t *vec)
158 for (index = 0; index < len; ++index) {
159 num elem = vec->entries[index].data;
169 unsigned vector_get_min_index(vector_t *vec)
173 unsigned min_index = 0;
179 for (index = 0; index < len; ++index) {
180 num elem = vec->entries[index].data;