Implement more bit operation functions in the LinuxKPI.

Some minor whitespace nits while at it.

Obtained from:		kmacy @
MFC after:		1 week
Sponsored by:		Mellanox Technologies
This commit is contained in:
Hans Petter Selasky 2017-02-27 14:38:17 +00:00
parent 522f4b2c75
commit 3cfeca84b4

View File

@ -35,6 +35,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/systm.h> #include <sys/systm.h>
#include <sys/errno.h> #include <sys/errno.h>
#include <sys/libkern.h>
#define BIT(nr) (1UL << (nr)) #define BIT(nr) (1UL << (nr))
#define BIT_ULL(nr) (1ULL << (nr)) #define BIT_ULL(nr) (1ULL << (nr))
@ -43,6 +44,7 @@
#else #else
#define BITS_PER_LONG 32 #define BITS_PER_LONG 32
#endif #endif
#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG)) #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
#define BITMAP_LAST_WORD_MASK(n) (~0UL >> (BITS_PER_LONG - (n))) #define BITMAP_LAST_WORD_MASK(n) (~0UL >> (BITS_PER_LONG - (n)))
#define BITS_TO_LONGS(n) howmany((n), BITS_PER_LONG) #define BITS_TO_LONGS(n) howmany((n), BITS_PER_LONG)
@ -51,6 +53,12 @@
#define GENMASK(h, l) (((~0UL) >> (BITS_PER_LONG - (h) - 1)) & ((~0UL) << (l))) #define GENMASK(h, l) (((~0UL) >> (BITS_PER_LONG - (h) - 1)) & ((~0UL) << (l)))
#define BITS_PER_BYTE 8 #define BITS_PER_BYTE 8
#define hweight8(x) bitcount((uint8_t)(x))
#define hweight16(x) bitcount16(x)
#define hweight32(x) bitcount32(x)
#define hweight64(x) bitcount64(x)
#define hweight_long(x) bitcountl(x)
static inline int static inline int
__ffs(int mask) __ffs(int mask)
{ {
@ -75,10 +83,15 @@ __flsl(long mask)
return (flsl(mask) - 1); return (flsl(mask) - 1);
} }
static inline int
fls64(uint64_t mask)
{
return (flsll(mask));
}
static inline uint32_t static inline uint32_t
ror32(uint32_t word, unsigned int shift) ror32(uint32_t word, unsigned int shift)
{ {
return ((word >> shift) | (word << (32 - shift))); return ((word >> shift) | (word << (32 - shift)));
} }
@ -542,4 +555,12 @@ bitmap_equal(const unsigned long *pa,
return (1); return (1);
} }
static inline uint64_t
sign_extend64(uint64_t value, int index)
{
uint8_t shift = 63 - index;
return ((int64_t)(value << shift) >> shift);
}
#endif /* _LINUX_BITOPS_H_ */ #endif /* _LINUX_BITOPS_H_ */