public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Rewrite gdb_mpz::operator==
@ 2023-04-19 15:09 Tom Tromey
  2023-04-19 16:23 ` Simon Marchi
  0 siblings, 1 reply; 7+ messages in thread
From: Tom Tromey @ 2023-04-19 15:09 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

Simon pointed out that the recent changes to gdb_mpz caused a build
failure on 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.
---
 gdb/gmp-utils.h | 38 ++++++++++++++------------------------
 1 file changed, 14 insertions(+), 24 deletions(-)

diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h
index d05c11ecbe4..3586d01b0f9 100644
--- a/gdb/gmp-utils.h
+++ b/gdb/gmp-utils.h
@@ -323,31 +323,21 @@ 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;
-  }
-
   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 (other == (T) (long) other)
+	  return mpz_cmp_si (m_val, other) == 0;
+      }
+    else
+      {
+	if (other == (T) (unsigned long) other)
+	  return mpz_cmp_ui (m_val, other) == 0;
+      }
+    return *this == gdb_mpz (other);
   }
 
   bool operator== (const gdb_mpz &other) const
-- 
2.39.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-04-25 18:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-19 15:09 [PATCH] Rewrite gdb_mpz::operator== 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
2023-04-25 18:55           ` Simon Marchi

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).