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