summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkim Demaille <akim.demaille@gmail.com>2020-11-18 21:19:16 +0100
committerAkim Demaille <akim.demaille@gmail.com>2020-11-19 07:01:22 +0100
commit51c37359b756d2a4e1b7e92f8c00cc499a40cafe (patch)
treead63984d50e0bd0918848f69aaad550a386e36ad
parentae6bdfaddd4711e1ccdc3bebb8a4c68aeb649992 (diff)
downloadgnulib-51c37359b756d2a4e1b7e92f8c00cc499a40cafe.tar.gz
gnulib-51c37359b756d2a4e1b7e92f8c00cc499a40cafe.tar.bz2
bitset: use ffs where possible in the vector implementation
* lib/bitset/vector.c (vbitset_list): Use BITSET_FOR_EACH_BIT.
-rw-r--r--ChangeLog5
-rw-r--r--lib/bitset/vector.c57
2 files changed, 27 insertions, 35 deletions
diff --git a/ChangeLog b/ChangeLog
index 5880f9032a..afad46b25c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
2020-11-19 Akim Demaille <akim@lrde.epita.fr>
+ bitset: use ffs where possible in the vector implementation
+ * lib/bitset/vector.c (vbitset_list): Use BITSET_FOR_EACH_BIT.
+
+2020-11-19 Akim Demaille <akim@lrde.epita.fr>
+
bitset: use ffs where possible in the table implementation
* lib/bitset/table.c (tbitset_list): Use BITSET_FOR_EACH_BIT.
diff --git a/lib/bitset/vector.c b/lib/bitset/vector.c
index fe14d67037..4c0c9dbfd2 100644
--- a/lib/bitset/vector.c
+++ b/lib/bitset/vector.c
@@ -204,6 +204,7 @@ static bitset_bindex
vbitset_list (bitset src, bitset_bindex *list,
bitset_bindex num, bitset_bindex *next)
{
+ /* FIXME: almost a duplicate of abitset_list. Factor? */
bitset_windex size = VBITSET_SIZE (src);
bitset_word *srcp = VBITSET_WORDS (src);
bitset_bindex bitno = *next;
@@ -211,7 +212,6 @@ vbitset_list (bitset src, bitset_bindex *list,
bitset_windex windex;
bitset_bindex bitoff;
- bitset_word word;
if (!bitno)
{
@@ -242,19 +242,16 @@ vbitset_list (bitset src, bitset_bindex *list,
on the previous call to this function. */
bitoff = windex * BITSET_WORD_BITS;
- word = srcp[windex] >> bitno;
- for (bitno = bitoff + bitno; word; bitno++)
+ bitset_word word = srcp[windex] >> bitno;
+ bitno = bitoff + bitno;
+ BITSET_FOR_EACH_BIT (pos, word)
{
- if (word & 1)
+ list[count++] = bitno + pos;
+ if (count >= num)
{
- list[count++] = bitno;
- if (count >= num)
- {
- *next = bitno + 1;
- return count;
- }
+ *next = bitno + pos + 1;
+ return count;
}
- word >>= 1;
}
windex++;
}
@@ -263,34 +260,24 @@ vbitset_list (bitset src, bitset_bindex *list,
for (; windex < size; windex++, bitoff += BITSET_WORD_BITS)
{
- if (!(word = srcp[windex]))
+ bitset_word word = srcp[windex];
+ if (!word)
continue;
+ /* Is there enough room to avoid checking in each iteration? */
if ((count + BITSET_WORD_BITS) < num)
- {
- for (bitno = bitoff; word; bitno++)
- {
- if (word & 1)
- list[count++] = bitno;
- word >>= 1;
- }
- }
+ BITSET_FOR_EACH_BIT (pos, word)
+ list[count++] = bitoff + pos;
else
- {
- for (bitno = bitoff; word; bitno++)
- {
- if (word & 1)
- {
- list[count++] = bitno;
- if (count >= num)
- {
- *next = bitno + 1;
- return count;
- }
- }
- word >>= 1;
- }
- }
+ BITSET_FOR_EACH_BIT (pos, word)
+ {
+ list[count++] = bitoff + pos;
+ if (count >= num)
+ {
+ *next = bitoff + pos + 1;
+ return count;
+ }
+ }
}
*next = bitoff;

Return to:

Send suggestions and report system problems to the System administrator.