* modules/bitset (Depends-on): Add integer_length_l.
* lib/bitset/base.h (bitset_fls_, BITSET_FOR_EACH_BIT_REVERSE): New.
* lib/bitset/array.c (abitset_list_reverse): Use it.
+2020-11-29 Akim Demaille <akim@lrde.epita.fr>
+
+ bitset: use integer_length in array implementation
+ * modules/bitset (Depends-on): Add integer_length_l.
+ * lib/bitset/base.h (bitset_fls_, BITSET_FOR_EACH_BIT_REVERSE): New.
+ * lib/bitset/array.c (abitset_list_reverse): Use it.
+
2020-11-29 Akim Demaille <akim@lrde.epita.fr>
bitset: style: use consistent names
do
{
- bitset_word word = srcp[windex] << (BITSET_WORD_BITS - 1 - bitcnt);
- for (; word; bitcnt--)
+ bitset_word word = srcp[windex];
+ if (bitcnt + 1 < BITSET_WORD_BITS)
+ /* We're starting in the middle of a word: smash bits to ignore. */
+ word &= ((bitset_word) 1 << (bitcnt + 1)) - 1;
+ BITSET_FOR_EACH_BIT_REVERSE(pos, word)
{
- if (word & BITSET_MSB)
+ list[count++] = bitoff + pos;
+ if (count >= num)
{
- list[count++] = bitoff + bitcnt;
- if (count >= num)
- {
- *next = n_bits - (bitoff + bitcnt);
- return count;
- }
+ *next = n_bits - (bitoff + pos);
+ return count;
}
- word <<= 1;
}
bitoff -= BITSET_WORD_BITS;
bitcnt = BITSET_WORD_BITS - 1;
#include <string.h> /* ffsl */
#include "attribute.h"
+#include "integer_length.h"
#include "xalloc.h"
/* Currently we support five flavours of bitsets:
0 <= Pos; \
Word ^= 1UL << Pos, Pos = bitset_ffs_ (Word))
+/* Iterate right to left over each set bit of WORD.
+ Each iteration sets POS to the 0-based index of the next set bit in WORD.
+ Repeatedly resets bits in WORD in place until it's null. */
+#define BITSET_FOR_EACH_BIT_REVERSE(Pos, Word) \
+ for (int Pos = bitset_fls_ (Word); \
+ 0 <= Pos; \
+ Word ^= 1UL << Pos, Pos = bitset_fls_ (Word))
+
/* Private functions for bitset implementations. */
bool bitset_toggle_ (bitset, bitset_bindex);
return ffsl ((long) word) - 1;
}
+/* Last set bit in WORD.
+ Indexes start at 0, return -1 if WORD is null. */
+static inline
+int bitset_fls_ (bitset_word word)
+{
+ return integer_length_l (word) - 1;
+}
+
#endif /* _BBITSET_H */
ffsl
fopen-gnu
gettext-h
+integer_length_l
obstack
xalloc