From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39373) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UpmHq-0006AK-Iw for qemu-devel@nongnu.org; Thu, 20 Jun 2013 17:20:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UpmHp-0002xm-MH for qemu-devel@nongnu.org; Thu, 20 Jun 2013 17:20:58 -0400 Received: from mail-ee0-x22b.google.com ([2a00:1450:4013:c00::22b]:36612) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UpmHp-0002xi-9K for qemu-devel@nongnu.org; Thu, 20 Jun 2013 17:20:57 -0400 Received: by mail-ee0-f43.google.com with SMTP id l10so4157114eei.16 for ; Thu, 20 Jun 2013 14:20:56 -0700 (PDT) Sender: Paolo Bonzini Message-ID: <51C37230.2040306@redhat.com> Date: Thu, 20 Jun 2013 23:20:48 +0200 From: Paolo Bonzini MIME-Version: 1.0 References: <1371740457-27445-1-git-send-email-pbonzini@redhat.com> <51C331E0.6030402@twiddle.net> In-Reply-To: <51C331E0.6030402@twiddle.net> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH] int128: optimize List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Richard Henderson Cc: peter.maydell@linaro.org, qemu-devel@nongnu.org Il 20/06/2013 18:46, Richard Henderson ha scritto: > On 06/20/2013 08:00 AM, Paolo Bonzini wrote: >> static inline Int128 int128_sub(Int128 a, Int128 b) >> { >> - return int128_add(a, int128_neg(b)); >> + uint64_t lo = a.lo - b.lo; >> + return (Int128) { lo, (lo < a.lo) + a.hi - b.hi }; > > This one isn't right. Consider { 2, 0 } - { 2, 0 } > > lo = 2 - 2 = 0; > = { 0, (0 < 2) + 0 - 0 } > = { 0, 1 } > > I'd be happier with a more traditional > > (Int128){ a.lo - b.lo, a.hi - b.hi - (a.lo < b.lo) }; Yeah, I wasn't quite sure of this and I was waiting for testcases to prove me wrong... To fix it in the style I used you need (Int128){ lo, a.hi - b.hi - (lo > a.lo) } (We have to sum a + ~b + 1. We have lo = a.lo + ~b.lo + 1, from which the carry-out is either lo <= a.lo or lo <= ~b.lo, using <= because of the carry-in. Then the high part is a.hi + ~b.hi + (lo <= a.lo) = a.hi + (-1 - b.hi) + 1 - (lo > a.lo) = a.hi - b.hi - (lo > a.lo) ). But I'll go with your version, it probably generates better code too. Paolo