qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH] bitops: unify bitops_flsl with host-utils.h, call i


From: Richard Henderson
Subject: [Qemu-devel] [PATCH] bitops: unify bitops_flsl with host-utils.h, call it bitops_clzl
Date: Mon, 4 Feb 2013 10:26:02 -0800

Similar to the bitops_ctz change just made.

Signed-off-by: Richard Henderson <address@hidden>
---
 include/qemu/bitops.h  | 44 +++++++++++++++-----------------------------
 target-i386/topology.h |  2 +-
 util/bitops.c          |  2 +-
 3 files changed, 17 insertions(+), 31 deletions(-)

diff --git a/include/qemu/bitops.h b/include/qemu/bitops.h
index 8b88791..07ada63 100644
--- a/include/qemu/bitops.h
+++ b/include/qemu/bitops.h
@@ -50,41 +50,27 @@ static unsigned long bitops_ctzl(unsigned long word)
 }
 
 /**
- * bitops_fls - find last (most-significant) set bit in a long word
+ * bitops_clzl - find last (most-significant) set bit in a long word
  * @word: the word to search
  *
  * Undefined if no set bit exists, so code should check against 0 first.
  */
-static inline unsigned long bitops_flsl(unsigned long word)
+static inline unsigned long bitops_clzl(unsigned long word)
 {
-       int num = BITS_PER_LONG - 1;
-
-#if LONG_MAX > 0x7FFFFFFF
-       if (!(word & (~0ul << 32))) {
-               num -= 32;
-               word <<= 32;
-       }
+    /* Both this function and the gcc builtin are undefined at 0, whereas
+       the host-utils functions return word-size at 0.  Thus we prefer the
+       gcc builtin as the closer match.  */
+#if QEMU_GNUC_PREREQ(3, 4)
+    return __builtin_clzl(word);
+#else
+    if (sizeof(long) == 4) {
+        return clz32(word);
+    } else if (sizeof(long) == 8) {
+        return clz64(word);
+    } else {
+        abort();
+    }
 #endif
-       if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
-               num -= 16;
-               word <<= 16;
-       }
-       if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
-               num -= 8;
-               word <<= 8;
-       }
-       if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
-               num -= 4;
-               word <<= 4;
-       }
-       if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
-               num -= 2;
-
-               word <<= 2;
-       }
-       if (!(word & (~0ul << (BITS_PER_LONG-1))))
-               num -= 1;
-       return num;
 }
 
 /**
diff --git a/target-i386/topology.h b/target-i386/topology.h
index 24ed525..96a559e 100644
--- a/target-i386/topology.h
+++ b/target-i386/topology.h
@@ -55,7 +55,7 @@ static unsigned apicid_bitwidth_for_count(unsigned count)
     if (count == 1) {
         return 0;
     }
-    return bitops_flsl(count - 1) + 1;
+    return bitops_clzl(count - 1) + 1;
 }
 
 /* Bit width of the SMT_ID (thread ID) field on the APIC ID
diff --git a/util/bitops.c b/util/bitops.c
index 7b853cf..4f4fa95 100644
--- a/util/bitops.c
+++ b/util/bitops.c
@@ -133,7 +133,7 @@ unsigned long find_last_bit(const unsigned long *addr, 
unsigned long size)
         tmp = addr[--words];
         if (tmp) {
         found:
-            return words * BITS_PER_LONG + bitops_flsl(tmp);
+            return words * BITS_PER_LONG + bitops_clzl(tmp);
         }
     }
 
-- 
1.8.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]