2 * bitops.h: Bit string operations on the ppc
8 #include <linux/config.h>
9 #include <asm/byteorder.h>
11 extern void set_bit(int nr, volatile void *addr);
12 extern void clear_bit(int nr, volatile void *addr);
13 extern void change_bit(int nr, volatile void *addr);
14 extern int test_and_set_bit(int nr, volatile void *addr);
15 extern int test_and_clear_bit(int nr, volatile void *addr);
16 extern int test_and_change_bit(int nr, volatile void *addr);
19 * Arguably these bit operations don't imply any memory barrier or
20 * SMP ordering, but in fact a lot of drivers expect them to imply
21 * both, since they do on x86 cpus.
24 #define SMP_WMB "eieio\n"
25 #define SMP_MB "\nsync"
29 #endif /* CONFIG_SMP */
31 #define __INLINE_BITOPS 1
35 * These used to be if'd out here because using : "cc" as a constraint
36 * resulted in errors from egcs. Things may be OK with gcc-2.95.
38 extern __inline__ void set_bit(int nr, volatile void * addr)
41 unsigned long mask = 1 << (nr & 0x1f);
42 unsigned long *p = ((unsigned long *)addr) + (nr >> 5);
44 __asm__ __volatile__(SMP_WMB "\
50 : "=&r" (old), "=m" (*p)
51 : "r" (mask), "r" (p), "m" (*p)
55 extern __inline__ void clear_bit(int nr, volatile void *addr)
58 unsigned long mask = 1 << (nr & 0x1f);
59 unsigned long *p = ((unsigned long *)addr) + (nr >> 5);
61 __asm__ __volatile__(SMP_WMB "\
67 : "=&r" (old), "=m" (*p)
68 : "r" (mask), "r" (p), "m" (*p)
72 extern __inline__ void change_bit(int nr, volatile void *addr)
75 unsigned long mask = 1 << (nr & 0x1f);
76 unsigned long *p = ((unsigned long *)addr) + (nr >> 5);
78 __asm__ __volatile__(SMP_WMB "\
84 : "=&r" (old), "=m" (*p)
85 : "r" (mask), "r" (p), "m" (*p)
89 extern __inline__ int test_and_set_bit(int nr, volatile void *addr)
92 unsigned int mask = 1 << (nr & 0x1f);
93 volatile unsigned int *p = ((volatile unsigned int *)addr) + (nr >> 5);
95 __asm__ __volatile__(SMP_WMB "\
101 : "=&r" (old), "=&r" (t), "=m" (*p)
102 : "r" (mask), "r" (p), "m" (*p)
105 return (old & mask) != 0;
108 extern __inline__ int test_and_clear_bit(int nr, volatile void *addr)
111 unsigned int mask = 1 << (nr & 0x1f);
112 volatile unsigned int *p = ((volatile unsigned int *)addr) + (nr >> 5);
114 __asm__ __volatile__(SMP_WMB "\
120 : "=&r" (old), "=&r" (t), "=m" (*p)
121 : "r" (mask), "r" (p), "m" (*p)
124 return (old & mask) != 0;
127 extern __inline__ int test_and_change_bit(int nr, volatile void *addr)
130 unsigned int mask = 1 << (nr & 0x1f);
131 volatile unsigned int *p = ((volatile unsigned int *)addr) + (nr >> 5);
133 __asm__ __volatile__(SMP_WMB "\
139 : "=&r" (old), "=&r" (t), "=m" (*p)
140 : "r" (mask), "r" (p), "m" (*p)
143 return (old & mask) != 0;
145 #endif /* __INLINE_BITOPS */
147 extern __inline__ int test_bit(int nr, __const__ volatile void *addr)
149 __const__ unsigned int *p = (__const__ unsigned int *) addr;
151 return ((p[nr >> 5] >> (nr & 0x1f)) & 1) != 0;
154 /* Return the bit position of the most significant 1 bit in a word */
155 /* - the result is undefined when x == 0 */
156 extern __inline__ int __ilog2(unsigned int x)
160 asm ("cntlzw %0,%1" : "=r" (lz) : "r" (x));
164 extern __inline__ int ffz(unsigned int x)
168 return __ilog2(x & -x);
172 * fls: find last (most-significant) bit set.
173 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
175 * On powerpc, __ilog2(0) returns -1, but this is not safe in general
177 static __inline__ int fls(unsigned int x)
179 return __ilog2(x) + 1;
184 * fls64 - find last set bit in a 64-bit word
185 * @x: the word to search
187 * This is defined in a similar way as the libc and compiler builtin
188 * ffsll, but returns the position of the most significant set bit.
190 * fls64(value) returns 0 if value is 0 or the position of the last
191 * set bit if value is nonzero. The last (most significant) bit is
194 #if BITS_PER_LONG == 32
195 static inline int fls64(__u64 x)
202 #elif BITS_PER_LONG == 64
203 static inline int fls64(__u64 x)
207 return __ilog2(x) + 1;
210 #error BITS_PER_LONG not 32 or 64
213 static inline int __ilog2_u64(u64 n)
218 static inline int ffs64(u64 x)
220 return __ilog2_u64(x & -x) + 1ull;
226 * ffs: find first bit set. This is defined the same way as
227 * the libc and compiler builtin ffs routines, therefore
228 * differs in spirit from the above ffz (man ffs).
230 extern __inline__ int ffs(int x)
232 return __ilog2(x & -x) + 1;
237 * hweightN: returns the hamming weight (i.e. the number
238 * of bits set) of a N-bit word
241 #define hweight32(x) generic_hweight32(x)
242 #define hweight16(x) generic_hweight16(x)
243 #define hweight8(x) generic_hweight8(x)
245 #endif /* __KERNEL__ */
248 * This implementation of find_{first,next}_zero_bit was stolen from
249 * Linus' asm-alpha/bitops.h.
251 #define find_first_zero_bit(addr, size) \
252 find_next_zero_bit((addr), (size), 0)
254 extern __inline__ unsigned long find_next_zero_bit(void * addr,
255 unsigned long size, unsigned long offset)
257 unsigned int * p = ((unsigned int *) addr) + (offset >> 5);
258 unsigned int result = offset & ~31UL;
267 tmp |= ~0UL >> (32-offset);
276 if ((tmp = *p++) != ~0U)
287 return result + ffz(tmp);
291 #define _EXT2_HAVE_ASM_BITOPS_
295 * test_and_{set,clear}_bit guarantee atomicity without
296 * disabling interrupts.
298 #define ext2_set_bit(nr, addr) test_and_set_bit((nr) ^ 0x18, addr)
299 #define ext2_clear_bit(nr, addr) test_and_clear_bit((nr) ^ 0x18, addr)
302 extern __inline__ int ext2_set_bit(int nr, void * addr)
305 unsigned char *ADDR = (unsigned char *) addr;
309 mask = 1 << (nr & 0x07);
310 oldbit = (*ADDR & mask) ? 1 : 0;
315 extern __inline__ int ext2_clear_bit(int nr, void * addr)
318 unsigned char *ADDR = (unsigned char *) addr;
322 mask = 1 << (nr & 0x07);
323 oldbit = (*ADDR & mask) ? 1 : 0;
324 *ADDR = *ADDR & ~mask;
327 #endif /* __KERNEL__ */
329 extern __inline__ int ext2_test_bit(int nr, __const__ void * addr)
331 __const__ unsigned char *ADDR = (__const__ unsigned char *) addr;
333 return (ADDR[nr >> 3] >> (nr & 7)) & 1;
337 * This implementation of ext2_find_{first,next}_zero_bit was stolen from
338 * Linus' asm-alpha/bitops.h and modified for a big-endian machine.
341 #define ext2_find_first_zero_bit(addr, size) \
342 ext2_find_next_zero_bit((addr), (size), 0)
344 static __inline__ unsigned long ext2_find_next_zero_bit(void *addr,
345 unsigned long size, unsigned long offset)
347 unsigned int *p = ((unsigned int *) addr) + (offset >> 5);
348 unsigned int result = offset & ~31UL;
356 tmp = cpu_to_le32p(p++);
357 tmp |= ~0UL >> (32-offset);
366 if ((tmp = cpu_to_le32p(p++)) != ~0U)
373 tmp = cpu_to_le32p(p);
377 return result + ffz(tmp);
380 /* Bitmap functions for the minix filesystem. */
381 #define minix_test_and_set_bit(nr,addr) ext2_set_bit(nr,addr)
382 #define minix_set_bit(nr,addr) ((void)ext2_set_bit(nr,addr))
383 #define minix_test_and_clear_bit(nr,addr) ext2_clear_bit(nr,addr)
384 #define minix_test_bit(nr,addr) ext2_test_bit(nr,addr)
385 #define minix_find_first_zero_bit(addr,size) ext2_find_first_zero_bit(addr,size)
387 #endif /* _PPC_BITOPS_H */