[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH] int128: optimize
From: |
Paolo Bonzini |
Subject: |
Re: [Qemu-devel] [PATCH] int128: optimize |
Date: |
Thu, 20 Jun 2013 23:20:48 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130514 Thunderbird/17.0.6 |
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