From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KopCo-0007Rg-Ts for qemu-devel@nongnu.org; Sat, 11 Oct 2008 20:53:10 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KopCo-0007RK-9T for qemu-devel@nongnu.org; Sat, 11 Oct 2008 20:53:10 -0400 Received: from [199.232.76.173] (port=52984 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KopCo-0007RE-3F for qemu-devel@nongnu.org; Sat, 11 Oct 2008 20:53:10 -0400 Received: from savannah.gnu.org ([199.232.41.3]:35779 helo=sv.gnu.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1KopCn-0003K8-Nr for qemu-devel@nongnu.org; Sat, 11 Oct 2008 20:53:09 -0400 Received: from cvs.savannah.gnu.org ([199.232.41.69]) by sv.gnu.org with esmtp (Exim 4.63) (envelope-from ) id 1KopCn-0001WN-7k for qemu-devel@nongnu.org; Sun, 12 Oct 2008 00:53:09 +0000 Received: from aurel32 by cvs.savannah.gnu.org with local (Exim 4.63) (envelope-from ) id 1KopCm-0001WI-Te for qemu-devel@nongnu.org; Sun, 12 Oct 2008 00:53:09 +0000 MIME-Version: 1.0 Errors-To: aurel32 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Aurelien Jarno Message-Id: Date: Sun, 12 Oct 2008 00:53:08 +0000 Subject: [Qemu-devel] [5464] Optimize some host-utils function with gcc builtins Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Revision: 5464 http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5464 Author: aurel32 Date: 2008-10-12 00:53:08 +0000 (Sun, 12 Oct 2008) Log Message: ----------- Optimize some host-utils function with gcc builtins Signed-off-by: Aurelien Jarno Modified Paths: -------------- trunk/host-utils.h Modified: trunk/host-utils.h =================================================================== --- trunk/host-utils.h 2008-10-12 00:52:58 UTC (rev 5463) +++ trunk/host-utils.h 2008-10-12 00:53:08 UTC (rev 5464) @@ -47,14 +47,16 @@ void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b); #endif -/* Note that some of those functions may end up calling libgcc functions, - depending on the host machine. It is up to the target emulation to - cope with that. */ - /* Binary search for leading zeros. */ static always_inline int clz32(uint32_t val) { +#if defined(__GNUC__) + if (val) + return __builtin_clz(val); + else + return 32; +#else int cnt = 0; if (!(val & 0xFFFF0000U)) { @@ -81,6 +83,7 @@ cnt++; } return cnt; +#endif } static always_inline int clo32(uint32_t val) @@ -90,6 +93,12 @@ static always_inline int clz64(uint64_t val) { +#if defined(__GNUC__) + if (val) + return __builtin_clzll(val); + else + return 64; +#else int cnt = 0; if (!(val >> 32)) { @@ -99,6 +108,7 @@ } return cnt + clz32(val); +#endif } static always_inline int clo64(uint64_t val) @@ -108,6 +118,12 @@ static always_inline int ctz32 (uint32_t val) { +#if defined(__GNUC__) + if (val) + return __builtin_ctz(val); + else + return 32; +#else int cnt; cnt = 0; @@ -136,6 +152,7 @@ } return cnt; +#endif } static always_inline int cto32 (uint32_t val) @@ -145,6 +162,12 @@ static always_inline int ctz64 (uint64_t val) { +#if defined(__GNUC__) + if (val) + return __builtin_ctz(val); + else + return 64; +#else int cnt; cnt = 0; @@ -154,6 +177,7 @@ } return cnt + ctz32(val); +#endif } static always_inline int cto64 (uint64_t val) @@ -182,6 +206,9 @@ static always_inline int ctpop32 (uint32_t val) { +#if defined(__GNUC__) + return __builtin_popcount(val); +#else val = (val & 0x55555555) + ((val >> 1) & 0x55555555); val = (val & 0x33333333) + ((val >> 2) & 0x33333333); val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f); @@ -189,10 +216,14 @@ val = (val & 0x0000ffff) + ((val >> 16) & 0x0000ffff); return val; +#endif } static always_inline int ctpop64 (uint64_t val) { +#if defined(__GNUC__) + return __builtin_popcountll(val); +#else val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL); val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL); val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >> 4) & 0x0f0f0f0f0f0f0f0fULL); @@ -201,4 +232,5 @@ val = (val & 0x00000000ffffffffULL) + ((val >> 32) & 0x00000000ffffffffULL); return val; +#endif }