Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
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 /* Set bits in the first 'n' bytes when loaded from memory */
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  * Defined here because those may be needed by architecture-specific static
0031  * inlines.
0032  */
0033 
0034 #include <asm-generic/bitops/generic-non-atomic.h>
0035 
0036 /*
0037  * Many architecture-specific non-atomic bitops contain inline asm code and due
0038  * to that the compiler can't optimize them to compile-time expressions or
0039  * constants. In contrary, generic_*() helpers are defined in pure C and
0040  * compilers optimize them just well.
0041  * Therefore, to make `unsigned long foo = 0; __set_bit(BAR, &foo)` effectively
0042  * equal to `unsigned long foo = BIT(BAR)`, pick the generic C alternative when
0043  * the arguments can be resolved at compile time. That expression itself is a
0044  * constant and doesn't bring any functional changes to the rest of cases.
0045  * The casts to `uintptr_t` are needed to mitigate `-Waddress` warnings when
0046  * passing a bitmap from .bss or .data (-> `!!addr` is always true).
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  * Include this here because some architectures need generic_ffs/fls in
0066  * scope
0067  */
0068 #include <asm/bitops.h>
0069 
0070 /* Check that the bitops prototypes are sane */
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;   /* We could be slightly more clever with -1 here... */
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  * rol64 - rotate a 64-bit value left
0101  * @word: value to rotate
0102  * @shift: bits to roll
0103  */
0104 static inline __u64 rol64(__u64 word, unsigned int shift)
0105 {
0106     return (word << (shift & 63)) | (word >> ((-shift) & 63));
0107 }
0108 
0109 /**
0110  * ror64 - rotate a 64-bit value right
0111  * @word: value to rotate
0112  * @shift: bits to roll
0113  */
0114 static inline __u64 ror64(__u64 word, unsigned int shift)
0115 {
0116     return (word >> (shift & 63)) | (word << ((-shift) & 63));
0117 }
0118 
0119 /**
0120  * rol32 - rotate a 32-bit value left
0121  * @word: value to rotate
0122  * @shift: bits to roll
0123  */
0124 static inline __u32 rol32(__u32 word, unsigned int shift)
0125 {
0126     return (word << (shift & 31)) | (word >> ((-shift) & 31));
0127 }
0128 
0129 /**
0130  * ror32 - rotate a 32-bit value right
0131  * @word: value to rotate
0132  * @shift: bits to roll
0133  */
0134 static inline __u32 ror32(__u32 word, unsigned int shift)
0135 {
0136     return (word >> (shift & 31)) | (word << ((-shift) & 31));
0137 }
0138 
0139 /**
0140  * rol16 - rotate a 16-bit value left
0141  * @word: value to rotate
0142  * @shift: bits to roll
0143  */
0144 static inline __u16 rol16(__u16 word, unsigned int shift)
0145 {
0146     return (word << (shift & 15)) | (word >> ((-shift) & 15));
0147 }
0148 
0149 /**
0150  * ror16 - rotate a 16-bit value right
0151  * @word: value to rotate
0152  * @shift: bits to roll
0153  */
0154 static inline __u16 ror16(__u16 word, unsigned int shift)
0155 {
0156     return (word >> (shift & 15)) | (word << ((-shift) & 15));
0157 }
0158 
0159 /**
0160  * rol8 - rotate an 8-bit value left
0161  * @word: value to rotate
0162  * @shift: bits to roll
0163  */
0164 static inline __u8 rol8(__u8 word, unsigned int shift)
0165 {
0166     return (word << (shift & 7)) | (word >> ((-shift) & 7));
0167 }
0168 
0169 /**
0170  * ror8 - rotate an 8-bit value right
0171  * @word: value to rotate
0172  * @shift: bits to roll
0173  */
0174 static inline __u8 ror8(__u8 word, unsigned int shift)
0175 {
0176     return (word >> (shift & 7)) | (word << ((-shift) & 7));
0177 }
0178 
0179 /**
0180  * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
0181  * @value: value to sign extend
0182  * @index: 0 based bit index (0<=index<32) to sign bit
0183  *
0184  * This is safe to use for 16- and 8-bit types as well.
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  * sign_extend64 - sign extend a 64-bit value using specified bit as sign-bit
0194  * @value: value to sign extend
0195  * @index: 0 based bit index (0<=index<64) to sign bit
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  * get_count_order_long - get order after rounding @l up to power of 2
0220  * @l: parameter
0221  *
0222  * it is same as get_count_order() but with long type parameter
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  * __ffs64 - find first set bit in a 64 bit word
0233  * @word: The 64 bit word
0234  *
0235  * On 64 bit arches this is a synonym for __ffs
0236  * The result is not defined if no bits are set, so check that @word
0237  * is non-zero before calling this.
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  * assign_bit - Assign value to a bit in memory
0252  * @nr: the bit to set
0253  * @addr: the address to start counting from
0254  * @value: the value to assign
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  * __ptr_set_bit - Set bit in a pointer's value
0276  * @nr: the bit to set
0277  * @addr: the address of the pointer variable
0278  *
0279  * Example:
0280  *  void *p = foo();
0281  *  __ptr_set_bit(bit, &p);
0282  */
0283 #define __ptr_set_bit(nr, addr)                         \
0284     ({                                              \
0285         typecheck_pointer(*(addr));             \
0286         __set_bit(nr, (unsigned long *)(addr)); \
0287     })
0288 
0289 /**
0290  * __ptr_clear_bit - Clear bit in a pointer's value
0291  * @nr: the bit to clear
0292  * @addr: the address of the pointer variable
0293  *
0294  * Example:
0295  *  void *p = foo();
0296  *  __ptr_clear_bit(bit, &p);
0297  */
0298 #define __ptr_clear_bit(nr, addr)                         \
0299     ({                                                \
0300         typecheck_pointer(*(addr));               \
0301         __clear_bit(nr, (unsigned long *)(addr)); \
0302     })
0303 
0304 /**
0305  * __ptr_test_bit - Test bit in a pointer's value
0306  * @nr: the bit to test
0307  * @addr: the address of the pointer variable
0308  *
0309  * Example:
0310  *  void *p = foo();
0311  *  if (__ptr_test_bit(bit, &p)) {
0312  *          ...
0313  *  } else {
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 /* __KERNEL__ */
0357 #endif