qemu-devel
[Top][All Lists]
Advanced

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

[Qemu-devel] Re: [PATCH v2 1/7] qcow2: Make get_bits_from_size() common


From: Anthony Liguori
Subject: [Qemu-devel] Re: [PATCH v2 1/7] qcow2: Make get_bits_from_size() common
Date: Fri, 08 Oct 2010 13:01:48 -0500
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.12) Gecko/20100915 Lightning/1.0b1 Thunderbird/3.0.8

On 10/08/2010 10:48 AM, Stefan Hajnoczi wrote:
The get_bits_from_size() calculates the log base-2 of a number.  This is
useful in bit manipulation code working with power-of-2s.

Currently used by qcow2 and needed by qed in a follow-on patch.

Signed-off-by: Stefan Hajnoczi<address@hidden>

Acked-by: Anthony Liguori <address@hidden>

Regards,

Anthony Liguori

---
  block/qcow2.c |   22 ----------------------
  cutils.c      |   18 ++++++++++++++++++
  qemu-common.h |    1 +
  3 files changed, 19 insertions(+), 22 deletions(-)

diff --git a/block/qcow2.c b/block/qcow2.c
index ee3481b..6e25812 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -794,28 +794,6 @@ static int qcow2_change_backing_file(BlockDriverState *bs,
      return qcow2_update_ext_header(bs, backing_file, backing_fmt);
  }

-static int get_bits_from_size(size_t size)
-{
-    int res = 0;
-
-    if (size == 0) {
-        return -1;
-    }
-
-    while (size != 1) {
-        /* Not a power of two */
-        if (size&  1) {
-            return -1;
-        }
-
-        size>>= 1;
-        res++;
-    }
-
-    return res;
-}
-
-
  static int preallocate(BlockDriverState *bs)
  {
      uint64_t nb_sectors;
diff --git a/cutils.c b/cutils.c
index 5883737..6c32198 100644
--- a/cutils.c
+++ b/cutils.c
@@ -283,3 +283,21 @@ int fcntl_setfl(int fd, int flag)
  }
  #endif

+/**
+ * Get the number of bits for a power of 2
+ *
+ * The following is true for powers of 2:
+ *   n == 1<<  get_bits_from_size(n)
+ */
+int get_bits_from_size(size_t size)
+{
+    if (size == 0 || (size&  (size - 1))) {
+        return -1;
+    }
+
+#if defined(_WIN32)&&  defined(__x86_64__)
+    return __builtin_ctzll(size);
+#else
+    return __builtin_ctzl(size);
+#endif
+}
diff --git a/qemu-common.h b/qemu-common.h
index 81aafa0..e0ca398 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -153,6 +153,7 @@ time_t mktimegm(struct tm *tm);
  int qemu_fls(int i);
  int qemu_fdatasync(int fd);
  int fcntl_setfl(int fd, int flag);
+int get_bits_from_size(size_t size);

  /* path.c */
  void init_paths(const char *prefix);




reply via email to

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