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
16 #ifndef _LINUX_BITOPS_H
17 #error only <linux/bitops.h> can be included directly
20 #include <linux/compiler.h>
23 * 32 bit bitops format:
24 * bit 0 is the LSB of *addr; bit 31 is the MSB of *addr;
25 * bit 32 is the LSB of *(addr+4). That combined with the
26 * big endian byte order on S390 give the following bit
28 * 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 \
29 * 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
30 * after that follows the next long with bit numbers
31 * 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
32 * 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
33 * The reason for this bit ordering is the fact that
34 * in the architecture independent code bits operations
35 * of the form "flags |= (1 << bitnr)" are used INTERMIXED
36 * with operation of the form "set_bit(bitnr, flags)".
38 * 64 bit bitops format:
39 * bit 0 is the LSB of *addr; bit 63 is the MSB of *addr;
40 * bit 64 is the LSB of *(addr+8). That combined with the
41 * big endian byte order on S390 give the following bit
43 * 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
44 * 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
45 * 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10
46 * 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
47 * after that follows the next long with bit numbers
48 * 7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70
49 * 6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60
50 * 5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50
51 * 4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40
52 * The reason for this bit ordering is the fact that
53 * in the architecture independent code bits operations
54 * of the form "flags |= (1 << bitnr)" are used INTERMIXED
55 * with operation of the form "set_bit(bitnr, flags)".
58 /* bitmap tables from arch/s390/kernel/bitmap.c */
59 extern const char _oi_bitmap[];
60 extern const char _ni_bitmap[];
61 extern const char _zb_findmap[];
62 extern const char _sb_findmap[];
66 #define __BITOPS_ALIGN 3
67 #define __BITOPS_WORDSIZE 32
68 #define __BITOPS_OR "or"
69 #define __BITOPS_AND "nr"
70 #define __BITOPS_XOR "xr"
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) \
84 #else /* CONFIG_64BIT */
86 #define __BITOPS_ALIGN 7
87 #define __BITOPS_WORDSIZE 64
88 #define __BITOPS_OR "ogr"
89 #define __BITOPS_AND "ngr"
90 #define __BITOPS_XOR "xgr"
92 #define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string) \
96 __op_string " %1,%3\n" \
99 : "=&d" (__old), "=&d" (__new), \
100 "=Q" (*(unsigned long *) __addr) \
101 : "d" (__val), "Q" (*(unsigned long *) __addr) \
104 #endif /* CONFIG_64BIT */
106 #define __BITOPS_WORDS(bits) (((bits)+__BITOPS_WORDSIZE-1)/__BITOPS_WORDSIZE)
107 #define __BITOPS_BARRIER() asm volatile("" : : : "memory")
111 * SMP safe set_bit routine based on compare and swap (CS)
113 static inline void set_bit_cs(unsigned long nr, volatile unsigned long *ptr)
115 unsigned long addr, old, new, mask;
117 addr = (unsigned long) ptr;
118 /* calculate address for CS */
119 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
121 mask = 1UL << (nr & (__BITOPS_WORDSIZE - 1));
122 /* Do the atomic update. */
123 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_OR);
127 * SMP safe clear_bit routine based on compare and swap (CS)
129 static inline void clear_bit_cs(unsigned long nr, volatile unsigned long *ptr)
131 unsigned long addr, old, new, mask;
133 addr = (unsigned long) ptr;
134 /* calculate address for CS */
135 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
137 mask = ~(1UL << (nr & (__BITOPS_WORDSIZE - 1)));
138 /* Do the atomic update. */
139 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_AND);
143 * SMP safe change_bit routine based on compare and swap (CS)
145 static inline void change_bit_cs(unsigned long nr, volatile unsigned long *ptr)
147 unsigned long addr, old, new, mask;
149 addr = (unsigned long) ptr;
150 /* calculate address for CS */
151 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
153 mask = 1UL << (nr & (__BITOPS_WORDSIZE - 1));
154 /* Do the atomic update. */
155 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_XOR);
159 * SMP safe test_and_set_bit routine based on compare and swap (CS)
162 test_and_set_bit_cs(unsigned long nr, volatile unsigned long *ptr)
164 unsigned long addr, old, new, mask;
166 addr = (unsigned long) ptr;
167 /* calculate address for CS */
168 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
169 /* make OR/test mask */
170 mask = 1UL << (nr & (__BITOPS_WORDSIZE - 1));
171 /* Do the atomic update. */
172 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_OR);
174 return (old & mask) != 0;
178 * SMP safe test_and_clear_bit routine based on compare and swap (CS)
181 test_and_clear_bit_cs(unsigned long nr, volatile unsigned long *ptr)
183 unsigned long addr, old, new, mask;
185 addr = (unsigned long) ptr;
186 /* calculate address for CS */
187 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
188 /* make AND/test mask */
189 mask = ~(1UL << (nr & (__BITOPS_WORDSIZE - 1)));
190 /* Do the atomic update. */
191 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_AND);
193 return (old ^ new) != 0;
197 * SMP safe test_and_change_bit routine based on compare and swap (CS)
200 test_and_change_bit_cs(unsigned long nr, volatile unsigned long *ptr)
202 unsigned long addr, old, new, mask;
204 addr = (unsigned long) ptr;
205 /* calculate address for CS */
206 addr += (nr ^ (nr & (__BITOPS_WORDSIZE - 1))) >> 3;
207 /* make XOR/test mask */
208 mask = 1UL << (nr & (__BITOPS_WORDSIZE - 1));
209 /* Do the atomic update. */
210 __BITOPS_LOOP(old, new, addr, mask, __BITOPS_XOR);
212 return (old & mask) != 0;
214 #endif /* CONFIG_SMP */
217 * fast, non-SMP set_bit routine
219 static inline void __set_bit(unsigned long nr, volatile unsigned long *ptr)
223 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
226 : "=Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7]) : "cc" );
230 __constant_set_bit(const unsigned long nr, volatile unsigned long *ptr)
234 addr = ((unsigned long) ptr) + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
235 *(unsigned char *) addr |= 1 << (nr & 7);
238 #define set_bit_simple(nr,addr) \
239 (__builtin_constant_p((nr)) ? \
240 __constant_set_bit((nr),(addr)) : \
241 __set_bit((nr),(addr)) )
244 * fast, non-SMP clear_bit routine
247 __clear_bit(unsigned long nr, volatile unsigned long *ptr)
251 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
254 : "=Q" (*(char *) addr) : "Q" (_ni_bitmap[nr & 7]) : "cc" );
258 __constant_clear_bit(const unsigned long nr, volatile unsigned long *ptr)
262 addr = ((unsigned long) ptr) + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
263 *(unsigned char *) addr &= ~(1 << (nr & 7));
266 #define clear_bit_simple(nr,addr) \
267 (__builtin_constant_p((nr)) ? \
268 __constant_clear_bit((nr),(addr)) : \
269 __clear_bit((nr),(addr)) )
272 * fast, non-SMP change_bit routine
274 static inline void __change_bit(unsigned long nr, volatile unsigned long *ptr)
278 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
281 : "=Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7]) : "cc" );
285 __constant_change_bit(const unsigned long nr, volatile unsigned long *ptr)
289 addr = ((unsigned long) ptr) + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
290 *(unsigned char *) addr ^= 1 << (nr & 7);
293 #define change_bit_simple(nr,addr) \
294 (__builtin_constant_p((nr)) ? \
295 __constant_change_bit((nr),(addr)) : \
296 __change_bit((nr),(addr)) )
299 * fast, non-SMP test_and_set_bit routine
302 test_and_set_bit_simple(unsigned long nr, volatile unsigned long *ptr)
307 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
308 ch = *(unsigned char *) addr;
311 : "=Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7])
313 return (ch >> (nr & 7)) & 1;
315 #define __test_and_set_bit(X,Y) test_and_set_bit_simple(X,Y)
318 * fast, non-SMP test_and_clear_bit routine
321 test_and_clear_bit_simple(unsigned long nr, volatile unsigned long *ptr)
326 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
327 ch = *(unsigned char *) addr;
330 : "=Q" (*(char *) addr) : "Q" (_ni_bitmap[nr & 7])
332 return (ch >> (nr & 7)) & 1;
334 #define __test_and_clear_bit(X,Y) test_and_clear_bit_simple(X,Y)
337 * fast, non-SMP test_and_change_bit routine
340 test_and_change_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 : "=Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7])
351 return (ch >> (nr & 7)) & 1;
353 #define __test_and_change_bit(X,Y) test_and_change_bit_simple(X,Y)
356 #define set_bit set_bit_cs
357 #define clear_bit clear_bit_cs
358 #define change_bit change_bit_cs
359 #define test_and_set_bit test_and_set_bit_cs
360 #define test_and_clear_bit test_and_clear_bit_cs
361 #define test_and_change_bit test_and_change_bit_cs
363 #define set_bit set_bit_simple
364 #define clear_bit clear_bit_simple
365 #define change_bit change_bit_simple
366 #define test_and_set_bit test_and_set_bit_simple
367 #define test_and_clear_bit test_and_clear_bit_simple
368 #define test_and_change_bit test_and_change_bit_simple
373 * This routine doesn't need to be atomic.
376 static inline int __test_bit(unsigned long nr, const volatile unsigned long *ptr)
381 addr = (unsigned long) ptr + ((nr ^ (__BITOPS_WORDSIZE - 8)) >> 3);
382 ch = *(volatile unsigned char *) addr;
383 return (ch >> (nr & 7)) & 1;
387 __constant_test_bit(unsigned long nr, const volatile unsigned long *addr) {
388 return (((volatile char *) addr)
389 [(nr^(__BITOPS_WORDSIZE-8))>>3] & (1<<(nr&7))) != 0;
392 #define test_bit(nr,addr) \
393 (__builtin_constant_p((nr)) ? \
394 __constant_test_bit((nr),(addr)) : \
395 __test_bit((nr),(addr)) )
398 * Optimized find bit helper functions.
402 * __ffz_word_loop - find byte offset of first long != -1UL
403 * @addr: pointer to array of unsigned long
404 * @size: size of the array in bits
406 static inline unsigned long __ffz_word_loop(const unsigned long *addr,
409 typedef struct { long _[__BITOPS_WORDS(size)]; } addrtype;
410 unsigned long bytes = 0;
426 "0: cg %2,0(%0,%3)\n"
432 : "+&a" (bytes), "+&d" (size)
433 : "d" (-1UL), "a" (addr), "m" (*(addrtype *) addr)
439 * __ffs_word_loop - find byte offset of first long != 0UL
440 * @addr: pointer to array of unsigned long
441 * @size: size of the array in bits
443 static inline unsigned long __ffs_word_loop(const unsigned long *addr,
446 typedef struct { long _[__BITOPS_WORDS(size)]; } addrtype;
447 unsigned long bytes = 0;
463 "0: cg %2,0(%0,%3)\n"
469 : "+&a" (bytes), "+&a" (size)
470 : "d" (0UL), "a" (addr), "m" (*(addrtype *) addr)
476 * __ffz_word - add number of the first unset bit
477 * @nr: base value the bit number is added to
478 * @word: the word that is searched for unset bits
480 static inline unsigned long __ffz_word(unsigned long nr, unsigned long word)
483 if ((word & 0xffffffff) == 0xffffffff) {
488 if ((word & 0xffff) == 0xffff) {
492 if ((word & 0xff) == 0xff) {
496 return nr + _zb_findmap[(unsigned char) word];
500 * __ffs_word - add number of the first set bit
501 * @nr: base value the bit number is added to
502 * @word: the word that is searched for set bits
504 static inline unsigned long __ffs_word(unsigned long nr, unsigned long word)
507 if ((word & 0xffffffff) == 0) {
512 if ((word & 0xffff) == 0) {
516 if ((word & 0xff) == 0) {
520 return nr + _sb_findmap[(unsigned char) word];
525 * __load_ulong_be - load big endian unsigned long
526 * @p: pointer to array of unsigned long
527 * @offset: byte offset of source value in the array
529 static inline unsigned long __load_ulong_be(const unsigned long *p,
530 unsigned long offset)
532 p = (unsigned long *)((unsigned long) p + offset);
537 * __load_ulong_le - load little endian unsigned long
538 * @p: pointer to array of unsigned long
539 * @offset: byte offset of source value in the array
541 static inline unsigned long __load_ulong_le(const unsigned long *p,
542 unsigned long offset)
546 p = (unsigned long *)((unsigned long) p + offset);
550 " icm %0,2,%O1+1(%R1)\n"
551 " icm %0,4,%O1+2(%R1)\n"
552 " icm %0,8,%O1+3(%R1)"
553 : "=&d" (word) : "Q" (*p) : "cc");
557 : "=d" (word) : "m" (*p) );
563 * The various find bit functions.
567 * ffz - find first zero in word.
568 * @word: The word to search
570 * Undefined if no zero exists, so code should check against ~0UL first.
572 static inline unsigned long ffz(unsigned long word)
574 return __ffz_word(0, word);
578 * __ffs - find first bit in word.
579 * @word: The word to search
581 * Undefined if no bit exists, so code should check against 0 first.
583 static inline unsigned long __ffs (unsigned long word)
585 return __ffs_word(0, word);
589 * ffs - find first bit set
590 * @x: the word to search
592 * This is defined the same way as
593 * the libc and compiler builtin ffs routines, therefore
594 * differs in spirit from the above ffz (man ffs).
596 static inline int ffs(int x)
600 return __ffs_word(1, x);
604 * find_first_zero_bit - find the first zero bit in a memory region
605 * @addr: The address to start the search at
606 * @size: The maximum size to search
608 * Returns the bit-number of the first zero bit, not the number of the byte
611 static inline unsigned long find_first_zero_bit(const unsigned long *addr,
614 unsigned long bytes, bits;
618 bytes = __ffz_word_loop(addr, size);
619 bits = __ffz_word(bytes*8, __load_ulong_be(addr, bytes));
620 return (bits < size) ? bits : size;
622 #define find_first_zero_bit find_first_zero_bit
625 * find_first_bit - find the first set bit in a memory region
626 * @addr: The address to start the search at
627 * @size: The maximum size to search
629 * Returns the bit-number of the first set bit, not the number of the byte
632 static inline unsigned long find_first_bit(const unsigned long * addr,
635 unsigned long bytes, bits;
639 bytes = __ffs_word_loop(addr, size);
640 bits = __ffs_word(bytes*8, __load_ulong_be(addr, bytes));
641 return (bits < size) ? bits : size;
643 #define find_first_bit find_first_bit
646 * find_next_zero_bit - find the first zero bit in a memory region
647 * @addr: The address to base the search on
648 * @offset: The bitnumber to start searching at
649 * @size: The maximum size to search
651 static inline int find_next_zero_bit (const unsigned long * addr,
653 unsigned long offset)
655 const unsigned long *p;
656 unsigned long bit, set;
660 bit = offset & (__BITOPS_WORDSIZE - 1);
663 p = addr + offset / __BITOPS_WORDSIZE;
666 * __ffz_word returns __BITOPS_WORDSIZE
667 * if no zero bit is present in the word.
669 set = __ffz_word(bit, *p >> bit);
671 return size + offset;
672 if (set < __BITOPS_WORDSIZE)
674 offset += __BITOPS_WORDSIZE;
675 size -= __BITOPS_WORDSIZE;
678 return offset + find_first_zero_bit(p, size);
680 #define find_next_zero_bit find_next_zero_bit
683 * find_next_bit - find the first set bit in a memory region
684 * @addr: The address to base the search on
685 * @offset: The bitnumber to start searching at
686 * @size: The maximum size to search
688 static inline int find_next_bit (const unsigned long * addr,
690 unsigned long offset)
692 const unsigned long *p;
693 unsigned long bit, set;
697 bit = offset & (__BITOPS_WORDSIZE - 1);
700 p = addr + offset / __BITOPS_WORDSIZE;
703 * __ffs_word returns __BITOPS_WORDSIZE
704 * if no one bit is present in the word.
706 set = __ffs_word(0, *p & (~0UL << bit));
708 return size + offset;
709 if (set < __BITOPS_WORDSIZE)
711 offset += __BITOPS_WORDSIZE;
712 size -= __BITOPS_WORDSIZE;
715 return offset + find_first_bit(p, size);
717 #define find_next_bit find_next_bit
720 * Every architecture must define this function. It's the fastest
721 * way of searching a 140-bit bitmap where the first 100 bits are
722 * unlikely to be set. It's guaranteed that at least one of the 140
725 static inline int sched_find_first_bit(unsigned long *b)
727 return find_first_bit(b, 140);
730 #include <asm-generic/bitops/fls.h>
731 #include <asm-generic/bitops/__fls.h>
732 #include <asm-generic/bitops/fls64.h>
734 #include <asm-generic/bitops/hweight.h>
735 #include <asm-generic/bitops/lock.h>
738 * ATTENTION: intel byte ordering convention for ext2 and minix !!
739 * bit 0 is the LSB of addr; bit 31 is the MSB of addr;
740 * bit 32 is the LSB of (addr+4).
741 * That combined with the little endian byte order of Intel gives the
742 * following bit order in memory:
743 * 07 06 05 04 03 02 01 00 15 14 13 12 11 10 09 08 \
744 * 23 22 21 20 19 18 17 16 31 30 29 28 27 26 25 24
747 static inline int find_first_zero_bit_le(void *vaddr, unsigned int size)
749 unsigned long bytes, bits;
753 bytes = __ffz_word_loop(vaddr, size);
754 bits = __ffz_word(bytes*8, __load_ulong_le(vaddr, bytes));
755 return (bits < size) ? bits : size;
757 #define find_first_zero_bit_le find_first_zero_bit_le
759 static inline int find_next_zero_bit_le(void *vaddr, unsigned long size,
760 unsigned long offset)
762 unsigned long *addr = vaddr, *p;
763 unsigned long bit, set;
767 bit = offset & (__BITOPS_WORDSIZE - 1);
770 p = addr + offset / __BITOPS_WORDSIZE;
773 * s390 version of ffz returns __BITOPS_WORDSIZE
774 * if no zero bit is present in the word.
776 set = __ffz_word(bit, __load_ulong_le(p, 0) >> bit);
778 return size + offset;
779 if (set < __BITOPS_WORDSIZE)
781 offset += __BITOPS_WORDSIZE;
782 size -= __BITOPS_WORDSIZE;
785 return offset + find_first_zero_bit_le(p, size);
787 #define find_next_zero_bit_le find_next_zero_bit_le
789 static inline unsigned long find_first_bit_le(void *vaddr, unsigned long size)
791 unsigned long bytes, bits;
795 bytes = __ffs_word_loop(vaddr, size);
796 bits = __ffs_word(bytes*8, __load_ulong_le(vaddr, bytes));
797 return (bits < size) ? bits : size;
799 #define find_first_bit_le find_first_bit_le
801 static inline int find_next_bit_le(void *vaddr, unsigned long size,
802 unsigned long offset)
804 unsigned long *addr = vaddr, *p;
805 unsigned long bit, set;
809 bit = offset & (__BITOPS_WORDSIZE - 1);
812 p = addr + offset / __BITOPS_WORDSIZE;
815 * s390 version of ffz returns __BITOPS_WORDSIZE
816 * if no zero bit is present in the word.
818 set = __ffs_word(0, __load_ulong_le(p, 0) & (~0UL << bit));
820 return size + offset;
821 if (set < __BITOPS_WORDSIZE)
823 offset += __BITOPS_WORDSIZE;
824 size -= __BITOPS_WORDSIZE;
827 return offset + find_first_bit_le(p, size);
829 #define find_next_bit_le find_next_bit_le
831 #include <asm-generic/bitops/le.h>
833 #include <asm-generic/bitops/ext2-atomic-setbit.h>
835 #endif /* _S390_BITOPS_H */