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 the bits
16 * |63..............0|127............64|191...........128|255...........192|
18 * There are a few little-endian macros used mostly for filesystem
19 * bitmaps, these work on similar bit array layouts, but byte-oriented:
20 * |7...0|15...8|23...16|31...24|39...32|47...40|55...48|63...56|
22 * The main difference is that bit 3-5 in the bit number field needs to be
23 * reversed compared to the big-endian bit fields. This can be achieved by
26 * We also have special functions which work with an MSB0 encoding.
27 * The bits are numbered:
28 * |0..............63|64............127|128...........191|192...........255|
30 * The main difference is that bit 0-63 in the bit number field needs to be
31 * reversed compared to the LSB0 encoded bit fields. This can be achieved by
36 #ifndef _S390_BITOPS_H
37 #define _S390_BITOPS_H
39 #ifndef _LINUX_BITOPS_H
40 #error only <linux/bitops.h> can be included directly
43 #include <linux/typecheck.h>
44 #include <linux/compiler.h>
45 #include <asm/barrier.h>
47 #define __BITOPS_NO_BARRIER "\n"
49 #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
51 #define __BITOPS_OR "laog"
52 #define __BITOPS_AND "lang"
53 #define __BITOPS_XOR "laxg"
54 #define __BITOPS_BARRIER "bcr 14,0\n"
56 #define __BITOPS_LOOP(__addr, __val, __op_string, __barrier) \
58 unsigned long __old; \
60 typecheck(unsigned long *, (__addr)); \
62 __op_string " %0,%2,%1\n" \
64 : "=d" (__old), "+Q" (*(__addr)) \
70 #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
72 #define __BITOPS_OR "ogr"
73 #define __BITOPS_AND "ngr"
74 #define __BITOPS_XOR "xgr"
75 #define __BITOPS_BARRIER "\n"
77 #define __BITOPS_LOOP(__addr, __val, __op_string, __barrier) \
79 unsigned long __old, __new; \
81 typecheck(unsigned long *, (__addr)); \
85 __op_string " %1,%3\n" \
88 : "=&d" (__old), "=&d" (__new), "+Q" (*(__addr))\
94 #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
96 #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG)
98 static inline unsigned long *
99 __bitops_word(unsigned long nr
, volatile unsigned long *ptr
)
103 addr
= (unsigned long)ptr
+ ((nr
^ (nr
& (BITS_PER_LONG
- 1))) >> 3);
104 return (unsigned long *)addr
;
107 static inline unsigned char *
108 __bitops_byte(unsigned long nr
, volatile unsigned long *ptr
)
110 return ((unsigned char *)ptr
) + ((nr
^ (BITS_PER_LONG
- 8)) >> 3);
113 static inline void set_bit(unsigned long nr
, volatile unsigned long *ptr
)
115 unsigned long *addr
= __bitops_word(nr
, ptr
);
118 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
119 if (__builtin_constant_p(nr
)) {
120 unsigned char *caddr
= __bitops_byte(nr
, ptr
);
125 : "i" (1 << (nr
& 7))
130 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
131 __BITOPS_LOOP(addr
, mask
, __BITOPS_OR
, __BITOPS_NO_BARRIER
);
134 static inline void clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
136 unsigned long *addr
= __bitops_word(nr
, ptr
);
139 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
140 if (__builtin_constant_p(nr
)) {
141 unsigned char *caddr
= __bitops_byte(nr
, ptr
);
146 : "i" (~(1 << (nr
& 7)))
151 mask
= ~(1UL << (nr
& (BITS_PER_LONG
- 1)));
152 __BITOPS_LOOP(addr
, mask
, __BITOPS_AND
, __BITOPS_NO_BARRIER
);
155 static inline void change_bit(unsigned long nr
, volatile unsigned long *ptr
)
157 unsigned long *addr
= __bitops_word(nr
, ptr
);
160 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
161 if (__builtin_constant_p(nr
)) {
162 unsigned char *caddr
= __bitops_byte(nr
, ptr
);
167 : "i" (1 << (nr
& 7))
172 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
173 __BITOPS_LOOP(addr
, mask
, __BITOPS_XOR
, __BITOPS_NO_BARRIER
);
177 test_and_set_bit(unsigned long nr
, volatile unsigned long *ptr
)
179 unsigned long *addr
= __bitops_word(nr
, ptr
);
180 unsigned long old
, mask
;
182 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
183 old
= __BITOPS_LOOP(addr
, mask
, __BITOPS_OR
, __BITOPS_BARRIER
);
184 return (old
& mask
) != 0;
188 test_and_clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
190 unsigned long *addr
= __bitops_word(nr
, ptr
);
191 unsigned long old
, mask
;
193 mask
= ~(1UL << (nr
& (BITS_PER_LONG
- 1)));
194 old
= __BITOPS_LOOP(addr
, mask
, __BITOPS_AND
, __BITOPS_BARRIER
);
195 return (old
& ~mask
) != 0;
199 test_and_change_bit(unsigned long nr
, volatile unsigned long *ptr
)
201 unsigned long *addr
= __bitops_word(nr
, ptr
);
202 unsigned long old
, mask
;
204 mask
= 1UL << (nr
& (BITS_PER_LONG
- 1));
205 old
= __BITOPS_LOOP(addr
, mask
, __BITOPS_XOR
, __BITOPS_BARRIER
);
206 return (old
& mask
) != 0;
209 static inline void __set_bit(unsigned long nr
, volatile unsigned long *ptr
)
211 unsigned char *addr
= __bitops_byte(nr
, ptr
);
213 *addr
|= 1 << (nr
& 7);
217 __clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
219 unsigned char *addr
= __bitops_byte(nr
, ptr
);
221 *addr
&= ~(1 << (nr
& 7));
224 static inline void __change_bit(unsigned long nr
, volatile unsigned long *ptr
)
226 unsigned char *addr
= __bitops_byte(nr
, ptr
);
228 *addr
^= 1 << (nr
& 7);
232 __test_and_set_bit(unsigned long nr
, volatile unsigned long *ptr
)
234 unsigned char *addr
= __bitops_byte(nr
, ptr
);
238 *addr
|= 1 << (nr
& 7);
239 return (ch
>> (nr
& 7)) & 1;
243 __test_and_clear_bit(unsigned long nr
, volatile unsigned long *ptr
)
245 unsigned char *addr
= __bitops_byte(nr
, ptr
);
249 *addr
&= ~(1 << (nr
& 7));
250 return (ch
>> (nr
& 7)) & 1;
254 __test_and_change_bit(unsigned long nr
, volatile unsigned long *ptr
)
256 unsigned char *addr
= __bitops_byte(nr
, ptr
);
260 *addr
^= 1 << (nr
& 7);
261 return (ch
>> (nr
& 7)) & 1;
264 static inline int test_bit(unsigned long nr
, const volatile unsigned long *ptr
)
266 const volatile unsigned char *addr
;
268 addr
= ((const volatile unsigned char *)ptr
);
269 addr
+= (nr
^ (BITS_PER_LONG
- 8)) >> 3;
270 return (*addr
>> (nr
& 7)) & 1;
273 static inline int test_and_set_bit_lock(unsigned long nr
,
274 volatile unsigned long *ptr
)
276 if (test_bit(nr
, ptr
))
278 return test_and_set_bit(nr
, ptr
);
281 static inline void clear_bit_unlock(unsigned long nr
,
282 volatile unsigned long *ptr
)
284 smp_mb__before_atomic();
288 static inline void __clear_bit_unlock(unsigned long nr
,
289 volatile unsigned long *ptr
)
292 __clear_bit(nr
, ptr
);
296 * Functions which use MSB0 bit numbering.
297 * The bits are numbered:
298 * |0..............63|64............127|128...........191|192...........255|
300 unsigned long find_first_bit_inv(const unsigned long *addr
, unsigned long size
);
301 unsigned long find_next_bit_inv(const unsigned long *addr
, unsigned long size
,
302 unsigned long offset
);
304 static inline void set_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
306 return set_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
309 static inline void clear_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
311 return clear_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
314 static inline void __set_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
316 return __set_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
319 static inline void __clear_bit_inv(unsigned long nr
, volatile unsigned long *ptr
)
321 return __clear_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
324 static inline int test_bit_inv(unsigned long nr
,
325 const volatile unsigned long *ptr
)
327 return test_bit(nr
^ (BITS_PER_LONG
- 1), ptr
);
330 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
333 * __flogr - find leftmost one
334 * @word - The word to search
336 * Returns the bit number of the most significant bit set,
337 * where the most significant bit has bit number 0.
338 * If no bit is set this function returns 64.
340 static inline unsigned char __flogr(unsigned long word
)
342 if (__builtin_constant_p(word
)) {
343 unsigned long bit
= 0;
347 if (!(word
& 0xffffffff00000000UL
)) {
351 if (!(word
& 0xffff000000000000UL
)) {
355 if (!(word
& 0xff00000000000000UL
)) {
359 if (!(word
& 0xf000000000000000UL
)) {
363 if (!(word
& 0xc000000000000000UL
)) {
367 if (!(word
& 0x8000000000000000UL
)) {
373 register unsigned long bit
asm("4") = word
;
374 register unsigned long out
asm("5");
377 " flogr %[bit],%[bit]\n"
378 : [bit
] "+d" (bit
), [out
] "=d" (out
) : : "cc");
384 * __ffs - find first bit in word.
385 * @word: The word to search
387 * Undefined if no bit exists, so code should check against 0 first.
389 static inline unsigned long __ffs(unsigned long word
)
391 return __flogr(-word
& word
) ^ (BITS_PER_LONG
- 1);
395 * ffs - find first bit set
396 * @word: the word to search
398 * This is defined the same way as the libc and
399 * compiler builtin ffs routines (man ffs).
401 static inline int ffs(int word
)
403 unsigned long mask
= 2 * BITS_PER_LONG
- 1;
404 unsigned int val
= (unsigned int)word
;
406 return (1 + (__flogr(-val
& val
) ^ (BITS_PER_LONG
- 1))) & mask
;
410 * __fls - find last (most-significant) set bit in a long word
411 * @word: the word to search
413 * Undefined if no set bit exists, so code should check against 0 first.
415 static inline unsigned long __fls(unsigned long word
)
417 return __flogr(word
) ^ (BITS_PER_LONG
- 1);
421 * fls64 - find last set bit in a 64-bit word
422 * @word: the word to search
424 * This is defined in a similar way as the libc and compiler builtin
425 * ffsll, but returns the position of the most significant set bit.
427 * fls64(value) returns 0 if value is 0 or the position of the last
428 * set bit if value is nonzero. The last (most significant) bit is
431 static inline int fls64(unsigned long word
)
433 unsigned long mask
= 2 * BITS_PER_LONG
- 1;
435 return (1 + (__flogr(word
) ^ (BITS_PER_LONG
- 1))) & mask
;
439 * fls - find last (most-significant) bit set
440 * @word: the word to search
442 * This is defined the same way as ffs.
443 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
445 static inline int fls(int word
)
447 return fls64((unsigned int)word
);
450 #else /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
452 #include <asm-generic/bitops/__ffs.h>
453 #include <asm-generic/bitops/ffs.h>
454 #include <asm-generic/bitops/__fls.h>
455 #include <asm-generic/bitops/fls.h>
456 #include <asm-generic/bitops/fls64.h>
458 #endif /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
460 #include <asm-generic/bitops/ffz.h>
461 #include <asm-generic/bitops/find.h>
462 #include <asm-generic/bitops/hweight.h>
463 #include <asm-generic/bitops/sched.h>
464 #include <asm-generic/bitops/le.h>
465 #include <asm-generic/bitops/ext2-atomic-setbit.h>
467 #endif /* _S390_BITOPS_H */