2 * bitops.h: Bit string operations on the ppc
9 #include <linux/config.h>
10 #include <linux/compiler.h>
11 #include <asm/byteorder.h>
12 #include <asm/atomic.h>
15 * The test_and_*_bit operations are taken to imply a memory barrier
19 #define SMP_WMB "eieio\n"
20 #define SMP_MB "\nsync"
24 #endif /* CONFIG_SMP */
26 static __inline__
void set_bit(int nr
, volatile unsigned long * addr
)
29 unsigned long mask
= 1 << (nr
& 0x1f);
30 unsigned long *p
= ((unsigned long *)addr
) + (nr
>> 5);
32 __asm__
__volatile__("\n\
38 : "=&r" (old
), "=m" (*p
)
39 : "r" (mask
), "r" (p
), "m" (*p
)
46 static __inline__
void __set_bit(int nr
, volatile unsigned long *addr
)
48 unsigned long mask
= 1 << (nr
& 0x1f);
49 unsigned long *p
= ((unsigned long *)addr
) + (nr
>> 5);
55 * clear_bit doesn't imply a memory barrier
57 #define smp_mb__before_clear_bit() smp_mb()
58 #define smp_mb__after_clear_bit() smp_mb()
60 static __inline__
void clear_bit(int nr
, volatile unsigned long *addr
)
63 unsigned long mask
= 1 << (nr
& 0x1f);
64 unsigned long *p
= ((unsigned long *)addr
) + (nr
>> 5);
66 __asm__
__volatile__("\n\
72 : "=&r" (old
), "=m" (*p
)
73 : "r" (mask
), "r" (p
), "m" (*p
)
80 static __inline__
void __clear_bit(int nr
, volatile unsigned long *addr
)
82 unsigned long mask
= 1 << (nr
& 0x1f);
83 unsigned long *p
= ((unsigned long *)addr
) + (nr
>> 5);
88 static __inline__
void change_bit(int nr
, volatile unsigned long *addr
)
91 unsigned long mask
= 1 << (nr
& 0x1f);
92 unsigned long *p
= ((unsigned long *)addr
) + (nr
>> 5);
94 __asm__
__volatile__("\n\
100 : "=&r" (old
), "=m" (*p
)
101 : "r" (mask
), "r" (p
), "m" (*p
)
108 static __inline__
void __change_bit(int nr
, volatile unsigned long *addr
)
110 unsigned long mask
= 1 << (nr
& 0x1f);
111 unsigned long *p
= ((unsigned long *)addr
) + (nr
>> 5);
117 * test_and_*_bit do imply a memory barrier (?)
119 static __inline__
int test_and_set_bit(int nr
, volatile unsigned long *addr
)
122 unsigned int mask
= 1 << (nr
& 0x1f);
123 volatile unsigned int *p
= ((volatile unsigned int *)addr
) + (nr
>> 5);
125 __asm__
__volatile__(SMP_WMB
"\n\
132 : "=&r" (old
), "=&r" (t
), "=m" (*p
)
133 : "r" (mask
), "r" (p
), "m" (*p
)
136 return (old
& mask
) != 0;
142 static __inline__
int __test_and_set_bit(int nr
, volatile unsigned long *addr
)
144 unsigned long mask
= 1 << (nr
& 0x1f);
145 unsigned long *p
= ((unsigned long *)addr
) + (nr
>> 5);
146 unsigned long old
= *p
;
149 return (old
& mask
) != 0;
152 static __inline__
int test_and_clear_bit(int nr
, volatile unsigned long *addr
)
155 unsigned int mask
= 1 << (nr
& 0x1f);
156 volatile unsigned int *p
= ((volatile unsigned int *)addr
) + (nr
>> 5);
158 __asm__
__volatile__(SMP_WMB
"\n\
165 : "=&r" (old
), "=&r" (t
), "=m" (*p
)
166 : "r" (mask
), "r" (p
), "m" (*p
)
169 return (old
& mask
) != 0;
175 static __inline__
int __test_and_clear_bit(int nr
, volatile unsigned long *addr
)
177 unsigned long mask
= 1 << (nr
& 0x1f);
178 unsigned long *p
= ((unsigned long *)addr
) + (nr
>> 5);
179 unsigned long old
= *p
;
182 return (old
& mask
) != 0;
185 static __inline__
int test_and_change_bit(int nr
, volatile unsigned long *addr
)
188 unsigned int mask
= 1 << (nr
& 0x1f);
189 volatile unsigned int *p
= ((volatile unsigned int *)addr
) + (nr
>> 5);
191 __asm__
__volatile__(SMP_WMB
"\n\
198 : "=&r" (old
), "=&r" (t
), "=m" (*p
)
199 : "r" (mask
), "r" (p
), "m" (*p
)
202 return (old
& mask
) != 0;
208 static __inline__
int __test_and_change_bit(int nr
, volatile unsigned long *addr
)
210 unsigned long mask
= 1 << (nr
& 0x1f);
211 unsigned long *p
= ((unsigned long *)addr
) + (nr
>> 5);
212 unsigned long old
= *p
;
215 return (old
& mask
) != 0;
218 static __inline__
int test_bit(int nr
, __const__
volatile unsigned long *addr
)
220 return ((addr
[nr
>> 5] >> (nr
& 0x1f)) & 1) != 0;
223 /* Return the bit position of the most significant 1 bit in a word */
224 static __inline__
int __ilog2(unsigned long x
)
228 asm ("cntlzw %0,%1" : "=r" (lz
) : "r" (x
));
232 static __inline__
int ffz(unsigned long x
)
236 return __ilog2(x
& -x
);
239 static inline int __ffs(unsigned long x
)
241 return __ilog2(x
& -x
);
245 * ffs: find first bit set. This is defined the same way as
246 * the libc and compiler builtin ffs routines, therefore
247 * differs in spirit from the above ffz (man ffs).
249 static __inline__
int ffs(int x
)
251 return __ilog2(x
& -x
) + 1;
255 * fls: find last (most-significant) bit set.
256 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
258 static __inline__
int fls(unsigned int x
)
262 asm ("cntlzw %0,%1" : "=r" (lz
) : "r" (x
));
267 * hweightN: returns the hamming weight (i.e. the number
268 * of bits set) of a N-bit word
271 #define hweight32(x) generic_hweight32(x)
272 #define hweight16(x) generic_hweight16(x)
273 #define hweight8(x) generic_hweight8(x)
276 * Find the first bit set in a 140-bit bitmap.
277 * The first 100 bits are unlikely to be set.
279 static inline int sched_find_first_bit(const unsigned long *b
)
284 return __ffs(b
[1]) + 32;
286 return __ffs(b
[2]) + 64;
288 return __ffs(b
[3]) + 96;
289 return __ffs(b
[4]) + 128;
293 * find_next_bit - find the next set bit in a memory region
294 * @addr: The address to base the search on
295 * @offset: The bitnumber to start searching at
296 * @size: The maximum size to search
298 static __inline__
unsigned long find_next_bit(const unsigned long *addr
,
299 unsigned long size
, unsigned long offset
)
301 unsigned int *p
= ((unsigned int *) addr
) + (offset
>> 5);
302 unsigned int result
= offset
& ~31UL;
311 tmp
&= ~0UL << offset
;
320 if ((tmp
= *p
++) != 0)
330 tmp
&= ~0UL >> (32 - size
);
331 if (tmp
== 0UL) /* Are any bits set? */
332 return result
+ size
; /* Nope. */
334 return result
+ __ffs(tmp
);
338 * find_first_bit - find the first set bit in a memory region
339 * @addr: The address to start the search at
340 * @size: The maximum size to search
342 * Returns the bit-number of the first set bit, not the number of the byte
345 #define find_first_bit(addr, size) \
346 find_next_bit((addr), (size), 0)
349 * This implementation of find_{first,next}_zero_bit was stolen from
350 * Linus' asm-alpha/bitops.h.
352 #define find_first_zero_bit(addr, size) \
353 find_next_zero_bit((addr), (size), 0)
355 static __inline__
unsigned long find_next_zero_bit(const unsigned long *addr
,
356 unsigned long size
, unsigned long offset
)
358 unsigned int * p
= ((unsigned int *) addr
) + (offset
>> 5);
359 unsigned int result
= offset
& ~31UL;
368 tmp
|= ~0UL >> (32-offset
);
377 if ((tmp
= *p
++) != ~0U)
387 if (tmp
== ~0UL) /* Are any bits zero? */
388 return result
+ size
; /* Nope. */
390 return result
+ ffz(tmp
);
394 #define ext2_set_bit(nr, addr) __test_and_set_bit((nr) ^ 0x18, (unsigned long *)(addr))
395 #define ext2_set_bit_atomic(lock, nr, addr) test_and_set_bit((nr) ^ 0x18, (unsigned long *)(addr))
396 #define ext2_clear_bit(nr, addr) __test_and_clear_bit((nr) ^ 0x18, (unsigned long *)(addr))
397 #define ext2_clear_bit_atomic(lock, nr, addr) test_and_clear_bit((nr) ^ 0x18, (unsigned long *)(addr))
399 static __inline__
int ext2_test_bit(int nr
, __const__
void * addr
)
401 __const__
unsigned char *ADDR
= (__const__
unsigned char *) addr
;
403 return (ADDR
[nr
>> 3] >> (nr
& 7)) & 1;
407 * This implementation of ext2_find_{first,next}_zero_bit was stolen from
408 * Linus' asm-alpha/bitops.h and modified for a big-endian machine.
411 #define ext2_find_first_zero_bit(addr, size) \
412 ext2_find_next_zero_bit((addr), (size), 0)
414 static __inline__
unsigned long ext2_find_next_zero_bit(const void *addr
,
415 unsigned long size
, unsigned long offset
)
417 unsigned int *p
= ((unsigned int *) addr
) + (offset
>> 5);
418 unsigned int result
= offset
& ~31UL;
426 tmp
= cpu_to_le32p(p
++);
427 tmp
|= ~0UL >> (32-offset
);
436 if ((tmp
= cpu_to_le32p(p
++)) != ~0U)
443 tmp
= cpu_to_le32p(p
);
446 if (tmp
== ~0UL) /* Are any bits zero? */
447 return result
+ size
; /* Nope. */
449 return result
+ ffz(tmp
);
452 /* Bitmap functions for the minix filesystem. */
453 #define minix_test_and_set_bit(nr,addr) ext2_set_bit(nr,addr)
454 #define minix_set_bit(nr,addr) ((void)ext2_set_bit(nr,addr))
455 #define minix_test_and_clear_bit(nr,addr) ext2_clear_bit(nr,addr)
456 #define minix_test_bit(nr,addr) ext2_test_bit(nr,addr)
457 #define minix_find_first_zero_bit(addr,size) ext2_find_first_zero_bit(addr,size)
459 #endif /* _PPC_BITOPS_H */
460 #endif /* __KERNEL__ */