2 * $FreeBSD: src/sys/gnu/ext2fs/i386-bitops.h,v 1.5 1999/11/15 23:16:06 obrien Exp $
3 * $DragonFly: src/sys/vfs/gnu/ext2fs/i386-bitops.h,v 1.4 2006/04/04 17:34:32 dillon Exp $
6 * this is mixture of i386/bitops.h and asm/string.h
7 * taken from the Linux source tree
9 * XXX replace with Mach routines or reprogram in C
11 #ifndef _VFS_GNU_EXT2FS_I386_BITOPS_H_
12 #define _VFS_GNU_EXT2FS_I386_BITOPS_H_
15 * Copyright 1992, Linus Torvalds.
19 * These have to be done with inline assembly: that way the bit-setting
20 * is guaranteed to be atomic. All bit operations return 0 if the bit
21 * was cleared before the operation and != 0 if it was not.
23 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
27 * Some hacks to defeat gcc over-optimizations..
29 struct __dummy { unsigned long a[100]; };
30 #define ADDR (*(struct __dummy *) addr)
33 set_bit(int nr, void *addr)
37 __asm__ __volatile__("btsl %2,%1\n\tsbbl %0,%0"
38 :"=r" (oldbit),"=m" (ADDR)
44 clear_bit(int nr, void *addr)
48 __asm__ __volatile__("btrl %2,%1\n\tsbbl %0,%0"
49 :"=r" (oldbit),"=m" (ADDR)
55 change_bit(int nr, void *addr)
59 __asm__ __volatile__("btcl %2,%1\n\tsbbl %0,%0"
60 :"=r" (oldbit),"=m" (ADDR)
66 * This routine doesn't need to be atomic, but it's faster to code it
70 test_bit(int nr, void *addr)
74 __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
76 :"m" (ADDR),"ir" (nr));
84 find_first_zero_bit(void *addr, unsigned size)
87 int _count = (size + 31) >> 5;
97 xorl -4(%%edi),%%eax \n\
100 1: subl %%ebx,%%edi \n\
103 : "=c" (_count), "=D" (addr), "=d" (res)
104 : "0" (_count), "1" (addr), "b" (addr)
109 static __inline__ int
110 find_next_zero_bit(void *addr, int size, int offset)
112 unsigned long * p = ((unsigned long *) addr) + (offset >> 5);
113 int set = 0, bit = offset & 31, res;
117 * Look for zero in first byte
125 : "r" (~(*p >> bit)));
126 if (set < (32 - bit))
132 * No zero yet, search remaining full bytes for a zero
134 res = find_first_zero_bit (p, size - 32 * (p - (unsigned long *) addr));
135 return (offset + set + res);
139 * ffz = Find First Zero in word. Undefined if no zero exists,
140 * so code should check against ~0UL first..
142 static __inline__ unsigned long
143 ffz(unsigned long word)
152 * memscan() taken from linux asm/string.h
155 * find the first occurrence of byte 'c', or 1 past the area if none
157 static __inline__ char *
158 memscan(void *addr, unsigned char c, int size)
168 : "=D" (addr), "=c" (size)
169 : "0" (addr), "1" (size), "a" (c));
173 #endif /* _VFS_GNU_EXT2FS_I386_BITOPS_H_ */