patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / include / asm-sparc64 / bitops.h
1 /* $Id: bitops.h,v 1.39 2002/01/30 01:40:00 davem Exp $
2  * bitops.h: Bit string operations on the V9.
3  *
4  * Copyright 1996, 1997 David S. Miller (davem@caip.rutgers.edu)
5  */
6
7 #ifndef _SPARC64_BITOPS_H
8 #define _SPARC64_BITOPS_H
9
10 #include <linux/compiler.h>
11 #include <asm/byteorder.h>
12
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);
16
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))
23
24 /* "non-atomic" versions... */
25
26 static __inline__ void __set_bit(int nr, volatile unsigned long *addr)
27 {
28         volatile unsigned long *m = addr + (nr >> 6);
29
30         *m |= (1UL << (nr & 63));
31 }
32
33 static __inline__ void __clear_bit(int nr, volatile unsigned long *addr)
34 {
35         volatile unsigned long *m = addr + (nr >> 6);
36
37         *m &= ~(1UL << (nr & 63));
38 }
39
40 static __inline__ void __change_bit(int nr, volatile unsigned long *addr)
41 {
42         volatile unsigned long *m = addr + (nr >> 6);
43
44         *m ^= (1UL << (nr & 63));
45 }
46
47 static __inline__ int __test_and_set_bit(int nr, volatile unsigned long *addr)
48 {
49         volatile unsigned long *m = addr + (nr >> 6);
50         long old = *m;
51         long mask = (1UL << (nr & 63));
52
53         *m = (old | mask);
54         return ((old & mask) != 0);
55 }
56
57 static __inline__ int __test_and_clear_bit(int nr, volatile unsigned long *addr)
58 {
59         volatile unsigned long *m = addr + (nr >> 6);
60         long old = *m;
61         long mask = (1UL << (nr & 63));
62
63         *m = (old & ~mask);
64         return ((old & mask) != 0);
65 }
66
67 static __inline__ int __test_and_change_bit(int nr, volatile unsigned long *addr)
68 {
69         volatile unsigned long *m = addr + (nr >> 6);
70         long old = *m;
71         long mask = (1UL << (nr & 63));
72
73         *m = (old ^ mask);
74         return ((old & mask) != 0);
75 }
76
77 #define smp_mb__before_clear_bit()      do { } while(0)
78 #define smp_mb__after_clear_bit()       do { } while(0)
79
80 static __inline__ int test_bit(int nr, __const__ volatile unsigned long *addr)
81 {
82         return (1UL & ((addr)[nr >> 6] >> (nr & 63))) != 0UL;
83 }
84
85 /* The easy/cheese version for now. */
86 static __inline__ unsigned long ffz(unsigned long word)
87 {
88         unsigned long result;
89
90         result = 0;
91         while(word & 1) {
92                 result++;
93                 word >>= 1;
94         }
95         return result;
96 }
97
98 /**
99  * __ffs - find first bit in word.
100  * @word: The word to search
101  *
102  * Undefined if no bit exists, so code should check against 0 first.
103  */
104 static __inline__ unsigned long __ffs(unsigned long word)
105 {
106         unsigned long result = 0;
107
108         while (!(word & 1UL)) {
109                 result++;
110                 word >>= 1;
111         }
112         return result;
113 }
114
115 /*
116  * fls: find last bit set.
117  */
118
119 #define fls(x) generic_fls(x)
120
121 #ifdef __KERNEL__
122
123 /*
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
127  * bits is cleared.
128  */
129 static inline int sched_find_first_bit(unsigned long *b)
130 {
131         if (unlikely(b[0]))
132                 return __ffs(b[0]);
133         if (unlikely(((unsigned int)b[1])))
134                 return __ffs(b[1]) + 64;
135         if (b[1] >> 32)
136                 return __ffs(b[1] >> 32) + 96;
137         return __ffs(b[2]) + 128;
138 }
139
140 /*
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).
144  */
145 static __inline__ int ffs(int x)
146 {
147         if (!x)
148                 return 0;
149         return __ffs((unsigned long)x) + 1;
150 }
151
152 /*
153  * hweightN: returns the hamming weight (i.e. the number
154  * of bits set) of a N-bit word
155  */
156
157 #ifdef ULTRA_HAS_POPULATION_COUNT
158
159 static __inline__ unsigned int hweight64(unsigned long w)
160 {
161         unsigned int res;
162
163         __asm__ ("popc %1,%0" : "=r" (res) : "r" (w));
164         return res;
165 }
166
167 static __inline__ unsigned int hweight32(unsigned int w)
168 {
169         unsigned int res;
170
171         __asm__ ("popc %1,%0" : "=r" (res) : "r" (w & 0xffffffff));
172         return res;
173 }
174
175 static __inline__ unsigned int hweight16(unsigned int w)
176 {
177         unsigned int res;
178
179         __asm__ ("popc %1,%0" : "=r" (res) : "r" (w & 0xffff));
180         return res;
181 }
182
183 static __inline__ unsigned int hweight8(unsigned int w)
184 {
185         unsigned int res;
186
187         __asm__ ("popc %1,%0" : "=r" (res) : "r" (w & 0xff));
188         return res;
189 }
190
191 #else
192
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)
197
198 #endif
199 #endif /* __KERNEL__ */
200
201 /**
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
206  */
207 extern unsigned long find_next_bit(unsigned long *, unsigned long, unsigned long);
208
209 /**
210  * find_first_bit - find the first set bit in a memory region
211  * @addr: The address to start the search at
212  * @size: The maximum size to search
213  *
214  * Returns the bit-number of the first set bit, not the number of the byte
215  * containing a bit.
216  */
217 #define find_first_bit(addr, size) \
218         find_next_bit((addr), (size), 0)
219
220 /* find_next_zero_bit() finds the first zero bit in a bit string of length
221  * 'size' bits, starting the search at bit 'offset'. This is largely based
222  * on Linus's ALPHA routines, which are pretty portable BTW.
223  */
224
225 extern unsigned long find_next_zero_bit(unsigned long *, unsigned long, unsigned long);
226
227 #define find_first_zero_bit(addr, size) \
228         find_next_zero_bit((addr), (size), 0)
229
230 extern long ___test_and_set_le_bit(int nr, volatile unsigned long *addr);
231 extern long ___test_and_clear_le_bit(int nr, volatile unsigned long *addr);
232
233 #define test_and_set_le_bit(nr,addr)    ({___test_and_set_le_bit(nr,addr)!=0;})
234 #define test_and_clear_le_bit(nr,addr)  ({___test_and_clear_le_bit(nr,addr)!=0;})
235 #define set_le_bit(nr,addr)             ((void)___test_and_set_le_bit(nr,addr))
236 #define clear_le_bit(nr,addr)           ((void)___test_and_clear_le_bit(nr,addr))
237
238 static __inline__ int test_le_bit(int nr, __const__ unsigned long * addr)
239 {
240         int                     mask;
241         __const__ unsigned char *ADDR = (__const__ unsigned char *) addr;
242
243         ADDR += nr >> 3;
244         mask = 1 << (nr & 0x07);
245         return ((mask & *ADDR) != 0);
246 }
247
248 #define find_first_zero_le_bit(addr, size) \
249         find_next_zero_le_bit((addr), (size), 0)
250
251 extern unsigned long find_next_zero_le_bit(unsigned long *, unsigned long, unsigned long);
252
253 #ifdef __KERNEL__
254
255 #define ext2_set_bit(nr,addr)           test_and_set_le_bit((nr),(unsigned long *)(addr))
256 #define ext2_set_bit_atomic(lock,nr,addr) test_and_set_le_bit((nr),(unsigned long *)(addr))
257 #define ext2_clear_bit(nr,addr)         test_and_clear_le_bit((nr),(unsigned long *)(addr))
258 #define ext2_clear_bit_atomic(lock,nr,addr) test_and_clear_le_bit((nr),(unsigned long *)(addr))
259 #define ext2_test_bit(nr,addr)          test_le_bit((nr),(unsigned long *)(addr))
260 #define ext2_find_first_zero_bit(addr, size) \
261         find_first_zero_le_bit((unsigned long *)(addr), (size))
262 #define ext2_find_next_zero_bit(addr, size, off) \
263         find_next_zero_le_bit((unsigned long *)(addr), (size), (off))
264
265 /* Bitmap functions for the minix filesystem.  */
266 #define minix_test_and_set_bit(nr,addr) test_and_set_bit((nr),(unsigned long *)(addr))
267 #define minix_set_bit(nr,addr)          set_bit((nr),(unsigned long *)(addr))
268 #define minix_test_and_clear_bit(nr,addr) \
269         test_and_clear_bit((nr),(unsigned long *)(addr))
270 #define minix_test_bit(nr,addr)         test_bit((nr),(unsigned long *)(addr))
271 #define minix_find_first_zero_bit(addr,size) \
272         find_first_zero_bit((unsigned long *)(addr),(size))
273
274 #endif /* __KERNEL__ */
275
276 #endif /* defined(_SPARC64_BITOPS_H) */