public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] Avoid a copy in gdb_mpz::safe_export
@ 2023-03-27 14:25 Tom Tromey
0 siblings, 0 replies; only message in thread
From: Tom Tromey @ 2023-03-27 14:25 UTC (permalink / raw)
To: gdb-cvs
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=c7c3708ac778625d3a87aad541de5f0666acbcc5
commit c7c3708ac778625d3a87aad541de5f0666acbcc5
Author: Tom Tromey <tromey@adacore.com>
Date: Wed Mar 1 12:33:27 2023 -0700
Avoid a copy in gdb_mpz::safe_export
Currently, gdb_mpz::safe_export will always make a copy of *this.
However, this copy isn't always needed. This patch makes this code
slightly more efficient, by avoiding the copy when possible.
Diff:
---
gdb/gmp-utils.c | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/gdb/gmp-utils.c b/gdb/gmp-utils.c
index d134bc32a1d..57f5f9766b9 100644
--- a/gdb/gmp-utils.c
+++ b/gdb/gmp-utils.c
@@ -81,7 +81,8 @@ gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
{
gdb_assert (buf.size () > 0);
- if (mpz_sgn (m_val) == 0)
+ int sign = mpz_sgn (m_val);
+ if (sign == 0)
{
/* Our value is zero, so no need to call mpz_export to do the work,
especially since mpz_export's documentation explicitly says
@@ -121,17 +122,16 @@ gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
lo.str ().c_str (),
hi.str ().c_str ());
- gdb_mpz exported_val (m_val);
-
- if (mpz_cmp_ui (exported_val.m_val, 0) < 0)
+ const gdb_mpz *exported_val = this;
+ gdb_mpz un_signed;
+ if (sign < 0)
{
/* mpz_export does not handle signed values, so create a positive
value whose bit representation as an unsigned of the same length
would be the same as our negative value. */
- gdb_mpz neg_offset;
-
- mpz_ui_pow_ui (neg_offset.m_val, 2, buf.size () * HOST_CHAR_BIT);
- mpz_add (exported_val.m_val, exported_val.m_val, neg_offset.m_val);
+ gdb_mpz neg_offset = gdb_mpz::pow (2, buf.size () * HOST_CHAR_BIT);
+ un_signed = *exported_val + neg_offset;
+ exported_val = &un_signed;
}
/* Do the export into a buffer allocated by GMP itself; that way,
@@ -147,7 +147,7 @@ gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
size_t word_countp;
gdb::unique_xmalloc_ptr<void> exported
(mpz_export (NULL, &word_countp, -1 /* order */, buf.size () /* size */,
- endian, 0 /* nails */, exported_val.m_val));
+ endian, 0 /* nails */, exported_val->m_val));
gdb_assert (word_countp == 1);
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-03-27 14:25 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-27 14:25 [binutils-gdb] Avoid a copy in gdb_mpz::safe_export Tom Tromey
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 read-only IMAP folder(s) and NNTP newsgroup(s).