1 #ifndef _ASM_IA64_BITOPS_H
2 #define _ASM_IA64_BITOPS_H
5 * Copyright (C) 1998-2003 Hewlett-Packard Co
6 * David Mosberger-Tang <davidm@hpl.hp.com>
8 * 02/06/02 find_next_bit() and find_first_bit() added from Erich Focht's ia64
12 #include <linux/compiler.h>
13 #include <linux/types.h>
14 #include <asm/intrinsics.h>
17 * set_bit - Atomically set a bit in memory
19 * @addr: the address to start counting from
21 * This function is atomic and may not be reordered. See __set_bit()
22 * if you do not require the atomic guarantees.
23 * Note that @nr may be almost arbitrarily large; this function is not
24 * restricted to acting on a single-word quantity.
26 * The address must be (at least) "long" aligned.
27 * Note that there are driver (e.g., eepro100) which use these operations to
28 * operate on hw-defined data-structures, so we can't easily change these
29 * operations to force a bigger alignment.
31 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
33 static __inline__ void
34 set_bit (int nr, volatile void *addr)
40 m = (volatile __u32 *) addr + (nr >> 5);
46 } while (cmpxchg_acq(m, old, new) != old);
50 * __set_bit - Set a bit in memory
52 * @addr: the address to start counting from
54 * Unlike set_bit(), this function is non-atomic and may be reordered.
55 * If it's called on the same region of memory simultaneously, the effect
56 * may be that only one operation succeeds.
58 static __inline__ void
59 __set_bit (int nr, volatile void *addr)
61 *((__u32 *) addr + (nr >> 5)) |= (1 << (nr & 31));
65 * clear_bit() has "acquire" semantics.
67 #define smp_mb__before_clear_bit() smp_mb()
68 #define smp_mb__after_clear_bit() do { /* skip */; } while (0)
71 * clear_bit - Clears a bit in memory
73 * @addr: Address to start counting from
75 * clear_bit() is atomic and may not be reordered. However, it does
76 * not contain a memory barrier, so if it is used for locking purposes,
77 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
78 * in order to ensure changes are visible on other processors.
80 static __inline__ void
81 clear_bit (int nr, volatile void *addr)
87 m = (volatile __u32 *) addr + (nr >> 5);
88 mask = ~(1 << (nr & 31));
93 } while (cmpxchg_acq(m, old, new) != old);
97 * __clear_bit - Clears a bit in memory (non-atomic version)
99 static __inline__ void
100 __clear_bit (int nr, volatile void *addr)
102 volatile __u32 *p = (__u32 *) addr + (nr >> 5);
103 __u32 m = 1 << (nr & 31);
108 * change_bit - Toggle a bit in memory
110 * @addr: Address to start counting from
112 * change_bit() is atomic and may not be reordered.
113 * Note that @nr may be almost arbitrarily large; this function is not
114 * restricted to acting on a single-word quantity.
116 static __inline__ void
117 change_bit (int nr, volatile void *addr)
121 CMPXCHG_BUGCHECK_DECL
123 m = (volatile __u32 *) addr + (nr >> 5);
124 bit = (1 << (nr & 31));
129 } while (cmpxchg_acq(m, old, new) != old);
133 * __change_bit - Toggle a bit in memory
134 * @nr: the bit to set
135 * @addr: the address to start counting from
137 * Unlike change_bit(), this function is non-atomic and may be reordered.
138 * If it's called on the same region of memory simultaneously, the effect
139 * may be that only one operation succeeds.
141 static __inline__ void
142 __change_bit (int nr, volatile void *addr)
144 *((__u32 *) addr + (nr >> 5)) ^= (1 << (nr & 31));
148 * test_and_set_bit - Set a bit and return its old value
150 * @addr: Address to count from
152 * This operation is atomic and cannot be reordered.
153 * It also implies a memory barrier.
155 static __inline__ int
156 test_and_set_bit (int nr, volatile void *addr)
160 CMPXCHG_BUGCHECK_DECL
162 m = (volatile __u32 *) addr + (nr >> 5);
163 bit = 1 << (nr & 31);
168 } while (cmpxchg_acq(m, old, new) != old);
169 return (old & bit) != 0;
173 * __test_and_set_bit - Set a bit and return its old value
175 * @addr: Address to count from
177 * This operation is non-atomic and can be reordered.
178 * If two examples of this operation race, one can appear to succeed
179 * but actually fail. You must protect multiple accesses with a lock.
181 static __inline__ int
182 __test_and_set_bit (int nr, volatile void *addr)
184 __u32 *p = (__u32 *) addr + (nr >> 5);
185 __u32 m = 1 << (nr & 31);
186 int oldbitset = (*p & m) != 0;
193 * test_and_clear_bit - Clear a bit and return its old value
195 * @addr: Address to count from
197 * This operation is atomic and cannot be reordered.
198 * It also implies a memory barrier.
200 static __inline__ int
201 test_and_clear_bit (int nr, volatile void *addr)
203 __u32 mask, old, new;
205 CMPXCHG_BUGCHECK_DECL
207 m = (volatile __u32 *) addr + (nr >> 5);
208 mask = ~(1 << (nr & 31));
213 } while (cmpxchg_acq(m, old, new) != old);
214 return (old & ~mask) != 0;
218 * __test_and_clear_bit - Clear a bit and return its old value
220 * @addr: Address to count from
222 * This operation is non-atomic and can be reordered.
223 * If two examples of this operation race, one can appear to succeed
224 * but actually fail. You must protect multiple accesses with a lock.
226 static __inline__ int
227 __test_and_clear_bit(int nr, volatile void * addr)
229 __u32 *p = (__u32 *) addr + (nr >> 5);
230 __u32 m = 1 << (nr & 31);
231 int oldbitset = *p & m;
238 * test_and_change_bit - Change a bit and return its old value
240 * @addr: Address to count from
242 * This operation is atomic and cannot be reordered.
243 * It also implies a memory barrier.
245 static __inline__ int
246 test_and_change_bit (int nr, volatile void *addr)
250 CMPXCHG_BUGCHECK_DECL
252 m = (volatile __u32 *) addr + (nr >> 5);
253 bit = (1 << (nr & 31));
258 } while (cmpxchg_acq(m, old, new) != old);
259 return (old & bit) != 0;
263 * WARNING: non atomic version.
265 static __inline__ int
266 __test_and_change_bit (int nr, void *addr)
268 __u32 old, bit = (1 << (nr & 31));
269 __u32 *m = (__u32 *) addr + (nr >> 5);
273 return (old & bit) != 0;
276 static __inline__ int
277 test_bit (int nr, const volatile void *addr)
279 return 1 & (((const volatile __u32 *) addr)[nr >> 5] >> (nr & 31));
283 * ffz - find the first zero bit in a long word
284 * @x: The long word to find the bit in
286 * Returns the bit-number (0..63) of the first (least significant) zero bit.
287 * Undefined if no zero exists, so code should check against ~0UL first...
289 static inline unsigned long
290 ffz (unsigned long x)
292 unsigned long result;
294 result = ia64_popcnt(x & (~x - 1));
299 * __ffs - find first bit in word.
300 * @x: The word to search
302 * Undefined if no bit exists, so code should check against 0 first.
304 static __inline__ unsigned long
305 __ffs (unsigned long x)
307 unsigned long result;
309 result = ia64_popcnt((x-1) & ~x);
316 * Return bit number of last (most-significant) bit set. Undefined
317 * for x==0. Bits are numbered from 0..63 (e.g., ia64_fls(9) == 3).
319 static inline unsigned long
320 ia64_fls (unsigned long x)
325 exp = ia64_getf_exp(d);
330 * Find the last (most significant) bit set. Returns 0 for x==0 and
331 * bits are numbered from 1..32 (e.g., fls(9) == 4).
336 unsigned long x = t & 0xffffffffu;
345 return ia64_popcnt(x);
348 #include <asm-generic/bitops/fls64.h>
351 * ffs: find first bit set. This is defined the same way as the libc and
352 * compiler builtin ffs routines, therefore differs in spirit from the above
353 * ffz (man ffs): it operates on "int" values only and the result value is the
354 * bit number + 1. ffs(0) is defined to return zero.
356 #define ffs(x) __builtin_ffs(x)
359 * hweightN: returns the hamming weight (i.e. the number
360 * of bits set) of a N-bit word
362 static __inline__ unsigned long
363 hweight64 (unsigned long x)
365 unsigned long result;
366 result = ia64_popcnt(x);
370 #define hweight32(x) (unsigned int) hweight64((x) & 0xfffffffful)
371 #define hweight16(x) (unsigned int) hweight64((x) & 0xfffful)
372 #define hweight8(x) (unsigned int) hweight64((x) & 0xfful)
374 #endif /* __KERNEL__ */
376 #include <asm-generic/bitops/find.h>
380 #include <asm-generic/bitops/ext2-non-atomic.h>
382 #define ext2_set_bit_atomic(l,n,a) test_and_set_bit(n,a)
383 #define ext2_clear_bit_atomic(l,n,a) test_and_clear_bit(n,a)
385 #include <asm-generic/bitops/minix.h>
386 #include <asm-generic/bitops/sched.h>
388 #endif /* __KERNEL__ */
390 #endif /* _ASM_IA64_BITOPS_H */