From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 4/6] Add overload of fits_in_type
Date: Tue, 28 Mar 2023 09:49:40 -0600 [thread overview]
Message-ID: <20230328-expr-128-bit-v1-4-f9eeb0143318@adacore.com> (raw)
In-Reply-To: <20230328-expr-128-bit-v1-0-f9eeb0143318@adacore.com>
This adds an overload of fits_in_type that accepts a gdb_mpz. A
subsequent patch will use this.
---
gdb/parse.c | 26 ++++++++++++++++++++++++++
gdb/parser-defs.h | 2 ++
2 files changed, 28 insertions(+)
diff --git a/gdb/parse.c b/gdb/parse.c
index 24db3ca57d9..23545e32160 100644
--- a/gdb/parse.c
+++ b/gdb/parse.c
@@ -648,6 +648,32 @@ fits_in_type (int n_sign, ULONGEST n, int type_bits, bool type_signed_p)
else
gdb_assert_not_reached ("");
}
+
+/* Return true if the number N_SIGN * N fits in a type with TYPE_BITS and
+ TYPE_SIGNED_P. N_SIGNED is either 1 or -1. */
+
+bool
+fits_in_type (int n_sign, const gdb_mpz &n, int type_bits, bool type_signed_p)
+{
+ /* N must be nonnegative. */
+ gdb_assert (n.sgn () >= 0);
+
+ /* Zero always fits. */
+ /* Normalize -0. */
+ if (n.sgn () == 0)
+ return true;
+
+ if (n_sign == -1 && !type_signed_p)
+ /* Can't fit a negative number in an unsigned type. */
+ return false;
+
+ gdb_mpz max = gdb_mpz::pow (2, (type_signed_p
+ ? type_bits - 1
+ : type_bits));
+ if (n_sign == -1)
+ return n <= max;
+ return n < max;
+}
\f
/* This function avoids direct calls to fprintf
in the parser generated debug code. */
diff --git a/gdb/parser-defs.h b/gdb/parser-defs.h
index 1780d856744..62829a86f9a 100644
--- a/gdb/parser-defs.h
+++ b/gdb/parser-defs.h
@@ -414,6 +414,8 @@ extern bool parse_float (const char *p, int len,
const struct type *type, gdb_byte *data);
extern bool fits_in_type (int n_sign, ULONGEST n, int type_bits,
bool type_signed_p);
+extern bool fits_in_type (int n_sign, const gdb_mpz &n, int type_bits,
+ bool type_signed_p);
\f
/* Function used to avoid direct calls to fprintf
--
2.39.1
next prev parent reply other threads:[~2023-03-28 15:49 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-28 15:49 [PATCH 0/6] Add 128-bit integers to Ada and Rust parsers Tom Tromey
2023-03-28 15:49 ` [PATCH 1/6] Additions to gdb_mpz Tom Tromey
2023-03-28 15:49 ` [PATCH 2/6] Convert long_const_operation to use gdb_mpz Tom Tromey
2023-03-28 15:49 ` [PATCH 3/6] Add 128-bit integer support to the Rust parser Tom Tromey
2023-03-28 15:49 ` Tom Tromey [this message]
2023-03-28 15:49 ` [PATCH 5/6] Remove some Ada parser helper functions Tom Tromey
2023-03-28 15:49 ` [PATCH 6/6] Add 128-bit integer support to the Ada parser Tom Tromey
2023-04-17 17:03 ` [PATCH 0/6] Add 128-bit integers to Ada and Rust parsers Tom Tromey
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=20230328-expr-128-bit-v1-4-f9eeb0143318@adacore.com \
--to=tromey@adacore.com \
--cc=gdb-patches@sourceware.org \
/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 read-only IMAP folder(s) and NNTP newsgroup(s).