2 * Copyright 2007-2009 Freescale Semiconductor, Inc. All Rights Reserved.
4 * This file is subject to the terms and conditions of the GNU General Public
5 * 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>
17 #define test_and_set_bit(nr,vaddr) \
18 (__builtin_constant_p(nr) ? \
19 __constant_coldfire_test_and_set_bit(nr, vaddr) : \
20 __generic_coldfire_test_and_set_bit(nr, vaddr))
22 static __inline__
int __constant_coldfire_test_and_set_bit(int nr
,
26 volatile char *p
= &((volatile char *)vaddr
)[(nr
^31) >> 3];
27 __asm__
__volatile__ ("bset %2,(%4); sne %0"
28 : "=d" (retval
), "=m" (*p
)
29 : "di" (nr
& 7), "m" (*p
), "a" (p
));
33 static __inline__
int __generic_coldfire_test_and_set_bit(int nr
,
38 __asm__
__volatile__ ("bset %2,%1; sne %0"
39 : "=d" (retval
), "=m" (((volatile char *)vaddr
)[(nr
^31) >> 3])
44 #define __test_and_set_bit(nr, vaddr) test_and_set_bit(nr, vaddr)
46 #define set_bit(nr,vaddr) \
47 (__builtin_constant_p(nr) ? \
48 __constant_coldfire_set_bit(nr, vaddr) : \
49 __generic_coldfire_set_bit(nr, vaddr))
51 static __inline__
void __constant_coldfire_set_bit(int nr
,
54 volatile char *p
= &((volatile char *)vaddr
)[(nr
^31) >> 3];
55 __asm__
__volatile__ ("bset %1,(%3)"
56 : "=m" (*p
) : "di" (nr
& 7), "m" (*p
), "a" (p
));
59 static __inline__
void __generic_coldfire_set_bit(int nr
,
62 __asm__
__volatile__ ("bset %1,%0"
63 : "=m" (((volatile char *)vaddr
)[(nr
^31) >> 3])
67 #define __set_bit(nr, vaddr) set_bit(nr, vaddr)
69 #define test_and_clear_bit(nr, vaddr) \
70 (__builtin_constant_p(nr) ? \
71 __constant_coldfire_test_and_clear_bit(nr, vaddr) : \
72 __generic_coldfire_test_and_clear_bit(nr, vaddr))
74 static __inline__
int __constant_coldfire_test_and_clear_bit(int nr
,
78 volatile char *p
= &((volatile char *)vaddr
)[(nr
^31) >> 3];
80 __asm__
__volatile__ ("bclr %2,(%4); sne %0"
81 : "=d" (retval
), "=m" (*p
)
82 : "id" (nr
& 7), "m" (*p
), "a" (p
));
87 static __inline__
int __generic_coldfire_test_and_clear_bit(int nr
,
92 __asm__
__volatile__ ("bclr %2,%1; sne %0"
93 : "=d" (retval
), "=m" (((volatile char *)vaddr
)[(nr
^31) >> 3])
99 #define __test_and_clear_bit(nr, vaddr) test_and_clear_bit(nr, vaddr)
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_coldfire_clear_bit(nr, vaddr) : \
110 __generic_coldfire_clear_bit(nr, vaddr))
112 static __inline__
void __constant_coldfire_clear_bit(int nr
,
113 volatile void *vaddr
)
115 volatile char *p
= &((volatile char *)vaddr
)[(nr
^31) >> 3];
116 __asm__
__volatile__ ("bclr %1,(%3)"
117 : "=m" (*p
) : "id" (nr
& 7), "m" (*p
), "a" (p
));
120 static __inline__
void __generic_coldfire_clear_bit(int nr
,
121 volatile void *vaddr
)
123 __asm__
__volatile__ ("bclr %1,%0"
124 : "=m" (((volatile char *)vaddr
)[(nr
^31) >> 3])
128 #define __clear_bit(nr, vaddr) clear_bit(nr, vaddr)
130 #define test_and_change_bit(nr, vaddr) \
131 (__builtin_constant_p(nr) ? \
132 __constant_coldfire_test_and_change_bit(nr, vaddr) : \
133 __generic_coldfire_test_and_change_bit(nr, vaddr))
135 static __inline__
int __constant_coldfire_test_and_change_bit(int nr
,
136 volatile void *vaddr
)
139 volatile char *p
= &((volatile char *)vaddr
)[(nr
^31) >> 3];
141 __asm__
__volatile__ ("bchg %2,(%4); sne %0"
142 : "=d" (retval
), "=m" (*p
)
143 : "id" (nr
& 7), "m" (*p
), "a" (p
));
148 static __inline__
int __generic_coldfire_test_and_change_bit(int nr
,
149 volatile void *vaddr
)
153 __asm__
__volatile__ ("bchg %2,%1; sne %0"
154 : "=d" (retval
), "=m" (((volatile char *)vaddr
)[(nr
^31) >> 3])
160 #define __test_and_change_bit(nr, vaddr) test_and_change_bit(nr, vaddr)
161 #define __change_bit(nr, vaddr) change_bit(nr, vaddr)
163 #define change_bit(nr,vaddr) \
164 (__builtin_constant_p(nr) ? \
165 __constant_coldfire_change_bit(nr, vaddr) : \
166 __generic_coldfire_change_bit(nr, vaddr))
168 static __inline__
void __constant_coldfire_change_bit(int nr
,
169 volatile void *vaddr
)
171 volatile char *p
= &((volatile char *)vaddr
)[(nr
^31) >> 3];
172 __asm__
__volatile__ ("bchg %1,(%3)"
173 : "=m" (*p
) : "id" (nr
& 7), "m" (*p
), "a" (p
));
176 static __inline__
void __generic_coldfire_change_bit(int nr
,
177 volatile void *vaddr
)
179 __asm__
__volatile__ ("bchg %1,%0"
180 : "=m" (((volatile char *)vaddr
)[(nr
^31) >> 3])
185 static inline int test_bit(int nr
, const unsigned long *vaddr
)
187 return (vaddr
[nr
>> 5] & (1UL << (nr
& 31))) != 0;
190 static __inline__
unsigned long ffz(unsigned long word
)
192 unsigned long result
= 0;
201 /* find_next_zero_bit() finds the first zero bit in a bit string of length
202 * 'size' bits, starting the search at bit 'offset'. This is largely based
203 * on Linus's ALPHA routines.
205 static __inline__
unsigned long find_next_zero_bit(void *addr
,
206 unsigned long size
, unsigned long offset
)
208 unsigned long *p
= ((unsigned long *) addr
) + (offset
>> 5);
209 unsigned long result
= offset
& ~31UL;
218 tmp
|= ~0UL >> (32-offset
);
226 while (size
& ~31UL) {
240 return result
+ ffz(tmp
);
243 #define find_first_zero_bit(addr, size) find_next_zero_bit(((void *)addr), \
246 /* Ported from included/linux/bitops.h */
247 static __inline__
int ffs(int x
)
275 #define __ffs(x) (ffs(x) - 1)
277 /* find_next_bit - find the next set bit in a memory region
278 * (from asm-ppc/bitops.h)
280 static __inline__
unsigned long find_next_bit(const unsigned long *addr
,
281 unsigned long size
, unsigned long offset
)
283 unsigned int *p
= ((unsigned int *) addr
) + (offset
>> 5);
284 unsigned int result
= offset
& ~31UL;
293 tmp
&= ~0UL << offset
;
313 tmp
&= ~0UL >> (32 - size
);
314 if (tmp
== 0UL) /* Are any bits set? */
315 return result
+ size
; /* Nope. */
317 return result
+ __ffs(tmp
);
320 #define find_first_bit(addr, size) find_next_bit((addr), (size), 0)
324 /* Ported from include/linux/bitops.h */
325 static __inline__
int fls(int x
)
331 if (!(x
& 0xffff0000u
)) {
335 if (!(x
& 0xff000000u
)) {
339 if (!(x
& 0xf0000000u
)) {
343 if (!(x
& 0xc0000000u
)) {
347 if (!(x
& 0x80000000u
)) {
354 static inline int __fls(int x
)
359 #include <asm-generic/bitops/fls64.h>
360 #include <asm-generic/bitops/sched.h>
361 #include <asm-generic/bitops/hweight.h>
362 #include <asm-generic/bitops/lock.h>
364 #define minix_find_first_zero_bit(addr, size) find_next_zero_bit((addr), \
366 #define minix_test_and_set_bit(nr, addr) test_and_set_bit((nr), \
367 (unsigned long *)(addr))
368 #define minix_set_bit(nr, addr) set_bit((nr), \
369 (unsigned long *)(addr))
370 #define minix_test_and_clear_bit(nr, addr) test_and_clear_bit((nr), \
371 (unsigned long *)(addr))
373 static inline int minix_test_bit(int nr
, const volatile unsigned long *vaddr
)
375 int *a
= (int *)vaddr
;
379 mask
= 1 << (nr
& 0x1f);
380 return ((mask
& *a
) != 0);
383 #define ext2_set_bit(nr, addr) test_and_set_bit((nr) ^ 24, \
384 (unsigned long *)(addr))
385 #define ext2_set_bit_atomic(lock, nr, addr) test_and_set_bit((nr) ^ 24, \
386 (unsigned long *)(addr))
387 #define ext2_clear_bit(nr, addr) test_and_clear_bit((nr) ^ 24, \
388 (unsigned long *)(addr))
389 #define ext2_clear_bit_atomic(lock, nr, addr) test_and_clear_bit((nr) ^ 24, \
390 (unsigned long *)(addr))
392 static inline int ext2_test_bit(int nr
, const void *vaddr
)
394 const unsigned char *p
= vaddr
;
395 return (p
[nr
>> 3] & (1U << (nr
& 7))) != 0;
398 static inline int ext2_find_first_zero_bit(const void *vaddr
, unsigned size
)
400 const unsigned long *p
= vaddr
, *addr
= vaddr
;
406 size
= (size
>> 5) + ((size
& 31) > 0);
407 while (*p
++ == ~0UL) {
409 return (p
- addr
) << 5;
413 for (res
= 0; res
< 32; res
++)
414 if (!ext2_test_bit (res
, p
))
416 return (p
- addr
) * 32 + res
;
419 static inline int ext2_find_next_zero_bit(const void *vaddr
, unsigned size
,
422 const unsigned long *addr
= vaddr
;
423 const unsigned long *p
= addr
+ (offset
>> 5);
424 int bit
= offset
& 31UL, res
;
430 /* Look for zero in first longword */
431 for (res
= bit
; res
< 32; res
++)
432 if (!ext2_test_bit (res
, p
))
433 return (p
- addr
) * 32 + res
;
436 /* No zero yet, search remaining full bytes for a zero */
437 res
= ext2_find_first_zero_bit(p
, size
- 32 * (p
- addr
));
438 return (p
- addr
) * 32 + res
;
443 #endif /* __CF_BITOPS__ */
This page took 0.076894 seconds and 5 git commands to generate.