From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:39885) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rvn90-00021z-VD for qemu-devel@nongnu.org; Fri, 10 Feb 2012 04:51:59 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Rvn8w-0001Hi-GM for qemu-devel@nongnu.org; Fri, 10 Feb 2012 04:51:54 -0500 Received: from mail-tul01m020-f173.google.com ([209.85.214.173]:43717) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rvn8w-0001He-Cf for qemu-devel@nongnu.org; Fri, 10 Feb 2012 04:51:50 -0500 Received: by obbup16 with SMTP id up16so4462529obb.4 for ; Fri, 10 Feb 2012 01:51:49 -0800 (PST) MIME-Version: 1.0 Date: Fri, 10 Feb 2012 09:51:48 +0000 Message-ID: From: Jay Foad Content-Type: text/plain; charset=UTF-8 Subject: Re: [Qemu-devel] [PATCH 1/3] Add support for 128-bit arithmeticRe: [PATCH 1/3] Add support for 128-bit arithmetic List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: avi@redhat.com Cc: qemu-devel@nongnu.org On 30 Oct 2011, Avi Kivity wrote: > The memory API supports 64-bit buses (e.g. PCI). A size on such a bus cannot > be represented with a 64-bit data type, if both 0 and the entire address > space size are to be represented. Futhermore, any address arithemetic may > overflow and return unexpected results. > > Introduce a 128-bit signed integer type for use in such cases. Addition, > subtraction, and comparison are the only operations supported. > > Signed-off-by: Avi Kivity > --- > int128.h | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 files changed, 116 insertions(+), 0 deletions(-) > create mode 100644 int128.h > > diff --git a/int128.h b/int128.h > new file mode 100644 > index 0000000..b3864b6 > --- /dev/null > +++ b/int128.h > @@ -0,0 +1,116 @@ > +#ifndef INT128_H > +#define INT128_H > + > +typedef struct Int128 Int128; > + > +struct Int128 { > + uint64_t lo; > + int64_t hi; > +}; > +static inline Int128 int128_add(Int128 a, Int128 b) > +{ > + Int128 r = { a.lo + b.lo, a.hi + b.hi }; > + r.hi += (r.lo < a.lo) || (r.lo < b.lo); > + return r; > +} This is a bit redundant. You only need either: r.hi += r.lo < a.lo; or: r.hi += r.lo < b.lo; because the way that two's complement addition works means that r.lo will always be less than both a.lo and b.lo, or greater-than-or-equal-to both of them. > +static inline bool int128_ge(Int128 a, Int128 b) > +{ > + return int128_nonneg(int128_sub(a, b)); > +} This is wrong if you get signed overflow in int128_sub(a, b). > Regardless, the need for careful coding means subtle bugs, Indeed :-) Jay.