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
12 * Require 68020 or better.
14 * They use the standard big-endian m680x0 bit ordering.
17 #define test_and_set_bit(nr,vaddr) \
18 (__builtin_constant_p(nr) ? \
19 __constant_test_and_set_bit(nr, vaddr) : \
20 __generic_test_and_set_bit(nr, vaddr))
22 extern __inline__
int __constant_test_and_set_bit(int nr
,void * vaddr
)
26 __asm__
__volatile__ ("bset %1,%2; sne %0"
28 : "di" (nr
& 7), "m" (((char *)vaddr
)[(nr
^31) >> 3]));
33 extern __inline__
int __generic_test_and_set_bit(int nr
,void * vaddr
)
37 __asm__
__volatile__ ("bfset %2@{%1:#1}; sne %0"
38 : "=d" (retval
) : "d" (nr
^31), "a" (vaddr
));
43 #define set_bit(nr,vaddr) \
44 (__builtin_constant_p(nr) ? \
45 __constant_set_bit(nr, vaddr) : \
46 __generic_set_bit(nr, vaddr))
48 extern __inline__
void __constant_set_bit(int nr
, void * vaddr
)
50 __asm__
__volatile__ ("bset %0,%1"
51 : : "di" (nr
& 7), "m" (((char *)vaddr
)[(nr
^31) >> 3]));
54 extern __inline__
void __generic_set_bit(int nr
, void * vaddr
)
56 __asm__
__volatile__ ("bfset %1@{%0:#1}"
57 : : "d" (nr
^31), "a" (vaddr
));
60 #define test_and_clear_bit(nr,vaddr) \
61 (__builtin_constant_p(nr) ? \
62 __constant_test_and_clear_bit(nr, vaddr) : \
63 __generic_test_and_clear_bit(nr, vaddr))
65 extern __inline__
int __constant_test_and_clear_bit(int nr
, void * vaddr
)
69 __asm__
__volatile__ ("bclr %1,%2; sne %0"
71 : "di" (nr
& 7), "m" (((char *)vaddr
)[(nr
^31) >> 3]));
76 extern __inline__
int __generic_test_and_clear_bit(int nr
, void * vaddr
)
80 __asm__
__volatile__ ("bfclr %2@{%1:#1}; sne %0"
81 : "=d" (retval
) : "d" (nr
^31), "a" (vaddr
));
86 #define clear_bit(nr,vaddr) \
87 (__builtin_constant_p(nr) ? \
88 __constant_clear_bit(nr, vaddr) : \
89 __generic_clear_bit(nr, vaddr))
91 extern __inline__
void __constant_clear_bit(int nr
, void * vaddr
)
93 __asm__
__volatile__ ("bclr %0,%1"
94 : : "di" (nr
& 7), "m" (((char *)vaddr
)[(nr
^31) >> 3]));
97 extern __inline__
void __generic_clear_bit(int nr
, void * vaddr
)
99 __asm__
__volatile__ ("bfclr %1@{%0:#1}"
100 : : "d" (nr
^31), "a" (vaddr
));
103 #define test_and_change_bit(nr,vaddr) \
104 (__builtin_constant_p(nr) ? \
105 __constant_test_and_change_bit(nr, vaddr) : \
106 __generic_test_and_change_bit(nr, vaddr))
108 extern __inline__
int __constant_test_and_change_bit(int nr
, void * vaddr
)
112 __asm__
__volatile__ ("bchg %1,%2; sne %0"
114 : "di" (nr
& 7), "m" (((char *)vaddr
)[(nr
^31) >> 3]));
119 extern __inline__
int __generic_test_and_change_bit(int nr
, void * vaddr
)
123 __asm__
__volatile__ ("bfchg %2@{%1:#1}; sne %0"
124 : "=d" (retval
) : "d" (nr
^31), "a" (vaddr
));
129 #define change_bit(nr,vaddr) \
130 (__builtin_constant_p(nr) ? \
131 __constant_change_bit(nr, vaddr) : \
132 __generic_change_bit(nr, vaddr))
134 extern __inline__
void __constant_change_bit(int nr
, void * vaddr
)
136 __asm__
__volatile__ ("bchg %0,%1"
137 : : "di" (nr
& 7), "m" (((char *)vaddr
)[(nr
^31) >> 3]));
140 extern __inline__
void __generic_change_bit(int nr
, void * vaddr
)
142 __asm__
__volatile__ ("bfchg %1@{%0:#1}"
143 : : "d" (nr
^31), "a" (vaddr
));
146 extern __inline__
int test_bit(int nr
, const void * vaddr
)
148 return ((1UL << (nr
& 31)) & (((const unsigned int *) vaddr
)[nr
>> 5])) != 0;
151 extern __inline__
int find_first_zero_bit(void * vaddr
, unsigned size
)
153 unsigned long *p
= vaddr
, *addr
= vaddr
;
154 unsigned long allones
= ~0UL;
161 size
= (size
>> 5) + ((size
& 31) > 0);
162 while (*p
++ == allones
)
165 return (p
- addr
) << 5;
169 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
170 : "=d" (res
) : "d" (num
& -num
));
171 return ((p
- addr
) << 5) + (res
^ 31);
174 extern __inline__
int find_next_zero_bit (void *vaddr
, int size
,
177 unsigned long *addr
= vaddr
;
178 unsigned long *p
= addr
+ (offset
>> 5);
179 int set
= 0, bit
= offset
& 31UL, res
;
185 unsigned long num
= ~*p
& (~0UL << bit
);
187 /* Look for zero in first longword */
188 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
189 : "=d" (res
) : "d" (num
& -num
));
191 return (offset
& ~31UL) + (res
^ 31);
195 /* No zero yet, search remaining full bytes for a zero */
196 res
= find_first_zero_bit (p
, size
- 32 * (p
- addr
));
197 return (offset
+ set
+ res
);
201 * ffz = Find First Zero in word. Undefined if no zero exists,
202 * so code should check against ~0UL first..
204 extern __inline__
unsigned long ffz(unsigned long word
)
208 __asm__
__volatile__ ("bfffo %1{#0,#0},%0"
209 : "=d" (res
) : "d" (~word
& -~word
));
216 * ffs: find first bit set. This is defined the same way as
217 * the libc and compiler builtin ffs routines, therefore
218 * differs in spirit from the above ffz (man ffs).
221 extern __inline__
int ffs(int x
)
225 asm ("bfffo %1{#0:#0}" : "=d" (cnt
) : "dm" (x
& -x
));
231 * hweightN: returns the hamming weight (i.e. the number
232 * of bits set) of a N-bit word
235 #define hweight32(x) generic_hweight32(x)
236 #define hweight16(x) generic_hweight16(x)
237 #define hweight8(x) generic_hweight8(x)
239 /* Bitmap functions for the minix filesystem */
241 extern __inline__
int
242 minix_find_first_zero_bit (const void *vaddr
, unsigned size
)
244 const unsigned short *p
= vaddr
, *addr
= vaddr
;
251 size
= (size
>> 4) + ((size
& 15) > 0);
252 while (*p
++ == 0xffff)
255 return (p
- addr
) << 4;
259 __asm__
__volatile__ ("bfffo %1{#16,#16},%0"
260 : "=d" (res
) : "d" (num
& -num
));
261 return ((p
- addr
) << 4) + (res
^ 31);
264 extern __inline__
int
265 minix_set_bit (int nr
, void *vaddr
)
269 __asm__
__volatile__ ("bfset %2{%1:#1}; sne %0"
270 : "=d" (retval
) : "d" (nr
^15), "m" (*(char *)vaddr
));
275 extern __inline__
int
276 minix_clear_bit (int nr
, void *vaddr
)
280 __asm__
__volatile__ ("bfclr %2{%1:#1}; sne %0"
281 : "=d" (retval
) : "d" (nr
^15), "m" (*(char *) vaddr
));
286 extern __inline__
int
287 minix_test_bit (int nr
, const void *vaddr
)
289 return ((1U << (nr
& 15)) & (((const unsigned short *) vaddr
)[nr
>> 4])) != 0;
292 /* Bitmap functions for the ext2 filesystem. */
294 extern __inline__
int
295 ext2_set_bit (int nr
, void *vaddr
)
299 __asm__
__volatile__ ("bfset %2{%1,#1}; sne %0"
300 : "=d" (retval
) : "d" (nr
^7), "m" (*(char *) vaddr
));
305 extern __inline__
int
306 ext2_clear_bit (int nr
, void *vaddr
)
310 __asm__
__volatile__ ("bfclr %2{%1,#1}; sne %0"
311 : "=d" (retval
) : "d" (nr
^7), "m" (*(char *) vaddr
));
316 extern __inline__
int
317 ext2_test_bit (int nr
, const void *vaddr
)
319 return ((1U << (nr
& 7)) & (((const unsigned char *) vaddr
)[nr
>> 3])) != 0;
322 extern __inline__
int
323 ext2_find_first_zero_bit (const void *vaddr
, unsigned size
)
325 const unsigned long *p
= vaddr
, *addr
= vaddr
;
331 size
= (size
>> 5) + ((size
& 31) > 0);
335 return (p
- addr
) << 5;
339 for (res
= 0; res
< 32; res
++)
340 if (!ext2_test_bit (res
, p
))
342 return (p
- addr
) * 32 + res
;
345 extern __inline__
int
346 ext2_find_next_zero_bit (const void *vaddr
, unsigned size
, unsigned offset
)
348 const unsigned long *addr
= vaddr
;
349 const unsigned long *p
= addr
+ (offset
>> 5);
350 int bit
= offset
& 31UL, res
;
356 /* Look for zero in first longword */
357 for (res
= bit
; res
< 32; res
++)
358 if (!ext2_test_bit (res
, p
))
359 return (p
- addr
) * 32 + res
;
362 /* No zero yet, search remaining full bytes for a zero */
363 res
= ext2_find_first_zero_bit (p
, size
- 32 * (p
- addr
));
364 return (p
- addr
) * 32 + res
;
367 #endif /* __KERNEL__ */
369 #endif /* _M68K_BITOPS_H */