From patchwork Mon Jan 28 18:52:34 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 216301 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 C17B92C0091 for ; Tue, 29 Jan 2013 05:53:06 +1100 (EST) Received: from localhost ([::1]:56633 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TztpI-0000Xk-VC for incoming@patchwork.ozlabs.org; Mon, 28 Jan 2013 13:53:04 -0500 Received: from eggs.gnu.org ([208.118.235.92]:41992) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tztp2-0000M4-0a for qemu-devel@nongnu.org; Mon, 28 Jan 2013 13:52:50 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tztoy-0005K9-Or for qemu-devel@nongnu.org; Mon, 28 Jan 2013 13:52:47 -0500 Received: from mail-qc0-f177.google.com ([209.85.216.177]:38909) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tztoy-0005K3-Ko for qemu-devel@nongnu.org; Mon, 28 Jan 2013 13:52:44 -0500 Received: by mail-qc0-f177.google.com with SMTP id u28so1436005qcs.8 for ; Mon, 28 Jan 2013 10:52:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:sender:from:to:subject:date:message-id:x-mailer :in-reply-to:references; bh=3RAJBT00CKHbDy02jsd10a6rGT/YxYEgyDQG5FTashc=; b=xj4CeumhFceXbQLZ4BnZCaawZ6rAs9Eim2DvMTgjdn5L42O2t3V4ARpHBjh5qXvNkk svCNs+Fkxt6Ov+rHrP6SZyzgKA013eo48z64GAa50BhAHRItdlccmCBEddU0tlezt6Gf rzQdveRi0fLr3mNs1oCVvbO4zAm+ic9I97ZO//IGB5CsrftijNWtaU23m0t0GV8gLRhn bzI4DHTOgvOZ2VCbMY5aGzWw5f9XlGHIMmU5Pvyzpnx+TREZF4+W8vvXrlvzLMhUGhw3 SpBIThHfoq8cj1DTPfNP1zse8mw5WYh4ySf0XP3OK081bhB6eGfcaaaD6hSjbhGNdyiG 0Xtw== X-Received: by 10.224.216.8 with SMTP id hg8mr16924698qab.74.1359399164159; Mon, 28 Jan 2013 10:52:44 -0800 (PST) Received: from anchor.twiddle.home.com (50-194-63-110-static.hfc.comcastbusiness.net. [50.194.63.110]) by mx.google.com with ESMTPS id bb8sm6203883qeb.5.2013.01.28.10.52.42 (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 28 Jan 2013 10:52:43 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Date: Mon, 28 Jan 2013 10:52:34 -0800 Message-Id: <1359399154-13050-3-git-send-email-rth@twiddle.net> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1359399154-13050-1-git-send-email-rth@twiddle.net> References: <1359399154-13050-1-git-send-email-rth@twiddle.net> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.216.177 Subject: [Qemu-devel] [PATCH 2/2] host-utils: Improve mulu64 and muls64 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 The new formulation makes better use of add-with-carry type insns that the host may have. Use gcc's sign adjustment trick to avoid having to perform a 128-bit negation. Signed-off-by: Richard Henderson --- util/host-utils.c | 98 +++++++++++++++++++++---------------------------------- 1 file changed, 38 insertions(+), 60 deletions(-) diff --git a/util/host-utils.c b/util/host-utils.c index 2d06a2c..3908b3a 100644 --- a/util/host-utils.c +++ b/util/host-utils.c @@ -27,79 +27,57 @@ #include #include "qemu/host-utils.h" -//#define DEBUG_MULDIV - /* Long integer helpers */ #ifndef CONFIG_INT128 -static void add128 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b) -{ - *plow += a; - /* carry test */ - if (*plow < a) - (*phigh)++; - *phigh += b; -} - -static void neg128 (uint64_t *plow, uint64_t *phigh) -{ - *plow = ~*plow; - *phigh = ~*phigh; - add128(plow, phigh, 1, 0); -} - -static void mul64 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b) +/* Unsigned 64x64 -> 128 multiplication */ +inline void mulu64 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b) { - uint32_t a0, a1, b0, b1; - uint64_t v; - - a0 = a; - a1 = a >> 32; - - b0 = b; - b1 = b >> 32; + typedef union { + uint64_t ll; + struct { +#ifdef HOST_WORDS_BIGENDIAN + uint32_t high, low; +#else + uint32_t low, high; +#endif + } l; + } LL; + LL rl, rm, rn, rh, a0, b0; + uint64_t c; - v = (uint64_t)a0 * (uint64_t)b0; - *plow = v; - *phigh = 0; + a0.ll = a; + b0.ll = b; - v = (uint64_t)a0 * (uint64_t)b1; - add128(plow, phigh, v << 32, v >> 32); + rl.ll= (uint64_t)a0.l.low * (uint64_t)b0.l.low; + rm.ll = (uint64_t)a0.l.low * (uint64_t)b0.l.high; + rn.ll = (uint64_t)a0.l.high * (uint64_t)b0.l.low; + rh.ll = (uint64_t)a0.l.high * (uint64_t)b0.l.high; - v = (uint64_t)a1 * (uint64_t)b0; - add128(plow, phigh, v << 32, v >> 32); + c = (uint64_t)rl.l.high + rm.l.low + rn.l.low; + rl.l.high = c; + c >>= 32; + c += (uint64_t)rm.l.high + rn.l.high + rh.l.low; + rh.l.low = c; + rh.l.high += (uint32_t)(c >> 32); - v = (uint64_t)a1 * (uint64_t)b1; - *phigh += v; -} - -/* Unsigned 64x64 -> 128 multiplication */ -void mulu64 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b) -{ - mul64(plow, phigh, a, b); -#if defined(DEBUG_MULDIV) - printf("mulu64: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n", - a, b, *phigh, *plow); -#endif + *plow = rl.ll; + *phigh = rh.ll; } /* Signed 64x64 -> 128 multiplication */ void muls64 (uint64_t *plow, uint64_t *phigh, int64_t a, int64_t b) { - int sa, sb; + uint64_t rh; + + mulu64(plow, &rh, a, b); - sa = (a < 0); - if (sa) - a = -a; - sb = (b < 0); - if (sb) - b = -b; - mul64(plow, phigh, a, b); - if (sa ^ sb) { - neg128(plow, phigh); + /* Adjust for signs. */ + if (b < 0) { + rh -= a; } -#if defined(DEBUG_MULDIV) - printf("muls64: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n", - a, b, *phigh, *plow); -#endif + if (a < 0) { + rh -= b; + } + *phigh = rh; } #endif /* !CONFIG_INT128 */