* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / include / asm-m68k / bitops.h
blobe17fdf7f8dad3f7a5e8c529a4c1a5503474b17bd
1 #ifndef _M68K_BITOPS_H
2 #define _M68K_BITOPS_H
3 /*
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
8 * for more details.
9 */
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)
24 char retval;
26 __asm__ __volatile__ ("bset %1,%2; sne %0"
27 : "=d" (retval)
28 : "di" (nr & 7), "m" (((char *)vaddr)[(nr^31) >> 3]));
30 return retval;
33 extern __inline__ int __generic_test_and_set_bit(int nr,void * vaddr)
35 char retval;
37 __asm__ __volatile__ ("bfset %2@{%1:#1}; sne %0"
38 : "=d" (retval) : "d" (nr^31), "a" (vaddr));
40 return retval;
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)
67 char retval;
69 __asm__ __volatile__ ("bclr %1,%2; sne %0"
70 : "=d" (retval)
71 : "di" (nr & 7), "m" (((char *)vaddr)[(nr^31) >> 3]));
73 return retval;
76 extern __inline__ int __generic_test_and_clear_bit(int nr, void * vaddr)
78 char retval;
80 __asm__ __volatile__ ("bfclr %2@{%1:#1}; sne %0"
81 : "=d" (retval) : "d" (nr^31), "a" (vaddr));
83 return retval;
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)
110 char retval;
112 __asm__ __volatile__ ("bchg %1,%2; sne %0"
113 : "=d" (retval)
114 : "di" (nr & 7), "m" (((char *)vaddr)[(nr^31) >> 3]));
116 return retval;
119 extern __inline__ int __generic_test_and_change_bit(int nr, void * vaddr)
121 char retval;
123 __asm__ __volatile__ ("bfchg %2@{%1:#1}; sne %0"
124 : "=d" (retval) : "d" (nr^31), "a" (vaddr));
126 return retval;
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;
155 int res;
156 unsigned long num;
158 if (!size)
159 return 0;
161 size = (size >> 5) + ((size & 31) > 0);
162 while (*p++ == allones)
164 if (--size == 0)
165 return (p - addr) << 5;
168 num = ~*--p;
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,
175 int offset)
177 unsigned long *addr = vaddr;
178 unsigned long *p = addr + (offset >> 5);
179 int set = 0, bit = offset & 31UL, res;
181 if (offset >= size)
182 return size;
184 if (bit) {
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));
190 if (res < 32)
191 return (offset & ~31UL) + (res ^ 31);
192 set = 32 - bit;
193 p++;
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)
206 int res;
208 __asm__ __volatile__ ("bfffo %1{#0,#0},%0"
209 : "=d" (res) : "d" (~word & -~word));
210 return res ^ 31;
213 #ifdef __KERNEL__
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)
223 int cnt;
225 asm ("bfffo %1{#0:#0}" : "=d" (cnt) : "dm" (x & -x));
227 return 32 - cnt;
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;
245 int res;
246 unsigned short num;
248 if (!size)
249 return 0;
251 size = (size >> 4) + ((size & 15) > 0);
252 while (*p++ == 0xffff)
254 if (--size == 0)
255 return (p - addr) << 4;
258 num = ~*--p;
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)
267 char retval;
269 __asm__ __volatile__ ("bfset %2{%1:#1}; sne %0"
270 : "=d" (retval) : "d" (nr^15), "m" (*(char *)vaddr));
272 return retval;
275 extern __inline__ int
276 minix_clear_bit (int nr, void *vaddr)
278 char retval;
280 __asm__ __volatile__ ("bfclr %2{%1:#1}; sne %0"
281 : "=d" (retval) : "d" (nr^15), "m" (*(char *) vaddr));
283 return retval;
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)
297 char retval;
299 __asm__ __volatile__ ("bfset %2{%1,#1}; sne %0"
300 : "=d" (retval) : "d" (nr^7), "m" (*(char *) vaddr));
302 return retval;
305 extern __inline__ int
306 ext2_clear_bit (int nr, void *vaddr)
308 char retval;
310 __asm__ __volatile__ ("bfclr %2{%1,#1}; sne %0"
311 : "=d" (retval) : "d" (nr^7), "m" (*(char *) vaddr));
313 return retval;
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;
326 int res;
328 if (!size)
329 return 0;
331 size = (size >> 5) + ((size & 31) > 0);
332 while (*p++ == ~0UL)
334 if (--size == 0)
335 return (p - addr) << 5;
338 --p;
339 for (res = 0; res < 32; res++)
340 if (!ext2_test_bit (res, p))
341 break;
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;
352 if (offset >= size)
353 return size;
355 if (bit) {
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;
360 p++;
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 */