remove maxq-coff port
[binutils.git] / opcodes / cgen-bitset.c
blob34d31d71c19fc8d27bf341b4a0c64f5096eb8d52
1 /* CGEN generic opcode support.
2 Copyright 2002, 2005, 2007, 2009
3 Free Software Foundation, Inc.
5 This file is part of libopcodes.
7 This library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 It is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
21 /* Functions for manipulating CGEN_BITSET. */
23 #include "libiberty.h"
24 #include "cgen/bitset.h"
25 #include <string.h>
27 /* Create a bit mask. */
29 CGEN_BITSET *
30 cgen_bitset_create (unsigned bit_count)
32 CGEN_BITSET * mask = xmalloc (sizeof (* mask));
33 cgen_bitset_init (mask, bit_count);
34 return mask;
37 /* Initialize an existing bit mask. */
39 void
40 cgen_bitset_init (CGEN_BITSET * mask, unsigned bit_count)
42 if (! mask)
43 return;
44 mask->length = (bit_count / 8) + 1;
45 mask->bits = xmalloc (mask->length);
46 cgen_bitset_clear (mask);
49 /* Clear the bits of a bit mask. */
51 void
52 cgen_bitset_clear (CGEN_BITSET * mask)
54 unsigned i;
56 if (! mask)
57 return;
59 for (i = 0; i < mask->length; ++i)
60 mask->bits[i] = 0;
63 /* Add a bit to a bit mask. */
65 void
66 cgen_bitset_add (CGEN_BITSET * mask, unsigned bit_num)
68 int byte_ix, bit_ix;
69 int bit_mask;
71 if (! mask)
72 return;
73 byte_ix = bit_num / 8;
74 bit_ix = bit_num % 8;
75 bit_mask = 1 << (7 - bit_ix);
76 mask->bits[byte_ix] |= bit_mask;
79 /* Set a bit mask. */
81 void
82 cgen_bitset_set (CGEN_BITSET * mask, unsigned bit_num)
84 if (! mask)
85 return;
86 cgen_bitset_clear (mask);
87 cgen_bitset_add (mask, bit_num);
90 /* Test for a bit in a bit mask.
91 Returns 1 if the bit is found */
93 int
94 cgen_bitset_contains (CGEN_BITSET * mask, unsigned bit_num)
96 int byte_ix, bit_ix;
97 int bit_mask;
99 if (! mask)
100 return 1; /* No bit restrictions. */
102 byte_ix = bit_num / 8;
103 bit_ix = 7 - (bit_num % 8);
104 bit_mask = 1 << bit_ix;
105 return (mask->bits[byte_ix] & bit_mask) >> bit_ix;
108 /* Compare two bit masks for equality.
109 Returns 0 if they are equal. */
112 cgen_bitset_compare (CGEN_BITSET * mask1, CGEN_BITSET * mask2)
114 if (mask1 == mask2)
115 return 0;
116 if (! mask1 || ! mask2)
117 return 1;
118 if (mask1->length != mask2->length)
119 return 1;
120 return memcmp (mask1->bits, mask2->bits, mask1->length);
123 /* Test two bit masks for common bits.
124 Returns 1 if a common bit is found. */
127 cgen_bitset_intersect_p (CGEN_BITSET * mask1, CGEN_BITSET * mask2)
129 unsigned i, limit;
131 if (mask1 == mask2)
132 return 1;
133 if (! mask1 || ! mask2)
134 return 0;
135 limit = mask1->length < mask2->length ? mask1->length : mask2->length;
137 for (i = 0; i < limit; ++i)
138 if ((mask1->bits[i] & mask2->bits[i]))
139 return 1;
141 return 0;
144 /* Make a copy of a bit mask. */
146 CGEN_BITSET *
147 cgen_bitset_copy (CGEN_BITSET * mask)
149 CGEN_BITSET* newmask;
151 if (! mask)
152 return NULL;
153 newmask = cgen_bitset_create ((mask->length * 8) - 1);
154 memcpy (newmask->bits, mask->bits, mask->length);
155 return newmask;
158 /* Combine two bit masks. */
160 void
161 cgen_bitset_union (CGEN_BITSET * mask1, CGEN_BITSET * mask2,
162 CGEN_BITSET * result)
164 unsigned i;
166 if (! mask1 || ! mask2 || ! result
167 || mask1->length != mask2->length
168 || mask1->length != result->length)
169 return;
171 for (i = 0; i < result->length; ++i)
172 result->bits[i] = mask1->bits[i] | mask2->bits[i];