Skip to content

Commit 3027e1c

Browse files
author
Linus Torvalds
committed
Add "bitmap.h" for some simple bitmap ops
1 parent 90cb7bd commit 3027e1c

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ AR=ar
66
PREFIX=$(HOME)
77
PROGRAMS=test-lexing test-parsing obfuscate check compile test-linearize
88

9-
LIB_H= token.h parse.h lib.h symbol.h scope.h expression.h target.h linearize.h
9+
LIB_H= token.h parse.h lib.h symbol.h scope.h expression.h target.h linearize.h bitmap.h
1010

1111
LIB_OBJS= target.o parse.o tokenize.o pre-process.o symbol.o lib.o scope.o \
1212
expression.o show-parse.o evaluate.o expand.o inline.o linearize.o

bitmap.h

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#ifndef BITMAP_H
2+
#define BITMAP_H
3+
4+
#define BITS_IN_LONG (sizeof(unsigned long)*8)
5+
#define LONGS(x) ((x + BITS_IN_LONG - 1) & -BITS_IN_LONG)
6+
7+
/* Every bitmap gets its own type */
8+
#define DECLARE_BITMAP(name, x) unsigned long name[LONGS(x)]
9+
10+
static inline int test_bit(unsigned int nr, unsigned long *bitmap)
11+
{
12+
unsigned long offset = nr / BITS_IN_LONG;
13+
unsigned long bit = nr & (BITS_IN_LONG-1);
14+
return (bitmap[offset] >> bit) & 1;
15+
}
16+
17+
static inline void set_bit(unsigned int nr, unsigned long *bitmap)
18+
{
19+
unsigned long offset = nr / BITS_IN_LONG;
20+
unsigned long bit = nr & (BITS_IN_LONG-1);
21+
bitmap[offset] |= 1UL << bit;
22+
}
23+
24+
static inline void clear_bit(unsigned int nr, unsigned long *bitmap)
25+
{
26+
unsigned long offset = nr / BITS_IN_LONG;
27+
unsigned long bit = nr & (BITS_IN_LONG-1);
28+
bitmap[offset] &= ~(1UL << bit);
29+
}
30+
31+
static inline int test_and_set_bit(unsigned int nr, unsigned long *bitmap)
32+
{
33+
unsigned long offset = nr / BITS_IN_LONG;
34+
unsigned long bit = nr & (BITS_IN_LONG-1);
35+
unsigned long old = bitmap[offset];
36+
unsigned long mask = 1UL << bit;
37+
bitmap[offset] = old | mask;
38+
return (old & mask) != 0;
39+
}
40+
41+
static inline int test_and_clear_bit(unsigned int nr, unsigned long *bitmap)
42+
{
43+
unsigned long offset = nr / BITS_IN_LONG;
44+
unsigned long bit = nr & (BITS_IN_LONG-1);
45+
unsigned long old = bitmap[offset];
46+
unsigned long mask = 1UL << bit;
47+
bitmap[offset] = old & ~mask;
48+
return (old & mask) != 0;
49+
}
50+
51+
#endif /* BITMAP_H */

0 commit comments

Comments
 (0)