From patchwork Fri Jul 1 17:04:32 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 9210247 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 59ADC607D8 for ; Fri, 1 Jul 2016 17:10:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 45141285BA for ; Fri, 1 Jul 2016 17:10:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 38364286CF; Fri, 1 Jul 2016 17:10:08 +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=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9CC27285BA for ; Fri, 1 Jul 2016 17:10:07 +0000 (UTC) Received: from localhost ([::1]:34640 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bJ1x8-000865-Pb for patchwork-qemu-devel@patchwork.kernel.org; Fri, 01 Jul 2016 13:10:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53541) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bJ1sH-0002CP-97 for qemu-devel@nongnu.org; Fri, 01 Jul 2016 13:05:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bJ1sD-0005LM-O2 for qemu-devel@nongnu.org; Fri, 01 Jul 2016 13:05:02 -0400 Received: from mail-pa0-x243.google.com ([2607:f8b0:400e:c03::243]:35549) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bJ1sD-0005Kt-DS for qemu-devel@nongnu.org; Fri, 01 Jul 2016 13:05:01 -0400 Received: by mail-pa0-x243.google.com with SMTP id hf6so10129823pac.2 for ; Fri, 01 Jul 2016 10:05:01 -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:in-reply-to:references; bh=xM+Tc8NLbjZnuzHy/juXxmFfYyEM9rh4yQ6SkUYHADo=; b=Fi8NNL8+6N0+5ZQ0VM4W41rgH5cGdbobEQw/aHAMDVuphqqm7Xff70VFXhpx5Xm3aR oJmTk6MjHdP0Jm9P97K9FPfguO2KRdDPGfhxtsInZmq/rqEIo7EmQIp5ny24oa67cMZB ZqYpLM8phgUKOf0rGu8QF6qseWUOvi3NZcTzlbVNyYJGiEmy/uR8PIavwcN67Np8GGVY 3+LJdRCRbDIZHGtCZDSYpvtvaCd3vL1T3gTnTkwMLNBUwwCySU1YZDVgcm5W5VG1npQk 7UBFE49xCpvADUzI+AUoqLCafMSRBrXk5Ut/awts2MJlbAEo1ss6VO98Ive2VnYbsuCq NQzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=xM+Tc8NLbjZnuzHy/juXxmFfYyEM9rh4yQ6SkUYHADo=; b=byYqXHKcwE1/d9QjVm4nASPS6WoWHPguGqs+1fT+xPB3ODbGrJq7ANWtyOHe3vGisF O5cKMYnVHV+zuGGhNMR7kTv6lnhz81IdoCCxsTI2niDoPmU93XJKKhAdftRktE7pWl4i /hfy8Bzj0BN3FhIViauLcmfBM+LclxLOhE3w6XjRG4yoKNjqhbUCd3vxsqPyQIkVceQU tQwYCiJ86WVuWbjczhaljA8rpn9Nid2BKlZ5FL+Xo0tfytPE48/Ol5/Wv/Q3bFuBmyTS PXi5a7xFVJPz3QrkjJxibfatxGPuqfR20w5pLowzBq+ubNY8vBXmRIPXSmVTYBpI6cAZ pWzA== X-Gm-Message-State: ALyK8tIQX4lKVoHWkql2oETV5fiU0+nr78G+lco89NvRVYGyYpfexgdjHbBHUcWChB+W9Q== X-Received: by 10.66.182.194 with SMTP id eg2mr33754002pac.159.1467392700573; Fri, 01 Jul 2016 10:05:00 -0700 (PDT) Received: from bigtime.twiddle.net (71-37-54-227.tukw.qwest.net. [71.37.54.227]) by smtp.gmail.com with ESMTPSA id ff9sm2652229pac.5.2016.07.01.10.04.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 01 Jul 2016 10:05:00 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Fri, 1 Jul 2016 10:04:32 -0700 Message-Id: <1467392693-22715-7-git-send-email-rth@twiddle.net> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1467392693-22715-1-git-send-email-rth@twiddle.net> References: <1467392693-22715-1-git-send-email-rth@twiddle.net> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c03::243 Subject: [Qemu-devel] [PATCH v2 06/27] int128: Use complex numbers if advisable X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, serge.fdrv@gmail.com, cota@braap.org, alex.bennee@linaro.org, peter.maydell@linaro.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP If __int128 is not supported, prefer a base type that is returned in registers rather than memory. Signed-off-by: Richard Henderson --- include/qemu/int128.h | 110 +++++++++++++++++++++++++++++++------------------- 1 file changed, 69 insertions(+), 41 deletions(-) diff --git a/include/qemu/int128.h b/include/qemu/int128.h index 67440fa..ab67275 100644 --- a/include/qemu/int128.h +++ b/include/qemu/int128.h @@ -139,27 +139,37 @@ static inline void int128_subfrom(Int128 *a, Int128 b) #else /* !CONFIG_INT128 */ -typedef struct Int128 Int128; +/* Here we are catering to the ABI of the host. If the host returns + 64-bit complex in registers, but the 128-bit structure in memory, + then choose the complex representation. */ +#if defined(__GNUC__) \ + && (defined(__powerpc__) || defined(__sparc__)) \ + && !defined(CONFIG_TCG_INTERPRETER) +typedef _Complex unsigned long long Int128; -struct Int128 { - uint64_t lo; - int64_t hi; -}; +static inline Int128 int128_make128(uint64_t lo, uint64_t hi) +{ + return lo + 1i * hi; +} -static inline Int128 int128_make64(uint64_t a) +static inline uint64_t int128_getlo(Int128 a) { - return (Int128) { a, 0 }; + return __real__ a; } -static inline Int128 int128_make128(uint64_t lo, uint64_t hi) +static inline int64_t int128_gethi(Int128 a) { - return (Int128) { lo, hi }; + return __imag__ a; } +#else +typedef struct Int128 { + uint64_t lo; + int64_t hi; +} Int128; -static inline uint64_t int128_get64(Int128 a) +static inline Int128 int128_make128(uint64_t lo, uint64_t hi) { - assert(!a.hi); - return a.lo; + return (Int128) { lo, hi }; } static inline uint64_t int128_getlo(Int128 a) @@ -171,78 +181,92 @@ static inline int64_t int128_gethi(Int128 a) { return a.hi; } +#endif /* complex 128 */ + +static inline Int128 int128_make64(uint64_t a) +{ + return int128_make128(a, 0); +} + +static inline uint64_t int128_get64(Int128 a) +{ + assert(int128_gethi(a) == 0); + return int128_getlo(a); +} static inline Int128 int128_zero(void) { - return int128_make64(0); + return int128_make128(0, 0); } static inline Int128 int128_one(void) { - return int128_make64(1); + return int128_make128(1, 0); } static inline Int128 int128_2_64(void) { - return (Int128) { 0, 1 }; + return int128_make128(0, 1); } static inline Int128 int128_exts64(int64_t a) { - return (Int128) { .lo = a, .hi = (a < 0) ? -1 : 0 }; + return int128_make128(a, a < 0 ? -1 : 0); } static inline Int128 int128_and(Int128 a, Int128 b) { - return (Int128) { a.lo & b.lo, a.hi & b.hi }; + uint64_t al = int128_getlo(a), bl = int128_getlo(b); + uint64_t ah = int128_gethi(a), bh = int128_gethi(b); + + return int128_make128(al & bl, ah & bh); } static inline Int128 int128_rshift(Int128 a, int n) { - int64_t h; - if (!n) { - return a; - } - h = a.hi >> (n & 63); - if (n >= 64) { + uint64_t al = int128_getlo(a), ah = int128_gethi(a); + int64_t h = ((int64_t)ah) >> (n & 63); + if (n & 64) { return int128_make128(h, h >> 63); } else { - return int128_make128((a.lo >> n) | ((uint64_t)a.hi << (64 - n)), h); + return int128_make128((al >> n) | (ah << (64 - n)), h); } } static inline Int128 int128_add(Int128 a, Int128 b) { - uint64_t lo = a.lo + b.lo; + uint64_t al = int128_getlo(a), bl = int128_getlo(b); + uint64_t ah = int128_gethi(a), bh = int128_gethi(b); + uint64_t lo = al + bl; - /* a.lo <= a.lo + b.lo < a.lo + k (k is the base, 2^64). Hence, - * a.lo + b.lo >= k implies 0 <= lo = a.lo + b.lo - k < a.lo. - * Similarly, a.lo + b.lo < k implies a.lo <= lo = a.lo + b.lo < k. - * - * So the carry is lo < a.lo. - */ - return int128_make128(lo, (uint64_t)a.hi + b.hi + (lo < a.lo)); + return int128_make128(lo, ah + bh + (lo < al)); } -static inline Int128 int128_neg(Int128 a) +static inline Int128 int128_sub(Int128 a, Int128 b) { - uint64_t lo = -a.lo; - return int128_make128(lo, ~(uint64_t)a.hi + !lo); + uint64_t al = int128_getlo(a), bl = int128_getlo(b); + uint64_t ah = int128_gethi(a), bh = int128_gethi(b); + + return int128_make128(al - bl, ah - bh - (al < bl)); } -static inline Int128 int128_sub(Int128 a, Int128 b) +static inline Int128 int128_neg(Int128 a) { - return int128_make128(a.lo - b.lo, (uint64_t)a.hi - b.hi - (a.lo < b.lo)); + uint64_t al = int128_getlo(a), ah = int128_gethi(a); + return int128_make128(-al, ~ah + !al); } static inline bool int128_nonneg(Int128 a) { - return a.hi >= 0; + return int128_gethi(a) >= 0; } static inline bool int128_eq(Int128 a, Int128 b) { - return a.lo == b.lo && a.hi == b.hi; + uint64_t al = int128_getlo(a), bl = int128_getlo(b); + uint64_t ah = int128_gethi(a), bh = int128_gethi(b); + + return al == bl && ah == bh; } static inline bool int128_ne(Int128 a, Int128 b) @@ -252,7 +276,10 @@ static inline bool int128_ne(Int128 a, Int128 b) static inline bool int128_ge(Int128 a, Int128 b) { - return a.hi > b.hi || (a.hi == b.hi && a.lo >= b.lo); + uint64_t al = int128_getlo(a), bl = int128_getlo(b); + int64_t ah = int128_gethi(a), bh = int128_gethi(b); + + return ah > bh || (ah == bh && al >= bl); } static inline bool int128_lt(Int128 a, Int128 b) @@ -272,7 +299,8 @@ static inline bool int128_gt(Int128 a, Int128 b) static inline bool int128_nz(Int128 a) { - return a.lo || a.hi; + uint64_t al = int128_getlo(a), ah = int128_gethi(a); + return (al | ah) != 0; } static inline Int128 int128_min(Int128 a, Int128 b)