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)
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"
27 /* 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
];
This page took 0.032604 seconds and 4 git commands to generate.