Rewrite gdb_mpz::operator==

Message ID 20230419150931.2047832-1-tromey@adacore.com
State New
Headers
Series Rewrite gdb_mpz::operator== |

Commit Message

Tom Tromey April 19, 2023, 3:09 p.m. UTC
  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(-)
  

Comments

Simon Marchi April 19, 2023, 4:23 p.m. UTC | #1
On 4/19/23 11:09, Tom Tromey via Gdb-patches wrote:
> 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

For some reason, just macOS on amd64, not arm64.

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

It's a bit hard to understand what the intent is, if you don't know the
problematic already.  Could you add a comment explaining that?

Also, wouldn't it work to compare size?  Like:

  if (sizeof (T) <= sizeof (long))

Simon
  
Tom Tromey April 21, 2023, 1 p.m. UTC | #2
>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:

Simon> It's a bit hard to understand what the intent is, if you don't know the
Simon> problematic already.  Could you add a comment explaining that?

Sure.

Simon> Also, wouldn't it work to compare size?  Like:
Simon>   if (sizeof (T) <= sizeof (long))

Yeah, though I was concerned that the compiler might emit warnings about
always-true conditions in this case.

Tom
  
Simon Marchi April 21, 2023, 1:13 p.m. UTC | #3
On 4/21/23 09:00, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:
> 
> Simon> It's a bit hard to understand what the intent is, if you don't know the
> Simon> problematic already.  Could you add a comment explaining that?
> 
> Sure.
> 
> Simon> Also, wouldn't it work to compare size?  Like:
> Simon>   if (sizeof (T) <= sizeof (long))
> 
> Yeah, though I was concerned that the compiler might emit warnings about
> always-true conditions in this case.

Well, that would be surprising for code in a template, where T is not
know in advance.

Simon
  
Tom Tromey April 21, 2023, 3:02 p.m. UTC | #4
>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:

>> Yeah, though I was concerned that the compiler might emit warnings about
>> always-true conditions in this case.

Simon> Well, that would be surprising for code in a template, where T is not
Simon> know in advance.

I feel like it happened in the past, but maybe it doesn't any more.
I'll rewrite this patch.

Tom
  
Tom Tromey April 24, 2023, 4:31 p.m. UTC | #5
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
  
Simon Marchi April 25, 2023, 6:55 p.m. UTC | #6
> 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>>>
The template expression would fit on a single line, if you'd prefer to
write it that way.

Otherwise, LGTM.  I think it's now relatively clear what the code tries
to achieve.

Approved-By: Simon Marchi <simon.marchi@efficios.com>

Simon
  

Patch

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