1bdea89bf62d2b18e09a99ddb1969ee9dd6101c2
[dragonfly.git] / sys / vfs / gnu / ext2fs / i386-bitops.h
1 /*
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 $
4  */
5 /*
6  * this is mixture of i386/bitops.h and asm/string.h
7  * taken from the Linux source tree 
8  *
9  * XXX replace with Mach routines or reprogram in C
10  */
11 #ifndef _VFS_GNU_EXT2FS_I386_BITOPS_H_
12 #define _VFS_GNU_EXT2FS_I386_BITOPS_H_
13
14 /*
15  * Copyright 1992, Linus Torvalds.
16  */
17
18 /*
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.
22  *
23  * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
24  */
25
26 /*
27  * Some hacks to defeat gcc over-optimizations..
28  */
29 struct __dummy { unsigned long a[100]; };
30 #define ADDR (*(struct __dummy *) addr)
31
32 static __inline__ int
33 set_bit(int nr, void *addr)
34 {
35         int oldbit;
36
37         __asm__ __volatile__("btsl %2,%1\n\tsbbl %0,%0"
38                 :"=r" (oldbit),"=m" (ADDR)
39                 :"ir" (nr));
40         return oldbit;
41 }
42
43 static __inline__ int
44 clear_bit(int nr, void *addr)
45 {
46         int oldbit;
47
48         __asm__ __volatile__("btrl %2,%1\n\tsbbl %0,%0"
49                 :"=r" (oldbit),"=m" (ADDR)
50                 :"ir" (nr));
51         return oldbit;
52 }
53
54 static __inline__ int
55 change_bit(int nr, void *addr)
56 {
57         int oldbit;
58
59         __asm__ __volatile__("btcl %2,%1\n\tsbbl %0,%0"
60                 :"=r" (oldbit),"=m" (ADDR)
61                 :"ir" (nr));
62         return oldbit;
63 }
64
65 /*
66  * This routine doesn't need to be atomic, but it's faster to code it
67  * this way.
68  */
69 static __inline__ int
70 test_bit(int nr, void *addr)
71 {
72         int oldbit;
73
74         __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0"
75                 :"=r" (oldbit)
76                 :"m" (ADDR),"ir" (nr));
77         return oldbit;
78 }
79
80 /*
81  * Find-bit routines..
82  */
83 static __inline__ int
84 find_first_zero_bit(void *addr, unsigned size)
85 {
86         int res;
87         int _count = (size + 31) >> 5;
88
89         if (!size)
90                 return 0;
91         __asm__("                       \n\
92                 cld                     \n\
93                 movl $-1,%%eax          \n\
94                 xorl %%edx,%%edx        \n\
95                 repe; scasl             \n\
96                 je 1f                   \n\
97                 xorl -4(%%edi),%%eax    \n\
98                 subl $4,%%edi           \n\
99                 bsfl %%eax,%%edx        \n\
100 1:              subl %%ebx,%%edi        \n\
101                 shll $3,%%edi           \n\
102                 addl %%edi,%%edx"
103                 : "=c" (_count), "=D" (addr), "=d" (res)
104                 : "0" (_count), "1" (addr), "b" (addr)
105                 : "ax");
106         return res;
107 }
108
109 static __inline__ int
110 find_next_zero_bit(void *addr, int size, int offset)
111 {
112         unsigned long * p = ((unsigned long *) addr) + (offset >> 5);
113         int set = 0, bit = offset & 31, res;
114         
115         if (bit) {
116                 /*
117                  * Look for zero in first byte
118                  */
119                 __asm__("                       \n\
120                         bsfl %1,%0              \n\
121                         jne 1f                  \n\
122                         movl $32, %0            \n\
123 1:                      "
124                         : "=r" (set)
125                         : "r" (~(*p >> bit)));
126                 if (set < (32 - bit))
127                         return set + offset;
128                 set = 32 - bit;
129                 p++;
130         }
131         /*
132          * No zero yet, search remaining full bytes for a zero
133          */
134         res = find_first_zero_bit (p, size - 32 * (p - (unsigned long *) addr));
135         return (offset + set + res);
136 }
137
138 /*
139  * ffz = Find First Zero in word. Undefined if no zero exists,
140  * so code should check against ~0UL first..
141  */
142 static __inline__ unsigned long
143 ffz(unsigned long word)
144 {
145         __asm__("bsfl %1,%0"
146                 :"=r" (word)
147                 :"r" (~word));
148         return word;
149 }
150
151 /* 
152  * memscan() taken from linux asm/string.h
153  */
154 /*
155  * find the first occurrence of byte 'c', or 1 past the area if none
156  */
157 static __inline__ char *
158 memscan(void *addr, unsigned char c, int size)
159 {
160         if (!size)
161                 return addr;
162         __asm__("                       \n\
163                 cld                     \n\
164                 repnz; scasb            \n\
165                 jnz 1f                  \n\
166                 dec %%edi               \n\
167 1:              "
168                 : "=D" (addr), "=c" (size)
169                 : "0" (addr), "1" (size), "a" (c));
170         return addr;
171 }
172
173 #endif /* _VFS_GNU_EXT2FS_I386_BITOPS_H_ */