]> Git Repo - qemu.git/blob - include/qemu/bitops.h
Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[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, unsigned long offset);
140
141 /**
142  * find_next_zero_bit - find the next cleared bit in a memory region
143  * @addr: The address to base the search on
144  * @offset: The bitnumber to start searching at
145  * @size: The bitmap size in bits
146  */
147
148 unsigned long find_next_zero_bit(const unsigned long *addr,
149                                  unsigned long size,
150                                  unsigned long offset);
151
152 /**
153  * find_first_bit - find the first set bit in a memory region
154  * @addr: The address to start the search at
155  * @size: The maximum size to search
156  *
157  * Returns the bit number of the first set bit.
158  */
159 static inline unsigned long find_first_bit(const unsigned long *addr,
160                                            unsigned long size)
161 {
162     unsigned long result, tmp;
163
164     for (result = 0; result < size; result += BITS_PER_LONG) {
165         tmp = *addr++;
166         if (tmp) {
167             result += ctzl(tmp);
168             return result < size ? result : size;
169         }
170     }
171     /* Not found */
172     return size;
173 }
174
175 /**
176  * find_first_zero_bit - find the first cleared bit in a memory region
177  * @addr: The address to start the search at
178  * @size: The maximum size to search
179  *
180  * Returns the bit number of the first cleared bit.
181  */
182 static inline unsigned long find_first_zero_bit(const unsigned long *addr,
183                                                 unsigned long size)
184 {
185     return find_next_zero_bit(addr, size, 0);
186 }
187
188 static inline unsigned long hweight_long(unsigned long w)
189 {
190     unsigned long count;
191
192     for (count = 0; w; w >>= 1) {
193         count += w & 1;
194     }
195     return count;
196 }
197
198 /**
199  * rol8 - rotate an 8-bit value left
200  * @word: value to rotate
201  * @shift: bits to roll
202  */
203 static inline uint8_t rol8(uint8_t word, unsigned int shift)
204 {
205     return (word << shift) | (word >> (8 - shift));
206 }
207
208 /**
209  * ror8 - rotate an 8-bit value right
210  * @word: value to rotate
211  * @shift: bits to roll
212  */
213 static inline uint8_t ror8(uint8_t word, unsigned int shift)
214 {
215     return (word >> shift) | (word << (8 - shift));
216 }
217
218 /**
219  * rol16 - rotate a 16-bit value left
220  * @word: value to rotate
221  * @shift: bits to roll
222  */
223 static inline uint16_t rol16(uint16_t word, unsigned int shift)
224 {
225     return (word << shift) | (word >> (16 - shift));
226 }
227
228 /**
229  * ror16 - rotate a 16-bit value right
230  * @word: value to rotate
231  * @shift: bits to roll
232  */
233 static inline uint16_t ror16(uint16_t word, unsigned int shift)
234 {
235     return (word >> shift) | (word << (16 - shift));
236 }
237
238 /**
239  * rol32 - rotate a 32-bit value left
240  * @word: value to rotate
241  * @shift: bits to roll
242  */
243 static inline uint32_t rol32(uint32_t word, unsigned int shift)
244 {
245     return (word << shift) | (word >> (32 - shift));
246 }
247
248 /**
249  * ror32 - rotate a 32-bit value right
250  * @word: value to rotate
251  * @shift: bits to roll
252  */
253 static inline uint32_t ror32(uint32_t word, unsigned int shift)
254 {
255     return (word >> shift) | (word << (32 - shift));
256 }
257
258 /**
259  * rol64 - rotate a 64-bit value left
260  * @word: value to rotate
261  * @shift: bits to roll
262  */
263 static inline uint64_t rol64(uint64_t word, unsigned int shift)
264 {
265     return (word << shift) | (word >> (64 - shift));
266 }
267
268 /**
269  * ror64 - rotate a 64-bit value right
270  * @word: value to rotate
271  * @shift: bits to roll
272  */
273 static inline uint64_t ror64(uint64_t word, unsigned int shift)
274 {
275     return (word >> shift) | (word << (64 - shift));
276 }
277
278 /**
279  * extract32:
280  * @value: the value to extract the bit field from
281  * @start: the lowest bit in the bit field (numbered from 0)
282  * @length: the length of the bit field
283  *
284  * Extract from the 32 bit input @value the bit field specified by the
285  * @start and @length parameters, and return it. The bit field must
286  * lie entirely within the 32 bit word. It is valid to request that
287  * all 32 bits are returned (ie @length 32 and @start 0).
288  *
289  * Returns: the value of the bit field extracted from the input value.
290  */
291 static inline uint32_t extract32(uint32_t value, int start, int length)
292 {
293     assert(start >= 0 && length > 0 && length <= 32 - start);
294     return (value >> start) & (~0U >> (32 - length));
295 }
296
297 /**
298  * extract64:
299  * @value: the value to extract the bit field from
300  * @start: the lowest bit in the bit field (numbered from 0)
301  * @length: the length of the bit field
302  *
303  * Extract from the 64 bit input @value the bit field specified by the
304  * @start and @length parameters, and return it. The bit field must
305  * lie entirely within the 64 bit word. It is valid to request that
306  * all 64 bits are returned (ie @length 64 and @start 0).
307  *
308  * Returns: the value of the bit field extracted from the input value.
309  */
310 static inline uint64_t extract64(uint64_t value, int start, int length)
311 {
312     assert(start >= 0 && length > 0 && length <= 64 - start);
313     return (value >> start) & (~0ULL >> (64 - length));
314 }
315
316 /**
317  * sextract32:
318  * @value: the value to extract the bit field from
319  * @start: the lowest bit in the bit field (numbered from 0)
320  * @length: the length of the bit field
321  *
322  * Extract from the 32 bit input @value the bit field specified by the
323  * @start and @length parameters, and return it, sign extended to
324  * an int32_t (ie with the most significant bit of the field propagated
325  * to all the upper bits of the return value). The bit field must lie
326  * entirely within the 32 bit word. It is valid to request that
327  * all 32 bits are returned (ie @length 32 and @start 0).
328  *
329  * Returns: the sign extended value of the bit field extracted from the
330  * input value.
331  */
332 static inline int32_t sextract32(uint32_t value, int start, int length)
333 {
334     assert(start >= 0 && length > 0 && length <= 32 - start);
335     /* Note that this implementation relies on right shift of signed
336      * integers being an arithmetic shift.
337      */
338     return ((int32_t)(value << (32 - length - start))) >> (32 - length);
339 }
340
341 /**
342  * sextract64:
343  * @value: the value to extract the bit field from
344  * @start: the lowest bit in the bit field (numbered from 0)
345  * @length: the length of the bit field
346  *
347  * Extract from the 64 bit input @value the bit field specified by the
348  * @start and @length parameters, and return it, sign extended to
349  * an int64_t (ie with the most significant bit of the field propagated
350  * to all the upper bits of the return value). The bit field must lie
351  * entirely within the 64 bit word. It is valid to request that
352  * all 64 bits are returned (ie @length 64 and @start 0).
353  *
354  * Returns: the sign extended value of the bit field extracted from the
355  * input value.
356  */
357 static inline int64_t sextract64(uint64_t value, int start, int length)
358 {
359     assert(start >= 0 && length > 0 && length <= 64 - start);
360     /* Note that this implementation relies on right shift of signed
361      * integers being an arithmetic shift.
362      */
363     return ((int64_t)(value << (64 - length - start))) >> (64 - length);
364 }
365
366 /**
367  * deposit32:
368  * @value: initial value to insert bit field into
369  * @start: the lowest bit in the bit field (numbered from 0)
370  * @length: the length of the bit field
371  * @fieldval: the value to insert into the bit field
372  *
373  * Deposit @fieldval into the 32 bit @value at the bit field specified
374  * by the @start and @length parameters, and return the modified
375  * @value. Bits of @value outside the bit field are not modified.
376  * Bits of @fieldval above the least significant @length bits are
377  * ignored. The bit field must lie entirely within the 32 bit word.
378  * It is valid to request that all 32 bits are modified (ie @length
379  * 32 and @start 0).
380  *
381  * Returns: the modified @value.
382  */
383 static inline uint32_t deposit32(uint32_t value, int start, int length,
384                                  uint32_t fieldval)
385 {
386     uint32_t mask;
387     assert(start >= 0 && length > 0 && length <= 32 - start);
388     mask = (~0U >> (32 - length)) << start;
389     return (value & ~mask) | ((fieldval << start) & mask);
390 }
391
392 /**
393  * deposit64:
394  * @value: initial value to insert bit field into
395  * @start: the lowest bit in the bit field (numbered from 0)
396  * @length: the length of the bit field
397  * @fieldval: the value to insert into the bit field
398  *
399  * Deposit @fieldval into the 64 bit @value at the bit field specified
400  * by the @start and @length parameters, and return the modified
401  * @value. Bits of @value outside the bit field are not modified.
402  * Bits of @fieldval above the least significant @length bits are
403  * ignored. The bit field must lie entirely within the 64 bit word.
404  * It is valid to request that all 64 bits are modified (ie @length
405  * 64 and @start 0).
406  *
407  * Returns: the modified @value.
408  */
409 static inline uint64_t deposit64(uint64_t value, int start, int length,
410                                  uint64_t fieldval)
411 {
412     uint64_t mask;
413     assert(start >= 0 && length > 0 && length <= 64 - start);
414     mask = (~0ULL >> (64 - length)) << start;
415     return (value & ~mask) | ((fieldval << start) & mask);
416 }
417
418 #endif
This page took 0.047295 seconds and 4 git commands to generate.