2 * Copyright IBM Corp. 1999,2013
4 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
6 * The description below was taken in large parts from the powerpc
8 * Within a word, bits are numbered LSB first. Lot's of places make
9 * this assumption by directly testing bits with (val & (1<<nr)).
10 * This can cause confusion for large (> 1 word) bitmaps on a
11 * big-endian system because, unlike little endian, the number of each
12 * bit depends on the word size.
14 * The bitop functions are defined to work on unsigned longs, so for an
15 * s390x system the bits end up numbered:
16 * |63..............0|127............64|191...........128|255...........192|
18 * |31.....0|63....32|95....64|127...96|159..128|191..160|223..192|255..224|
20 * There are a few little-endian macros used mostly for filesystem
21 * bitmaps, these work on similar bit arrays layouts, but
23 * |7...0|15...8|23...16|31...24|39...32|47...40|55...48|63...56|
25 * The main difference is that bit 3-5 (64b) or 3-4 (32b) in the bit
26 * number field needs to be reversed compared to the big-endian bit
27 * fields. This can be achieved by XOR with 0x38 (64b) or 0x18 (32b).
29 * We also have special functions which work with an MSB0 encoding:
30 * on an s390x system the bits are numbered:
31 * |0..............63|64............127|128...........191|192...........255|
33 * |0.....31|32....63|64....95|96...127|128..159|160..191|192..223|224..255|
35 * The main difference is that bit 0-63 (64b) or 0-31 (32b) in the bit
36 * number field needs to be reversed compared to the LSB0 encoded bit
37 * fields. This can be achieved by XOR with 0x3f (64b) or 0x1f (32b).
41 #ifndef _S390_BITOPS_H
42 #define _S390_BITOPS_H
44 #ifndef _LINUX_BITOPS_H
45 #error only <linux/bitops.h> can be included directly
48 #include <linux/typecheck.h>
49 #include <linux/compiler.h>
50 #include <asm/barrier.h>
52 #define __BITOPS_NO_BARRIER "\n"
54 #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
56 #define __BITOPS_OR "laog"
57 #define __BITOPS_AND "lang"
58 #define __BITOPS_XOR "laxg"
59 #define __BITOPS_BARRIER "bcr 14,0\n"
61 #define __BITOPS_LOOP(__addr, __val, __op_string, __barrier) \
63 unsigned long __old; \
65 typecheck(unsigned long *, (__addr)); \
68 __op_string " %0,%2,%1\n" \
70 : "=d" (__old), "+Q" (*(__addr)) \
76 #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
78 #define __BITOPS_OR "ogr"
79 #define __BITOPS_AND "ngr"
80 #define __BITOPS_XOR "xgr"
81 #define __BITOPS_BARRIER "\n"
83 #define __BITOPS_LOOP(__addr, __val, __op_string, __barrier) \
85 unsigned long __old, __new; \
87 typecheck(unsigned long *, (__addr)); \
91 __op_string " %1,%3\n" \
94 : "=&d" (__old), "=&d" (__new), "+Q" (*(__addr))\
100 #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
102 #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG)
104 static inline unsigned long *
105 __bitops_word(unsigned long nr
, volatile unsigned long *ptr
)
109 addr
= (unsigned long)ptr
+ ((nr
^ (nr
& (BITS_PER_LONG
- 1))) >> 3);
110 return (unsigned long *)addr
;
113 static inline unsigned char *
114 __bitops_byte(unsigned long nr
, volatile unsigned long *ptr
)
116 return ((unsigned char *)ptr
) + ((nr
^ (BITS_PER_LONG
- 8)) >> 3);
119 static inline void set_bit(unsigned long nr
, volatile unsigned long *ptr
)
121 unsigned long *addr
= __bitops_word(nr
, ptr
);
124 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
125 if (__builtin_constant_p(nr
)) {
126 unsigned char *caddr
= __bitops_byte(nr
, ptr
);
131 : "i" (1 << (nr
& 7))
136 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
137 __BITOPS_LOOP(addr
, mask
, __BITOPS_OR
, __BITOPS_NO_BARRIER
);
140 static inline void clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
142 unsigned long *addr
= __bitops_word(nr
, ptr
);
145 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
146 if (__builtin_constant_p(nr
)) {
147 unsigned char *caddr
= __bitops_byte(nr
, ptr
);
152 : "i" (~(1 << (nr
& 7)))
157 mask
= ~(1UL << (nr
& (BITS_PER_LONG
- 1)));
158 __BITOPS_LOOP(addr
, mask
, __BITOPS_AND
, __BITOPS_NO_BARRIER
);
161 static inline void change_bit(unsigned long nr
, volatile unsigned long *ptr
)
163 unsigned long *addr
= __bitops_word(nr
, ptr
);
166 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
167 if (__builtin_constant_p(nr
)) {
168 unsigned char *caddr
= __bitops_byte(nr
, ptr
);
173 : "i" (1 << (nr
& 7))
178 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
179 __BITOPS_LOOP(addr
, mask
, __BITOPS_XOR
, __BITOPS_NO_BARRIER
);
183 test_and_set_bit(unsigned long nr
, volatile unsigned long *ptr
)
185 unsigned long *addr
= __bitops_word(nr
, ptr
);
186 unsigned long old
, mask
;
188 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
189 old
= __BITOPS_LOOP(addr
, mask
, __BITOPS_OR
, __BITOPS_BARRIER
);
190 return (old
& mask
) != 0;
194 test_and_clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
196 unsigned long *addr
= __bitops_word(nr
, ptr
);
197 unsigned long old
, mask
;
199 mask
= ~(1UL << (nr
& (BITS_PER_LONG
- 1)));
200 old
= __BITOPS_LOOP(addr
, mask
, __BITOPS_AND
, __BITOPS_BARRIER
);
201 return (old
& ~mask
) != 0;
205 test_and_change_bit(unsigned long nr
, volatile unsigned long *ptr
)
207 unsigned long *addr
= __bitops_word(nr
, ptr
);
208 unsigned long old
, mask
;
210 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
211 old
= __BITOPS_LOOP(addr
, mask
, __BITOPS_XOR
, __BITOPS_BARRIER
);
212 return (old
& mask
) != 0;
215 static inline void __set_bit(unsigned long nr
, volatile unsigned long *ptr
)
217 unsigned char *addr
= __bitops_byte(nr
, ptr
);
219 *addr
|= 1 << (nr
& 7);
223 __clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
225 unsigned char *addr
= __bitops_byte(nr
, ptr
);
227 *addr
&= ~(1 << (nr
& 7));
230 static inline void __change_bit(unsigned long nr
, volatile unsigned long *ptr
)
232 unsigned char *addr
= __bitops_byte(nr
, ptr
);
234 *addr
^= 1 << (nr
& 7);
238 __test_and_set_bit(unsigned long nr
, volatile unsigned long *ptr
)
240 unsigned char *addr
= __bitops_byte(nr
, ptr
);
244 *addr
|= 1 << (nr
& 7);
245 return (ch
>> (nr
& 7)) & 1;
249 __test_and_clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
251 unsigned char *addr
= __bitops_byte(nr
, ptr
);
255 *addr
&= ~(1 << (nr
& 7));
256 return (ch
>> (nr
& 7)) & 1;
260 __test_and_change_bit(unsigned long nr
, volatile unsigned long *ptr
)
262 unsigned char *addr
= __bitops_byte(nr
, ptr
);
266 *addr
^= 1 << (nr
& 7);
267 return (ch
>> (nr
& 7)) & 1;
270 static inline int test_bit(unsigned long nr
, const volatile unsigned long *ptr
)
272 const volatile unsigned char *addr
;
274 addr
= ((const volatile unsigned char *)ptr
);
275 addr
+= (nr
^ (BITS_PER_LONG
- 8)) >> 3;
276 return (*addr
>> (nr
& 7)) & 1;
279 static inline int test_and_set_bit_lock(unsigned long nr
,
280 volatile unsigned long *ptr
)
282 if (test_bit(nr
, ptr
))
284 return test_and_set_bit(nr
, ptr
);
287 static inline void clear_bit_unlock(unsigned long nr
,
288 volatile unsigned long *ptr
)
290 smp_mb__before_atomic();
294 static inline void __clear_bit_unlock(unsigned long nr
,
295 volatile unsigned long *ptr
)
298 __clear_bit(nr
, ptr
);
302 * Functions which use MSB0 bit numbering.
303 * On an s390x system the bits are numbered:
304 * |0..............63|64............127|128...........191|192...........255|
306 * |0.....31|32....63|64....95|96...127|128..159|160..191|192..223|224..255|
308 unsigned long find_first_bit_inv(const unsigned long *addr
, unsigned long size
);
309 unsigned long find_next_bit_inv(const unsigned long *addr
, unsigned long size
,
310 unsigned long offset
);
312 static inline void set_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
314 return set_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
317 static inline void clear_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
319 return clear_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
322 static inline void __set_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
324 return __set_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
327 static inline void __clear_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
329 return __clear_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
332 static inline int test_bit_inv(unsigned long nr
,
333 const volatile unsigned long *ptr
)
335 return test_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
338 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
341 * __flogr - find leftmost one
342 * @word - The word to search
344 * Returns the bit number of the most significant bit set,
345 * where the most significant bit has bit number 0.
346 * If no bit is set this function returns 64.
348 static inline unsigned char __flogr(unsigned long word
)
350 if (__builtin_constant_p(word
)) {
351 unsigned long bit
= 0;
355 if (!(word
& 0xffffffff00000000UL
)) {
359 if (!(word
& 0xffff000000000000UL
)) {
363 if (!(word
& 0xff00000000000000UL
)) {
367 if (!(word
& 0xf000000000000000UL
)) {
371 if (!(word
& 0xc000000000000000UL
)) {
375 if (!(word
& 0x8000000000000000UL
)) {
381 register unsigned long bit
asm("4") = word
;
382 register unsigned long out
asm("5");
385 " flogr %[bit],%[bit]\n"
386 : [bit
] "+d" (bit
), [out
] "=d" (out
) : : "cc");
392 * __ffs - find first bit in word.
393 * @word: The word to search
395 * Undefined if no bit exists, so code should check against 0 first.
397 static inline unsigned long __ffs(unsigned long word
)
399 return __flogr(-word
& word
) ^ (BITS_PER_LONG
- 1);
403 * ffs - find first bit set
404 * @word: the word to search
406 * This is defined the same way as the libc and
407 * compiler builtin ffs routines (man ffs).
409 static inline int ffs(int word
)
411 unsigned long mask
= 2 * BITS_PER_LONG
- 1;
412 unsigned int val
= (unsigned int)word
;
414 return (1 + (__flogr(-val
& val
) ^ (BITS_PER_LONG
- 1))) & mask
;
418 * __fls - find last (most-significant) set bit in a long word
419 * @word: the word to search
421 * Undefined if no set bit exists, so code should check against 0 first.
423 static inline unsigned long __fls(unsigned long word
)
425 return __flogr(word
) ^ (BITS_PER_LONG
- 1);
429 * fls64 - find last set bit in a 64-bit word
430 * @word: the word to search
432 * This is defined in a similar way as the libc and compiler builtin
433 * ffsll, but returns the position of the most significant set bit.
435 * fls64(value) returns 0 if value is 0 or the position of the last
436 * set bit if value is nonzero. The last (most significant) bit is
439 static inline int fls64(unsigned long word
)
441 unsigned long mask
= 2 * BITS_PER_LONG
- 1;
443 return (1 + (__flogr(word
) ^ (BITS_PER_LONG
- 1))) & mask
;
447 * fls - find last (most-significant) bit set
448 * @word: the word to search
450 * This is defined the same way as ffs.
451 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
453 static inline int fls(int word
)
455 return fls64((unsigned int)word
);
458 #else /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
460 #include <asm-generic/bitops/__ffs.h>
461 #include <asm-generic/bitops/ffs.h>
462 #include <asm-generic/bitops/__fls.h>
463 #include <asm-generic/bitops/fls.h>
464 #include <asm-generic/bitops/fls64.h>
466 #endif /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
468 #include <asm-generic/bitops/ffz.h>
469 #include <asm-generic/bitops/find.h>
470 #include <asm-generic/bitops/hweight.h>
471 #include <asm-generic/bitops/sched.h>
472 #include <asm-generic/bitops/le.h>
473 #include <asm-generic/bitops/ext2-atomic-setbit.h>
475 #endif /* _S390_BITOPS_H */
This page took 0.054411 seconds and 5 git commands to generate.