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)
26 #define PLATFORM__CLEAR_BIT
28 extern void change_bit(int nr, volatile void * addr);
29 extern void __change_bit(int nr, volatile void * addr);
30 extern int test_and_set_bit(int nr, volatile void * addr);
31 extern int __test_and_set_bit(int nr, volatile void * addr);
32 extern int test_and_clear_bit(int nr, volatile void * addr);
33 extern int __test_and_clear_bit(int nr, volatile void * addr);
34 extern int test_and_change_bit(int nr, volatile void * addr);
35 extern int __test_and_change_bit(int nr, volatile void * addr);
36 extern int __constant_test_bit(int nr, const volatile void * addr);
37 extern int __test_bit(int nr, volatile void * addr);
38 extern int find_first_zero_bit(void * addr, unsigned size);
39 extern int find_next_zero_bit (void * addr, int size, int offset);
42 * ffz = Find First Zero in word. Undefined if no zero exists,
43 * so code should check against ~0UL first..
45 extern __inline__ unsigned long ffz(unsigned long word)
47 unsigned long result = 0;
57 extern __inline__ void set_bit(int nr, volatile void * addr)
59 int * a = (int *) addr;
64 mask = 1 << (nr & 0x1f);
65 save_flags_cli(flags);
70 extern __inline__ void __set_bit(int nr, volatile void * addr)
72 int * a = (int *) addr;
76 mask = 1 << (nr & 0x1f);
79 #define PLATFORM__SET_BIT
82 * clear_bit() doesn't provide any barrier for the compiler.
84 #define smp_mb__before_clear_bit() barrier()
85 #define smp_mb__after_clear_bit() barrier()
87 extern __inline__ void clear_bit(int nr, volatile void * addr)
89 int * a = (int *) addr;
94 mask = 1 << (nr & 0x1f);
95 save_flags_cli(flags);
100 extern __inline__ void change_bit(int nr, volatile void * addr)
104 unsigned long *ADDR = (unsigned long *) addr;
107 mask = 1 << (nr & 31);
108 save_flags_cli(flags);
110 restore_flags(flags);
113 extern __inline__ void __change_bit(int nr, volatile void * addr)
116 unsigned long *ADDR = (unsigned long *) addr;
119 mask = 1 << (nr & 31);
123 extern __inline__ int test_and_set_bit(int nr, volatile void * addr)
126 volatile unsigned int *a = (volatile unsigned int *) addr;
130 mask = 1 << (nr & 0x1f);
131 save_flags_cli(flags);
132 retval = (mask & *a) != 0;
134 restore_flags(flags);
139 extern __inline__ int __test_and_set_bit(int nr, volatile void * addr)
142 volatile unsigned int *a = (volatile unsigned int *) addr;
145 mask = 1 << (nr & 0x1f);
146 retval = (mask & *a) != 0;
151 extern __inline__ int test_and_clear_bit(int nr, volatile void * addr)
154 volatile unsigned int *a = (volatile unsigned int *) addr;
158 mask = 1 << (nr & 0x1f);
159 save_flags_cli(flags);
160 retval = (mask & *a) != 0;
162 restore_flags(flags);
167 extern __inline__ int __test_and_clear_bit(int nr, volatile void * addr)
170 volatile unsigned int *a = (volatile unsigned int *) addr;
173 mask = 1 << (nr & 0x1f);
174 retval = (mask & *a) != 0;
179 extern __inline__ int test_and_change_bit(int nr, volatile void * addr)
182 volatile unsigned int *a = (volatile unsigned int *) addr;
186 mask = 1 << (nr & 0x1f);
187 save_flags_cli(flags);
188 retval = (mask & *a) != 0;
190 restore_flags(flags);
195 extern __inline__ int __test_and_change_bit(int nr, volatile void * addr)
198 volatile unsigned int *a = (volatile unsigned int *) addr;
201 mask = 1 << (nr & 0x1f);
202 retval = (mask & *a) != 0;
208 * This routine doesn't need to be atomic.
210 extern __inline__ int __constant_test_bit(int nr, const volatile void * addr)
212 return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
215 extern __inline__ int __test_bit(int nr, volatile void * addr)
217 int * a = (int *) addr;
221 mask = 1 << (nr & 0x1f);
222 return ((mask & *a) != 0);
225 #define test_bit(nr,addr) \
226 (__builtin_constant_p(nr) ? \
227 __constant_test_bit((nr),(addr)) : \
228 __test_bit((nr),(addr)))
230 #define find_first_zero_bit(addr, size) \
231 find_next_zero_bit((addr), (size), 0)
233 extern __inline__ int find_next_zero_bit (void * addr, int size, int offset)
235 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
236 unsigned long result = offset & ~31UL;
245 tmp |= ~0UL >> (32-offset);
253 while (size & ~31UL) {
266 return result + ffz(tmp);
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 */