2 * this is mixture of i386/bitops.h and asm/string.h
3 * taken from the Linux source tree
5 * XXX replace with Mach routines or reprogram in C
7 #ifndef _SYS_GNU_EXT2FS_I386_BITOPS_H_
8 #define _SYS_GNU_EXT2FS_I386_BITOPS_H_
11 * Copyright 1992, Linus Torvalds.
15 * These have to be done with inline assembly: that way the bit-setting
16 * is guaranteed to be atomic. All bit operations return 0 if the bit
17 * was cleared before the operation and != 0 if it was not.
19 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
23 * Some hacks to defeat gcc over-optimizations..
25 struct __dummy { unsigned long a[100]; };
26 #define ADDR (*(struct __dummy *) addr)
28 static __inline__ int set_bit(int nr, void * addr)
32 __asm__ __volatile__("btsl %2,%1\n\tsbbl %0,%0"
33 :"=r" (oldbit),"=m" (ADDR)
38 static __inline__ int clear_bit(int nr, void * addr)
42 __asm__ __volatile__("btrl %2,%1\n\tsbbl %0,%0"
43 :"=r" (oldbit),"=m" (ADDR)
48 static __inline__ int change_bit(int nr, void * addr)
52 __asm__ __volatile__("btcl %2,%1\n\tsbbl %0,%0"
53 :"=r" (oldbit),"=m" (ADDR)
59 * This routine doesn't need to be atomic, but it's faster to code it
62 static __inline__ int test_bit(int nr, void * addr)
66 __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
68 :"m" (ADDR),"ir" (nr));
75 static __inline__ int find_first_zero_bit(void * addr, unsigned size)
87 xorl -4(%%edi),%%eax \n\
90 1: subl %%ebx,%%edi \n\
94 :"c" ((size + 31) >> 5), "D" (addr), "b" (addr)
99 static __inline__ int find_next_zero_bit (void * addr, int size, int offset)
101 unsigned long * p = ((unsigned long *) addr) + (offset >> 5);
102 int set = 0, bit = offset & 31, res;
106 * Look for zero in first byte
114 : "r" (~(*p >> bit)));
115 if (set < (32 - bit))
121 * No zero yet, search remaining full bytes for a zero
123 res = find_first_zero_bit (p, size - 32 * (p - (unsigned long *) addr));
124 return (offset + set + res);
128 * ffz = Find First Zero in word. Undefined if no zero exists,
129 * so code should check against ~0UL first..
131 static __inline__ unsigned long ffz(unsigned long word)
140 * memscan() taken from linux asm/string.h
143 * find the first occurrence of byte 'c', or 1 past the area if none
145 static __inline__ char * memscan(void * addr, unsigned char c, int size)
155 : "=D" (addr), "=c" (size)
156 : "0" (addr), "1" (size), "a" (c));
160 #endif /* !_SYS_GNU_EXT2FS_I386_BITOPS_H_ */