OSDN Git Service

bitops: provide an inline implementation of find_first_bit
authorAurelien Jarno <aurelien@aurel32.net>
Sat, 21 Dec 2013 00:15:21 +0000 (01:15 +0100)
committerAurelien Jarno <aurelien@aurel32.net>
Wed, 18 Jun 2014 16:10:47 +0000 (18:10 +0200)
find_first_bit has started to be used heavily in TCG code. The current
implementation based on find_next_bit is not optimal and can't be
optimized be the compiler if the bit array has a fixed size, which is
the case most of the time.

This new implementation does not use find_next_bit and is yet small
enough to be inlined.

Cc: Corentin Chary <corentin.chary@gmail.com>
Reviewed-by: Richard Henderson <rth@twiddle.net>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
include/qemu/bitops.h

index 340b1e7..7e2d5c9 100644 (file)
@@ -157,7 +157,17 @@ unsigned long find_next_zero_bit(const unsigned long *addr,
 static inline unsigned long find_first_bit(const unsigned long *addr,
                                            unsigned long size)
 {
-    return find_next_bit(addr, size, 0);
+    unsigned long result, tmp;
+
+    for (result = 0; result < size; result += BITS_PER_LONG) {
+        tmp = *addr++;
+        if (tmp) {
+            result += ctzl(tmp);
+            return result < size ? result : size;
+        }
+    }
+    /* Not found */
+    return size;
 }
 
 /**