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 O(1)
12 #include <linux/compiler.h>
13 #include <linux/types.h>
14 #include <asm/bitops.h>
15 #include <asm/intrinsics.h>
18 * set_bit - Atomically set a bit in memory
20 * @addr: the address to start counting from
22 * This function is atomic and may not be reordered. See __set_bit()
23 * if you do not require the atomic guarantees.
24 * Note that @nr may be almost arbitrarily large; this function is not
25 * restricted to acting on a single-word quantity.
27 * The address must be (at least) "long" aligned.
28 * Note that there are driver (e.g., eepro100) which use these operations to operate on
29 * hw-defined data-structures, so we can't easily change these operations to force a
32 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
34 static __inline__
void
35 set_bit (int nr
, volatile void *addr
)
41 m
= (volatile __u32
*) addr
+ (nr
>> 5);
47 } while (cmpxchg_acq(m
, old
, new) != old
);
51 * __set_bit - Set a bit in memory
53 * @addr: the address to start counting from
55 * Unlike set_bit(), this function is non-atomic and may be reordered.
56 * If it's called on the same region of memory simultaneously, the effect
57 * may be that only one operation succeeds.
59 static __inline__
void
60 __set_bit (int nr
, volatile void *addr
)
62 *((__u32
*) addr
+ (nr
>> 5)) |= (1 << (nr
& 31));
66 * clear_bit() has "acquire" semantics.
68 #define smp_mb__before_clear_bit() smp_mb()
69 #define smp_mb__after_clear_bit() do { /* skip */; } while (0)
72 * clear_bit - Clears a bit in memory
74 * @addr: Address to start counting from
76 * clear_bit() is atomic and may not be reordered. However, it does
77 * not contain a memory barrier, so if it is used for locking purposes,
78 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
79 * in order to ensure changes are visible on other processors.
81 static __inline__
void
82 clear_bit (int nr
, volatile void *addr
)
88 m
= (volatile __u32
*) addr
+ (nr
>> 5);
89 mask
= ~(1 << (nr
& 31));
94 } while (cmpxchg_acq(m
, old
, new) != old
);
98 * __clear_bit - Clears a bit in memory (non-atomic version)
100 static __inline__
void
101 __clear_bit (int nr
, volatile void *addr
)
103 volatile __u32
*p
= (__u32
*) addr
+ (nr
>> 5);
104 __u32 m
= 1 << (nr
& 31);
109 * change_bit - Toggle a bit in memory
111 * @addr: Address to start counting from
113 * change_bit() is atomic and may not be reordered.
114 * Note that @nr may be almost arbitrarily large; this function is not
115 * restricted to acting on a single-word quantity.
117 static __inline__
void
118 change_bit (int nr
, volatile void *addr
)
122 CMPXCHG_BUGCHECK_DECL
124 m
= (volatile __u32
*) addr
+ (nr
>> 5);
125 bit
= (1 << (nr
& 31));
130 } while (cmpxchg_acq(m
, old
, new) != old
);
134 * __change_bit - Toggle a bit in memory
135 * @nr: the bit to set
136 * @addr: the address to start counting from
138 * Unlike change_bit(), this function is non-atomic and may be reordered.
139 * If it's called on the same region of memory simultaneously, the effect
140 * may be that only one operation succeeds.
142 static __inline__
void
143 __change_bit (int nr
, volatile void *addr
)
145 *((__u32
*) addr
+ (nr
>> 5)) ^= (1 << (nr
& 31));
149 * test_and_set_bit - Set a bit and return its old value
151 * @addr: Address to count from
153 * This operation is atomic and cannot be reordered.
154 * It also implies a memory barrier.
156 static __inline__
int
157 test_and_set_bit (int nr
, volatile void *addr
)
161 CMPXCHG_BUGCHECK_DECL
163 m
= (volatile __u32
*) addr
+ (nr
>> 5);
164 bit
= 1 << (nr
& 31);
169 } while (cmpxchg_acq(m
, old
, new) != old
);
170 return (old
& bit
) != 0;
174 * __test_and_set_bit - Set a bit and return its old value
176 * @addr: Address to count from
178 * This operation is non-atomic and can be reordered.
179 * If two examples of this operation race, one can appear to succeed
180 * but actually fail. You must protect multiple accesses with a lock.
182 static __inline__
int
183 __test_and_set_bit (int nr
, volatile void *addr
)
185 __u32
*p
= (__u32
*) addr
+ (nr
>> 5);
186 __u32 m
= 1 << (nr
& 31);
187 int oldbitset
= (*p
& m
) != 0;
194 * test_and_clear_bit - Clear a bit and return its old value
196 * @addr: Address to count from
198 * This operation is atomic and cannot be reordered.
199 * It also implies a memory barrier.
201 static __inline__
int
202 test_and_clear_bit (int nr
, volatile void *addr
)
204 __u32 mask
, old
, new;
206 CMPXCHG_BUGCHECK_DECL
208 m
= (volatile __u32
*) addr
+ (nr
>> 5);
209 mask
= ~(1 << (nr
& 31));
214 } while (cmpxchg_acq(m
, old
, new) != old
);
215 return (old
& ~mask
) != 0;
219 * __test_and_clear_bit - Clear a bit and return its old value
221 * @addr: Address to count from
223 * This operation is non-atomic and can be reordered.
224 * If two examples of this operation race, one can appear to succeed
225 * but actually fail. You must protect multiple accesses with a lock.
227 static __inline__
int
228 __test_and_clear_bit(int nr
, volatile void * addr
)
230 __u32
*p
= (__u32
*) addr
+ (nr
>> 5);
231 __u32 m
= 1 << (nr
& 31);
232 int oldbitset
= *p
& m
;
239 * test_and_change_bit - Change a bit and return its old value
241 * @addr: Address to count from
243 * This operation is atomic and cannot be reordered.
244 * It also implies a memory barrier.
246 static __inline__
int
247 test_and_change_bit (int nr
, volatile void *addr
)
251 CMPXCHG_BUGCHECK_DECL
253 m
= (volatile __u32
*) addr
+ (nr
>> 5);
254 bit
= (1 << (nr
& 31));
259 } while (cmpxchg_acq(m
, old
, new) != old
);
260 return (old
& bit
) != 0;
264 * WARNING: non atomic version.
266 static __inline__
int
267 __test_and_change_bit (int nr
, void *addr
)
269 __u32 old
, bit
= (1 << (nr
& 31));
270 __u32
*m
= (__u32
*) addr
+ (nr
>> 5);
274 return (old
& bit
) != 0;
277 static __inline__
int
278 test_bit (int nr
, const volatile void *addr
)
280 return 1 & (((const volatile __u32
*) addr
)[nr
>> 5] >> (nr
& 31));
284 * ffz - find the first zero bit in a long word
285 * @x: The long word to find the bit in
287 * Returns the bit-number (0..63) of the first (least significant) zero bit. Undefined if
288 * no zero exists, so code should check against ~0UL first...
290 static inline unsigned long
291 ffz (unsigned long x
)
293 unsigned long result
;
295 result
= ia64_popcnt(x
& (~x
- 1));
300 * __ffs - find first bit in word.
301 * @x: The word to search
303 * Undefined if no bit exists, so code should check against 0 first.
305 static __inline__
unsigned long
306 __ffs (unsigned long x
)
308 unsigned long result
;
310 result
= ia64_popcnt((x
-1) & ~x
);
317 * find_last_zero_bit - find the last zero bit in a 64 bit quantity
318 * @x: The value to search
320 static inline unsigned long
321 ia64_fls (unsigned long x
)
326 exp
= ia64_getf_exp(d
);
333 return ia64_fls((unsigned int) x
);
337 * ffs: find first bit set. This is defined the same way as the libc and compiler builtin
338 * ffs routines, therefore differs in spirit from the above ffz (man ffs): it operates on
339 * "int" values only and the result value is the bit number + 1. ffs(0) is defined to
342 #define ffs(x) __builtin_ffs(x)
345 * hweightN: returns the hamming weight (i.e. the number
346 * of bits set) of a N-bit word
348 static __inline__
unsigned long
349 hweight64 (unsigned long x
)
351 unsigned long result
;
352 result
= ia64_popcnt(x
);
356 #define hweight32(x) hweight64 ((x) & 0xfffffffful)
357 #define hweight16(x) hweight64 ((x) & 0xfffful)
358 #define hweight8(x) hweight64 ((x) & 0xfful)
360 #endif /* __KERNEL__ */
362 extern int __find_next_zero_bit (void *addr
, unsigned long size
,
363 unsigned long offset
);
364 extern int __find_next_bit(const void *addr
, unsigned long size
,
365 unsigned long offset
);
367 #define find_next_zero_bit(addr, size, offset) \
368 __find_next_zero_bit((addr), (size), (offset))
369 #define find_next_bit(addr, size, offset) \
370 __find_next_bit((addr), (size), (offset))
373 * The optimizer actually does good code for this case..
375 #define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0)
377 #define find_first_bit(addr, size) find_next_bit((addr), (size), 0)
381 #define __clear_bit(nr, addr) clear_bit(nr, addr)
383 #define ext2_set_bit test_and_set_bit
384 #define ext2_set_bit_atomic(l,n,a) test_and_set_bit(n,a)
385 #define ext2_clear_bit test_and_clear_bit
386 #define ext2_clear_bit_atomic(l,n,a) test_and_clear_bit(n,a)
387 #define ext2_test_bit test_bit
388 #define ext2_find_first_zero_bit find_first_zero_bit
389 #define ext2_find_next_zero_bit find_next_zero_bit
391 /* Bitmap functions for the minix filesystem. */
392 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
393 #define minix_set_bit(nr,addr) set_bit(nr,addr)
394 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
395 #define minix_test_bit(nr,addr) test_bit(nr,addr)
396 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
399 sched_find_first_bit (unsigned long *b
)
404 return 64 + __ffs(b
[1]);
405 return __ffs(b
[2]) + 128;
408 #endif /* __KERNEL__ */
410 #endif /* _ASM_IA64_BITOPS_H */