hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/include/linux/bitops.h
....@@ -4,8 +4,18 @@
44 #include <asm/types.h>
55 #include <linux/bits.h>
66
7
-#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
7
+/* Set bits in the first 'n' bytes when loaded from memory */
8
+#ifdef __LITTLE_ENDIAN
9
+# define aligned_byte_mask(n) ((1UL << 8*(n))-1)
10
+#else
11
+# define aligned_byte_mask(n) (~0xffUL << (BITS_PER_LONG - 8 - 8*(n)))
12
+#endif
13
+
14
+#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
815 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(long))
16
+#define BITS_TO_U64(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(u64))
17
+#define BITS_TO_U32(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(u32))
18
+#define BITS_TO_BYTES(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(char))
919
1020 extern unsigned int __sw_hweight8(unsigned int w);
1121 extern unsigned int __sw_hweight16(unsigned int w);
....@@ -39,6 +49,18 @@
3949 for ((bit) = find_next_zero_bit((addr), (size), (bit)); \
4050 (bit) < (size); \
4151 (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
52
+
53
+/**
54
+ * for_each_set_clump8 - iterate over bitmap for each 8-bit clump with set bits
55
+ * @start: bit offset to start search and to store the current iteration offset
56
+ * @clump: location to store copy of current 8-bit clump
57
+ * @bits: bitmap address to base the search on
58
+ * @size: bitmap size in number of bits
59
+ */
60
+#define for_each_set_clump8(start, clump, bits, size) \
61
+ for ((start) = find_first_clump8(&(clump), (bits), (size)); \
62
+ (start) < (size); \
63
+ (start) = find_next_clump8(&(clump), (bits), (size), (start) + 8))
4264
4365 static inline int get_bitmask_order(unsigned int count)
4466 {
....@@ -140,7 +162,7 @@
140162 *
141163 * This is safe to use for 16- and 8-bit types as well.
142164 */
143
-static inline __s32 sign_extend32(__u32 value, int index)
165
+static __always_inline __s32 sign_extend32(__u32 value, int index)
144166 {
145167 __u8 shift = 31 - index;
146168 return (__s32)(value << shift) >> shift;
....@@ -151,7 +173,7 @@
151173 * @value: value to sign extend
152174 * @index: 0 based bit index (0<=index<64) to sign bit
153175 */
154
-static inline __s64 sign_extend64(__u64 value, int index)
176
+static __always_inline __s64 sign_extend64(__u64 value, int index)
155177 {
156178 __u8 shift = 63 - index;
157179 return (__s64)(value << shift) >> shift;
....@@ -166,12 +188,10 @@
166188
167189 static inline int get_count_order(unsigned int count)
168190 {
169
- int order;
191
+ if (count == 0)
192
+ return -1;
170193
171
- order = fls(count) - 1;
172
- if (count & (count - 1))
173
- order++;
174
- return order;
194
+ return fls(--count);
175195 }
176196
177197 /**
....@@ -184,10 +204,7 @@
184204 {
185205 if (l == 0UL)
186206 return -1;
187
- else if (l & (l - 1UL))
188
- return (int)fls_long(l);
189
- else
190
- return (int)fls_long(l) - 1;
207
+ return (int)fls_long(--l);
191208 }
192209
193210 /**
....@@ -246,23 +263,23 @@
246263 new__ = (old__ & ~mask__) | bits__; \
247264 } while (cmpxchg(ptr, old__, new__) != old__); \
248265 \
249
- new__; \
266
+ old__; \
250267 })
251268 #endif
252269
253270 #ifndef bit_clear_unless
254
-#define bit_clear_unless(ptr, _clear, _test) \
271
+#define bit_clear_unless(ptr, clear, test) \
255272 ({ \
256
- const typeof(*ptr) clear = (_clear), test = (_test); \
257
- typeof(*ptr) old, new; \
273
+ const typeof(*(ptr)) clear__ = (clear), test__ = (test);\
274
+ typeof(*(ptr)) old__, new__; \
258275 \
259276 do { \
260
- old = READ_ONCE(*ptr); \
261
- new = old & ~clear; \
262
- } while (!(old & test) && \
263
- cmpxchg(ptr, old, new) != old); \
277
+ old__ = READ_ONCE(*(ptr)); \
278
+ new__ = old__ & ~clear__; \
279
+ } while (!(old__ & test__) && \
280
+ cmpxchg(ptr, old__, new__) != old__); \
264281 \
265
- !(old & test); \
282
+ !(old__ & test__); \
266283 })
267284 #endif
268285