From patchwork Mon Jun 18 18:42:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 10472547 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 885126029B for ; Mon, 18 Jun 2018 18:43:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 76FF32621E for ; Mon, 18 Jun 2018 18:43:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6BE5D27861; Mon, 18 Jun 2018 18:43:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1CD622621E for ; Mon, 18 Jun 2018 18:43:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935948AbeFRSnZ (ORCPT ); Mon, 18 Jun 2018 14:43:25 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:39236 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935836AbeFRSnX (ORCPT ); Mon, 18 Jun 2018 14:43:23 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 28A6380D; Mon, 18 Jun 2018 11:43:23 -0700 (PDT) Received: from ostrya.cambridge.arm.com (ostrya.cambridge.arm.com [10.1.210.39]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 9C05E3F557; Mon, 18 Jun 2018 11:43:21 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org Cc: kvmarm@lists.cs.columbia.edu, will.deacon@arm.com, robin.murphy@arm.com, lorenzo.pieralisi@arm.com, marc.zyngier@arm.com, punit.agrawal@arm.com, alex.williamson@redhat.com Subject: [PATCH v6 kvmtool 07/13] Add fls_long and roundup_pow_of_two helpers Date: Mon, 18 Jun 2018 19:42:05 +0100 Message-Id: <20180618184211.43904-8-jean-philippe.brucker@arm.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180618184211.43904-1-jean-philippe.brucker@arm.com> References: <20180618184211.43904-1-jean-philippe.brucker@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP It's always nice to have a log2 handy, and the vfio-pci code will need to perform power of two allocation from an arbitrary size. Add fls_long and roundup_pow_of_two, based on the GCC builtin. Reviewed-by: Punit Agrawal Signed-off-by: Jean-Philippe Brucker --- include/kvm/util.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/kvm/util.h b/include/kvm/util.h index 0df9f0dfd..4ca7aa939 100644 --- a/include/kvm/util.h +++ b/include/kvm/util.h @@ -90,6 +90,20 @@ static inline void msleep(unsigned int msecs) usleep(MSECS_TO_USECS(msecs)); } +/* + * Find last (most significant) bit set. Same implementation as Linux: + * fls(0) = 0, fls(1) = 1, fls(1UL << 63) = 64 + */ +static inline int fls_long(unsigned long x) +{ + return x ? sizeof(x) * 8 - __builtin_clzl(x) : 0; +} + +static inline unsigned long roundup_pow_of_two(unsigned long x) +{ + return x ? 1UL << fls_long(x - 1) : 0; +} + struct kvm; void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size); void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size);