aspeed: add support for the SMC segment registers
[qemu/ar7.git] / util / bitmap.c
blob43ed0117204e3e0bd53a56487962469961b9c7db
1 /*
2 * Bitmap Module
4 * Stolen from linux/src/lib/bitmap.c
6 * Copyright (C) 2010 Corentin Chary
8 * This source code is licensed under the GNU General Public License,
9 * Version 2.
12 #include "qemu/osdep.h"
13 #include "qemu/bitops.h"
14 #include "qemu/bitmap.h"
15 #include "qemu/atomic.h"
18 * bitmaps provide an array of bits, implemented using an
19 * array of unsigned longs. The number of valid bits in a
20 * given bitmap does _not_ need to be an exact multiple of
21 * BITS_PER_LONG.
23 * The possible unused bits in the last, partially used word
24 * of a bitmap are 'don't care'. The implementation makes
25 * no particular effort to keep them zero. It ensures that
26 * their value will not affect the results of any operation.
27 * The bitmap operations that return Boolean (bitmap_empty,
28 * for example) or scalar (bitmap_weight, for example) results
29 * carefully filter out these unused bits from impacting their
30 * results.
32 * These operations actually hold to a slightly stronger rule:
33 * if you don't input any bitmaps to these ops that have some
34 * unused bits set, then they won't output any set unused bits
35 * in output bitmaps.
37 * The byte ordering of bitmaps is more natural on little
38 * endian architectures.
41 int slow_bitmap_empty(const unsigned long *bitmap, long bits)
43 long k, lim = bits/BITS_PER_LONG;
45 for (k = 0; k < lim; ++k) {
46 if (bitmap[k]) {
47 return 0;
50 if (bits % BITS_PER_LONG) {
51 if (bitmap[k] & BITMAP_LAST_WORD_MASK(bits)) {
52 return 0;
56 return 1;
59 int slow_bitmap_full(const unsigned long *bitmap, long bits)
61 long k, lim = bits/BITS_PER_LONG;
63 for (k = 0; k < lim; ++k) {
64 if (~bitmap[k]) {
65 return 0;
69 if (bits % BITS_PER_LONG) {
70 if (~bitmap[k] & BITMAP_LAST_WORD_MASK(bits)) {
71 return 0;
75 return 1;
78 int slow_bitmap_equal(const unsigned long *bitmap1,
79 const unsigned long *bitmap2, long bits)
81 long k, lim = bits/BITS_PER_LONG;
83 for (k = 0; k < lim; ++k) {
84 if (bitmap1[k] != bitmap2[k]) {
85 return 0;
89 if (bits % BITS_PER_LONG) {
90 if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits)) {
91 return 0;
95 return 1;
98 void slow_bitmap_complement(unsigned long *dst, const unsigned long *src,
99 long bits)
101 long k, lim = bits/BITS_PER_LONG;
103 for (k = 0; k < lim; ++k) {
104 dst[k] = ~src[k];
107 if (bits % BITS_PER_LONG) {
108 dst[k] = ~src[k] & BITMAP_LAST_WORD_MASK(bits);
112 int slow_bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
113 const unsigned long *bitmap2, long bits)
115 long k;
116 long nr = BITS_TO_LONGS(bits);
117 unsigned long result = 0;
119 for (k = 0; k < nr; k++) {
120 result |= (dst[k] = bitmap1[k] & bitmap2[k]);
122 return result != 0;
125 void slow_bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
126 const unsigned long *bitmap2, long bits)
128 long k;
129 long nr = BITS_TO_LONGS(bits);
131 for (k = 0; k < nr; k++) {
132 dst[k] = bitmap1[k] | bitmap2[k];
136 void slow_bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
137 const unsigned long *bitmap2, long bits)
139 long k;
140 long nr = BITS_TO_LONGS(bits);
142 for (k = 0; k < nr; k++) {
143 dst[k] = bitmap1[k] ^ bitmap2[k];
147 int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
148 const unsigned long *bitmap2, long bits)
150 long k;
151 long nr = BITS_TO_LONGS(bits);
152 unsigned long result = 0;
154 for (k = 0; k < nr; k++) {
155 result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
157 return result != 0;
160 void bitmap_set(unsigned long *map, long start, long nr)
162 unsigned long *p = map + BIT_WORD(start);
163 const long size = start + nr;
164 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
165 unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
167 while (nr - bits_to_set >= 0) {
168 *p |= mask_to_set;
169 nr -= bits_to_set;
170 bits_to_set = BITS_PER_LONG;
171 mask_to_set = ~0UL;
172 p++;
174 if (nr) {
175 mask_to_set &= BITMAP_LAST_WORD_MASK(size);
176 *p |= mask_to_set;
180 void bitmap_set_atomic(unsigned long *map, long start, long nr)
182 unsigned long *p = map + BIT_WORD(start);
183 const long size = start + nr;
184 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
185 unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
187 /* First word */
188 if (nr - bits_to_set > 0) {
189 atomic_or(p, mask_to_set);
190 nr -= bits_to_set;
191 bits_to_set = BITS_PER_LONG;
192 mask_to_set = ~0UL;
193 p++;
196 /* Full words */
197 if (bits_to_set == BITS_PER_LONG) {
198 while (nr >= BITS_PER_LONG) {
199 *p = ~0UL;
200 nr -= BITS_PER_LONG;
201 p++;
205 /* Last word */
206 if (nr) {
207 mask_to_set &= BITMAP_LAST_WORD_MASK(size);
208 atomic_or(p, mask_to_set);
209 } else {
210 /* If we avoided the full barrier in atomic_or(), issue a
211 * barrier to account for the assignments in the while loop.
213 smp_mb();
217 void bitmap_clear(unsigned long *map, long start, long nr)
219 unsigned long *p = map + BIT_WORD(start);
220 const long size = start + nr;
221 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
222 unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
224 while (nr - bits_to_clear >= 0) {
225 *p &= ~mask_to_clear;
226 nr -= bits_to_clear;
227 bits_to_clear = BITS_PER_LONG;
228 mask_to_clear = ~0UL;
229 p++;
231 if (nr) {
232 mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
233 *p &= ~mask_to_clear;
237 bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr)
239 unsigned long *p = map + BIT_WORD(start);
240 const long size = start + nr;
241 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
242 unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
243 unsigned long dirty = 0;
244 unsigned long old_bits;
246 /* First word */
247 if (nr - bits_to_clear > 0) {
248 old_bits = atomic_fetch_and(p, ~mask_to_clear);
249 dirty |= old_bits & mask_to_clear;
250 nr -= bits_to_clear;
251 bits_to_clear = BITS_PER_LONG;
252 mask_to_clear = ~0UL;
253 p++;
256 /* Full words */
257 if (bits_to_clear == BITS_PER_LONG) {
258 while (nr >= BITS_PER_LONG) {
259 if (*p) {
260 old_bits = atomic_xchg(p, 0);
261 dirty |= old_bits;
263 nr -= BITS_PER_LONG;
264 p++;
268 /* Last word */
269 if (nr) {
270 mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
271 old_bits = atomic_fetch_and(p, ~mask_to_clear);
272 dirty |= old_bits & mask_to_clear;
273 } else {
274 if (!dirty) {
275 smp_mb();
279 return dirty != 0;
282 #define ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
285 * bitmap_find_next_zero_area - find a contiguous aligned zero area
286 * @map: The address to base the search on
287 * @size: The bitmap size in bits
288 * @start: The bitnumber to start searching at
289 * @nr: The number of zeroed bits we're looking for
290 * @align_mask: Alignment mask for zero area
292 * The @align_mask should be one less than a power of 2; the effect is that
293 * the bit offset of all zero areas this function finds is multiples of that
294 * power of 2. A @align_mask of 0 means no alignment is required.
296 unsigned long bitmap_find_next_zero_area(unsigned long *map,
297 unsigned long size,
298 unsigned long start,
299 unsigned long nr,
300 unsigned long align_mask)
302 unsigned long index, end, i;
303 again:
304 index = find_next_zero_bit(map, size, start);
306 /* Align allocation */
307 index = ALIGN_MASK(index, align_mask);
309 end = index + nr;
310 if (end > size) {
311 return end;
313 i = find_next_bit(map, end, index);
314 if (i < end) {
315 start = i + 1;
316 goto again;
318 return index;
321 int slow_bitmap_intersects(const unsigned long *bitmap1,
322 const unsigned long *bitmap2, long bits)
324 long k, lim = bits/BITS_PER_LONG;
326 for (k = 0; k < lim; ++k) {
327 if (bitmap1[k] & bitmap2[k]) {
328 return 1;
332 if (bits % BITS_PER_LONG) {
333 if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits)) {
334 return 1;
337 return 0;