Skip to content

Commit

Permalink
bitmap: Fix some bugs on 32-bit platforms
Browse files Browse the repository at this point in the history
The bitmap_word type is an unsigned long.  However in some places we assign
it using -1ULL, a 64-bit value on many platforms.  We sometimes get away
with this because it masks correctly, but in other cases it breaks things.

To clean this up define a new BITMAP_WORD_1 constant, indicating a
bitmap_word with all bits set, and use that instead of explicit UL or ULL
qualifiers.

Signed-off-by: David Gibson <[email protected]>
  • Loading branch information
dgibson committed Jul 11, 2019
1 parent ce4660a commit cdd0b8b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
14 changes: 7 additions & 7 deletions ccan/bitmap/bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ void bitmap_zero_range(bitmap *bitmap, unsigned long n, unsigned long m)
{
unsigned long an = BIT_ALIGN_UP(n);
unsigned long am = BIT_ALIGN_DOWN(m);
bitmap_word headmask = -1ULL >> (n % BITMAP_WORD_BITS);
bitmap_word tailmask = ~(-1ULL >> (m % BITMAP_WORD_BITS));
bitmap_word headmask = BITMAP_WORD_1 >> (n % BITMAP_WORD_BITS);
bitmap_word tailmask = ~(BITMAP_WORD_1 >> (m % BITMAP_WORD_BITS));

assert(m >= n);

Expand All @@ -38,8 +38,8 @@ void bitmap_fill_range(bitmap *bitmap, unsigned long n, unsigned long m)
{
unsigned long an = BIT_ALIGN_UP(n);
unsigned long am = BIT_ALIGN_DOWN(m);
bitmap_word headmask = -1ULL >> (n % BITMAP_WORD_BITS);
bitmap_word tailmask = ~(-1ULL >> (m % BITMAP_WORD_BITS));
bitmap_word headmask = BITMAP_WORD_1 >> (n % BITMAP_WORD_BITS);
bitmap_word tailmask = ~(BITMAP_WORD_1 >> (m % BITMAP_WORD_BITS));

assert(m >= n);

Expand All @@ -65,7 +65,7 @@ static int bitmap_clz(bitmap_word w)
return __builtin_clzl(w);
#else
int lz = 0;
bitmap_word mask = 1UL << (BITMAP_WORD_BITS - 1);
bitmap_word mask = (bitmap_word)1 << (BITMAP_WORD_BITS - 1);

while (!(w & mask)) {
lz++;
Expand All @@ -81,8 +81,8 @@ unsigned long bitmap_ffs(const bitmap *bitmap,
{
unsigned long an = BIT_ALIGN_UP(n);
unsigned long am = BIT_ALIGN_DOWN(m);
bitmap_word headmask = -1ULL >> (n % BITMAP_WORD_BITS);
bitmap_word tailmask = ~(-1ULL >> (m % BITMAP_WORD_BITS));
bitmap_word headmask = BITMAP_WORD_1 >> (n % BITMAP_WORD_BITS);
bitmap_word tailmask = ~(BITMAP_WORD_1 >> (m % BITMAP_WORD_BITS));

assert(m >= n);

Expand Down
3 changes: 3 additions & 0 deletions ccan/bitmap/bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ typedef unsigned long bitmap_word;
#define BITMAP_NWORDS(_n) \
(((_n) + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS)

#define BITMAP_WORD_0 (0)
#define BITMAP_WORD_1 ((bitmap_word)-1UL)

/*
* We wrap each word in a structure for type checking.
*/
Expand Down

0 comments on commit cdd0b8b

Please sign in to comment.