1 /* $Id: bitops.h,v 1.67 2001/11/19 18:36:34 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)
6 * Copyright 2001 Anton Blanchard (anton@samba.org)
9 #ifndef _SPARC_BITOPS_H
10 #define _SPARC_BITOPS_H
12 #include <linux/compiler.h>
13 #include <asm/byteorder.h>
18 * Set bit 'nr' in 32-bit quantity at address 'addr' where bit '0'
19 * is in the highest of the four bytes and bit '31' is the high bit
20 * within the first byte. Sparc is BIG-Endian. Unless noted otherwise
21 * all bit-ops return 0 if bit was previously clear and != 0 otherwise.
23 static inline int test_and_set_bit(unsigned long nr
, volatile unsigned long *addr
)
25 register unsigned long mask
asm("g2");
26 register unsigned long *ADDR
asm("g1");
27 register int tmp1
asm("g3");
28 register int tmp2
asm("g4");
29 register int tmp3
asm("g5");
30 register int tmp4
asm("g7");
32 ADDR
= ((unsigned long *) addr
) + (nr
>> 5);
33 mask
= 1 << (nr
& 31);
38 " add %%o7, 8, %%o7\n"
39 : "=&r" (mask
), "=r" (tmp1
), "=r" (tmp2
), "=r" (tmp3
), "=r" (tmp4
)
40 : "0" (mask
), "r" (ADDR
)
46 static inline void set_bit(unsigned long nr
, volatile unsigned long *addr
)
48 register unsigned long mask
asm("g2");
49 register unsigned long *ADDR
asm("g1");
50 register int tmp1
asm("g3");
51 register int tmp2
asm("g4");
52 register int tmp3
asm("g5");
53 register int tmp4
asm("g7");
55 ADDR
= ((unsigned long *) addr
) + (nr
>> 5);
56 mask
= 1 << (nr
& 31);
61 " add %%o7, 8, %%o7\n"
62 : "=&r" (mask
), "=r" (tmp1
), "=r" (tmp2
), "=r" (tmp3
), "=r" (tmp4
)
63 : "0" (mask
), "r" (ADDR
)
67 static inline int test_and_clear_bit(unsigned long nr
, volatile unsigned long *addr
)
69 register unsigned long mask
asm("g2");
70 register unsigned long *ADDR
asm("g1");
71 register int tmp1
asm("g3");
72 register int tmp2
asm("g4");
73 register int tmp3
asm("g5");
74 register int tmp4
asm("g7");
76 ADDR
= ((unsigned long *) addr
) + (nr
>> 5);
77 mask
= 1 << (nr
& 31);
81 "call ___clear_bit\n\t"
82 " add %%o7, 8, %%o7\n"
83 : "=&r" (mask
), "=r" (tmp1
), "=r" (tmp2
), "=r" (tmp3
), "=r" (tmp4
)
84 : "0" (mask
), "r" (ADDR
)
90 static inline void clear_bit(unsigned long nr
, volatile unsigned long *addr
)
92 register unsigned long mask
asm("g2");
93 register unsigned long *ADDR
asm("g1");
94 register int tmp1
asm("g3");
95 register int tmp2
asm("g4");
96 register int tmp3
asm("g5");
97 register int tmp4
asm("g7");
99 ADDR
= ((unsigned long *) addr
) + (nr
>> 5);
100 mask
= 1 << (nr
& 31);
102 __asm__
__volatile__(
104 "call ___clear_bit\n\t"
105 " add %%o7, 8, %%o7\n"
106 : "=&r" (mask
), "=r" (tmp1
), "=r" (tmp2
), "=r" (tmp3
), "=r" (tmp4
)
107 : "0" (mask
), "r" (ADDR
)
111 static inline int test_and_change_bit(unsigned long nr
, volatile unsigned long *addr
)
113 register unsigned long mask
asm("g2");
114 register unsigned long *ADDR
asm("g1");
115 register int tmp1
asm("g3");
116 register int tmp2
asm("g4");
117 register int tmp3
asm("g5");
118 register int tmp4
asm("g7");
120 ADDR
= ((unsigned long *) addr
) + (nr
>> 5);
121 mask
= 1 << (nr
& 31);
123 __asm__
__volatile__(
125 "call ___change_bit\n\t"
126 " add %%o7, 8, %%o7\n"
127 : "=&r" (mask
), "=r" (tmp1
), "=r" (tmp2
), "=r" (tmp3
), "=r" (tmp4
)
128 : "0" (mask
), "r" (ADDR
)
134 static inline void change_bit(unsigned long nr
, volatile unsigned long *addr
)
136 register unsigned long mask
asm("g2");
137 register unsigned long *ADDR
asm("g1");
138 register int tmp1
asm("g3");
139 register int tmp2
asm("g4");
140 register int tmp3
asm("g5");
141 register int tmp4
asm("g7");
143 ADDR
= ((unsigned long *) addr
) + (nr
>> 5);
144 mask
= 1 << (nr
& 31);
146 __asm__
__volatile__(
148 "call ___change_bit\n\t"
149 " add %%o7, 8, %%o7\n"
150 : "=&r" (mask
), "=r" (tmp1
), "=r" (tmp2
), "=r" (tmp3
), "=r" (tmp4
)
151 : "0" (mask
), "r" (ADDR
)
156 * non-atomic versions
158 static inline void __set_bit(int nr
, volatile unsigned long *addr
)
160 unsigned long mask
= 1UL << (nr
& 0x1f);
161 unsigned long *p
= ((unsigned long *)addr
) + (nr
>> 5);
166 static inline void __clear_bit(int nr
, volatile unsigned long *addr
)
168 unsigned long mask
= 1UL << (nr
& 0x1f);
169 unsigned long *p
= ((unsigned long *)addr
) + (nr
>> 5);
174 static inline void __change_bit(int nr
, volatile unsigned long *addr
)
176 unsigned long mask
= 1UL << (nr
& 0x1f);
177 unsigned long *p
= ((unsigned long *)addr
) + (nr
>> 5);
182 static inline int __test_and_set_bit(int nr
, volatile unsigned long *addr
)
184 unsigned long mask
= 1UL << (nr
& 0x1f);
185 unsigned long *p
= ((unsigned long *)addr
) + (nr
>> 5);
186 unsigned long old
= *p
;
189 return (old
& mask
) != 0;
192 static inline int __test_and_clear_bit(int nr
, volatile unsigned long *addr
)
194 unsigned long mask
= 1UL << (nr
& 0x1f);
195 unsigned long *p
= ((unsigned long *)addr
) + (nr
>> 5);
196 unsigned long old
= *p
;
199 return (old
& mask
) != 0;
202 static inline int __test_and_change_bit(int nr
, volatile unsigned long *addr
)
204 unsigned long mask
= 1UL << (nr
& 0x1f);
205 unsigned long *p
= ((unsigned long *)addr
) + (nr
>> 5);
206 unsigned long old
= *p
;
209 return (old
& mask
) != 0;
212 #define smp_mb__before_clear_bit() do { } while(0)
213 #define smp_mb__after_clear_bit() do { } while(0)
215 /* The following routine need not be atomic. */
216 static inline int test_bit(int nr
, __const__
volatile unsigned long *addr
)
218 return (1UL & (((unsigned long *)addr
)[nr
>> 5] >> (nr
& 31))) != 0UL;
221 /* The easy/cheese version for now. */
222 static inline unsigned long ffz(unsigned long word
)
224 unsigned long result
= 0;
234 * __ffs - find first bit in word.
235 * @word: The word to search
237 * Undefined if no bit exists, so code should check against 0 first.
239 static inline int __ffs(unsigned long word
)
243 if ((word
& 0xffff) == 0) {
247 if ((word
& 0xff) == 0) {
251 if ((word
& 0xf) == 0) {
255 if ((word
& 0x3) == 0) {
259 if ((word
& 0x1) == 0)
265 * Every architecture must define this function. It's the fastest
266 * way of searching a 140-bit bitmap where the first 100 bits are
267 * unlikely to be set. It's guaranteed that at least one of the 140
270 static inline int sched_find_first_bit(unsigned long *b
)
276 return __ffs(b
[1]) + 32;
278 return __ffs(b
[2]) + 64;
280 return __ffs(b
[3]) + 96;
281 return __ffs(b
[4]) + 128;
285 * ffs: find first bit set. This is defined the same way as
286 * the libc and compiler builtin ffs routines, therefore
287 * differs in spirit from the above ffz (man ffs).
289 static inline int ffs(int x
)
293 return __ffs((unsigned long)x
) + 1;
297 * fls: find last (most-significant) bit set.
298 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
300 #define fls(x) generic_fls(x)
301 #define fls64(x) generic_fls64(x)
304 * hweightN: returns the hamming weight (i.e. the number
305 * of bits set) of a N-bit word
307 #define hweight32(x) generic_hweight32(x)
308 #define hweight16(x) generic_hweight16(x)
309 #define hweight8(x) generic_hweight8(x)
312 * find_next_zero_bit() finds the first zero bit in a bit string of length
313 * 'size' bits, starting the search at bit 'offset'. This is largely based
314 * on Linus's ALPHA routines, which are pretty portable BTW.
316 static inline unsigned long find_next_zero_bit(const unsigned long *addr
,
317 unsigned long size
, unsigned long offset
)
319 const unsigned long *p
= addr
+ (offset
>> 5);
320 unsigned long result
= offset
& ~31UL;
329 tmp
|= ~0UL >> (32-offset
);
337 while (size
& ~31UL) {
349 if (tmp
== ~0UL) /* Are any bits zero? */
350 return result
+ size
; /* Nope. */
352 return result
+ ffz(tmp
);
356 * Linus sez that gcc can optimize the following correctly, we'll see if this
357 * holds on the Sparc as it does for the ALPHA.
359 #define find_first_zero_bit(addr, size) \
360 find_next_zero_bit((addr), (size), 0)
363 * find_next_bit - find the first set bit in a memory region
364 * @addr: The address to base the search on
365 * @offset: The bitnumber to start searching at
366 * @size: The maximum size to search
368 * Scheduler induced bitop, do not use.
370 static inline int find_next_bit(const unsigned long *addr
, int size
, int offset
)
372 const unsigned long *p
= addr
+ (offset
>> 5);
373 int num
= offset
& ~0x1f;
377 word
&= ~((1 << (offset
& 0x1f)) - 1);
380 return __ffs(word
) + num
;
389 * find_first_bit - find the first set bit in a memory region
390 * @addr: The address to start the search at
391 * @size: The maximum size to search
393 * Returns the bit-number of the first set bit, not the number of the byte
396 #define find_first_bit(addr, size) \
397 find_next_bit((addr), (size), 0)
401 static inline int test_le_bit(int nr
, __const__
unsigned long * addr
)
403 __const__
unsigned char *ADDR
= (__const__
unsigned char *) addr
;
404 return (ADDR
[nr
>> 3] >> (nr
& 7)) & 1;
408 * non-atomic versions
410 static inline void __set_le_bit(int nr
, unsigned long *addr
)
412 unsigned char *ADDR
= (unsigned char *)addr
;
415 *ADDR
|= 1 << (nr
& 0x07);
418 static inline void __clear_le_bit(int nr
, unsigned long *addr
)
420 unsigned char *ADDR
= (unsigned char *)addr
;
423 *ADDR
&= ~(1 << (nr
& 0x07));
426 static inline int __test_and_set_le_bit(int nr
, unsigned long *addr
)
429 unsigned char *ADDR
= (unsigned char *)addr
;
432 mask
= 1 << (nr
& 0x07);
433 retval
= (mask
& *ADDR
) != 0;
438 static inline int __test_and_clear_le_bit(int nr
, unsigned long *addr
)
441 unsigned char *ADDR
= (unsigned char *)addr
;
444 mask
= 1 << (nr
& 0x07);
445 retval
= (mask
& *ADDR
) != 0;
450 static inline unsigned long find_next_zero_le_bit(const unsigned long *addr
,
451 unsigned long size
, unsigned long offset
)
453 const unsigned long *p
= addr
+ (offset
>> 5);
454 unsigned long result
= offset
& ~31UL;
463 tmp
|= __swab32(~0UL >> (32-offset
));
471 while(size
& ~31UL) {
482 tmp
= __swab32(tmp
) | (~0UL << size
);
483 if (tmp
== ~0UL) /* Are any bits zero? */
484 return result
+ size
; /* Nope. */
485 return result
+ ffz(tmp
);
488 return result
+ ffz(__swab32(tmp
));
491 #define find_first_zero_le_bit(addr, size) \
492 find_next_zero_le_bit((addr), (size), 0)
494 #define ext2_set_bit(nr,addr) \
495 __test_and_set_le_bit((nr),(unsigned long *)(addr))
496 #define ext2_clear_bit(nr,addr) \
497 __test_and_clear_le_bit((nr),(unsigned long *)(addr))
499 #define ext2_set_bit_atomic(lock, nr, addr) \
503 ret = ext2_set_bit((nr), (unsigned long *)(addr)); \
508 #define ext2_clear_bit_atomic(lock, nr, addr) \
512 ret = ext2_clear_bit((nr), (unsigned long *)(addr)); \
517 #define ext2_test_bit(nr,addr) \
518 test_le_bit((nr),(unsigned long *)(addr))
519 #define ext2_find_first_zero_bit(addr, size) \
520 find_first_zero_le_bit((unsigned long *)(addr), (size))
521 #define ext2_find_next_zero_bit(addr, size, off) \
522 find_next_zero_le_bit((unsigned long *)(addr), (size), (off))
524 /* Bitmap functions for the minix filesystem. */
525 #define minix_test_and_set_bit(nr,addr) \
526 test_and_set_bit((nr),(unsigned long *)(addr))
527 #define minix_set_bit(nr,addr) \
528 set_bit((nr),(unsigned long *)(addr))
529 #define minix_test_and_clear_bit(nr,addr) \
530 test_and_clear_bit((nr),(unsigned long *)(addr))
531 #define minix_test_bit(nr,addr) \
532 test_bit((nr),(unsigned long *)(addr))
533 #define minix_find_first_zero_bit(addr,size) \
534 find_first_zero_bit((unsigned long *)(addr),(size))
536 #endif /* __KERNEL__ */
538 #endif /* defined(_SPARC_BITOPS_H) */