1 /* $Id: bitops.h,v 1.61 2000/09/23 02:11:22 davem Exp $
2 * bitops.h: Bit string operations on the Sparc.
4 * Copyright 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright 1996 Eddie C. Dost (ecd@skynet.be)
8 #ifndef _SPARC_BITOPS_H
9 #define _SPARC_BITOPS_H
11 #include <linux/kernel.h>
12 #include <asm/byteorder.h>
16 /* User mode bitops, defined here for convenience. Note: these are not
17 * atomic, so packages like nthreads should do some locking around these
21 extern __inline__
unsigned long set_bit(unsigned long nr
, void *addr
)
24 unsigned long *ADDR
= (unsigned long *) addr
;
27 mask
= 1 << (nr
& 31);
28 __asm__
__volatile__("
35 : "0" (ADDR
), "r" (mask
)
38 return (unsigned long) ADDR
;
41 extern __inline__
unsigned long clear_bit(unsigned long nr
, void *addr
)
44 unsigned long *ADDR
= (unsigned long *) addr
;
47 mask
= 1 << (nr
& 31);
48 __asm__
__volatile__("
55 : "0" (ADDR
), "r" (mask
)
58 return (unsigned long) ADDR
;
61 extern __inline__
void change_bit(unsigned long nr
, void *addr
)
64 unsigned long *ADDR
= (unsigned long *) addr
;
67 mask
= 1 << (nr
& 31);
68 __asm__
__volatile__("
75 : "0" (ADDR
), "r" (mask
)
79 #else /* __KERNEL__ */
81 #include <asm/system.h>
83 /* Set bit 'nr' in 32-bit quantity at address 'addr' where bit '0'
84 * is in the highest of the four bytes and bit '31' is the high bit
85 * within the first byte. Sparc is BIG-Endian. Unless noted otherwise
86 * all bit-ops return 0 if bit was previously clear and != 0 otherwise.
89 extern __inline__
int test_and_set_bit(unsigned long nr
, volatile void *addr
)
91 register unsigned long mask
asm("g2");
92 register unsigned long *ADDR
asm("g1");
93 ADDR
= ((unsigned long *) addr
) + (nr
>> 5);
94 mask
= 1 << (nr
& 31);
95 __asm__
__volatile__("
100 : "0" (mask
), "r" (ADDR
)
101 : "g3", "g4", "g5", "g7", "cc");
106 extern __inline__
void set_bit(unsigned long nr
, volatile void *addr
)
108 (void) test_and_set_bit(nr
, addr
);
111 extern __inline__
int test_and_clear_bit(unsigned long nr
, volatile void *addr
)
113 register unsigned long mask
asm("g2");
114 register unsigned long *ADDR
asm("g1");
116 ADDR
= ((unsigned long *) addr
) + (nr
>> 5);
117 mask
= 1 << (nr
& 31);
118 __asm__
__volatile__("
123 : "0" (mask
), "r" (ADDR
)
124 : "g3", "g4", "g5", "g7", "cc");
129 extern __inline__
void clear_bit(unsigned long nr
, volatile void *addr
)
131 (void) test_and_clear_bit(nr
, addr
);
134 extern __inline__
int test_and_change_bit(unsigned long nr
, volatile void *addr
)
136 register unsigned long mask
asm("g2");
137 register unsigned long *ADDR
asm("g1");
139 ADDR
= ((unsigned long *) addr
) + (nr
>> 5);
140 mask
= 1 << (nr
& 31);
141 __asm__
__volatile__("
146 : "0" (mask
), "r" (ADDR
)
147 : "g3", "g4", "g5", "g7", "cc");
152 extern __inline__
void change_bit(unsigned long nr
, volatile void *addr
)
154 (void) test_and_change_bit(nr
, addr
);
157 #endif /* __KERNEL__ */
159 #define smp_mb__before_clear_bit() do { } while(0)
160 #define smp_mb__after_clear_bit() do { } while(0)
162 /* The following routine need not be atomic. */
163 extern __inline__
int test_bit(int nr
, __const__
void *addr
)
165 return (1 & (((__const__
unsigned int *) addr
)[nr
>> 5] >> (nr
& 31))) != 0;
168 /* The easy/cheese version for now. */
169 extern __inline__
unsigned long ffz(unsigned long word
)
171 unsigned long result
= 0;
183 * ffs: find first bit set. This is defined the same way as
184 * the libc and compiler builtin ffs routines, therefore
185 * differs in spirit from the above ffz (man ffs).
188 #define ffs(x) generic_ffs(x)
191 * hweightN: returns the hamming weight (i.e. the number
192 * of bits set) of a N-bit word
195 #define hweight32(x) generic_hweight32(x)
196 #define hweight16(x) generic_hweight16(x)
197 #define hweight8(x) generic_hweight8(x)
199 #endif /* __KERNEL__ */
201 /* find_next_zero_bit() finds the first zero bit in a bit string of length
202 * 'size' bits, starting the search at bit 'offset'. This is largely based
203 * on Linus's ALPHA routines, which are pretty portable BTW.
206 extern __inline__
unsigned long find_next_zero_bit(void *addr
, unsigned long size
, unsigned long offset
)
208 unsigned long *p
= ((unsigned long *) addr
) + (offset
>> 5);
209 unsigned long result
= offset
& ~31UL;
218 tmp
|= ~0UL >> (32-offset
);
226 while (size
& ~31UL) {
238 if (tmp
== ~0UL) /* Are any bits zero? */
239 return result
+ size
; /* Nope. */
241 return result
+ ffz(tmp
);
244 /* Linus sez that gcc can optimize the following correctly, we'll see if this
245 * holds on the Sparc as it does for the ALPHA.
248 #define find_first_zero_bit(addr, size) \
249 find_next_zero_bit((addr), (size), 0)
253 extern __inline__
int set_le_bit(int nr
, void *addr
)
256 unsigned char *ADDR
= (unsigned char *) addr
;
259 mask
= 1 << (nr
& 0x07);
260 __asm__
__volatile__("
267 : "0" (ADDR
), "r" (mask
)
273 extern __inline__
int clear_le_bit(int nr
, void *addr
)
276 unsigned char *ADDR
= (unsigned char *) addr
;
279 mask
= 1 << (nr
& 0x07);
280 __asm__
__volatile__("
287 : "0" (ADDR
), "r" (mask
)
293 #else /* __KERNEL__ */
295 /* Now for the ext2 filesystem bit operations and helper routines. */
297 extern __inline__
int set_le_bit(int nr
, volatile void * addr
)
299 register int mask
asm("g2");
300 register unsigned char *ADDR
asm("g1");
302 ADDR
= ((unsigned char *) addr
) + (nr
>> 3);
303 mask
= 1 << (nr
& 0x07);
304 __asm__
__volatile__("
309 : "0" (mask
), "r" (ADDR
)
310 : "g3", "g4", "g5", "g7", "cc");
315 extern __inline__
int clear_le_bit(int nr
, volatile void * addr
)
317 register int mask
asm("g2");
318 register unsigned char *ADDR
asm("g1");
320 ADDR
= ((unsigned char *) addr
) + (nr
>> 3);
321 mask
= 1 << (nr
& 0x07);
322 __asm__
__volatile__("
327 : "0" (mask
), "r" (ADDR
)
328 : "g3", "g4", "g5", "g7", "cc");
333 #endif /* __KERNEL__ */
335 extern __inline__
int test_le_bit(int nr
, __const__
void * addr
)
338 __const__
unsigned char *ADDR
= (__const__
unsigned char *) addr
;
341 mask
= 1 << (nr
& 0x07);
342 return ((mask
& *ADDR
) != 0);
347 #define ext2_set_bit set_le_bit
348 #define ext2_clear_bit clear_le_bit
349 #define ext2_test_bit test_le_bit
351 #endif /* __KERNEL__ */
353 #define find_first_zero_le_bit(addr, size) \
354 find_next_zero_le_bit((addr), (size), 0)
356 extern __inline__
unsigned long find_next_zero_le_bit(void *addr
, unsigned long size
, unsigned long offset
)
358 unsigned long *p
= ((unsigned long *) addr
) + (offset
>> 5);
359 unsigned long result
= offset
& ~31UL;
368 tmp
|= __swab32(~0UL >> (32-offset
));
376 while(size
& ~31UL) {
387 tmp
= __swab32(tmp
) | (~0UL << size
);
388 if (tmp
== ~0UL) /* Are any bits zero? */
389 return result
+ size
; /* Nope. */
390 return result
+ ffz(tmp
);
393 return result
+ ffz(__swab32(tmp
));
398 #define ext2_find_first_zero_bit find_first_zero_le_bit
399 #define ext2_find_next_zero_bit find_next_zero_le_bit
401 /* Bitmap functions for the minix filesystem. */
402 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
403 #define minix_set_bit(nr,addr) set_bit(nr,addr)
404 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
405 #define minix_test_bit(nr,addr) test_bit(nr,addr)
406 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
408 #endif /* __KERNEL__ */
410 #endif /* defined(_SPARC_BITOPS_H) */