1 /* CGEN generic opcode support.
2 Copyright (C) 2002-2016 Free Software Foundation, Inc.
4 This file is part of libopcodes.
6 This library is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 It is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
20 /* Functions for manipulating CGEN_BITSET. */
22 #include "libiberty.h"
23 #include "cgen/bitset.h"
26 /* Create a bit mask. */
29 cgen_bitset_create (unsigned bit_count)
31 CGEN_BITSET * mask = xmalloc (sizeof (* mask));
32 cgen_bitset_init (mask, bit_count);
36 /* Initialize an existing bit mask. */
39 cgen_bitset_init (CGEN_BITSET * mask, unsigned bit_count)
43 mask->length = (bit_count / 8) + 1;
44 mask->bits = xmalloc (mask->length);
45 cgen_bitset_clear (mask);
48 /* Clear the bits of a bit mask. */
51 cgen_bitset_clear (CGEN_BITSET * mask)
58 for (i = 0; i < mask->length; ++i)
62 /* Add a bit to a bit mask. */
65 cgen_bitset_add (CGEN_BITSET * mask, unsigned bit_num)
72 byte_ix = bit_num / 8;
74 bit_mask = 1 << (7 - bit_ix);
75 mask->bits[byte_ix] |= bit_mask;
81 cgen_bitset_set (CGEN_BITSET * mask, unsigned bit_num)
85 cgen_bitset_clear (mask);
86 cgen_bitset_add (mask, bit_num);
89 /* Test for a bit in a bit mask.
90 Returns 1 if the bit is found */
93 cgen_bitset_contains (CGEN_BITSET * mask, unsigned bit_num)
99 return 1; /* No bit restrictions. */
101 byte_ix = bit_num / 8;
102 bit_ix = 7 - (bit_num % 8);
103 bit_mask = 1 << bit_ix;
104 return (mask->bits[byte_ix] & bit_mask) >> bit_ix;
107 /* Compare two bit masks for equality.
108 Returns 0 if they are equal. */
111 cgen_bitset_compare (CGEN_BITSET * mask1, CGEN_BITSET * mask2)
115 if (! mask1 || ! mask2)
117 if (mask1->length != mask2->length)
119 return memcmp (mask1->bits, mask2->bits, mask1->length);
122 /* Test two bit masks for common bits.
123 Returns 1 if a common bit is found. */
126 cgen_bitset_intersect_p (CGEN_BITSET * mask1, CGEN_BITSET * mask2)
132 if (! mask1 || ! mask2)
134 limit = mask1->length < mask2->length ? mask1->length : mask2->length;
136 for (i = 0; i < limit; ++i)
137 if ((mask1->bits[i] & mask2->bits[i]))
143 /* Make a copy of a bit mask. */
146 cgen_bitset_copy (CGEN_BITSET * mask)
148 CGEN_BITSET* newmask;
152 newmask = cgen_bitset_create ((mask->length * 8) - 1);
153 memcpy (newmask->bits, mask->bits, mask->length);
157 /* Combine two bit masks. */
160 cgen_bitset_union (CGEN_BITSET * mask1, CGEN_BITSET * mask2,
161 CGEN_BITSET * result)
165 if (! mask1 || ! mask2 || ! result
166 || mask1->length != mask2->length
167 || mask1->length != result->length)
170 for (i = 0; i < result->length; ++i)
171 result->bits[i] = mask1->bits[i] | mask2->bits[i];