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>
16 #include <asm/system.h>
19 * clear_bit() doesn't provide any barrier for the compiler
21 #define smp_mb__before_clear_bit() barrier()
22 #define smp_mb__after_clear_bit() barrier()
25 * set_bit - Atomically set a bit in memory
27 * @addr: the address to start counting from
29 * This function is atomic and may not be reordered. See __set_bit()
30 * if you do not require the atomic guarantees.
32 * Note that @nr may be almost arbitrarily large; this function is not
33 * restricted to acting on a single-word quantity.
35 static inline void set_bit(int nr, volatile void * addr)
37 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
40 if (__builtin_constant_p(nr)) {
47 : "=&r"(tmp), "=o"(*p)
51 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
58 : "=&r"(tmp), "=o"(*p)
65 * clear_bit - Clears a bit in memory
67 * @addr: Address to start counting from
69 * clear_bit() is atomic and may not be reordered. However, it does
70 * not contain a memory barrier, so if it is used for locking purposes,
71 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
72 * in order to ensure changes are visible on other processors.
74 static inline void clear_bit(int nr, volatile void * addr)
76 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
79 if (__builtin_constant_p(nr)) {
86 : "=&r"(tmp), "=o"(*p)
90 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
97 : "=&r"(tmp), "=o"(*p)
104 * change_bit - Toggle a bit in memory
106 * @addr: Address to start counting from
108 * change_bit() is atomic and may not be reordered.
109 * Note that @nr may be almost arbitrarily large; this function is not
110 * restricted to acting on a single-word quantity.
112 static inline void change_bit(int nr, volatile void * addr)
114 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
115 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
124 : "=&r"(tmp), "=o"(*p)
130 * test_and_set_bit - Set a bit and return its old value
132 * @addr: Address to count from
134 * This operation is atomic and cannot be reordered.
135 * It also implies a memory barrier.
137 static inline int test_and_set_bit(int nr, volatile void * addr)
139 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
140 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
141 unsigned long tmp, old;
143 if (__builtin_constant_p(nr)) {
151 : "=&r"(tmp), "=o"(*p), "=&r"(old)
161 : "=&r"(tmp), "=o"(*p), "=&r"(old)
166 return (old & mask) != 0;
170 * test_and_clear_bit - Clear a bit and return its old value
172 * @addr: Address to count from
174 * This operation is atomic and cannot be reordered.
175 * It also implies a memory barrier.
177 static inline int test_and_clear_bit(int nr, volatile void * addr)
179 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
180 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
181 unsigned long tmp, old;
183 if (__builtin_constant_p(nr)) {
191 : "=&r"(tmp), "=o"(*p), "=&r"(old)
202 : "=&r"(tmp), "=o"(*p), "=&r"(old)
207 return (old & mask) != 0;
211 * test_and_change_bit - Change a bit and return its old value
213 * @addr: Address to count from
215 * This operation is atomic and cannot be reordered.
216 * It also implies a memory barrier.
218 static inline int test_and_change_bit(int nr, volatile void * addr)
220 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
221 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
222 unsigned long tmp, old;
230 : "=&r"(tmp), "=o"(*p), "=&r"(old)
234 return (old & mask) != 0;
237 #include <asm-generic/bitops/non-atomic.h>
239 /* Find First bit Set */
240 static inline unsigned long __ffs(unsigned long word)
242 unsigned long result;
246 : "=r"(result), "=&r"(word)
251 /* Find First Zero */
252 static inline unsigned long ffz(unsigned long word)
257 /* Find Last bit Set */
258 static inline int fls(unsigned long word)
260 unsigned long result;
262 asm("clz %0,%1" : "=r"(result) : "r"(word));
266 unsigned long find_first_zero_bit(const unsigned long *addr,
268 unsigned long find_next_zero_bit(const unsigned long *addr,
270 unsigned long offset);
271 unsigned long find_first_bit(const unsigned long *addr,
273 unsigned long find_next_bit(const unsigned long *addr,
275 unsigned long offset);
278 * ffs: find first bit set. This is defined the same way as
279 * the libc and compiler builtin ffs routines, therefore
280 * differs in spirit from the above ffz (man ffs).
282 * The difference is that bit numbering starts at 1, and if no bit is set,
283 * the function returns 0.
285 static inline int ffs(unsigned long word)
289 return __ffs(word) + 1;
292 #include <asm-generic/bitops/fls64.h>
293 #include <asm-generic/bitops/sched.h>
294 #include <asm-generic/bitops/hweight.h>
295 #include <asm-generic/bitops/lock.h>
297 #include <asm-generic/bitops/ext2-non-atomic.h>
298 #include <asm-generic/bitops/ext2-atomic.h>
299 #include <asm-generic/bitops/minix-le.h>
301 #endif /* __ASM_AVR32_BITOPS_H */