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 #include <linux/compiler.h>
14 * Require 68020 or better.
16 * They use the standard big-endian m680x0 bit ordering.
19 #define test_and_set_bit(nr,vaddr) \
20 (__builtin_constant_p(nr) ? \
21 __constant_test_and_set_bit(nr, vaddr) : \
22 __generic_test_and_set_bit(nr, vaddr))
24 #define __test_and_set_bit(nr,vaddr) test_and_set_bit(nr,vaddr)
26 static inline int __constant_test_and_set_bit(int nr
, unsigned long *vaddr
)
28 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
31 __asm__
__volatile__ ("bset %2,%1; sne %0"
32 : "=d" (retval
), "+m" (*p
)
38 static inline int __generic_test_and_set_bit(int nr
, unsigned long *vaddr
)
42 __asm__
__volatile__ ("bfset %2{%1:#1}; sne %0"
43 : "=d" (retval
) : "d" (nr
^31), "o" (*vaddr
) : "memory");
48 #define set_bit(nr,vaddr) \
49 (__builtin_constant_p(nr) ? \
50 __constant_set_bit(nr, vaddr) : \
51 __generic_set_bit(nr, vaddr))
53 #define __set_bit(nr,vaddr) set_bit(nr,vaddr)
55 static inline void __constant_set_bit(int nr
, volatile unsigned long *vaddr
)
57 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
58 __asm__
__volatile__ ("bset %1,%0"
59 : "+m" (*p
) : "di" (nr
& 7));
62 static inline void __generic_set_bit(int nr
, volatile unsigned long *vaddr
)
64 __asm__
__volatile__ ("bfset %1{%0:#1}"
65 : : "d" (nr
^31), "o" (*vaddr
) : "memory");
68 #define test_and_clear_bit(nr,vaddr) \
69 (__builtin_constant_p(nr) ? \
70 __constant_test_and_clear_bit(nr, vaddr) : \
71 __generic_test_and_clear_bit(nr, vaddr))
73 #define __test_and_clear_bit(nr,vaddr) test_and_clear_bit(nr,vaddr)
75 static inline int __constant_test_and_clear_bit(int nr
, unsigned long *vaddr
)
77 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
80 __asm__
__volatile__ ("bclr %2,%1; sne %0"
81 : "=d" (retval
), "+m" (*p
)
87 static inline int __generic_test_and_clear_bit(int nr
, unsigned long *vaddr
)
91 __asm__
__volatile__ ("bfclr %2{%1:#1}; sne %0"
92 : "=d" (retval
) : "d" (nr
^31), "o" (*vaddr
) : "memory");
98 * clear_bit() doesn't provide any barrier for the compiler.
100 #define smp_mb__before_clear_bit() barrier()
101 #define smp_mb__after_clear_bit() barrier()
103 #define clear_bit(nr,vaddr) \
104 (__builtin_constant_p(nr) ? \
105 __constant_clear_bit(nr, vaddr) : \
106 __generic_clear_bit(nr, vaddr))
107 #define __clear_bit(nr,vaddr) clear_bit(nr,vaddr)
109 static inline void __constant_clear_bit(int nr
, volatile unsigned long *vaddr
)
111 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
112 __asm__
__volatile__ ("bclr %1,%0"
113 : "+m" (*p
) : "di" (nr
& 7));
116 static inline void __generic_clear_bit(int nr
, volatile unsigned long *vaddr
)
118 __asm__
__volatile__ ("bfclr %1{%0:#1}"
119 : : "d" (nr
^31), "o" (*vaddr
) : "memory");
122 #define test_and_change_bit(nr,vaddr) \
123 (__builtin_constant_p(nr) ? \
124 __constant_test_and_change_bit(nr, vaddr) : \
125 __generic_test_and_change_bit(nr, vaddr))
127 #define __test_and_change_bit(nr,vaddr) test_and_change_bit(nr,vaddr)
128 #define __change_bit(nr,vaddr) change_bit(nr,vaddr)
130 static inline int __constant_test_and_change_bit(int nr
, unsigned long *vaddr
)
132 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
135 __asm__
__volatile__ ("bchg %2,%1; sne %0"
136 : "=d" (retval
), "+m" (*p
)
142 static inline int __generic_test_and_change_bit(int nr
, unsigned long *vaddr
)
146 __asm__
__volatile__ ("bfchg %2{%1:#1}; sne %0"
147 : "=d" (retval
) : "d" (nr
^31), "o" (*vaddr
) : "memory");
152 #define change_bit(nr,vaddr) \
153 (__builtin_constant_p(nr) ? \
154 __constant_change_bit(nr, vaddr) : \
155 __generic_change_bit(nr, vaddr))
157 static inline void __constant_change_bit(int nr
, unsigned long *vaddr
)
159 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
160 __asm__
__volatile__ ("bchg %1,%0"
161 : "+m" (*p
) : "di" (nr
& 7));
164 static inline void __generic_change_bit(int nr
, unsigned long *vaddr
)
166 __asm__
__volatile__ ("bfchg %1{%0:#1}"
167 : : "d" (nr
^31), "o" (*vaddr
) : "memory");
170 static inline int test_bit(int nr
, const unsigned long *vaddr
)
172 return (vaddr
[nr
>> 5] & (1UL << (nr
& 31))) != 0;
175 static inline int find_first_zero_bit(const unsigned long *vaddr
,
178 const unsigned long *p
= vaddr
;
185 size
= (size
+ 31) >> 5;
186 while (!(num
= ~*p
++)) {
191 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
192 : "=d" (res
) : "d" (num
& -num
));
195 return ((long)p
- (long)vaddr
- 4) * 8 + res
;
198 static inline int find_next_zero_bit(const unsigned long *vaddr
, int size
,
201 const unsigned long *p
= vaddr
+ (offset
>> 5);
202 int bit
= offset
& 31UL, res
;
208 unsigned long num
= ~*p
++ & (~0UL << bit
);
211 /* Look for zero in first longword */
212 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
213 : "=d" (res
) : "d" (num
& -num
));
215 return offset
+ (res
^ 31);
218 /* No zero yet, search remaining full bytes for a zero */
219 res
= find_first_zero_bit(p
, size
- ((long)p
- (long)vaddr
) * 8);
223 static inline int find_first_bit(const unsigned long *vaddr
, unsigned size
)
225 const unsigned long *p
= vaddr
;
232 size
= (size
+ 31) >> 5;
233 while (!(num
= *p
++)) {
238 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
239 : "=d" (res
) : "d" (num
& -num
));
242 return ((long)p
- (long)vaddr
- 4) * 8 + res
;
245 static inline int find_next_bit(const unsigned long *vaddr
, int size
,
248 const unsigned long *p
= vaddr
+ (offset
>> 5);
249 int bit
= offset
& 31UL, res
;
255 unsigned long num
= *p
++ & (~0UL << bit
);
258 /* Look for one in first longword */
259 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
260 : "=d" (res
) : "d" (num
& -num
));
262 return offset
+ (res
^ 31);
265 /* No one yet, search remaining full bytes for a one */
266 res
= find_first_bit(p
, size
- ((long)p
- (long)vaddr
) * 8);
271 * ffz = Find First Zero in word. Undefined if no zero exists,
272 * so code should check against ~0UL first..
274 static inline unsigned long ffz(unsigned long word
)
278 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
279 : "=d" (res
) : "d" (~word
& -~word
));
286 * ffs: find first bit set. This is defined the same way as
287 * the libc and compiler builtin ffs routines, therefore
288 * differs in spirit from the above ffz (man ffs).
291 static inline int ffs(int x
)
295 asm ("bfffo %1{#0:#0},%0" : "=d" (cnt
) : "dm" (x
& -x
));
299 #define __ffs(x) (ffs(x) - 1)
302 * fls: find last bit set.
305 static inline int fls(int x
)
309 asm ("bfffo %1{#0,#0},%0" : "=d" (cnt
) : "dm" (x
));
313 #define fls64(x) generic_fls64(x)
316 * Every architecture must define this function. It's the fastest
317 * way of searching a 140-bit bitmap where the first 100 bits are
318 * unlikely to be set. It's guaranteed that at least one of the 140
321 static inline int sched_find_first_bit(const unsigned long *b
)
326 return __ffs(b
[1]) + 32;
328 return __ffs(b
[2]) + 64;
330 return __ffs(b
[3]) + 96;
331 return __ffs(b
[4]) + 128;
336 * hweightN: returns the hamming weight (i.e. the number
337 * of bits set) of a N-bit word
340 #define hweight32(x) generic_hweight32(x)
341 #define hweight16(x) generic_hweight16(x)
342 #define hweight8(x) generic_hweight8(x)
344 /* Bitmap functions for the minix filesystem */
346 static inline int minix_find_first_zero_bit(const void *vaddr
, unsigned size
)
348 const unsigned short *p
= vaddr
, *addr
= vaddr
;
355 size
= (size
>> 4) + ((size
& 15) > 0);
356 while (*p
++ == 0xffff)
359 return (p
- addr
) << 4;
363 __asm__
__volatile__ ("bfffo %1{#16,#16},%0"
364 : "=d" (res
) : "d" (num
& -num
));
365 return ((p
- addr
) << 4) + (res
^ 31);
368 #define minix_test_and_set_bit(nr, addr) test_and_set_bit((nr) ^ 16, (unsigned long *)(addr))
369 #define minix_set_bit(nr,addr) set_bit((nr) ^ 16, (unsigned long *)(addr))
370 #define minix_test_and_clear_bit(nr, addr) test_and_clear_bit((nr) ^ 16, (unsigned long *)(addr))
372 static inline int minix_test_bit(int nr
, const void *vaddr
)
374 const unsigned short *p
= vaddr
;
375 return (p
[nr
>> 4] & (1U << (nr
& 15))) != 0;
378 /* Bitmap functions for the ext2 filesystem. */
380 #define ext2_set_bit(nr, addr) test_and_set_bit((nr) ^ 24, (unsigned long *)(addr))
381 #define ext2_set_bit_atomic(lock, nr, addr) test_and_set_bit((nr) ^ 24, (unsigned long *)(addr))
382 #define ext2_clear_bit(nr, addr) test_and_clear_bit((nr) ^ 24, (unsigned long *)(addr))
383 #define ext2_clear_bit_atomic(lock, nr, addr) test_and_clear_bit((nr) ^ 24, (unsigned long *)(addr))
385 static inline int ext2_test_bit(int nr
, const void *vaddr
)
387 const unsigned char *p
= vaddr
;
388 return (p
[nr
>> 3] & (1U << (nr
& 7))) != 0;
391 static inline int ext2_find_first_zero_bit(const void *vaddr
, unsigned size
)
393 const unsigned long *p
= vaddr
, *addr
= vaddr
;
399 size
= (size
>> 5) + ((size
& 31) > 0);
403 return (p
- addr
) << 5;
407 for (res
= 0; res
< 32; res
++)
408 if (!ext2_test_bit (res
, p
))
410 return (p
- addr
) * 32 + res
;
413 static inline int ext2_find_next_zero_bit(const void *vaddr
, unsigned size
,
416 const unsigned long *addr
= vaddr
;
417 const unsigned long *p
= addr
+ (offset
>> 5);
418 int bit
= offset
& 31UL, res
;
424 /* Look for zero in first longword */
425 for (res
= bit
; res
< 32; res
++)
426 if (!ext2_test_bit (res
, p
))
427 return (p
- addr
) * 32 + res
;
430 /* No zero yet, search remaining full bytes for a zero */
431 res
= ext2_find_first_zero_bit (p
, size
- 32 * (p
- addr
));
432 return (p
- addr
) * 32 + res
;
435 #endif /* __KERNEL__ */
437 #endif /* _M68K_BITOPS_H */