2 * $Id: bitops.h,v 1.31 2000/09/23 02:09:21 davem Exp $
3 * bitops.h: Bit string operations on the V9.
5 * Copyright 1996, 1997 David S. Miller (davem@caip.rutgers.edu)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #ifndef _SPARC64_BITOPS_H
24 #define _SPARC64_BITOPS_H
26 #include <asm/byteorder.h>
28 extern long __test_and_set_bit(unsigned long nr, volatile void *addr);
29 extern long __test_and_clear_bit(unsigned long nr, volatile void *addr);
30 extern long __test_and_change_bit(unsigned long nr, volatile void *addr);
32 #define test_and_set_bit(nr,addr) (__test_and_set_bit(nr,addr)!=0)
33 #define test_and_clear_bit(nr,addr) (__test_and_clear_bit(nr,addr)!=0)
34 #define test_and_change_bit(nr,addr) (__test_and_change_bit(nr,addr)!=0)
35 #define set_bit(nr,addr) ((void)__test_and_set_bit(nr,addr))
36 #define clear_bit(nr,addr) ((void)__test_and_clear_bit(nr,addr))
37 #define change_bit(nr,addr) ((void)__test_and_change_bit(nr,addr))
39 #define smp_mb__before_clear_bit() do { } while(0)
40 #define smp_mb__after_clear_bit() do { } while(0)
42 extern __inline__ int test_bit(int nr, __const__ void *addr)
44 return (1UL & (((__const__ long *) addr)[nr >> 6] >> (nr & 63))) != 0UL;
47 /* The easy/cheese version for now. */
48 extern __inline__ unsigned long ffz(unsigned long word)
52 #ifdef ULTRA_HAS_POPULATION_COUNT /* Thanks for nothing Sun... */
53 __asm__ __volatile__("
62 #if 1 /* def EASY_CHEESE_VERSION */
77 if (!(unsigned short)tmp) {
81 if (!(unsigned char)tmp) {
85 if (tmp & 0xf0) result += 4;
86 if (tmp & 0xcc) result += 2;
87 if (tmp & 0xaa) result ++;
96 * ffs: find first bit set. This is defined the same way as
97 * the libc and compiler builtin ffs routines, therefore
98 * differs in spirit from the above ffz (man ffs).
101 #define ffs(x) generic_ffs(x)
104 * hweightN: returns the hamming weight (i.e. the number
105 * of bits set) of a N-bit word
108 #ifdef ULTRA_HAS_POPULATION_COUNT
110 extern __inline__ unsigned int hweight32(unsigned int w)
114 __asm__ ("popc %1,%0" : "=r" (res) : "r" (w & 0xffffffff));
118 extern __inline__ unsigned int hweight16(unsigned int w)
122 __asm__ ("popc %1,%0" : "=r" (res) : "r" (w & 0xffff));
126 extern __inline__ unsigned int hweight8(unsigned int w)
130 __asm__ ("popc %1,%0" : "=r" (res) : "r" (w & 0xff));
136 #define hweight32(x) generic_hweight32(x)
137 #define hweight16(x) generic_hweight16(x)
138 #define hweight8(x) generic_hweight8(x)
141 #endif /* __KERNEL__ */
143 /* find_next_zero_bit() finds the first zero bit in a bit string of length
144 * 'size' bits, starting the search at bit 'offset'. This is largely based
145 * on Linus's ALPHA routines, which are pretty portable BTW.
148 extern __inline__ unsigned long find_next_zero_bit(void *addr, unsigned long size, unsigned long offset)
150 unsigned long *p = ((unsigned long *) addr) + (offset >> 6);
151 unsigned long result = offset & ~63UL;
160 tmp |= ~0UL >> (64-offset);
168 while (size & ~63UL) {
180 if (tmp == ~0UL) /* Are any bits zero? */
181 return result + size; /* Nope. */
183 return result + ffz(tmp);
186 #define find_first_zero_bit(addr, size) \
187 find_next_zero_bit((addr), (size), 0)
189 extern long __test_and_set_le_bit(int nr, volatile void *addr);
190 extern long __test_and_clear_le_bit(int nr, volatile void *addr);
192 #define test_and_set_le_bit(nr,addr) (__test_and_set_le_bit(nr,addr)!=0)
193 #define test_and_clear_le_bit(nr,addr) (__test_and_clear_le_bit(nr,addr)!=0)
194 #define set_le_bit(nr,addr) ((void)__test_and_set_le_bit(nr,addr))
195 #define clear_le_bit(nr,addr) ((void)__test_and_clear_le_bit(nr,addr))
197 extern __inline__ int test_le_bit(int nr, __const__ void * addr)
200 __const__ unsigned char *ADDR = (__const__ unsigned char *) addr;
203 mask = 1 << (nr & 0x07);
204 return ((mask & *ADDR) != 0);
207 #define find_first_zero_le_bit(addr, size) \
208 find_next_zero_le_bit((addr), (size), 0)
210 extern __inline__ unsigned long find_next_zero_le_bit(void *addr, unsigned long size, unsigned long offset)
212 unsigned long *p = ((unsigned long *) addr) + (offset >> 6);
213 unsigned long result = offset & ~63UL;
221 tmp = __swab64p(p++);
222 tmp |= (~0UL >> (64-offset));
231 if(~(tmp = __swab64p(p++)))
240 tmp |= (~0UL << size);
241 if (tmp == ~0UL) /* Are any bits zero? */
242 return result + size; /* Nope. */
244 return result + ffz(tmp);
249 #define ext2_set_bit test_and_set_le_bit
250 #define ext2_clear_bit test_and_clear_le_bit
251 #define ext2_test_bit test_le_bit
252 #define ext2_find_first_zero_bit find_first_zero_le_bit
253 #define ext2_find_next_zero_bit find_next_zero_le_bit
255 /* Bitmap functions for the minix filesystem. */
256 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
257 #define minix_set_bit(nr,addr) set_bit(nr,addr)
258 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
259 #define minix_test_bit(nr,addr) test_bit(nr,addr)
260 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
262 #endif /* __KERNEL__ */
264 #endif /* defined(_SPARC64_BITOPS_H) */