Patchwork [1/7] qcow2: Make get_bits_from_size() common

login
register
mail settings
Submitter Stefan Hajnoczi
Date Sept. 23, 2010, 3:41 p.m.
Message ID <1285256514-21138-2-git-send-email-stefanha@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/65567/
State New
Headers show

Comments

Stefan Hajnoczi - Sept. 23, 2010, 3:41 p.m.
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 <stefanha@linux.vnet.ibm.com>
---
 block/qcow2.c |   22 ----------------------
 cutils.c      |   26 ++++++++++++++++++++++++++
 qemu-common.h |    1 +
 3 files changed, 27 insertions(+), 22 deletions(-)
malc - Sept. 23, 2010, 9:50 p.m.
On Thu, 23 Sep 2010, 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.

int ilog2 (size_t size)
{
    if (size & (size - 1))
        return -1;

    return __builtin_ctzl (size);
}

Ifdef WIN64 omitted for brevity.

[..snip..]
Stefan Hajnoczi - Sept. 24, 2010, 8:37 a.m.
On Fri, Sep 24, 2010 at 01:50:39AM +0400, malc wrote:
> On Thu, 23 Sep 2010, 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.
> 
> int ilog2 (size_t size)
> {
>     if (size & (size - 1))
>         return -1;
> 
>     return __builtin_ctzl (size);
> }
> 
> Ifdef WIN64 omitted for brevity.

What is the situation with WIN64?

Stefan
Paolo Bonzini - Sept. 24, 2010, 12:56 p.m.
On 09/24/2010 10:37 AM, Stefan Hajnoczi wrote:
> On Fri, Sep 24, 2010 at 01:50:39AM +0400, malc wrote:
>> On Thu, 23 Sep 2010, 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.
>>
>> int ilog2 (size_t size)
>> {
>>      if (size&  (size - 1))
>>          return -1;
>>
>>      return __builtin_ctzl (size);
>> }
>>
>> Ifdef WIN64 omitted for brevity.
>
> What is the situation with WIN64?

long is only 32 bit, so you need __builtin_ctzll.

Paolo
Stefan Hajnoczi - Sept. 24, 2010, 2:18 p.m.
On Fri, Sep 24, 2010 at 1:56 PM, Paolo Bonzini <pbonzini@redhat.com> wrote:
> On 09/24/2010 10:37 AM, Stefan Hajnoczi wrote:
>>
>> On Fri, Sep 24, 2010 at 01:50:39AM +0400, malc wrote:
>>>
>>> On Thu, 23 Sep 2010, 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.
>>>
>>> int ilog2 (size_t size)
>>> {
>>>     if (size&  (size - 1))
>>>         return -1;
>>>
>>>     return __builtin_ctzl (size);
>>> }
>>>
>>> Ifdef WIN64 omitted for brevity.
>>
>> What is the situation with WIN64?
>
> long is only 32 bit, so you need __builtin_ctzll.

Thanks Paolo and malc.  It should be easy enough to use the builtin instead.

Stefan

Patch

diff --git a/block/qcow2.c b/block/qcow2.c
index a53014d..72c923a 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -767,28 +767,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 036ae3c..f9812d5 100644
--- a/cutils.c
+++ b/cutils.c
@@ -251,3 +251,29 @@  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)
+{
+    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;
+}
diff --git a/qemu-common.h b/qemu-common.h
index dfd3dc0..3170c64 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -137,6 +137,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);