1 #ifndef _M68KNOMMU_BITOPS_H
2 #define _M68KNOMMU_BITOPS_H
5 * Copyright 1992, Linus Torvalds.
8 #include <linux/config.h>
9 #include <linux/compiler.h>
10 #include <asm/byteorder.h> /* swab32 */
11 #include <asm/system.h> /* save_flags */
18 static inline int ffs(int x)
50 static inline int __ffs(int x)
80 * Every architecture must define this function. It's the fastest
81 * way of searching a 140-bit bitmap where the first 100 bits are
82 * unlikely to be set. It's guaranteed that at least one of the 140
85 static inline int sched_find_first_bit(unsigned long *b)
90 return __ffs(b[1]) + 32;
92 return __ffs(b[2]) + 64;
94 return __ffs(b[3]) + 96;
95 return __ffs(b[4]) + 128;
99 * ffz = Find First Zero in word. Undefined if no zero exists,
100 * so code should check against ~0UL first..
102 static __inline__ unsigned long ffz(unsigned long word)
104 unsigned long result = 0;
114 static __inline__ void set_bit(int nr, volatile unsigned long * addr)
116 #ifdef CONFIG_COLDFIRE
117 __asm__ __volatile__ ("lea %0,%%a0; bset %1,(%%a0)"
118 : "+m" (((volatile char *)addr)[(nr^31) >> 3])
122 __asm__ __volatile__ ("bset %1,%0"
123 : "+m" (((volatile char *)addr)[(nr^31) >> 3])
129 #define __set_bit(nr, addr) set_bit(nr, addr)
132 * clear_bit() doesn't provide any barrier for the compiler.
134 #define smp_mb__before_clear_bit() barrier()
135 #define smp_mb__after_clear_bit() barrier()
137 static __inline__ void clear_bit(int nr, volatile unsigned long * addr)
139 #ifdef CONFIG_COLDFIRE
140 __asm__ __volatile__ ("lea %0,%%a0; bclr %1,(%%a0)"
141 : "+m" (((volatile char *)addr)[(nr^31) >> 3])
145 __asm__ __volatile__ ("bclr %1,%0"
146 : "+m" (((volatile char *)addr)[(nr^31) >> 3])
152 #define __clear_bit(nr, addr) clear_bit(nr, addr)
154 static __inline__ void change_bit(int nr, volatile unsigned long * addr)
156 #ifdef CONFIG_COLDFIRE
157 __asm__ __volatile__ ("lea %0,%%a0; bchg %1,(%%a0)"
158 : "+m" (((volatile char *)addr)[(nr^31) >> 3])
162 __asm__ __volatile__ ("bchg %1,%0"
163 : "+m" (((volatile char *)addr)[(nr^31) >> 3])
169 #define __change_bit(nr, addr) change_bit(nr, addr)
171 static __inline__ int test_and_set_bit(int nr, volatile unsigned long * addr)
175 #ifdef CONFIG_COLDFIRE
176 __asm__ __volatile__ ("lea %1,%%a0; bset %2,(%%a0); sne %0"
177 : "=d" (retval), "+m" (((volatile char *)addr)[(nr^31) >> 3])
181 __asm__ __volatile__ ("bset %2,%1; sne %0"
182 : "=d" (retval), "+m" (((volatile char *)addr)[(nr^31) >> 3])
190 #define __test_and_set_bit(nr, addr) test_and_set_bit(nr, addr)
192 static __inline__ int test_and_clear_bit(int nr, volatile unsigned long * addr)
196 #ifdef CONFIG_COLDFIRE
197 __asm__ __volatile__ ("lea %1,%%a0; bclr %2,(%%a0); sne %0"
198 : "=d" (retval), "+m" (((volatile char *)addr)[(nr^31) >> 3])
202 __asm__ __volatile__ ("bclr %2,%1; sne %0"
203 : "=d" (retval), "+m" (((volatile char *)addr)[(nr^31) >> 3])
211 #define __test_and_clear_bit(nr, addr) test_and_clear_bit(nr, addr)
213 static __inline__ int test_and_change_bit(int nr, volatile unsigned long * addr)
217 #ifdef CONFIG_COLDFIRE
218 __asm__ __volatile__ ("lea %1,%%a0\n\tbchg %2,(%%a0)\n\tsne %0"
219 : "=d" (retval), "+m" (((volatile char *)addr)[(nr^31) >> 3])
223 __asm__ __volatile__ ("bchg %2,%1; sne %0"
224 : "=d" (retval), "+m" (((volatile char *)addr)[(nr^31) >> 3])
232 #define __test_and_change_bit(nr, addr) test_and_change_bit(nr, addr)
235 * This routine doesn't need to be atomic.
237 static __inline__ int __constant_test_bit(int nr, const volatile unsigned long * addr)
239 return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
242 static __inline__ int __test_bit(int nr, const volatile unsigned long * addr)
244 int * a = (int *) addr;
248 mask = 1 << (nr & 0x1f);
249 return ((mask & *a) != 0);
252 #define test_bit(nr,addr) \
253 (__builtin_constant_p(nr) ? \
254 __constant_test_bit((nr),(addr)) : \
255 __test_bit((nr),(addr)))
257 #define find_first_zero_bit(addr, size) \
258 find_next_zero_bit((addr), (size), 0)
259 #define find_first_bit(addr, size) \
260 find_next_bit((addr), (size), 0)
262 static __inline__ int find_next_zero_bit (const void * addr, int size, int offset)
264 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
265 unsigned long result = offset & ~31UL;
274 tmp |= ~0UL >> (32-offset);
282 while (size & ~31UL) {
295 return result + ffz(tmp);
299 * Find next one bit in a bitmap reasonably efficiently.
301 static __inline__ unsigned long find_next_bit(const unsigned long *addr,
302 unsigned long size, unsigned long offset)
304 unsigned int *p = ((unsigned int *) addr) + (offset >> 5);
305 unsigned int result = offset & ~31UL;
314 tmp &= ~0UL << offset;
323 if ((tmp = *p++) != 0)
333 tmp &= ~0UL >> (32 - size);
334 if (tmp == 0UL) /* Are any bits set? */
335 return result + size; /* Nope. */
337 return result + __ffs(tmp);
341 * hweightN: returns the hamming weight (i.e. the number
342 * of bits set) of a N-bit word
345 #define hweight32(x) generic_hweight32(x)
346 #define hweight16(x) generic_hweight16(x)
347 #define hweight8(x) generic_hweight8(x)
350 static __inline__ int ext2_set_bit(int nr, volatile void * addr)
354 #ifdef CONFIG_COLDFIRE
355 __asm__ __volatile__ ("lea %1,%%a0; bset %2,(%%a0); sne %0"
356 : "=d" (retval), "+m" (((volatile char *)addr)[nr >> 3])
360 __asm__ __volatile__ ("bset %2,%1; sne %0"
361 : "=d" (retval), "+m" (((volatile char *)addr)[nr >> 3])
369 static __inline__ int ext2_clear_bit(int nr, volatile void * addr)
373 #ifdef CONFIG_COLDFIRE
374 __asm__ __volatile__ ("lea %1,%%a0; bclr %2,(%%a0); sne %0"
375 : "=d" (retval), "+m" (((volatile char *)addr)[nr >> 3])
379 __asm__ __volatile__ ("bclr %2,%1; sne %0"
380 : "=d" (retval), "+m" (((volatile char *)addr)[nr >> 3])
388 #define ext2_set_bit_atomic(lock, nr, addr) \
392 ret = ext2_set_bit((nr), (addr)); \
397 #define ext2_clear_bit_atomic(lock, nr, addr) \
401 ret = ext2_clear_bit((nr), (addr)); \
406 static __inline__ int ext2_test_bit(int nr, const volatile void * addr)
410 #ifdef CONFIG_COLDFIRE
411 __asm__ __volatile__ ("lea %1,%%a0; btst %2,(%%a0); sne %0"
413 : "m" (((const volatile char *)addr)[nr >> 3]), "d" (nr)
416 __asm__ __volatile__ ("btst %2,%1; sne %0"
418 : "m" (((const volatile char *)addr)[nr >> 3]), "di" (nr)
425 #define ext2_find_first_zero_bit(addr, size) \
426 ext2_find_next_zero_bit((addr), (size), 0)
428 static __inline__ unsigned long ext2_find_next_zero_bit(void *addr, unsigned long size, unsigned long offset)
430 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
431 unsigned long result = offset & ~31UL;
439 /* We hold the little endian value in tmp, but then the
440 * shift is illegal. So we could keep a big endian value
443 * tmp = __swab32(*(p++));
444 * tmp |= ~0UL >> (32-offset);
446 * but this would decrease preformance, so we change the
450 tmp |= __swab32(~0UL >> (32-offset));
458 while(size & ~31UL) {
469 /* tmp is little endian, so we would have to swab the shift,
470 * see above. But then we have to swab tmp below for ffz, so
471 * we might as well do this here.
473 return result + ffz(__swab32(tmp) | (~0UL << size));
475 return result + ffz(__swab32(tmp));
478 /* Bitmap functions for the minix filesystem. */
479 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
480 #define minix_set_bit(nr,addr) set_bit(nr,addr)
481 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
482 #define minix_test_bit(nr,addr) test_bit(nr,addr)
483 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
486 * hweightN - returns the hamming weight of a N-bit word
487 * @x: the word to weigh
489 * The Hamming Weight of a number is the total number of bits set in it.
492 #define hweight32(x) generic_hweight32(x)
493 #define hweight16(x) generic_hweight16(x)
494 #define hweight8(x) generic_hweight8(x)
496 #endif /* __KERNEL__ */
499 * fls: find last bit set.
501 #define fls(x) generic_fls(x)
503 #endif /* _M68KNOMMU_BITOPS_H */