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/config.h>
11 #include <linux/compiler.h>
12 #include <asm/byteorder.h>
13
14 extern int test_and_set_bit(unsigned long nr, volatile void *addr);
15 extern int test_and_clear_bit(unsigned long nr, volatile void *addr);
16 extern int test_and_change_bit(unsigned long nr, volatile void *addr);
17 extern void set_bit(unsigned long nr, volatile void *addr);
18 extern void clear_bit(unsigned long nr, volatile void *addr);
19 extern void change_bit(unsigned long nr, volatile void *addr);
20
21 /* "non-atomic" versions... */
22
__set_bit(int nr,volatile void * addr)23 static __inline__ void __set_bit(int nr, volatile void *addr)
24 {
25 unsigned long *m;
26
27 m = ((unsigned long *)addr) + (nr >> 6);
28 *m |= (1UL << (nr & 63));
29 }
30
__clear_bit(int nr,volatile void * addr)31 static __inline__ void __clear_bit(int nr, volatile void *addr)
32 {
33 unsigned long *m;
34
35 m = ((unsigned long *)addr) + (nr >> 6);
36 *m &= ~(1UL << (nr & 63));
37 }
38
__change_bit(int nr,volatile void * addr)39 static __inline__ void __change_bit(int nr, volatile void *addr)
40 {
41 unsigned long *m;
42
43 m = ((unsigned long *)addr) + (nr >> 6);
44 *m ^= (1UL << (nr & 63));
45 }
46
__test_and_set_bit(int nr,volatile void * addr)47 static __inline__ int __test_and_set_bit(int nr, volatile void *addr)
48 {
49 unsigned long *m = ((unsigned long *)addr) + (nr >> 6);
50 unsigned long old = *m;
51 unsigned long mask = (1UL << (nr & 63));
52
53 *m = (old | mask);
54 return ((old & mask) != 0);
55 }
56
__test_and_clear_bit(int nr,volatile void * addr)57 static __inline__ int __test_and_clear_bit(int nr, volatile void *addr)
58 {
59 unsigned long *m = ((unsigned long *)addr) + (nr >> 6);
60 unsigned long old = *m;
61 unsigned long mask = (1UL << (nr & 63));
62
63 *m = (old & ~mask);
64 return ((old & mask) != 0);
65 }
66
__test_and_change_bit(int nr,volatile void * addr)67 static __inline__ int __test_and_change_bit(int nr, volatile void *addr)
68 {
69 unsigned long *m = ((unsigned long *)addr) + (nr >> 6);
70 unsigned long old = *m;
71 unsigned long mask = (1UL << (nr & 63));
72
73 *m = (old ^ mask);
74 return ((old & mask) != 0);
75 }
76
77 #ifdef CONFIG_SMP
78 #define smp_mb__before_clear_bit() membar_safe("#StoreLoad | #LoadLoad")
79 #define smp_mb__after_clear_bit() membar_safe("#StoreLoad | #StoreStore")
80 #else
81 #define smp_mb__before_clear_bit() barrier()
82 #define smp_mb__after_clear_bit() barrier()
83 #endif
84
test_bit(int nr,__const__ volatile void * _addr)85 static __inline__ int test_bit(int nr, __const__ volatile void *_addr)
86 {
87 __const__ unsigned long *addr;
88
89 addr = (__const__ unsigned long *) _addr;
90
91 return (1UL & ((addr)[nr >> 6] >> (nr & 63))) != 0UL;
92 }
93
94 /* The easy/cheese version for now. */
ffz(unsigned long word)95 static __inline__ unsigned long ffz(unsigned long word)
96 {
97 unsigned long result;
98
99 result = 0;
100 while(word & 1) {
101 result++;
102 word >>= 1;
103 }
104 return result;
105 }
106
107 /**
108 * __ffs - find first bit in word.
109 * @word: The word to search
110 *
111 * Undefined if no bit exists, so code should check against 0 first.
112 */
__ffs(unsigned long word)113 static __inline__ unsigned long __ffs(unsigned long word)
114 {
115 unsigned long result = 0;
116
117 while (!(word & 1UL)) {
118 result++;
119 word >>= 1;
120 }
121 return result;
122 }
123
124 /*
125 * fls: find last bit set.
126 */
127
128 #define fls(x) generic_fls(x)
129
130 #ifdef __KERNEL__
131
132 /*
133 * ffs: find first bit set. This is defined the same way as
134 * the libc and compiler builtin ffs routines, therefore
135 * differs in spirit from the above ffz (man ffs).
136 */
ffs(int x)137 static __inline__ int ffs(int x)
138 {
139 if (!x)
140 return 0;
141 return __ffs((unsigned long)x) + 1;
142 }
143
144 /*
145 * hweightN: returns the hamming weight (i.e. the number
146 * of bits set) of a N-bit word
147 */
148
149 #ifdef ULTRA_HAS_POPULATION_COUNT
150
hweight64(unsigned long w)151 static __inline__ unsigned int hweight64(unsigned long w)
152 {
153 unsigned int res;
154
155 __asm__ ("popc %1,%0" : "=r" (res) : "r" (w));
156 return res;
157 }
158
hweight32(unsigned int w)159 static __inline__ unsigned int hweight32(unsigned int w)
160 {
161 unsigned int res;
162
163 __asm__ ("popc %1,%0" : "=r" (res) : "r" (w & 0xffffffff));
164 return res;
165 }
166
hweight16(unsigned int w)167 static __inline__ unsigned int hweight16(unsigned int w)
168 {
169 unsigned int res;
170
171 __asm__ ("popc %1,%0" : "=r" (res) : "r" (w & 0xffff));
172 return res;
173 }
174
hweight8(unsigned int w)175 static __inline__ unsigned int hweight8(unsigned int w)
176 {
177 unsigned int res;
178
179 __asm__ ("popc %1,%0" : "=r" (res) : "r" (w & 0xff));
180 return res;
181 }
182
183 #else
184
185 #define hweight64(x) generic_hweight64(x)
186 #define hweight32(x) generic_hweight32(x)
187 #define hweight16(x) generic_hweight16(x)
188 #define hweight8(x) generic_hweight8(x)
189
190 #endif
191 #endif /* __KERNEL__ */
192
193 /* find_next_zero_bit() finds the first zero bit in a bit string of length
194 * 'size' bits, starting the search at bit 'offset'. This is largely based
195 * on Linus's ALPHA routines, which are pretty portable BTW.
196 */
197
find_next_zero_bit(void * addr,unsigned long size,unsigned long offset)198 static __inline__ unsigned long find_next_zero_bit(void *addr, unsigned long size, unsigned long offset)
199 {
200 unsigned long *p = ((unsigned long *) addr) + (offset >> 6);
201 unsigned long result = offset & ~63UL;
202 unsigned long tmp;
203
204 if (offset >= size)
205 return size;
206 size -= result;
207 offset &= 63UL;
208 if (offset) {
209 tmp = *(p++);
210 tmp |= ~0UL >> (64-offset);
211 if (size < 64)
212 goto found_first;
213 if (~tmp)
214 goto found_middle;
215 size -= 64;
216 result += 64;
217 }
218 while (size & ~63UL) {
219 if (~(tmp = *(p++)))
220 goto found_middle;
221 result += 64;
222 size -= 64;
223 }
224 if (!size)
225 return result;
226 tmp = *p;
227
228 found_first:
229 tmp |= ~0UL << size;
230 if (tmp == ~0UL) /* Are any bits zero? */
231 return result + size; /* Nope. */
232 found_middle:
233 return result + ffz(tmp);
234 }
235
236 #define find_first_zero_bit(addr, size) \
237 find_next_zero_bit((addr), (size), 0)
238
239 #define test_and_set_le_bit(nr,addr) \
240 test_and_set_bit((nr) ^ 0x38, (addr))
241 #define test_and_clear_le_bit(nr,addr) \
242 test_and_clear_bit((nr) ^ 0x38, (addr))
243
test_le_bit(int nr,__const__ void * addr)244 static __inline__ int test_le_bit(int nr, __const__ void *addr)
245 {
246 int mask;
247 __const__ unsigned char *ADDR = (__const__ unsigned char *) addr;
248
249 ADDR += nr >> 3;
250 mask = 1 << (nr & 0x07);
251 return ((mask & *ADDR) != 0);
252 }
253
254 #define find_first_zero_le_bit(addr, size) \
255 find_next_zero_le_bit((addr), (size), 0)
256
find_next_zero_le_bit(void * addr,unsigned long size,unsigned long offset)257 static __inline__ unsigned long find_next_zero_le_bit(void *addr, unsigned long size, unsigned long offset)
258 {
259 unsigned long *p = ((unsigned long *) addr) + (offset >> 6);
260 unsigned long result = offset & ~63UL;
261 unsigned long tmp;
262
263 if (offset >= size)
264 return size;
265 size -= result;
266 offset &= 63UL;
267 if(offset) {
268 tmp = __swab64p(p++);
269 tmp |= (~0UL >> (64-offset));
270 if(size < 64)
271 goto found_first;
272 if(~tmp)
273 goto found_middle;
274 size -= 64;
275 result += 64;
276 }
277 while(size & ~63) {
278 if(~(tmp = __swab64p(p++)))
279 goto found_middle;
280 result += 64;
281 size -= 64;
282 }
283 if(!size)
284 return result;
285 tmp = __swab64p(p);
286 found_first:
287 tmp |= (~0UL << size);
288 if (tmp == ~0UL) /* Are any bits zero? */
289 return result + size; /* Nope. */
290 found_middle:
291 return result + ffz(tmp);
292 }
293
294 #ifdef __KERNEL__
295
296 #define __set_le_bit(nr, addr) \
297 __set_bit((nr) ^ 0x38, (addr))
298 #define __clear_le_bit(nr, addr) \
299 __clear_bit((nr) ^ 0x38, (addr))
300 #define __test_and_clear_le_bit(nr, addr) \
301 __test_and_clear_bit((nr) ^ 0x38, (addr))
302 #define __test_and_set_le_bit(nr, addr) \
303 __test_and_set_bit((nr) ^ 0x38, (addr))
304
305 #define ext2_set_bit(nr,addr) \
306 __test_and_set_le_bit((nr),(unsigned long *)(addr))
307 #define ext2_set_bit_atomic(lock,nr,addr) \
308 test_and_set_le_bit((nr),(unsigned long *)(addr))
309 #define ext2_clear_bit(nr,addr) \
310 __test_and_clear_le_bit((nr),(unsigned long *)(addr))
311 #define ext2_clear_bit_atomic(lock,nr,addr) \
312 test_and_clear_le_bit((nr),(unsigned long *)(addr))
313 #define ext2_test_bit(nr,addr) \
314 test_le_bit((nr),(unsigned long *)(addr))
315 #define ext2_find_first_zero_bit(addr, size) \
316 find_first_zero_le_bit((unsigned long *)(addr), (size))
317 #define ext2_find_next_zero_bit(addr, size, off) \
318 find_next_zero_le_bit((unsigned long *)(addr), (size), (off))
319
320 /* Bitmap functions for the minix filesystem. */
321 #define minix_test_and_set_bit(nr,addr) \
322 test_and_set_bit((nr),(unsigned long *)(addr))
323 #define minix_set_bit(nr,addr) \
324 set_bit((nr),(unsigned long *)(addr))
325 #define minix_test_and_clear_bit(nr,addr) \
326 test_and_clear_bit((nr),(unsigned long *)(addr))
327 #define minix_test_bit(nr,addr) \
328 test_bit((nr),(unsigned long *)(addr))
329 #define minix_find_first_zero_bit(addr,size) \
330 find_first_zero_bit((unsigned long *)(addr),(size))
331
332 #endif /* __KERNEL__ */
333
334 #endif /* defined(_SPARC64_BITOPS_H) */
335