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 #include <linux/compiler.h>
21 * 32 bit bitops format:
22 * bit 0 is the LSB of *addr; bit 31 is the MSB of *addr;
23 * bit 32 is the LSB of *(addr+4). That combined with the
24 * big endian byte order on S390 give the following bit
26 * 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 \
27 * 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
28 * after that follows the next long with bit numbers
29 * 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
30 * 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
31 * The reason for this bit ordering is the fact that
32 * in the architecture independent code bits operations
33 * of the form "flags |= (1 << bitnr)" are used INTERMIXED
34 * with operation of the form "set_bit(bitnr, flags)".
36 * 64 bit bitops format:
37 * bit 0 is the LSB of *addr; bit 63 is the MSB of *addr;
38 * bit 64 is the LSB of *(addr+8). That combined with the
39 * big endian byte order on S390 give the following bit
41 * 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
42 * 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
43 * 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10
44 * 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
45 * after that follows the next long with bit numbers
46 * 7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70
47 * 6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60
48 * 5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50
49 * 4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40
50 * The reason for this bit ordering is the fact that
51 * in the architecture independent code bits operations
52 * of the form "flags |= (1 << bitnr)" are used INTERMIXED
53 * with operation of the form "set_bit(bitnr, flags)".
56 /* bitmap tables from arch/S390/kernel/bitmap.S */
57 extern const char _oi_bitmap
[];
58 extern const char _ni_bitmap
[];
59 extern const char _zb_findmap
[];
60 extern const char _sb_findmap
[];
64 #define __BITOPS_ALIGN 3
65 #define __BITOPS_WORDSIZE 32
66 #define __BITOPS_OR "or"
67 #define __BITOPS_AND "nr"
68 #define __BITOPS_XOR "xr"
70 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
72 #define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string) \
76 __op_string " %1,%3\n" \
79 : "=&d" (__old), "=&d" (__new), \
80 "=Q" (*(unsigned long *) __addr) \
81 : "d" (__val), "Q" (*(unsigned long *) __addr) \
86 #define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string) \
90 __op_string " %1,%3\n" \
93 : "=&d" (__old), "=&d" (__new), \
94 "=m" (*(unsigned long *) __addr) \
95 : "d" (__val), "a" (__addr), \
96 "m" (*(unsigned long *) __addr) : "cc");
100 #else /* __s390x__ */
102 #define __BITOPS_ALIGN 7
103 #define __BITOPS_WORDSIZE 64
104 #define __BITOPS_OR "ogr"
105 #define __BITOPS_AND "ngr"
106 #define __BITOPS_XOR "xgr"
108 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
110 #define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string) \
114 __op_string " %1,%3\n" \
117 : "=&d" (__old), "=&d" (__new), \
118 "=Q" (*(unsigned long *) __addr) \
119 : "d" (__val), "Q" (*(unsigned long *) __addr) \
124 #define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string) \
128 __op_string " %1,%3\n" \
129 " csg %0,%1,0(%4)\n" \
131 : "=&d" (__old), "=&d" (__new), \
132 "=m" (*(unsigned long *) __addr) \
133 : "d" (__val), "a" (__addr), \
134 "m" (*(unsigned long *) __addr) : "cc");
137 #endif /* __GNUC__ */
139 #endif /* __s390x__ */
141 #define __BITOPS_WORDS(bits) (((bits)+__BITOPS_WORDSIZE-1)/__BITOPS_WORDSIZE)
142 #define __BITOPS_BARRIER() asm volatile("" : : : "memory")
146 * SMP safe set_bit routine based on compare and swap (CS)
148 static inline void set_bit_cs(unsigned long nr
, volatile unsigned long *ptr
)
150 unsigned long addr
, old
, new, mask
;
152 addr
= (unsigned long) ptr
;
153 /* calculate address for CS */
154 addr
+= (nr
^ (nr
& (__BITOPS_WORDSIZE
- 1))) >> 3;
156 mask
= 1UL << (nr
& (__BITOPS_WORDSIZE
- 1));
157 /* Do the atomic update. */
158 __BITOPS_LOOP(old
, new, addr
, mask
, __BITOPS_OR
);
162 * SMP safe clear_bit routine based on compare and swap (CS)
164 static inline void clear_bit_cs(unsigned long nr
, volatile unsigned long *ptr
)
166 unsigned long addr
, old
, new, mask
;
168 addr
= (unsigned long) ptr
;
169 /* calculate address for CS */
170 addr
+= (nr
^ (nr
& (__BITOPS_WORDSIZE
- 1))) >> 3;
172 mask
= ~(1UL << (nr
& (__BITOPS_WORDSIZE
- 1)));
173 /* Do the atomic update. */
174 __BITOPS_LOOP(old
, new, addr
, mask
, __BITOPS_AND
);
178 * SMP safe change_bit routine based on compare and swap (CS)
180 static inline void change_bit_cs(unsigned long nr
, volatile unsigned long *ptr
)
182 unsigned long addr
, old
, new, mask
;
184 addr
= (unsigned long) ptr
;
185 /* calculate address for CS */
186 addr
+= (nr
^ (nr
& (__BITOPS_WORDSIZE
- 1))) >> 3;
188 mask
= 1UL << (nr
& (__BITOPS_WORDSIZE
- 1));
189 /* Do the atomic update. */
190 __BITOPS_LOOP(old
, new, addr
, mask
, __BITOPS_XOR
);
194 * SMP safe test_and_set_bit routine based on compare and swap (CS)
197 test_and_set_bit_cs(unsigned long nr
, volatile unsigned long *ptr
)
199 unsigned long addr
, old
, new, mask
;
201 addr
= (unsigned long) ptr
;
202 /* calculate address for CS */
203 addr
+= (nr
^ (nr
& (__BITOPS_WORDSIZE
- 1))) >> 3;
204 /* make OR/test mask */
205 mask
= 1UL << (nr
& (__BITOPS_WORDSIZE
- 1));
206 /* Do the atomic update. */
207 __BITOPS_LOOP(old
, new, addr
, mask
, __BITOPS_OR
);
209 return (old
& mask
) != 0;
213 * SMP safe test_and_clear_bit routine based on compare and swap (CS)
216 test_and_clear_bit_cs(unsigned long nr
, volatile unsigned long *ptr
)
218 unsigned long addr
, old
, new, mask
;
220 addr
= (unsigned long) ptr
;
221 /* calculate address for CS */
222 addr
+= (nr
^ (nr
& (__BITOPS_WORDSIZE
- 1))) >> 3;
223 /* make AND/test mask */
224 mask
= ~(1UL << (nr
& (__BITOPS_WORDSIZE
- 1)));
225 /* Do the atomic update. */
226 __BITOPS_LOOP(old
, new, addr
, mask
, __BITOPS_AND
);
228 return (old
^ new) != 0;
232 * SMP safe test_and_change_bit routine based on compare and swap (CS)
235 test_and_change_bit_cs(unsigned long nr
, volatile unsigned long *ptr
)
237 unsigned long addr
, old
, new, mask
;
239 addr
= (unsigned long) ptr
;
240 /* calculate address for CS */
241 addr
+= (nr
^ (nr
& (__BITOPS_WORDSIZE
- 1))) >> 3;
242 /* make XOR/test mask */
243 mask
= 1UL << (nr
& (__BITOPS_WORDSIZE
- 1));
244 /* Do the atomic update. */
245 __BITOPS_LOOP(old
, new, addr
, mask
, __BITOPS_XOR
);
247 return (old
& mask
) != 0;
249 #endif /* CONFIG_SMP */
252 * fast, non-SMP set_bit routine
254 static inline void __set_bit(unsigned long nr
, volatile unsigned long *ptr
)
258 addr
= (unsigned long) ptr
+ ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
261 : "=m" (*(char *) addr
) : "a" (addr
),
262 "a" (_oi_bitmap
+ (nr
& 7)), "m" (*(char *) addr
) : "cc" );
266 __constant_set_bit(const unsigned long nr
, volatile unsigned long *ptr
)
270 addr
= ((unsigned long) ptr
) + ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
271 *(unsigned char *) addr
|= 1 << (nr
& 7);
274 #define set_bit_simple(nr,addr) \
275 (__builtin_constant_p((nr)) ? \
276 __constant_set_bit((nr),(addr)) : \
277 __set_bit((nr),(addr)) )
280 * fast, non-SMP clear_bit routine
283 __clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
287 addr
= (unsigned long) ptr
+ ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
290 : "=m" (*(char *) addr
) : "a" (addr
),
291 "a" (_ni_bitmap
+ (nr
& 7)), "m" (*(char *) addr
) : "cc");
295 __constant_clear_bit(const unsigned long nr
, volatile unsigned long *ptr
)
299 addr
= ((unsigned long) ptr
) + ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
300 *(unsigned char *) addr
&= ~(1 << (nr
& 7));
303 #define clear_bit_simple(nr,addr) \
304 (__builtin_constant_p((nr)) ? \
305 __constant_clear_bit((nr),(addr)) : \
306 __clear_bit((nr),(addr)) )
309 * fast, non-SMP change_bit routine
311 static inline void __change_bit(unsigned long nr
, volatile unsigned long *ptr
)
315 addr
= (unsigned long) ptr
+ ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
318 : "=m" (*(char *) addr
) : "a" (addr
),
319 "a" (_oi_bitmap
+ (nr
& 7)), "m" (*(char *) addr
) : "cc" );
323 __constant_change_bit(const unsigned long nr
, volatile unsigned long *ptr
)
327 addr
= ((unsigned long) ptr
) + ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
328 *(unsigned char *) addr
^= 1 << (nr
& 7);
331 #define change_bit_simple(nr,addr) \
332 (__builtin_constant_p((nr)) ? \
333 __constant_change_bit((nr),(addr)) : \
334 __change_bit((nr),(addr)) )
337 * fast, non-SMP test_and_set_bit routine
340 test_and_set_bit_simple(unsigned long nr
, volatile unsigned long *ptr
)
345 addr
= (unsigned long) ptr
+ ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
346 ch
= *(unsigned char *) addr
;
349 : "=m" (*(char *) addr
)
350 : "a" (addr
), "a" (_oi_bitmap
+ (nr
& 7)),
351 "m" (*(char *) addr
) : "cc", "memory");
352 return (ch
>> (nr
& 7)) & 1;
354 #define __test_and_set_bit(X,Y) test_and_set_bit_simple(X,Y)
357 * fast, non-SMP test_and_clear_bit routine
360 test_and_clear_bit_simple(unsigned long nr
, volatile unsigned long *ptr
)
365 addr
= (unsigned long) ptr
+ ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
366 ch
= *(unsigned char *) addr
;
369 : "=m" (*(char *) addr
)
370 : "a" (addr
), "a" (_ni_bitmap
+ (nr
& 7)),
371 "m" (*(char *) addr
) : "cc", "memory");
372 return (ch
>> (nr
& 7)) & 1;
374 #define __test_and_clear_bit(X,Y) test_and_clear_bit_simple(X,Y)
377 * fast, non-SMP test_and_change_bit routine
380 test_and_change_bit_simple(unsigned long nr
, volatile unsigned long *ptr
)
385 addr
= (unsigned long) ptr
+ ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
386 ch
= *(unsigned char *) addr
;
389 : "=m" (*(char *) addr
)
390 : "a" (addr
), "a" (_oi_bitmap
+ (nr
& 7)),
391 "m" (*(char *) addr
) : "cc", "memory");
392 return (ch
>> (nr
& 7)) & 1;
394 #define __test_and_change_bit(X,Y) test_and_change_bit_simple(X,Y)
397 #define set_bit set_bit_cs
398 #define clear_bit clear_bit_cs
399 #define change_bit change_bit_cs
400 #define test_and_set_bit test_and_set_bit_cs
401 #define test_and_clear_bit test_and_clear_bit_cs
402 #define test_and_change_bit test_and_change_bit_cs
404 #define set_bit set_bit_simple
405 #define clear_bit clear_bit_simple
406 #define change_bit change_bit_simple
407 #define test_and_set_bit test_and_set_bit_simple
408 #define test_and_clear_bit test_and_clear_bit_simple
409 #define test_and_change_bit test_and_change_bit_simple
414 * This routine doesn't need to be atomic.
417 static inline int __test_bit(unsigned long nr
, const volatile unsigned long *ptr
)
422 addr
= (unsigned long) ptr
+ ((nr
^ (__BITOPS_WORDSIZE
- 8)) >> 3);
423 ch
= *(volatile unsigned char *) addr
;
424 return (ch
>> (nr
& 7)) & 1;
428 __constant_test_bit(unsigned long nr
, const volatile unsigned long *addr
) {
429 return (((volatile char *) addr
)
430 [(nr
^(__BITOPS_WORDSIZE
-8))>>3] & (1<<(nr
&7))) != 0;
433 #define test_bit(nr,addr) \
434 (__builtin_constant_p((nr)) ? \
435 __constant_test_bit((nr),(addr)) : \
436 __test_bit((nr),(addr)) )
439 * ffz = Find First Zero in word. Undefined if no zero exists,
440 * so code should check against ~0UL first..
442 static inline unsigned long ffz(unsigned long word
)
444 unsigned long bit
= 0;
447 if (likely((word
& 0xffffffff) == 0xffffffff)) {
452 if (likely((word
& 0xffff) == 0xffff)) {
456 if (likely((word
& 0xff) == 0xff)) {
460 return bit
+ _zb_findmap
[word
& 0xff];
464 * __ffs = find first bit in word. Undefined if no bit exists,
465 * so code should check against 0UL first..
467 static inline unsigned long __ffs (unsigned long word
)
469 unsigned long bit
= 0;
472 if (likely((word
& 0xffffffff) == 0)) {
477 if (likely((word
& 0xffff) == 0)) {
481 if (likely((word
& 0xff) == 0)) {
485 return bit
+ _sb_findmap
[word
& 0xff];
489 * Find-bit routines..
495 find_first_zero_bit(const unsigned long * addr
, unsigned long size
)
497 typedef struct { long _
[__BITOPS_WORDS(size
)]; } addrtype
;
498 unsigned long cmp
, count
;
530 : "=&a" (res
), "=&d" (cmp
), "=&a" (count
)
531 : "a" (size
), "a" (addr
), "a" (&_zb_findmap
),
532 "m" (*(addrtype
*) addr
) : "cc");
533 return (res
< size
) ? res
: size
;
537 find_first_bit(const unsigned long * addr
, unsigned long size
)
539 typedef struct { long _
[__BITOPS_WORDS(size
)]; } addrtype
;
540 unsigned long cmp
, count
;
572 : "=&a" (res
), "=&d" (cmp
), "=&a" (count
)
573 : "a" (size
), "a" (addr
), "a" (&_sb_findmap
),
574 "m" (*(addrtype
*) addr
) : "cc");
575 return (res
< size
) ? res
: size
;
578 #else /* __s390x__ */
580 static inline unsigned long
581 find_first_zero_bit(const unsigned long * addr
, unsigned long size
)
583 typedef struct { long _
[__BITOPS_WORDS(size
)]; } addrtype
;
584 unsigned long res
, cmp
, count
;
594 "0: cg %1,0(%0,%4)\n"
600 "1: lg %2,0(%0,%4)\n"
611 "3: tmll %2,0x00ff\n"
619 : "=&a" (res
), "=&d" (cmp
), "=&a" (count
)
620 : "a" (size
), "a" (addr
), "a" (&_zb_findmap
),
621 "m" (*(addrtype
*) addr
) : "cc");
622 return (res
< size
) ? res
: size
;
625 static inline unsigned long
626 find_first_bit(const unsigned long * addr
, unsigned long size
)
628 typedef struct { long _
[__BITOPS_WORDS(size
)]; } addrtype
;
629 unsigned long res
, cmp
, count
;
639 "0: cg %1,0(%0,%4)\n"
645 "1: lg %2,0(%0,%4)\n"
656 "3: tmll %2,0x00ff\n"
664 : "=&a" (res
), "=&d" (cmp
), "=&a" (count
)
665 : "a" (size
), "a" (addr
), "a" (&_sb_findmap
),
666 "m" (*(addrtype
*) addr
) : "cc");
667 return (res
< size
) ? res
: size
;
670 #endif /* __s390x__ */
673 find_next_zero_bit (const unsigned long * addr
, unsigned long size
,
674 unsigned long offset
)
676 const unsigned long *p
;
677 unsigned long bit
, set
;
681 bit
= offset
& (__BITOPS_WORDSIZE
- 1);
684 p
= addr
+ offset
/ __BITOPS_WORDSIZE
;
687 * s390 version of ffz returns __BITOPS_WORDSIZE
688 * if no zero bit is present in the word.
690 set
= ffz(*p
>> bit
) + bit
;
692 return size
+ offset
;
693 if (set
< __BITOPS_WORDSIZE
)
695 offset
+= __BITOPS_WORDSIZE
;
696 size
-= __BITOPS_WORDSIZE
;
699 return offset
+ find_first_zero_bit(p
, size
);
703 find_next_bit (const unsigned long * addr
, unsigned long size
,
704 unsigned long offset
)
706 const unsigned long *p
;
707 unsigned long bit
, set
;
711 bit
= offset
& (__BITOPS_WORDSIZE
- 1);
714 p
= addr
+ offset
/ __BITOPS_WORDSIZE
;
717 * s390 version of __ffs returns __BITOPS_WORDSIZE
718 * if no one bit is present in the word.
720 set
= __ffs(*p
& (~0UL << bit
));
722 return size
+ offset
;
723 if (set
< __BITOPS_WORDSIZE
)
725 offset
+= __BITOPS_WORDSIZE
;
726 size
-= __BITOPS_WORDSIZE
;
729 return offset
+ find_first_bit(p
, size
);
733 * Every architecture must define this function. It's the fastest
734 * way of searching a 140-bit bitmap where the first 100 bits are
735 * unlikely to be set. It's guaranteed that at least one of the 140
738 static inline int sched_find_first_bit(unsigned long *b
)
740 return find_first_bit(b
, 140);
743 #include <asm-generic/bitops/ffs.h>
745 #include <asm-generic/bitops/fls.h>
746 #include <asm-generic/bitops/fls64.h>
748 #include <asm-generic/bitops/hweight.h>
751 * ATTENTION: intel byte ordering convention for ext2 and minix !!
752 * bit 0 is the LSB of addr; bit 31 is the MSB of addr;
753 * bit 32 is the LSB of (addr+4).
754 * That combined with the little endian byte order of Intel gives the
755 * following bit order in memory:
756 * 07 06 05 04 03 02 01 00 15 14 13 12 11 10 09 08 \
757 * 23 22 21 20 19 18 17 16 31 30 29 28 27 26 25 24
760 #define ext2_set_bit(nr, addr) \
761 __test_and_set_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
762 #define ext2_set_bit_atomic(lock, nr, addr) \
763 test_and_set_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
764 #define ext2_clear_bit(nr, addr) \
765 __test_and_clear_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
766 #define ext2_clear_bit_atomic(lock, nr, addr) \
767 test_and_clear_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
768 #define ext2_test_bit(nr, addr) \
769 test_bit((nr)^(__BITOPS_WORDSIZE - 8), (unsigned long *)addr)
774 ext2_find_first_zero_bit(void *vaddr
, unsigned int size
)
776 typedef struct { long _
[__BITOPS_WORDS(size
)]; } addrtype
;
777 unsigned long cmp
, count
;
788 "0: cl %1,0(%0,%4)\n"
810 : "=&a" (res
), "=&d" (cmp
), "=&a" (count
)
811 : "a" (size
), "a" (vaddr
), "a" (&_zb_findmap
),
812 "m" (*(addrtype
*) vaddr
) : "cc");
813 return (res
< size
) ? res
: size
;
816 #else /* __s390x__ */
818 static inline unsigned long
819 ext2_find_first_zero_bit(void *vaddr
, unsigned long size
)
821 typedef struct { long _
[__BITOPS_WORDS(size
)]; } addrtype
;
822 unsigned long res
, cmp
, count
;
832 "0: clg %1,0(%0,%4)\n"
838 "1: cl %1,0(%0,%4)\n"
849 "3: tmll %2,0xff00\n"
857 : "=&a" (res
), "=&d" (cmp
), "=&a" (count
)
858 : "a" (size
), "a" (vaddr
), "a" (&_zb_findmap
),
859 "m" (*(addrtype
*) vaddr
) : "cc");
860 return (res
< size
) ? res
: size
;
863 #endif /* __s390x__ */
866 ext2_find_next_zero_bit(void *vaddr
, unsigned long size
, unsigned long offset
)
868 unsigned long *addr
= vaddr
, *p
;
869 unsigned long word
, bit
, set
;
873 bit
= offset
& (__BITOPS_WORDSIZE
- 1);
876 p
= addr
+ offset
/ __BITOPS_WORDSIZE
;
884 : "=&a" (word
) : "a" (p
), "m" (*p
) : "cc");
888 : "=a" (word
) : "m" (*p
) );
891 * s390 version of ffz returns __BITOPS_WORDSIZE
892 * if no zero bit is present in the word.
894 set
= ffz(word
>> bit
) + bit
;
896 return size
+ offset
;
897 if (set
< __BITOPS_WORDSIZE
)
899 offset
+= __BITOPS_WORDSIZE
;
900 size
-= __BITOPS_WORDSIZE
;
903 return offset
+ ext2_find_first_zero_bit(p
, size
);
906 #include <asm-generic/bitops/minix.h>
908 #endif /* __KERNEL__ */
910 #endif /* _S390_BITOPS_H */