1 /* Functions to support link list bitsets.
2 Copyright (C) 2002, 2003 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 2 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, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 /* This file implements linked-list bitsets. These bitsets can be of
32 arbitrary length and are more efficient than arrays of bits for
35 Usually if all the bits in an element are zero we remove the element
36 from the list. However, a side effect of the bit caching is that we
37 do not always notice when an element becomes zero. Hence the
38 lbitset_weed function which removes zero elements. */
41 /* Number of words to use for each element. The larger the value the
42 greater the size of the cache and the shorter the time to find a given bit
43 but the more memory wasted for sparse bitsets and the longer the time
44 to search for set bits.
46 The routines that dominate timing profiles are lbitset_elt_find
47 and lbitset_elt_link, especially when accessing the bits randomly. */
49 #define LBITSET_ELT_WORDS 2
51 typedef bitset_word lbitset_word
;
53 #define LBITSET_WORD_BITS BITSET_WORD_BITS
55 /* Number of bits stored in each element. */
56 #define LBITSET_ELT_BITS \
57 ((unsigned) (LBITSET_ELT_WORDS * LBITSET_WORD_BITS))
59 /* Lbitset element. We use an array of bits for each element.
60 These are linked together in a doubly-linked list. */
61 typedef struct lbitset_elt_struct
63 struct lbitset_elt_struct
*next
; /* Next element. */
64 struct lbitset_elt_struct
*prev
; /* Previous element. */
65 bitset_windex index
; /* bitno / BITSET_WORD_BITS. */
66 bitset_word words
[LBITSET_ELT_WORDS
]; /* Bits that are set. */
71 enum lbitset_find_mode
72 { LBITSET_FIND
, LBITSET_CREATE
, LBITSET_SUBST
};
74 static lbitset_elt lbitset_zero_elts
[3]; /* Elements of all zero bits. */
76 /* Obstack to allocate bitset elements from. */
77 static struct obstack lbitset_obstack
;
78 static bool lbitset_obstack_init
= false;
79 static lbitset_elt
*lbitset_free_list
; /* Free list of bitset elements. */
81 extern void debug_lbitset
PARAMS ((bitset
));
83 #define LBITSET_CURRENT1(X) \
84 ((lbitset_elt *) (void *) ((char *) (X) - offsetof (lbitset_elt, words)))
86 #define LBITSET_CURRENT(X) LBITSET_CURRENT1((X)->b.cdata)
88 #define LBITSET_HEAD(X) ((X)->l.head)
89 #define LBITSET_TAIL(X) ((X)->l.tail)
91 /* Allocate a lbitset element. The bits are not cleared. */
92 static inline lbitset_elt
*
93 lbitset_elt_alloc (void)
97 if (lbitset_free_list
!= 0)
99 elt
= lbitset_free_list
;
100 lbitset_free_list
= elt
->next
;
104 if (!lbitset_obstack_init
)
106 lbitset_obstack_init
= true;
108 /* Let particular systems override the size of a chunk. */
110 #ifndef OBSTACK_CHUNK_SIZE
111 #define OBSTACK_CHUNK_SIZE 0
114 /* Let them override the alloc and free routines too. */
116 #ifndef OBSTACK_CHUNK_ALLOC
117 #define OBSTACK_CHUNK_ALLOC xmalloc
120 #ifndef OBSTACK_CHUNK_FREE
121 #define OBSTACK_CHUNK_FREE free
124 #if !defined(__GNUC__) || (__GNUC__ < 2)
125 #define __alignof__(type) 0
128 obstack_specify_allocation (&lbitset_obstack
, OBSTACK_CHUNK_SIZE
,
129 __alignof__ (lbitset_elt
),
130 (void *(*)PARAMS ((long)))
132 (void (*)PARAMS ((void *)))
136 /* Perhaps we should add a number of new elements to the free
138 elt
= (lbitset_elt
*) obstack_alloc (&lbitset_obstack
,
139 sizeof (lbitset_elt
));
146 /* Allocate a lbitset element. The bits are cleared. */
147 static inline lbitset_elt
*
148 lbitset_elt_calloc (void)
152 elt
= lbitset_elt_alloc ();
153 memset (elt
->words
, 0, sizeof (elt
->words
));
159 lbitset_elt_free (lbitset_elt
*elt
)
161 elt
->next
= lbitset_free_list
;
162 lbitset_free_list
= elt
;
166 /* Unlink element ELT from bitset BSET. */
168 lbitset_elt_unlink (bitset bset
, lbitset_elt
*elt
)
170 lbitset_elt
*next
= elt
->next
;
171 lbitset_elt
*prev
= elt
->prev
;
179 if (LBITSET_HEAD (bset
) == elt
)
180 LBITSET_HEAD (bset
) = next
;
181 if (LBITSET_TAIL (bset
) == elt
)
182 LBITSET_TAIL (bset
) = prev
;
184 /* Update cache pointer. Since the first thing we try is to insert
185 before current, make current the next entry in preference to the
187 if (LBITSET_CURRENT (bset
) == elt
)
191 bset
->b
.cdata
= next
->words
;
192 bset
->b
.cindex
= next
->index
;
196 bset
->b
.cdata
= prev
->words
;
197 bset
->b
.cindex
= prev
->index
;
206 lbitset_elt_free (elt
);
210 /* Cut the chain of bitset BSET before element ELT and free the
213 lbitset_prune (bitset bset
, lbitset_elt
*elt
)
222 LBITSET_TAIL (bset
) = elt
->prev
;
223 bset
->b
.cdata
= elt
->prev
->words
;
224 bset
->b
.cindex
= elt
->prev
->index
;
229 LBITSET_HEAD (bset
) = 0;
230 LBITSET_TAIL (bset
) = 0;
235 for (; elt
; elt
= next
)
238 lbitset_elt_free (elt
);
243 /* Are all bits in an element zero? */
245 lbitset_elt_zero_p (lbitset_elt
*elt
)
249 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
257 /* Link the bitset element into the current bitset linked list. */
259 lbitset_elt_link (bitset bset
, lbitset_elt
*elt
)
261 bitset_windex windex
= elt
->index
;
263 lbitset_elt
*current
;
266 current
= LBITSET_CURRENT (bset
);
268 current
= LBITSET_HEAD (bset
);
270 /* If this is the first and only element, add it in. */
271 if (LBITSET_HEAD (bset
) == 0)
273 elt
->next
= elt
->prev
= 0;
274 LBITSET_HEAD (bset
) = elt
;
275 LBITSET_TAIL (bset
) = elt
;
278 /* If this index is less than that of the current element, it goes
279 somewhere before the current element. */
280 else if (windex
< bset
->b
.cindex
)
283 ptr
->prev
&& ptr
->prev
->index
> windex
; ptr
= ptr
->prev
)
287 ptr
->prev
->next
= elt
;
289 LBITSET_HEAD (bset
) = elt
;
291 elt
->prev
= ptr
->prev
;
296 /* Otherwise, it must go somewhere after the current element. */
300 ptr
->next
&& ptr
->next
->index
< windex
; ptr
= ptr
->next
)
304 ptr
->next
->prev
= elt
;
306 LBITSET_TAIL (bset
) = elt
;
308 elt
->next
= ptr
->next
;
313 /* Set up so this is the first element searched. */
314 bset
->b
.cindex
= windex
;
315 bset
->b
.csize
= LBITSET_ELT_WORDS
;
316 bset
->b
.cdata
= elt
->words
;
321 lbitset_elt_find (bitset bset
, bitset_windex windex
,
322 enum lbitset_find_mode mode
)
325 lbitset_elt
*current
;
329 current
= LBITSET_CURRENT (bset
);
330 /* Check if element is the cached element. */
331 if ((windex
- bset
->b
.cindex
) < bset
->b
.csize
)
336 current
= LBITSET_HEAD (bset
);
341 if (windex
< bset
->b
.cindex
)
344 elt
->prev
&& elt
->index
> windex
; elt
= elt
->prev
)
350 elt
->next
&& (elt
->index
+ LBITSET_ELT_WORDS
- 1) < windex
;
355 /* ELT is the nearest to the one we want. If it's not the one
356 we want, the one we want does not exist. */
357 if (elt
&& (windex
- elt
->index
) < LBITSET_ELT_WORDS
)
359 bset
->b
.cindex
= elt
->index
;
360 bset
->b
.csize
= LBITSET_ELT_WORDS
;
361 bset
->b
.cdata
= elt
->words
;
372 windex
-= windex
% LBITSET_ELT_WORDS
;
374 elt
= lbitset_elt_calloc ();
376 lbitset_elt_link (bset
, elt
);
380 return &lbitset_zero_elts
[0];
388 /* Weed out the zero elements from the list. */
390 lbitset_weed (bitset bset
)
395 for (elt
= LBITSET_HEAD (bset
); elt
; elt
= next
)
398 if (lbitset_elt_zero_p (elt
))
399 lbitset_elt_unlink (bset
, elt
);
404 /* Set all bits in the bitset to zero. */
406 lbitset_zero (bitset bset
)
410 head
= LBITSET_HEAD (bset
);
414 /* Clear a bitset by freeing the linked list at the head element. */
415 lbitset_prune (bset
, head
);
421 lbitset_equal_p (bitset dst
, bitset src
)
432 for (selt
= LBITSET_HEAD (src
), delt
= LBITSET_HEAD (dst
);
433 selt
&& delt
; selt
= selt
->next
, delt
= delt
->next
)
435 if (selt
->index
!= delt
->index
)
438 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
439 if (delt
->words
[j
] != selt
->words
[j
])
442 return !selt
&& !delt
;
446 /* Copy bits from bitset SRC to bitset DST. */
448 lbitset_copy (bitset dst
, bitset src
)
460 head
= LBITSET_HEAD (src
);
465 for (elt
= head
; elt
; elt
= elt
->next
)
467 tmp
= lbitset_elt_alloc ();
468 tmp
->index
= elt
->index
;
474 LBITSET_HEAD (dst
) = tmp
;
477 memcpy (tmp
->words
, elt
->words
, sizeof (elt
->words
));
479 LBITSET_TAIL (dst
) = tmp
;
481 dst
->b
.csize
= LBITSET_ELT_WORDS
;
482 dst
->b
.cdata
= LBITSET_HEAD (dst
)->words
;
483 dst
->b
.cindex
= LBITSET_HEAD (dst
)->index
;
487 /* Copy bits from bitset SRC to bitset DST. Return true if
488 bitsets different. */
490 lbitset_copy_cmp (bitset dst
, bitset src
)
495 if (!LBITSET_HEAD (dst
))
497 lbitset_copy (dst
, src
);
498 return LBITSET_HEAD (src
) != 0;
501 if (lbitset_equal_p (dst
, src
))
504 lbitset_copy (dst
, src
);
510 lbitset_resize (bitset src
, bitset_bindex size
)
512 BITSET_NBITS_ (src
) = size
;
514 /* Need to prune any excess bits. FIXME. */
518 /* Set bit BITNO in bitset DST. */
520 lbitset_set (bitset dst
, bitset_bindex bitno
)
522 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
524 lbitset_elt_find (dst
, windex
, LBITSET_CREATE
);
526 dst
->b
.cdata
[windex
- dst
->b
.cindex
] |=
527 (bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
);
531 /* Reset bit BITNO in bitset DST. */
533 lbitset_reset (bitset dst
, bitset_bindex bitno
)
535 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
537 if (!lbitset_elt_find (dst
, windex
, LBITSET_FIND
))
540 dst
->b
.cdata
[windex
- dst
->b
.cindex
] &=
541 ~((bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
));
543 /* If all the data is zero, perhaps we should unlink it now... */
547 /* Test bit BITNO in bitset SRC. */
549 lbitset_test (bitset src
, bitset_bindex bitno
)
551 bitset_windex windex
= bitno
/ BITSET_WORD_BITS
;
553 return (lbitset_elt_find (src
, windex
, LBITSET_FIND
)
554 && ((src
->b
.cdata
[windex
- src
->b
.cindex
]
555 >> (bitno
% BITSET_WORD_BITS
))
561 lbitset_free (bitset bset
)
567 /* Find list of up to NUM bits set in BSET starting from and including
568 *NEXT and store in array LIST. Return with actual number of bits
569 found and with *NEXT indicating where search stopped. */
571 lbitset_list_reverse (bitset bset
, bitset_bindex
*list
,
572 bitset_bindex num
, bitset_bindex
*next
)
574 bitset_bindex rbitno
;
577 bitset_bindex boffset
;
578 bitset_windex windex
;
582 bitset_bindex n_bits
;
584 elt
= LBITSET_TAIL (bset
);
588 n_bits
= (elt
->index
+ LBITSET_ELT_WORDS
) * BITSET_WORD_BITS
;
591 if (rbitno
>= n_bits
)
594 bitno
= n_bits
- (rbitno
+ 1);
596 windex
= bitno
/ BITSET_WORD_BITS
;
598 /* Skip back to starting element. */
599 for (; elt
&& elt
->index
> windex
; elt
= elt
->prev
)
605 if (windex
>= elt
->index
+ LBITSET_ELT_WORDS
)
607 /* We are trying to start in no-mans land so start
608 at end of current elt. */
609 bcount
= BITSET_WORD_BITS
- 1;
610 windex
= elt
->index
+ LBITSET_ELT_WORDS
- 1;
614 bcount
= bitno
% BITSET_WORD_BITS
;
618 boffset
= windex
* BITSET_WORD_BITS
;
620 /* If num is 1, we could speed things up with a binary search
621 of the word of interest. */
625 bitset_word
*srcp
= elt
->words
;
627 for (; (windex
- elt
->index
) < LBITSET_ELT_WORDS
;
628 windex
--, boffset
-= BITSET_WORD_BITS
,
629 bcount
= BITSET_WORD_BITS
- 1)
632 srcp
[windex
- elt
->index
] << (BITSET_WORD_BITS
- 1 - bcount
);
634 for (; word
; bcount
--)
636 if (word
& BITSET_MSB
)
638 list
[count
++] = boffset
+ bcount
;
641 *next
= n_bits
- (boffset
+ bcount
);
652 windex
= elt
->index
+ LBITSET_ELT_WORDS
- 1;
653 boffset
= windex
* BITSET_WORD_BITS
;
657 *next
= n_bits
- (boffset
+ 1);
662 /* Find list of up to NUM bits set in BSET starting from and including
663 *NEXT and store in array LIST. Return with actual number of bits
664 found and with *NEXT indicating where search stopped. */
666 lbitset_list (bitset bset
, bitset_bindex
*list
,
667 bitset_bindex num
, bitset_bindex
*next
)
670 bitset_windex windex
;
676 head
= LBITSET_HEAD (bset
);
685 /* This is the most common case. */
687 /* Start with the first element. */
690 bitno
= windex
* BITSET_WORD_BITS
;
694 windex
= bitno
/ BITSET_WORD_BITS
;
696 /* Skip to starting element. */
698 elt
&& (elt
->index
+ LBITSET_ELT_WORDS
- 1) < windex
;
705 if (windex
< elt
->index
)
708 bitno
= windex
* BITSET_WORD_BITS
;
712 bitset_word
*srcp
= elt
->words
;
714 /* We are starting within an element. */
716 for (; (windex
- elt
->index
) < LBITSET_ELT_WORDS
; windex
++)
718 word
= srcp
[windex
- elt
->index
] >> (bitno
% BITSET_WORD_BITS
);
720 for (; word
; bitno
++)
724 list
[count
++] = bitno
;
733 bitno
= (windex
+ 1) * BITSET_WORD_BITS
;
740 bitno
= windex
* BITSET_WORD_BITS
;
746 /* If num is 1, we could speed things up with a binary search
747 of the word of interest. */
752 bitset_word
*srcp
= elt
->words
;
754 if ((count
+ LBITSET_ELT_BITS
) < num
)
756 /* The coast is clear, plant boot! */
758 #if LBITSET_ELT_WORDS == 2
762 if (!(word
& 0xffff))
772 for (; word
; bitno
++)
775 list
[count
++] = bitno
;
780 bitno
= windex
* BITSET_WORD_BITS
;
785 if (!(word
& 0xffff))
790 for (; word
; bitno
++)
793 list
[count
++] = bitno
;
798 bitno
= windex
* BITSET_WORD_BITS
;
800 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
805 if (!(word
& 0xffff))
815 for (; word
; bitno
++)
818 list
[count
++] = bitno
;
823 bitno
= windex
* BITSET_WORD_BITS
;
829 /* Tread more carefully since we need to check
830 if array overflows. */
832 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
834 for (word
= srcp
[i
]; word
; bitno
++)
838 list
[count
++] = bitno
;
848 bitno
= windex
* BITSET_WORD_BITS
;
856 bitno
= windex
* BITSET_WORD_BITS
;
866 lbitset_empty_p (bitset dst
)
871 for (elt
= LBITSET_HEAD (dst
); elt
; elt
= next
)
874 if (!lbitset_elt_zero_p (elt
))
877 lbitset_elt_unlink (dst
, elt
);
884 /* Ensure that any unused bits within the last element are clear. */
886 lbitset_unused_clear (bitset dst
)
888 unsigned int last_bit
;
889 bitset_bindex n_bits
;
891 n_bits
= BITSET_SIZE_ (dst
);
892 last_bit
= n_bits
% LBITSET_ELT_BITS
;
897 bitset_windex windex
;
900 elt
= LBITSET_TAIL (dst
);
902 windex
= n_bits
/ BITSET_WORD_BITS
;
904 srcp
[windex
- elt
->index
] &= ((bitset_word
) 1 << last_bit
) - 1;
907 for (; (windex
- elt
->index
) < LBITSET_ELT_WORDS
; windex
++)
908 srcp
[windex
- elt
->index
] = 0;
914 lbitset_ones (bitset dst
)
917 bitset_windex windex
;
920 /* This is a decidedly unfriendly operation for a linked list
921 bitset! It makes a sparse bitset become dense. An alternative
922 is to have a flag that indicates that the bitset stores the
923 complement of what it indicates. */
925 windex
= (BITSET_SIZE_ (dst
) + BITSET_WORD_BITS
- 1) / BITSET_WORD_BITS
;
927 for (i
= 0; i
< windex
; i
+= LBITSET_ELT_WORDS
)
929 /* Create new elements if they cannot be found. */
930 elt
= lbitset_elt_find (dst
, i
, LBITSET_CREATE
);
931 memset (elt
->words
, -1, sizeof (elt
->words
));
934 lbitset_unused_clear (dst
);
939 lbitset_not (bitset dst
, bitset src
)
946 bitset_windex windex
;
948 /* This is another unfriendly operation for a linked list
950 elt
= LBITSET_TAIL (dst
);
952 windex
= (BITSET_SIZE_ (dst
) + BITSET_WORD_BITS
- 1) / BITSET_WORD_BITS
;
954 for (i
= 0; i
< windex
; i
+= LBITSET_ELT_WORDS
)
956 /* Create new elements for dst if they cannot be found
957 or substitute zero elements if src elements not found. */
958 selt
= lbitset_elt_find (src
, i
, LBITSET_SUBST
);
959 delt
= lbitset_elt_find (dst
, i
, LBITSET_CREATE
);
961 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
962 delt
->words
[j
] = ~selt
->words
[j
];
964 lbitset_unused_clear (dst
);
970 /* Is DST == DST | SRC? */
972 lbitset_subset_p (bitset dst
, bitset src
)
978 for (selt
= LBITSET_HEAD (src
), delt
= LBITSET_HEAD (dst
);
979 selt
|| delt
; selt
= selt
->next
, delt
= delt
->next
)
982 selt
= &lbitset_zero_elts
[0];
984 delt
= &lbitset_zero_elts
[0];
985 else if (selt
->index
!= delt
->index
)
987 if (selt
->index
< delt
->index
)
989 lbitset_zero_elts
[2].next
= delt
;
990 delt
= &lbitset_zero_elts
[2];
994 lbitset_zero_elts
[1].next
= selt
;
995 selt
= &lbitset_zero_elts
[1];
999 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
1000 if (delt
->words
[j
] != (selt
->words
[j
] | delt
->words
[j
]))
1007 /* Is DST & SRC == 0? */
1009 lbitset_disjoint_p (bitset dst
, bitset src
)
1015 for (selt
= LBITSET_HEAD (src
), delt
= LBITSET_HEAD (dst
);
1016 selt
&& delt
; selt
= selt
->next
, delt
= delt
->next
)
1018 if (selt
->index
!= delt
->index
)
1020 if (selt
->index
< delt
->index
)
1022 lbitset_zero_elts
[2].next
= delt
;
1023 delt
= &lbitset_zero_elts
[2];
1027 lbitset_zero_elts
[1].next
= selt
;
1028 selt
= &lbitset_zero_elts
[1];
1030 /* Since the elements are different, there is no
1031 intersection of these elements. */
1035 for (j
= 0; j
< LBITSET_ELT_WORDS
; j
++)
1036 if (selt
->words
[j
] & delt
->words
[j
])
1044 lbitset_op3_cmp (bitset dst
, bitset src1
, bitset src2
, enum bitset_ops op
)
1046 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1047 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1048 lbitset_elt
*delt
= LBITSET_HEAD (dst
);
1049 bitset_windex windex1
;
1050 bitset_windex windex2
;
1051 bitset_windex windex
;
1058 bool changed
= false;
1061 LBITSET_HEAD (dst
) = 0;
1064 windex1
= (selt1
) ? selt1
->index
: BITSET_WINDEX_MAX
;
1065 windex2
= (selt2
) ? selt2
->index
: BITSET_WINDEX_MAX
;
1067 while (selt1
|| selt2
)
1069 /* Figure out whether we need to substitute zero elements for
1071 if (windex1
== windex2
)
1076 selt1
= selt1
->next
;
1077 windex1
= (selt1
) ? selt1
->index
: BITSET_WINDEX_MAX
;
1078 selt2
= selt2
->next
;
1079 windex2
= (selt2
) ? selt2
->index
: BITSET_WINDEX_MAX
;
1081 else if (windex1
< windex2
)
1085 stmp2
= &lbitset_zero_elts
[0];
1086 selt1
= selt1
->next
;
1087 windex1
= (selt1
) ? selt1
->index
: BITSET_WINDEX_MAX
;
1092 stmp1
= &lbitset_zero_elts
[0];
1094 selt2
= selt2
->next
;
1095 windex2
= (selt2
) ? selt2
->index
: BITSET_WINDEX_MAX
;
1098 /* Find the appropriate element from DST. Begin by discarding
1099 elements that we've skipped. */
1100 while (delt
&& delt
->index
< windex
)
1105 lbitset_elt_free (dtmp
);
1107 if (delt
&& delt
->index
== windex
)
1113 dtmp
= lbitset_elt_calloc ();
1115 /* Do the operation, and if any bits are set, link it into the
1117 srcp1
= stmp1
->words
;
1118 srcp2
= stmp2
->words
;
1123 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1125 bitset_word tmp
= *srcp1
++ | *srcp2
++;
1136 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1138 bitset_word tmp
= *srcp1
++ & *srcp2
++;
1149 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1151 bitset_word tmp
= *srcp1
++ ^ *srcp2
++;
1161 case BITSET_OP_ANDN
:
1162 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++, dstp
++)
1164 bitset_word tmp
= *srcp1
++ & ~(*srcp2
++);
1178 if (!lbitset_elt_zero_p (dtmp
))
1180 dtmp
->index
= windex
;
1181 /* Perhaps this could be optimised... */
1182 lbitset_elt_link (dst
, dtmp
);
1186 lbitset_elt_free (dtmp
);
1190 /* If we have elements of DST left over, free them all. */
1194 lbitset_prune (dst
, delt
);
1202 lbitset_and_cmp (bitset dst
, bitset src1
, bitset src2
)
1204 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1205 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1211 changed
= !LBITSET_HEAD (dst
);
1218 changed
= !LBITSET_HEAD (dst
);
1222 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_AND
);
1227 lbitset_and (bitset dst
, bitset src1
, bitset src2
)
1229 lbitset_and_cmp (dst
, src1
, src2
);
1234 lbitset_andn_cmp (bitset dst
, bitset src1
, bitset src2
)
1236 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1237 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1242 return lbitset_copy_cmp (dst
, src1
);
1247 changed
= !LBITSET_HEAD (dst
);
1251 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_ANDN
);
1256 lbitset_andn (bitset dst
, bitset src1
, bitset src2
)
1258 lbitset_andn_cmp (dst
, src1
, src2
);
1263 lbitset_or_cmp (bitset dst
, bitset src1
, bitset src2
)
1265 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1266 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1270 return lbitset_copy_cmp (dst
, src1
);
1274 return lbitset_copy_cmp (dst
, src2
);
1276 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_OR
);
1281 lbitset_or (bitset dst
, bitset src1
, bitset src2
)
1283 lbitset_or_cmp (dst
, src1
, src2
);
1288 lbitset_xor_cmp (bitset dst
, bitset src1
, bitset src2
)
1290 lbitset_elt
*selt1
= LBITSET_HEAD (src1
);
1291 lbitset_elt
*selt2
= LBITSET_HEAD (src2
);
1295 return lbitset_copy_cmp (dst
, src1
);
1299 return lbitset_copy_cmp (dst
, src2
);
1301 return lbitset_op3_cmp (dst
, src1
, src2
, BITSET_OP_XOR
);
1306 lbitset_xor (bitset dst
, bitset src1
, bitset src2
)
1308 lbitset_xor_cmp (dst
, src1
, src2
);
1313 /* Vector of operations for linked-list bitsets. */
1314 struct bitset_vtable lbitset_vtable
= {
1341 bitset_andn_or_cmp_
,
1345 lbitset_list_reverse
,
1351 /* Return size of initial structure. */
1353 lbitset_bytes (bitset_bindex n_bits ATTRIBUTE_UNUSED
)
1355 return sizeof (struct lbitset_struct
);
1359 /* Initialize a bitset. */
1361 lbitset_init (bitset bset
, bitset_bindex n_bits ATTRIBUTE_UNUSED
)
1363 BITSET_NBITS_ (bset
) = n_bits
;
1364 bset
->b
.vtable
= &lbitset_vtable
;
1370 lbitset_release_memory (void)
1372 lbitset_free_list
= 0;
1373 if (lbitset_obstack_init
)
1375 lbitset_obstack_init
= false;
1376 obstack_free (&lbitset_obstack
, NULL
);
1381 /* Function to be called from debugger to debug lbitset. */
1383 debug_lbitset (bitset bset
)
1391 for (elt
= LBITSET_HEAD (bset
); elt
; elt
= elt
->next
)
1393 fprintf (stderr
, "Elt %lu\n", (unsigned long) elt
->index
);
1394 for (i
= 0; i
< LBITSET_ELT_WORDS
; i
++)
1399 word
= elt
->words
[i
];
1401 fprintf (stderr
, " Word %u:", i
);
1402 for (j
= 0; j
< LBITSET_WORD_BITS
; j
++)
1403 if ((word
& ((bitset_word
) 1 << j
)))
1404 fprintf (stderr
, " %u", j
);
1405 fprintf (stderr
, "\n");