]> Git Repo - qemu.git/blob - include/qemu/bitops.h
Merge remote-tracking branch 'remotes/mjt/tags/pull-trivial-patches-2015-04-30' into...
[qemu.git] / include / qemu / bitops.h
1 /*
2  * Bitops Module
3  *
4  * Copyright (C) 2010 Corentin Chary <[email protected]>
5  *
6  * Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
7  *
8  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
9  * See the COPYING.LIB file in the top-level directory.
10  */
11
12 #ifndef BITOPS_H
13 #define BITOPS_H
14
15 #include <stdint.h>
16 #include <assert.h>
17
18 #include "host-utils.h"
19
20 #define BITS_PER_BYTE           CHAR_BIT
21 #define BITS_PER_LONG           (sizeof (unsigned long) * BITS_PER_BYTE)
22
23 #define BIT(nr)                 (1UL << (nr))
24 #define BIT_MASK(nr)            (1UL << ((nr) % BITS_PER_LONG))
25 #define BIT_WORD(nr)            ((nr) / BITS_PER_LONG)
26 #define BITS_TO_LONGS(nr)       DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
27
28 /**
29  * set_bit - Set a bit in memory
30  * @nr: the bit to set
31  * @addr: the address to start counting from
32  */
33 static inline void set_bit(long nr, unsigned long *addr)
34 {
35     unsigned long mask = BIT_MASK(nr);
36     unsigned long *p = addr + BIT_WORD(nr);
37
38     *p  |= mask;
39 }
40
41 /**
42  * clear_bit - Clears a bit in memory
43  * @nr: Bit to clear
44  * @addr: Address to start counting from
45  */
46 static inline void clear_bit(long nr, unsigned long *addr)
47 {
48     unsigned long mask = BIT_MASK(nr);
49     unsigned long *p = addr + BIT_WORD(nr);
50
51     *p &= ~mask;
52 }
53
54 /**
55  * change_bit - Toggle a bit in memory
56  * @nr: Bit to change
57  * @addr: Address to start counting from
58  */
59 static inline void change_bit(long nr, unsigned long *addr)
60 {
61     unsigned long mask = BIT_MASK(nr);
62     unsigned long *p = addr + BIT_WORD(nr);
63
64     *p ^= mask;
65 }
66
67 /**
68  * test_and_set_bit - Set a bit and return its old value
69  * @nr: Bit to set
70  * @addr: Address to count from
71  */
72 static inline int test_and_set_bit(long nr, unsigned long *addr)
73 {
74     unsigned long mask = BIT_MASK(nr);
75     unsigned long *p = addr + BIT_WORD(nr);
76     unsigned long old = *p;
77
78     *p = old | mask;
79     return (old & mask) != 0;
80 }
81
82 /**
83  * test_and_clear_bit - Clear a bit and return its old value
84  * @nr: Bit to clear
85  * @addr: Address to count from
86  */
87 static inline int test_and_clear_bit(long nr, unsigned long *addr)
88 {
89     unsigned long mask = BIT_MASK(nr);
90     unsigned long *p = addr + BIT_WORD(nr);
91     unsigned long old = *p;
92
93     *p = old & ~mask;
94     return (old & mask) != 0;
95 }
96
97 /**
98  * test_and_change_bit - Change a bit and return its old value
99  * @nr: Bit to change
100  * @addr: Address to count from
101  */
102 static inline int test_and_change_bit(long nr, unsigned long *addr)
103 {
104     unsigned long mask = BIT_MASK(nr);
105     unsigned long *p = addr + BIT_WORD(nr);
106     unsigned long old = *p;
107
108     *p = old ^ mask;
109     return (old & mask) != 0;
110 }
111
112 /**
113  * test_bit - Determine whether a bit is set
114  * @nr: bit number to test
115  * @addr: Address to start counting from
116  */
117 static inline int test_bit(long nr, const unsigned long *addr)
118 {
119     return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
120 }
121
122 /**
123  * find_last_bit - find the last set bit in a memory region
124  * @addr: The address to start the search at
125  * @size: The maximum size to search
126  *
127  * Returns the bit number of the first set bit, or size.
128  */
129 unsigned long find_last_bit(const unsigned long *addr,
130                             unsigned long size);
131
132 /**
133  * find_next_bit - find the next set bit in a memory region
134  * @addr: The address to base the search on
135  * @offset: The bitnumber to start searching at
136  * @size: The bitmap size in bits
137  */
138 unsigned long find_next_bit(const unsigned long *addr,
139                             unsigned long size,
140                             unsigned long offset);
141
142 /**
143  * find_next_zero_bit - find the next cleared bit in a memory region
144  * @addr: The address to base the search on
145  * @offset: The bitnumber to start searching at
146  * @size: The bitmap size in bits
147  */
148
149 unsigned long find_next_zero_bit(const unsigned long *addr,
150                                  unsigned long size,
151                                  unsigned long offset);
152
153 /**
154  * find_first_bit - find the first set bit in a memory region
155  * @addr: The address to start the search at
156  * @size: The maximum size to search
157  *
158  * Returns the bit number of the first set bit.
159  */
160 static inline unsigned long find_first_bit(const unsigned long *addr,
161                                            unsigned long size)
162 {
163     unsigned long result, tmp;
164
165     for (result = 0; result < size; result += BITS_PER_LONG) {
166         tmp = *addr++;
167         if (tmp) {
168             result += ctzl(tmp);
169             return result < size ? result : size;
170         }
171     }
172     /* Not found */
173     return size;
174 }
175
176 /**
177  * find_first_zero_bit - find the first cleared bit in a memory region
178  * @addr: The address to start the search at
179  * @size: The maximum size to search
180  *
181  * Returns the bit number of the first cleared bit.
182  */
183 static inline unsigned long find_first_zero_bit(const unsigned long *addr,
184                                                 unsigned long size)
185 {
186     return find_next_zero_bit(addr, size, 0);
187 }
188
189 static inline unsigned long hweight_long(unsigned long w)
190 {
191     unsigned long count;
192
193     for (count = 0; w; w >>= 1) {
194         count += w & 1;
195     }
196     return count;
197 }
198
199 /**
200  * rol8 - rotate an 8-bit value left
201  * @word: value to rotate
202  * @shift: bits to roll
203  */
204 static inline uint8_t rol8(uint8_t word, unsigned int shift)
205 {
206     return (word << shift) | (word >> (8 - shift));
207 }
208
209 /**
210  * ror8 - rotate an 8-bit value right
211  * @word: value to rotate
212  * @shift: bits to roll
213  */
214 static inline uint8_t ror8(uint8_t word, unsigned int shift)
215 {
216     return (word >> shift) | (word << (8 - shift));
217 }
218
219 /**
220  * rol16 - rotate a 16-bit value left
221  * @word: value to rotate
222  * @shift: bits to roll
223  */
224 static inline uint16_t rol16(uint16_t word, unsigned int shift)
225 {
226     return (word << shift) | (word >> (16 - shift));
227 }
228
229 /**
230  * ror16 - rotate a 16-bit value right
231  * @word: value to rotate
232  * @shift: bits to roll
233  */
234 static inline uint16_t ror16(uint16_t word, unsigned int shift)
235 {
236     return (word >> shift) | (word << (16 - shift));
237 }
238
239 /**
240  * rol32 - rotate a 32-bit value left
241  * @word: value to rotate
242  * @shift: bits to roll
243  */
244 static inline uint32_t rol32(uint32_t word, unsigned int shift)
245 {
246     return (word << shift) | (word >> (32 - shift));
247 }
248
249 /**
250  * ror32 - rotate a 32-bit value right
251  * @word: value to rotate
252  * @shift: bits to roll
253  */
254 static inline uint32_t ror32(uint32_t word, unsigned int shift)
255 {
256     return (word >> shift) | (word << (32 - shift));
257 }
258
259 /**
260  * rol64 - rotate a 64-bit value left
261  * @word: value to rotate
262  * @shift: bits to roll
263  */
264 static inline uint64_t rol64(uint64_t word, unsigned int shift)
265 {
266     return (word << shift) | (word >> (64 - shift));
267 }
268
269 /**
270  * ror64 - rotate a 64-bit value right
271  * @word: value to rotate
272  * @shift: bits to roll
273  */
274 static inline uint64_t ror64(uint64_t word, unsigned int shift)
275 {
276     return (word >> shift) | (word << (64 - shift));
277 }
278
279 /**
280  * extract32:
281  * @value: the value to extract the bit field from
282  * @start: the lowest bit in the bit field (numbered from 0)
283  * @length: the length of the bit field
284  *
285  * Extract from the 32 bit input @value the bit field specified by the
286  * @start and @length parameters, and return it. The bit field must
287  * lie entirely within the 32 bit word. It is valid to request that
288  * all 32 bits are returned (ie @length 32 and @start 0).
289  *
290  * Returns: the value of the bit field extracted from the input value.
291  */
292 static inline uint32_t extract32(uint32_t value, int start, int length)
293 {
294     assert(start >= 0 && length > 0 && length <= 32 - start);
295     return (value >> start) & (~0U >> (32 - length));
296 }
297
298 /**
299  * extract64:
300  * @value: the value to extract the bit field from
301  * @start: the lowest bit in the bit field (numbered from 0)
302  * @length: the length of the bit field
303  *
304  * Extract from the 64 bit input @value the bit field specified by the
305  * @start and @length parameters, and return it. The bit field must
306  * lie entirely within the 64 bit word. It is valid to request that
307  * all 64 bits are returned (ie @length 64 and @start 0).
308  *
309  * Returns: the value of the bit field extracted from the input value.
310  */
311 static inline uint64_t extract64(uint64_t value, int start, int length)
312 {
313     assert(start >= 0 && length > 0 && length <= 64 - start);
314     return (value >> start) & (~0ULL >> (64 - length));
315 }
316
317 /**
318  * sextract32:
319  * @value: the value to extract the bit field from
320  * @start: the lowest bit in the bit field (numbered from 0)
321  * @length: the length of the bit field
322  *
323  * Extract from the 32 bit input @value the bit field specified by the
324  * @start and @length parameters, and return it, sign extended to
325  * an int32_t (ie with the most significant bit of the field propagated
326  * to all the upper bits of the return value). The bit field must lie
327  * entirely within the 32 bit word. It is valid to request that
328  * all 32 bits are returned (ie @length 32 and @start 0).
329  *
330  * Returns: the sign extended value of the bit field extracted from the
331  * input value.
332  */
333 static inline int32_t sextract32(uint32_t value, int start, int length)
334 {
335     assert(start >= 0 && length > 0 && length <= 32 - start);
336     /* Note that this implementation relies on right shift of signed
337      * integers being an arithmetic shift.
338      */
339     return ((int32_t)(value << (32 - length - start))) >> (32 - length);
340 }
341
342 /**
343  * sextract64:
344  * @value: the value to extract the bit field from
345  * @start: the lowest bit in the bit field (numbered from 0)
346  * @length: the length of the bit field
347  *
348  * Extract from the 64 bit input @value the bit field specified by the
349  * @start and @length parameters, and return it, sign extended to
350  * an int64_t (ie with the most significant bit of the field propagated
351  * to all the upper bits of the return value). The bit field must lie
352  * entirely within the 64 bit word. It is valid to request that
353  * all 64 bits are returned (ie @length 64 and @start 0).
354  *
355  * Returns: the sign extended value of the bit field extracted from the
356  * input value.
357  */
358 static inline int64_t sextract64(uint64_t value, int start, int length)
359 {
360     assert(start >= 0 && length > 0 && length <= 64 - start);
361     /* Note that this implementation relies on right shift of signed
362      * integers being an arithmetic shift.
363      */
364     return ((int64_t)(value << (64 - length - start))) >> (64 - length);
365 }
366
367 /**
368  * deposit32:
369  * @value: initial value to insert bit field into
370  * @start: the lowest bit in the bit field (numbered from 0)
371  * @length: the length of the bit field
372  * @fieldval: the value to insert into the bit field
373  *
374  * Deposit @fieldval into the 32 bit @value at the bit field specified
375  * by the @start and @length parameters, and return the modified
376  * @value. Bits of @value outside the bit field are not modified.
377  * Bits of @fieldval above the least significant @length bits are
378  * ignored. The bit field must lie entirely within the 32 bit word.
379  * It is valid to request that all 32 bits are modified (ie @length
380  * 32 and @start 0).
381  *
382  * Returns: the modified @value.
383  */
384 static inline uint32_t deposit32(uint32_t value, int start, int length,
385                                  uint32_t fieldval)
386 {
387     uint32_t mask;
388     assert(start >= 0 && length > 0 && length <= 32 - start);
389     mask = (~0U >> (32 - length)) << start;
390     return (value & ~mask) | ((fieldval << start) & mask);
391 }
392
393 /**
394  * deposit64:
395  * @value: initial value to insert bit field into
396  * @start: the lowest bit in the bit field (numbered from 0)
397  * @length: the length of the bit field
398  * @fieldval: the value to insert into the bit field
399  *
400  * Deposit @fieldval into the 64 bit @value at the bit field specified
401  * by the @start and @length parameters, and return the modified
402  * @value. Bits of @value outside the bit field are not modified.
403  * Bits of @fieldval above the least significant @length bits are
404  * ignored. The bit field must lie entirely within the 64 bit word.
405  * It is valid to request that all 64 bits are modified (ie @length
406  * 64 and @start 0).
407  *
408  * Returns: the modified @value.
409  */
410 static inline uint64_t deposit64(uint64_t value, int start, int length,
411                                  uint64_t fieldval)
412 {
413     uint64_t mask;
414     assert(start >= 0 && length > 0 && length <= 64 - start);
415     mask = (~0ULL >> (64 - length)) << start;
416     return (value & ~mask) | ((fieldval << start) & mask);
417 }
418
419 #endif
This page took 0.050127 seconds and 4 git commands to generate.