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>
16 #include <asm/barrier.h>
19 * Bit access functions vary across the ColdFire and 68k families.
20 * So we will break them out here, and then macro in the ones we want.
22 * ColdFire - supports standard bset/bclr/bchg with register operand only
23 * 68000 - supports standard bset/bclr/bchg with memory operand
24 * >= 68020 - also supports the bfset/bfclr/bfchg instructions
26 * Although it is possible to use only the bset/bclr/bchg with register
27 * operands on all platforms you end up with larger generated code.
28 * So we use the best form possible on a given platform.
31 static inline void bset_reg_set_bit(int nr
, volatile unsigned long *vaddr
)
33 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
35 __asm__
__volatile__ ("bset %1,(%0)"
37 : "a" (p
), "di" (nr
& 7)
41 static inline void bset_mem_set_bit(int nr
, volatile unsigned long *vaddr
)
43 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
45 __asm__
__volatile__ ("bset %1,%0"
50 static inline void bfset_mem_set_bit(int nr
, volatile unsigned long *vaddr
)
52 __asm__
__volatile__ ("bfset %1{%0:#1}"
54 : "d" (nr
^ 31), "o" (*vaddr
)
58 #if defined(CONFIG_COLDFIRE)
59 #define set_bit(nr, vaddr) bset_reg_set_bit(nr, vaddr)
60 #elif defined(CONFIG_CPU_HAS_NO_BITFIELDS)
61 #define set_bit(nr, vaddr) bset_mem_set_bit(nr, vaddr)
63 #define set_bit(nr, vaddr) (__builtin_constant_p(nr) ? \
64 bset_mem_set_bit(nr, vaddr) : \
65 bfset_mem_set_bit(nr, vaddr))
68 #define __set_bit(nr, vaddr) set_bit(nr, vaddr)
71 static inline void bclr_reg_clear_bit(int nr
, volatile unsigned long *vaddr
)
73 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
75 __asm__
__volatile__ ("bclr %1,(%0)"
77 : "a" (p
), "di" (nr
& 7)
81 static inline void bclr_mem_clear_bit(int nr
, volatile unsigned long *vaddr
)
83 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
85 __asm__
__volatile__ ("bclr %1,%0"
90 static inline void bfclr_mem_clear_bit(int nr
, volatile unsigned long *vaddr
)
92 __asm__
__volatile__ ("bfclr %1{%0:#1}"
94 : "d" (nr
^ 31), "o" (*vaddr
)
98 #if defined(CONFIG_COLDFIRE)
99 #define clear_bit(nr, vaddr) bclr_reg_clear_bit(nr, vaddr)
100 #elif defined(CONFIG_CPU_HAS_NO_BITFIELDS)
101 #define clear_bit(nr, vaddr) bclr_mem_clear_bit(nr, vaddr)
103 #define clear_bit(nr, vaddr) (__builtin_constant_p(nr) ? \
104 bclr_mem_clear_bit(nr, vaddr) : \
105 bfclr_mem_clear_bit(nr, vaddr))
108 #define __clear_bit(nr, vaddr) clear_bit(nr, vaddr)
111 static inline void bchg_reg_change_bit(int nr
, volatile unsigned long *vaddr
)
113 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
115 __asm__
__volatile__ ("bchg %1,(%0)"
117 : "a" (p
), "di" (nr
& 7)
121 static inline void bchg_mem_change_bit(int nr
, volatile unsigned long *vaddr
)
123 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
125 __asm__
__volatile__ ("bchg %1,%0"
130 static inline void bfchg_mem_change_bit(int nr
, volatile unsigned long *vaddr
)
132 __asm__
__volatile__ ("bfchg %1{%0:#1}"
134 : "d" (nr
^ 31), "o" (*vaddr
)
138 #if defined(CONFIG_COLDFIRE)
139 #define change_bit(nr, vaddr) bchg_reg_change_bit(nr, vaddr)
140 #elif defined(CONFIG_CPU_HAS_NO_BITFIELDS)
141 #define change_bit(nr, vaddr) bchg_mem_change_bit(nr, vaddr)
143 #define change_bit(nr, vaddr) (__builtin_constant_p(nr) ? \
144 bchg_mem_change_bit(nr, vaddr) : \
145 bfchg_mem_change_bit(nr, vaddr))
148 #define __change_bit(nr, vaddr) change_bit(nr, vaddr)
151 static inline int test_bit(int nr
, const volatile unsigned long *vaddr
)
153 return (vaddr
[nr
>> 5] & (1UL << (nr
& 31))) != 0;
157 static inline int bset_reg_test_and_set_bit(int nr
,
158 volatile unsigned long *vaddr
)
160 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
163 __asm__
__volatile__ ("bset %2,(%1); sne %0"
165 : "a" (p
), "di" (nr
& 7)
170 static inline int bset_mem_test_and_set_bit(int nr
,
171 volatile unsigned long *vaddr
)
173 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
176 __asm__
__volatile__ ("bset %2,%1; sne %0"
177 : "=d" (retval
), "+m" (*p
)
182 static inline int bfset_mem_test_and_set_bit(int nr
,
183 volatile unsigned long *vaddr
)
187 __asm__
__volatile__ ("bfset %2{%1:#1}; sne %0"
189 : "d" (nr
^ 31), "o" (*vaddr
)
194 #if defined(CONFIG_COLDFIRE)
195 #define test_and_set_bit(nr, vaddr) bset_reg_test_and_set_bit(nr, vaddr)
196 #elif defined(CONFIG_CPU_HAS_NO_BITFIELDS)
197 #define test_and_set_bit(nr, vaddr) bset_mem_test_and_set_bit(nr, vaddr)
199 #define test_and_set_bit(nr, vaddr) (__builtin_constant_p(nr) ? \
200 bset_mem_test_and_set_bit(nr, vaddr) : \
201 bfset_mem_test_and_set_bit(nr, vaddr))
204 #define __test_and_set_bit(nr, vaddr) test_and_set_bit(nr, vaddr)
207 static inline int bclr_reg_test_and_clear_bit(int nr
,
208 volatile unsigned long *vaddr
)
210 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
213 __asm__
__volatile__ ("bclr %2,(%1); sne %0"
215 : "a" (p
), "di" (nr
& 7)
220 static inline int bclr_mem_test_and_clear_bit(int nr
,
221 volatile unsigned long *vaddr
)
223 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
226 __asm__
__volatile__ ("bclr %2,%1; sne %0"
227 : "=d" (retval
), "+m" (*p
)
232 static inline int bfclr_mem_test_and_clear_bit(int nr
,
233 volatile unsigned long *vaddr
)
237 __asm__
__volatile__ ("bfclr %2{%1:#1}; sne %0"
239 : "d" (nr
^ 31), "o" (*vaddr
)
244 #if defined(CONFIG_COLDFIRE)
245 #define test_and_clear_bit(nr, vaddr) bclr_reg_test_and_clear_bit(nr, vaddr)
246 #elif defined(CONFIG_CPU_HAS_NO_BITFIELDS)
247 #define test_and_clear_bit(nr, vaddr) bclr_mem_test_and_clear_bit(nr, vaddr)
249 #define test_and_clear_bit(nr, vaddr) (__builtin_constant_p(nr) ? \
250 bclr_mem_test_and_clear_bit(nr, vaddr) : \
251 bfclr_mem_test_and_clear_bit(nr, vaddr))
254 #define __test_and_clear_bit(nr, vaddr) test_and_clear_bit(nr, vaddr)
257 static inline int bchg_reg_test_and_change_bit(int nr
,
258 volatile unsigned long *vaddr
)
260 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
263 __asm__
__volatile__ ("bchg %2,(%1); sne %0"
265 : "a" (p
), "di" (nr
& 7)
270 static inline int bchg_mem_test_and_change_bit(int nr
,
271 volatile unsigned long *vaddr
)
273 char *p
= (char *)vaddr
+ (nr
^ 31) / 8;
276 __asm__
__volatile__ ("bchg %2,%1; sne %0"
277 : "=d" (retval
), "+m" (*p
)
282 static inline int bfchg_mem_test_and_change_bit(int nr
,
283 volatile unsigned long *vaddr
)
287 __asm__
__volatile__ ("bfchg %2{%1:#1}; sne %0"
289 : "d" (nr
^ 31), "o" (*vaddr
)
294 #if defined(CONFIG_COLDFIRE)
295 #define test_and_change_bit(nr, vaddr) bchg_reg_test_and_change_bit(nr, vaddr)
296 #elif defined(CONFIG_CPU_HAS_NO_BITFIELDS)
297 #define test_and_change_bit(nr, vaddr) bchg_mem_test_and_change_bit(nr, vaddr)
299 #define test_and_change_bit(nr, vaddr) (__builtin_constant_p(nr) ? \
300 bchg_mem_test_and_change_bit(nr, vaddr) : \
301 bfchg_mem_test_and_change_bit(nr, vaddr))
304 #define __test_and_change_bit(nr, vaddr) test_and_change_bit(nr, vaddr)
308 * The true 68020 and more advanced processors support the "bfffo"
309 * instruction for finding bits. ColdFire and simple 68000 parts
310 * (including CPU32) do not support this. They simply use the generic
313 #if defined(CONFIG_CPU_HAS_NO_BITFIELDS)
314 #include <asm-generic/bitops/ffz.h>
317 static inline int find_first_zero_bit(const unsigned long *vaddr
,
320 const unsigned long *p
= vaddr
;
328 words
= (size
+ 31) >> 5;
329 while (!(num
= ~*p
++)) {
334 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
335 : "=d" (res
) : "d" (num
& -num
));
338 res
+= ((long)p
- (long)vaddr
- 4) * 8;
339 return res
< size
? res
: size
;
341 #define find_first_zero_bit find_first_zero_bit
343 static inline int find_next_zero_bit(const unsigned long *vaddr
, int size
,
346 const unsigned long *p
= vaddr
+ (offset
>> 5);
347 int bit
= offset
& 31UL, res
;
353 unsigned long num
= ~*p
++ & (~0UL << bit
);
356 /* Look for zero in first longword */
357 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
358 : "=d" (res
) : "d" (num
& -num
));
361 return offset
< size
? offset
: size
;
368 /* No zero yet, search remaining full bytes for a zero */
369 return offset
+ find_first_zero_bit(p
, size
- offset
);
371 #define find_next_zero_bit find_next_zero_bit
373 static inline int find_first_bit(const unsigned long *vaddr
, unsigned size
)
375 const unsigned long *p
= vaddr
;
383 words
= (size
+ 31) >> 5;
384 while (!(num
= *p
++)) {
389 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
390 : "=d" (res
) : "d" (num
& -num
));
393 res
+= ((long)p
- (long)vaddr
- 4) * 8;
394 return res
< size
? res
: size
;
396 #define find_first_bit find_first_bit
398 static inline int find_next_bit(const unsigned long *vaddr
, int size
,
401 const unsigned long *p
= vaddr
+ (offset
>> 5);
402 int bit
= offset
& 31UL, res
;
408 unsigned long num
= *p
++ & (~0UL << bit
);
411 /* Look for one in first longword */
412 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
413 : "=d" (res
) : "d" (num
& -num
));
416 return offset
< size
? offset
: size
;
423 /* No one yet, search remaining full bytes for a one */
424 return offset
+ find_first_bit(p
, size
- offset
);
426 #define find_next_bit find_next_bit
429 * ffz = Find First Zero in word. Undefined if no zero exists,
430 * so code should check against ~0UL first..
432 static inline unsigned long ffz(unsigned long word
)
436 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
437 : "=d" (res
) : "d" (~word
& -~word
));
443 #include <asm-generic/bitops/find.h>
447 #if defined(CONFIG_CPU_HAS_NO_BITFIELDS)
450 * The newer ColdFire family members support a "bitrev" instruction
451 * and we can use that to implement a fast ffs. Older Coldfire parts,
452 * and normal 68000 parts don't have anything special, so we use the
453 * generic functions for those.
455 #if (defined(__mcfisaaplus__) || defined(__mcfisac__)) && \
456 !defined(CONFIG_M68000) && !defined(CONFIG_MCPU32)
457 static inline unsigned long __ffs(unsigned long x
)
459 __asm__
__volatile__ ("bitrev %0; ff1 %0"
465 static inline int ffs(int x
)
473 #include <asm-generic/bitops/ffs.h>
474 #include <asm-generic/bitops/__ffs.h>
477 #include <asm-generic/bitops/fls.h>
478 #include <asm-generic/bitops/__fls.h>
483 * ffs: find first bit set. This is defined the same way as
484 * the libc and compiler builtin ffs routines, therefore
485 * differs in spirit from the above ffz (man ffs).
487 static inline int ffs(int x
)
491 __asm__ ("bfffo %1{#0:#0},%0"
497 static inline unsigned long __ffs(unsigned long x
)
503 * fls: find last bit set.
505 static inline int fls(unsigned int x
)
509 __asm__ ("bfffo %1{#0,#0},%0"
515 static inline int __fls(int x
)
522 /* Simple test-and-set bit locks */
523 #define test_and_set_bit_lock test_and_set_bit
524 #define clear_bit_unlock clear_bit
525 #define __clear_bit_unlock clear_bit_unlock
527 #include <asm-generic/bitops/ext2-atomic.h>
528 #include <asm-generic/bitops/le.h>
529 #include <asm-generic/bitops/fls64.h>
530 #include <asm-generic/bitops/sched.h>
531 #include <asm-generic/bitops/hweight.h>
532 #endif /* __KERNEL__ */
534 #endif /* _M68K_BITOPS_H */