1 /* bitops.h: bit operations for the Fujitsu FR-V CPUs
3 * For an explanation of how atomic ops work in this arch, see:
4 * Documentation/fujitsu/frv/atomic-ops.txt
6 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
7 * Written by David Howells (dhowells@redhat.com)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
17 #include <linux/config.h>
18 #include <linux/compiler.h>
19 #include <asm/byteorder.h>
20 #include <asm/system.h>
21 #include <asm/atomic.h>
26 * ffz = Find First Zero in word. Undefined if no zero exists,
27 * so code should check against ~0UL first..
29 static inline unsigned long ffz(unsigned long word)
31 unsigned long result = 0;
41 * clear_bit() doesn't provide any barrier for the compiler.
43 #define smp_mb__before_clear_bit() barrier()
44 #define smp_mb__after_clear_bit() barrier()
46 static inline int test_and_clear_bit(int nr, volatile void *addr)
48 volatile unsigned long *ptr = addr;
49 unsigned long mask = 1UL << (nr & 31);
51 return (atomic_test_and_ANDNOT_mask(mask, ptr) & mask) != 0;
54 static inline int test_and_set_bit(int nr, volatile void *addr)
56 volatile unsigned long *ptr = addr;
57 unsigned long mask = 1UL << (nr & 31);
59 return (atomic_test_and_OR_mask(mask, ptr) & mask) != 0;
62 static inline int test_and_change_bit(int nr, volatile void *addr)
64 volatile unsigned long *ptr = addr;
65 unsigned long mask = 1UL << (nr & 31);
67 return (atomic_test_and_XOR_mask(mask, ptr) & mask) != 0;
70 static inline void clear_bit(int nr, volatile void *addr)
72 test_and_clear_bit(nr, addr);
75 static inline void set_bit(int nr, volatile void *addr)
77 test_and_set_bit(nr, addr);
80 static inline void change_bit(int nr, volatile void * addr)
82 test_and_change_bit(nr, addr);
85 static inline void __clear_bit(int nr, volatile void * addr)
87 volatile unsigned long *a = addr;
91 mask = 1 << (nr & 31);
95 static inline void __set_bit(int nr, volatile void * addr)
97 volatile unsigned long *a = addr;
101 mask = 1 << (nr & 31);
105 static inline void __change_bit(int nr, volatile void *addr)
107 volatile unsigned long *a = addr;
111 mask = 1 << (nr & 31);
115 static inline int __test_and_clear_bit(int nr, volatile void * addr)
117 volatile unsigned long *a = addr;
121 mask = 1 << (nr & 31);
122 retval = (mask & *a) != 0;
127 static inline int __test_and_set_bit(int nr, volatile void * addr)
129 volatile unsigned long *a = addr;
133 mask = 1 << (nr & 31);
134 retval = (mask & *a) != 0;
139 static inline int __test_and_change_bit(int nr, volatile void * addr)
141 volatile unsigned long *a = addr;
145 mask = 1 << (nr & 31);
146 retval = (mask & *a) != 0;
152 * This routine doesn't need to be atomic.
154 static inline int __constant_test_bit(int nr, const volatile void * addr)
156 return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
159 static inline int __test_bit(int nr, const volatile void * addr)
161 int * a = (int *) addr;
165 mask = 1 << (nr & 0x1f);
166 return ((mask & *a) != 0);
169 #define test_bit(nr,addr) \
170 (__builtin_constant_p(nr) ? \
171 __constant_test_bit((nr),(addr)) : \
172 __test_bit((nr),(addr)))
174 extern int find_next_bit(const unsigned long *addr, int size, int offset);
176 #define find_first_bit(addr, size) find_next_bit(addr, size, 0)
178 #define find_first_zero_bit(addr, size) \
179 find_next_zero_bit((addr), (size), 0)
181 static inline int find_next_zero_bit(const void *addr, int size, int offset)
183 const unsigned long *p = ((const unsigned long *) addr) + (offset >> 5);
184 unsigned long result = offset & ~31UL;
193 tmp |= ~0UL >> (32-offset);
201 while (size & ~31UL) {
214 return result + ffz(tmp);
217 #define ffs(x) generic_ffs(x)
218 #define __ffs(x) (ffs(x) - 1)
221 * fls: find last bit set.
227 asm("scan %1,gr0,%0" : "=r"(bit) : "r"(x)); \
229 bit ? 33 - bit : bit; \
233 * Every architecture must define this function. It's the fastest
234 * way of searching a 140-bit bitmap where the first 100 bits are
235 * unlikely to be set. It's guaranteed that at least one of the 140
238 static inline int sched_find_first_bit(const unsigned long *b)
243 return __ffs(b[1]) + 32;
245 return __ffs(b[2]) + 64;
247 return __ffs(b[3]) + 96;
248 return __ffs(b[4]) + 128;
253 * hweightN: returns the hamming weight (i.e. the number
254 * of bits set) of a N-bit word
257 #define hweight32(x) generic_hweight32(x)
258 #define hweight16(x) generic_hweight16(x)
259 #define hweight8(x) generic_hweight8(x)
261 #define ext2_set_bit(nr, addr) test_and_set_bit ((nr) ^ 0x18, (addr))
262 #define ext2_clear_bit(nr, addr) test_and_clear_bit((nr) ^ 0x18, (addr))
264 #define ext2_set_bit_atomic(lock,nr,addr) ext2_set_bit((nr), addr)
265 #define ext2_clear_bit_atomic(lock,nr,addr) ext2_clear_bit((nr), addr)
267 static inline int ext2_test_bit(int nr, const volatile void * addr)
269 const volatile unsigned char *ADDR = (const unsigned char *) addr;
273 mask = 1 << (nr & 0x07);
274 return ((mask & *ADDR) != 0);
277 #define ext2_find_first_zero_bit(addr, size) \
278 ext2_find_next_zero_bit((addr), (size), 0)
280 static inline unsigned long ext2_find_next_zero_bit(const void *addr,
282 unsigned long offset)
284 const unsigned long *p = ((const unsigned long *) addr) + (offset >> 5);
285 unsigned long result = offset & ~31UL;
293 /* We hold the little endian value in tmp, but then the
294 * shift is illegal. So we could keep a big endian value
297 * tmp = __swab32(*(p++));
298 * tmp |= ~0UL >> (32-offset);
300 * but this would decrease preformance, so we change the
304 tmp |= __swab32(~0UL >> (32-offset));
312 while(size & ~31UL) {
323 /* tmp is little endian, so we would have to swab the shift,
324 * see above. But then we have to swab tmp below for ffz, so
325 * we might as well do this here.
327 return result + ffz(__swab32(tmp) | (~0UL << size));
329 return result + ffz(__swab32(tmp));
332 /* Bitmap functions for the minix filesystem. */
333 #define minix_test_and_set_bit(nr,addr) ext2_set_bit(nr,addr)
334 #define minix_set_bit(nr,addr) ext2_set_bit(nr,addr)
335 #define minix_test_and_clear_bit(nr,addr) ext2_clear_bit(nr,addr)
336 #define minix_test_bit(nr,addr) ext2_test_bit(nr,addr)
337 #define minix_find_first_zero_bit(addr,size) ext2_find_first_zero_bit(addr,size)
339 #endif /* __KERNEL__ */
341 #endif /* _ASM_BITOPS_H */