3 * Copyright IBM Corp. 1999
4 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
6 * Derived from "include/asm-i386/bitops.h"
7 * Copyright (C) 1992, Linus Torvalds
11 #ifndef _S390_BITOPS_H
12 #define _S390_BITOPS_H
14 #ifndef _LINUX_BITOPS_H
15 #error only <linux/bitops.h> can be included directly
18 #include <linux/typecheck.h>
19 #include <linux/compiler.h>
22 * 32 bit bitops format:
23 * bit 0 is the LSB of *addr; bit 31 is the MSB of *addr;
24 * bit 32 is the LSB of *(addr+4). That combined with the
25 * big endian byte order on S390 give the following bit
27 * 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 \
28 * 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
29 * after that follows the next long with bit numbers
30 * 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
31 * 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
32 * The reason for this bit ordering is the fact that
33 * in the architecture independent code bits operations
34 * of the form "flags |= (1 << bitnr)" are used INTERMIXED
35 * with operation of the form "set_bit(bitnr, flags)".
37 * 64 bit bitops format:
38 * bit 0 is the LSB of *addr; bit 63 is the MSB of *addr;
39 * bit 64 is the LSB of *(addr+8). That combined with the
40 * big endian byte order on S390 give the following bit
42 * 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
43 * 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
44 * 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10
45 * 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
46 * after that follows the next long with bit numbers
47 * 7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70
48 * 6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60
49 * 5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50
50 * 4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40
51 * The reason for this bit ordering is the fact that
52 * in the architecture independent code bits operations
53 * of the form "flags |= (1 << bitnr)" are used INTERMIXED
54 * with operation of the form "set_bit(bitnr, flags)".
57 /* bitmap tables from arch/s390/kernel/bitmap.c */
58 extern const char _zb_findmap[];
59 extern const char _sb_findmap[];
63 #define __BITOPS_OR "or"
64 #define __BITOPS_AND "nr"
65 #define __BITOPS_XOR "xr"
67 #define __BITOPS_LOOP(__addr, __val, __op_string) \
69 unsigned long __old, __new; \
71 typecheck(unsigned long *, (__addr)); \
75 __op_string " %1,%3\n" \
78 : "=&d" (__old), "=&d" (__new), "+Q" (*(__addr))\
84 #else /* CONFIG_64BIT */
86 #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
88 #define __BITOPS_OR "laog"
89 #define __BITOPS_AND "lang"
90 #define __BITOPS_XOR "laxg"
92 #define __BITOPS_LOOP(__addr, __val, __op_string) \
94 unsigned long __old; \
96 typecheck(unsigned long *, (__addr)); \
98 __op_string " %0,%2,%1\n" \
99 : "=d" (__old), "+Q" (*(__addr)) \
105 #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
107 #define __BITOPS_OR "ogr"
108 #define __BITOPS_AND "ngr"
109 #define __BITOPS_XOR "xgr"
111 #define __BITOPS_LOOP(__addr, __val, __op_string) \
113 unsigned long __old, __new; \
115 typecheck(unsigned long *, (__addr)); \
119 __op_string " %1,%3\n" \
122 : "=&d" (__old), "=&d" (__new), "+Q" (*(__addr))\
128 #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
130 #endif /* CONFIG_64BIT */
132 #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG)
134 static inline unsigned long *
135 __bitops_word(unsigned long nr, volatile unsigned long *ptr)
139 addr = (unsigned long)ptr + ((nr ^ (nr & (BITS_PER_LONG - 1))) >> 3);
140 return (unsigned long *)addr;
143 static inline unsigned char *
144 __bitops_byte(unsigned long nr, volatile unsigned long *ptr)
146 return ((unsigned char *)ptr) + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
149 static inline void set_bit(unsigned long nr, volatile unsigned long *ptr)
151 unsigned long *addr = __bitops_word(nr, ptr);
154 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
155 if (__builtin_constant_p(nr)) {
156 unsigned char *caddr = __bitops_byte(nr, ptr);
161 : "i" (1 << (nr & 7))
166 mask = 1UL << (nr & (BITS_PER_LONG - 1));
167 __BITOPS_LOOP(addr, mask, __BITOPS_OR);
170 static inline void clear_bit(unsigned long nr, volatile unsigned long *ptr)
172 unsigned long *addr = __bitops_word(nr, ptr);
175 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
176 if (__builtin_constant_p(nr)) {
177 unsigned char *caddr = __bitops_byte(nr, ptr);
182 : "i" (~(1 << (nr & 7)))
187 mask = ~(1UL << (nr & (BITS_PER_LONG - 1)));
188 __BITOPS_LOOP(addr, mask, __BITOPS_AND);
191 static inline void change_bit(unsigned long nr, volatile unsigned long *ptr)
193 unsigned long *addr = __bitops_word(nr, ptr);
196 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
197 if (__builtin_constant_p(nr)) {
198 unsigned char *caddr = __bitops_byte(nr, ptr);
203 : "i" (1 << (nr & 7))
208 mask = 1UL << (nr & (BITS_PER_LONG - 1));
209 __BITOPS_LOOP(addr, mask, __BITOPS_XOR);
213 test_and_set_bit(unsigned long nr, volatile unsigned long *ptr)
215 unsigned long *addr = __bitops_word(nr, ptr);
216 unsigned long old, mask;
218 mask = 1UL << (nr & (BITS_PER_LONG - 1));
219 old = __BITOPS_LOOP(addr, mask, __BITOPS_OR);
221 return (old & mask) != 0;
225 test_and_clear_bit(unsigned long nr, volatile unsigned long *ptr)
227 unsigned long *addr = __bitops_word(nr, ptr);
228 unsigned long old, mask;
230 mask = ~(1UL << (nr & (BITS_PER_LONG - 1)));
231 old = __BITOPS_LOOP(addr, mask, __BITOPS_AND);
233 return (old & ~mask) != 0;
237 test_and_change_bit(unsigned long nr, volatile unsigned long *ptr)
239 unsigned long *addr = __bitops_word(nr, ptr);
240 unsigned long old, mask;
242 mask = 1UL << (nr & (BITS_PER_LONG - 1));
243 old = __BITOPS_LOOP(addr, mask, __BITOPS_XOR);
245 return (old & mask) != 0;
248 static inline void __set_bit(unsigned long nr, volatile unsigned long *ptr)
250 unsigned char *addr = __bitops_byte(nr, ptr);
252 *addr |= 1 << (nr & 7);
256 __clear_bit(unsigned long nr, volatile unsigned long *ptr)
258 unsigned char *addr = __bitops_byte(nr, ptr);
260 *addr &= ~(1 << (nr & 7));
263 static inline void __change_bit(unsigned long nr, volatile unsigned long *ptr)
265 unsigned char *addr = __bitops_byte(nr, ptr);
267 *addr ^= 1 << (nr & 7);
271 __test_and_set_bit(unsigned long nr, volatile unsigned long *ptr)
273 unsigned char *addr = __bitops_byte(nr, ptr);
277 *addr |= 1 << (nr & 7);
278 return (ch >> (nr & 7)) & 1;
282 __test_and_clear_bit(unsigned long nr, volatile unsigned long *ptr)
284 unsigned char *addr = __bitops_byte(nr, ptr);
288 *addr &= ~(1 << (nr & 7));
289 return (ch >> (nr & 7)) & 1;
293 __test_and_change_bit(unsigned long nr, volatile unsigned long *ptr)
295 unsigned char *addr = __bitops_byte(nr, ptr);
299 *addr ^= 1 << (nr & 7);
300 return (ch >> (nr & 7)) & 1;
303 static inline int test_bit(unsigned long nr, const volatile unsigned long *ptr)
305 const volatile unsigned char *addr;
307 addr = ((const volatile unsigned char *)ptr);
308 addr += (nr ^ (BITS_PER_LONG - 8)) >> 3;
309 return (*addr >> (nr & 7)) & 1;
313 * Optimized find bit helper functions.
317 * __ffz_word_loop - find byte offset of first long != -1UL
318 * @addr: pointer to array of unsigned long
319 * @size: size of the array in bits
321 static inline unsigned long __ffz_word_loop(const unsigned long *addr,
324 typedef struct { long _[__BITOPS_WORDS(size)]; } addrtype;
325 unsigned long bytes = 0;
341 "0: cg %2,0(%0,%3)\n"
347 : "+&a" (bytes), "+&d" (size)
348 : "d" (-1UL), "a" (addr), "m" (*(addrtype *) addr)
354 * __ffs_word_loop - find byte offset of first long != 0UL
355 * @addr: pointer to array of unsigned long
356 * @size: size of the array in bits
358 static inline unsigned long __ffs_word_loop(const unsigned long *addr,
361 typedef struct { long _[__BITOPS_WORDS(size)]; } addrtype;
362 unsigned long bytes = 0;
378 "0: cg %2,0(%0,%3)\n"
384 : "+&a" (bytes), "+&a" (size)
385 : "d" (0UL), "a" (addr), "m" (*(addrtype *) addr)
391 * __ffz_word - add number of the first unset bit
392 * @nr: base value the bit number is added to
393 * @word: the word that is searched for unset bits
395 static inline unsigned long __ffz_word(unsigned long nr, unsigned long word)
398 if ((word & 0xffffffff) == 0xffffffff) {
403 if ((word & 0xffff) == 0xffff) {
407 if ((word & 0xff) == 0xff) {
411 return nr + _zb_findmap[(unsigned char) word];
415 * __ffs_word - add number of the first set bit
416 * @nr: base value the bit number is added to
417 * @word: the word that is searched for set bits
419 static inline unsigned long __ffs_word(unsigned long nr, unsigned long word)
422 if ((word & 0xffffffff) == 0) {
427 if ((word & 0xffff) == 0) {
431 if ((word & 0xff) == 0) {
435 return nr + _sb_findmap[(unsigned char) word];
440 * __load_ulong_be - load big endian unsigned long
441 * @p: pointer to array of unsigned long
442 * @offset: byte offset of source value in the array
444 static inline unsigned long __load_ulong_be(const unsigned long *p,
445 unsigned long offset)
447 p = (unsigned long *)((unsigned long) p + offset);
452 * __load_ulong_le - load little endian unsigned long
453 * @p: pointer to array of unsigned long
454 * @offset: byte offset of source value in the array
456 static inline unsigned long __load_ulong_le(const unsigned long *p,
457 unsigned long offset)
461 p = (unsigned long *)((unsigned long) p + offset);
465 " icm %0,2,%O1+1(%R1)\n"
466 " icm %0,4,%O1+2(%R1)\n"
467 " icm %0,8,%O1+3(%R1)"
468 : "=&d" (word) : "Q" (*p) : "cc");
472 : "=d" (word) : "m" (*p) );
478 * The various find bit functions.
482 * ffz - find first zero in word.
483 * @word: The word to search
485 * Undefined if no zero exists, so code should check against ~0UL first.
487 static inline unsigned long ffz(unsigned long word)
489 return __ffz_word(0, word);
493 * __ffs - find first bit in word.
494 * @word: The word to search
496 * Undefined if no bit exists, so code should check against 0 first.
498 static inline unsigned long __ffs (unsigned long word)
500 return __ffs_word(0, word);
504 * ffs - find first bit set
505 * @x: the word to search
507 * This is defined the same way as
508 * the libc and compiler builtin ffs routines, therefore
509 * differs in spirit from the above ffz (man ffs).
511 static inline int ffs(int x)
515 return __ffs_word(1, x);
519 * find_first_zero_bit - find the first zero bit in a memory region
520 * @addr: The address to start the search at
521 * @size: The maximum size to search
523 * Returns the bit-number of the first zero bit, not the number of the byte
526 static inline unsigned long find_first_zero_bit(const unsigned long *addr,
529 unsigned long bytes, bits;
533 bytes = __ffz_word_loop(addr, size);
534 bits = __ffz_word(bytes*8, __load_ulong_be(addr, bytes));
535 return (bits < size) ? bits : size;
537 #define find_first_zero_bit find_first_zero_bit
540 * find_first_bit - find the first set bit in a memory region
541 * @addr: The address to start the search at
542 * @size: The maximum size to search
544 * Returns the bit-number of the first set bit, not the number of the byte
547 static inline unsigned long find_first_bit(const unsigned long * addr,
550 unsigned long bytes, bits;
554 bytes = __ffs_word_loop(addr, size);
555 bits = __ffs_word(bytes*8, __load_ulong_be(addr, bytes));
556 return (bits < size) ? bits : size;
558 #define find_first_bit find_first_bit
561 * Big endian variant whichs starts bit counting from left using
562 * the flogr (find leftmost one) instruction.
564 static inline unsigned long __flo_word(unsigned long nr, unsigned long val)
566 register unsigned long bit asm("2") = val;
567 register unsigned long out asm("3");
570 " .insn rre,0xb9830000,%[bit],%[bit]\n"
571 : [bit] "+d" (bit), [out] "=d" (out) : : "cc");
576 * 64 bit special left bitops format:
578 * 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
579 * 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
580 * 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
581 * 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
582 * after that follows the next long with bit numbers
583 * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
584 * 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
585 * 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
586 * 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f
587 * The reason for this bit ordering is the fact that
588 * the hardware sets bits in a bitmap starting at bit 0
589 * and we don't want to scan the bitmap from the 'wrong
592 static inline unsigned long find_first_bit_left(const unsigned long *addr,
595 unsigned long bytes, bits;
599 bytes = __ffs_word_loop(addr, size);
600 bits = __flo_word(bytes * 8, __load_ulong_be(addr, bytes));
601 return (bits < size) ? bits : size;
604 static inline int find_next_bit_left(const unsigned long *addr,
606 unsigned long offset)
608 const unsigned long *p;
609 unsigned long bit, set;
613 bit = offset & (BITS_PER_LONG - 1);
616 p = addr + offset / BITS_PER_LONG;
618 set = __flo_word(0, *p & (~0UL >> bit));
620 return size + offset;
621 if (set < BITS_PER_LONG)
623 offset += BITS_PER_LONG;
624 size -= BITS_PER_LONG;
627 return offset + find_first_bit_left(p, size);
630 #define for_each_set_bit_left(bit, addr, size) \
631 for ((bit) = find_first_bit_left((addr), (size)); \
633 (bit) = find_next_bit_left((addr), (size), (bit) + 1))
635 /* same as for_each_set_bit() but use bit as value to start with */
636 #define for_each_set_bit_left_cont(bit, addr, size) \
637 for ((bit) = find_next_bit_left((addr), (size), (bit)); \
639 (bit) = find_next_bit_left((addr), (size), (bit) + 1))
642 * find_next_zero_bit - find the first zero bit in a memory region
643 * @addr: The address to base the search on
644 * @offset: The bitnumber to start searching at
645 * @size: The maximum size to search
647 static inline int find_next_zero_bit (const unsigned long * addr,
649 unsigned long offset)
651 const unsigned long *p;
652 unsigned long bit, set;
656 bit = offset & (BITS_PER_LONG - 1);
659 p = addr + offset / BITS_PER_LONG;
662 * __ffz_word returns BITS_PER_LONG
663 * if no zero bit is present in the word.
665 set = __ffz_word(bit, *p >> bit);
667 return size + offset;
668 if (set < BITS_PER_LONG)
670 offset += BITS_PER_LONG;
671 size -= BITS_PER_LONG;
674 return offset + find_first_zero_bit(p, size);
676 #define find_next_zero_bit find_next_zero_bit
679 * find_next_bit - find the first set bit in a memory region
680 * @addr: The address to base the search on
681 * @offset: The bitnumber to start searching at
682 * @size: The maximum size to search
684 static inline int find_next_bit (const unsigned long * addr,
686 unsigned long offset)
688 const unsigned long *p;
689 unsigned long bit, set;
693 bit = offset & (BITS_PER_LONG - 1);
696 p = addr + offset / BITS_PER_LONG;
699 * __ffs_word returns BITS_PER_LONG
700 * if no one bit is present in the word.
702 set = __ffs_word(0, *p & (~0UL << bit));
704 return size + offset;
705 if (set < BITS_PER_LONG)
707 offset += BITS_PER_LONG;
708 size -= BITS_PER_LONG;
711 return offset + find_first_bit(p, size);
713 #define find_next_bit find_next_bit
716 * Every architecture must define this function. It's the fastest
717 * way of searching a 140-bit bitmap where the first 100 bits are
718 * unlikely to be set. It's guaranteed that at least one of the 140
721 static inline int sched_find_first_bit(unsigned long *b)
723 return find_first_bit(b, 140);
726 #include <asm-generic/bitops/fls.h>
727 #include <asm-generic/bitops/__fls.h>
728 #include <asm-generic/bitops/fls64.h>
730 #include <asm-generic/bitops/hweight.h>
731 #include <asm-generic/bitops/lock.h>
734 * ATTENTION: intel byte ordering convention for ext2 and minix !!
735 * bit 0 is the LSB of addr; bit 31 is the MSB of addr;
736 * bit 32 is the LSB of (addr+4).
737 * That combined with the little endian byte order of Intel gives the
738 * following bit order in memory:
739 * 07 06 05 04 03 02 01 00 15 14 13 12 11 10 09 08 \
740 * 23 22 21 20 19 18 17 16 31 30 29 28 27 26 25 24
743 static inline int find_first_zero_bit_le(void *vaddr, unsigned int size)
745 unsigned long bytes, bits;
749 bytes = __ffz_word_loop(vaddr, size);
750 bits = __ffz_word(bytes*8, __load_ulong_le(vaddr, bytes));
751 return (bits < size) ? bits : size;
753 #define find_first_zero_bit_le find_first_zero_bit_le
755 static inline int find_next_zero_bit_le(void *vaddr, unsigned long size,
756 unsigned long offset)
758 unsigned long *addr = vaddr, *p;
759 unsigned long bit, set;
763 bit = offset & (BITS_PER_LONG - 1);
766 p = addr + offset / BITS_PER_LONG;
769 * s390 version of ffz returns BITS_PER_LONG
770 * if no zero bit is present in the word.
772 set = __ffz_word(bit, __load_ulong_le(p, 0) >> bit);
774 return size + offset;
775 if (set < BITS_PER_LONG)
777 offset += BITS_PER_LONG;
778 size -= BITS_PER_LONG;
781 return offset + find_first_zero_bit_le(p, size);
783 #define find_next_zero_bit_le find_next_zero_bit_le
785 static inline unsigned long find_first_bit_le(void *vaddr, unsigned long size)
787 unsigned long bytes, bits;
791 bytes = __ffs_word_loop(vaddr, size);
792 bits = __ffs_word(bytes*8, __load_ulong_le(vaddr, bytes));
793 return (bits < size) ? bits : size;
795 #define find_first_bit_le find_first_bit_le
797 static inline int find_next_bit_le(void *vaddr, unsigned long size,
798 unsigned long offset)
800 unsigned long *addr = vaddr, *p;
801 unsigned long bit, set;
805 bit = offset & (BITS_PER_LONG - 1);
808 p = addr + offset / BITS_PER_LONG;
811 * s390 version of ffz returns BITS_PER_LONG
812 * if no zero bit is present in the word.
814 set = __ffs_word(0, __load_ulong_le(p, 0) & (~0UL << bit));
816 return size + offset;
817 if (set < BITS_PER_LONG)
819 offset += BITS_PER_LONG;
820 size -= BITS_PER_LONG;
823 return offset + find_first_bit_le(p, size);
825 #define find_next_bit_le find_next_bit_le
827 #include <asm-generic/bitops/le.h>
829 #include <asm-generic/bitops/ext2-atomic-setbit.h>
831 #endif /* _S390_BITOPS_H */