]> git.proxmox.com Git - qemu.git/blame - include/qemu/bitops.h
osdep.h: include sys/types.h for ssize_t definition
[qemu.git] / include / qemu / bitops.h
CommitLineData
e0e53b2f
CC
1/*
2 * Bitops Module
3 *
4 * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
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 "qemu-common.h"
fbeadf50 16#include "host-utils.h"
e0e53b2f
CC
17
18#define BITS_PER_BYTE CHAR_BIT
19#define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
20
21#define BIT(nr) (1UL << (nr))
22#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
23#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
24#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
25
e0e53b2f
CC
26/**
27 * set_bit - Set a bit in memory
28 * @nr: the bit to set
29 * @addr: the address to start counting from
30 */
17a4ed8a 31static inline void set_bit(int nr, unsigned long *addr)
e0e53b2f
CC
32{
33 unsigned long mask = BIT_MASK(nr);
17a4ed8a 34 unsigned long *p = addr + BIT_WORD(nr);
e0e53b2f
CC
35
36 *p |= mask;
37}
38
39/**
40 * clear_bit - Clears a bit in memory
41 * @nr: Bit to clear
42 * @addr: Address to start counting from
43 */
17a4ed8a 44static inline void clear_bit(int nr, unsigned long *addr)
e0e53b2f
CC
45{
46 unsigned long mask = BIT_MASK(nr);
17a4ed8a 47 unsigned long *p = addr + BIT_WORD(nr);
e0e53b2f
CC
48
49 *p &= ~mask;
50}
51
52/**
53 * change_bit - Toggle a bit in memory
54 * @nr: Bit to change
55 * @addr: Address to start counting from
56 */
17a4ed8a 57static inline void change_bit(int nr, unsigned long *addr)
e0e53b2f
CC
58{
59 unsigned long mask = BIT_MASK(nr);
17a4ed8a 60 unsigned long *p = addr + BIT_WORD(nr);
e0e53b2f
CC
61
62 *p ^= mask;
63}
64
65/**
66 * test_and_set_bit - Set a bit and return its old value
67 * @nr: Bit to set
68 * @addr: Address to count from
69 */
17a4ed8a 70static inline int test_and_set_bit(int nr, unsigned long *addr)
e0e53b2f
CC
71{
72 unsigned long mask = BIT_MASK(nr);
17a4ed8a 73 unsigned long *p = addr + BIT_WORD(nr);
e0e53b2f
CC
74 unsigned long old = *p;
75
76 *p = old | mask;
77 return (old & mask) != 0;
78}
79
80/**
81 * test_and_clear_bit - Clear a bit and return its old value
82 * @nr: Bit to clear
83 * @addr: Address to count from
84 */
17a4ed8a 85static inline int test_and_clear_bit(int nr, unsigned long *addr)
e0e53b2f
CC
86{
87 unsigned long mask = BIT_MASK(nr);
17a4ed8a 88 unsigned long *p = addr + BIT_WORD(nr);
e0e53b2f
CC
89 unsigned long old = *p;
90
91 *p = old & ~mask;
92 return (old & mask) != 0;
93}
94
95/**
96 * test_and_change_bit - Change a bit and return its old value
97 * @nr: Bit to change
98 * @addr: Address to count from
99 */
17a4ed8a 100static inline int test_and_change_bit(int nr, unsigned long *addr)
e0e53b2f
CC
101{
102 unsigned long mask = BIT_MASK(nr);
17a4ed8a 103 unsigned long *p = addr + BIT_WORD(nr);
04483e15 104 unsigned long old = *p;
e0e53b2f
CC
105
106 *p = old ^ mask;
107 return (old & mask) != 0;
108}
109
110/**
111 * test_bit - Determine whether a bit is set
112 * @nr: bit number to test
113 * @addr: Address to start counting from
114 */
17a4ed8a 115static inline int test_bit(int nr, const unsigned long *addr)
e0e53b2f
CC
116{
117 return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
118}
119
120/**
121 * find_last_bit - find the last set bit in a memory region
122 * @addr: The address to start the search at
123 * @size: The maximum size to search
124 *
125 * Returns the bit number of the first set bit, or size.
126 */
127unsigned long find_last_bit(const unsigned long *addr,
128 unsigned long size);
129
130/**
131 * find_next_bit - find the next set bit in a memory region
132 * @addr: The address to base the search on
133 * @offset: The bitnumber to start searching at
134 * @size: The bitmap size in bits
135 */
136unsigned long find_next_bit(const unsigned long *addr,
137 unsigned long size, unsigned long offset);
138
139/**
140 * find_next_zero_bit - find the next cleared bit in a memory region
141 * @addr: The address to base the search on
142 * @offset: The bitnumber to start searching at
143 * @size: The bitmap size in bits
144 */
145
146unsigned long find_next_zero_bit(const unsigned long *addr,
147 unsigned long size,
148 unsigned long offset);
149
150/**
151 * find_first_bit - find the first set bit in a memory region
152 * @addr: The address to start the search at
153 * @size: The maximum size to search
154 *
155 * Returns the bit number of the first set bit.
156 */
157static inline unsigned long find_first_bit(const unsigned long *addr,
158 unsigned long size)
159{
160 return find_next_bit(addr, size, 0);
161}
162
163/**
164 * find_first_zero_bit - find the first cleared bit in a memory region
165 * @addr: The address to start the search at
166 * @size: The maximum size to search
167 *
168 * Returns the bit number of the first cleared bit.
169 */
170static inline unsigned long find_first_zero_bit(const unsigned long *addr,
171 unsigned long size)
172{
173 return find_next_zero_bit(addr, size, 0);
174}
175
176static inline unsigned long hweight_long(unsigned long w)
177{
178 unsigned long count;
179
180 for (count = 0; w; w >>= 1) {
181 count += w & 1;
182 }
183 return count;
184}
185
84988cf9
PM
186/**
187 * extract32:
188 * @value: the value to extract the bit field from
189 * @start: the lowest bit in the bit field (numbered from 0)
190 * @length: the length of the bit field
191 *
192 * Extract from the 32 bit input @value the bit field specified by the
193 * @start and @length parameters, and return it. The bit field must
194 * lie entirely within the 32 bit word. It is valid to request that
195 * all 32 bits are returned (ie @length 32 and @start 0).
196 *
197 * Returns: the value of the bit field extracted from the input value.
198 */
199static inline uint32_t extract32(uint32_t value, int start, int length)
200{
201 assert(start >= 0 && length > 0 && length <= 32 - start);
202 return (value >> start) & (~0U >> (32 - length));
203}
204
205/**
206 * extract64:
207 * @value: the value to extract the bit field from
208 * @start: the lowest bit in the bit field (numbered from 0)
209 * @length: the length of the bit field
210 *
211 * Extract from the 64 bit input @value the bit field specified by the
212 * @start and @length parameters, and return it. The bit field must
213 * lie entirely within the 64 bit word. It is valid to request that
214 * all 64 bits are returned (ie @length 64 and @start 0).
215 *
216 * Returns: the value of the bit field extracted from the input value.
217 */
218static inline uint64_t extract64(uint64_t value, int start, int length)
219{
220 assert(start >= 0 && length > 0 && length <= 64 - start);
221 return (value >> start) & (~0ULL >> (64 - length));
222}
223
224/**
225 * deposit32:
226 * @value: initial value to insert bit field into
227 * @start: the lowest bit in the bit field (numbered from 0)
228 * @length: the length of the bit field
229 * @fieldval: the value to insert into the bit field
230 *
231 * Deposit @fieldval into the 32 bit @value at the bit field specified
232 * by the @start and @length parameters, and return the modified
233 * @value. Bits of @value outside the bit field are not modified.
234 * Bits of @fieldval above the least significant @length bits are
235 * ignored. The bit field must lie entirely within the 32 bit word.
ab411770
SW
236 * It is valid to request that all 32 bits are modified (ie @length
237 * 32 and @start 0).
84988cf9
PM
238 *
239 * Returns: the modified @value.
240 */
241static inline uint32_t deposit32(uint32_t value, int start, int length,
242 uint32_t fieldval)
243{
244 uint32_t mask;
245 assert(start >= 0 && length > 0 && length <= 32 - start);
246 mask = (~0U >> (32 - length)) << start;
247 return (value & ~mask) | ((fieldval << start) & mask);
248}
249
250/**
ab411770 251 * deposit64:
84988cf9
PM
252 * @value: initial value to insert bit field into
253 * @start: the lowest bit in the bit field (numbered from 0)
254 * @length: the length of the bit field
255 * @fieldval: the value to insert into the bit field
256 *
257 * Deposit @fieldval into the 64 bit @value at the bit field specified
258 * by the @start and @length parameters, and return the modified
259 * @value. Bits of @value outside the bit field are not modified.
260 * Bits of @fieldval above the least significant @length bits are
ab411770 261 * ignored. The bit field must lie entirely within the 64 bit word.
84988cf9
PM
262 * It is valid to request that all 64 bits are modified (ie @length
263 * 64 and @start 0).
264 *
265 * Returns: the modified @value.
266 */
267static inline uint64_t deposit64(uint64_t value, int start, int length,
268 uint64_t fieldval)
269{
270 uint64_t mask;
271 assert(start >= 0 && length > 0 && length <= 64 - start);
272 mask = (~0ULL >> (64 - length)) << start;
273 return (value & ~mask) | ((fieldval << start) & mask);
274}
275
e0e53b2f 276#endif