diff options
-rw-r--r-- | test/bitset_test.c | 7 | ||||
-rw-r--r-- | zix/bitset.c | 12 |
2 files changed, 11 insertions, 8 deletions
diff --git a/test/bitset_test.c b/test/bitset_test.c index dbb7bd7..4514e79 100644 --- a/test/bitset_test.c +++ b/test/bitset_test.c @@ -74,10 +74,9 @@ main(int argc, char** argv) } zix_bitset_clear(b, t, N_BITS); - for (size_t i = 0; i <= N_BITS; i += 2) { - if (i < N_BITS) { - zix_bitset_set(b, t, i); - } + for (size_t i = 0; i < N_BITS; i += 2) { + zix_bitset_set(b, t, i); + const size_t count = zix_bitset_count_up_to(b, t, i + 1); const size_t result = MIN(N_BITS / 2, i / 2 + 1); if (count != result) { diff --git a/zix/bitset.c b/zix/bitset.c index 6a4d245..e5f8348 100644 --- a/zix/bitset.c +++ b/zix/bitset.c @@ -77,8 +77,10 @@ zix_bitset_count_up_to(const ZixBitset* b, const ZixBitsetTally* t, size_t i) count += t[e]; } - const ZixBitset mask = ~(~(ZixBitset)0 << extra); - count += (size_t)__builtin_popcountl(b[full_elems] & mask); + if (extra) { + const ZixBitset mask = ~(~(ZixBitset)0 << extra); + count += (size_t)__builtin_popcountl(b[full_elems] & mask); + } return count; } @@ -99,8 +101,10 @@ zix_bitset_count_up_to_if(const ZixBitset* b, const ZixBitsetTally* t, size_t i) count += t[e]; } - const ZixBitset mask = ~(~(ZixBitset)0 << extra); - count += __builtin_popcountl(b[full_elems] & mask); + if (extra) { + const ZixBitset mask = ~(~(ZixBitset)0 << extra); + count += __builtin_popcountl(b[full_elems] & mask); + } return count; } |