diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h index 54aeeef1f0ec..989c214ec8a5 100644 --- a/include/linux/bitfield.h +++ b/include/linux/bitfield.h @@ -178,6 +178,22 @@ __FIELD_GET(_mask, _reg, "FIELD_GET: "); \ }) +/** + * FIELD_GET_SIGNED() - extract a signed bitfield element + * @mask: shifted mask defining the field's length and position + * @reg: value of entire bitfield + * + * Returns the sign-extended field specified by @_mask from the + * bitfield passed in as @reg by masking and shifting it down. + */ +#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)))); \ + }) + /** * FIELD_MODIFY() - modify a bitfield element * @_mask: shifted mask defining the field's length and position