From: Tom Tromey <tromey@adacore.com>
To: Tom Tromey <tromey@adacore.com>
Cc: Simon Marchi <simark@simark.ca>, gdb-patches@sourceware.org
Subject: Re: [PATCH] Rewrite gdb_mpz::operator==
Date: Mon, 24 Apr 2023 10:31:48 -0600 [thread overview]
Message-ID: <87edo9z1fv.fsf@tromey.com> (raw)
In-Reply-To: <87ttx9z3be.fsf@tromey.com> (Tom Tromey's message of "Fri, 21 Apr 2023 09:02:13 -0600")
Tom> I feel like it happened in the past, but maybe it doesn't any more.
Tom> I'll rewrite this patch.
Here's v3.
Tom
commit f12c9b23d3ac219e08818935930f5ee891396158
Author: Tom Tromey <tromey@adacore.com>
Date: Mon Apr 17 12:59:57 2023 -0600
Rewrite gdb_mpz::operator==
Simon pointed out that the recent changes to gdb_mpz caused a build
failure on amd64 macOS. It turns out to be somewhat difficult to
overload a method in a way that will work "naturally" for all integer
types; especially in a case like gdb_mpz::operator==, where it's
desirable to special case all integer types that are no wider than
'long'.
After a false start, I came up with this patch, which seems to work.
It applies the desirable GMP special cases directly in the body,
rather than via overloads.
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h
index d05c11ecbe4..1bbdd9564fa 100644
--- a/gdb/gmp-utils.h
+++ b/gdb/gmp-utils.h
@@ -323,31 +323,26 @@ struct gdb_mpz
return mpz_cmp_si (m_val, other) < 0;
}
- bool operator== (int other) const
- {
- return mpz_cmp_si (m_val, other) == 0;
- }
-
- bool operator== (long other) const
- {
- return mpz_cmp_si (m_val, other) == 0;
- }
-
- bool operator== (unsigned long other) const
- {
- return mpz_cmp_ui (m_val, other) == 0;
- }
-
+ /* We want an operator== that can handle all integer types. For
+ types that are 'long' or narrower, we can use a GMP function and
+ avoid boxing the RHS. But, because overloading based on integer
+ type is a pain in C++, we accept all such types here and check
+ the size in the body. */
template<typename T,
- typename = gdb::Requires<
- gdb::And<std::is_integral<T>,
- std::integral_constant<bool,
- (sizeof (T) > sizeof (long))>>
- >
- >
- bool operator== (T src)
- {
- return *this == gdb_mpz (src);
+ typename = gdb::Requires<std::is_integral<T>>>
+ bool operator== (T other) const
+ {
+ if (std::is_signed<T>::value)
+ {
+ if (sizeof (T) <= sizeof (long))
+ return mpz_cmp_si (m_val, other) == 0;
+ }
+ else
+ {
+ if (sizeof (T) <= sizeof (unsigned long))
+ return mpz_cmp_ui (m_val, other) == 0;
+ }
+ return *this == gdb_mpz (other);
}
bool operator== (const gdb_mpz &other) const
next prev parent reply other threads:[~2023-04-24 16:31 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-19 15:09 Tom Tromey
2023-04-19 16:23 ` Simon Marchi
2023-04-21 13:00 ` Tom Tromey
2023-04-21 13:13 ` Simon Marchi
2023-04-21 15:02 ` Tom Tromey
2023-04-24 16:31 ` Tom Tromey [this message]
2023-04-25 18:55 ` Simon Marchi
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=87edo9z1fv.fsf@tromey.com \
--to=tromey@adacore.com \
--cc=gdb-patches@sourceware.org \
--cc=simark@simark.ca \
/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).