Rev 1964 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1408 | serge | 1 | #ifndef _LINUX_BITOPS_H |
2 | #define _LINUX_BITOPS_H |
||
3 | |||
4 | #define BIT(nr) (1UL << (nr)) |
||
5 | #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) |
||
6 | #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) |
||
7 | #define BITS_PER_BYTE 8 |
||
8 | #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long)) |
||
9 | |||
10 | /* |
||
11 | * Include this here because some architectures need generic_ffs/fls in |
||
12 | * scope |
||
13 | */ |
||
14 | #include |
||
15 | |||
16 | #define for_each_bit(bit, addr, size) \ |
||
17 | for ((bit) = find_first_bit((addr), (size)); \ |
||
18 | (bit) < (size); \ |
||
19 | (bit) = find_next_bit((addr), (size), (bit) + 1)) |
||
20 | |||
21 | |||
22 | static __inline__ int get_bitmask_order(unsigned int count) |
||
23 | { |
||
24 | int order; |
||
25 | |||
26 | order = fls(count); |
||
27 | return order; /* We could be slightly more clever with -1 here... */ |
||
28 | } |
||
29 | |||
30 | static __inline__ int get_count_order(unsigned int count) |
||
31 | { |
||
32 | int order; |
||
33 | |||
34 | order = fls(count) - 1; |
||
35 | if (count & (count - 1)) |
||
36 | order++; |
||
37 | return order; |
||
38 | } |
||
39 | |||
40 | static inline unsigned long hweight_long(unsigned long w) |
||
41 | { |
||
42 | return sizeof(w) == 4 ? hweight32(w) : hweight64(w); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * rol32 - rotate a 32-bit value left |
||
47 | * @word: value to rotate |
||
48 | * @shift: bits to roll |
||
49 | */ |
||
50 | static inline __u32 rol32(__u32 word, unsigned int shift) |
||
51 | { |
||
52 | return (word << shift) | (word >> (32 - shift)); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * ror32 - rotate a 32-bit value right |
||
57 | * @word: value to rotate |
||
58 | * @shift: bits to roll |
||
59 | */ |
||
60 | static inline __u32 ror32(__u32 word, unsigned int shift) |
||
61 | { |
||
62 | return (word >> shift) | (word << (32 - shift)); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * rol16 - rotate a 16-bit value left |
||
67 | * @word: value to rotate |
||
68 | * @shift: bits to roll |
||
69 | */ |
||
70 | static inline __u16 rol16(__u16 word, unsigned int shift) |
||
71 | { |
||
72 | return (word << shift) | (word >> (16 - shift)); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * ror16 - rotate a 16-bit value right |
||
77 | * @word: value to rotate |
||
78 | * @shift: bits to roll |
||
79 | */ |
||
80 | static inline __u16 ror16(__u16 word, unsigned int shift) |
||
81 | { |
||
82 | return (word >> shift) | (word << (16 - shift)); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * rol8 - rotate an 8-bit value left |
||
87 | * @word: value to rotate |
||
88 | * @shift: bits to roll |
||
89 | */ |
||
90 | static inline __u8 rol8(__u8 word, unsigned int shift) |
||
91 | { |
||
92 | return (word << shift) | (word >> (8 - shift)); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * ror8 - rotate an 8-bit value right |
||
97 | * @word: value to rotate |
||
98 | * @shift: bits to roll |
||
99 | */ |
||
100 | static inline __u8 ror8(__u8 word, unsigned int shift) |
||
101 | { |
||
102 | return (word >> shift) | (word << (8 - shift)); |
||
103 | } |
||
104 | |||
105 | static inline unsigned fls_long(unsigned long l) |
||
106 | { |
||
107 | if (sizeof(l) == 4) |
||
108 | return fls(l); |
||
109 | return fls64(l); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * __ffs64 - find first set bit in a 64 bit word |
||
114 | * @word: The 64 bit word |
||
115 | * |
||
116 | * On 64 bit arches this is a synomyn for __ffs |
||
117 | * The result is not defined if no bits are set, so check that @word |
||
118 | * is non-zero before calling this. |
||
119 | */ |
||
120 | static inline unsigned long __ffs64(u64 word) |
||
121 | { |
||
122 | #if BITS_PER_LONG == 32 |
||
123 | if (((u32)word) == 0UL) |
||
124 | return __ffs((u32)(word >> 32)) + 32; |
||
125 | #elif BITS_PER_LONG != 64 |
||
126 | #error BITS_PER_LONG not 32 or 64 |
||
127 | #endif |
||
128 | return __ffs((unsigned long)word); |
||
129 | } |
||
130 | |||
131 | #ifdef __KERNEL__ |
||
132 | #ifdef CONFIG_GENERIC_FIND_FIRST_BIT |
||
133 | |||
134 | /** |
||
135 | * find_first_bit - find the first set bit in a memory region |
||
136 | * @addr: The address to start the search at |
||
137 | * @size: The maximum size to search |
||
138 | * |
||
139 | * Returns the bit number of the first set bit. |
||
140 | */ |
||
141 | extern unsigned long find_first_bit(const unsigned long *addr, |
||
142 | unsigned long size); |
||
143 | |||
144 | /** |
||
145 | * find_first_zero_bit - find the first cleared bit in a memory region |
||
146 | * @addr: The address to start the search at |
||
147 | * @size: The maximum size to search |
||
148 | * |
||
149 | * Returns the bit number of the first cleared bit. |
||
150 | */ |
||
151 | extern unsigned long find_first_zero_bit(const unsigned long *addr, |
||
152 | unsigned long size); |
||
153 | #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */ |
||
154 | |||
155 | #ifdef CONFIG_GENERIC_FIND_LAST_BIT |
||
156 | /** |
||
157 | * find_last_bit - find the last set bit in a memory region |
||
158 | * @addr: The address to start the search at |
||
159 | * @size: The maximum size to search |
||
160 | * |
||
161 | * Returns the bit number of the first set bit, or size. |
||
162 | */ |
||
163 | extern unsigned long find_last_bit(const unsigned long *addr, |
||
164 | unsigned long size); |
||
165 | #endif /* CONFIG_GENERIC_FIND_LAST_BIT */ |
||
166 | |||
167 | #ifdef CONFIG_GENERIC_FIND_NEXT_BIT |
||
168 | |||
169 | /** |
||
170 | * find_next_bit - find the next set bit in a memory region |
||
171 | * @addr: The address to base the search on |
||
172 | * @offset: The bitnumber to start searching at |
||
173 | * @size: The bitmap size in bits |
||
174 | */ |
||
175 | extern unsigned long find_next_bit(const unsigned long *addr, |
||
176 | unsigned long size, unsigned long offset); |
||
177 | |||
178 | /** |
||
179 | * find_next_zero_bit - find the next cleared bit in a memory region |
||
180 | * @addr: The address to base the search on |
||
181 | * @offset: The bitnumber to start searching at |
||
182 | * @size: The bitmap size in bits |
||
183 | */ |
||
184 | |||
185 | extern unsigned long find_next_zero_bit(const unsigned long *addr, |
||
186 | unsigned long size, |
||
187 | unsigned long offset); |
||
188 | |||
189 | #endif /* CONFIG_GENERIC_FIND_NEXT_BIT */ |
||
190 | #endif /* __KERNEL__ */ |
||
191 | #endif><>><>><>><>><>><>>><>><> |