1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_IA64_BITOPS_H
3 #define _ASM_IA64_BITOPS_H
6 * Copyright (C) 1998-2003 Hewlett-Packard Co
7 * David Mosberger-Tang <davidm@hpl.hp.com>
9 * 02/06/02 find_next_bit() and find_first_bit() added from Erich Focht's ia64
10 * O(1) scheduler patch
13 #ifndef _LINUX_BITOPS_H
14 #error only <linux/bitops.h> can be included directly
17 #include <linux/compiler.h>
18 #include <linux/types.h>
19 #include <asm/intrinsics.h>
20 #include <asm/barrier.h>
23 * set_bit - Atomically set a bit in memory
25 * @addr: the address to start counting from
27 * This function is atomic and may not be reordered. See __set_bit()
28 * if you do not require the atomic guarantees.
29 * Note that @nr may be almost arbitrarily large; this function is not
30 * restricted to acting on a single-word quantity.
32 * The address must be (at least) "long" aligned.
33 * Note that there are driver (e.g., eepro100) which use these operations to
34 * operate on hw-defined data-structures, so we can't easily change these
35 * operations to force a bigger alignment.
37 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
39 static __inline__
void
40 set_bit (int nr
, volatile void *addr
)
46 m
= (volatile __u32
*) addr
+ (nr
>> 5);
52 } while (cmpxchg_acq(m
, old
, new) != old
);
56 * __set_bit - Set a bit in memory
58 * @addr: the address to start counting from
60 * Unlike set_bit(), this function is non-atomic and may be reordered.
61 * If it's called on the same region of memory simultaneously, the effect
62 * may be that only one operation succeeds.
64 static __inline__
void
65 __set_bit (int nr
, volatile void *addr
)
67 *((__u32
*) addr
+ (nr
>> 5)) |= (1 << (nr
& 31));
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_atomic() and/or smp_mb__after_atomic()
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_unlock - Clears a bit in memory with release
99 * @addr: Address to start counting from
101 * clear_bit_unlock() is atomic and may not be reordered. It does
102 * contain a memory barrier suitable for unlock type operations.
104 static __inline__
void
105 clear_bit_unlock (int nr
, volatile void *addr
)
107 __u32 mask
, old
, new;
109 CMPXCHG_BUGCHECK_DECL
111 m
= (volatile __u32
*) addr
+ (nr
>> 5);
112 mask
= ~(1 << (nr
& 31));
117 } while (cmpxchg_rel(m
, old
, new) != old
);
121 * __clear_bit_unlock - Non-atomically clears a bit in memory with release
123 * @addr: Address to start counting from
125 * Similarly to clear_bit_unlock, the implementation uses a store
126 * with release semantics. See also arch_spin_unlock().
128 static __inline__
void
129 __clear_bit_unlock(int nr
, void *addr
)
131 __u32
* const m
= (__u32
*) addr
+ (nr
>> 5);
132 __u32
const new = *m
& ~(1 << (nr
& 31));
134 ia64_st4_rel_nta(m
, new);
138 * __clear_bit - Clears a bit in memory (non-atomic version)
139 * @nr: the bit to clear
140 * @addr: the address to start counting from
142 * Unlike clear_bit(), this function is non-atomic and may be reordered.
143 * If it's called on the same region of memory simultaneously, the effect
144 * may be that only one operation succeeds.
146 static __inline__
void
147 __clear_bit (int nr
, volatile void *addr
)
149 *((__u32
*) addr
+ (nr
>> 5)) &= ~(1 << (nr
& 31));
153 * change_bit - Toggle a bit in memory
155 * @addr: Address to start counting from
157 * change_bit() is atomic and may not be reordered.
158 * Note that @nr may be almost arbitrarily large; this function is not
159 * restricted to acting on a single-word quantity.
161 static __inline__
void
162 change_bit (int nr
, volatile void *addr
)
166 CMPXCHG_BUGCHECK_DECL
168 m
= (volatile __u32
*) addr
+ (nr
>> 5);
169 bit
= (1 << (nr
& 31));
174 } while (cmpxchg_acq(m
, old
, new) != old
);
178 * __change_bit - Toggle a bit in memory
179 * @nr: the bit to toggle
180 * @addr: the address to start counting from
182 * Unlike change_bit(), this function is non-atomic and may be reordered.
183 * If it's called on the same region of memory simultaneously, the effect
184 * may be that only one operation succeeds.
186 static __inline__
void
187 __change_bit (int nr
, volatile void *addr
)
189 *((__u32
*) addr
+ (nr
>> 5)) ^= (1 << (nr
& 31));
193 * test_and_set_bit - Set 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 the acquisition side of the memory barrier.
200 static __inline__
int
201 test_and_set_bit (int nr
, volatile void *addr
)
205 CMPXCHG_BUGCHECK_DECL
207 m
= (volatile __u32
*) addr
+ (nr
>> 5);
208 bit
= 1 << (nr
& 31);
213 } while (cmpxchg_acq(m
, old
, new) != old
);
214 return (old
& bit
) != 0;
218 * test_and_set_bit_lock - Set a bit and return its old value for lock
220 * @addr: Address to count from
222 * This is the same as test_and_set_bit on ia64
224 #define test_and_set_bit_lock test_and_set_bit
227 * __test_and_set_bit - Set a bit and return its old value
229 * @addr: Address to count from
231 * This operation is non-atomic and can be reordered.
232 * If two examples of this operation race, one can appear to succeed
233 * but actually fail. You must protect multiple accesses with a lock.
235 static __inline__
int
236 __test_and_set_bit (int nr
, volatile void *addr
)
238 __u32
*p
= (__u32
*) addr
+ (nr
>> 5);
239 __u32 m
= 1 << (nr
& 31);
240 int oldbitset
= (*p
& m
) != 0;
247 * test_and_clear_bit - Clear a bit and return its old value
249 * @addr: Address to count from
251 * This operation is atomic and cannot be reordered.
252 * It also implies the acquisition side of the memory barrier.
254 static __inline__
int
255 test_and_clear_bit (int nr
, volatile void *addr
)
257 __u32 mask
, old
, new;
259 CMPXCHG_BUGCHECK_DECL
261 m
= (volatile __u32
*) addr
+ (nr
>> 5);
262 mask
= ~(1 << (nr
& 31));
267 } while (cmpxchg_acq(m
, old
, new) != old
);
268 return (old
& ~mask
) != 0;
272 * __test_and_clear_bit - Clear a bit and return its old value
274 * @addr: Address to count from
276 * This operation is non-atomic and can be reordered.
277 * If two examples of this operation race, one can appear to succeed
278 * but actually fail. You must protect multiple accesses with a lock.
280 static __inline__
int
281 __test_and_clear_bit(int nr
, volatile void * addr
)
283 __u32
*p
= (__u32
*) addr
+ (nr
>> 5);
284 __u32 m
= 1 << (nr
& 31);
285 int oldbitset
= (*p
& m
) != 0;
292 * test_and_change_bit - Change a bit and return its old value
294 * @addr: Address to count from
296 * This operation is atomic and cannot be reordered.
297 * It also implies the acquisition side of the memory barrier.
299 static __inline__
int
300 test_and_change_bit (int nr
, volatile void *addr
)
304 CMPXCHG_BUGCHECK_DECL
306 m
= (volatile __u32
*) addr
+ (nr
>> 5);
307 bit
= (1 << (nr
& 31));
312 } while (cmpxchg_acq(m
, old
, new) != old
);
313 return (old
& bit
) != 0;
317 * __test_and_change_bit - Change a bit and return its old value
319 * @addr: Address to count from
321 * This operation is non-atomic and can be reordered.
323 static __inline__
int
324 __test_and_change_bit (int nr
, void *addr
)
326 __u32 old
, bit
= (1 << (nr
& 31));
327 __u32
*m
= (__u32
*) addr
+ (nr
>> 5);
331 return (old
& bit
) != 0;
334 static __inline__
int
335 test_bit (int nr
, const volatile void *addr
)
337 return 1 & (((const volatile __u32
*) addr
)[nr
>> 5] >> (nr
& 31));
341 * ffz - find the first zero bit in a long word
342 * @x: The long word to find the bit in
344 * Returns the bit-number (0..63) of the first (least significant) zero bit.
345 * Undefined if no zero exists, so code should check against ~0UL first...
347 static inline unsigned long
348 ffz (unsigned long x
)
350 unsigned long result
;
352 result
= ia64_popcnt(x
& (~x
- 1));
357 * __ffs - find first bit in word.
358 * @x: The word to search
360 * Undefined if no bit exists, so code should check against 0 first.
362 static __inline__
unsigned long
363 __ffs (unsigned long x
)
365 unsigned long result
;
367 result
= ia64_popcnt((x
-1) & ~x
);
374 * Return bit number of last (most-significant) bit set. Undefined
375 * for x==0. Bits are numbered from 0..63 (e.g., ia64_fls(9) == 3).
377 static inline unsigned long
378 ia64_fls (unsigned long x
)
383 exp
= ia64_getf_exp(d
);
388 * Find the last (most significant) bit set. Returns 0 for x==0 and
389 * bits are numbered from 1..32 (e.g., fls(9) == 4).
391 static inline int fls(unsigned int t
)
393 unsigned long x
= t
& 0xffffffffu
;
402 return ia64_popcnt(x
);
406 * Find the last (most significant) bit set. Undefined for x==0.
407 * Bits are numbered from 0..63 (e.g., __fls(9) == 3).
409 static inline unsigned long
410 __fls (unsigned long x
)
418 return ia64_popcnt(x
) - 1;
421 #include <asm-generic/bitops/fls64.h>
423 #include <asm-generic/bitops/builtin-ffs.h>
426 * hweightN: returns the hamming weight (i.e. the number
427 * of bits set) of a N-bit word
429 static __inline__
unsigned long __arch_hweight64(unsigned long x
)
431 unsigned long result
;
432 result
= ia64_popcnt(x
);
436 #define __arch_hweight32(x) ((unsigned int) __arch_hweight64((x) & 0xfffffffful))
437 #define __arch_hweight16(x) ((unsigned int) __arch_hweight64((x) & 0xfffful))
438 #define __arch_hweight8(x) ((unsigned int) __arch_hweight64((x) & 0xfful))
440 #include <asm-generic/bitops/const_hweight.h>
442 #endif /* __KERNEL__ */
444 #include <asm-generic/bitops/find.h>
448 #include <asm-generic/bitops/le.h>
450 #include <asm-generic/bitops/ext2-atomic-setbit.h>
452 #include <asm-generic/bitops/sched.h>
454 #endif /* __KERNEL__ */
456 #endif /* _ASM_IA64_BITOPS_H */