x86: merge the simple bitops and move them to bitops.h
[linux-2.6] / include / asm-x86 / bitops_32.h
1 #ifndef _I386_BITOPS_H
2 #define _I386_BITOPS_H
3
4 /*
5  * Copyright 1992, Linus Torvalds.
6  */
7
8 /**
9  * find_first_zero_bit - find the first zero bit in a memory region
10  * @addr: The address to start the search at
11  * @size: The maximum size to search
12  *
13  * Returns the bit number of the first zero bit, not the number of the byte
14  * containing a bit.
15  */
16 static inline int find_first_zero_bit(const unsigned long *addr, unsigned size)
17 {
18         int d0, d1, d2;
19         int res;
20
21         if (!size)
22                 return 0;
23         /* This looks at memory.
24          * Mark it volatile to tell gcc not to move it around
25          */
26         asm volatile("movl $-1,%%eax\n\t"
27                      "xorl %%edx,%%edx\n\t"
28                      "repe; scasl\n\t"
29                      "je 1f\n\t"
30                      "xorl -4(%%edi),%%eax\n\t"
31                      "subl $4,%%edi\n\t"
32                      "bsfl %%eax,%%edx\n"
33                      "1:\tsubl %%ebx,%%edi\n\t"
34                      "shll $3,%%edi\n\t"
35                      "addl %%edi,%%edx"
36                      : "=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
37                      : "1" ((size + 31) >> 5), "2" (addr),
38                        "b" (addr) : "memory");
39         return res;
40 }
41
42 /**
43  * find_first_bit - find the first set bit in a memory region
44  * @addr: The address to start the search at
45  * @size: The maximum size to search
46  *
47  * Returns the bit number of the first set bit, not the number of the byte
48  * containing a bit.
49  */
50 static inline unsigned find_first_bit(const unsigned long *addr, unsigned size)
51 {
52         unsigned x = 0;
53
54         while (x < size) {
55                 unsigned long val = *addr++;
56                 if (val)
57                         return __ffs(val) + x;
58                 x += sizeof(*addr) << 3;
59         }
60         return x;
61 }
62
63 #ifdef __KERNEL__
64
65 #include <asm-generic/bitops/sched.h>
66
67 #include <asm-generic/bitops/hweight.h>
68
69 #endif /* __KERNEL__ */
70
71 #include <asm-generic/bitops/fls64.h>
72
73 #ifdef __KERNEL__
74
75 #include <asm-generic/bitops/ext2-non-atomic.h>
76
77 #define ext2_set_bit_atomic(lock, nr, addr)                     \
78         test_and_set_bit((nr), (unsigned long *)(addr))
79 #define ext2_clear_bit_atomic(lock, nr, addr)                   \
80         test_and_clear_bit((nr), (unsigned long *)(addr))
81
82 #include <asm-generic/bitops/minix.h>
83
84 #endif /* __KERNEL__ */
85
86 #endif /* _I386_BITOPS_H */