3 * this is mixture of i386/bitops.h and asm/string.h
4 * taken from the Linux source tree
6 * XXX replace with Mach routines or reprogram in C
9 * Copyright 1992, Linus Torvalds.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #ifndef _SYS_GNU_EXT2FS_I386_BITOPS_H_
26 #define _SYS_GNU_EXT2FS_I386_BITOPS_H_
29 * These have to be done with inline assembly: that way the bit-setting
30 * is guaranteed to be atomic. All bit operations return 0 if the bit
31 * was cleared before the operation and != 0 if it was not.
33 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
37 * Some hacks to defeat gcc over-optimizations..
39 struct __dummy { unsigned long a[100]; };
40 #define ADDR (*(struct __dummy *) addr)
42 static __inline__ int set_bit(int nr, void * addr)
46 __asm__ __volatile__("btsl %2,%1\n\tsbbl %0,%0"
47 :"=r" (oldbit),"=m" (ADDR)
52 static __inline__ int clear_bit(int nr, void * addr)
56 __asm__ __volatile__("btrl %2,%1\n\tsbbl %0,%0"
57 :"=r" (oldbit),"=m" (ADDR)
62 static __inline__ int change_bit(int nr, void * addr)
66 __asm__ __volatile__("btcl %2,%1\n\tsbbl %0,%0"
67 :"=r" (oldbit),"=m" (ADDR)
73 * This routine doesn't need to be atomic, but it's faster to code it
76 static __inline__ int test_bit(int nr, void * addr)
80 __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
82 :"m" (ADDR),"ir" (nr));
89 static __inline__ int find_first_zero_bit(void * addr, unsigned size)
92 int _count = (size + 31) >> 5;
102 xorl -4(%%edi),%%eax \n\
105 1: subl %%ebx,%%edi \n\
108 : "=c" (_count), "=D" (addr), "=d" (res)
109 : "0" (_count), "1" (addr), "b" (addr)
114 static __inline__ int find_next_zero_bit (void * addr, int size, int offset)
116 unsigned long * p = ((unsigned long *) addr) + (offset >> 5);
117 int set = 0, bit = offset & 31, res;
121 * Look for zero in first byte
129 : "r" (~(*p >> bit)));
130 if (set < (32 - bit))
136 * No zero yet, search remaining full bytes for a zero
138 res = find_first_zero_bit (p, size - 32 * (p - (unsigned long *) addr));
139 return (offset + set + res);
143 * ffz = Find First Zero in word. Undefined if no zero exists,
144 * so code should check against ~0UL first..
146 static __inline__ unsigned long ffz(unsigned long word)
155 * memscan() taken from linux asm/string.h
158 * find the first occurrence of byte 'c', or 1 past the area if none
160 static __inline__ char * memscan(void * addr, unsigned char c, int size)
170 : "=D" (addr), "=c" (size)
171 : "0" (addr), "1" (size), "a" (c));
175 #endif /* !_SYS_GNU_EXT2FS_I386_BITOPS_H_ */