@samitouri / QOSamiQemu / commits / d922df10b2

bitops.h: Add find_first_bit32()

set_bit32()/test_bit32()/etc already let devices operate on guest-visible uint32_t register arrays without depending on the host's 'unsigned long' size. Add find_first_bit32() so callers need not cast a uint32_t array to 'unsigned long *'. Reviewed-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com> Link: https://lore.kernel.org/qemu-devel/20260806042723.3785369-7-brian.cain@oss.qualcomm.com Signed-off-by: Brian Cain <brian.cain@oss.qualcomm.com>

Brian Cain committed Aug 5, 2026 at 21:27 UTC d922df10b27aa9f492638a22b62a43e873246c40
1 file changed +26 -4
include/qemu/bitops.h
+26 -4
@@ -43,10 +43,9 @@
43 * be some guest-visible register view of the bit array.
44 *
45 * We do not currently implement uint32_t versions of find_last_bit(),
46 - * find_next_bit(), find_next_zero_bit(), find_first_bit() or
47 - * find_first_zero_bit(), because we haven't yet needed them. If you
48 - * need them you should implement them similarly to the 'unsigned long'
49 - * versions.
46 + * find_next_bit(), find_next_zero_bit() or find_first_zero_bit(),
47 + * because we haven't yet needed them. If you need them you should
48 + * implement them similarly to the 'unsigned long' versions.
49 *
50 * You can declare a bitmap to be used with these functions via the
51 * DECLARE_BITMAP and DECLARE_BITMAP32 macros in bitmap.h.
@@ -382,6 +381,29 @@ static inline int test_bit32(long nr, const uint32_t *addr)
381 return 1U & (addr[BIT32_WORD(nr)] >> (nr & 31));
382 }
383
384 +/**
385 + * find_first_bit32 - find the first set bit in a memory region
386 + * @addr: The address to start the search at
387 + * @size: The maximum size to search
388 + *
389 + * Returns the bit number of the first set bit,
390 + * or @size if there is no set bit in the bitmap.
391 + */
392 +static inline uint32_t find_first_bit32(const uint32_t *addr, uint32_t size)
393 +{
394 + uint32_t result;
395 +
396 + for (result = 0; result < size; result += 32) {
397 + uint32_t tmp = *addr++;
398 + if (tmp) {
399 + result += ctz32(tmp);
400 + return result < size ? result : size;
401 + }
402 + }
403 + /* Not found */
404 + return size;
405 +}
406 +
407 /**
408 * DOC: Miscellaneous bit operations on single values
409 *