blob: 602af23b98c7856af4c7ee0473630b4866b3e54d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _LINUX_BITOPS_H
3#define _LINUX_BITOPS_H
4#include <asm/types.h>
Will Deacon8bd9cb52018-06-19 13:53:08 +01005#include <linux/bits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
Chris Wilson9144d752018-08-21 21:57:03 -07007#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
8#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(long))
Chen, Gong10ef6b02013-10-18 14:29:07 -07009
Borislav Petkov4677d4a2010-05-03 14:57:11 +020010extern unsigned int __sw_hweight8(unsigned int w);
11extern unsigned int __sw_hweight16(unsigned int w);
12extern unsigned int __sw_hweight32(unsigned int w);
13extern unsigned long __sw_hweight64(__u64 w);
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * Include this here because some architectures need generic_ffs/fls in
17 * scope
18 */
19#include <asm/bitops.h>
20
Akinobu Mita984b3f52010-03-05 13:41:37 -080021#define for_each_set_bit(bit, addr, size) \
Robert Richter1e2ad282011-11-18 12:35:21 +010022 for ((bit) = find_first_bit((addr), (size)); \
23 (bit) < (size); \
24 (bit) = find_next_bit((addr), (size), (bit) + 1))
25
26/* same as for_each_set_bit() but use bit as value to start with */
Akinobu Mita307b1cd2012-03-23 15:02:03 -070027#define for_each_set_bit_from(bit, addr, size) \
Robert Richter1e2ad282011-11-18 12:35:21 +010028 for ((bit) = find_next_bit((addr), (size), (bit)); \
29 (bit) < (size); \
Shannon Nelson3e037452007-10-16 01:27:40 -070030 (bit) = find_next_bit((addr), (size), (bit) + 1))
31
Akinobu Mita03f4a822012-03-23 15:02:04 -070032#define for_each_clear_bit(bit, addr, size) \
33 for ((bit) = find_first_zero_bit((addr), (size)); \
34 (bit) < (size); \
35 (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
36
37/* same as for_each_clear_bit() but use bit as value to start with */
38#define for_each_clear_bit_from(bit, addr, size) \
39 for ((bit) = find_next_zero_bit((addr), (size), (bit)); \
40 (bit) < (size); \
41 (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
42
Denys Vlasenko1a1d48a2015-08-04 16:15:14 +020043static inline int get_bitmask_order(unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 int order;
Peter Zijlstra9f416992010-01-22 15:59:29 +010046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 order = fls(count);
48 return order; /* We could be slightly more clever with -1 here... */
49}
50
Denys Vlasenko1a1d48a2015-08-04 16:15:14 +020051static __always_inline unsigned long hweight_long(unsigned long w)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Akinobu Mitae9bebd62006-03-26 01:39:55 -080053 return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Robert P. J. Day45f8bde2007-01-26 00:57:09 -080056/**
Alexey Dobriyanf2ea0f52012-01-14 21:44:49 +030057 * rol64 - rotate a 64-bit value left
58 * @word: value to rotate
59 * @shift: bits to roll
60 */
61static inline __u64 rol64(__u64 word, unsigned int shift)
62{
63 return (word << shift) | (word >> (64 - shift));
64}
65
66/**
67 * ror64 - rotate a 64-bit value right
68 * @word: value to rotate
69 * @shift: bits to roll
70 */
71static inline __u64 ror64(__u64 word, unsigned int shift)
72{
73 return (word >> shift) | (word << (64 - shift));
74}
75
76/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * rol32 - rotate a 32-bit value left
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * @word: value to rotate
79 * @shift: bits to roll
80 */
81static inline __u32 rol32(__u32 word, unsigned int shift)
82{
Sasha Levind7e35df2015-12-03 22:04:01 -050083 return (word << shift) | (word >> ((-shift) & 31));
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Robert P. J. Day45f8bde2007-01-26 00:57:09 -080086/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * ror32 - rotate a 32-bit value right
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * @word: value to rotate
89 * @shift: bits to roll
90 */
91static inline __u32 ror32(__u32 word, unsigned int shift)
92{
93 return (word >> shift) | (word << (32 - shift));
94}
95
Harvey Harrison3afe3922008-03-28 14:16:01 -070096/**
97 * rol16 - rotate a 16-bit value left
98 * @word: value to rotate
99 * @shift: bits to roll
100 */
101static inline __u16 rol16(__u16 word, unsigned int shift)
102{
103 return (word << shift) | (word >> (16 - shift));
104}
105
106/**
107 * ror16 - rotate a 16-bit value right
108 * @word: value to rotate
109 * @shift: bits to roll
110 */
111static inline __u16 ror16(__u16 word, unsigned int shift)
112{
113 return (word >> shift) | (word << (16 - shift));
114}
115
116/**
117 * rol8 - rotate an 8-bit value left
118 * @word: value to rotate
119 * @shift: bits to roll
120 */
121static inline __u8 rol8(__u8 word, unsigned int shift)
122{
123 return (word << shift) | (word >> (8 - shift));
124}
125
126/**
127 * ror8 - rotate an 8-bit value right
128 * @word: value to rotate
129 * @shift: bits to roll
130 */
131static inline __u8 ror8(__u8 word, unsigned int shift)
132{
133 return (word >> shift) | (word << (8 - shift));
134}
135
Andreas Herrmann7919a572010-08-30 19:04:01 +0000136/**
137 * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
138 * @value: value to sign extend
139 * @index: 0 based bit index (0<=index<32) to sign bit
Martin Kepplingere2eb53a2015-11-06 16:30:58 -0800140 *
141 * This is safe to use for 16- and 8-bit types as well.
Andreas Herrmann7919a572010-08-30 19:04:01 +0000142 */
143static inline __s32 sign_extend32(__u32 value, int index)
144{
145 __u8 shift = 31 - index;
146 return (__s32)(value << shift) >> shift;
147}
148
Martin Kepplinger48e203e2015-11-06 16:31:02 -0800149/**
150 * sign_extend64 - sign extend a 64-bit value using specified bit as sign-bit
151 * @value: value to sign extend
152 * @index: 0 based bit index (0<=index<64) to sign bit
153 */
154static inline __s64 sign_extend64(__u64 value, int index)
155{
156 __u8 shift = 63 - index;
157 return (__s64)(value << shift) >> shift;
158}
159
Andrew Morton962749a2006-03-25 03:08:01 -0800160static inline unsigned fls_long(unsigned long l)
161{
162 if (sizeof(l) == 4)
163 return fls(l);
164 return fls64(l);
165}
166
zijun_hu252e5c62016-10-07 16:57:26 -0700167static inline int get_count_order(unsigned int count)
168{
169 int order;
170
171 order = fls(count) - 1;
172 if (count & (count - 1))
173 order++;
174 return order;
175}
176
177/**
178 * get_count_order_long - get order after rounding @l up to power of 2
179 * @l: parameter
180 *
181 * it is same as get_count_order() but with long type parameter
182 */
183static inline int get_count_order_long(unsigned long l)
184{
185 if (l == 0UL)
186 return -1;
187 else if (l & (l - 1UL))
188 return (int)fls_long(l);
189 else
190 return (int)fls_long(l) - 1;
191}
192
Steven Whitehouse952043a2009-04-23 08:48:15 +0100193/**
194 * __ffs64 - find first set bit in a 64 bit word
195 * @word: The 64 bit word
196 *
197 * On 64 bit arches this is a synomyn for __ffs
198 * The result is not defined if no bits are set, so check that @word
199 * is non-zero before calling this.
200 */
201static inline unsigned long __ffs64(u64 word)
202{
203#if BITS_PER_LONG == 32
204 if (((u32)word) == 0UL)
205 return __ffs((u32)(word >> 32)) + 32;
206#elif BITS_PER_LONG != 64
207#error BITS_PER_LONG not 32 or 64
208#endif
209 return __ffs((unsigned long)word);
210}
211
Lukas Wunner5307e2a2017-10-12 12:40:10 +0200212/**
213 * assign_bit - Assign value to a bit in memory
214 * @nr: the bit to set
215 * @addr: the address to start counting from
216 * @value: the value to assign
217 */
218static __always_inline void assign_bit(long nr, volatile unsigned long *addr,
219 bool value)
220{
221 if (value)
222 set_bit(nr, addr);
223 else
224 clear_bit(nr, addr);
225}
226
227static __always_inline void __assign_bit(long nr, volatile unsigned long *addr,
228 bool value)
229{
230 if (value)
231 __set_bit(nr, addr);
232 else
233 __clear_bit(nr, addr);
234}
235
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100236#ifdef __KERNEL__
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200237
Theodore Ts'o00a1a052014-03-30 10:20:01 -0400238#ifndef set_mask_bits
Miklos Szeredi18127422018-10-15 15:43:06 +0200239#define set_mask_bits(ptr, mask, bits) \
Theodore Ts'o00a1a052014-03-30 10:20:01 -0400240({ \
Miklos Szeredi18127422018-10-15 15:43:06 +0200241 const typeof(*(ptr)) mask__ = (mask), bits__ = (bits); \
242 typeof(*(ptr)) old__, new__; \
Theodore Ts'o00a1a052014-03-30 10:20:01 -0400243 \
244 do { \
Miklos Szeredi18127422018-10-15 15:43:06 +0200245 old__ = READ_ONCE(*(ptr)); \
246 new__ = (old__ & ~mask__) | bits__; \
247 } while (cmpxchg(ptr, old__, new__) != old__); \
Theodore Ts'o00a1a052014-03-30 10:20:01 -0400248 \
Vineet Gupta1db604f2019-03-07 16:28:14 -0800249 old__; \
Theodore Ts'o00a1a052014-03-30 10:20:01 -0400250})
251#endif
252
Guoqing Jiang85ad1d12016-05-03 22:22:13 -0400253#ifndef bit_clear_unless
Miklos Szerediedfa8722018-10-15 15:43:06 +0200254#define bit_clear_unless(ptr, clear, test) \
Guoqing Jiang85ad1d12016-05-03 22:22:13 -0400255({ \
Miklos Szerediedfa8722018-10-15 15:43:06 +0200256 const typeof(*(ptr)) clear__ = (clear), test__ = (test);\
257 typeof(*(ptr)) old__, new__; \
Guoqing Jiang85ad1d12016-05-03 22:22:13 -0400258 \
259 do { \
Miklos Szerediedfa8722018-10-15 15:43:06 +0200260 old__ = READ_ONCE(*(ptr)); \
261 new__ = old__ & ~clear__; \
262 } while (!(old__ & test__) && \
263 cmpxchg(ptr, old__, new__) != old__); \
Guoqing Jiang85ad1d12016-05-03 22:22:13 -0400264 \
Miklos Szerediedfa8722018-10-15 15:43:06 +0200265 !(old__ & test__); \
Guoqing Jiang85ad1d12016-05-03 22:22:13 -0400266})
267#endif
268
Akinobu Mita19de85e2011-05-26 16:26:09 -0700269#ifndef find_last_bit
Rusty Russellab53d472009-01-01 10:12:19 +1030270/**
271 * find_last_bit - find the last set bit in a memory region
272 * @addr: The address to start the search at
Yury Norov2c57a0e2015-04-16 12:43:13 -0700273 * @size: The number of bits to search
Rusty Russellab53d472009-01-01 10:12:19 +1030274 *
Yury Norov2c57a0e2015-04-16 12:43:13 -0700275 * Returns the bit number of the last set bit, or size.
Rusty Russellab53d472009-01-01 10:12:19 +1030276 */
277extern unsigned long find_last_bit(const unsigned long *addr,
278 unsigned long size);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700279#endif
Rusty Russellab53d472009-01-01 10:12:19 +1030280
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100281#endif /* __KERNEL__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282#endif