From patchwork Tue Jul 24 11:04:14 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 172834 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 2213B2C008D for ; Tue, 24 Jul 2012 21:20:12 +1000 (EST) Received: from localhost ([::1]:50726 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Std9u-0004zY-60 for incoming@patchwork.ozlabs.org; Tue, 24 Jul 2012 07:20:10 -0400 Received: from eggs.gnu.org ([208.118.235.92]:56986) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Stcwl-0008MT-6l for qemu-devel@nongnu.org; Tue, 24 Jul 2012 07:06:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Stcwb-0005sR-KM for qemu-devel@nongnu.org; Tue, 24 Jul 2012 07:06:35 -0400 Received: from mail-gh0-f173.google.com ([209.85.160.173]:59147) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Stcwb-0005Rh-GB for qemu-devel@nongnu.org; Tue, 24 Jul 2012 07:06:25 -0400 Received: by mail-gh0-f173.google.com with SMTP id r14so6581704ghr.4 for ; Tue, 24 Jul 2012 04:06:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=NsVNYBfwhMCBx1X7ywfJBklNTiiZ/hdcxWNDblyA220=; b=wgvo9dg3QcxNlyPuXlyPwRhbRAXdgdrNzn/g+TCnKYMmxYUJkAHAuHylzX5DrkP0sb 4zs+h/0MIEn3C/sPgghcMKBXpMK/hnlyYZWhtszJ9Z7VmAeKqfkjFFzYt6yHRirP5nqP qRb5EKuUHy2tKGJHBj2Aqs0L8tqEaYs6f9wwiGY6a9xkSRGgfDv44XB+afYGzNBtD87x L1CkuM26bKQIXaY3G52OLA6lB3pZZFw3KloG0TtEpqAKC8yZK4bIZyJO7FG2XkEsvTLF 21hXFrTGLBWAo1eimKq2f1SQvANWk7wgXo7l6f+lPJezkMn3IYUxZ5v4aGWKw4yVSZV9 Qqfg== Received: by 10.42.154.199 with SMTP id r7mr14390872icw.55.1343127985160; Tue, 24 Jul 2012 04:06:25 -0700 (PDT) Received: from yakj.usersys.redhat.com (93-34-189-113.ip51.fastwebnet.it. [93.34.189.113]) by mx.google.com with ESMTPS id if4sm1752561igc.10.2012.07.24.04.06.22 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 24 Jul 2012 04:06:23 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 24 Jul 2012 13:04:14 +0200 Message-Id: <1343127865-16608-37-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1343127865-16608-1-git-send-email-pbonzini@redhat.com> References: <1343127865-16608-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.160.173 Cc: kwolf@redhat.com, jcody@redhat.com, eblake@redhat.com, stefanha@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH 36/47] host-utils: add ffsl and flsl X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org We can provide fast versions based on the other functions defined by host-utils.h. Some care is required on glibc, which provides ffsl already. Signed-off-by: Paolo Bonzini --- host-utils.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/host-utils.h b/host-utils.h index 821db93..4250eb0 100644 --- a/host-utils.h +++ b/host-utils.h @@ -24,6 +24,7 @@ */ #include "compiler.h" /* QEMU_GNUC_PREREQ */ +#include /* ffsl */ #if defined(__x86_64__) #define __HAVE_FAST_MULU64__ @@ -234,3 +235,47 @@ static inline int ctpop64(uint64_t val) return val; #endif } + +/* glibc does not provide an inline version of ffsl, so always define + * ours. + */ +#ifdef __GLIBC__ +#define ffsl qemu_ffsl +#endif +static inline int ffsl(long val) +{ + if (!val) { + return 0; + } + +#if QEMU_GNUC_PREREQ(3, 4) + return __builtin_ctzl(val) + 1; +#else + if (sizeof(long) == 4) { + return ctz32(val) + 1; + } else if (sizeof(long) == 8) { + return ctz64(val) + 1; + } else { + abort(); + } +#endif +} + +static inline int flsl(long val) +{ + if (!val) { + return 0; + } + +#if QEMU_GNUC_PREREQ(3, 4) + return __builtin_clzl(val) + 1; +#else + if (sizeof(long) == 4) { + return clz32(val) + 1; + } else if (sizeof(long) == 8) { + return clz64(val) + 1; + } else { + abort(); + } +#endif +}