1 #ifndef _I386_BITOPS_H
2 #define _I386_BITOPS_H
3 
4 /*
5  * Copyright 1992, Linus Torvalds.
6  */
7 
8 /*
9  * These have to be done with inline assembly: that way the bit-setting
10  * is guaranteed to be atomic. All bit operations return 0 if the bit
11  * was cleared before the operation and != 0 if it was not.
12  *
13  * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
14  */
15 
16 #include <asm-generic/bitops/fls.h>
17 #include <asm-generic/bitops/__fls.h>
18 #include <asm-generic/bitops/fls64.h>
19 
20 #ifdef CONFIG_SMP
21 #define LOCK_PREFIX "lock ; "
22 #else
23 #define LOCK_PREFIX ""
24 #endif
25 
26 #define ADDR (*(volatile long *) addr)
27 
28 /**
29  * set_bit - Atomically set a bit in memory
30  * @nr: the bit to set
31  * @addr: the address to start counting from
32  *
33  * This function is atomic and may not be reordered.  See __set_bit()
34  * if you do not require the atomic guarantees.
35  * Note that @nr may be almost arbitrarily large; this function is not
36  * restricted to acting on a single-word quantity.
37  */
set_bit(int nr,volatile void * addr)38 static __inline__ void set_bit(int nr, volatile void * addr)
39 {
40 	__asm__ __volatile__( LOCK_PREFIX
41 		"btsl %1,%0"
42 		:"=m" (ADDR)
43 		:"Ir" (nr));
44 }
45 
46 /**
47  * __set_bit - Set a bit in memory
48  * @nr: the bit to set
49  * @addr: the address to start counting from
50  *
51  * Unlike set_bit(), this function is non-atomic and may be reordered.
52  * If it's called on the same region of memory simultaneously, the effect
53  * may be that only one operation succeeds.
54  */
__set_bit(int nr,volatile void * addr)55 static __inline__ void __set_bit(int nr, volatile void * addr)
56 {
57 	__asm__(
58 		"btsl %1,%0"
59 		:"=m" (ADDR)
60 		:"Ir" (nr));
61 }
62 
63 #define PLATFORM__SET_BIT
64 
65 /**
66  * clear_bit - Clears a bit in memory
67  * @nr: Bit to clear
68  * @addr: Address to start counting from
69  *
70  * clear_bit() is atomic and may not be reordered.  However, it does
71  * not contain a memory barrier, so if it is used for locking purposes,
72  * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
73  * in order to ensure changes are visible on other processors.
74  */
clear_bit(int nr,volatile void * addr)75 static __inline__ void clear_bit(int nr, volatile void * addr)
76 {
77 	__asm__ __volatile__( LOCK_PREFIX
78 		"btrl %1,%0"
79 		:"=m" (ADDR)
80 		:"Ir" (nr));
81 }
82 #define smp_mb__before_clear_bit()	barrier()
83 #define smp_mb__after_clear_bit()	barrier()
84 
85 /**
86  * __change_bit - Toggle a bit in memory
87  * @nr: the bit to set
88  * @addr: the address to start counting from
89  *
90  * Unlike change_bit(), this function is non-atomic and may be reordered.
91  * If it's called on the same region of memory simultaneously, the effect
92  * may be that only one operation succeeds.
93  */
__change_bit(int nr,volatile void * addr)94 static __inline__ void __change_bit(int nr, volatile void * addr)
95 {
96 	__asm__ __volatile__(
97 		"btcl %1,%0"
98 		:"=m" (ADDR)
99 		:"Ir" (nr));
100 }
101 
102 /**
103  * change_bit - Toggle a bit in memory
104  * @nr: Bit to clear
105  * @addr: Address to start counting from
106  *
107  * change_bit() is atomic and may not be reordered.
108  * Note that @nr may be almost arbitrarily large; this function is not
109  * restricted to acting on a single-word quantity.
110  */
change_bit(int nr,volatile void * addr)111 static __inline__ void change_bit(int nr, volatile void * addr)
112 {
113 	__asm__ __volatile__( LOCK_PREFIX
114 		"btcl %1,%0"
115 		:"=m" (ADDR)
116 		:"Ir" (nr));
117 }
118 
119 /**
120  * test_and_set_bit - Set a bit and return its old value
121  * @nr: Bit to set
122  * @addr: Address to count from
123  *
124  * This operation is atomic and cannot be reordered.
125  * It also implies a memory barrier.
126  */
test_and_set_bit(int nr,volatile void * addr)127 static __inline__ int test_and_set_bit(int nr, volatile void * addr)
128 {
129 	int oldbit;
130 
131 	__asm__ __volatile__( LOCK_PREFIX
132 		"btsl %2,%1\n\tsbbl %0,%0"
133 		:"=r" (oldbit),"=m" (ADDR)
134 		:"Ir" (nr) : "memory");
135 	return oldbit;
136 }
137 
138 /**
139  * __test_and_set_bit - Set a bit and return its old value
140  * @nr: Bit to set
141  * @addr: Address to count from
142  *
143  * This operation is non-atomic and can be reordered.
144  * If two examples of this operation race, one can appear to succeed
145  * but actually fail.  You must protect multiple accesses with a lock.
146  */
__test_and_set_bit(int nr,volatile void * addr)147 static __inline__ int __test_and_set_bit(int nr, volatile void * addr)
148 {
149 	int oldbit;
150 
151 	__asm__(
152 		"btsl %2,%1\n\tsbbl %0,%0"
153 		:"=r" (oldbit),"=m" (ADDR)
154 		:"Ir" (nr));
155 	return oldbit;
156 }
157 
158 /**
159  * test_and_clear_bit - Clear a bit and return its old value
160  * @nr: Bit to set
161  * @addr: Address to count from
162  *
163  * This operation is atomic and cannot be reordered.
164  * It also implies a memory barrier.
165  */
test_and_clear_bit(int nr,volatile void * addr)166 static __inline__ int test_and_clear_bit(int nr, volatile void * addr)
167 {
168 	int oldbit;
169 
170 	__asm__ __volatile__( LOCK_PREFIX
171 		"btrl %2,%1\n\tsbbl %0,%0"
172 		:"=r" (oldbit),"=m" (ADDR)
173 		:"Ir" (nr) : "memory");
174 	return oldbit;
175 }
176 
177 /**
178  * __test_and_clear_bit - Clear a bit and return its old value
179  * @nr: Bit to set
180  * @addr: Address to count from
181  *
182  * This operation is non-atomic and can be reordered.
183  * If two examples of this operation race, one can appear to succeed
184  * but actually fail.  You must protect multiple accesses with a lock.
185  */
__test_and_clear_bit(int nr,volatile void * addr)186 static __inline__ int __test_and_clear_bit(int nr, volatile void * addr)
187 {
188 	int oldbit;
189 
190 	__asm__(
191 		"btrl %2,%1\n\tsbbl %0,%0"
192 		:"=r" (oldbit),"=m" (ADDR)
193 		:"Ir" (nr));
194 	return oldbit;
195 }
196 
197 /* WARNING: non atomic and it can be reordered! */
__test_and_change_bit(int nr,volatile void * addr)198 static __inline__ int __test_and_change_bit(int nr, volatile void * addr)
199 {
200 	int oldbit;
201 
202 	__asm__ __volatile__(
203 		"btcl %2,%1\n\tsbbl %0,%0"
204 		:"=r" (oldbit),"=m" (ADDR)
205 		:"Ir" (nr) : "memory");
206 	return oldbit;
207 }
208 
209 /**
210  * test_and_change_bit - Change a bit and return its new value
211  * @nr: Bit to set
212  * @addr: Address to count from
213  *
214  * This operation is atomic and cannot be reordered.
215  * It also implies a memory barrier.
216  */
test_and_change_bit(int nr,volatile void * addr)217 static __inline__ int test_and_change_bit(int nr, volatile void * addr)
218 {
219 	int oldbit;
220 
221 	__asm__ __volatile__( LOCK_PREFIX
222 		"btcl %2,%1\n\tsbbl %0,%0"
223 		:"=r" (oldbit),"=m" (ADDR)
224 		:"Ir" (nr) : "memory");
225 	return oldbit;
226 }
227 
228 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
229 /**
230  * test_bit - Determine whether a bit is set
231  * @nr: bit number to test
232  * @addr: Address to start counting from
233  */
234 static int test_bit(int nr, const volatile void * addr);
235 #endif
236 
constant_test_bit(int nr,const volatile void * addr)237 static __inline__ int constant_test_bit(int nr, const volatile void * addr)
238 {
239 	return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
240 }
241 
variable_test_bit(int nr,volatile void * addr)242 static __inline__ int variable_test_bit(int nr, volatile void * addr)
243 {
244 	int oldbit;
245 
246 	__asm__ __volatile__(
247 		"btl %2,%1\n\tsbbl %0,%0"
248 		:"=r" (oldbit)
249 		:"m" (ADDR),"Ir" (nr));
250 	return oldbit;
251 }
252 
253 #define test_bit(nr,addr) \
254 (__builtin_constant_p(nr) ? \
255  constant_test_bit((nr),(addr)) : \
256  variable_test_bit((nr),(addr)))
257 
258 /**
259  * find_first_zero_bit - find the first zero bit in a memory region
260  * @addr: The address to start the search at
261  * @size: The maximum size to search
262  *
263  * Returns the bit-number of the first zero bit, not the number of the byte
264  * containing a bit.
265  */
find_first_zero_bit(void * addr,unsigned size)266 static __inline__ int find_first_zero_bit(void * addr, unsigned size)
267 {
268 	int d0, d1, d2;
269 	int res;
270 
271 	if (!size)
272 		return 0;
273 	/* This looks at memory. Mark it volatile to tell gcc not to move it around */
274 	__asm__ __volatile__(
275 		"movl $-1,%%eax\n\t"
276 		"xorl %%edx,%%edx\n\t"
277 		"repe; scasl\n\t"
278 		"je 1f\n\t"
279 		"xorl -4(%%edi),%%eax\n\t"
280 		"subl $4,%%edi\n\t"
281 		"bsfl %%eax,%%edx\n"
282 		"1:\tsubl %%ebx,%%edi\n\t"
283 		"shll $3,%%edi\n\t"
284 		"addl %%edi,%%edx"
285 		:"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
286 		:"1" ((size + 31) >> 5), "2" (addr), "b" (addr));
287 	return res;
288 }
289 
290 /**
291  * find_next_zero_bit - find the first zero bit in a memory region
292  * @addr: The address to base the search on
293  * @offset: The bitnumber to start searching at
294  * @size: The maximum size to search
295  */
find_next_zero_bit(void * addr,int size,int offset)296 static __inline__ int find_next_zero_bit (void * addr, int size, int offset)
297 {
298 	unsigned long * p = ((unsigned long *) addr) + (offset >> 5);
299 	int set = 0, bit = offset & 31, res;
300 
301 	if (bit) {
302 		/*
303 		 * Look for zero in first byte
304 		 */
305 		__asm__("bsfl %1,%0\n\t"
306 			"jne 1f\n\t"
307 			"movl $32, %0\n"
308 			"1:"
309 			: "=r" (set)
310 			: "r" (~(*p >> bit)));
311 		if (set < (32 - bit))
312 			return set + offset;
313 		set = 32 - bit;
314 		p++;
315 	}
316 	/*
317 	 * No zero yet, search remaining full bytes for a zero
318 	 */
319 	res = find_first_zero_bit (p, size - 32 * (p - (unsigned long *) addr));
320 	return (offset + set + res);
321 }
322 
323 /**
324  * ffz - find first zero in word.
325  * @word: The word to search
326  *
327  * Undefined if no zero exists, so code should check against ~0UL first.
328  */
ffz(unsigned long word)329 static __inline__ unsigned long ffz(unsigned long word)
330 {
331 	__asm__("bsfl %1,%0"
332 		:"=r" (word)
333 		:"r" (~word));
334 	return word;
335 }
336 
337 #ifdef __KERNEL__
338 
339 /**
340  * __ffs - find first set bit in word
341  * @word: The word to search
342  *
343  * Undefined if no bit exists, so code should check against 0 first.
344  */
__ffs(unsigned long word)345 static inline unsigned long __ffs(unsigned long word)
346 {
347 	__asm__("rep; bsf %1,%0"
348 		: "=r" (word)
349 		: "rm" (word));
350 	return word;
351 }
352 
353 /**
354  * ffs - find first bit set
355  * @x: the word to search
356  *
357  * This is defined the same way as
358  * the libc and compiler builtin ffs routines, therefore
359  * differs in spirit from the above ffz (man ffs).
360  */
ffs(int x)361 static __inline__ int ffs(int x)
362 {
363 	int r;
364 
365 	__asm__("bsfl %1,%0\n\t"
366 		"jnz 1f\n\t"
367 		"movl $-1,%0\n"
368 		"1:" : "=r" (r) : "rm" (x));
369 
370 	return r+1;
371 }
372 #define PLATFORM_FFS
373 
__ilog2(unsigned int x)374 static inline int __ilog2(unsigned int x)
375 {
376 	return generic_fls(x) - 1;
377 }
378 
379 /**
380  * hweightN - returns the hamming weight of a N-bit word
381  * @x: the word to weigh
382  *
383  * The Hamming Weight of a number is the total number of bits set in it.
384  */
385 
386 #define hweight32(x) generic_hweight32(x)
387 #define hweight16(x) generic_hweight16(x)
388 #define hweight8(x) generic_hweight8(x)
389 
390 #endif /* __KERNEL__ */
391 
392 #ifdef __KERNEL__
393 
394 #define ext2_set_bit                 __test_and_set_bit
395 #define ext2_clear_bit               __test_and_clear_bit
396 #define ext2_test_bit                test_bit
397 #define ext2_find_first_zero_bit     find_first_zero_bit
398 #define ext2_find_next_zero_bit      find_next_zero_bit
399 
400 /* Bitmap functions for the minix filesystem.  */
401 #define minix_test_and_set_bit(nr,addr) __test_and_set_bit(nr,addr)
402 #define minix_set_bit(nr,addr) __set_bit(nr,addr)
403 #define minix_test_and_clear_bit(nr,addr) __test_and_clear_bit(nr,addr)
404 #define minix_test_bit(nr,addr) test_bit(nr,addr)
405 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
406 
407 #endif /* __KERNEL__ */
408 
409 #endif /* _I386_BITOPS_H */
410