1 #ifndef _ASM_IA64_BITOPS_H
2 #define _ASM_IA64_BITOPS_H
5 * Copyright (C) 1998-2000 Hewlett-Packard Co
6 * Copyright (C) 1998-2000 David Mosberger-Tang <davidm@hpl.hp.com>
8 * 02/04/00 D. Mosberger Require 64-bit alignment for bitops, per suggestion from davem
11 #include <asm/system.h>
14 * These operations need to be atomic. The address must be (at least)
15 * 32-bit aligned. Note that there are driver (e.g., eepro100) which
16 * use these operations to operate on hw-defined data-structures, so
17 * we can't easily change these operations to force a bigger
20 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
23 extern __inline__
void
24 set_bit (int nr
, volatile void *addr
)
30 m
= (volatile __u32
*) addr
+ (nr
>> 5);
36 } while (cmpxchg_acq(m
, old
, new) != old
);
39 extern __inline__
void
40 clear_bit (int nr
, volatile void *addr
)
46 m
= (volatile __u32
*) addr
+ (nr
>> 5);
47 mask
= ~(1 << (nr
& 31));
52 } while (cmpxchg_acq(m
, old
, new) != old
);
55 extern __inline__
void
56 change_bit (int nr
, volatile void *addr
)
62 m
= (volatile __u32
*) addr
+ (nr
>> 5);
63 bit
= (1 << (nr
& 31));
68 } while (cmpxchg_acq(m
, old
, new) != old
);
72 test_and_set_bit (int nr
, volatile void *addr
)
78 m
= (volatile __u32
*) addr
+ (nr
>> 5);
84 } while (cmpxchg_acq(m
, old
, new) != old
);
85 return (old
& bit
) != 0;
89 test_and_clear_bit (int nr
, volatile void *addr
)
95 m
= (volatile __u32
*) addr
+ (nr
>> 5);
96 mask
= ~(1 << (nr
& 31));
101 } while (cmpxchg_acq(m
, old
, new) != old
);
102 return (old
& ~mask
) != 0;
105 extern __inline__
int
106 test_and_change_bit (int nr
, volatile void *addr
)
110 CMPXCHG_BUGCHECK_DECL
112 m
= (volatile __u32
*) addr
+ (nr
>> 5);
113 bit
= (1 << (nr
& 31));
118 } while (cmpxchg_acq(m
, old
, new) != old
);
119 return (old
& bit
) != 0;
122 extern __inline__
int
123 test_bit (int nr
, volatile void *addr
)
125 return 1 & (((const volatile __u32
*) addr
)[nr
>> 5] >> (nr
& 31));
129 * ffz = Find First Zero in word. Undefined if no zero exists,
130 * so code should check against ~0UL first..
132 extern inline unsigned long
133 ffz (unsigned long x
)
135 unsigned long result
;
137 __asm__ ("popcnt %0=%1" : "=r" (result
) : "r" (x
& (~x
- 1)));
144 * Find the most significant bit that is set (undefined if no bit is
147 static inline unsigned long
148 ia64_fls (unsigned long x
)
153 __asm__ ("getf.exp %0=%1" : "=r"(exp
) : "f"(d
));
157 * ffs: find first bit set. This is defined the same way as
158 * the libc and compiler builtin ffs routines, therefore
159 * differs in spirit from the above ffz (man ffs).
161 #define ffs(x) __builtin_ffs(x)
164 * hweightN: returns the hamming weight (i.e. the number
165 * of bits set) of a N-bit word
167 extern __inline__
unsigned long
168 hweight64 (unsigned long x
)
170 unsigned long result
;
171 __asm__ ("popcnt %0=%1" : "=r" (result
) : "r" (x
));
175 #define hweight32(x) hweight64 ((x) & 0xfffffffful)
176 #define hweight16(x) hweight64 ((x) & 0xfffful)
177 #define hweight8(x) hweight64 ((x) & 0xfful)
179 #endif /* __KERNEL__ */
182 * Find next zero bit in a bitmap reasonably efficiently..
185 find_next_zero_bit (void *addr
, unsigned long size
, unsigned long offset
)
187 unsigned long *p
= ((unsigned long *) addr
) + (offset
>> 6);
188 unsigned long result
= offset
& ~63UL;
197 tmp
|= ~0UL >> (64-offset
);
205 while (size
& ~63UL) {
217 return result
+ ffz(tmp
);
221 * The optimizer actually does good code for this case..
223 #define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0)
227 #define ext2_set_bit test_and_set_bit
228 #define ext2_clear_bit test_and_clear_bit
229 #define ext2_test_bit test_bit
230 #define ext2_find_first_zero_bit find_first_zero_bit
231 #define ext2_find_next_zero_bit find_next_zero_bit
233 /* Bitmap functions for the minix filesystem. */
234 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
235 #define minix_set_bit(nr,addr) set_bit(nr,addr)
236 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
237 #define minix_test_bit(nr,addr) test_bit(nr,addr)
238 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
240 #endif /* __KERNEL__ */
242 #endif /* _ASM_IA64_BITOPS_H */