mirror of
https://github.com/ioacademy-jikim/debugging
synced 2025-06-07 16:06:09 +00:00
81 lines
1.6 KiB
C
81 lines
1.6 KiB
C
#include <stdio.h>
|
|
#define BITS_PER_LONG 32
|
|
unsigned long __ffs(unsigned long word)
|
|
{
|
|
int num = 0;
|
|
|
|
#if BITS_PER_LONG == 64
|
|
if ((word & 0xffffffff) == 0) {
|
|
num += 32;
|
|
word >>= 32;
|
|
}
|
|
#endif
|
|
if ((word & 0xffff) == 0) {
|
|
num += 16;
|
|
word >>= 16;
|
|
}
|
|
if ((word & 0xff) == 0) {
|
|
num += 8;
|
|
word >>= 8;
|
|
}
|
|
if ((word & 0xf) == 0) {
|
|
num += 4;
|
|
word >>= 4;
|
|
}
|
|
if ((word & 0x3) == 0) {
|
|
num += 2;
|
|
word >>= 2;
|
|
}
|
|
if ((word & 0x1) == 0)
|
|
num += 1;
|
|
return num;
|
|
}
|
|
#define min(x, y) ({ \
|
|
typeof(x) _min1 = (x); \
|
|
typeof(y) _min2 = (y); \
|
|
(void) (&_min1 == &_min2); \
|
|
_min1 < _min2 ? _min1 : _min2; })
|
|
#define __round_mask(x, y) ((__typeof__(x))((y)-1))
|
|
#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
|
|
#define round_down(x, y) ((x) & ~__round_mask(x, y))
|
|
#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
|
|
|
|
static unsigned long _find_next_bit(const unsigned long *addr,
|
|
unsigned long nbits, unsigned long start, unsigned long invert)
|
|
{
|
|
unsigned long tmp;
|
|
|
|
if (!nbits || start >= nbits)
|
|
return nbits;
|
|
|
|
tmp = addr[start / BITS_PER_LONG] ^ invert;
|
|
|
|
/* Handle 1st word. */
|
|
tmp &= BITMAP_FIRST_WORD_MASK(start);
|
|
start = round_down(start, BITS_PER_LONG);
|
|
|
|
while (!tmp) {
|
|
start += BITS_PER_LONG;
|
|
if (start >= nbits)
|
|
return nbits;
|
|
|
|
tmp = addr[start / BITS_PER_LONG] ^ invert;
|
|
}
|
|
|
|
return min(start + __ffs(tmp), nbits);
|
|
}
|
|
unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
|
|
unsigned long offset)
|
|
{
|
|
return _find_next_bit(addr, size, offset, 0UL);
|
|
}
|
|
int main()
|
|
{
|
|
unsigned long item=0;
|
|
item |= 1<<23;
|
|
item |= 1<<11;
|
|
printf("%lu\n", find_next_bit(&item, 32, 20));
|
|
return 0;
|
|
}
|
|
|