1 /* Functions to support expandable bitsets.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 /* This file implements expandable bitsets. These bitsets can be of
27 arbitrary length and are more efficient than arrays of bits for
30 Empty elements are represented by a NULL pointer in the table of
31 element pointers. An alternative is to point to a special zero
32 element. Similarly, we could represent an all 1's element with
33 another special ones element pointer.
35 Bitsets are commonly empty so we need to ensure that this special
36 case is fast. A zero bitset is indicated when cdata is 0. This is
37 conservative since cdata may be non zero and the bitset may still
40 The bitset cache can be disabled either by setting cindex to
41 BITSET_WINDEX_MAX or by setting csize to 0. Here
42 we use the former approach since cindex needs to be updated whenever
47 /* Number of words to use for each element. */
48 #define EBITSET_ELT_WORDS 2
50 /* Number of bits stored in each element. */
51 #define EBITSET_ELT_BITS \
52 ((unsigned int) (EBITSET_ELT_WORDS * BITSET_WORD_BITS))
54 /* Ebitset element. We use an array of bits. */
55 typedef struct ebitset_elt_struct
59 bitset_word words
[EBITSET_ELT_WORDS
]; /* Bits that are set. */
60 struct ebitset_elt_struct
*next
;
67 typedef ebitset_elt
*ebitset_elts
;
70 /* Number of elements to initially allocate. */
72 #ifndef EBITSET_INITIAL_SIZE
73 #define EBITSET_INITIAL_SIZE 2
77 enum ebitset_find_mode
78 { EBITSET_FIND
, EBITSET_CREATE
, EBITSET_SUBST
};
80 static ebitset_elt ebitset_zero_elts
[1]; /* Elements of all zero bits. */
82 /* Obstack to allocate bitset elements from. */
83 static struct obstack ebitset_obstack
;
84 static bool ebitset_obstack_init
= false;
85 static ebitset_elt
*ebitset_free_list
; /* Free list of bitset elements. */
87 #define EBITSET_N_ELTS(N) (((N) + EBITSET_ELT_BITS - 1) / EBITSET_ELT_BITS)
88 #define EBITSET_ELTS(BSET) ((BSET)->e.elts)
89 #define EBITSET_SIZE(BSET) EBITSET_N_ELTS (BITSET_NBITS_ (BSET))
90 #define EBITSET_ASIZE(BSET) ((BSET)->e.size)
92 #define EBITSET_NEXT(ELT) ((ELT)->u.next)
93 #define EBITSET_WORDS(ELT) ((ELT)->u.words)
95 /* Disable bitset cache and mark BSET as being zero. */
96 #define EBITSET_ZERO_SET(BSET) ((BSET)->b.cindex = BITSET_WINDEX_MAX, \
99 #define EBITSET_CACHE_DISABLE(BSET) ((BSET)->b.cindex = BITSET_WINDEX_MAX)
101 /* Disable bitset cache and mark BSET as being possibly non-zero. */
102 #define EBITSET_NONZERO_SET(BSET) \
103 (EBITSET_CACHE_DISABLE (BSET), (BSET)->b.cdata = (bitset_word *)~0)
105 /* A conservative estimate of whether the bitset is zero.
106 This is non-zero only if we know for sure that the bitset is zero. */
107 #define EBITSET_ZERO_P(BSET) ((BSET)->b.cdata == 0)
109 /* Enable cache to point to element with table index EINDEX.
110 The element must exist. */
111 #define EBITSET_CACHE_SET(BSET, EINDEX) \
112 ((BSET)->b.cindex = (EINDEX) * EBITSET_ELT_WORDS, \
113 (BSET)->b.cdata = EBITSET_WORDS (EBITSET_ELTS (BSET) [EINDEX]))
117 #define min(a, b) ((a) > (b) ? (b) : (a))
118 #define max(a, b) ((a) > (b) ? (a) : (b))
121 ebitset_resize (bitset src
, bitset_bindex n_bits
)
123 bitset_windex oldsize
;
124 bitset_windex newsize
;
126 if (n_bits
== BITSET_NBITS_ (src
))
129 oldsize
= EBITSET_SIZE (src
);
130 newsize
= EBITSET_N_ELTS (n_bits
);
132 if (oldsize
< newsize
)
136 /* The bitset needs to grow. If we already have enough memory
137 allocated, then just zero what we need. */
138 if (newsize
> EBITSET_ASIZE (src
))
140 /* We need to allocate more memory. When oldsize is
141 non-zero this means that we are changing the size, so
142 grow the bitset 25% larger than requested to reduce
143 number of reallocations. */
148 size
= newsize
+ newsize
/ 4;
151 = realloc (EBITSET_ELTS (src
), size
* sizeof (ebitset_elt
*));
152 EBITSET_ASIZE (src
) = size
;
155 memset (EBITSET_ELTS (src
) + oldsize
, 0,
156 (newsize
- oldsize
) * sizeof (ebitset_elt
*));
160 /* The bitset needs to shrink. There's no point deallocating
161 the memory unless it is shrinking by a reasonable amount. */
162 if ((oldsize
- newsize
) >= oldsize
/ 2)
165 = realloc (EBITSET_ELTS (src
), newsize
* sizeof (ebitset_elt
*));
166 EBITSET_ASIZE (src
) = newsize
;
169 /* Need to prune any excess bits. FIXME. */
172 BITSET_NBITS_ (src
) = n_bits
;
177 /* Allocate a ebitset element. The bits are not cleared. */
178 static inline ebitset_elt
*
179 ebitset_elt_alloc (void)
183 if (ebitset_free_list
!= 0)
185 elt
= ebitset_free_list
;
186 ebitset_free_list
= EBITSET_NEXT (elt
);
190 if (!ebitset_obstack_init
)
192 ebitset_obstack_init
= true;
194 /* Let particular systems override the size of a chunk. */
196 #ifndef OBSTACK_CHUNK_SIZE
197 #define OBSTACK_CHUNK_SIZE 0
200 /* Let them override the alloc and free routines too. */
202 #ifndef OBSTACK_CHUNK_ALLOC
203 #define OBSTACK_CHUNK_ALLOC xmalloc
206 #ifndef OBSTACK_CHUNK_FREE
207 #define OBSTACK_CHUNK_FREE free
210 #if ! defined __GNUC__ || __GNUC__ < 2
211 #define __alignof__(type) 0
214 obstack_specify_allocation (&ebitset_obstack
, OBSTACK_CHUNK_SIZE
,
215 __alignof__ (ebitset_elt
),
220 /* Perhaps we should add a number of new elements to the free
222 elt
= (ebitset_elt
*) obstack_alloc (&ebitset_obstack
,
223 sizeof (ebitset_elt
));
230 /* Allocate a ebitset element. The bits are cleared. */
231 static inline ebitset_elt
*
232 ebitset_elt_calloc (void)
236 elt
= ebitset_elt_alloc ();
237 memset (EBITSET_WORDS (elt
), 0, sizeof (EBITSET_WORDS (elt
)));
243 ebitset_elt_free (ebitset_elt
*elt
)
245 EBITSET_NEXT (elt
) = ebitset_free_list
;
246 ebitset_free_list
= elt
;
250 /* Remove element with index EINDEX from bitset BSET. */
252 ebitset_elt_remove (bitset bset
, bitset_windex eindex
)
257 elts
= EBITSET_ELTS (bset
);
262 ebitset_elt_free (elt
);
266 /* Add ELT into elts at index EINDEX of bitset BSET. */
268 ebitset_elt_add (bitset bset
, ebitset_elt
*elt
, bitset_windex eindex
)
272 elts
= EBITSET_ELTS (bset
);
273 /* Assume that the elts entry not allocated. */
278 /* Are all bits in an element zero? */
280 ebitset_elt_zero_p (ebitset_elt
*elt
)
284 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
285 if (EBITSET_WORDS (elt
)[i
])
293 ebitset_elt_find (bitset bset
, bitset_bindex bindex
,
294 enum ebitset_find_mode mode
)
298 bitset_windex eindex
;
301 eindex
= bindex
/ EBITSET_ELT_BITS
;
303 elts
= EBITSET_ELTS (bset
);
304 size
= EBITSET_SIZE (bset
);
308 if ((elt
= elts
[eindex
]))
310 if (EBITSET_WORDS (elt
) == bset
->b
.cdata
)
313 EBITSET_CACHE_SET (bset
, eindex
);
318 /* The element could not be found. */
330 ebitset_resize (bset
, bindex
);
332 /* Create a new element. */
333 elt
= ebitset_elt_calloc ();
334 ebitset_elt_add (bset
, elt
, eindex
);
335 EBITSET_CACHE_SET (bset
, eindex
);
339 return &ebitset_zero_elts
[0];
344 /* Weed out the zero elements from the elts. */
345 static inline bitset_windex
346 ebitset_weed (bitset bset
)
352 if (EBITSET_ZERO_P (bset
))
355 elts
= EBITSET_ELTS (bset
);
357 for (j
= 0; j
< EBITSET_SIZE (bset
); j
++)
359 ebitset_elt
*elt
= elts
[j
];
363 if (ebitset_elt_zero_p (elt
))
365 ebitset_elt_remove (bset
, j
);
376 /* All the bits are zero. We could shrink the elts.
377 For now just mark BSET as known to be zero. */
378 EBITSET_ZERO_SET (bset
);
381 EBITSET_NONZERO_SET (bset
);
387 /* Set all bits in the bitset to zero. */
389 ebitset_zero (bitset bset
)
394 if (EBITSET_ZERO_P (bset
))
397 elts
= EBITSET_ELTS (bset
);
398 for (j
= 0; j
< EBITSET_SIZE (bset
); j
++)
400 ebitset_elt
*elt
= elts
[j
];
403 ebitset_elt_remove (bset
, j
);
406 /* All the bits are zero. We could shrink the elts.
407 For now just mark BSET as known to be zero. */
408 EBITSET_ZERO_SET (bset
);
413 ebitset_equal_p (bitset dst
, bitset src
)
425 if (EBITSET_SIZE (src
) != EBITSET_SIZE (dst
))
428 selts
= EBITSET_ELTS (src
);
429 delts
= EBITSET_ELTS (dst
);
431 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
434 ebitset_elt
*selt
= selts
[j
];
435 ebitset_elt
*delt
= delts
[j
];
439 if ((selt
&& !delt
) || (!selt
&& delt
))
442 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
443 if (EBITSET_WORDS (selt
)[i
] != EBITSET_WORDS (delt
)[i
])
450 /* Copy bits from bitset SRC to bitset DST. */
452 ebitset_copy_ (bitset dst
, bitset src
)
463 if (BITSET_NBITS_ (dst
) != BITSET_NBITS_ (src
))
464 ebitset_resize (dst
, BITSET_NBITS_ (src
));
466 selts
= EBITSET_ELTS (src
);
467 delts
= EBITSET_ELTS (dst
);
468 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
470 ebitset_elt
*selt
= selts
[j
];
476 tmp
= ebitset_elt_alloc ();
478 memcpy (EBITSET_WORDS (tmp
), EBITSET_WORDS (selt
),
479 sizeof (EBITSET_WORDS (selt
)));
482 EBITSET_NONZERO_SET (dst
);
486 /* Copy bits from bitset SRC to bitset DST. Return true if
487 bitsets different. */
489 ebitset_copy_cmp (bitset dst
, bitset src
)
494 if (EBITSET_ZERO_P (dst
))
496 ebitset_copy_ (dst
, src
);
497 return !EBITSET_ZERO_P (src
);
500 if (ebitset_equal_p (dst
, src
))
503 ebitset_copy_ (dst
, src
);
508 /* Set bit BITNO in bitset DST. */
510 ebitset_set (bitset dst
, bitset_bindex bitno
)
512 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
514 ebitset_elt_find (dst
, bitno
, EBITSET_CREATE
);
516 dst
->b
.cdata
[windex
- dst
->b
.cindex
] |=
517 (bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
);
521 /* Reset bit BITNO in bitset DST. */
523 ebitset_reset (bitset dst
, bitset_bindex bitno
)
525 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
527 if (!ebitset_elt_find (dst
, bitno
, EBITSET_FIND
))
530 dst
->b
.cdata
[windex
- dst
->b
.cindex
] &=
531 ~((bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
));
533 /* If all the data is zero, perhaps we should remove it now...
534 However, there is a good chance that the element will be needed
539 /* Test bit BITNO in bitset SRC. */
541 ebitset_test (bitset src
, bitset_bindex bitno
)
543 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
545 return (ebitset_elt_find (src
, bitno
, EBITSET_FIND
)
546 && ((src
->b
.cdata
[windex
- src
->b
.cindex
]
547 >> (bitno
% BITSET_WORD_BITS
))
553 ebitset_free (bitset bset
)
556 free (EBITSET_ELTS (bset
));
560 /* Find list of up to NUM bits set in BSET starting from and including
561 *NEXT and store in array LIST. Return with actual number of bits
562 found and with *NEXT indicating where search stopped. */
564 ebitset_list_reverse (bitset bset
, bitset_bindex
*list
,
565 bitset_bindex num
, bitset_bindex
*next
)
567 bitset_bindex n_bits
;
569 bitset_bindex rbitno
;
571 bitset_bindex boffset
;
572 bitset_windex windex
;
573 bitset_windex eindex
;
574 bitset_windex woffset
;
579 if (EBITSET_ZERO_P (bset
))
582 size
= EBITSET_SIZE (bset
);
583 n_bits
= size
* EBITSET_ELT_BITS
;
586 if (rbitno
>= n_bits
)
589 elts
= EBITSET_ELTS (bset
);
591 bitno
= n_bits
- (rbitno
+ 1);
593 windex
= bitno
/ BITSET_WORD_BITS
;
594 eindex
= bitno
/ EBITSET_ELT_BITS
;
595 woffset
= windex
- eindex
* EBITSET_ELT_WORDS
;
597 /* If num is 1, we could speed things up with a binary search
598 of the word of interest. */
601 bcount
= bitno
% BITSET_WORD_BITS
;
602 boffset
= windex
* BITSET_WORD_BITS
;
612 srcp
= EBITSET_WORDS (elt
);
618 word
= srcp
[woffset
] << (BITSET_WORD_BITS
- 1 - bcount
);
620 for (; word
; bcount
--)
622 if (word
& BITSET_MSB
)
624 list
[count
++] = boffset
+ bcount
;
627 *next
= n_bits
- (boffset
+ bcount
);
633 boffset
-= BITSET_WORD_BITS
;
634 bcount
= BITSET_WORD_BITS
- 1;
639 woffset
= EBITSET_ELT_WORDS
- 1;
640 boffset
= eindex
* EBITSET_ELT_BITS
- BITSET_WORD_BITS
;
644 *next
= n_bits
- (boffset
+ 1);
649 /* Find list of up to NUM bits set in BSET starting from and including
650 *NEXT and store in array LIST. Return with actual number of bits
651 found and with *NEXT indicating where search stopped. */
653 ebitset_list (bitset bset
, bitset_bindex
*list
,
654 bitset_bindex num
, bitset_bindex
*next
)
657 bitset_windex windex
;
658 bitset_windex eindex
;
665 if (EBITSET_ZERO_P (bset
))
671 elts
= EBITSET_ELTS (bset
);
672 size
= EBITSET_SIZE (bset
);
673 eindex
= bitno
/ EBITSET_ELT_BITS
;
675 if (bitno
% EBITSET_ELT_BITS
)
677 /* We need to start within an element. This is not very common. */
682 bitset_windex woffset
;
683 bitset_word
*srcp
= EBITSET_WORDS (elt
);
685 windex
= bitno
/ BITSET_WORD_BITS
;
686 woffset
= eindex
* EBITSET_ELT_WORDS
;
688 for (; (windex
- woffset
) < EBITSET_ELT_WORDS
; windex
++)
690 word
= srcp
[windex
- woffset
] >> (bitno
% BITSET_WORD_BITS
);
692 for (; word
; bitno
++)
696 list
[count
++] = bitno
;
705 bitno
= (windex
+ 1) * BITSET_WORD_BITS
;
709 /* Skip to next element. */
713 /* If num is 1, we could speed things up with a binary search
714 of the word of interest. */
716 for (; eindex
< size
; eindex
++)
725 srcp
= EBITSET_WORDS (elt
);
726 windex
= eindex
* EBITSET_ELT_WORDS
;
728 if ((count
+ EBITSET_ELT_BITS
) < num
)
730 /* The coast is clear, plant boot! */
732 #if EBITSET_ELT_WORDS == 2
736 if (!(word
& 0xffff))
746 for (; word
; bitno
++)
749 list
[count
++] = bitno
;
754 bitno
= windex
* BITSET_WORD_BITS
;
759 if (!(word
& 0xffff))
764 for (; word
; bitno
++)
767 list
[count
++] = bitno
;
772 bitno
= windex
* BITSET_WORD_BITS
;
774 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, windex
++)
776 bitno
= windex
* BITSET_WORD_BITS
;
781 if (!(word
& 0xffff))
791 for (; word
; bitno
++)
794 list
[count
++] = bitno
;
803 /* Tread more carefully since we need to check
804 if array overflows. */
806 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, windex
++)
808 bitno
= windex
* BITSET_WORD_BITS
;
810 for (word
= srcp
[i
]; word
; bitno
++)
814 list
[count
++] = bitno
;
832 /* Ensure that any unused bits within the last element are clear. */
834 ebitset_unused_clear (bitset dst
)
836 unsigned int last_bit
;
837 bitset_bindex n_bits
;
839 n_bits
= BITSET_NBITS_ (dst
);
840 last_bit
= n_bits
% EBITSET_ELT_BITS
;
844 bitset_windex eindex
;
848 elts
= EBITSET_ELTS (dst
);
850 eindex
= n_bits
/ EBITSET_ELT_BITS
;
855 bitset_windex windex
;
856 bitset_windex woffset
;
857 bitset_word
*srcp
= EBITSET_WORDS (elt
);
859 windex
= n_bits
/ BITSET_WORD_BITS
;
860 woffset
= eindex
* EBITSET_ELT_WORDS
;
862 srcp
[windex
- woffset
] &= ((bitset_word
) 1 << last_bit
) - 1;
864 for (; (windex
- woffset
) < EBITSET_ELT_WORDS
; windex
++)
865 srcp
[windex
- woffset
] = 0;
872 ebitset_ones (bitset dst
)
877 for (j
= 0; j
< EBITSET_SIZE (dst
); j
++)
879 /* Create new elements if they cannot be found. Perhaps
880 we should just add pointers to a ones element? */
882 ebitset_elt_find (dst
, j
* EBITSET_ELT_BITS
, EBITSET_CREATE
);
883 memset (EBITSET_WORDS (elt
), -1, sizeof (EBITSET_WORDS (elt
)));
885 EBITSET_NONZERO_SET (dst
);
886 ebitset_unused_clear (dst
);
891 ebitset_empty_p (bitset dst
)
896 if (EBITSET_ZERO_P (dst
))
899 elts
= EBITSET_ELTS (dst
);
900 for (j
= 0; j
< EBITSET_SIZE (dst
); j
++)
902 ebitset_elt
*elt
= elts
[j
];
906 if (!ebitset_elt_zero_p (elt
))
908 /* Do some weeding as we go. */
909 ebitset_elt_remove (dst
, j
);
913 /* All the bits are zero. We could shrink the elts.
914 For now just mark DST as known to be zero. */
915 EBITSET_ZERO_SET (dst
);
921 ebitset_not (bitset dst
, bitset src
)
928 ebitset_resize (dst
, BITSET_NBITS_ (src
));
930 for (j
= 0; j
< EBITSET_SIZE (src
); j
++)
932 /* Create new elements for dst if they cannot be found
933 or substitute zero elements if src elements not found. */
935 ebitset_elt_find (dst
, j
* EBITSET_ELT_BITS
, EBITSET_SUBST
);
937 ebitset_elt_find (dst
, j
* EBITSET_ELT_BITS
, EBITSET_CREATE
);
939 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
940 EBITSET_WORDS (delt
)[i
] = ~EBITSET_WORDS (selt
)[i
];
942 EBITSET_NONZERO_SET (dst
);
943 ebitset_unused_clear (dst
);
947 /* Is DST == DST | SRC? */
949 ebitset_subset_p (bitset dst
, bitset src
)
957 selts
= EBITSET_ELTS (src
);
958 delts
= EBITSET_ELTS (dst
);
960 ssize
= EBITSET_SIZE (src
);
961 dsize
= EBITSET_SIZE (dst
);
963 for (j
= 0; j
< ssize
; j
++)
969 selt
= j
< ssize
? selts
[j
] : 0;
970 delt
= j
< dsize
? delts
[j
] : 0;
976 selt
= &ebitset_zero_elts
[0];
978 delt
= &ebitset_zero_elts
[0];
980 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
981 if (EBITSET_WORDS (delt
)[i
]
982 != (EBITSET_WORDS (selt
)[i
] | EBITSET_WORDS (delt
)[i
]))
989 /* Is DST & SRC == 0? */
991 ebitset_disjoint_p (bitset dst
, bitset src
)
999 selts
= EBITSET_ELTS (src
);
1000 delts
= EBITSET_ELTS (dst
);
1002 ssize
= EBITSET_SIZE (src
);
1003 dsize
= EBITSET_SIZE (dst
);
1005 for (j
= 0; j
< ssize
; j
++)
1011 selt
= j
< ssize
? selts
[j
] : 0;
1012 delt
= j
< dsize
? delts
[j
] : 0;
1017 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++)
1018 if ((EBITSET_WORDS (selt
)[i
] & EBITSET_WORDS (delt
)[i
]))
1027 ebitset_op3_cmp (bitset dst
, bitset src1
, bitset src2
, enum bitset_ops op
)
1029 bitset_windex ssize1
;
1030 bitset_windex ssize2
;
1031 bitset_windex dsize
;
1033 ebitset_elts
*selts1
;
1034 ebitset_elts
*selts2
;
1035 ebitset_elts
*delts
;
1039 bool changed
= false;
1043 ebitset_resize (dst
, max (BITSET_NBITS_ (src1
), BITSET_NBITS_ (src2
)));
1045 ssize1
= EBITSET_SIZE (src1
);
1046 ssize2
= EBITSET_SIZE (src2
);
1047 dsize
= EBITSET_SIZE (dst
);
1052 selts1
= EBITSET_ELTS (src1
);
1053 selts2
= EBITSET_ELTS (src2
);
1054 delts
= EBITSET_ELTS (dst
);
1056 for (j
= 0; j
< size
; j
++)
1062 selt1
= j
< ssize1
? selts1
[j
] : 0;
1063 selt2
= j
< ssize2
? selts2
[j
] : 0;
1064 delt
= j
< dsize
? delts
[j
] : 0;
1066 if (!selt1
&& !selt2
)
1071 ebitset_elt_remove (dst
, j
);
1077 selt1
= &ebitset_zero_elts
[0];
1079 selt2
= &ebitset_zero_elts
[0];
1081 delt
= ebitset_elt_calloc ();
1085 srcp1
= EBITSET_WORDS (selt1
);
1086 srcp2
= EBITSET_WORDS (selt2
);
1087 dstp
= EBITSET_WORDS (delt
);
1094 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1096 bitset_word tmp
= *srcp1
++ | *srcp2
++;
1107 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1109 bitset_word tmp
= *srcp1
++ & *srcp2
++;
1120 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1122 bitset_word tmp
= *srcp1
++ ^ *srcp2
++;
1132 case BITSET_OP_ANDN
:
1133 for (i
= 0; i
< EBITSET_ELT_WORDS
; i
++, dstp
++)
1135 bitset_word tmp
= *srcp1
++ & ~(*srcp2
++);
1146 if (!ebitset_elt_zero_p (delt
))
1148 ebitset_elt_add (dst
, delt
, j
);
1152 ebitset_elt_free (delt
);
1156 /* If we have elements of DST left over, free them all. */
1157 for (; j
< dsize
; j
++)
1166 ebitset_elt_remove (dst
, j
);
1169 EBITSET_NONZERO_SET (dst
);
1175 ebitset_and_cmp (bitset dst
, bitset src1
, bitset src2
)
1179 if (EBITSET_ZERO_P (src2
))
1182 changed
= EBITSET_ZERO_P (dst
);
1186 else if (EBITSET_ZERO_P (src1
))
1189 changed
= EBITSET_ZERO_P (dst
);
1193 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_AND
);
1198 ebitset_and (bitset dst
, bitset src1
, bitset src2
)
1200 ebitset_and_cmp (dst
, src1
, src2
);
1205 ebitset_andn_cmp (bitset dst
, bitset src1
, bitset src2
)
1209 if (EBITSET_ZERO_P (src2
))
1211 return ebitset_copy_cmp (dst
, src1
);
1213 else if (EBITSET_ZERO_P (src1
))
1216 changed
= EBITSET_ZERO_P (dst
);
1220 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_ANDN
);
1225 ebitset_andn (bitset dst
, bitset src1
, bitset src2
)
1227 ebitset_andn_cmp (dst
, src1
, src2
);
1232 ebitset_or_cmp (bitset dst
, bitset src1
, bitset src2
)
1234 if (EBITSET_ZERO_P (src2
))
1236 return ebitset_copy_cmp (dst
, src1
);
1238 else if (EBITSET_ZERO_P (src1
))
1240 return ebitset_copy_cmp (dst
, src2
);
1242 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_OR
);
1247 ebitset_or (bitset dst
, bitset src1
, bitset src2
)
1249 ebitset_or_cmp (dst
, src1
, src2
);
1254 ebitset_xor_cmp (bitset dst
, bitset src1
, bitset src2
)
1256 if (EBITSET_ZERO_P (src2
))
1258 return ebitset_copy_cmp (dst
, src1
);
1260 else if (EBITSET_ZERO_P (src1
))
1262 return ebitset_copy_cmp (dst
, src2
);
1264 return ebitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_XOR
);
1269 ebitset_xor (bitset dst
, bitset src1
, bitset src2
)
1271 ebitset_xor_cmp (dst
, src1
, src2
);
1276 ebitset_copy (bitset dst
, bitset src
)
1278 if (BITSET_COMPATIBLE_ (dst
, src
))
1279 ebitset_copy_ (dst
, src
);
1281 bitset_copy_ (dst
, src
);
1285 /* Vector of operations for linked-list bitsets. */
1286 struct bitset_vtable ebitset_vtable
= {
1313 bitset_andn_or_cmp_
,
1317 ebitset_list_reverse
,
1323 /* Return size of initial structure. */
1325 ebitset_bytes (bitset_bindex n_bits ATTRIBUTE_UNUSED
)
1327 return sizeof (struct ebitset_struct
);
1331 /* Initialize a bitset. */
1334 ebitset_init (bitset bset
, bitset_bindex n_bits
)
1338 bset
->b
.vtable
= &ebitset_vtable
;
1340 bset
->b
.csize
= EBITSET_ELT_WORDS
;
1342 EBITSET_ZERO_SET (bset
);
1344 size
= n_bits
? (n_bits
+ EBITSET_ELT_BITS
- 1) / EBITSET_ELT_BITS
1345 : EBITSET_INITIAL_SIZE
;
1347 EBITSET_ASIZE (bset
) = 0;
1348 EBITSET_ELTS (bset
) = 0;
1349 ebitset_resize (bset
, n_bits
);
1356 ebitset_release_memory (void)
1358 ebitset_free_list
= 0;
1359 if (ebitset_obstack_init
)
1361 ebitset_obstack_init
= false;
1362 obstack_free (&ebitset_obstack
, NULL
);