From nobody Mon Feb 9 10:28:45 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1504723750252235.60282417365102; Wed, 6 Sep 2017 11:49:10 -0700 (PDT) Received: from localhost ([::1]:37492 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dpfNt-0000pT-6F for importer@patchew.org; Wed, 06 Sep 2017 14:49:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57658) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dpfGm-0002zu-82 for qemu-devel@nongnu.org; Wed, 06 Sep 2017 14:41:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dpfGl-0001n7-CB for qemu-devel@nongnu.org; Wed, 06 Sep 2017 14:41:48 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43862) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dpfGl-0001lJ-6J for qemu-devel@nongnu.org; Wed, 06 Sep 2017 14:41:47 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 39DB88553D for ; Wed, 6 Sep 2017 18:41:46 +0000 (UTC) Received: from dgilbert-t530.redhat.com (ovpn-117-207.ams2.redhat.com [10.36.117.207]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9BD0C5D6A0; Wed, 6 Sep 2017 18:41:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 39DB88553D Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=dgilbert@redhat.com From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org Date: Wed, 6 Sep 2017 19:41:29 +0100 Message-Id: <20170906184133.25524-5-dgilbert@redhat.com> In-Reply-To: <20170906184133.25524-1-dgilbert@redhat.com> References: <20170906184133.25524-1-dgilbert@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 06 Sep 2017 18:41:46 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 4/8] host-utils: Simplify pow2ceil() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, armbru@redhat.com, peterx@redhat.com, quintela@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" From: Markus Armbruster Cc: Radim Kr=C4=8Dm=C3=A1=C5=99 Signed-off-by: Markus Armbruster Message-Id: <1501148776-16890-4-git-send-email-armbru@redhat.com> Reviewed-by: Eric Blake Signed-off-by: Dr. David Alan Gilbert --- include/qemu/host-utils.h | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h index 6c6005f5cf..5ac621cf1f 100644 --- a/include/qemu/host-utils.h +++ b/include/qemu/host-utils.h @@ -381,18 +381,23 @@ static inline uint64_t pow2floor(uint64_t value) return 0x8000000000000000ull >> clz64(value); } =20 -/* round up to the nearest power of 2 (0 if overflow) */ +/* + * Return @value rounded up to the nearest power of two modulo 2^64. + * This is *zero* for @value > 2^63, so be careful. + */ static inline uint64_t pow2ceil(uint64_t value) { - uint8_t nlz =3D clz64(value); - - if (is_power_of_2(value)) { - return value; - } - if (!nlz) { - return 0; + int n =3D clz64(value - 1); + + if (!n) { + /* + * @value - 1 has no leading zeroes, thus @value - 1 >=3D 2^63 + * Therefore, either @value =3D=3D 0 or @value > 2^63. + * If it's 0, return 1, else return 0. + */ + return !value; } - return 1ULL << (64 - nlz); + return 0x8000000000000000ull >> (n - 1); } =20 /** --=20 2.13.5