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