1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright IBM Corp. 1999,2013
7 * The description below was taken in large parts from the powerpc
9 * Within a word, bits are numbered LSB first. Lot's of places make
10 * this assumption by directly testing bits with (val & (1<<nr)).
11 * This can cause confusion for large (> 1 word) bitmaps on a
12 * big-endian system because, unlike little endian, the number of each
13 * bit depends on the word size.
15 * The bitop functions are defined to work on unsigned longs, so the bits
17 * |63..............0|127............64|191...........128|255...........192|
19 * We also have special functions which work with an MSB0 encoding.
20 * The bits are numbered:
21 * |0..............63|64............127|128...........191|192...........255|
23 * The main difference is that bit 0-63 in the bit number field needs to be
24 * reversed compared to the LSB0 encoded bit fields. This can be achieved by
29 #ifndef _S390_BITOPS_H
30 #define _S390_BITOPS_H
32 #ifndef _LINUX_BITOPS_H
33 #error only <linux/bitops.h> can be included directly
36 #include <linux/typecheck.h>
37 #include <linux/compiler.h>
38 #include <linux/types.h>
39 #include <asm/atomic_ops.h>
40 #include <asm/barrier.h>
42 #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG)
44 static inline unsigned long *
45 __bitops_word(unsigned long nr, const volatile unsigned long *ptr)
49 addr = (unsigned long)ptr + ((nr ^ (nr & (BITS_PER_LONG - 1))) >> 3);
50 return (unsigned long *)addr;
53 static inline unsigned long __bitops_mask(unsigned long nr)
55 return 1UL << (nr & (BITS_PER_LONG - 1));
58 static __always_inline void arch_set_bit(unsigned long nr, volatile unsigned long *ptr)
60 unsigned long *addr = __bitops_word(nr, ptr);
61 unsigned long mask = __bitops_mask(nr);
63 __atomic64_or(mask, (long *)addr);
66 static __always_inline void arch_clear_bit(unsigned long nr, volatile unsigned long *ptr)
68 unsigned long *addr = __bitops_word(nr, ptr);
69 unsigned long mask = __bitops_mask(nr);
71 __atomic64_and(~mask, (long *)addr);
74 static __always_inline void arch_change_bit(unsigned long nr,
75 volatile unsigned long *ptr)
77 unsigned long *addr = __bitops_word(nr, ptr);
78 unsigned long mask = __bitops_mask(nr);
80 __atomic64_xor(mask, (long *)addr);
83 static inline bool arch_test_and_set_bit(unsigned long nr,
84 volatile unsigned long *ptr)
86 unsigned long *addr = __bitops_word(nr, ptr);
87 unsigned long mask = __bitops_mask(nr);
90 old = __atomic64_or_barrier(mask, (long *)addr);
94 static inline bool arch_test_and_clear_bit(unsigned long nr,
95 volatile unsigned long *ptr)
97 unsigned long *addr = __bitops_word(nr, ptr);
98 unsigned long mask = __bitops_mask(nr);
101 old = __atomic64_and_barrier(~mask, (long *)addr);
105 static inline bool arch_test_and_change_bit(unsigned long nr,
106 volatile unsigned long *ptr)
108 unsigned long *addr = __bitops_word(nr, ptr);
109 unsigned long mask = __bitops_mask(nr);
112 old = __atomic64_xor_barrier(mask, (long *)addr);
116 static __always_inline void
117 arch___set_bit(unsigned long nr, volatile unsigned long *addr)
119 unsigned long *p = __bitops_word(nr, addr);
120 unsigned long mask = __bitops_mask(nr);
125 static __always_inline void
126 arch___clear_bit(unsigned long nr, volatile unsigned long *addr)
128 unsigned long *p = __bitops_word(nr, addr);
129 unsigned long mask = __bitops_mask(nr);
134 static __always_inline void
135 arch___change_bit(unsigned long nr, volatile unsigned long *addr)
137 unsigned long *p = __bitops_word(nr, addr);
138 unsigned long mask = __bitops_mask(nr);
143 static __always_inline bool
144 arch___test_and_set_bit(unsigned long nr, volatile unsigned long *addr)
146 unsigned long *p = __bitops_word(nr, addr);
147 unsigned long mask = __bitops_mask(nr);
155 static __always_inline bool
156 arch___test_and_clear_bit(unsigned long nr, volatile unsigned long *addr)
158 unsigned long *p = __bitops_word(nr, addr);
159 unsigned long mask = __bitops_mask(nr);
167 static __always_inline bool
168 arch___test_and_change_bit(unsigned long nr, volatile unsigned long *addr)
170 unsigned long *p = __bitops_word(nr, addr);
171 unsigned long mask = __bitops_mask(nr);
179 #define arch_test_bit generic_test_bit
180 #define arch_test_bit_acquire generic_test_bit_acquire
182 static inline bool arch_test_and_set_bit_lock(unsigned long nr,
183 volatile unsigned long *ptr)
185 if (arch_test_bit(nr, ptr))
187 return arch_test_and_set_bit(nr, ptr);
190 static inline void arch_clear_bit_unlock(unsigned long nr,
191 volatile unsigned long *ptr)
193 smp_mb__before_atomic();
194 arch_clear_bit(nr, ptr);
197 static inline void arch___clear_bit_unlock(unsigned long nr,
198 volatile unsigned long *ptr)
201 arch___clear_bit(nr, ptr);
204 #include <asm-generic/bitops/instrumented-atomic.h>
205 #include <asm-generic/bitops/instrumented-non-atomic.h>
206 #include <asm-generic/bitops/instrumented-lock.h>
209 * Functions which use MSB0 bit numbering.
210 * The bits are numbered:
211 * |0..............63|64............127|128...........191|192...........255|
213 unsigned long find_first_bit_inv(const unsigned long *addr, unsigned long size);
214 unsigned long find_next_bit_inv(const unsigned long *addr, unsigned long size,
215 unsigned long offset);
217 #define for_each_set_bit_inv(bit, addr, size) \
218 for ((bit) = find_first_bit_inv((addr), (size)); \
220 (bit) = find_next_bit_inv((addr), (size), (bit) + 1))
222 static inline void set_bit_inv(unsigned long nr, volatile unsigned long *ptr)
224 return set_bit(nr ^ (BITS_PER_LONG - 1), ptr);
227 static inline void clear_bit_inv(unsigned long nr, volatile unsigned long *ptr)
229 return clear_bit(nr ^ (BITS_PER_LONG - 1), ptr);
232 static inline bool test_and_clear_bit_inv(unsigned long nr,
233 volatile unsigned long *ptr)
235 return test_and_clear_bit(nr ^ (BITS_PER_LONG - 1), ptr);
238 static inline void __set_bit_inv(unsigned long nr, volatile unsigned long *ptr)
240 return __set_bit(nr ^ (BITS_PER_LONG - 1), ptr);
243 static inline void __clear_bit_inv(unsigned long nr, volatile unsigned long *ptr)
245 return __clear_bit(nr ^ (BITS_PER_LONG - 1), ptr);
248 static inline bool test_bit_inv(unsigned long nr,
249 const volatile unsigned long *ptr)
251 return test_bit(nr ^ (BITS_PER_LONG - 1), ptr);
255 * __flogr - find leftmost one
256 * @word - The word to search
258 * Returns the bit number of the most significant bit set,
259 * where the most significant bit has bit number 0.
260 * If no bit is set this function returns 64.
262 static inline unsigned char __flogr(unsigned long word)
264 if (__builtin_constant_p(word)) {
265 unsigned long bit = 0;
269 if (!(word & 0xffffffff00000000UL)) {
273 if (!(word & 0xffff000000000000UL)) {
277 if (!(word & 0xff00000000000000UL)) {
281 if (!(word & 0xf000000000000000UL)) {
285 if (!(word & 0xc000000000000000UL)) {
289 if (!(word & 0x8000000000000000UL)) {
295 union register_pair rp;
299 " flogr %[rp],%[rp]\n"
300 : [rp] "+d" (rp.pair) : : "cc");
306 * __ffs - find first bit in word.
307 * @word: The word to search
309 * Undefined if no bit exists, so code should check against 0 first.
311 static inline unsigned long __ffs(unsigned long word)
313 return __flogr(-word & word) ^ (BITS_PER_LONG - 1);
317 * ffs - find first bit set
318 * @word: the word to search
320 * This is defined the same way as the libc and
321 * compiler builtin ffs routines (man ffs).
323 static inline int ffs(int word)
325 unsigned long mask = 2 * BITS_PER_LONG - 1;
326 unsigned int val = (unsigned int)word;
328 return (1 + (__flogr(-val & val) ^ (BITS_PER_LONG - 1))) & mask;
332 * __fls - find last (most-significant) set bit in a long word
333 * @word: the word to search
335 * Undefined if no set bit exists, so code should check against 0 first.
337 static inline unsigned long __fls(unsigned long word)
339 return __flogr(word) ^ (BITS_PER_LONG - 1);
343 * fls64 - find last set bit in a 64-bit word
344 * @word: the word to search
346 * This is defined in a similar way as the libc and compiler builtin
347 * ffsll, but returns the position of the most significant set bit.
349 * fls64(value) returns 0 if value is 0 or the position of the last
350 * set bit if value is nonzero. The last (most significant) bit is
353 static inline int fls64(unsigned long word)
355 unsigned long mask = 2 * BITS_PER_LONG - 1;
357 return (1 + (__flogr(word) ^ (BITS_PER_LONG - 1))) & mask;
361 * fls - find last (most-significant) bit set
362 * @word: the word to search
364 * This is defined the same way as ffs.
365 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
367 static inline int fls(unsigned int word)
372 #include <asm-generic/bitops/ffz.h>
373 #include <asm-generic/bitops/hweight.h>
374 #include <asm-generic/bitops/sched.h>
375 #include <asm-generic/bitops/le.h>
376 #include <asm-generic/bitops/ext2-atomic-setbit.h>
378 #endif /* _S390_BITOPS_H */