1 #ifndef _MICROBLAZE_BITOPS_H
2 #define _MICROBLAZE_BITOPS_H
5 * Copyright 1992, Linus Torvalds.
8 #include <linux/config.h>
9 #include <asm/byteorder.h> /* swab32 */
10 #include <asm/system.h> /* save_flags */
14 * Function prototypes to keep gcc -Wall happy
18 * The __ functions are not atomic
21 extern void set_bit(int nr, volatile void * addr);
22 extern void __set_bit(int nr, volatile void * addr);
24 extern void clear_bit(int nr, volatile void * addr);
25 #define __clear_bit(nr, addr) clear_bit(nr, addr)
27 extern void change_bit(int nr, volatile void * addr);
28 extern void __change_bit(int nr, volatile void * addr);
29 extern int test_and_set_bit(int nr, volatile void * addr);
30 extern int __test_and_set_bit(int nr, volatile void * addr);
31 extern int test_and_clear_bit(int nr, volatile void * addr);
32 extern int __test_and_clear_bit(int nr, volatile void * addr);
33 extern int test_and_change_bit(int nr, volatile void * addr);
34 extern int __test_and_change_bit(int nr, volatile void * addr);
35 extern int __constant_test_bit(int nr, const volatile void * addr);
36 extern int __test_bit(int nr, volatile void * addr);
37 extern int find_first_zero_bit(void * addr, unsigned size);
38 extern int find_next_zero_bit (void * addr, int size, int offset);
41 * ffz = Find First Zero in word. Undefined if no zero exists,
42 * so code should check against ~0UL first..
44 extern __inline__ unsigned long ffz(unsigned long word)
46 unsigned long result = 0;
56 extern __inline__ void set_bit(int nr, volatile void * addr)
58 int * a = (int *) addr;
63 mask = 1 << (nr & 0x1f);
64 save_flags_cli(flags);
69 extern __inline__ void __set_bit(int nr, volatile void * addr)
71 int * a = (int *) addr;
75 mask = 1 << (nr & 0x1f);
80 * clear_bit() doesn't provide any barrier for the compiler.
82 #define smp_mb__before_clear_bit() barrier()
83 #define smp_mb__after_clear_bit() barrier()
85 extern __inline__ void clear_bit(int nr, volatile void * addr)
87 int * a = (int *) addr;
92 mask = 1 << (nr & 0x1f);
93 save_flags_cli(flags);
98 extern __inline__ void change_bit(int nr, volatile void * addr)
102 unsigned long *ADDR = (unsigned long *) addr;
105 mask = 1 << (nr & 31);
106 save_flags_cli(flags);
108 restore_flags(flags);
111 extern __inline__ void __change_bit(int nr, volatile void * addr)
114 unsigned long *ADDR = (unsigned long *) addr;
117 mask = 1 << (nr & 31);
121 extern __inline__ int test_and_set_bit(int nr, volatile void * addr)
124 volatile unsigned int *a = (volatile unsigned int *) addr;
128 mask = 1 << (nr & 0x1f);
129 save_flags_cli(flags);
130 retval = (mask & *a) != 0;
132 restore_flags(flags);
137 extern __inline__ int __test_and_set_bit(int nr, volatile void * addr)
140 volatile unsigned int *a = (volatile unsigned int *) addr;
143 mask = 1 << (nr & 0x1f);
144 retval = (mask & *a) != 0;
149 extern __inline__ int test_and_clear_bit(int nr, volatile void * addr)
152 volatile unsigned int *a = (volatile unsigned int *) addr;
156 mask = 1 << (nr & 0x1f);
157 save_flags_cli(flags);
158 retval = (mask & *a) != 0;
160 restore_flags(flags);
165 extern __inline__ int __test_and_clear_bit(int nr, volatile void * addr)
168 volatile unsigned int *a = (volatile unsigned int *) addr;
171 mask = 1 << (nr & 0x1f);
172 retval = (mask & *a) != 0;
177 extern __inline__ int test_and_change_bit(int nr, volatile void * addr)
180 volatile unsigned int *a = (volatile unsigned int *) addr;
184 mask = 1 << (nr & 0x1f);
185 save_flags_cli(flags);
186 retval = (mask & *a) != 0;
188 restore_flags(flags);
193 extern __inline__ int __test_and_change_bit(int nr, volatile void * addr)
196 volatile unsigned int *a = (volatile unsigned int *) addr;
199 mask = 1 << (nr & 0x1f);
200 retval = (mask & *a) != 0;
206 * This routine doesn't need to be atomic.
208 extern __inline__ int __constant_test_bit(int nr, const volatile void * addr)
210 return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
213 extern __inline__ int __test_bit(int nr, volatile void * addr)
215 int * a = (int *) addr;
219 mask = 1 << (nr & 0x1f);
220 return ((mask & *a) != 0);
223 #define test_bit(nr,addr) \
224 (__builtin_constant_p(nr) ? \
225 __constant_test_bit((nr),(addr)) : \
226 __test_bit((nr),(addr)))
228 #define find_first_zero_bit(addr, size) \
229 find_next_zero_bit((addr), (size), 0)
231 extern __inline__ int find_next_zero_bit (void * addr, int size, int offset)
233 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
234 unsigned long result = offset & ~31UL;
243 tmp |= ~0UL >> (32-offset);
251 while (size & ~31UL) {
264 return result + ffz(tmp);
267 #define ffs(x) generic_ffs(x)
270 * hweightN: returns the hamming weight (i.e. the number
271 * of bits set) of a N-bit word
274 #define hweight32(x) generic_hweight32(x)
275 #define hweight16(x) generic_hweight16(x)
276 #define hweight8(x) generic_hweight8(x)
279 extern __inline__ int ext2_set_bit(int nr, volatile void * addr)
283 volatile unsigned char *ADDR = (unsigned char *) addr;
286 mask = 1 << (nr & 0x07);
287 save_flags_cli(flags);
288 retval = (mask & *ADDR) != 0;
290 restore_flags(flags);
294 extern __inline__ int ext2_clear_bit(int nr, volatile void * addr)
298 volatile unsigned char *ADDR = (unsigned char *) addr;
301 mask = 1 << (nr & 0x07);
302 save_flags_cli(flags);
303 retval = (mask & *ADDR) != 0;
305 restore_flags(flags);
309 extern __inline__ int ext2_test_bit(int nr, const volatile void * addr)
312 const volatile unsigned char *ADDR = (const unsigned char *) addr;
315 mask = 1 << (nr & 0x07);
316 return ((mask & *ADDR) != 0);
319 #define ext2_find_first_zero_bit(addr, size) \
320 ext2_find_next_zero_bit((addr), (size), 0)
322 extern __inline__ unsigned long ext2_find_next_zero_bit(void *addr, unsigned long size, unsigned long offset)
324 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
325 unsigned long result = offset & ~31UL;
333 /* We hold the little endian value in tmp, but then the
334 * shift is illegal. So we could keep a big endian value
337 * tmp = __swab32(*(p++));
338 * tmp |= ~0UL >> (32-offset);
340 * but this would decrease preformance, so we change the
344 tmp |= __swab32(~0UL >> (32-offset));
352 while(size & ~31UL) {
363 /* tmp is little endian, so we would have to swab the shift,
364 * see above. But then we have to swab tmp below for ffz, so
365 * we might as well do this here.
367 return result + ffz(__swab32(tmp) | (~0UL << size));
369 return result + ffz(__swab32(tmp));
372 /* Bitmap functions for the minix filesystem. */
373 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
374 #define minix_set_bit(nr,addr) set_bit(nr,addr)
375 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
376 #define minix_test_bit(nr,addr) test_bit(nr,addr)
377 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
380 * hweightN - returns the hamming weight of a N-bit word
381 * @x: the word to weigh
383 * The Hamming Weight of a number is the total number of bits set in it.
386 #define hweight32(x) generic_hweight32(x)
387 #define hweight16(x) generic_hweight16(x)
388 #define hweight8(x) generic_hweight8(x)
390 #endif /* __KERNEL__ */
392 #endif /* _MICROBLAZE_BITOPS_H */