1#ifndef _I386_BITOPS_H
2#define _I386_BITOPS_H
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#ifdef CONFIG_SMP
18#define LOCK_PREFIX "lock ; "
19#else
20#define LOCK_PREFIX ""
21#endif
22
23#define ADDR (*(volatile long *) addr)
24
25
26
27
28
29
30
31
32
33
34
35static __inline__ void set_bit(int nr, volatile void * addr)
36{
37 __asm__ __volatile__( LOCK_PREFIX
38 "btsl %1,%0"
39 :"=m" (ADDR)
40 :"Ir" (nr));
41}
42
43
44
45
46
47
48
49
50
51
52static __inline__ void __set_bit(int nr, volatile void * addr)
53{
54 __asm__(
55 "btsl %1,%0"
56 :"=m" (ADDR)
57 :"Ir" (nr));
58}
59
60
61
62
63
64
65
66
67
68
69
70static __inline__ void clear_bit(int nr, volatile void * addr)
71{
72 __asm__ __volatile__( LOCK_PREFIX
73 "btrl %1,%0"
74 :"=m" (ADDR)
75 :"Ir" (nr));
76}
77#define smp_mb__before_clear_bit() barrier()
78#define smp_mb__after_clear_bit() barrier()
79
80
81
82
83
84
85
86
87
88
89static __inline__ void __change_bit(int nr, volatile void * addr)
90{
91 __asm__ __volatile__(
92 "btcl %1,%0"
93 :"=m" (ADDR)
94 :"Ir" (nr));
95}
96
97
98
99
100
101
102
103
104
105
106static __inline__ void change_bit(int nr, volatile void * addr)
107{
108 __asm__ __volatile__( LOCK_PREFIX
109 "btcl %1,%0"
110 :"=m" (ADDR)
111 :"Ir" (nr));
112}
113
114
115
116
117
118
119
120
121
122static __inline__ int test_and_set_bit(int nr, volatile void * addr)
123{
124 int oldbit;
125
126 __asm__ __volatile__( LOCK_PREFIX
127 "btsl %2,%1\n\tsbbl %0,%0"
128 :"=r" (oldbit),"=m" (ADDR)
129 :"Ir" (nr) : "memory");
130 return oldbit;
131}
132
133
134
135
136
137
138
139
140
141
142static __inline__ int __test_and_set_bit(int nr, volatile void * addr)
143{
144 int oldbit;
145
146 __asm__(
147 "btsl %2,%1\n\tsbbl %0,%0"
148 :"=r" (oldbit),"=m" (ADDR)
149 :"Ir" (nr));
150 return oldbit;
151}
152
153
154
155
156
157
158
159
160
161static __inline__ int test_and_clear_bit(int nr, volatile void * addr)
162{
163 int oldbit;
164
165 __asm__ __volatile__( LOCK_PREFIX
166 "btrl %2,%1\n\tsbbl %0,%0"
167 :"=r" (oldbit),"=m" (ADDR)
168 :"Ir" (nr) : "memory");
169 return oldbit;
170}
171
172
173
174
175
176
177
178
179
180
181static __inline__ int __test_and_clear_bit(int nr, volatile void * addr)
182{
183 int oldbit;
184
185 __asm__(
186 "btrl %2,%1\n\tsbbl %0,%0"
187 :"=r" (oldbit),"=m" (ADDR)
188 :"Ir" (nr));
189 return oldbit;
190}
191
192
193static __inline__ int __test_and_change_bit(int nr, volatile void * addr)
194{
195 int oldbit;
196
197 __asm__ __volatile__(
198 "btcl %2,%1\n\tsbbl %0,%0"
199 :"=r" (oldbit),"=m" (ADDR)
200 :"Ir" (nr) : "memory");
201 return oldbit;
202}
203
204
205
206
207
208
209
210
211
212static __inline__ int test_and_change_bit(int nr, volatile void * addr)
213{
214 int oldbit;
215
216 __asm__ __volatile__( LOCK_PREFIX
217 "btcl %2,%1\n\tsbbl %0,%0"
218 :"=r" (oldbit),"=m" (ADDR)
219 :"Ir" (nr) : "memory");
220 return oldbit;
221}
222
223#if 0
224
225
226
227
228
229static int test_bit(int nr, const volatile void * addr);
230#endif
231
232static __inline__ int constant_test_bit(int nr, const volatile void * addr)
233{
234 return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
235}
236
237static __inline__ int variable_test_bit(int nr, volatile void * addr)
238{
239 int oldbit;
240
241 __asm__ __volatile__(
242 "btl %2,%1\n\tsbbl %0,%0"
243 :"=r" (oldbit)
244 :"m" (ADDR),"Ir" (nr));
245 return oldbit;
246}
247
248#define test_bit(nr,addr) \
249(__builtin_constant_p(nr) ? \
250 constant_test_bit((nr),(addr)) : \
251 variable_test_bit((nr),(addr)))
252
253
254
255
256
257
258
259
260
261static __inline__ int find_first_zero_bit(void * addr, unsigned size)
262{
263 int d0, d1, d2;
264 int res;
265
266 if (!size)
267 return 0;
268
269 __asm__ __volatile__(
270 "movl $-1,%%eax\n\t"
271 "xorl %%edx,%%edx\n\t"
272 "repe; scasl\n\t"
273 "je 1f\n\t"
274 "xorl -4(%%edi),%%eax\n\t"
275 "subl $4,%%edi\n\t"
276 "bsfl %%eax,%%edx\n"
277 "1:\tsubl %%ebx,%%edi\n\t"
278 "shll $3,%%edi\n\t"
279 "addl %%edi,%%edx"
280 :"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
281 :"1" ((size + 31) >> 5), "2" (addr), "b" (addr));
282 return res;
283}
284
285
286
287
288
289
290
291static __inline__ int find_next_zero_bit (void * addr, int size, int offset)
292{
293 unsigned long * p = ((unsigned long *) addr) + (offset >> 5);
294 int set = 0, bit = offset & 31, res;
295
296 if (bit) {
297
298
299
300 __asm__("bsfl %1,%0\n\t"
301 "jne 1f\n\t"
302 "movl $32, %0\n"
303 "1:"
304 : "=r" (set)
305 : "r" (~(*p >> bit)));
306 if (set < (32 - bit))
307 return set + offset;
308 set = 32 - bit;
309 p++;
310 }
311
312
313
314 res = find_first_zero_bit (p, size - 32 * (p - (unsigned long *) addr));
315 return (offset + set + res);
316}
317
318
319
320
321
322
323
324static __inline__ unsigned long ffz(unsigned long word)
325{
326 __asm__("bsfl %1,%0"
327 :"=r" (word)
328 :"r" (~word));
329 return word;
330}
331
332#ifdef __KERNEL__
333
334
335
336
337
338
339
340
341
342static __inline__ int ffs(int x)
343{
344 int r;
345
346 __asm__("bsfl %1,%0\n\t"
347 "jnz 1f\n\t"
348 "movl $-1,%0\n"
349 "1:" : "=r" (r) : "g" (x));
350 return r+1;
351}
352
353
354
355
356
357
358
359
360#define hweight32(x) generic_hweight32(x)
361#define hweight16(x) generic_hweight16(x)
362#define hweight8(x) generic_hweight8(x)
363
364#endif
365
366#ifdef __KERNEL__
367
368#define ext2_set_bit __test_and_set_bit
369#define ext2_clear_bit __test_and_clear_bit
370#define ext2_test_bit test_bit
371#define ext2_find_first_zero_bit find_first_zero_bit
372#define ext2_find_next_zero_bit find_next_zero_bit
373
374
375#define minix_test_and_set_bit(nr,addr) __test_and_set_bit(nr,addr)
376#define minix_set_bit(nr,addr) __set_bit(nr,addr)
377#define minix_test_and_clear_bit(nr,addr) __test_and_clear_bit(nr,addr)
378#define minix_test_bit(nr,addr) test_bit(nr,addr)
379#define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
380
381#endif
382
383#endif
384