1 /* asm/bitops.h for Linux/CRIS
3 * TODO: asm versions if speed is needed
5 * All bit operations return 0 if the bit was cleared before the
6 * operation and != 0 if it was not.
8 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
11 #ifndef _CRIS_BITOPS_H
12 #define _CRIS_BITOPS_H
14 /* Currently this is unsuitable for consumption outside the kernel. */
17 #include <asm/arch/bitops.h>
18 #include <asm/system.h>
19 #include <asm/atomic.h>
20 #include <linux/compiler.h>
23 * Some hacks to defeat gcc over-optimizations..
25 struct __dummy { unsigned long a[100]; };
26 #define ADDR (*(struct __dummy *) addr)
27 #define CONST_ADDR (*(const struct __dummy *) addr)
30 * set_bit - Atomically set a bit in memory
32 * @addr: the address to start counting from
34 * This function is atomic and may not be reordered. See __set_bit()
35 * if you do not require the atomic guarantees.
36 * Note that @nr may be almost arbitrarily large; this function is not
37 * restricted to acting on a single-word quantity.
40 #define set_bit(nr, addr) (void)test_and_set_bit(nr, addr)
42 #define __set_bit(nr, addr) (void)__test_and_set_bit(nr, addr)
45 * clear_bit - Clears a bit in memory
47 * @addr: Address to start counting from
49 * clear_bit() is atomic and may not be reordered. However, it does
50 * not contain a memory barrier, so if it is used for locking purposes,
51 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
52 * in order to ensure changes are visible on other processors.
55 #define clear_bit(nr, addr) (void)test_and_clear_bit(nr, addr)
57 #define __clear_bit(nr, addr) (void)__test_and_clear_bit(nr, addr)
60 * change_bit - Toggle a bit in memory
62 * @addr: Address to start counting from
64 * change_bit() is atomic and may not be reordered.
65 * Note that @nr may be almost arbitrarily large; this function is not
66 * restricted to acting on a single-word quantity.
69 #define change_bit(nr, addr) (void)test_and_change_bit(nr, addr)
72 * __change_bit - Toggle a bit in memory
73 * @nr: the bit to change
74 * @addr: the address to start counting from
76 * Unlike change_bit(), this function is non-atomic and may be reordered.
77 * If it's called on the same region of memory simultaneously, the effect
78 * may be that only one operation succeeds.
81 #define __change_bit(nr, addr) (void)__test_and_change_bit(nr, addr)
84 * test_and_set_bit - Set a bit and return its old value
86 * @addr: Address to count from
88 * This operation is atomic and cannot be reordered.
89 * It also implies a memory barrier.
92 static inline int test_and_set_bit(int nr, volatile unsigned long *addr)
94 unsigned int mask, retval;
96 unsigned int *adr = (unsigned int *)addr;
99 mask = 1 << (nr & 0x1f);
100 cris_atomic_save(addr, flags);
101 retval = (mask & *adr) != 0;
103 cris_atomic_restore(addr, flags);
104 local_irq_restore(flags);
108 static inline int __test_and_set_bit(int nr, volatile unsigned long *addr)
110 unsigned int mask, retval;
111 unsigned int *adr = (unsigned int *)addr;
114 mask = 1 << (nr & 0x1f);
115 retval = (mask & *adr) != 0;
121 * clear_bit() doesn't provide any barrier for the compiler.
123 #define smp_mb__before_clear_bit() barrier()
124 #define smp_mb__after_clear_bit() barrier()
127 * test_and_clear_bit - Clear a bit and return its old value
129 * @addr: Address to count from
131 * This operation is atomic and cannot be reordered.
132 * It also implies a memory barrier.
135 static inline int test_and_clear_bit(int nr, volatile unsigned long *addr)
137 unsigned int mask, retval;
139 unsigned int *adr = (unsigned int *)addr;
142 mask = 1 << (nr & 0x1f);
143 cris_atomic_save(addr, flags);
144 retval = (mask & *adr) != 0;
146 cris_atomic_restore(addr, flags);
151 * __test_and_clear_bit - Clear a bit and return its old value
153 * @addr: Address to count from
155 * This operation is non-atomic and can be reordered.
156 * If two examples of this operation race, one can appear to succeed
157 * but actually fail. You must protect multiple accesses with a lock.
160 static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)
162 unsigned int mask, retval;
163 unsigned int *adr = (unsigned int *)addr;
166 mask = 1 << (nr & 0x1f);
167 retval = (mask & *adr) != 0;
172 * test_and_change_bit - Change a bit and return its old value
174 * @addr: Address to count from
176 * This operation is atomic and cannot be reordered.
177 * It also implies a memory barrier.
180 static inline int test_and_change_bit(int nr, volatile unsigned long *addr)
182 unsigned int mask, retval;
184 unsigned int *adr = (unsigned int *)addr;
186 mask = 1 << (nr & 0x1f);
187 cris_atomic_save(addr, flags);
188 retval = (mask & *adr) != 0;
190 cris_atomic_restore(addr, flags);
194 /* WARNING: non atomic and it can be reordered! */
196 static inline int __test_and_change_bit(int nr, volatile unsigned long *addr)
198 unsigned int mask, retval;
199 unsigned int *adr = (unsigned int *)addr;
202 mask = 1 << (nr & 0x1f);
203 retval = (mask & *adr) != 0;
210 * test_bit - Determine whether a bit is set
211 * @nr: bit number to test
212 * @addr: Address to start counting from
214 * This routine doesn't need to be atomic.
217 static inline int test_bit(int nr, const volatile unsigned long *addr)
220 unsigned int *adr = (unsigned int *)addr;
223 mask = 1 << (nr & 0x1f);
224 return ((mask & *adr) != 0);
228 * Find-bit routines..
232 * Since we define it "external", it collides with the built-in
233 * definition, which doesn't have the same semantics. We don't want to
234 * use -fno-builtin, so just hide the name ffs.
236 #define ffs kernel_ffs
239 * fls: find last bit set.
242 #define fls(x) generic_fls(x)
243 #define fls64(x) generic_fls64(x)
246 * hweightN - returns the hamming weight of a N-bit word
247 * @x: the word to weigh
249 * The Hamming Weight of a number is the total number of bits set in it.
252 #define hweight32(x) generic_hweight32(x)
253 #define hweight16(x) generic_hweight16(x)
254 #define hweight8(x) generic_hweight8(x)
257 * find_next_zero_bit - find the first zero bit in a memory region
258 * @addr: The address to base the search on
259 * @offset: The bitnumber to start searching at
260 * @size: The maximum size to search
262 static inline int find_next_zero_bit (const unsigned long * 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_bit - find the first set bit in a memory region
300 * @addr: The address to base the search on
301 * @offset: The bitnumber to start searching at
302 * @size: The maximum size to search
304 static __inline__ int find_next_bit(const unsigned long *addr, int size, int offset)
306 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
307 unsigned long result = offset & ~31UL;
316 tmp &= (~0UL << offset);
324 while (size & ~31UL) {
335 tmp &= (~0UL >> (32 - size));
336 if (tmp == 0UL) /* Are any bits set? */
337 return result + size; /* Nope. */
339 return result + __ffs(tmp);
343 * find_first_zero_bit - find the first zero bit in a memory region
344 * @addr: The address to start the search at
345 * @size: The maximum size to search
347 * Returns the bit-number of the first zero bit, not the number of the byte
351 #define find_first_zero_bit(addr, size) \
352 find_next_zero_bit((addr), (size), 0)
353 #define find_first_bit(addr, size) \
354 find_next_bit((addr), (size), 0)
356 #define ext2_set_bit test_and_set_bit
357 #define ext2_set_bit_atomic(l,n,a) test_and_set_bit(n,a)
358 #define ext2_clear_bit test_and_clear_bit
359 #define ext2_clear_bit_atomic(l,n,a) test_and_clear_bit(n,a)
360 #define ext2_test_bit test_bit
361 #define ext2_find_first_zero_bit find_first_zero_bit
362 #define ext2_find_next_zero_bit find_next_zero_bit
364 /* Bitmap functions for the minix filesystem. */
365 #define minix_set_bit(nr,addr) test_and_set_bit(nr,addr)
366 #define minix_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
367 #define minix_test_bit(nr,addr) test_bit(nr,addr)
368 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
370 static inline int sched_find_first_bit(const unsigned long *b)
375 return __ffs(b[1]) + 32;
377 return __ffs(b[2]) + 64;
379 return __ffs(b[3]) + 96;
381 return __ffs(b[4]) + 128;
382 return __ffs(b[5]) + 32 + 128;
385 #endif /* __KERNEL__ */
387 #endif /* _CRIS_BITOPS_H */