hwmon: (jc42) Change detection class
[linux/fpc-iii.git] / arch / m68k / include / asm / bitops_mm.h
blobe9020f88a748f4ae5905015eaa300e793a447732
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 */
11 #ifndef _LINUX_BITOPS_H
12 #error only <linux/bitops.h> can be included directly
13 #endif
15 #include <linux/compiler.h>
18 * Require 68020 or better.
20 * They use the standard big-endian m680x0 bit ordering.
23 #define test_and_set_bit(nr,vaddr) \
24 (__builtin_constant_p(nr) ? \
25 __constant_test_and_set_bit(nr, vaddr) : \
26 __generic_test_and_set_bit(nr, vaddr))
28 #define __test_and_set_bit(nr,vaddr) test_and_set_bit(nr,vaddr)
30 static inline int __constant_test_and_set_bit(int nr, unsigned long *vaddr)
32 char *p = (char *)vaddr + (nr ^ 31) / 8;
33 char retval;
35 __asm__ __volatile__ ("bset %2,%1; sne %0"
36 : "=d" (retval), "+m" (*p)
37 : "di" (nr & 7));
39 return retval;
42 static inline int __generic_test_and_set_bit(int nr, unsigned long *vaddr)
44 char retval;
46 __asm__ __volatile__ ("bfset %2{%1:#1}; sne %0"
47 : "=d" (retval) : "d" (nr^31), "o" (*vaddr) : "memory");
49 return retval;
52 #define set_bit(nr,vaddr) \
53 (__builtin_constant_p(nr) ? \
54 __constant_set_bit(nr, vaddr) : \
55 __generic_set_bit(nr, vaddr))
57 #define __set_bit(nr,vaddr) set_bit(nr,vaddr)
59 static inline void __constant_set_bit(int nr, volatile unsigned long *vaddr)
61 char *p = (char *)vaddr + (nr ^ 31) / 8;
62 __asm__ __volatile__ ("bset %1,%0"
63 : "+m" (*p) : "di" (nr & 7));
66 static inline void __generic_set_bit(int nr, volatile unsigned long *vaddr)
68 __asm__ __volatile__ ("bfset %1{%0:#1}"
69 : : "d" (nr^31), "o" (*vaddr) : "memory");
72 #define test_and_clear_bit(nr,vaddr) \
73 (__builtin_constant_p(nr) ? \
74 __constant_test_and_clear_bit(nr, vaddr) : \
75 __generic_test_and_clear_bit(nr, vaddr))
77 #define __test_and_clear_bit(nr,vaddr) test_and_clear_bit(nr,vaddr)
79 static inline int __constant_test_and_clear_bit(int nr, unsigned long *vaddr)
81 char *p = (char *)vaddr + (nr ^ 31) / 8;
82 char retval;
84 __asm__ __volatile__ ("bclr %2,%1; sne %0"
85 : "=d" (retval), "+m" (*p)
86 : "di" (nr & 7));
88 return retval;
91 static inline int __generic_test_and_clear_bit(int nr, unsigned long *vaddr)
93 char retval;
95 __asm__ __volatile__ ("bfclr %2{%1:#1}; sne %0"
96 : "=d" (retval) : "d" (nr^31), "o" (*vaddr) : "memory");
98 return retval;
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_clear_bit(nr, vaddr) : \
110 __generic_clear_bit(nr, vaddr))
111 #define __clear_bit(nr,vaddr) clear_bit(nr,vaddr)
113 static inline void __constant_clear_bit(int nr, volatile unsigned long *vaddr)
115 char *p = (char *)vaddr + (nr ^ 31) / 8;
116 __asm__ __volatile__ ("bclr %1,%0"
117 : "+m" (*p) : "di" (nr & 7));
120 static inline void __generic_clear_bit(int nr, volatile unsigned long *vaddr)
122 __asm__ __volatile__ ("bfclr %1{%0:#1}"
123 : : "d" (nr^31), "o" (*vaddr) : "memory");
126 #define test_and_change_bit(nr,vaddr) \
127 (__builtin_constant_p(nr) ? \
128 __constant_test_and_change_bit(nr, vaddr) : \
129 __generic_test_and_change_bit(nr, vaddr))
131 #define __test_and_change_bit(nr,vaddr) test_and_change_bit(nr,vaddr)
132 #define __change_bit(nr,vaddr) change_bit(nr,vaddr)
134 static inline int __constant_test_and_change_bit(int nr, unsigned long *vaddr)
136 char *p = (char *)vaddr + (nr ^ 31) / 8;
137 char retval;
139 __asm__ __volatile__ ("bchg %2,%1; sne %0"
140 : "=d" (retval), "+m" (*p)
141 : "di" (nr & 7));
143 return retval;
146 static inline int __generic_test_and_change_bit(int nr, unsigned long *vaddr)
148 char retval;
150 __asm__ __volatile__ ("bfchg %2{%1:#1}; sne %0"
151 : "=d" (retval) : "d" (nr^31), "o" (*vaddr) : "memory");
153 return retval;
156 #define change_bit(nr,vaddr) \
157 (__builtin_constant_p(nr) ? \
158 __constant_change_bit(nr, vaddr) : \
159 __generic_change_bit(nr, vaddr))
161 static inline void __constant_change_bit(int nr, unsigned long *vaddr)
163 char *p = (char *)vaddr + (nr ^ 31) / 8;
164 __asm__ __volatile__ ("bchg %1,%0"
165 : "+m" (*p) : "di" (nr & 7));
168 static inline void __generic_change_bit(int nr, unsigned long *vaddr)
170 __asm__ __volatile__ ("bfchg %1{%0:#1}"
171 : : "d" (nr^31), "o" (*vaddr) : "memory");
174 static inline int test_bit(int nr, const unsigned long *vaddr)
176 return (vaddr[nr >> 5] & (1UL << (nr & 31))) != 0;
179 static inline int find_first_zero_bit(const unsigned long *vaddr,
180 unsigned size)
182 const unsigned long *p = vaddr;
183 int res = 32;
184 unsigned int words;
185 unsigned long num;
187 if (!size)
188 return 0;
190 words = (size + 31) >> 5;
191 while (!(num = ~*p++)) {
192 if (!--words)
193 goto out;
196 __asm__ __volatile__ ("bfffo %1{#0,#0},%0"
197 : "=d" (res) : "d" (num & -num));
198 res ^= 31;
199 out:
200 res += ((long)p - (long)vaddr - 4) * 8;
201 return res < size ? res : size;
204 static inline int find_next_zero_bit(const unsigned long *vaddr, int size,
205 int offset)
207 const unsigned long *p = vaddr + (offset >> 5);
208 int bit = offset & 31UL, res;
210 if (offset >= size)
211 return size;
213 if (bit) {
214 unsigned long num = ~*p++ & (~0UL << bit);
215 offset -= bit;
217 /* Look for zero in first longword */
218 __asm__ __volatile__ ("bfffo %1{#0,#0},%0"
219 : "=d" (res) : "d" (num & -num));
220 if (res < 32) {
221 offset += res ^ 31;
222 return offset < size ? offset : size;
224 offset += 32;
226 if (offset >= size)
227 return size;
229 /* No zero yet, search remaining full bytes for a zero */
230 return offset + find_first_zero_bit(p, size - offset);
233 static inline int find_first_bit(const unsigned long *vaddr, unsigned size)
235 const unsigned long *p = vaddr;
236 int res = 32;
237 unsigned int words;
238 unsigned long num;
240 if (!size)
241 return 0;
243 words = (size + 31) >> 5;
244 while (!(num = *p++)) {
245 if (!--words)
246 goto out;
249 __asm__ __volatile__ ("bfffo %1{#0,#0},%0"
250 : "=d" (res) : "d" (num & -num));
251 res ^= 31;
252 out:
253 res += ((long)p - (long)vaddr - 4) * 8;
254 return res < size ? res : size;
257 static inline int find_next_bit(const unsigned long *vaddr, int size,
258 int offset)
260 const unsigned long *p = vaddr + (offset >> 5);
261 int bit = offset & 31UL, res;
263 if (offset >= size)
264 return size;
266 if (bit) {
267 unsigned long num = *p++ & (~0UL << bit);
268 offset -= bit;
270 /* Look for one in first longword */
271 __asm__ __volatile__ ("bfffo %1{#0,#0},%0"
272 : "=d" (res) : "d" (num & -num));
273 if (res < 32) {
274 offset += res ^ 31;
275 return offset < size ? offset : size;
277 offset += 32;
279 if (offset >= size)
280 return size;
282 /* No one yet, search remaining full bytes for a one */
283 return offset + find_first_bit(p, size - offset);
287 * ffz = Find First Zero in word. Undefined if no zero exists,
288 * so code should check against ~0UL first..
290 static inline unsigned long ffz(unsigned long word)
292 int res;
294 __asm__ __volatile__ ("bfffo %1{#0,#0},%0"
295 : "=d" (res) : "d" (~word & -~word));
296 return res ^ 31;
299 #ifdef __KERNEL__
302 * ffs: find first bit set. This is defined the same way as
303 * the libc and compiler builtin ffs routines, therefore
304 * differs in spirit from the above ffz (man ffs).
307 static inline int ffs(int x)
309 int cnt;
311 asm ("bfffo %1{#0:#0},%0" : "=d" (cnt) : "dm" (x & -x));
313 return 32 - cnt;
315 #define __ffs(x) (ffs(x) - 1)
318 * fls: find last bit set.
321 static inline int fls(int x)
323 int cnt;
325 asm ("bfffo %1{#0,#0},%0" : "=d" (cnt) : "dm" (x));
327 return 32 - cnt;
330 static inline int __fls(int x)
332 return fls(x) - 1;
335 #include <asm-generic/bitops/fls64.h>
336 #include <asm-generic/bitops/sched.h>
337 #include <asm-generic/bitops/hweight.h>
338 #include <asm-generic/bitops/lock.h>
340 /* Bitmap functions for the little endian bitmap. */
342 static inline void __set_bit_le(int nr, void *addr)
344 __set_bit(nr ^ 24, addr);
347 static inline void __clear_bit_le(int nr, void *addr)
349 __clear_bit(nr ^ 24, addr);
352 static inline int __test_and_set_bit_le(int nr, void *addr)
354 return __test_and_set_bit(nr ^ 24, addr);
357 static inline int test_and_set_bit_le(int nr, void *addr)
359 return test_and_set_bit(nr ^ 24, addr);
362 static inline int __test_and_clear_bit_le(int nr, void *addr)
364 return __test_and_clear_bit(nr ^ 24, addr);
367 static inline int test_and_clear_bit_le(int nr, void *addr)
369 return test_and_clear_bit(nr ^ 24, addr);
372 static inline int test_bit_le(int nr, const void *vaddr)
374 const unsigned char *p = vaddr;
375 return (p[nr >> 3] & (1U << (nr & 7))) != 0;
378 static inline int find_first_zero_bit_le(const void *vaddr, unsigned size)
380 const unsigned long *p = vaddr, *addr = vaddr;
381 int res = 0;
382 unsigned int words;
384 if (!size)
385 return 0;
387 words = (size >> 5) + ((size & 31) > 0);
388 while (*p++ == ~0UL) {
389 if (--words == 0)
390 goto out;
393 --p;
394 for (res = 0; res < 32; res++)
395 if (!test_bit_le(res, p))
396 break;
397 out:
398 res += (p - addr) * 32;
399 return res < size ? res : size;
402 static inline unsigned long find_next_zero_bit_le(const void *addr,
403 unsigned long size, unsigned long offset)
405 const unsigned long *p = addr;
406 int bit = offset & 31UL, res;
408 if (offset >= size)
409 return size;
411 p += offset >> 5;
413 if (bit) {
414 offset -= bit;
415 /* Look for zero in first longword */
416 for (res = bit; res < 32; res++)
417 if (!test_bit_le(res, p)) {
418 offset += res;
419 return offset < size ? offset : size;
421 p++;
422 offset += 32;
424 if (offset >= size)
425 return size;
427 /* No zero yet, search remaining full bytes for a zero */
428 return offset + find_first_zero_bit_le(p, size - offset);
431 static inline int find_first_bit_le(const void *vaddr, unsigned size)
433 const unsigned long *p = vaddr, *addr = vaddr;
434 int res = 0;
435 unsigned int words;
437 if (!size)
438 return 0;
440 words = (size >> 5) + ((size & 31) > 0);
441 while (*p++ == 0UL) {
442 if (--words == 0)
443 goto out;
446 --p;
447 for (res = 0; res < 32; res++)
448 if (test_bit_le(res, p))
449 break;
450 out:
451 res += (p - addr) * 32;
452 return res < size ? res : size;
455 static inline unsigned long find_next_bit_le(const void *addr,
456 unsigned long size, unsigned long offset)
458 const unsigned long *p = addr;
459 int bit = offset & 31UL, res;
461 if (offset >= size)
462 return size;
464 p += offset >> 5;
466 if (bit) {
467 offset -= bit;
468 /* Look for one in first longword */
469 for (res = bit; res < 32; res++)
470 if (test_bit_le(res, p)) {
471 offset += res;
472 return offset < size ? offset : size;
474 p++;
475 offset += 32;
477 if (offset >= size)
478 return size;
480 /* No set bit yet, search remaining full bytes for a set bit */
481 return offset + find_first_bit_le(p, size - offset);
484 /* Bitmap functions for the ext2 filesystem. */
486 #define ext2_set_bit_atomic(lock, nr, addr) \
487 test_and_set_bit_le(nr, addr)
488 #define ext2_clear_bit_atomic(lock, nr, addr) \
489 test_and_clear_bit_le(nr, addr)
491 #endif /* __KERNEL__ */
493 #endif /* _M68K_BITOPS_H */