0001
0002 #ifndef _LINUX_BITOPS_H
0003 #define _LINUX_BITOPS_H
0004
0005 #include <asm/types.h>
0006 #include <linux/bits.h>
0007 #include <linux/typecheck.h>
0008
0009 #include <uapi/linux/kernel.h>
0010
0011
0012 #ifdef __LITTLE_ENDIAN
0013 # define aligned_byte_mask(n) ((1UL << 8*(n))-1)
0014 #else
0015 # define aligned_byte_mask(n) (~0xffUL << (BITS_PER_LONG - 8 - 8*(n)))
0016 #endif
0017
0018 #define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
0019 #define BITS_TO_LONGS(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(long))
0020 #define BITS_TO_U64(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u64))
0021 #define BITS_TO_U32(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u32))
0022 #define BITS_TO_BYTES(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(char))
0023
0024 extern unsigned int __sw_hweight8(unsigned int w);
0025 extern unsigned int __sw_hweight16(unsigned int w);
0026 extern unsigned int __sw_hweight32(unsigned int w);
0027 extern unsigned long __sw_hweight64(__u64 w);
0028
0029
0030
0031
0032
0033
0034 #include <asm-generic/bitops/generic-non-atomic.h>
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 #define bitop(op, nr, addr) \
0049 ((__builtin_constant_p(nr) && \
0050 __builtin_constant_p((uintptr_t)(addr) != (uintptr_t)NULL) && \
0051 (uintptr_t)(addr) != (uintptr_t)NULL && \
0052 __builtin_constant_p(*(const unsigned long *)(addr))) ? \
0053 const##op(nr, addr) : op(nr, addr))
0054
0055 #define __set_bit(nr, addr) bitop(___set_bit, nr, addr)
0056 #define __clear_bit(nr, addr) bitop(___clear_bit, nr, addr)
0057 #define __change_bit(nr, addr) bitop(___change_bit, nr, addr)
0058 #define __test_and_set_bit(nr, addr) bitop(___test_and_set_bit, nr, addr)
0059 #define __test_and_clear_bit(nr, addr) bitop(___test_and_clear_bit, nr, addr)
0060 #define __test_and_change_bit(nr, addr) bitop(___test_and_change_bit, nr, addr)
0061 #define test_bit(nr, addr) bitop(_test_bit, nr, addr)
0062 #define test_bit_acquire(nr, addr) bitop(_test_bit_acquire, nr, addr)
0063
0064
0065
0066
0067
0068 #include <asm/bitops.h>
0069
0070
0071 #define __check_bitop_pr(name) \
0072 static_assert(__same_type(arch_##name, generic_##name) && \
0073 __same_type(const_##name, generic_##name) && \
0074 __same_type(_##name, generic_##name))
0075
0076 __check_bitop_pr(__set_bit);
0077 __check_bitop_pr(__clear_bit);
0078 __check_bitop_pr(__change_bit);
0079 __check_bitop_pr(__test_and_set_bit);
0080 __check_bitop_pr(__test_and_clear_bit);
0081 __check_bitop_pr(__test_and_change_bit);
0082 __check_bitop_pr(test_bit);
0083
0084 #undef __check_bitop_pr
0085
0086 static inline int get_bitmask_order(unsigned int count)
0087 {
0088 int order;
0089
0090 order = fls(count);
0091 return order;
0092 }
0093
0094 static __always_inline unsigned long hweight_long(unsigned long w)
0095 {
0096 return sizeof(w) == 4 ? hweight32(w) : hweight64((__u64)w);
0097 }
0098
0099
0100
0101
0102
0103
0104 static inline __u64 rol64(__u64 word, unsigned int shift)
0105 {
0106 return (word << (shift & 63)) | (word >> ((-shift) & 63));
0107 }
0108
0109
0110
0111
0112
0113
0114 static inline __u64 ror64(__u64 word, unsigned int shift)
0115 {
0116 return (word >> (shift & 63)) | (word << ((-shift) & 63));
0117 }
0118
0119
0120
0121
0122
0123
0124 static inline __u32 rol32(__u32 word, unsigned int shift)
0125 {
0126 return (word << (shift & 31)) | (word >> ((-shift) & 31));
0127 }
0128
0129
0130
0131
0132
0133
0134 static inline __u32 ror32(__u32 word, unsigned int shift)
0135 {
0136 return (word >> (shift & 31)) | (word << ((-shift) & 31));
0137 }
0138
0139
0140
0141
0142
0143
0144 static inline __u16 rol16(__u16 word, unsigned int shift)
0145 {
0146 return (word << (shift & 15)) | (word >> ((-shift) & 15));
0147 }
0148
0149
0150
0151
0152
0153
0154 static inline __u16 ror16(__u16 word, unsigned int shift)
0155 {
0156 return (word >> (shift & 15)) | (word << ((-shift) & 15));
0157 }
0158
0159
0160
0161
0162
0163
0164 static inline __u8 rol8(__u8 word, unsigned int shift)
0165 {
0166 return (word << (shift & 7)) | (word >> ((-shift) & 7));
0167 }
0168
0169
0170
0171
0172
0173
0174 static inline __u8 ror8(__u8 word, unsigned int shift)
0175 {
0176 return (word >> (shift & 7)) | (word << ((-shift) & 7));
0177 }
0178
0179
0180
0181
0182
0183
0184
0185
0186 static __always_inline __s32 sign_extend32(__u32 value, int index)
0187 {
0188 __u8 shift = 31 - index;
0189 return (__s32)(value << shift) >> shift;
0190 }
0191
0192
0193
0194
0195
0196
0197 static __always_inline __s64 sign_extend64(__u64 value, int index)
0198 {
0199 __u8 shift = 63 - index;
0200 return (__s64)(value << shift) >> shift;
0201 }
0202
0203 static inline unsigned fls_long(unsigned long l)
0204 {
0205 if (sizeof(l) == 4)
0206 return fls(l);
0207 return fls64(l);
0208 }
0209
0210 static inline int get_count_order(unsigned int count)
0211 {
0212 if (count == 0)
0213 return -1;
0214
0215 return fls(--count);
0216 }
0217
0218
0219
0220
0221
0222
0223
0224 static inline int get_count_order_long(unsigned long l)
0225 {
0226 if (l == 0UL)
0227 return -1;
0228 return (int)fls_long(--l);
0229 }
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239 static inline unsigned long __ffs64(u64 word)
0240 {
0241 #if BITS_PER_LONG == 32
0242 if (((u32)word) == 0UL)
0243 return __ffs((u32)(word >> 32)) + 32;
0244 #elif BITS_PER_LONG != 64
0245 #error BITS_PER_LONG not 32 or 64
0246 #endif
0247 return __ffs((unsigned long)word);
0248 }
0249
0250
0251
0252
0253
0254
0255
0256 static __always_inline void assign_bit(long nr, volatile unsigned long *addr,
0257 bool value)
0258 {
0259 if (value)
0260 set_bit(nr, addr);
0261 else
0262 clear_bit(nr, addr);
0263 }
0264
0265 static __always_inline void __assign_bit(long nr, volatile unsigned long *addr,
0266 bool value)
0267 {
0268 if (value)
0269 __set_bit(nr, addr);
0270 else
0271 __clear_bit(nr, addr);
0272 }
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283 #define __ptr_set_bit(nr, addr) \
0284 ({ \
0285 typecheck_pointer(*(addr)); \
0286 __set_bit(nr, (unsigned long *)(addr)); \
0287 })
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298 #define __ptr_clear_bit(nr, addr) \
0299 ({ \
0300 typecheck_pointer(*(addr)); \
0301 __clear_bit(nr, (unsigned long *)(addr)); \
0302 })
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317 #define __ptr_test_bit(nr, addr) \
0318 ({ \
0319 typecheck_pointer(*(addr)); \
0320 test_bit(nr, (unsigned long *)(addr)); \
0321 })
0322
0323 #ifdef __KERNEL__
0324
0325 #ifndef set_mask_bits
0326 #define set_mask_bits(ptr, mask, bits) \
0327 ({ \
0328 const typeof(*(ptr)) mask__ = (mask), bits__ = (bits); \
0329 typeof(*(ptr)) old__, new__; \
0330 \
0331 do { \
0332 old__ = READ_ONCE(*(ptr)); \
0333 new__ = (old__ & ~mask__) | bits__; \
0334 } while (cmpxchg(ptr, old__, new__) != old__); \
0335 \
0336 old__; \
0337 })
0338 #endif
0339
0340 #ifndef bit_clear_unless
0341 #define bit_clear_unless(ptr, clear, test) \
0342 ({ \
0343 const typeof(*(ptr)) clear__ = (clear), test__ = (test);\
0344 typeof(*(ptr)) old__, new__; \
0345 \
0346 do { \
0347 old__ = READ_ONCE(*(ptr)); \
0348 new__ = old__ & ~clear__; \
0349 } while (!(old__ & test__) && \
0350 cmpxchg(ptr, old__, new__) != old__); \
0351 \
0352 !(old__ & test__); \
0353 })
0354 #endif
0355
0356 #endif
0357 #endif