From patchwork Fri Oct 8 15:48:28 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 67270 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1C58B102650 for ; Sat, 9 Oct 2010 06:09:02 +1100 (EST) Received: from localhost ([127.0.0.1]:48882 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P4IJH-0003Hu-E0 for incoming@patchwork.ozlabs.org; Fri, 08 Oct 2010 15:08:51 -0400 Received: from [140.186.70.92] (port=49068 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P4I9F-0005lP-IP for qemu-devel@nongnu.org; Fri, 08 Oct 2010 14:58:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P4I9B-00024e-9D for qemu-devel@nongnu.org; Fri, 08 Oct 2010 14:58:27 -0400 Received: from mtagate5.de.ibm.com ([195.212.17.165]:55268) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P4I9B-00023f-0w for qemu-devel@nongnu.org; Fri, 08 Oct 2010 14:58:25 -0400 Received: from d12nrmr1607.megacenter.de.ibm.com (d12nrmr1607.megacenter.de.ibm.com [9.149.167.49]) by mtagate5.de.ibm.com (8.13.1/8.13.1) with ESMTP id o98Fmjgq030607 for ; Fri, 8 Oct 2010 15:48:45 GMT Received: from d12av02.megacenter.de.ibm.com (d12av02.megacenter.de.ibm.com [9.149.165.228]) by d12nrmr1607.megacenter.de.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o98FmiPc4038692 for ; Fri, 8 Oct 2010 17:48:45 +0200 Received: from d12av02.megacenter.de.ibm.com (loopback [127.0.0.1]) by d12av02.megacenter.de.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id o98Fmh2A019629 for ; Fri, 8 Oct 2010 17:48:43 +0200 Received: from stefan-thinkpad.manchester-maybrook.uk.ibm.com (dyn-9-174-218-53.manchester-maybrook.uk.ibm.com [9.174.218.53]) by d12av02.megacenter.de.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id o98Fmgek019417; Fri, 8 Oct 2010 17:48:43 +0200 From: Stefan Hajnoczi To: Date: Fri, 8 Oct 2010 16:48:28 +0100 Message-Id: <1286552914-27014-2-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1286552914-27014-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1286552914-27014-1-git-send-email-stefanha@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: Kevin Wolf , Anthony Liguori , Christoph Hellwig , Stefan Hajnoczi , Avi Kivity Subject: [Qemu-devel] [PATCH v2 1/7] qcow2: Make get_bits_from_size() common X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- 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);