merge kaps
[libfirm] / ir / kaps / vector.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   PBQP vector.
23  * @date    02.10.2008
24  * @author  Sebastian Buchwald
25  * @version $Id$
26  */
27 #include "config.h"
28
29 #include <string.h>
30
31 #include "adt/array.h"
32
33 #include "vector.h"
34
35 num pbqp_add(num x, num y)
36 {
37         if (x == INF_COSTS || y == INF_COSTS) return INF_COSTS;
38
39         num res = x + y;
40
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);
45 #endif
46
47         /* No negative overflow. */
48         assert(x > 0 || y > 0 || res <= x);
49         assert(x > 0 || y > 0 || res <= y);
50
51         /* Result is not infinity.*/
52         assert(res < INF_COSTS);
53
54         return res;
55 }
56
57 vector_t *vector_alloc(pbqp_t *pbqp, unsigned length)
58 {
59         assert(length > 0);
60         vector_t *vec = (vector_t*)obstack_alloc(&pbqp->obstack, sizeof(*vec) + sizeof(*vec->entries) * length);
61         assert(vec);
62
63         vec->len = length;
64         memset(vec->entries, 0, sizeof(*vec->entries) * length);
65
66         return vec;
67 }
68
69 vector_t *vector_copy(pbqp_t *pbqp, vector_t *v)
70 {
71         unsigned  len  = v->len;
72         vector_t *copy = (vector_t*)obstack_copy(&pbqp->obstack, v, sizeof(*copy) + sizeof(*copy->entries) * len);
73         assert(copy);
74
75         return copy;
76 }
77
78 void vector_add(vector_t *sum, vector_t *summand)
79 {
80         int i;
81         int len;
82
83         assert(sum);
84         assert(summand);
85         assert(sum->len == summand->len);
86
87         len = sum->len;
88
89         for (i = 0; i < len; ++i) {
90                 sum->entries[i].data = pbqp_add(sum->entries[i].data,
91                                 summand->entries[i].data);
92         }
93 }
94
95 void vector_set(vector_t *vec, unsigned index, num value)
96 {
97         assert(index < vec->len);
98         vec->entries[index].data = value;
99 }
100
101 #if KAPS_ENABLE_VECTOR_NAMES
102 void vector_set_description(vector_t *vec, unsigned index, const char *name)
103 {
104         assert(index < vec->len);
105         vec->entries[index].name = name;
106 }
107 #endif
108
109 void vector_add_value(vector_t *vec, num value)
110 {
111         unsigned index;
112         unsigned len;
113
114         assert(vec);
115
116         len = vec->len;
117
118         for (index = 0; index < len; ++index) {
119                 vec->entries[index].data = pbqp_add(vec->entries[index].data, value);
120         }
121 }
122
123 void vector_add_matrix_col(vector_t *vec, pbqp_matrix_t *mat, unsigned col_index)
124 {
125         unsigned index;
126         unsigned len;
127
128         assert(vec);
129         assert(mat);
130         assert(vec->len == mat->rows);
131         assert(col_index < mat->cols);
132
133         len = vec->len;
134
135         for (index = 0; index < len; ++index) {
136                 vec->entries[index].data = pbqp_add(vec->entries[index].data, mat->entries[index * mat->cols + col_index]);
137         }
138 }
139
140 void vector_add_matrix_row(vector_t *vec, pbqp_matrix_t *mat, unsigned row_index)
141 {
142         unsigned index;
143         unsigned len;
144
145         assert(vec);
146         assert(mat);
147         assert(vec->len == mat->cols);
148         assert(row_index < mat->rows);
149
150         len = vec->len;
151
152         for (index = 0; index < len; ++index) {
153                 vec->entries[index].data = pbqp_add(vec->entries[index].data,
154                                 mat->entries[row_index * mat->cols + index]);
155         }
156 }
157
158 num vector_get_min(vector_t *vec)
159 {
160         unsigned index;
161         unsigned len;
162         num      min = INF_COSTS;
163
164         assert(vec);
165
166         len = vec->len;
167         assert(len > 0);
168
169         for (index = 0; index < len; ++index) {
170                 num elem = vec->entries[index].data;
171
172                 if (elem < min) {
173                         min = elem;
174                 }
175         }
176
177         return min;
178 }
179
180 unsigned vector_get_min_index(vector_t *vec)
181 {
182         unsigned index;
183         unsigned len;
184         unsigned min_index = 0;
185         num      min       = INF_COSTS;
186
187         assert(vec);
188
189         len = vec->len;
190         assert(len > 0);
191
192         for (index = 0; index < len; ++index) {
193                 num elem = vec->entries[index].data;
194
195                 if (elem < min) {
196                         min = elem;
197                         min_index = index;
198                 }
199         }
200
201         return min_index;
202 }