xref: /OK3568_Linux_fs/kernel/arch/ia64/include/asm/bitops.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _ASM_IA64_BITOPS_H
3*4882a593Smuzhiyun #define _ASM_IA64_BITOPS_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun  * Copyright (C) 1998-2003 Hewlett-Packard Co
7*4882a593Smuzhiyun  *	David Mosberger-Tang <davidm@hpl.hp.com>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * 02/06/02 find_next_bit() and find_first_bit() added from Erich Focht's ia64
10*4882a593Smuzhiyun  * O(1) scheduler patch
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #ifndef _LINUX_BITOPS_H
14*4882a593Smuzhiyun #error only <linux/bitops.h> can be included directly
15*4882a593Smuzhiyun #endif
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <linux/compiler.h>
18*4882a593Smuzhiyun #include <linux/types.h>
19*4882a593Smuzhiyun #include <asm/intrinsics.h>
20*4882a593Smuzhiyun #include <asm/barrier.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /**
23*4882a593Smuzhiyun  * set_bit - Atomically set a bit in memory
24*4882a593Smuzhiyun  * @nr: the bit to set
25*4882a593Smuzhiyun  * @addr: the address to start counting from
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * This function is atomic and may not be reordered.  See __set_bit()
28*4882a593Smuzhiyun  * if you do not require the atomic guarantees.
29*4882a593Smuzhiyun  * Note that @nr may be almost arbitrarily large; this function is not
30*4882a593Smuzhiyun  * restricted to acting on a single-word quantity.
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * The address must be (at least) "long" aligned.
33*4882a593Smuzhiyun  * Note that there are driver (e.g., eepro100) which use these operations to
34*4882a593Smuzhiyun  * operate on hw-defined data-structures, so we can't easily change these
35*4882a593Smuzhiyun  * operations to force a bigger alignment.
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
38*4882a593Smuzhiyun  */
39*4882a593Smuzhiyun static __inline__ void
set_bit(int nr,volatile void * addr)40*4882a593Smuzhiyun set_bit (int nr, volatile void *addr)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	__u32 bit, old, new;
43*4882a593Smuzhiyun 	volatile __u32 *m;
44*4882a593Smuzhiyun 	CMPXCHG_BUGCHECK_DECL
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	m = (volatile __u32 *) addr + (nr >> 5);
47*4882a593Smuzhiyun 	bit = 1 << (nr & 31);
48*4882a593Smuzhiyun 	do {
49*4882a593Smuzhiyun 		CMPXCHG_BUGCHECK(m);
50*4882a593Smuzhiyun 		old = *m;
51*4882a593Smuzhiyun 		new = old | bit;
52*4882a593Smuzhiyun 	} while (cmpxchg_acq(m, old, new) != old);
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /**
56*4882a593Smuzhiyun  * __set_bit - Set a bit in memory
57*4882a593Smuzhiyun  * @nr: the bit to set
58*4882a593Smuzhiyun  * @addr: the address to start counting from
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * Unlike set_bit(), this function is non-atomic and may be reordered.
61*4882a593Smuzhiyun  * If it's called on the same region of memory simultaneously, the effect
62*4882a593Smuzhiyun  * may be that only one operation succeeds.
63*4882a593Smuzhiyun  */
64*4882a593Smuzhiyun static __inline__ void
__set_bit(int nr,volatile void * addr)65*4882a593Smuzhiyun __set_bit (int nr, volatile void *addr)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	*((__u32 *) addr + (nr >> 5)) |= (1 << (nr & 31));
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun /**
71*4882a593Smuzhiyun  * clear_bit - Clears a bit in memory
72*4882a593Smuzhiyun  * @nr: Bit to clear
73*4882a593Smuzhiyun  * @addr: Address to start counting from
74*4882a593Smuzhiyun  *
75*4882a593Smuzhiyun  * clear_bit() is atomic and may not be reordered.  However, it does
76*4882a593Smuzhiyun  * not contain a memory barrier, so if it is used for locking purposes,
77*4882a593Smuzhiyun  * you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
78*4882a593Smuzhiyun  * in order to ensure changes are visible on other processors.
79*4882a593Smuzhiyun  */
80*4882a593Smuzhiyun static __inline__ void
clear_bit(int nr,volatile void * addr)81*4882a593Smuzhiyun clear_bit (int nr, volatile void *addr)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	__u32 mask, old, new;
84*4882a593Smuzhiyun 	volatile __u32 *m;
85*4882a593Smuzhiyun 	CMPXCHG_BUGCHECK_DECL
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	m = (volatile __u32 *) addr + (nr >> 5);
88*4882a593Smuzhiyun 	mask = ~(1 << (nr & 31));
89*4882a593Smuzhiyun 	do {
90*4882a593Smuzhiyun 		CMPXCHG_BUGCHECK(m);
91*4882a593Smuzhiyun 		old = *m;
92*4882a593Smuzhiyun 		new = old & mask;
93*4882a593Smuzhiyun 	} while (cmpxchg_acq(m, old, new) != old);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /**
97*4882a593Smuzhiyun  * clear_bit_unlock - Clears a bit in memory with release
98*4882a593Smuzhiyun  * @nr: Bit to clear
99*4882a593Smuzhiyun  * @addr: Address to start counting from
100*4882a593Smuzhiyun  *
101*4882a593Smuzhiyun  * clear_bit_unlock() is atomic and may not be reordered.  It does
102*4882a593Smuzhiyun  * contain a memory barrier suitable for unlock type operations.
103*4882a593Smuzhiyun  */
104*4882a593Smuzhiyun static __inline__ void
clear_bit_unlock(int nr,volatile void * addr)105*4882a593Smuzhiyun clear_bit_unlock (int nr, volatile void *addr)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun 	__u32 mask, old, new;
108*4882a593Smuzhiyun 	volatile __u32 *m;
109*4882a593Smuzhiyun 	CMPXCHG_BUGCHECK_DECL
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	m = (volatile __u32 *) addr + (nr >> 5);
112*4882a593Smuzhiyun 	mask = ~(1 << (nr & 31));
113*4882a593Smuzhiyun 	do {
114*4882a593Smuzhiyun 		CMPXCHG_BUGCHECK(m);
115*4882a593Smuzhiyun 		old = *m;
116*4882a593Smuzhiyun 		new = old & mask;
117*4882a593Smuzhiyun 	} while (cmpxchg_rel(m, old, new) != old);
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun /**
121*4882a593Smuzhiyun  * __clear_bit_unlock - Non-atomically clears a bit in memory with release
122*4882a593Smuzhiyun  * @nr: Bit to clear
123*4882a593Smuzhiyun  * @addr: Address to start counting from
124*4882a593Smuzhiyun  *
125*4882a593Smuzhiyun  * Similarly to clear_bit_unlock, the implementation uses a store
126*4882a593Smuzhiyun  * with release semantics. See also arch_spin_unlock().
127*4882a593Smuzhiyun  */
128*4882a593Smuzhiyun static __inline__ void
__clear_bit_unlock(int nr,void * addr)129*4882a593Smuzhiyun __clear_bit_unlock(int nr, void *addr)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun 	__u32 * const m = (__u32 *) addr + (nr >> 5);
132*4882a593Smuzhiyun 	__u32 const new = *m & ~(1 << (nr & 31));
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	ia64_st4_rel_nta(m, new);
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun /**
138*4882a593Smuzhiyun  * __clear_bit - Clears a bit in memory (non-atomic version)
139*4882a593Smuzhiyun  * @nr: the bit to clear
140*4882a593Smuzhiyun  * @addr: the address to start counting from
141*4882a593Smuzhiyun  *
142*4882a593Smuzhiyun  * Unlike clear_bit(), this function is non-atomic and may be reordered.
143*4882a593Smuzhiyun  * If it's called on the same region of memory simultaneously, the effect
144*4882a593Smuzhiyun  * may be that only one operation succeeds.
145*4882a593Smuzhiyun  */
146*4882a593Smuzhiyun static __inline__ void
__clear_bit(int nr,volatile void * addr)147*4882a593Smuzhiyun __clear_bit (int nr, volatile void *addr)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	*((__u32 *) addr + (nr >> 5)) &= ~(1 << (nr & 31));
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun /**
153*4882a593Smuzhiyun  * change_bit - Toggle a bit in memory
154*4882a593Smuzhiyun  * @nr: Bit to toggle
155*4882a593Smuzhiyun  * @addr: Address to start counting from
156*4882a593Smuzhiyun  *
157*4882a593Smuzhiyun  * change_bit() is atomic and may not be reordered.
158*4882a593Smuzhiyun  * Note that @nr may be almost arbitrarily large; this function is not
159*4882a593Smuzhiyun  * restricted to acting on a single-word quantity.
160*4882a593Smuzhiyun  */
161*4882a593Smuzhiyun static __inline__ void
change_bit(int nr,volatile void * addr)162*4882a593Smuzhiyun change_bit (int nr, volatile void *addr)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	__u32 bit, old, new;
165*4882a593Smuzhiyun 	volatile __u32 *m;
166*4882a593Smuzhiyun 	CMPXCHG_BUGCHECK_DECL
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	m = (volatile __u32 *) addr + (nr >> 5);
169*4882a593Smuzhiyun 	bit = (1 << (nr & 31));
170*4882a593Smuzhiyun 	do {
171*4882a593Smuzhiyun 		CMPXCHG_BUGCHECK(m);
172*4882a593Smuzhiyun 		old = *m;
173*4882a593Smuzhiyun 		new = old ^ bit;
174*4882a593Smuzhiyun 	} while (cmpxchg_acq(m, old, new) != old);
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun /**
178*4882a593Smuzhiyun  * __change_bit - Toggle a bit in memory
179*4882a593Smuzhiyun  * @nr: the bit to toggle
180*4882a593Smuzhiyun  * @addr: the address to start counting from
181*4882a593Smuzhiyun  *
182*4882a593Smuzhiyun  * Unlike change_bit(), this function is non-atomic and may be reordered.
183*4882a593Smuzhiyun  * If it's called on the same region of memory simultaneously, the effect
184*4882a593Smuzhiyun  * may be that only one operation succeeds.
185*4882a593Smuzhiyun  */
186*4882a593Smuzhiyun static __inline__ void
__change_bit(int nr,volatile void * addr)187*4882a593Smuzhiyun __change_bit (int nr, volatile void *addr)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun 	*((__u32 *) addr + (nr >> 5)) ^= (1 << (nr & 31));
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun /**
193*4882a593Smuzhiyun  * test_and_set_bit - Set a bit and return its old value
194*4882a593Smuzhiyun  * @nr: Bit to set
195*4882a593Smuzhiyun  * @addr: Address to count from
196*4882a593Smuzhiyun  *
197*4882a593Smuzhiyun  * This operation is atomic and cannot be reordered.
198*4882a593Smuzhiyun  * It also implies the acquisition side of the memory barrier.
199*4882a593Smuzhiyun  */
200*4882a593Smuzhiyun static __inline__ int
test_and_set_bit(int nr,volatile void * addr)201*4882a593Smuzhiyun test_and_set_bit (int nr, volatile void *addr)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun 	__u32 bit, old, new;
204*4882a593Smuzhiyun 	volatile __u32 *m;
205*4882a593Smuzhiyun 	CMPXCHG_BUGCHECK_DECL
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	m = (volatile __u32 *) addr + (nr >> 5);
208*4882a593Smuzhiyun 	bit = 1 << (nr & 31);
209*4882a593Smuzhiyun 	do {
210*4882a593Smuzhiyun 		CMPXCHG_BUGCHECK(m);
211*4882a593Smuzhiyun 		old = *m;
212*4882a593Smuzhiyun 		new = old | bit;
213*4882a593Smuzhiyun 	} while (cmpxchg_acq(m, old, new) != old);
214*4882a593Smuzhiyun 	return (old & bit) != 0;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun /**
218*4882a593Smuzhiyun  * test_and_set_bit_lock - Set a bit and return its old value for lock
219*4882a593Smuzhiyun  * @nr: Bit to set
220*4882a593Smuzhiyun  * @addr: Address to count from
221*4882a593Smuzhiyun  *
222*4882a593Smuzhiyun  * This is the same as test_and_set_bit on ia64
223*4882a593Smuzhiyun  */
224*4882a593Smuzhiyun #define test_and_set_bit_lock test_and_set_bit
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun /**
227*4882a593Smuzhiyun  * __test_and_set_bit - Set a bit and return its old value
228*4882a593Smuzhiyun  * @nr: Bit to set
229*4882a593Smuzhiyun  * @addr: Address to count from
230*4882a593Smuzhiyun  *
231*4882a593Smuzhiyun  * This operation is non-atomic and can be reordered.
232*4882a593Smuzhiyun  * If two examples of this operation race, one can appear to succeed
233*4882a593Smuzhiyun  * but actually fail.  You must protect multiple accesses with a lock.
234*4882a593Smuzhiyun  */
235*4882a593Smuzhiyun static __inline__ int
__test_and_set_bit(int nr,volatile void * addr)236*4882a593Smuzhiyun __test_and_set_bit (int nr, volatile void *addr)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun 	__u32 *p = (__u32 *) addr + (nr >> 5);
239*4882a593Smuzhiyun 	__u32 m = 1 << (nr & 31);
240*4882a593Smuzhiyun 	int oldbitset = (*p & m) != 0;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	*p |= m;
243*4882a593Smuzhiyun 	return oldbitset;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun /**
247*4882a593Smuzhiyun  * test_and_clear_bit - Clear a bit and return its old value
248*4882a593Smuzhiyun  * @nr: Bit to clear
249*4882a593Smuzhiyun  * @addr: Address to count from
250*4882a593Smuzhiyun  *
251*4882a593Smuzhiyun  * This operation is atomic and cannot be reordered.
252*4882a593Smuzhiyun  * It also implies the acquisition side of the memory barrier.
253*4882a593Smuzhiyun  */
254*4882a593Smuzhiyun static __inline__ int
test_and_clear_bit(int nr,volatile void * addr)255*4882a593Smuzhiyun test_and_clear_bit (int nr, volatile void *addr)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun 	__u32 mask, old, new;
258*4882a593Smuzhiyun 	volatile __u32 *m;
259*4882a593Smuzhiyun 	CMPXCHG_BUGCHECK_DECL
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	m = (volatile __u32 *) addr + (nr >> 5);
262*4882a593Smuzhiyun 	mask = ~(1 << (nr & 31));
263*4882a593Smuzhiyun 	do {
264*4882a593Smuzhiyun 		CMPXCHG_BUGCHECK(m);
265*4882a593Smuzhiyun 		old = *m;
266*4882a593Smuzhiyun 		new = old & mask;
267*4882a593Smuzhiyun 	} while (cmpxchg_acq(m, old, new) != old);
268*4882a593Smuzhiyun 	return (old & ~mask) != 0;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun /**
272*4882a593Smuzhiyun  * __test_and_clear_bit - Clear a bit and return its old value
273*4882a593Smuzhiyun  * @nr: Bit to clear
274*4882a593Smuzhiyun  * @addr: Address to count from
275*4882a593Smuzhiyun  *
276*4882a593Smuzhiyun  * This operation is non-atomic and can be reordered.
277*4882a593Smuzhiyun  * If two examples of this operation race, one can appear to succeed
278*4882a593Smuzhiyun  * but actually fail.  You must protect multiple accesses with a lock.
279*4882a593Smuzhiyun  */
280*4882a593Smuzhiyun static __inline__ int
__test_and_clear_bit(int nr,volatile void * addr)281*4882a593Smuzhiyun __test_and_clear_bit(int nr, volatile void * addr)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun 	__u32 *p = (__u32 *) addr + (nr >> 5);
284*4882a593Smuzhiyun 	__u32 m = 1 << (nr & 31);
285*4882a593Smuzhiyun 	int oldbitset = (*p & m) != 0;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	*p &= ~m;
288*4882a593Smuzhiyun 	return oldbitset;
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun /**
292*4882a593Smuzhiyun  * test_and_change_bit - Change a bit and return its old value
293*4882a593Smuzhiyun  * @nr: Bit to change
294*4882a593Smuzhiyun  * @addr: Address to count from
295*4882a593Smuzhiyun  *
296*4882a593Smuzhiyun  * This operation is atomic and cannot be reordered.
297*4882a593Smuzhiyun  * It also implies the acquisition side of the memory barrier.
298*4882a593Smuzhiyun  */
299*4882a593Smuzhiyun static __inline__ int
test_and_change_bit(int nr,volatile void * addr)300*4882a593Smuzhiyun test_and_change_bit (int nr, volatile void *addr)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun 	__u32 bit, old, new;
303*4882a593Smuzhiyun 	volatile __u32 *m;
304*4882a593Smuzhiyun 	CMPXCHG_BUGCHECK_DECL
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	m = (volatile __u32 *) addr + (nr >> 5);
307*4882a593Smuzhiyun 	bit = (1 << (nr & 31));
308*4882a593Smuzhiyun 	do {
309*4882a593Smuzhiyun 		CMPXCHG_BUGCHECK(m);
310*4882a593Smuzhiyun 		old = *m;
311*4882a593Smuzhiyun 		new = old ^ bit;
312*4882a593Smuzhiyun 	} while (cmpxchg_acq(m, old, new) != old);
313*4882a593Smuzhiyun 	return (old & bit) != 0;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun /**
317*4882a593Smuzhiyun  * __test_and_change_bit - Change a bit and return its old value
318*4882a593Smuzhiyun  * @nr: Bit to change
319*4882a593Smuzhiyun  * @addr: Address to count from
320*4882a593Smuzhiyun  *
321*4882a593Smuzhiyun  * This operation is non-atomic and can be reordered.
322*4882a593Smuzhiyun  */
323*4882a593Smuzhiyun static __inline__ int
__test_and_change_bit(int nr,void * addr)324*4882a593Smuzhiyun __test_and_change_bit (int nr, void *addr)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	__u32 old, bit = (1 << (nr & 31));
327*4882a593Smuzhiyun 	__u32 *m = (__u32 *) addr + (nr >> 5);
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	old = *m;
330*4882a593Smuzhiyun 	*m = old ^ bit;
331*4882a593Smuzhiyun 	return (old & bit) != 0;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun static __inline__ int
test_bit(int nr,const volatile void * addr)335*4882a593Smuzhiyun test_bit (int nr, const volatile void *addr)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun 	return 1 & (((const volatile __u32 *) addr)[nr >> 5] >> (nr & 31));
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun /**
341*4882a593Smuzhiyun  * ffz - find the first zero bit in a long word
342*4882a593Smuzhiyun  * @x: The long word to find the bit in
343*4882a593Smuzhiyun  *
344*4882a593Smuzhiyun  * Returns the bit-number (0..63) of the first (least significant) zero bit.
345*4882a593Smuzhiyun  * Undefined if no zero exists, so code should check against ~0UL first...
346*4882a593Smuzhiyun  */
347*4882a593Smuzhiyun static inline unsigned long
ffz(unsigned long x)348*4882a593Smuzhiyun ffz (unsigned long x)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun 	unsigned long result;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	result = ia64_popcnt(x & (~x - 1));
353*4882a593Smuzhiyun 	return result;
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun /**
357*4882a593Smuzhiyun  * __ffs - find first bit in word.
358*4882a593Smuzhiyun  * @x: The word to search
359*4882a593Smuzhiyun  *
360*4882a593Smuzhiyun  * Undefined if no bit exists, so code should check against 0 first.
361*4882a593Smuzhiyun  */
362*4882a593Smuzhiyun static __inline__ unsigned long
__ffs(unsigned long x)363*4882a593Smuzhiyun __ffs (unsigned long x)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun 	unsigned long result;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	result = ia64_popcnt((x-1) & ~x);
368*4882a593Smuzhiyun 	return result;
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun #ifdef __KERNEL__
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun /*
374*4882a593Smuzhiyun  * Return bit number of last (most-significant) bit set.  Undefined
375*4882a593Smuzhiyun  * for x==0.  Bits are numbered from 0..63 (e.g., ia64_fls(9) == 3).
376*4882a593Smuzhiyun  */
377*4882a593Smuzhiyun static inline unsigned long
ia64_fls(unsigned long x)378*4882a593Smuzhiyun ia64_fls (unsigned long x)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun 	long double d = x;
381*4882a593Smuzhiyun 	long exp;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	exp = ia64_getf_exp(d);
384*4882a593Smuzhiyun 	return exp - 0xffff;
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun /*
388*4882a593Smuzhiyun  * Find the last (most significant) bit set.  Returns 0 for x==0 and
389*4882a593Smuzhiyun  * bits are numbered from 1..32 (e.g., fls(9) == 4).
390*4882a593Smuzhiyun  */
fls(unsigned int t)391*4882a593Smuzhiyun static inline int fls(unsigned int t)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun 	unsigned long x = t & 0xffffffffu;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	if (!x)
396*4882a593Smuzhiyun 		return 0;
397*4882a593Smuzhiyun 	x |= x >> 1;
398*4882a593Smuzhiyun 	x |= x >> 2;
399*4882a593Smuzhiyun 	x |= x >> 4;
400*4882a593Smuzhiyun 	x |= x >> 8;
401*4882a593Smuzhiyun 	x |= x >> 16;
402*4882a593Smuzhiyun 	return ia64_popcnt(x);
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun /*
406*4882a593Smuzhiyun  * Find the last (most significant) bit set.  Undefined for x==0.
407*4882a593Smuzhiyun  * Bits are numbered from 0..63 (e.g., __fls(9) == 3).
408*4882a593Smuzhiyun  */
409*4882a593Smuzhiyun static inline unsigned long
__fls(unsigned long x)410*4882a593Smuzhiyun __fls (unsigned long x)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun 	x |= x >> 1;
413*4882a593Smuzhiyun 	x |= x >> 2;
414*4882a593Smuzhiyun 	x |= x >> 4;
415*4882a593Smuzhiyun 	x |= x >> 8;
416*4882a593Smuzhiyun 	x |= x >> 16;
417*4882a593Smuzhiyun 	x |= x >> 32;
418*4882a593Smuzhiyun 	return ia64_popcnt(x) - 1;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun #include <asm-generic/bitops/fls64.h>
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun #include <asm-generic/bitops/builtin-ffs.h>
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun /*
426*4882a593Smuzhiyun  * hweightN: returns the hamming weight (i.e. the number
427*4882a593Smuzhiyun  * of bits set) of a N-bit word
428*4882a593Smuzhiyun  */
__arch_hweight64(unsigned long x)429*4882a593Smuzhiyun static __inline__ unsigned long __arch_hweight64(unsigned long x)
430*4882a593Smuzhiyun {
431*4882a593Smuzhiyun 	unsigned long result;
432*4882a593Smuzhiyun 	result = ia64_popcnt(x);
433*4882a593Smuzhiyun 	return result;
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun #define __arch_hweight32(x) ((unsigned int) __arch_hweight64((x) & 0xfffffffful))
437*4882a593Smuzhiyun #define __arch_hweight16(x) ((unsigned int) __arch_hweight64((x) & 0xfffful))
438*4882a593Smuzhiyun #define __arch_hweight8(x)  ((unsigned int) __arch_hweight64((x) & 0xfful))
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun #include <asm-generic/bitops/const_hweight.h>
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun #endif /* __KERNEL__ */
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun #include <asm-generic/bitops/find.h>
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun #ifdef __KERNEL__
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun #include <asm-generic/bitops/le.h>
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun #include <asm-generic/bitops/ext2-atomic-setbit.h>
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun #include <asm-generic/bitops/sched.h>
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun #endif /* __KERNEL__ */
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun #endif /* _ASM_IA64_BITOPS_H */
457