123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
#ifndef _LINUX_BITFIELD_H
#define _LINUX_BITFIELD_H
#include <linux/build_bug.h>
#include <linux/compiler.h>
#include <linux/typecheck.h>
#include <linux/bitfield-fix-width.h>
#define __bf_shf __builtin_ctzll
#define __scalar_type_to_unsigned_cases(type) \
unsigned type: (unsigned type)0, \
signed type: (unsigned type)0
#define __unsigned_scalar_typeof(x) typeof( \
_Generic((x), \
char: (unsigned char)0, \
__scalar_type_to_unsigned_cases(char), \
__scalar_type_to_unsigned_cases(short), \
__scalar_type_to_unsigned_cases(int), \
__scalar_type_to_unsigned_cases(long), \
__scalar_type_to_unsigned_cases(long long), \
default: (x)))
#define __bf_cast_unsigned(type, x) ((__unsigned_scalar_typeof(type))(x))
#define __BF_FIELD_CHECK_MASK(_mask, _val, _pfx) \
({ \
BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \
_pfx "mask is not constant"); \
BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero"); \
BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \
~((_mask) >> __bf_shf(_mask)) & \
(0 + (_val)) : 0, \
_pfx "value too large for the field"); \
__BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \
(1ULL << __bf_shf(_mask))); \
})
#define __BF_FIELD_CHECK_REG(mask, reg, pfx) \
BUILD_BUG_ON_MSG(__bf_cast_unsigned(mask, mask) > \
__bf_cast_unsigned(reg, ~0ull), \
pfx "type of reg too small for mask")
#define __BF_FIELD_CHECK(mask, reg, val, pfx) \
({ \
__BF_FIELD_CHECK_MASK(mask, val, pfx); \
__BF_FIELD_CHECK_REG(mask, reg, pfx); \
})
#define __FIELD_PREP(mask, val, pfx) \
({ \
__BF_FIELD_CHECK_MASK(mask, val, pfx); \
((typeof(mask))(val) << __bf_shf(mask)) & (mask); \
})
#define __FIELD_GET(mask, reg, pfx) \
({ \
__BF_FIELD_CHECK_MASK(mask, 0U, pfx); \
(typeof(mask))(((reg) & (mask)) >> __bf_shf(mask)); \
})
#define FIELD_MAX(_mask) \
({ \
__BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_MAX: "); \
(typeof(_mask))((_mask) >> __bf_shf(_mask)); \
})
#define FIELD_FIT(_mask, _val) \
({ \
__BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: "); \
!((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
})
#define FIELD_PREP(_mask, _val) \
({ \
__BF_FIELD_CHECK_REG(_mask, 0ULL, "FIELD_PREP: "); \
__FIELD_PREP(_mask, _val, "FIELD_PREP: "); \
})
#define __BF_CHECK_POW2(n) BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) != 0)
#define FIELD_PREP_CONST(_mask, _val) \
( \
/* mask must be non-zero */ \
BUILD_BUG_ON_ZERO((_mask) == 0) + \
/* check if value fits */ \
BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \
/* check if mask is contiguous */ \
__BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask))) + \
/* and create the value */ \
(((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask)) \
)
#define FIELD_GET(_mask, _reg) \
({ \
__BF_FIELD_CHECK_REG(_mask, _reg, "FIELD_GET: "); \
__FIELD_GET(_mask, _reg, "FIELD_GET: "); \
})
#define FIELD_GET_SIGNED(mask, reg) \
({ \
__BF_FIELD_CHECK(mask, reg, 0U, "FIELD_GET_SIGNED: "); \
((__signed_scalar_typeof(mask)) \
(((long long)(reg) << __builtin_clzll(mask)) >> \
(__builtin_clzll(mask) + __builtin_ctzll(mask)))); \
})
#define FIELD_MODIFY(_mask, _reg_p, _val) \
({ \
typecheck_pointer(_reg_p); \
__BF_FIELD_CHECK(_mask, *(_reg_p), _val, "FIELD_MODIFY: "); \
*(_reg_p) &= ~(_mask); \
*(_reg_p) |= (((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask)); \
})
#define __field_prep(mask, val) \
({ \
auto __mask = (mask); \
typeof(__mask) __val = (val); \
unsigned int __shift = BITS_PER_TYPE(__mask) <= 32 ? \
__ffs(__mask) : __ffs64(__mask); \
(__val << __shift) & __mask; \
})
#define __field_get(mask, reg) \
({ \
auto __mask = (mask); \
typeof(__mask) __reg = (reg); \
unsigned int __shift = BITS_PER_TYPE(__mask) <= 32 ? \
__ffs(__mask) : __ffs64(__mask); \
(__reg & __mask) >> __shift; \
})
#define field_prep(mask, val) \
(__builtin_constant_p(mask) ? __FIELD_PREP(mask, val, "field_prep: ") \
: __field_prep(mask, val))
#define field_get(mask, reg) \
(__builtin_constant_p(mask) ? __FIELD_GET(mask, reg, "field_get: ") \
: __field_get(mask, reg))
#endif