From patchwork Sun Apr 10 19:12:56 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aurelien Jarno X-Patchwork-Id: 90534 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 1CD22B6F2B for ; Mon, 11 Apr 2011 05:14:07 +1000 (EST) Received: from localhost ([127.0.0.1]:41246 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q905E-0005t2-2j for incoming@patchwork.ozlabs.org; Sun, 10 Apr 2011 15:14:04 -0400 Received: from [140.186.70.92] (port=60510 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q904B-0005p0-7F for qemu-devel@nongnu.org; Sun, 10 Apr 2011 15:13:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Q904A-0008VT-4V for qemu-devel@nongnu.org; Sun, 10 Apr 2011 15:12:59 -0400 Received: from hall.aurel32.net ([88.191.126.93]:36351) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Q904A-0008V9-03 for qemu-devel@nongnu.org; Sun, 10 Apr 2011 15:12:58 -0400 Received: from [2001:470:d4ed:0:5e26:aff:fe2b:6f5b] (helo=volta.aurel32.net) by hall.aurel32.net with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.72) (envelope-from ) id 1Q9049-0002GM-F1; Sun, 10 Apr 2011 21:12:57 +0200 Received: from aurel32 by volta.aurel32.net with local (Exim 4.72) (envelope-from ) id 1Q9048-0002FY-F5; Sun, 10 Apr 2011 21:12:56 +0200 From: Aurelien Jarno To: qemu-devel@nongnu.org Date: Sun, 10 Apr 2011 21:12:56 +0200 Message-Id: <1302462776-8615-1-git-send-email-aurelien@aurel32.net> X-Mailer: git-send-email 1.7.2.3 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 88.191.126.93 Cc: Peter Maydell , Aurelien Jarno Subject: [Qemu-devel] [PATCH] softfloat: use GCC builtins to count the leading zeros 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 Softfloat has its own implementation to count the leading zeros. However a lot of architectures have either a dedicated instruction or an optimized to do that. When using GCC >= 3.4, this patch uses GCC builtins instead of the handcoded implementation. Note that I amware that QEMU_GNUC_PREREQ is defined in osdep.h and that clz32() and clz64() are defined in host-utils.h, but I think it is better to keep the softfloat implemntation self contained. Cc: Peter Maydell Signed-off-by: Aurelien Jarno --- fpu/softfloat-macros.h | 29 +++++++++++++++++++++++++++-- 1 files changed, 27 insertions(+), 2 deletions(-) diff --git a/fpu/softfloat-macros.h b/fpu/softfloat-macros.h index 3128e60..e82ce23 100644 --- a/fpu/softfloat-macros.h +++ b/fpu/softfloat-macros.h @@ -36,6 +36,17 @@ these four paragraphs for those parts of this code that are retained. =============================================================================*/ /*---------------------------------------------------------------------------- +| This macro tests for minimum version of the GNU C compiler. +*----------------------------------------------------------------------------*/ +#if defined(__GNUC__) && defined(__GNUC_MINOR__) +# define SOFTFLOAT_GNUC_PREREQ(maj, min) \ + ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) +#else +# define SOFTFLOAT_GNUC_PREREQ(maj, min) 0 +#endif + + +/*---------------------------------------------------------------------------- | Shifts `a' right by the number of bits given in `count'. If any nonzero | bits are shifted off, they are ``jammed'' into the least significant bit of | the result by setting the least significant bit to 1. The value of `count' @@ -616,6 +627,13 @@ static uint32_t estimateSqrt32( int16 aExp, uint32_t a ) static int8 countLeadingZeros32( uint32_t a ) { +#if SOFTFLOAT_GNUC_PREREQ(3, 4) + if (a) { + return __builtin_clz(a); + } else { + return 32; + } +#else static const int8 countLeadingZerosHigh[] = { 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, @@ -647,7 +665,7 @@ static int8 countLeadingZeros32( uint32_t a ) } shiftCount += countLeadingZerosHigh[ a>>24 ]; return shiftCount; - +#endif } /*---------------------------------------------------------------------------- @@ -657,6 +675,13 @@ static int8 countLeadingZeros32( uint32_t a ) static int8 countLeadingZeros64( uint64_t a ) { +#if SOFTFLOAT_GNUC_PREREQ(3, 4) + if (a) { + return __builtin_clzll(a); + } else { + return 64; + } +#else int8 shiftCount; shiftCount = 0; @@ -668,7 +693,7 @@ static int8 countLeadingZeros64( uint64_t a ) } shiftCount += countLeadingZeros32( a ); return shiftCount; - +#endif } /*----------------------------------------------------------------------------