1 #ifndef __ASM_SH64_BITOPS_H
2 #define __ASM_SH64_BITOPS_H
5 * This file is subject to the terms and conditions of the GNU General Public
6 * License. See the file "COPYING" in the main directory of this archive
9 * include/asm-sh64/bitops.h
11 * Copyright (C) 2000, 2001 Paolo Alberelli
12 * Copyright (C) 2003 Paul Mundt
16 #include <linux/compiler.h>
17 #include <asm/system.h>
19 #include <asm/byteorder.h>
21 static __inline__ void set_bit(int nr, volatile void * addr)
24 volatile unsigned int *a = addr;
28 mask = 1 << (nr & 0x1f);
29 local_irq_save(flags);
31 local_irq_restore(flags);
34 static inline void __set_bit(int nr, void *addr)
37 unsigned int *a = addr;
40 mask = 1 << (nr & 0x1f);
45 * clear_bit() doesn't provide any barrier for the compiler.
47 #define smp_mb__before_clear_bit() barrier()
48 #define smp_mb__after_clear_bit() barrier()
49 static inline void clear_bit(int nr, volatile unsigned long *a)
55 mask = 1 << (nr & 0x1f);
56 local_irq_save(flags);
58 local_irq_restore(flags);
61 static inline void __clear_bit(int nr, volatile unsigned long *a)
66 mask = 1 << (nr & 0x1f);
70 static __inline__ void change_bit(int nr, volatile void * addr)
73 volatile unsigned int *a = addr;
77 mask = 1 << (nr & 0x1f);
78 local_irq_save(flags);
80 local_irq_restore(flags);
83 static __inline__ void __change_bit(int nr, volatile void * addr)
86 volatile unsigned int *a = addr;
89 mask = 1 << (nr & 0x1f);
93 static __inline__ int test_and_set_bit(int nr, volatile void * addr)
96 volatile unsigned int *a = addr;
100 mask = 1 << (nr & 0x1f);
101 local_irq_save(flags);
102 retval = (mask & *a) != 0;
104 local_irq_restore(flags);
109 static __inline__ int __test_and_set_bit(int nr, volatile void * addr)
112 volatile unsigned int *a = addr;
115 mask = 1 << (nr & 0x1f);
116 retval = (mask & *a) != 0;
122 static __inline__ int test_and_clear_bit(int nr, volatile void * addr)
125 volatile unsigned int *a = addr;
129 mask = 1 << (nr & 0x1f);
130 local_irq_save(flags);
131 retval = (mask & *a) != 0;
133 local_irq_restore(flags);
138 static __inline__ int __test_and_clear_bit(int nr, volatile void * addr)
141 volatile unsigned int *a = addr;
144 mask = 1 << (nr & 0x1f);
145 retval = (mask & *a) != 0;
151 static __inline__ int test_and_change_bit(int nr, volatile void * addr)
154 volatile unsigned int *a = addr;
158 mask = 1 << (nr & 0x1f);
159 local_irq_save(flags);
160 retval = (mask & *a) != 0;
162 local_irq_restore(flags);
167 static __inline__ int __test_and_change_bit(int nr, volatile void * addr)
170 volatile unsigned int *a = addr;
173 mask = 1 << (nr & 0x1f);
174 retval = (mask & *a) != 0;
180 static __inline__ int test_bit(int nr, const volatile void *addr)
182 return 1UL & (((const volatile unsigned int *) addr)[nr >> 5] >> (nr & 31));
185 static __inline__ unsigned long ffz(unsigned long word)
187 unsigned long result, __d2, __d3;
189 __asm__("gettr tr0, %2\n\t"
192 "beq %3, r63, tr0\n\t"
195 "shlri.l %1, 1, %1\n\t"
201 : "=r" (result), "=r" (word), "=r" (__d2), "=r" (__d3)
202 : "0" (0L), "1" (word));
208 * __ffs - find first bit in word
209 * @word: The word to search
211 * Undefined if no bit exists, so code should check against 0 first.
213 static inline unsigned long __ffs(unsigned long word)
219 if (!(word & 0xffff)) {
223 if (!(word & 0xff)) {
243 * find_next_bit - find the next set bit in a memory region
244 * @addr: The address to base the search on
245 * @offset: The bitnumber to start searching at
246 * @size: The maximum size to search
248 static inline unsigned long find_next_bit(const unsigned long *addr,
249 unsigned long size, unsigned long offset)
251 unsigned int *p = ((unsigned int *) addr) + (offset >> 5);
252 unsigned int result = offset & ~31UL;
261 tmp &= ~0UL << offset;
270 if ((tmp = *p++) != 0)
280 tmp &= ~0UL >> (32 - size);
281 if (tmp == 0UL) /* Are any bits set? */
282 return result + size; /* Nope. */
284 return result + __ffs(tmp);
288 * find_first_bit - find the first set bit in a memory region
289 * @addr: The address to start the search at
290 * @size: The maximum size to search
292 * Returns the bit-number of the first set bit, not the number of the byte
295 #define find_first_bit(addr, size) \
296 find_next_bit((addr), (size), 0)
299 static inline int find_next_zero_bit(void *addr, int size, int offset)
301 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
302 unsigned long result = offset & ~31UL;
311 tmp |= ~0UL >> (32-offset);
319 while (size & ~31UL) {
332 return result + ffz(tmp);
335 #define find_first_zero_bit(addr, size) \
336 find_next_zero_bit((addr), (size), 0)
339 * hweightN: returns the hamming weight (i.e. the number
340 * of bits set) of a N-bit word
343 #define hweight32(x) generic_hweight32(x)
344 #define hweight16(x) generic_hweight16(x)
345 #define hweight8(x) generic_hweight8(x)
348 * Every architecture must define this function. It's the fastest
349 * way of searching a 140-bit bitmap where the first 100 bits are
350 * unlikely to be set. It's guaranteed that at least one of the 140
354 static inline int sched_find_first_bit(unsigned long *b)
359 return __ffs(b[1]) + 32;
361 return __ffs(b[2]) + 64;
363 return __ffs(b[3]) + 96;
364 return __ffs(b[4]) + 128;
368 * ffs: find first bit set. This is defined the same way as
369 * the libc and compiler builtin ffs routines, therefore
370 * differs in spirit from the above ffz (man ffs).
373 #define ffs(x) generic_ffs(x)
376 * hweightN: returns the hamming weight (i.e. the number
377 * of bits set) of a N-bit word
380 #define hweight32(x) generic_hweight32(x)
381 #define hweight16(x) generic_hweight16(x)
382 #define hweight8(x) generic_hweight8(x)
384 #ifdef __LITTLE_ENDIAN__
385 #define ext2_set_bit(nr, addr) test_and_set_bit((nr), (addr))
386 #define ext2_clear_bit(nr, addr) test_and_clear_bit((nr), (addr))
387 #define ext2_test_bit(nr, addr) test_bit((nr), (addr))
388 #define ext2_find_first_zero_bit(addr, size) find_first_zero_bit((addr), (size))
389 #define ext2_find_next_zero_bit(addr, size, offset) \
390 find_next_zero_bit((addr), (size), (offset))
392 static __inline__ int ext2_set_bit(int nr, volatile void * addr)
396 volatile unsigned char *ADDR = (unsigned char *) addr;
399 mask = 1 << (nr & 0x07);
400 local_irq_save(flags);
401 retval = (mask & *ADDR) != 0;
403 local_irq_restore(flags);
407 static __inline__ int ext2_clear_bit(int nr, volatile void * addr)
411 volatile unsigned char *ADDR = (unsigned char *) addr;
414 mask = 1 << (nr & 0x07);
415 local_irq_save(flags);
416 retval = (mask & *ADDR) != 0;
418 local_irq_restore(flags);
422 static __inline__ int ext2_test_bit(int nr, const volatile void * addr)
425 const volatile unsigned char *ADDR = (const unsigned char *) addr;
428 mask = 1 << (nr & 0x07);
429 return ((mask & *ADDR) != 0);
432 #define ext2_find_first_zero_bit(addr, size) \
433 ext2_find_next_zero_bit((addr), (size), 0)
435 static __inline__ unsigned long ext2_find_next_zero_bit(void *addr, unsigned long size, unsigned long offset)
437 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
438 unsigned long result = offset & ~31UL;
446 /* We hold the little endian value in tmp, but then the
447 * shift is illegal. So we could keep a big endian value
450 * tmp = __swab32(*(p++));
451 * tmp |= ~0UL >> (32-offset);
453 * but this would decrease preformance, so we change the
457 tmp |= __swab32(~0UL >> (32-offset));
465 while(size & ~31UL) {
476 /* tmp is little endian, so we would have to swab the shift,
477 * see above. But then we have to swab tmp below for ffz, so
478 * we might as well do this here.
480 return result + ffz(__swab32(tmp) | (~0UL << size));
482 return result + ffz(__swab32(tmp));
486 #define ext2_set_bit_atomic(lock, nr, addr) \
490 ret = ext2_set_bit((nr), (addr)); \
495 #define ext2_clear_bit_atomic(lock, nr, addr) \
499 ret = ext2_clear_bit((nr), (addr)); \
504 /* Bitmap functions for the minix filesystem. */
505 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
506 #define minix_set_bit(nr,addr) set_bit(nr,addr)
507 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
508 #define minix_test_bit(nr,addr) test_bit(nr,addr)
509 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
511 #define ffs(x) generic_ffs(x)
512 #define fls(x) generic_fls(x)
514 #endif /* __KERNEL__ */
516 #endif /* __ASM_SH64_BITOPS_H */