2 * Copyright (C) 2004-2006 Atmel Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 #ifndef __ASM_AVR32_BITOPS_H
9 #define __ASM_AVR32_BITOPS_H
11 #ifndef _LINUX_BITOPS_H
12 #error only <linux/bitops.h> can be included directly
15 #include <asm/byteorder.h>
18 * clear_bit() doesn't provide any barrier for the compiler
20 #define smp_mb__before_clear_bit() barrier()
21 #define smp_mb__after_clear_bit() barrier()
24 * set_bit - Atomically set a bit in memory
26 * @addr: the address to start counting from
28 * This function is atomic and may not be reordered. See __set_bit()
29 * if you do not require the atomic guarantees.
31 * Note that @nr may be almost arbitrarily large; this function is not
32 * restricted to acting on a single-word quantity.
34 static inline void set_bit(int nr, volatile void * addr)
36 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
39 if (__builtin_constant_p(nr)) {
46 : "=&r"(tmp), "=o"(*p)
50 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
57 : "=&r"(tmp), "=o"(*p)
64 * clear_bit - Clears a bit in memory
66 * @addr: Address to start counting from
68 * clear_bit() is atomic and may not be reordered. However, it does
69 * not contain a memory barrier, so if it is used for locking purposes,
70 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
71 * in order to ensure changes are visible on other processors.
73 static inline void clear_bit(int nr, volatile void * addr)
75 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
78 if (__builtin_constant_p(nr)) {
85 : "=&r"(tmp), "=o"(*p)
89 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
96 : "=&r"(tmp), "=o"(*p)
103 * change_bit - Toggle a bit in memory
105 * @addr: Address to start counting from
107 * change_bit() is atomic and may not be reordered.
108 * Note that @nr may be almost arbitrarily large; this function is not
109 * restricted to acting on a single-word quantity.
111 static inline void change_bit(int nr, volatile void * addr)
113 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
114 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
123 : "=&r"(tmp), "=o"(*p)
129 * test_and_set_bit - Set a bit and return its old value
131 * @addr: Address to count from
133 * This operation is atomic and cannot be reordered.
134 * It also implies a memory barrier.
136 static inline int test_and_set_bit(int nr, volatile void * addr)
138 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
139 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
140 unsigned long tmp, old;
142 if (__builtin_constant_p(nr)) {
150 : "=&r"(tmp), "=o"(*p), "=&r"(old)
160 : "=&r"(tmp), "=o"(*p), "=&r"(old)
165 return (old & mask) != 0;
169 * test_and_clear_bit - Clear a bit and return its old value
171 * @addr: Address to count from
173 * This operation is atomic and cannot be reordered.
174 * It also implies a memory barrier.
176 static inline int test_and_clear_bit(int nr, volatile void * addr)
178 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
179 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
180 unsigned long tmp, old;
182 if (__builtin_constant_p(nr)) {
190 : "=&r"(tmp), "=o"(*p), "=&r"(old)
201 : "=&r"(tmp), "=o"(*p), "=&r"(old)
206 return (old & mask) != 0;
210 * test_and_change_bit - Change a bit and return its old value
212 * @addr: Address to count from
214 * This operation is atomic and cannot be reordered.
215 * It also implies a memory barrier.
217 static inline int test_and_change_bit(int nr, volatile void * addr)
219 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
220 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
221 unsigned long tmp, old;
229 : "=&r"(tmp), "=o"(*p), "=&r"(old)
233 return (old & mask) != 0;
236 #include <asm-generic/bitops/non-atomic.h>
238 /* Find First bit Set */
239 static inline unsigned long __ffs(unsigned long word)
241 unsigned long result;
245 : "=r"(result), "=&r"(word)
250 /* Find First Zero */
251 static inline unsigned long ffz(unsigned long word)
256 /* Find Last bit Set */
257 static inline int fls(unsigned long word)
259 unsigned long result;
261 asm("clz %0,%1" : "=r"(result) : "r"(word));
265 static inline int __fls(unsigned long word)
267 return fls(word) - 1;
270 unsigned long find_first_zero_bit(const unsigned long *addr,
272 #define find_first_zero_bit find_first_zero_bit
274 unsigned long find_next_zero_bit(const unsigned long *addr,
276 unsigned long offset);
277 #define find_next_zero_bit find_next_zero_bit
279 unsigned long find_first_bit(const unsigned long *addr,
281 #define find_first_bit find_first_bit
283 unsigned long find_next_bit(const unsigned long *addr,
285 unsigned long offset);
286 #define find_next_bit find_next_bit
289 * ffs: find first bit set. This is defined the same way as
290 * the libc and compiler builtin ffs routines, therefore
291 * differs in spirit from the above ffz (man ffs).
293 * The difference is that bit numbering starts at 1, and if no bit is set,
294 * the function returns 0.
296 static inline int ffs(unsigned long word)
300 return __ffs(word) + 1;
303 #include <asm-generic/bitops/fls64.h>
304 #include <asm-generic/bitops/sched.h>
305 #include <asm-generic/bitops/hweight.h>
306 #include <asm-generic/bitops/lock.h>
308 extern unsigned long find_next_zero_bit_le(const void *addr,
309 unsigned long size, unsigned long offset);
310 #define find_next_zero_bit_le find_next_zero_bit_le
312 extern unsigned long find_next_bit_le(const void *addr,
313 unsigned long size, unsigned long offset);
314 #define find_next_bit_le find_next_bit_le
316 #include <asm-generic/bitops/le.h>
317 #include <asm-generic/bitops/ext2-atomic.h>
319 #endif /* __ASM_AVR32_BITOPS_H */