1 /* $Id: bitops.h,v 1.39 2002/01/30 01:40:00 davem Exp $
2 * bitops.h: Bit string operations on the V9.
4 * Copyright 1996, 1997 David S. Miller (davem@caip.rutgers.edu)
7 #ifndef _SPARC64_BITOPS_H
8 #define _SPARC64_BITOPS_H
10 #include <linux/compiler.h>
11 #include <asm/byteorder.h>
13 extern long ___test_and_set_bit(unsigned long nr
, volatile unsigned long *addr
);
14 extern long ___test_and_clear_bit(unsigned long nr
, volatile unsigned long *addr
);
15 extern long ___test_and_change_bit(unsigned long nr
, volatile unsigned long *addr
);
17 #define test_and_set_bit(nr,addr) ({___test_and_set_bit(nr,addr)!=0;})
18 #define test_and_clear_bit(nr,addr) ({___test_and_clear_bit(nr,addr)!=0;})
19 #define test_and_change_bit(nr,addr) ({___test_and_change_bit(nr,addr)!=0;})
20 #define set_bit(nr,addr) ((void)___test_and_set_bit(nr,addr))
21 #define clear_bit(nr,addr) ((void)___test_and_clear_bit(nr,addr))
22 #define change_bit(nr,addr) ((void)___test_and_change_bit(nr,addr))
24 /* "non-atomic" versions... */
26 static __inline__
void __set_bit(int nr
, volatile unsigned long *addr
)
28 volatile unsigned long *m
= addr
+ (nr
>> 6);
30 *m
|= (1UL << (nr
& 63));
33 static __inline__
void __clear_bit(int nr
, volatile unsigned long *addr
)
35 volatile unsigned long *m
= addr
+ (nr
>> 6);
37 *m
&= ~(1UL << (nr
& 63));
40 static __inline__
void __change_bit(int nr
, volatile unsigned long *addr
)
42 volatile unsigned long *m
= addr
+ (nr
>> 6);
44 *m
^= (1UL << (nr
& 63));
47 static __inline__
int __test_and_set_bit(int nr
, volatile unsigned long *addr
)
49 volatile unsigned long *m
= addr
+ (nr
>> 6);
51 long mask
= (1UL << (nr
& 63));
54 return ((old
& mask
) != 0);
57 static __inline__
int __test_and_clear_bit(int nr
, volatile unsigned long *addr
)
59 volatile unsigned long *m
= addr
+ (nr
>> 6);
61 long mask
= (1UL << (nr
& 63));
64 return ((old
& mask
) != 0);
67 static __inline__
int __test_and_change_bit(int nr
, volatile unsigned long *addr
)
69 volatile unsigned long *m
= addr
+ (nr
>> 6);
71 long mask
= (1UL << (nr
& 63));
74 return ((old
& mask
) != 0);
77 #define smp_mb__before_clear_bit() do { } while(0)
78 #define smp_mb__after_clear_bit() do { } while(0)
80 static __inline__
int test_bit(int nr
, __const__
volatile unsigned long *addr
)
82 return (1UL & ((addr
)[nr
>> 6] >> (nr
& 63))) != 0UL;
85 /* The easy/cheese version for now. */
86 static __inline__
unsigned long ffz(unsigned long word
)
99 * __ffs - find first bit in word.
100 * @word: The word to search
102 * Undefined if no bit exists, so code should check against 0 first.
104 static __inline__
unsigned long __ffs(unsigned long word
)
106 unsigned long result
= 0;
108 while (!(word
& 1UL)) {
116 * fls: find last bit set.
119 #define fls(x) generic_fls(x)
124 * Every architecture must define this function. It's the fastest
125 * way of searching a 140-bit bitmap where the first 100 bits are
126 * unlikely to be set. It's guaranteed that at least one of the 140
129 static inline int sched_find_first_bit(unsigned long *b
)
133 if (unlikely(((unsigned int)b
[1])))
134 return __ffs(b
[1]) + 64;
136 return __ffs(b
[1] >> 32) + 96;
137 return __ffs(b
[2]) + 128;
141 * ffs: find first bit set. This is defined the same way as
142 * the libc and compiler builtin ffs routines, therefore
143 * differs in spirit from the above ffz (man ffs).
145 static __inline__
int ffs(int x
)
149 return __ffs((unsigned long)x
) + 1;
153 * hweightN: returns the hamming weight (i.e. the number
154 * of bits set) of a N-bit word
157 #ifdef ULTRA_HAS_POPULATION_COUNT
159 static __inline__
unsigned int hweight64(unsigned long w
)
163 __asm__ ("popc %1,%0" : "=r" (res
) : "r" (w
));
167 static __inline__
unsigned int hweight32(unsigned int w
)
171 __asm__ ("popc %1,%0" : "=r" (res
) : "r" (w
& 0xffffffff));
175 static __inline__
unsigned int hweight16(unsigned int w
)
179 __asm__ ("popc %1,%0" : "=r" (res
) : "r" (w
& 0xffff));
183 static __inline__
unsigned int hweight8(unsigned int w
)
187 __asm__ ("popc %1,%0" : "=r" (res
) : "r" (w
& 0xff));
193 #define hweight64(x) generic_hweight64(x)
194 #define hweight32(x) generic_hweight32(x)
195 #define hweight16(x) generic_hweight16(x)
196 #define hweight8(x) generic_hweight8(x)
199 #endif /* __KERNEL__ */
202 * find_next_bit - find the next set bit in a memory region
203 * @addr: The address to base the search on
204 * @offset: The bitnumber to start searching at
205 * @size: The maximum size to search
207 extern unsigned long find_next_bit(const unsigned long *, unsigned long,
211 * find_first_bit - find the first set bit in a memory region
212 * @addr: The address to start the search at
213 * @size: The maximum size to search
215 * Returns the bit-number of the first set bit, not the number of the byte
218 #define find_first_bit(addr, size) \
219 find_next_bit((addr), (size), 0)
221 /* find_next_zero_bit() finds the first zero bit in a bit string of length
222 * 'size' bits, starting the search at bit 'offset'. This is largely based
223 * on Linus's ALPHA routines, which are pretty portable BTW.
226 extern unsigned long find_next_zero_bit(unsigned long *, unsigned long, unsigned long);
228 #define find_first_zero_bit(addr, size) \
229 find_next_zero_bit((addr), (size), 0)
231 #define test_and_set_le_bit(nr,addr) \
232 ({ ___test_and_set_bit((nr) ^ 0x38, (addr)) != 0; })
233 #define test_and_clear_le_bit(nr,addr) \
234 ({ ___test_and_clear_bit((nr) ^ 0x38, (addr)) != 0; })
236 static __inline__
int test_le_bit(int nr
, __const__
unsigned long * addr
)
239 __const__
unsigned char *ADDR
= (__const__
unsigned char *) addr
;
242 mask
= 1 << (nr
& 0x07);
243 return ((mask
& *ADDR
) != 0);
246 #define find_first_zero_le_bit(addr, size) \
247 find_next_zero_le_bit((addr), (size), 0)
249 extern unsigned long find_next_zero_le_bit(unsigned long *, unsigned long, unsigned long);
253 #define ext2_set_bit(nr,addr) \
254 test_and_set_le_bit((nr),(unsigned long *)(addr))
255 #define ext2_set_bit_atomic(lock,nr,addr) \
256 test_and_set_le_bit((nr),(unsigned long *)(addr))
257 #define ext2_clear_bit(nr,addr) \
258 test_and_clear_le_bit((nr),(unsigned long *)(addr))
259 #define ext2_clear_bit_atomic(lock,nr,addr) \
260 test_and_clear_le_bit((nr),(unsigned long *)(addr))
261 #define ext2_test_bit(nr,addr) \
262 test_le_bit((nr),(unsigned long *)(addr))
263 #define ext2_find_first_zero_bit(addr, size) \
264 find_first_zero_le_bit((unsigned long *)(addr), (size))
265 #define ext2_find_next_zero_bit(addr, size, off) \
266 find_next_zero_le_bit((unsigned long *)(addr), (size), (off))
268 /* Bitmap functions for the minix filesystem. */
269 #define minix_test_and_set_bit(nr,addr) \
270 test_and_set_bit((nr),(unsigned long *)(addr))
271 #define minix_set_bit(nr,addr) \
272 set_bit((nr),(unsigned long *)(addr))
273 #define minix_test_and_clear_bit(nr,addr) \
274 test_and_clear_bit((nr),(unsigned long *)(addr))
275 #define minix_test_bit(nr,addr) \
276 test_bit((nr),(unsigned long *)(addr))
277 #define minix_find_first_zero_bit(addr,size) \
278 find_first_zero_bit((unsigned long *)(addr),(size))
280 #endif /* __KERNEL__ */
282 #endif /* defined(_SPARC64_BITOPS_H) */