qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: peter.maydell@linaro.org, rth@twiddle.net
Subject: [Qemu-devel] [PATCH] int128: optimize
Date: Thu, 20 Jun 2013 17:00:57 +0200	[thread overview]
Message-ID: <1371740457-27445-1-git-send-email-pbonzini@redhat.com> (raw)

For add and sub, carry computation only requires checking one of the
arguments (any for add, the LHS for sub because the RHS is negated).
For neg, we can similarly optimize computation of the carry.

For ge, we can just do lexicographic order.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
	Will post unit tests soon.

 include/qemu/int128.h | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/include/qemu/int128.h b/include/qemu/int128.h
index bfe7678..d36cc7a 100644
--- a/include/qemu/int128.h
+++ b/include/qemu/int128.h
@@ -55,21 +55,20 @@ static inline Int128 int128_rshift(Int128 a, int n)
 
 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;
+    uint64_t lo = a.lo + b.lo;
+    return (Int128) { lo, (lo < a.lo) + a.hi + b.hi };
 }
 
 static inline Int128 int128_neg(Int128 a)
 {
-    a.lo = ~a.lo;
-    a.hi = ~a.hi;
-    return int128_add(a, int128_one());
+    uint64_t lo = -a.lo;
+    return (Int128) { lo, ~a.hi + !lo };
 }
 
 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 };
 }
 
 static inline bool int128_nonneg(Int128 a)
@@ -89,7 +88,7 @@ static inline bool int128_ne(Int128 a, Int128 b)
 
 static inline bool int128_ge(Int128 a, Int128 b)
 {
-    return int128_nonneg(int128_sub(a, b));
+    return a.hi > b.hi || (a.hi == b.hi && a.lo >= b.lo);
 }
 
 static inline bool int128_lt(Int128 a, Int128 b)
-- 
1.8.1.4

             reply	other threads:[~2013-06-20 15:01 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-20 15:00 Paolo Bonzini [this message]
2013-06-20 16:46 ` [Qemu-devel] [PATCH] int128: optimize Richard Henderson
2013-06-20 21:20   ` Paolo Bonzini
  -- strict thread matches above, loose matches on Subject: below --
2013-07-02 12:46 Jay Foad
2013-07-02 14:54 ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1371740457-27445-1-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).