Remove superfluous asserts
[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
62         vec->len = length;
63         memset(vec->entries, 0, sizeof(*vec->entries) * length);
64
65         return vec;
66 }
67
68 vector_t *vector_copy(pbqp_t *pbqp, vector_t *v)
69 {
70         unsigned  len  = v->len;
71         vector_t *copy = (vector_t*)obstack_copy(&pbqp->obstack, v, sizeof(*copy) + sizeof(*copy->entries) * len);
72         assert(copy);
73
74         return copy;
75 }
76
77 void vector_add(vector_t *sum, vector_t *summand)
78 {
79         int i;
80         int len;
81
82         assert(sum->len == summand->len);
83
84         len = sum->len;
85
86         for (i = 0; i < len; ++i) {
87                 sum->entries[i].data = pbqp_add(sum->entries[i].data,
88                                 summand->entries[i].data);
89         }
90 }
91
92 void vector_set(vector_t *vec, unsigned index, num value)
93 {
94         assert(index < vec->len);
95         vec->entries[index].data = value;
96 }
97
98 #if KAPS_ENABLE_VECTOR_NAMES
99 void vector_set_description(vector_t *vec, unsigned index, const char *name)
100 {
101         assert(index < vec->len);
102         vec->entries[index].name = name;
103 }
104 #endif
105
106 void vector_add_value(vector_t *vec, num value)
107 {
108         unsigned index;
109         unsigned len;
110
111         len = vec->len;
112
113         for (index = 0; index < len; ++index) {
114                 vec->entries[index].data = pbqp_add(vec->entries[index].data, value);
115         }
116 }
117
118 void vector_add_matrix_col(vector_t *vec, pbqp_matrix_t *mat, unsigned col_index)
119 {
120         unsigned index;
121         unsigned len;
122
123         assert(vec->len == mat->rows);
124         assert(col_index < mat->cols);
125
126         len = vec->len;
127
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]);
130         }
131 }
132
133 void vector_add_matrix_row(vector_t *vec, pbqp_matrix_t *mat, unsigned row_index)
134 {
135         unsigned index;
136         unsigned len;
137
138         assert(vec->len == mat->cols);
139         assert(row_index < mat->rows);
140
141         len = vec->len;
142
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]);
146         }
147 }
148
149 num vector_get_min(vector_t *vec)
150 {
151         unsigned index;
152         unsigned len;
153         num      min = INF_COSTS;
154
155         len = vec->len;
156         assert(len > 0);
157
158         for (index = 0; index < len; ++index) {
159                 num elem = vec->entries[index].data;
160
161                 if (elem < min) {
162                         min = elem;
163                 }
164         }
165
166         return min;
167 }
168
169 unsigned vector_get_min_index(vector_t *vec)
170 {
171         unsigned index;
172         unsigned len;
173         unsigned min_index = 0;
174         num      min       = INF_COSTS;
175
176         len = vec->len;
177         assert(len > 0);
178
179         for (index = 0; index < len; ++index) {
180                 num elem = vec->entries[index].data;
181
182                 if (elem < min) {
183                         min = elem;
184                         min_index = index;
185                 }
186         }
187
188         return min_index;
189 }