diff options
author | Tom Tromey <tromey@adacore.com> | 2023-03-27 14:35:17 -0600 |
---|---|---|
committer | Tom Tromey <tromey@adacore.com> | 2023-04-17 10:43:06 -0600 |
commit | 767c4b92bc4333cb1b49a42b8d012170a40900fe (patch) | |
tree | 767c74a10a155e97e09806c2208d43510598c691 /gdb/gmp-utils.h | |
parent | c53c6186c8d69586a71a7f8284a9ed7fa8cb207f (diff) | |
download | gdb-767c4b92bc4333cb1b49a42b8d012170a40900fe.zip gdb-767c4b92bc4333cb1b49a42b8d012170a40900fe.tar.gz gdb-767c4b92bc4333cb1b49a42b8d012170a40900fe.tar.bz2 |
Additions to gdb_mpz
In preparation for adding more 128-bit support to gdb, a few additions
to gdb_mpz are needed.
First, this adds a new 'as_integer_truncate' method. This method
works like 'as_integer' but does not require the value to fit in the
target type -- it just truncates.
Second, gdb_mpz::export_bits is changed to handle the somewhat unusual
situation of zero-length types. This can happen for a Rust '()' type;
but I think other languages have zero-bit integer types as well.
Finally, this adds some operator== overloads.
Diffstat (limited to 'gdb/gmp-utils.h')
-rw-r--r-- | gdb/gmp-utils.h | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h index f294ab6..d05c11e 100644 --- a/gdb/gmp-utils.h +++ b/gdb/gmp-utils.h @@ -119,11 +119,17 @@ struct gdb_mpz return result; } - /* Convert VAL to an integer of the given type. + /* Convert this value to an integer of the given type. The return type can signed or unsigned, with no size restriction. */ template<typename T> T as_integer () const; + /* Convert this value to an integer of the given type. If this + value is too large, it is truncated. + + The return type can signed or unsigned, with no size restriction. */ + template<typename T> T as_integer_truncate () const; + /* Set VAL by importing the number stored in the byte array (BUF), using the given BYTE_ORDER. The size of the data to read is the byte array's size. @@ -312,7 +318,7 @@ struct gdb_mpz return mpz_cmp (m_val, other.m_val) <= 0; } - bool operator< (int other) const + bool operator< (long other) const { return mpz_cmp_si (m_val, other) < 0; } @@ -322,6 +328,28 @@ struct gdb_mpz 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); + } + bool operator== (const gdb_mpz &other) const { return mpz_cmp (m_val, other.m_val) == 0; @@ -612,4 +640,20 @@ gdb_mpz::as_integer () const return result; } +/* See declaration above. */ + +template<typename T> +T +gdb_mpz::as_integer_truncate () const +{ + T result; + + this->export_bits ({(gdb_byte *) &result, sizeof (result)}, + 0 /* endian (0 = native) */, + !std::is_signed<T>::value /* unsigned_p */, + false /* safe */); + + return result; +} + #endif |