1 /*-----------------------------------------------------------------
2 SDCCbitv.c - contains support routines for bitvectors
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
27 #include "SDCCglobl.h"
29 int bitVectDefault = 1024;
31 /* genernal note about a bitvectors:
32 bit vectors are stored from left to right i.e.
33 bit position 0 is the MS bit of the first byte
34 this also means that bit positions must start from 0*/
35 /*-----------------------------------------------------------------*/
36 /* newBitVect - returns a new bitvector of size */
37 /*-----------------------------------------------------------------*/
38 bitVect *newBitVect (int size)
43 ALLOC(bvp,sizeof (bitVect));
46 bvp->bSize = byteSize = ( size / 8) + 1 ;
47 ALLOC(bvp->vect,byteSize);
51 /*-----------------------------------------------------------------*/
52 /* bitVectResize - changes the size of a bit vector */
53 /*-----------------------------------------------------------------*/
54 bitVect *bitVectResize (bitVect *bvp, int size)
56 int bSize = ( size / 8) + 1 ;
59 return newBitVect (size);
61 /* if we already have enough space */
62 if (bvp->bSize >= bSize ) {
70 bvp->vect = GC_realloc(bvp->vect, bSize);
74 /*-----------------------------------------------------------------*/
75 /* bitVectSetBit - sets a given bit in the bit vector */
76 /*-----------------------------------------------------------------*/
77 bitVect *bitVectSetBit (bitVect *bvp, int pos)
82 /* if set is null then allocate it */
84 bvp = newBitVect(bitVectDefault) ; /* allocate for twice the size */
86 if (bvp->size <= pos )
87 bvp = bitVectResize (bvp,pos+2); /* conservatively resize */
91 bvp->vect[byteSize] |= (((unsigned char)1) <<
96 /*-----------------------------------------------------------------*/
97 /* bitVectUnSetBit - unsets the value of a bit in a bitvector */
98 /*-----------------------------------------------------------------*/
99 void bitVectUnSetBit (bitVect *bvp, int pos)
108 if (bvp->bSize <= byteSize)
113 bvp->vect[byteSize] &= ~ (((unsigned char)1) <<
117 /*-----------------------------------------------------------------*/
118 /* bitVectBitValue - returns value value at bit position */
119 /*-----------------------------------------------------------------*/
120 int bitVectBitValue (bitVect *bvp, int pos)
130 if ( bvp->bSize <= byteSize )
135 return ((bvp->vect[byteSize] >> (7-offset)) & ((unsigned char)1) );
139 /*-----------------------------------------------------------------*/
140 /* bitVectUnion - unions two bitvectors */
141 /*-----------------------------------------------------------------*/
142 bitVect *bitVectUnion ( bitVect *bvp1, bitVect *bvp2)
151 /* if one of them null then return the other */
153 return bitVectCopy (bvp2);
155 if ( bvp1 && ! bvp2 )
156 return bitVectCopy (bvp1);
158 /* if they are not the same size */
159 /* make them the same size */
160 if (bvp1->bSize < bvp2->bSize)
161 bvp1 = bitVectResize (bvp1,bvp2->size);
163 if (bvp2->bSize < bvp1->bSize)
164 bvp2 = bitVectResize (bvp2,bvp1->size);
166 newBvp = newBitVect(bvp1->size);
168 for ( i = 0 ; i < bvp1->bSize ;i++)
169 newBvp->vect[i] = bvp1->vect[i] | bvp2->vect[i];
175 /*-----------------------------------------------------------------*/
176 /* bitVectIntersect - intersect two bitvectors */
177 /*-----------------------------------------------------------------*/
178 bitVect *bitVectIntersect ( bitVect *bvp1, bitVect *bvp2)
183 if (! bvp2 || ! bvp1 )
186 /* if they are not the same size */
187 /* make them the same size */
188 if (bvp1->bSize < bvp2->bSize)
189 bvp1 = bitVectResize (bvp1,bvp2->bSize);
191 if (bvp2->size < bvp1->size)
192 bvp2 = bitVectResize (bvp2,bvp1->size);
194 newBvp = newBitVect(bvp1->size);
196 for ( i = 0 ; i < bvp1->bSize ;i++)
197 newBvp->vect[i] = bvp1->vect[i] & bvp2->vect[i];
202 /*-----------------------------------------------------------------*/
203 /* bitVectBitsInCommon - special case of intersection determines */
204 /* if the vectors have any common bits set */
205 /*-----------------------------------------------------------------*/
206 int bitVectBitsInCommon ( bitVect *bvp1, bitVect *bvp2 )
210 if ( ! bvp1 || ! bvp2 )
213 for ( i = 0 ; i < min(bvp1->bSize,bvp2->bSize) ; i ++ )
214 if ( bvp1->vect[i] & bvp2->vect[i] )
220 /*-----------------------------------------------------------------*/
221 /* bitVectCplAnd - complement the second & and it with the first */
222 /*-----------------------------------------------------------------*/
223 bitVect *bitVectCplAnd ( bitVect *bvp1, bitVect *bvp2)
233 /* if they are not the same size */
234 /* make them the same size */
235 if (bvp1->bSize < bvp2->bSize)
236 bvp1 = bitVectResize (bvp1,bvp2->bSize);
238 if (bvp2->size < bvp1->size)
239 bvp2 = bitVectResize (bvp2,bvp1->size);
241 for ( i = 0 ; i < bvp1->bSize ;i++)
242 bvp1->vect[i] = bvp1->vect[i] & (~ bvp2->vect[i]);
247 /*-----------------------------------------------------------------*/
248 /* bitVectIsZero - bit vector has all bits turned off */
249 /*-----------------------------------------------------------------*/
250 int bitVectIsZero (bitVect *bvp)
257 for ( i = 0 ; i < bvp->bSize ; i++ )
258 if (bvp->vect[i] != 0)
264 /*-----------------------------------------------------------------*/
265 /* bitVectsEqual - returns 1 if the two bit vectors are equal */
266 /*-----------------------------------------------------------------*/
267 int bitVectEqual ( bitVect *bvp1, bitVect *bvp2)
277 if (bvp1->bSize != bvp2->bSize)
280 for (i = 0 ; i < bvp1->bSize ; i++ )
281 if ( bvp1->vect[i] != bvp2->vect[i] )
287 /*-----------------------------------------------------------------*/
288 /* bitVectCopy - creates a bitvector from another bit Vector */
289 /*-----------------------------------------------------------------*/
290 bitVect *bitVectCopy (bitVect *bvp)
298 newBvp = newBitVect(bvp->size);
299 for ( i = 0 ; i < bvp->bSize; i++ )
300 newBvp->vect[i] = bvp->vect[i];
305 /*-----------------------------------------------------------------*/
306 /* bitVectnBitsOn - returns the number of bits that are on */
307 /*-----------------------------------------------------------------*/
308 int bitVectnBitsOn (bitVect *bvp)
317 /* rip through most of the data in byte sized chunks */
319 for (i = 0 ; i < j; i++) {
321 for (k=0; k<8; k++) { count += byte&1; byte = byte>>1; }
324 /* finish up the last fractional byte, if any */
325 for (i = j*8 ; i < bvp->size; i++)
326 count += bitVectBitValue(bvp,i);
332 /*-----------------------------------------------------------------*/
333 /* bitVectFirstBit - returns the key for the first bit that is on */
334 /*-----------------------------------------------------------------*/
335 int bitVectFirstBit (bitVect *bvp)
341 for (i = 0; i < bvp->size ; i++ )
342 if (bitVectBitValue(bvp,i))
348 /*-----------------------------------------------------------------*/
349 /* bitVectDebugOn - prints bits that are on */
350 /*-----------------------------------------------------------------*/
351 void bitVectDebugOn (bitVect *bvp, FILE *of)
360 fprintf(of,"bitvector Size = %d bSize = %d\n",bvp->size,bvp->bSize);
361 fprintf(of,"Bits on { ");
362 for (i = 0 ; i < bvp->size ; i++) {
363 if (bitVectBitValue(bvp,i))
364 fprintf(of,"(%d) ",i);