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/atomic_ops.h>
46 #include <asm/barrier.h>
48 #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG)
50 static inline unsigned long *
51 __bitops_word(unsigned long nr, volatile unsigned long *ptr)
55 addr = (unsigned long)ptr + ((nr ^ (nr & (BITS_PER_LONG - 1))) >> 3);
56 return (unsigned long *)addr;
59 static inline unsigned char *
60 __bitops_byte(unsigned long nr, volatile unsigned long *ptr)
62 return ((unsigned char *)ptr) + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
65 static inline void set_bit(unsigned long nr, volatile unsigned long *ptr)
67 unsigned long *addr = __bitops_word(nr, ptr);
70 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
71 if (__builtin_constant_p(nr)) {
72 unsigned char *caddr = __bitops_byte(nr, ptr);
82 mask = 1UL << (nr & (BITS_PER_LONG - 1));
83 __atomic64_or(mask, addr);
86 static inline void clear_bit(unsigned long nr, volatile unsigned long *ptr)
88 unsigned long *addr = __bitops_word(nr, ptr);
91 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
92 if (__builtin_constant_p(nr)) {
93 unsigned char *caddr = __bitops_byte(nr, ptr);
98 : "i" (~(1 << (nr & 7)))
103 mask = ~(1UL << (nr & (BITS_PER_LONG - 1)));
104 __atomic64_and(mask, addr);
107 static inline void change_bit(unsigned long nr, volatile unsigned long *ptr)
109 unsigned long *addr = __bitops_word(nr, ptr);
112 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
113 if (__builtin_constant_p(nr)) {
114 unsigned char *caddr = __bitops_byte(nr, ptr);
119 : "i" (1 << (nr & 7))
124 mask = 1UL << (nr & (BITS_PER_LONG - 1));
125 __atomic64_xor(mask, addr);
129 test_and_set_bit(unsigned long nr, volatile unsigned long *ptr)
131 unsigned long *addr = __bitops_word(nr, ptr);
132 unsigned long old, mask;
134 mask = 1UL << (nr & (BITS_PER_LONG - 1));
135 old = __atomic64_or_barrier(mask, addr);
136 return (old & mask) != 0;
140 test_and_clear_bit(unsigned long nr, volatile unsigned long *ptr)
142 unsigned long *addr = __bitops_word(nr, ptr);
143 unsigned long old, mask;
145 mask = ~(1UL << (nr & (BITS_PER_LONG - 1)));
146 old = __atomic64_and_barrier(mask, addr);
147 return (old & ~mask) != 0;
151 test_and_change_bit(unsigned long nr, volatile unsigned long *ptr)
153 unsigned long *addr = __bitops_word(nr, ptr);
154 unsigned long old, mask;
156 mask = 1UL << (nr & (BITS_PER_LONG - 1));
157 old = __atomic64_xor_barrier(mask, addr);
158 return (old & mask) != 0;
161 static inline void __set_bit(unsigned long nr, volatile unsigned long *ptr)
163 unsigned char *addr = __bitops_byte(nr, ptr);
165 *addr |= 1 << (nr & 7);
169 __clear_bit(unsigned long nr, volatile unsigned long *ptr)
171 unsigned char *addr = __bitops_byte(nr, ptr);
173 *addr &= ~(1 << (nr & 7));
176 static inline void __change_bit(unsigned long nr, volatile unsigned long *ptr)
178 unsigned char *addr = __bitops_byte(nr, ptr);
180 *addr ^= 1 << (nr & 7);
184 __test_and_set_bit(unsigned long nr, volatile unsigned long *ptr)
186 unsigned char *addr = __bitops_byte(nr, ptr);
190 *addr |= 1 << (nr & 7);
191 return (ch >> (nr & 7)) & 1;
195 __test_and_clear_bit(unsigned long nr, volatile unsigned long *ptr)
197 unsigned char *addr = __bitops_byte(nr, ptr);
201 *addr &= ~(1 << (nr & 7));
202 return (ch >> (nr & 7)) & 1;
206 __test_and_change_bit(unsigned long nr, volatile unsigned long *ptr)
208 unsigned char *addr = __bitops_byte(nr, ptr);
212 *addr ^= 1 << (nr & 7);
213 return (ch >> (nr & 7)) & 1;
216 static inline int test_bit(unsigned long nr, const volatile unsigned long *ptr)
218 const volatile unsigned char *addr;
220 addr = ((const volatile unsigned char *)ptr);
221 addr += (nr ^ (BITS_PER_LONG - 8)) >> 3;
222 return (*addr >> (nr & 7)) & 1;
225 static inline int test_and_set_bit_lock(unsigned long nr,
226 volatile unsigned long *ptr)
228 if (test_bit(nr, ptr))
230 return test_and_set_bit(nr, ptr);
233 static inline void clear_bit_unlock(unsigned long nr,
234 volatile unsigned long *ptr)
236 smp_mb__before_atomic();
240 static inline void __clear_bit_unlock(unsigned long nr,
241 volatile unsigned long *ptr)
244 __clear_bit(nr, ptr);
248 * Functions which use MSB0 bit numbering.
249 * The bits are numbered:
250 * |0..............63|64............127|128...........191|192...........255|
252 unsigned long find_first_bit_inv(const unsigned long *addr, unsigned long size);
253 unsigned long find_next_bit_inv(const unsigned long *addr, unsigned long size,
254 unsigned long offset);
256 static inline void set_bit_inv(unsigned long nr, volatile unsigned long *ptr)
258 return set_bit(nr ^ (BITS_PER_LONG - 1), ptr);
261 static inline void clear_bit_inv(unsigned long nr, volatile unsigned long *ptr)
263 return clear_bit(nr ^ (BITS_PER_LONG - 1), ptr);
266 static inline void __set_bit_inv(unsigned long nr, volatile unsigned long *ptr)
268 return __set_bit(nr ^ (BITS_PER_LONG - 1), ptr);
271 static inline void __clear_bit_inv(unsigned long nr, volatile unsigned long *ptr)
273 return __clear_bit(nr ^ (BITS_PER_LONG - 1), ptr);
276 static inline int test_bit_inv(unsigned long nr,
277 const volatile unsigned long *ptr)
279 return test_bit(nr ^ (BITS_PER_LONG - 1), ptr);
282 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
285 * __flogr - find leftmost one
286 * @word - The word to search
288 * Returns the bit number of the most significant bit set,
289 * where the most significant bit has bit number 0.
290 * If no bit is set this function returns 64.
292 static inline unsigned char __flogr(unsigned long word)
294 if (__builtin_constant_p(word)) {
295 unsigned long bit = 0;
299 if (!(word & 0xffffffff00000000UL)) {
303 if (!(word & 0xffff000000000000UL)) {
307 if (!(word & 0xff00000000000000UL)) {
311 if (!(word & 0xf000000000000000UL)) {
315 if (!(word & 0xc000000000000000UL)) {
319 if (!(word & 0x8000000000000000UL)) {
325 register unsigned long bit asm("4") = word;
326 register unsigned long out asm("5");
329 " flogr %[bit],%[bit]\n"
330 : [bit] "+d" (bit), [out] "=d" (out) : : "cc");
336 * __ffs - find first bit in word.
337 * @word: The word to search
339 * Undefined if no bit exists, so code should check against 0 first.
341 static inline unsigned long __ffs(unsigned long word)
343 return __flogr(-word & word) ^ (BITS_PER_LONG - 1);
347 * ffs - find first bit set
348 * @word: the word to search
350 * This is defined the same way as the libc and
351 * compiler builtin ffs routines (man ffs).
353 static inline int ffs(int word)
355 unsigned long mask = 2 * BITS_PER_LONG - 1;
356 unsigned int val = (unsigned int)word;
358 return (1 + (__flogr(-val & val) ^ (BITS_PER_LONG - 1))) & mask;
362 * __fls - find last (most-significant) set bit in a long word
363 * @word: the word to search
365 * Undefined if no set bit exists, so code should check against 0 first.
367 static inline unsigned long __fls(unsigned long word)
369 return __flogr(word) ^ (BITS_PER_LONG - 1);
373 * fls64 - find last set bit in a 64-bit word
374 * @word: the word to search
376 * This is defined in a similar way as the libc and compiler builtin
377 * ffsll, but returns the position of the most significant set bit.
379 * fls64(value) returns 0 if value is 0 or the position of the last
380 * set bit if value is nonzero. The last (most significant) bit is
383 static inline int fls64(unsigned long word)
385 unsigned long mask = 2 * BITS_PER_LONG - 1;
387 return (1 + (__flogr(word) ^ (BITS_PER_LONG - 1))) & mask;
391 * fls - find last (most-significant) bit set
392 * @word: the word to search
394 * This is defined the same way as ffs.
395 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
397 static inline int fls(int word)
399 return fls64((unsigned int)word);
402 #else /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
404 #include <asm-generic/bitops/__ffs.h>
405 #include <asm-generic/bitops/ffs.h>
406 #include <asm-generic/bitops/__fls.h>
407 #include <asm-generic/bitops/fls.h>
408 #include <asm-generic/bitops/fls64.h>
410 #endif /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
412 #include <asm-generic/bitops/ffz.h>
413 #include <asm-generic/bitops/find.h>
414 #include <asm-generic/bitops/hweight.h>
415 #include <asm-generic/bitops/sched.h>
416 #include <asm-generic/bitops/le.h>
417 #include <asm-generic/bitops/ext2-atomic-setbit.h>
419 #endif /* _S390_BITOPS_H */