1 /* CGEN generic opcode support.
4 Free Software Foundation, Inc.
6 This file is part of the GNU Binutils and GDB, the GNU debugger.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22 /* Functions for manipulating CGEN_BITSET. */
24 #include "libiberty.h"
25 #include "opcode/cgen-bitset.h"
28 /* Create a bit mask. */
30 cgen_bitset_create (unsigned bit_count
)
32 CGEN_BITSET
* mask
= xmalloc (sizeof (* mask
));
33 cgen_bitset_init (mask
, bit_count
);
37 /* Initialize an existing bit mask. */
40 cgen_bitset_init (CGEN_BITSET
* mask
, unsigned bit_count
)
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. */
52 cgen_bitset_clear (CGEN_BITSET
* mask
)
59 for (i
= 0; i
< mask
->length
; ++i
)
63 /* Add a bit to a bit mask. */
66 cgen_bitset_add (CGEN_BITSET
* mask
, unsigned bit_num
)
73 byte_ix
= bit_num
/ 8;
75 bit_mask
= 1 << (7 - bit_ix
);
76 mask
->bits
[byte_ix
] |= bit_mask
;
82 cgen_bitset_set (CGEN_BITSET
* mask
, unsigned bit_num
)
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 */
94 cgen_bitset_contains (CGEN_BITSET
* mask
, unsigned bit_num
)
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
)
116 if (! mask1
|| ! mask2
)
118 if (mask1
->length
!= mask2
->length
)
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
)
133 if (! mask1
|| ! mask2
)
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
]))
144 /* Make a copy of a bit mask. */
147 cgen_bitset_copy (CGEN_BITSET
* mask
)
149 CGEN_BITSET
* newmask
;
153 newmask
= cgen_bitset_create ((mask
->length
* 8) - 1);
154 memcpy (newmask
->bits
, mask
->bits
, mask
->length
);
158 /* Combine two bit masks. */
161 cgen_bitset_union (CGEN_BITSET
* mask1
, CGEN_BITSET
* mask2
,
162 CGEN_BITSET
* result
)
166 if (! mask1
|| ! mask2
|| ! result
167 || mask1
->length
!= mask2
->length
168 || mask1
->length
!= result
->length
)
171 for (i
= 0; i
< result
->length
; ++i
)
172 result
->bits
[i
] = mask1
->bits
[i
] | mask2
->bits
[i
];