Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
422 | giacomo | 1 | #ifndef __LINUX_BITMAP_H |
2 | #define __LINUX_BITMAP_H |
||
3 | |||
4 | #ifndef __ASSEMBLY__ |
||
5 | |||
6 | #include <linux/config.h> |
||
7 | #include <linux/compiler.h> |
||
8 | #include <linux/types.h> |
||
9 | #include <linux/kernel.h> |
||
10 | #include <linux/bitops.h> |
||
11 | #include <linux/string.h> |
||
12 | |||
13 | static inline int bitmap_empty(const unsigned long *bitmap, int bits) |
||
14 | { |
||
15 | int k, lim = bits/BITS_PER_LONG; |
||
16 | for (k = 0; k < lim; ++k) |
||
17 | if (bitmap[k]) |
||
18 | return 0; |
||
19 | |||
20 | if (bits % BITS_PER_LONG) |
||
21 | if (bitmap[k] & ((1UL << (bits % BITS_PER_LONG)) - 1)) |
||
22 | return 0; |
||
23 | |||
24 | return 1; |
||
25 | } |
||
26 | |||
27 | static inline int bitmap_full(const unsigned long *bitmap, int bits) |
||
28 | { |
||
29 | int k, lim = bits/BITS_PER_LONG; |
||
30 | for (k = 0; k < lim; ++k) |
||
31 | if (~bitmap[k]) |
||
32 | return 0; |
||
33 | |||
34 | if (bits % BITS_PER_LONG) |
||
35 | if (~bitmap[k] & ((1UL << (bits % BITS_PER_LONG)) - 1)) |
||
36 | return 0; |
||
37 | |||
38 | return 1; |
||
39 | } |
||
40 | |||
41 | static inline int bitmap_equal(const unsigned long *bitmap1, |
||
42 | unsigned long *bitmap2, int bits) |
||
43 | { |
||
44 | int k, lim = bits/BITS_PER_LONG;; |
||
45 | for (k = 0; k < lim; ++k) |
||
46 | if (bitmap1[k] != bitmap2[k]) |
||
47 | return 0; |
||
48 | |||
49 | if (bits % BITS_PER_LONG) |
||
50 | if ((bitmap1[k] ^ bitmap2[k]) & |
||
51 | ((1UL << (bits % BITS_PER_LONG)) - 1)) |
||
52 | return 0; |
||
53 | |||
54 | return 1; |
||
55 | } |
||
56 | |||
57 | static inline void bitmap_complement(unsigned long *bitmap, int bits) |
||
58 | { |
||
59 | int k; |
||
60 | |||
61 | for (k = 0; k < BITS_TO_LONGS(bits); ++k) |
||
62 | bitmap[k] = ~bitmap[k]; |
||
63 | } |
||
64 | |||
65 | static inline void bitmap_clear(unsigned long *bitmap, int bits) |
||
66 | { |
||
67 | CLEAR_BITMAP((unsigned long *)bitmap, bits); |
||
68 | } |
||
69 | |||
70 | static inline void bitmap_fill(unsigned long *bitmap, int bits) |
||
71 | { |
||
72 | memset(bitmap, 0xff, BITS_TO_LONGS(bits)*sizeof(unsigned long)); |
||
73 | } |
||
74 | |||
75 | static inline void bitmap_copy(unsigned long *dst, |
||
76 | const unsigned long *src, int bits) |
||
77 | { |
||
78 | memcpy(dst, src, BITS_TO_LONGS(bits)*sizeof(unsigned long)); |
||
79 | } |
||
80 | |||
81 | static inline void bitmap_shift_right(unsigned long *dst, |
||
82 | const unsigned long *src, int shift, int bits) |
||
83 | { |
||
84 | int k; |
||
85 | DECLARE_BITMAP(__shr_tmp, bits); |
||
86 | |||
87 | bitmap_clear(__shr_tmp, bits); |
||
88 | for (k = 0; k < bits - shift; ++k) |
||
89 | if (test_bit(k + shift, src)) |
||
90 | set_bit(k, __shr_tmp); |
||
91 | bitmap_copy(dst, __shr_tmp, bits); |
||
92 | } |
||
93 | |||
94 | static inline void bitmap_shift_left(unsigned long *dst, |
||
95 | const unsigned long *src, int shift, int bits) |
||
96 | { |
||
97 | int k; |
||
98 | DECLARE_BITMAP(__shl_tmp, bits); |
||
99 | |||
100 | bitmap_clear(__shl_tmp, bits); |
||
101 | for (k = bits; k >= shift; --k) |
||
102 | if (test_bit(k - shift, src)) |
||
103 | set_bit(k, __shl_tmp); |
||
104 | bitmap_copy(dst, __shl_tmp, bits); |
||
105 | } |
||
106 | |||
107 | static inline void bitmap_and(unsigned long *dst, const unsigned long *bitmap1, |
||
108 | const unsigned long *bitmap2, int bits) |
||
109 | { |
||
110 | int k; |
||
111 | int nr = BITS_TO_LONGS(bits); |
||
112 | |||
113 | for (k = 0; k < nr; k++) |
||
114 | dst[k] = bitmap1[k] & bitmap2[k]; |
||
115 | } |
||
116 | |||
117 | static inline void bitmap_or(unsigned long *dst, const unsigned long *bitmap1, |
||
118 | const unsigned long *bitmap2, int bits) |
||
119 | { |
||
120 | int k; |
||
121 | int nr = BITS_TO_LONGS(bits); |
||
122 | |||
123 | for (k = 0; k < nr; k++) |
||
124 | dst[k] = bitmap1[k] | bitmap2[k]; |
||
125 | } |
||
126 | |||
127 | #if BITS_PER_LONG == 32 |
||
128 | static inline int bitmap_weight(const unsigned long *bitmap, int bits) |
||
129 | { |
||
130 | int k, w = 0, lim = bits/BITS_PER_LONG; |
||
131 | |||
132 | for (k = 0; k < lim; k++) |
||
133 | w += hweight32(bitmap[k]); |
||
134 | |||
135 | if (bits % BITS_PER_LONG) |
||
136 | w += hweight32(bitmap[k] & |
||
137 | ((1UL << (bits % BITS_PER_LONG)) - 1)); |
||
138 | |||
139 | return w; |
||
140 | } |
||
141 | #else |
||
142 | static inline int bitmap_weight(const unsigned long *bitmap, int bits) |
||
143 | { |
||
144 | int k, w = 0, lim = bits/BITS_PER_LONG; |
||
145 | |||
146 | for (k = 0; k < lim; k++) |
||
147 | w += hweight64(bitmap[k]); |
||
148 | |||
149 | if (bits % BITS_PER_LONG) |
||
150 | w += hweight64(bitmap[k] & |
||
151 | ((1UL << (bits % BITS_PER_LONG)) - 1)); |
||
152 | |||
153 | return w; |
||
154 | } |
||
155 | #endif |
||
156 | |||
157 | #endif /* __ASSEMBLY__ */ |
||
158 | |||
159 | #endif /* __LINUX_BITMAP_H */ |