5 * include/asm-s390/bitops.h
8 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
9 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
11 * Derived from "include/asm-i386/bitops.h"
12 * Copyright (C) 1992, Linus Torvalds
18 #ifndef _LINUX_BITOPS_H
19 #error only <linux/bitops.h> can be included directly
22 #include <linux/compiler.h>
25 * 32 bit bitops format:
26 * bit 0 is the LSB of *addr; bit 31 is the MSB of *addr;
27 * bit 32 is the LSB of *(addr+4). That combined with the
28 * big endian byte order on S390 give the following bit
30 * 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 \
31 * 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
32 * after that follows the next long with bit numbers
33 * 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
34 * 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
35 * The reason for this bit ordering is the fact that
36 * in the architecture independent code bits operations
37 * of the form "flags |= (1 << bitnr)" are used INTERMIXED
38 * with operation of the form "set_bit(bitnr, flags)".
40 * 64 bit bitops format:
41 * bit 0 is the LSB of *addr; bit 63 is the MSB of *addr;
42 * bit 64 is the LSB of *(addr+8). That combined with the
43 * big endian byte order on S390 give the following bit
45 * 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
46 * 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
47 * 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10
48 * 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
49 * after that follows the next long with bit numbers
50 * 7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70
51 * 6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60
52 * 5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50
53 * 4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40
54 * The reason for this bit ordering is the fact that
55 * in the architecture independent code bits operations
56 * of the form "flags |= (1 << bitnr)" are used INTERMIXED
57 * with operation of the form "set_bit(bitnr, flags)".
60 /* bitmap tables from arch/S390/kernel/bitmap.S */
61 extern const char _oi_bitmap
[];
62 extern const char _ni_bitmap
[];
63 extern const char _zb_findmap
[];
64 extern const char _sb_findmap
[];
68 #define __BITOPS_ALIGN 3
69 #define __BITOPS_WORDSIZE 32
70 #define __BITOPS_OR "or"
71 #define __BITOPS_AND "nr"
72 #define __BITOPS_XOR "xr"
74 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
76 #define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string) \
80 __op_string " %1,%3\n" \
83 : "=&d" (__old), "=&d" (__new), \
84 "=Q" (*(unsigned long *) __addr) \
85 : "d" (__val), "Q" (*(unsigned long *) __addr) \
90 #define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string) \
94 __op_string " %1,%3\n" \
97 : "=&d" (__old), "=&d" (__new), \
98 "=m" (*(unsigned long *) __addr) \
99 : "d" (__val), "a" (__addr), \
100 "m" (*(unsigned long *) __addr) : "cc");
102 #endif /* __GNUC__ */
104 #else /* __s390x__ */
106 #define __BITOPS_ALIGN 7
107 #define __BITOPS_WORDSIZE 64
108 #define __BITOPS_OR "ogr"
109 #define __BITOPS_AND "ngr"
110 #define __BITOPS_XOR "xgr"
112 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
114 #define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string) \
118 __op_string " %1,%3\n" \
121 : "=&d" (__old), "=&d" (__new), \
122 "=Q" (*(unsigned long *) __addr) \
123 : "d" (__val), "Q" (*(unsigned long *) __addr) \
128 #define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string) \
132 __op_string " %1,%3\n" \
133 " csg %0,%1,0(%4)\n" \
135 : "=&d" (__old), "=&d" (__new), \
136 "=m" (*(unsigned long *) __addr) \
137 : "d" (__val), "a" (__addr), \
138 "m" (*(unsigned long *) __addr) : "cc");
141 #endif /* __GNUC__ */
143 #endif /* __s390x__ */
145 #define __BITOPS_WORDS(bits) (((bits)+__BITOPS_WORDSIZE-1)/__BITOPS_WORDSIZE)
146 #define __BITOPS_BARRIER() asm volatile("" : : : "memory")
150 * SMP safe set_bit routine based on compare and swap (CS)
152 static inline void set_bit_cs(unsigned long nr
, volatile unsigned long *ptr
)
154 unsigned long addr
, old
, new, mask
;
156 addr
= (unsigned long) ptr
;
157 /* calculate address for CS */
158 addr
+= (nr
^ (nr
& (__BITOPS_WORDSIZE
- 1))) >> 3;
160 mask
= 1UL << (nr
& (__BITOPS_WORDSIZE
- 1));
161 /* Do the atomic update. */
162 __BITOPS_LOOP(old
, new, addr
, mask
, __BITOPS_OR
);
166 * SMP safe clear_bit routine based on compare and swap (CS)
168 static inline void clear_bit_cs(unsigned long nr
, volatile unsigned long *ptr
)
170 unsigned long addr
, old
, new, mask
;
172 addr
= (unsigned long) ptr
;
173 /* calculate address for CS */
174 addr
+= (nr
^ (nr
& (__BITOPS_WORDSIZE
- 1))) >> 3;
176 mask
= ~(1UL << (nr
& (__BITOPS_WORDSIZE
- 1)));
177 /* Do the atomic update. */
178 __BITOPS_LOOP(old
, new, addr
, mask
, __BITOPS_AND
);
182 * SMP safe change_bit routine based on compare and swap (CS)
184 static inline void change_bit_cs(unsigned long nr
, volatile unsigned long *ptr
)
186 unsigned long addr
, old
, new, mask
;
188 addr
= (unsigned long) ptr
;
189 /* calculate address for CS */
190 addr
+= (nr
^ (nr
& (__BITOPS_WORDSIZE
- 1))) >> 3;
192 mask
= 1UL << (nr
& (__BITOPS_WORDSIZE
- 1));
193 /* Do the atomic update. */
194 __BITOPS_LOOP(old
, new, addr
, mask
, __BITOPS_XOR
);
198 * SMP safe test_and_set_bit routine based on compare and swap (CS)
201 test_and_set_bit_cs(unsigned long nr
, volatile unsigned long *ptr
)
203 unsigned long addr
, old
, new, mask
;
205 addr
= (unsigned long) ptr
;
206 /* calculate address for CS */
207 addr
+= (nr
^ (nr
& (__BITOPS_WORDSIZE
- 1))) >> 3;
208 /* make OR/test mask */
209 mask
= 1UL << (nr
& (__BITOPS_WORDSIZE
- 1));
210 /* Do the atomic update. */
211 __BITOPS_LOOP(old
, new, addr
, mask
, __BITOPS_OR
);
213 return (old
& mask
) != 0;
217 * SMP safe test_and_clear_bit routine based on compare and swap (CS)
220 test_and_clear_bit_cs(unsigned long nr
, volatile unsigned long *ptr
)
222 unsigned long addr
, old
, new, mask
;
224 addr
= (unsigned long) ptr
;
225 /* calculate address for CS */
226 addr
+= (nr
^ (nr
& (__BITOPS_WORDSIZE
- 1))) >> 3;
227 /* make AND/test mask */
228 mask
= ~(1UL << (nr
& (__BITOPS_WORDSIZE
- 1)));
229 /* Do the atomic update. */
230 __BITOPS_LOOP(old
, new, addr
, mask
, __BITOPS_AND
);
232 return (old
^ new) != 0;
236 * SMP safe test_and_change_bit routine based on compare and swap (CS)
239 test_and_change_bit_cs(unsigned long nr
, volatile unsigned long *ptr
)
241 unsigned long addr
, old
, new, mask
;
243 addr
= (unsigned long) ptr
;
244 /* calculate address for CS */
245 addr
+= (nr
^ (nr
& (__BITOPS_WORDSIZE
- 1))) >> 3;
246 /* make XOR/test mask */
247 mask
= 1UL << (nr
& (__BITOPS_WORDSIZE
- 1));
248 /* Do the atomic update. */
249 __BITOPS_LOOP(old
, new, addr
, mask
, __BITOPS_XOR
);
251 return (old
& mask
) != 0;
253 #endif /* CONFIG_SMP */
256 * fast, non-SMP set_bit routine
258 static inline void __set_bit(unsigned long nr
, volatile unsigned long *ptr
)
262 addr
= (unsigned long) ptr
+ ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
265 : "=m" (*(char *) addr
) : "a" (addr
),
266 "a" (_oi_bitmap
+ (nr
& 7)), "m" (*(char *) addr
) : "cc" );
270 __constant_set_bit(const unsigned long nr
, volatile unsigned long *ptr
)
274 addr
= ((unsigned long) ptr
) + ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
275 *(unsigned char *) addr
|= 1 << (nr
& 7);
278 #define set_bit_simple(nr,addr) \
279 (__builtin_constant_p((nr)) ? \
280 __constant_set_bit((nr),(addr)) : \
281 __set_bit((nr),(addr)) )
284 * fast, non-SMP clear_bit routine
287 __clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
291 addr
= (unsigned long) ptr
+ ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
294 : "=m" (*(char *) addr
) : "a" (addr
),
295 "a" (_ni_bitmap
+ (nr
& 7)), "m" (*(char *) addr
) : "cc");
299 __constant_clear_bit(const unsigned long nr
, volatile unsigned long *ptr
)
303 addr
= ((unsigned long) ptr
) + ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
304 *(unsigned char *) addr
&= ~(1 << (nr
& 7));
307 #define clear_bit_simple(nr,addr) \
308 (__builtin_constant_p((nr)) ? \
309 __constant_clear_bit((nr),(addr)) : \
310 __clear_bit((nr),(addr)) )
313 * fast, non-SMP change_bit routine
315 static inline void __change_bit(unsigned long nr
, volatile unsigned long *ptr
)
319 addr
= (unsigned long) ptr
+ ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
322 : "=m" (*(char *) addr
) : "a" (addr
),
323 "a" (_oi_bitmap
+ (nr
& 7)), "m" (*(char *) addr
) : "cc" );
327 __constant_change_bit(const unsigned long nr
, volatile unsigned long *ptr
)
331 addr
= ((unsigned long) ptr
) + ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
332 *(unsigned char *) addr
^= 1 << (nr
& 7);
335 #define change_bit_simple(nr,addr) \
336 (__builtin_constant_p((nr)) ? \
337 __constant_change_bit((nr),(addr)) : \
338 __change_bit((nr),(addr)) )
341 * fast, non-SMP test_and_set_bit routine
344 test_and_set_bit_simple(unsigned long nr
, volatile unsigned long *ptr
)
349 addr
= (unsigned long) ptr
+ ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
350 ch
= *(unsigned char *) addr
;
353 : "=m" (*(char *) addr
)
354 : "a" (addr
), "a" (_oi_bitmap
+ (nr
& 7)),
355 "m" (*(char *) addr
) : "cc", "memory");
356 return (ch
>> (nr
& 7)) & 1;
358 #define __test_and_set_bit(X,Y) test_and_set_bit_simple(X,Y)
361 * fast, non-SMP test_and_clear_bit routine
364 test_and_clear_bit_simple(unsigned long nr
, volatile unsigned long *ptr
)
369 addr
= (unsigned long) ptr
+ ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
370 ch
= *(unsigned char *) addr
;
373 : "=m" (*(char *) addr
)
374 : "a" (addr
), "a" (_ni_bitmap
+ (nr
& 7)),
375 "m" (*(char *) addr
) : "cc", "memory");
376 return (ch
>> (nr
& 7)) & 1;
378 #define __test_and_clear_bit(X,Y) test_and_clear_bit_simple(X,Y)
381 * fast, non-SMP test_and_change_bit routine
384 test_and_change_bit_simple(unsigned long nr
, volatile unsigned long *ptr
)
389 addr
= (unsigned long) ptr
+ ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
390 ch
= *(unsigned char *) addr
;
393 : "=m" (*(char *) addr
)
394 : "a" (addr
), "a" (_oi_bitmap
+ (nr
& 7)),
395 "m" (*(char *) addr
) : "cc", "memory");
396 return (ch
>> (nr
& 7)) & 1;
398 #define __test_and_change_bit(X,Y) test_and_change_bit_simple(X,Y)
401 #define set_bit set_bit_cs
402 #define clear_bit clear_bit_cs
403 #define change_bit change_bit_cs
404 #define test_and_set_bit test_and_set_bit_cs
405 #define test_and_clear_bit test_and_clear_bit_cs
406 #define test_and_change_bit test_and_change_bit_cs
408 #define set_bit set_bit_simple
409 #define clear_bit clear_bit_simple
410 #define change_bit change_bit_simple
411 #define test_and_set_bit test_and_set_bit_simple
412 #define test_and_clear_bit test_and_clear_bit_simple
413 #define test_and_change_bit test_and_change_bit_simple
418 * This routine doesn't need to be atomic.
421 static inline int __test_bit(unsigned long nr
, const volatile unsigned long *ptr
)
426 addr
= (unsigned long) ptr
+ ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
427 ch
= *(volatile unsigned char *) addr
;
428 return (ch
>> (nr
& 7)) & 1;
432 __constant_test_bit(unsigned long nr
, const volatile unsigned long *addr
) {
433 return (((volatile char *) addr
)
434 [(nr
^(__BITOPS_WORDSIZE
-8))>>3] & (1<<(nr
&7))) != 0;
437 #define test_bit(nr,addr) \
438 (__builtin_constant_p((nr)) ? \
439 __constant_test_bit((nr),(addr)) : \
440 __test_bit((nr),(addr)) )
443 * ffz = Find First Zero in word. Undefined if no zero exists,
444 * so code should check against ~0UL first..
446 static inline unsigned long ffz(unsigned long word
)
448 unsigned long bit
= 0;
451 if (likely((word
& 0xffffffff) == 0xffffffff)) {
456 if (likely((word
& 0xffff) == 0xffff)) {
460 if (likely((word
& 0xff) == 0xff)) {
464 return bit
+ _zb_findmap
[word
& 0xff];
468 * __ffs = find first bit in word. Undefined if no bit exists,
469 * so code should check against 0UL first..
471 static inline unsigned long __ffs (unsigned long word
)
473 unsigned long bit
= 0;
476 if (likely((word
& 0xffffffff) == 0)) {
481 if (likely((word
& 0xffff) == 0)) {
485 if (likely((word
& 0xff) == 0)) {
489 return bit
+ _sb_findmap
[word
& 0xff];
493 * Find-bit routines..
499 find_first_zero_bit(const unsigned long * addr
, unsigned long size
)
501 typedef struct { long _
[__BITOPS_WORDS(size
)]; } addrtype
;
502 unsigned long cmp
, count
;
534 : "=&a" (res
), "=&d" (cmp
), "=&a" (count
)
535 : "a" (size
), "a" (addr
), "a" (&_zb_findmap
),
536 "m" (*(addrtype
*) addr
) : "cc");
537 return (res
< size
) ? res
: size
;
541 find_first_bit(const unsigned long * addr
, unsigned long size
)
543 typedef struct { long _
[__BITOPS_WORDS(size
)]; } addrtype
;
544 unsigned long cmp
, count
;
576 : "=&a" (res
), "=&d" (cmp
), "=&a" (count
)
577 : "a" (size
), "a" (addr
), "a" (&_sb_findmap
),
578 "m" (*(addrtype
*) addr
) : "cc");
579 return (res
< size
) ? res
: size
;
582 #else /* __s390x__ */
584 static inline unsigned long
585 find_first_zero_bit(const unsigned long * addr
, unsigned long size
)
587 typedef struct { long _
[__BITOPS_WORDS(size
)]; } addrtype
;
588 unsigned long res
, cmp
, count
;
598 "0: cg %1,0(%0,%4)\n"
604 "1: lg %2,0(%0,%4)\n"
615 "3: tmll %2,0x00ff\n"
623 : "=&a" (res
), "=&d" (cmp
), "=&a" (count
)
624 : "a" (size
), "a" (addr
), "a" (&_zb_findmap
),
625 "m" (*(addrtype
*) addr
) : "cc");
626 return (res
< size
) ? res
: size
;
629 static inline unsigned long
630 find_first_bit(const unsigned long * addr
, unsigned long size
)
632 typedef struct { long _
[__BITOPS_WORDS(size
)]; } addrtype
;
633 unsigned long res
, cmp
, count
;
643 "0: cg %1,0(%0,%4)\n"
649 "1: lg %2,0(%0,%4)\n"
660 "3: tmll %2,0x00ff\n"
668 : "=&a" (res
), "=&d" (cmp
), "=&a" (count
)
669 : "a" (size
), "a" (addr
), "a" (&_sb_findmap
),
670 "m" (*(addrtype
*) addr
) : "cc");
671 return (res
< size
) ? res
: size
;
674 #endif /* __s390x__ */
677 find_next_zero_bit (const unsigned long * addr
, unsigned long size
,
678 unsigned long offset
)
680 const unsigned long *p
;
681 unsigned long bit
, set
;
685 bit
= offset
& (__BITOPS_WORDSIZE
- 1);
688 p
= addr
+ offset
/ __BITOPS_WORDSIZE
;
691 * s390 version of ffz returns __BITOPS_WORDSIZE
692 * if no zero bit is present in the word.
694 set
= ffz(*p
>> bit
) + bit
;
696 return size
+ offset
;
697 if (set
< __BITOPS_WORDSIZE
)
699 offset
+= __BITOPS_WORDSIZE
;
700 size
-= __BITOPS_WORDSIZE
;
703 return offset
+ find_first_zero_bit(p
, size
);
707 find_next_bit (const unsigned long * addr
, unsigned long size
,
708 unsigned long offset
)
710 const unsigned long *p
;
711 unsigned long bit
, set
;
715 bit
= offset
& (__BITOPS_WORDSIZE
- 1);
718 p
= addr
+ offset
/ __BITOPS_WORDSIZE
;
721 * s390 version of __ffs returns __BITOPS_WORDSIZE
722 * if no one bit is present in the word.
724 set
= __ffs(*p
& (~0UL << bit
));
726 return size
+ offset
;
727 if (set
< __BITOPS_WORDSIZE
)
729 offset
+= __BITOPS_WORDSIZE
;
730 size
-= __BITOPS_WORDSIZE
;
733 return offset
+ find_first_bit(p
, size
);
737 * Every architecture must define this function. It's the fastest
738 * way of searching a 140-bit bitmap where the first 100 bits are
739 * unlikely to be set. It's guaranteed that at least one of the 140
742 static inline int sched_find_first_bit(unsigned long *b
)
744 return find_first_bit(b
, 140);
747 #include <asm-generic/bitops/ffs.h>
749 #include <asm-generic/bitops/fls.h>
750 #include <asm-generic/bitops/fls64.h>
752 #include <asm-generic/bitops/hweight.h>
753 #include <asm-generic/bitops/lock.h>
756 * ATTENTION: intel byte ordering convention for ext2 and minix !!
757 * bit 0 is the LSB of addr; bit 31 is the MSB of addr;
758 * bit 32 is the LSB of (addr+4).
759 * That combined with the little endian byte order of Intel gives the
760 * following bit order in memory:
761 * 07 06 05 04 03 02 01 00 15 14 13 12 11 10 09 08 \
762 * 23 22 21 20 19 18 17 16 31 30 29 28 27 26 25 24
765 #define ext2_set_bit(nr, addr) \
766 __test_and_set_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
767 #define ext2_set_bit_atomic(lock, nr, addr) \
768 test_and_set_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
769 #define ext2_clear_bit(nr, addr) \
770 __test_and_clear_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
771 #define ext2_clear_bit_atomic(lock, nr, addr) \
772 test_and_clear_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
773 #define ext2_test_bit(nr, addr) \
774 test_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
775 #define ext2_find_next_bit(addr, size, off) \
776 generic_find_next_le_bit((unsigned long *)(addr), (size), (off))
781 ext2_find_first_zero_bit(void *vaddr
, unsigned int size
)
783 typedef struct { long _
[__BITOPS_WORDS(size
)]; } addrtype
;
784 unsigned long cmp
, count
;
795 "0: cl %1,0(%0,%4)\n"
817 : "=&a" (res
), "=&d" (cmp
), "=&a" (count
)
818 : "a" (size
), "a" (vaddr
), "a" (&_zb_findmap
),
819 "m" (*(addrtype
*) vaddr
) : "cc");
820 return (res
< size
) ? res
: size
;
823 #else /* __s390x__ */
825 static inline unsigned long
826 ext2_find_first_zero_bit(void *vaddr
, unsigned long size
)
828 typedef struct { long _
[__BITOPS_WORDS(size
)]; } addrtype
;
829 unsigned long res
, cmp
, count
;
839 "0: clg %1,0(%0,%4)\n"
845 "1: cl %1,0(%0,%4)\n"
856 "3: tmll %2,0xff00\n"
864 : "=&a" (res
), "=&d" (cmp
), "=&a" (count
)
865 : "a" (size
), "a" (vaddr
), "a" (&_zb_findmap
),
866 "m" (*(addrtype
*) vaddr
) : "cc");
867 return (res
< size
) ? res
: size
;
870 #endif /* __s390x__ */
873 ext2_find_next_zero_bit(void *vaddr
, unsigned long size
, unsigned long offset
)
875 unsigned long *addr
= vaddr
, *p
;
876 unsigned long word
, bit
, set
;
880 bit
= offset
& (__BITOPS_WORDSIZE
- 1);
883 p
= addr
+ offset
/ __BITOPS_WORDSIZE
;
891 : "=&a" (word
) : "a" (p
), "m" (*p
) : "cc");
895 : "=a" (word
) : "m" (*p
) );
898 * s390 version of ffz returns __BITOPS_WORDSIZE
899 * if no zero bit is present in the word.
901 set
= ffz(word
>> bit
) + bit
;
903 return size
+ offset
;
904 if (set
< __BITOPS_WORDSIZE
)
906 offset
+= __BITOPS_WORDSIZE
;
907 size
-= __BITOPS_WORDSIZE
;
910 return offset
+ ext2_find_first_zero_bit(p
, size
);
913 #include <asm-generic/bitops/minix.h>
915 #endif /* __KERNEL__ */
917 #endif /* _S390_BITOPS_H */