bitops: linux: Add fns() to find N'th set bit

Add the fns() function from Linux to find the N'th set bit in a word.
This is needed by lib/find_bit.c which uses it in the FIND_NTH_BIT
macro.

Taken from Linux v6.19

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
This commit is contained in:
Simon Glass
2025-12-30 08:13:39 -07:00
parent 014f082f05
commit ca3b7c4fe2

View File

@@ -194,6 +194,19 @@ static inline unsigned long __ffs64(u64 word)
return __ffs((unsigned long)word);
}
/**
* fns - find N'th set bit in a word
* @word: The word to search
* @n: Bit to find
*/
static inline unsigned int fns(unsigned long word, unsigned int n)
{
while (word && n--)
word &= word - 1;
return word ? __ffs(word) : BITS_PER_LONG;
}
/**
* __set_bit - Set a bit in memory
* @nr: the bit to set