Hi Brian,

some random thoughts...

On 15/7/26 20:56, Brian Cain wrote:
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. find_first_bit() has no such
equivalent, which pushes callers towards casting a uint32_t array
to 'unsigned long *'.

Add find_first_bit32(), implemented the same way as find_first_bit().

Reviewed-by: Pierrick Bouvier <[email protected]>
Signed-off-by: Brian Cain <[email protected]>
---
  include/qemu/bitops.h | 30 ++++++++++++++++++++++++++----
  1 file changed, 26 insertions(+), 4 deletions(-)


+/**
+ * find_first_bit32 - find the first set bit in a memory region
+ * @addr: The address to start the search at

s/addr/ptr/?

+ * @size: The maximum size to search

Pre-existing, it is not clear this is expressed in bits. Neither if
we expect it to be a multiple of 32, or @addr to be 32-bit aligbed.

Maybe rename as @last_searched_bit?

+ *
+ * Returns the bit number of the first set bit,

"Returns the index of the first bit,"?

+ * or @size if there is no set bit in the bitmap.
+ */
+static inline uint32_t find_first_bit32(const uint32_t *addr, uint32_t size)

Could we simply return a plain unsigned? Also take a size_t?

+{
+    uint32_t result;
+
+    for (result = 0; result < size; result += 32) {
+        uint32_t tmp = *addr++;
+        if (tmp) {
+            result += ctz32(tmp);
+            return result < size ? result : size;
+        }
+    }
+    /* Not found */
+    return size;
+}


Reply via email to