Re: [PATCH v9 2/5] include/linux_scaffold: add FIELD_GET() bitfield helper

Matt Roper
2025-12-31T17:00:47Z

On Wed, Dec 31, 2025 at 05:55:22AM +0000, Xin Wang wrote:
> Add __bf_shf() and FIELD_GET() so code can extract bitfields from
> registers using a mask, matching the common Linux FIELD_GET()
> semantics without pulling in kernel headers.
>
> Signed-off-by: Xin Wang <x.wang@intel.com>
> ---
> include/linux_scaffold.h | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/include/linux_scaffold.h b/include/linux_scaffold.h
> index f6620ad0a..bd69c1ed2 100644
> --- a/include/linux_scaffold.h
> +++ b/include/linux_scaffold.h
> @@ -51,4 +51,9 @@ static inline s64 sign_extend64(u64 value, int index)
> #define BITS_PER_LONG BITS_PER_TYPE(long)
> #define BITS_PER_LONG_LONG BITS_PER_TYPE(long long)
>
> +#define __bf_shf(x) (__builtin_ffsll(x) - 1)

Since this is userspace code, shouldn't we just use ffsll() from libc
rather than going directly to the gcc builtin?

Matt

> +
> +#define FIELD_GET(mask, reg) \
> + (typeof(mask))(((reg) & (mask)) >> __bf_shf(mask))
> +
> #endif /* _INTEL_GPU_COMMANDS_SCAFFOLD_H_ */
> --
> 2.43.0
>

--
Matt Roper
Graphics Software Engineer
Linux GPU Platform Enablement
Intel Corporation