From 3680c33b2eb099b3364e966b696cf6e33c248b8a Mon Sep 17 00:00:00 2001 From: Christian Lamparter Date: Fri, 6 May 2022 14:00:36 +0200 Subject: [PATCH] carl9170: add hweight and fieldmax from the linux kernel Signed-off-by: Christian Lamparter --- include/linux/compiler.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index cd97693..e2656ae 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -111,6 +111,14 @@ static inline unsigned int hweight8(unsigned int w) return (res + (res >> 4)) & 0x0F; } +static inline unsigned int hweight16(unsigned int w) +{ + unsigned int res = w - ((w >> 1) & 0x5555); + res = (res & 0x3333) + ((res >> 2) & 0x3333); + res = (res + (res >> 4)) & 0x0F0F; + return (res + (res >> 8)) & 0x00FF; +} + /** * DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union * @@ -127,4 +135,11 @@ static inline unsigned int hweight8(unsigned int w) TYPE NAME[]; \ } +#define __bf_shf(x) (__builtin_ffsll(x) - 1) + +#define FIELD_MAX(_mask) \ + ({ \ + (typeof(_mask))((_mask) >> __bf_shf(_mask)); \ + }) + #endif /* __SHARED_COMPILER_H */ -- 2.31.1