1 #ifndef __ASM_SH_BITOPS_H
2 #define __ASM_SH_BITOPS_H
5 #include <asm/system.h>
7 #include <asm/byteorder.h>
9 static __inline__ void set_bit(int nr, volatile void * addr)
12 volatile unsigned int *a = addr;
16 mask = 1 << (nr & 0x1f);
17 local_irq_save(flags);
19 local_irq_restore(flags);
22 static __inline__ void __set_bit(int nr, volatile void * addr)
25 volatile unsigned int *a = addr;
28 mask = 1 << (nr & 0x1f);
33 * clear_bit() doesn't provide any barrier for the compiler.
35 #define smp_mb__before_clear_bit() barrier()
36 #define smp_mb__after_clear_bit() barrier()
37 static __inline__ void clear_bit(int nr, volatile void * addr)
40 volatile unsigned int *a = addr;
44 mask = 1 << (nr & 0x1f);
45 local_irq_save(flags);
47 local_irq_restore(flags);
50 static __inline__ void __clear_bit(int nr, volatile void * addr)
53 volatile unsigned int *a = addr;
56 mask = 1 << (nr & 0x1f);
60 static __inline__ void change_bit(int nr, volatile void * addr)
63 volatile unsigned int *a = addr;
67 mask = 1 << (nr & 0x1f);
68 local_irq_save(flags);
70 local_irq_restore(flags);
73 static __inline__ void __change_bit(int nr, volatile void * addr)
76 volatile unsigned int *a = addr;
79 mask = 1 << (nr & 0x1f);
83 static __inline__ int test_and_set_bit(int nr, volatile void * addr)
86 volatile unsigned int *a = addr;
90 mask = 1 << (nr & 0x1f);
91 local_irq_save(flags);
92 retval = (mask & *a) != 0;
94 local_irq_restore(flags);
99 static __inline__ int __test_and_set_bit(int nr, volatile void * addr)
102 volatile unsigned int *a = addr;
105 mask = 1 << (nr & 0x1f);
106 retval = (mask & *a) != 0;
112 static __inline__ int test_and_clear_bit(int nr, volatile void * addr)
115 volatile unsigned int *a = addr;
119 mask = 1 << (nr & 0x1f);
120 local_irq_save(flags);
121 retval = (mask & *a) != 0;
123 local_irq_restore(flags);
128 static __inline__ int __test_and_clear_bit(int nr, volatile void * addr)
131 volatile unsigned int *a = addr;
134 mask = 1 << (nr & 0x1f);
135 retval = (mask & *a) != 0;
141 static __inline__ int test_and_change_bit(int nr, volatile void * addr)
144 volatile unsigned int *a = addr;
148 mask = 1 << (nr & 0x1f);
149 local_irq_save(flags);
150 retval = (mask & *a) != 0;
152 local_irq_restore(flags);
157 static __inline__ int __test_and_change_bit(int nr, volatile void * addr)
160 volatile unsigned int *a = addr;
163 mask = 1 << (nr & 0x1f);
164 retval = (mask & *a) != 0;
170 static __inline__ int test_bit(int nr, const volatile void *addr)
172 return 1UL & (((const volatile unsigned int *) addr)[nr >> 5] >> (nr & 31));
175 static __inline__ unsigned long ffz(unsigned long word)
177 unsigned long result;
183 : "=r" (result), "=r" (word)
184 : "0" (~0L), "1" (word)
190 * __ffs - find first bit in word.
191 * @word: The word to search
193 * Undefined if no bit exists, so code should check against 0 first.
195 static __inline__ unsigned long __ffs(unsigned long word)
197 unsigned long result;
203 : "=r" (result), "=r" (word)
204 : "0" (~0L), "1" (word)
210 * find_next_bit - find the next set bit in a memory region
211 * @addr: The address to base the search on
212 * @offset: The bitnumber to start searching at
213 * @size: The maximum size to search
215 static __inline__ unsigned long find_next_bit(const unsigned long *addr,
216 unsigned long size, unsigned long offset)
218 unsigned int *p = ((unsigned int *) addr) + (offset >> 5);
219 unsigned int result = offset & ~31UL;
228 tmp &= ~0UL << offset;
237 if ((tmp = *p++) != 0)
247 tmp &= ~0UL >> (32 - size);
248 if (tmp == 0UL) /* Are any bits set? */
249 return result + size; /* Nope. */
251 return result + __ffs(tmp);
255 * find_first_bit - find the first set bit in a memory region
256 * @addr: The address to start the search at
257 * @size: The maximum size to search
259 * Returns the bit-number of the first set bit, not the number of the byte
262 #define find_first_bit(addr, size) \
263 find_next_bit((addr), (size), 0)
265 static __inline__ int find_next_zero_bit(const unsigned long *addr, int size, int offset)
267 const unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
268 unsigned long result = offset & ~31UL;
277 tmp |= ~0UL >> (32-offset);
285 while (size & ~31UL) {
298 return result + ffz(tmp);
301 #define find_first_zero_bit(addr, size) \
302 find_next_zero_bit((addr), (size), 0)
305 * ffs: find first bit set. This is defined the same way as
306 * the libc and compiler builtin ffs routines, therefore
307 * differs in spirit from the above ffz (man ffs).
310 #define ffs(x) generic_ffs(x)
313 * hweightN: returns the hamming weight (i.e. the number
314 * of bits set) of a N-bit word
317 #define hweight32(x) generic_hweight32(x)
318 #define hweight16(x) generic_hweight16(x)
319 #define hweight8(x) generic_hweight8(x)
322 * Every architecture must define this function. It's the fastest
323 * way of searching a 140-bit bitmap where the first 100 bits are
324 * unlikely to be set. It's guaranteed that at least one of the 140
328 static inline int sched_find_first_bit(const unsigned long *b)
333 return __ffs(b[1]) + 32;
335 return __ffs(b[2]) + 64;
337 return __ffs(b[3]) + 96;
338 return __ffs(b[4]) + 128;
341 #ifdef __LITTLE_ENDIAN__
342 #define ext2_set_bit(nr, addr) test_and_set_bit((nr), (addr))
343 #define ext2_clear_bit(nr, addr) test_and_clear_bit((nr), (addr))
344 #define ext2_test_bit(nr, addr) test_bit((nr), (addr))
345 #define ext2_find_first_zero_bit(addr, size) find_first_zero_bit((addr), (size))
346 #define ext2_find_next_zero_bit(addr, size, offset) \
347 find_next_zero_bit((unsigned long *)(addr), (size), (offset))
349 static __inline__ int ext2_set_bit(int nr, volatile void * addr)
353 volatile unsigned char *ADDR = (unsigned char *) addr;
356 mask = 1 << (nr & 0x07);
357 local_irq_save(flags);
358 retval = (mask & *ADDR) != 0;
360 local_irq_restore(flags);
364 static __inline__ int ext2_clear_bit(int nr, volatile void * addr)
368 volatile unsigned char *ADDR = (unsigned char *) addr;
371 mask = 1 << (nr & 0x07);
372 local_irq_save(flags);
373 retval = (mask & *ADDR) != 0;
375 local_irq_restore(flags);
379 static __inline__ int ext2_test_bit(int nr, const volatile void * addr)
382 const volatile unsigned char *ADDR = (const unsigned char *) addr;
385 mask = 1 << (nr & 0x07);
386 return ((mask & *ADDR) != 0);
389 #define ext2_find_first_zero_bit(addr, size) \
390 ext2_find_next_zero_bit((addr), (size), 0)
392 static __inline__ unsigned long ext2_find_next_zero_bit(void *addr, unsigned long size, unsigned long offset)
394 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
395 unsigned long result = offset & ~31UL;
403 /* We hold the little endian value in tmp, but then the
404 * shift is illegal. So we could keep a big endian value
407 * tmp = __swab32(*(p++));
408 * tmp |= ~0UL >> (32-offset);
410 * but this would decrease preformance, so we change the
414 tmp |= __swab32(~0UL >> (32-offset));
422 while(size & ~31UL) {
433 /* tmp is little endian, so we would have to swab the shift,
434 * see above. But then we have to swab tmp below for ffz, so
435 * we might as well do this here.
437 return result + ffz(__swab32(tmp) | (~0UL << size));
439 return result + ffz(__swab32(tmp));
443 #define ext2_set_bit_atomic(lock, nr, addr) \
447 ret = ext2_set_bit((nr), (addr)); \
452 #define ext2_clear_bit_atomic(lock, nr, addr) \
456 ret = ext2_clear_bit((nr), (addr)); \
461 /* Bitmap functions for the minix filesystem. */
462 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
463 #define minix_set_bit(nr,addr) set_bit(nr,addr)
464 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
465 #define minix_test_bit(nr,addr) test_bit(nr,addr)
466 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
469 * fls: find last bit set.
472 #define fls(x) generic_fls(x)
473 #define fls64(x) generic_fls64(x)
475 #endif /* __KERNEL__ */
477 #endif /* __ASM_SH_BITOPS_H */