4 * Copyright 1992, Linus Torvalds.
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive
11 #ifndef _LINUX_BITOPS_H
12 #error only <linux/bitops.h> can be included directly
15 #include <linux/compiler.h>
18 * Require 68020 or better.
20 * They use the standard big-endian m680x0 bit ordering.
23 #define test_and_set_bit(nr,vaddr) \
24 (__builtin_constant_p(nr) ? \
25 __constant_test_and_set_bit(nr, vaddr) : \
26 __generic_test_and_set_bit(nr, vaddr))
28 #define __test_and_set_bit(nr,vaddr) test_and_set_bit(nr,vaddr)
30 static inline int __constant_test_and_set_bit(int nr
, unsigned long *vaddr
)
32 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
35 __asm__
__volatile__ ("bset %2,%1; sne %0"
36 : "=d" (retval
), "+m" (*p
)
42 static inline int __generic_test_and_set_bit(int nr
, unsigned long *vaddr
)
46 __asm__
__volatile__ ("bfset %2{%1:#1}; sne %0"
47 : "=d" (retval
) : "d" (nr
^31), "o" (*vaddr
) : "memory");
52 #define set_bit(nr,vaddr) \
53 (__builtin_constant_p(nr) ? \
54 __constant_set_bit(nr, vaddr) : \
55 __generic_set_bit(nr, vaddr))
57 #define __set_bit(nr,vaddr) set_bit(nr,vaddr)
59 static inline void __constant_set_bit(int nr
, volatile unsigned long *vaddr
)
61 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
62 __asm__
__volatile__ ("bset %1,%0"
63 : "+m" (*p
) : "di" (nr
& 7));
66 static inline void __generic_set_bit(int nr
, volatile unsigned long *vaddr
)
68 __asm__
__volatile__ ("bfset %1{%0:#1}"
69 : : "d" (nr
^31), "o" (*vaddr
) : "memory");
72 #define test_and_clear_bit(nr,vaddr) \
73 (__builtin_constant_p(nr) ? \
74 __constant_test_and_clear_bit(nr, vaddr) : \
75 __generic_test_and_clear_bit(nr, vaddr))
77 #define __test_and_clear_bit(nr,vaddr) test_and_clear_bit(nr,vaddr)
79 static inline int __constant_test_and_clear_bit(int nr
, unsigned long *vaddr
)
81 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
84 __asm__
__volatile__ ("bclr %2,%1; sne %0"
85 : "=d" (retval
), "+m" (*p
)
91 static inline int __generic_test_and_clear_bit(int nr
, unsigned long *vaddr
)
95 __asm__
__volatile__ ("bfclr %2{%1:#1}; sne %0"
96 : "=d" (retval
) : "d" (nr
^31), "o" (*vaddr
) : "memory");
102 * clear_bit() doesn't provide any barrier for the compiler.
104 #define smp_mb__before_clear_bit() barrier()
105 #define smp_mb__after_clear_bit() barrier()
107 #define clear_bit(nr,vaddr) \
108 (__builtin_constant_p(nr) ? \
109 __constant_clear_bit(nr, vaddr) : \
110 __generic_clear_bit(nr, vaddr))
111 #define __clear_bit(nr,vaddr) clear_bit(nr,vaddr)
113 static inline void __constant_clear_bit(int nr
, volatile unsigned long *vaddr
)
115 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
116 __asm__
__volatile__ ("bclr %1,%0"
117 : "+m" (*p
) : "di" (nr
& 7));
120 static inline void __generic_clear_bit(int nr
, volatile unsigned long *vaddr
)
122 __asm__
__volatile__ ("bfclr %1{%0:#1}"
123 : : "d" (nr
^31), "o" (*vaddr
) : "memory");
126 #define test_and_change_bit(nr,vaddr) \
127 (__builtin_constant_p(nr) ? \
128 __constant_test_and_change_bit(nr, vaddr) : \
129 __generic_test_and_change_bit(nr, vaddr))
131 #define __test_and_change_bit(nr,vaddr) test_and_change_bit(nr,vaddr)
132 #define __change_bit(nr,vaddr) change_bit(nr,vaddr)
134 static inline int __constant_test_and_change_bit(int nr
, unsigned long *vaddr
)
136 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
139 __asm__
__volatile__ ("bchg %2,%1; sne %0"
140 : "=d" (retval
), "+m" (*p
)
146 static inline int __generic_test_and_change_bit(int nr
, unsigned long *vaddr
)
150 __asm__
__volatile__ ("bfchg %2{%1:#1}; sne %0"
151 : "=d" (retval
) : "d" (nr
^31), "o" (*vaddr
) : "memory");
156 #define change_bit(nr,vaddr) \
157 (__builtin_constant_p(nr) ? \
158 __constant_change_bit(nr, vaddr) : \
159 __generic_change_bit(nr, vaddr))
161 static inline void __constant_change_bit(int nr
, unsigned long *vaddr
)
163 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
164 __asm__
__volatile__ ("bchg %1,%0"
165 : "+m" (*p
) : "di" (nr
& 7));
168 static inline void __generic_change_bit(int nr
, unsigned long *vaddr
)
170 __asm__
__volatile__ ("bfchg %1{%0:#1}"
171 : : "d" (nr
^31), "o" (*vaddr
) : "memory");
174 static inline int test_bit(int nr
, const unsigned long *vaddr
)
176 return (vaddr
[nr
>> 5] & (1UL << (nr
& 31))) != 0;
179 static inline int find_first_zero_bit(const unsigned long *vaddr
,
182 const unsigned long *p
= vaddr
;
190 words
= (size
+ 31) >> 5;
191 while (!(num
= ~*p
++)) {
196 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
197 : "=d" (res
) : "d" (num
& -num
));
200 res
+= ((long)p
- (long)vaddr
- 4) * 8;
201 return res
< size
? res
: size
;
204 static inline int find_next_zero_bit(const unsigned long *vaddr
, int size
,
207 const unsigned long *p
= vaddr
+ (offset
>> 5);
208 int bit
= offset
& 31UL, res
;
214 unsigned long num
= ~*p
++ & (~0UL << bit
);
217 /* Look for zero in first longword */
218 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
219 : "=d" (res
) : "d" (num
& -num
));
222 return offset
< size
? offset
: size
;
229 /* No zero yet, search remaining full bytes for a zero */
230 return offset
+ find_first_zero_bit(p
, size
- offset
);
233 static inline int find_first_bit(const unsigned long *vaddr
, unsigned size
)
235 const unsigned long *p
= vaddr
;
243 words
= (size
+ 31) >> 5;
244 while (!(num
= *p
++)) {
249 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
250 : "=d" (res
) : "d" (num
& -num
));
253 res
+= ((long)p
- (long)vaddr
- 4) * 8;
254 return res
< size
? res
: size
;
257 static inline int find_next_bit(const unsigned long *vaddr
, int size
,
260 const unsigned long *p
= vaddr
+ (offset
>> 5);
261 int bit
= offset
& 31UL, res
;
267 unsigned long num
= *p
++ & (~0UL << bit
);
270 /* Look for one in first longword */
271 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
272 : "=d" (res
) : "d" (num
& -num
));
275 return offset
< size
? offset
: size
;
282 /* No one yet, search remaining full bytes for a one */
283 return offset
+ find_first_bit(p
, size
- offset
);
287 * ffz = Find First Zero in word. Undefined if no zero exists,
288 * so code should check against ~0UL first..
290 static inline unsigned long ffz(unsigned long word
)
294 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
295 : "=d" (res
) : "d" (~word
& -~word
));
302 * ffs: find first bit set. This is defined the same way as
303 * the libc and compiler builtin ffs routines, therefore
304 * differs in spirit from the above ffz (man ffs).
307 static inline int ffs(int x
)
311 asm ("bfffo %1{#0:#0},%0" : "=d" (cnt
) : "dm" (x
& -x
));
315 #define __ffs(x) (ffs(x) - 1)
318 * fls: find last bit set.
321 static inline int fls(int x
)
325 asm ("bfffo %1{#0,#0},%0" : "=d" (cnt
) : "dm" (x
));
330 static inline int __fls(int x
)
335 #include <asm-generic/bitops/fls64.h>
336 #include <asm-generic/bitops/sched.h>
337 #include <asm-generic/bitops/hweight.h>
338 #include <asm-generic/bitops/lock.h>
340 /* Bitmap functions for the little endian bitmap. */
342 static inline void __set_bit_le(int nr
, void *addr
)
344 __set_bit(nr
^ 24, addr
);
347 static inline void __clear_bit_le(int nr
, void *addr
)
349 __clear_bit(nr
^ 24, addr
);
352 static inline int __test_and_set_bit_le(int nr
, void *addr
)
354 return __test_and_set_bit(nr
^ 24, addr
);
357 static inline int test_and_set_bit_le(int nr
, void *addr
)
359 return test_and_set_bit(nr
^ 24, addr
);
362 static inline int __test_and_clear_bit_le(int nr
, void *addr
)
364 return __test_and_clear_bit(nr
^ 24, addr
);
367 static inline int test_and_clear_bit_le(int nr
, void *addr
)
369 return test_and_clear_bit(nr
^ 24, addr
);
372 static inline int test_bit_le(int nr
, const void *vaddr
)
374 const unsigned char *p
= vaddr
;
375 return (p
[nr
>> 3] & (1U << (nr
& 7))) != 0;
378 static inline int find_first_zero_bit_le(const void *vaddr
, unsigned size
)
380 const unsigned long *p
= vaddr
, *addr
= vaddr
;
387 words
= (size
>> 5) + ((size
& 31) > 0);
388 while (*p
++ == ~0UL) {
394 for (res
= 0; res
< 32; res
++)
395 if (!test_bit_le(res
, p
))
398 res
+= (p
- addr
) * 32;
399 return res
< size
? res
: size
;
402 static inline unsigned long find_next_zero_bit_le(const void *addr
,
403 unsigned long size
, unsigned long offset
)
405 const unsigned long *p
= addr
;
406 int bit
= offset
& 31UL, res
;
415 /* Look for zero in first longword */
416 for (res
= bit
; res
< 32; res
++)
417 if (!test_bit_le(res
, p
)) {
419 return offset
< size
? offset
: size
;
427 /* No zero yet, search remaining full bytes for a zero */
428 return offset
+ find_first_zero_bit_le(p
, size
- offset
);
431 static inline int find_first_bit_le(const void *vaddr
, unsigned size
)
433 const unsigned long *p
= vaddr
, *addr
= vaddr
;
440 words
= (size
>> 5) + ((size
& 31) > 0);
441 while (*p
++ == 0UL) {
447 for (res
= 0; res
< 32; res
++)
448 if (test_bit_le(res
, p
))
451 res
+= (p
- addr
) * 32;
452 return res
< size
? res
: size
;
455 static inline unsigned long find_next_bit_le(const void *addr
,
456 unsigned long size
, unsigned long offset
)
458 const unsigned long *p
= addr
;
459 int bit
= offset
& 31UL, res
;
468 /* Look for one in first longword */
469 for (res
= bit
; res
< 32; res
++)
470 if (test_bit_le(res
, p
)) {
472 return offset
< size
? offset
: size
;
480 /* No set bit yet, search remaining full bytes for a set bit */
481 return offset
+ find_first_bit_le(p
, size
- offset
);
484 /* Bitmap functions for the ext2 filesystem. */
486 #define ext2_set_bit_atomic(lock, nr, addr) \
487 test_and_set_bit_le(nr, addr)
488 #define ext2_clear_bit_atomic(lock, nr, addr) \
489 test_and_clear_bit_le(nr, addr)
491 #endif /* __KERNEL__ */
493 #endif /* _M68K_BITOPS_H */