1 /* toolbox/bitvector.c - bitvector implementation
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Christian Ullrich
33 #include "mm/memory.h"
34 #include "toolbox/bitvector.h"
37 /******************************************************************************
39 Bitvector Implementation
42 ******************************************************************************/
45 /* Number of ints needed for size bits */
46 # define BV_NUM_INTS(size) (((((size) + 7)/ 8) + sizeof(int) - 1) / sizeof(int) + 1)
47 /* Get index in bitvector */
48 # define BV_INT_INDEX(bit) ( ((bit) / 8) / sizeof(int) + 1)
49 /* Get bit index inside int */
50 # define BV_BIT_INDEX(bit, index) ( (bit) - (index - 1) * sizeof(int) * 8 );
52 /* Number of ints needed for size bits */
53 # define BV_NUM_INTS(size) (((((size) + 7)/ 8) + sizeof(int) - 1) / sizeof(int))
54 /* Get index in bitvector */
55 # define BV_INT_INDEX(bit) ( ((bit) / 8) / sizeof(int) )
56 /* Get bit index inside int */
57 # define BV_BIT_INDEX(bit, index) ( (bit) - (index) * sizeof(int) * 8 );
61 char *bv_to_string(bitvector bv, char *string, int size) {
64 _BV_ASSERT(bv[0] == size);
66 for(i = 0; i < size; i++)
67 if (bv_get_bit(bv, i))
76 int *bv_new(int size) {
80 /* Number of ints needed for size bits */
81 /* n = (((size+7)/8) + sizeof(int) - 1)/sizeof(int); */
82 n = BV_NUM_INTS(size);
86 for(i = 0; i < n; i++) bv[i] = 0;
95 bool bv_get_bit(bitvector bv, int bit) {
100 i = BV_INT_INDEX(bit);
101 n = BV_BIT_INDEX(bit, i);
103 _BV_ASSERT(i < (BV_NUM_INTS(bv[0])));
104 return (bv[i] & (1<<n));
107 void bv_set_bit(bitvector bv, int bit) {
110 _BV_ASSERT(bit >= 0);
112 i = BV_INT_INDEX(bit);
113 n = BV_BIT_INDEX(bit, i);
115 _BV_ASSERT(i < BV_NUM_INTS(bv[0]));
120 void bv_reset_bit(bitvector bv, int bit) {
123 _BV_ASSERT(bit >= 0);
125 i = BV_INT_INDEX(bit);
126 n = BV_BIT_INDEX(bit, i);
128 _BV_ASSERT(i < BV_NUM_INTS(bv[0]));
133 void bv_reset(bitvector bv, int size) {
136 _BV_ASSERT(bv[0] == size);
138 n = BV_NUM_INTS(size);
140 #ifdef BV_DEBUG_CHECK
141 for(i = 1; i < n; i++)
143 for(i = 0; i < n; i++)
148 bool bv_is_empty(bitvector bv, int size) {
152 _BV_ASSERT(bv[0] == size);
154 n = BV_NUM_INTS(size);
157 #ifdef BV_DEBUG_CHECK
158 for(i = 1; (i < n) && empty; i++)
160 for(i = 0; (i < n) && empty; i++)
162 empty = empty && (bv[i] == 0);
166 void bv_copy(bitvector dst, bitvector src, int size) {
168 /* copy the whole bitvector */
169 _BV_ASSERT(dst[0] == size);
170 _BV_ASSERT(src[0] == size);
171 n = BV_NUM_INTS(size);
173 #ifdef BV_DEBUG_CHECK
174 for(i = 1; i < n; i++)
176 for(i = 0; i < n; i++)
181 bool bv_equal(bitvector s1, bitvector s2, int size) {
185 /* copy the whole bitvector */
186 _BV_ASSERT(s1[0] == size);
187 _BV_ASSERT(s2[0] == size);
192 n = BV_NUM_INTS(size);
194 #ifdef BV_DEBUG_CHECK
195 for(i = 1; equal && (i < n-1); i++)
197 for(i = 0; equal && (i < n-1); i++)
199 equal = (s1[i] == s2[i]);
201 /* Last compare maybe has to be masked! */
203 i = BV_INT_INDEX(size - 1);
204 n = BV_BIT_INDEX(size - 1, i);
206 _BV_ASSERT(i < BV_NUM_INTS(s1[0]));
207 _BV_ASSERT(i < BV_NUM_INTS(s2[0]));
209 if (n == (sizeof(int) * 8 - 1)) {
213 mask = (1<<(n+1)) - 1;
216 equal = equal && ( (s1[i]&mask) == (s2[i]&mask));
221 void bv_minus(bitvector d, bitvector s1, bitvector s2, int size) {
224 _BV_ASSERT(d[0] == size);
225 _BV_ASSERT(s1[0] == size);
226 _BV_ASSERT(s2[0] == size);
227 n = BV_NUM_INTS(size);
228 #ifdef BV_DEBUG_CHECK
229 for(i = 1; i < n; i++)
231 for(i = 0; i < n; i++)
233 d[i] = s1[i] & (~s2[i]);
236 void bv_union(bitvector d, bitvector s1, bitvector s2, int size) {
238 /* d = s1 union s2 */
239 _BV_ASSERT(d[0] == size);
240 _BV_ASSERT(s1[0] == size);
241 _BV_ASSERT(s2[0] == size);
242 n = BV_NUM_INTS(size);
243 #ifdef BV_DEBUG_CHECK
244 for(i = 1; i < n; i++)
246 for(i = 0; i < n; i++)
248 d[i] = s1[i] | s2[i];
252 * These are local overrides for various environment variables in Emacs.
253 * Please do not remove this and leave it at the end of the file, where
254 * Emacs will automagically detect them.
255 * ---------------------------------------------------------------------
258 * indent-tabs-mode: t