1. include/linux/bits.h GENMASK(h, l)
/*
* Create a contiguous bitmask starting at bit position @l and ending at
* position @h. For example
* GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
*/
#define GENMASK(h, l) (((~UL(0)) - (UL(1) << (l)) + 1) & \ ||1
(~UL(0) >> (BITS_PER_LONG - 1 - (h)))) ||2
Exp. (6,4) of 0x1111 1111
1-->0x1111 1111 - (0x1<<4) = 0x1110 1111 + 1 = 0x1111 0000
2-->0x1111 1111 >> (8-1-6) = 0x0111 1111
1&2 = 0x0111 0000
2. include/linux/bitfield.h
/*
* Bitfield access macros
*
* FIELD_{GET,PREP} macros take as first parameter shifted mask
* from which they extract the base mask and shift amount.
* Mask must be a compilation time constant.
*
* Example:
*
* #define REG_FIELD_A GENMASK(6, 0)
* #define REG_FIELD_B BIT(7)
* #define REG_FIELD_C GENMASK(15, 8)
* #define REG_FIELD_D GENMASK(31, 16)
*
* Get:
* a = FIELD_GET(REG_FIELD_A, reg);
* b = FIELD_GET(REG_FIELD_B, reg);
*
* Set:
* reg = FIELD_PREP(REG_FIELD_A, 1) |
* FIELD_PREP(REG_FIELD_B, 0) |
* FIELD_PREP(REG_FIELD_C, c) |
* FIELD_PREP(REG_FIELD_D, 0x40);
*
* Modify:
* reg &= ~REG_FIELD_C;
* reg |= FIELD_PREP(REG_FIELD_C, c);
*/
#define __bf_shf(x) (__builtin_ffsll(x) - 1)
Linux "yin"才们的奇"yin"小技巧 --请用东北发音夸他们
原文:https://www.cnblogs.com/feliz/p/11858561.html