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/barrier.h>
19 * set_bit - Atomically set a bit in memory
21 * @addr: the address to start counting from
23 * This function is atomic and may not be reordered. See __set_bit()
24 * if you do not require the atomic guarantees.
26 * Note that @nr may be almost arbitrarily large; this function is not
27 * restricted to acting on a single-word quantity.
29 static inline void set_bit(int nr, volatile void * addr)
31 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
34 if (__builtin_constant_p(nr)) {
41 : "=&r"(tmp), "=o"(*p)
45 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
52 : "=&r"(tmp), "=o"(*p)
59 * clear_bit - Clears a bit in memory
61 * @addr: Address to start counting from
63 * clear_bit() is atomic and may not be reordered. However, it does
64 * not contain a memory barrier, so if it is used for locking purposes,
65 * you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
66 * in order to ensure changes are visible on other processors.
68 static inline void clear_bit(int nr, volatile void * addr)
70 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
73 if (__builtin_constant_p(nr)) {
80 : "=&r"(tmp), "=o"(*p)
84 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
91 : "=&r"(tmp), "=o"(*p)
98 * change_bit - Toggle a bit in memory
100 * @addr: Address to start counting from
102 * change_bit() is atomic and may not be reordered.
103 * Note that @nr may be almost arbitrarily large; this function is not
104 * restricted to acting on a single-word quantity.
106 static inline void change_bit(int nr, volatile void * addr)
108 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
109 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
118 : "=&r"(tmp), "=o"(*p)
124 * test_and_set_bit - Set a bit and return its old value
126 * @addr: Address to count from
128 * This operation is atomic and cannot be reordered.
129 * It also implies a memory barrier.
131 static inline int test_and_set_bit(int nr, volatile void * addr)
133 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
134 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
135 unsigned long tmp, old;
137 if (__builtin_constant_p(nr)) {
145 : "=&r"(tmp), "=o"(*p), "=&r"(old)
155 : "=&r"(tmp), "=o"(*p), "=&r"(old)
160 return (old & mask) != 0;
164 * test_and_clear_bit - Clear a bit and return its old value
166 * @addr: Address to count from
168 * This operation is atomic and cannot be reordered.
169 * It also implies a memory barrier.
171 static inline int test_and_clear_bit(int nr, volatile void * addr)
173 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
174 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
175 unsigned long tmp, old;
177 if (__builtin_constant_p(nr)) {
185 : "=&r"(tmp), "=o"(*p), "=&r"(old)
196 : "=&r"(tmp), "=o"(*p), "=&r"(old)
201 return (old & mask) != 0;
205 * test_and_change_bit - Change a bit and return its old value
207 * @addr: Address to count from
209 * This operation is atomic and cannot be reordered.
210 * It also implies a memory barrier.
212 static inline int test_and_change_bit(int nr, volatile void * addr)
214 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
215 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
216 unsigned long tmp, old;
224 : "=&r"(tmp), "=o"(*p), "=&r"(old)
228 return (old & mask) != 0;
231 #include <asm-generic/bitops/non-atomic.h>
233 /* Find First bit Set */
234 static inline unsigned long __ffs(unsigned long word)
236 unsigned long result;
240 : "=r"(result), "=&r"(word)
245 /* Find First Zero */
246 static inline unsigned long ffz(unsigned long word)
251 /* Find Last bit Set */
252 static inline int fls(unsigned long word)
254 unsigned long result;
256 asm("clz %0,%1" : "=r"(result) : "r"(word));
260 static inline int __fls(unsigned long word)
262 return fls(word) - 1;
265 unsigned long find_first_zero_bit(const unsigned long *addr,
267 #define find_first_zero_bit find_first_zero_bit
269 unsigned long find_next_zero_bit(const unsigned long *addr,
271 unsigned long offset);
272 #define find_next_zero_bit find_next_zero_bit
274 unsigned long find_first_bit(const unsigned long *addr,
276 #define find_first_bit find_first_bit
278 unsigned long find_next_bit(const unsigned long *addr,
280 unsigned long offset);
281 #define find_next_bit find_next_bit
284 * ffs: find first bit set. This is defined the same way as
285 * the libc and compiler builtin ffs routines, therefore
286 * differs in spirit from the above ffz (man ffs).
288 * The difference is that bit numbering starts at 1, and if no bit is set,
289 * the function returns 0.
291 static inline int ffs(unsigned long word)
295 return __ffs(word) + 1;
298 #include <asm-generic/bitops/fls64.h>
299 #include <asm-generic/bitops/sched.h>
300 #include <asm-generic/bitops/hweight.h>
301 #include <asm-generic/bitops/lock.h>
303 extern unsigned long find_next_zero_bit_le(const void *addr,
304 unsigned long size, unsigned long offset);
305 #define find_next_zero_bit_le find_next_zero_bit_le
307 extern unsigned long find_next_bit_le(const void *addr,
308 unsigned long size, unsigned long offset);
309 #define find_next_bit_le find_next_bit_le
311 #include <asm-generic/bitops/le.h>
312 #include <asm-generic/bitops/ext2-atomic.h>
314 #endif /* __ASM_AVR32_BITOPS_H */