* [PATCH 0/6] Make GMP data members private
@ 2023-02-23 19:47 Tom Tromey
2023-02-23 19:47 ` [PATCH 1/6] Clean up gmp-utils.h includes Tom Tromey
` (6 more replies)
0 siblings, 7 replies; 8+ messages in thread
From: Tom Tromey @ 2023-02-23 19:47 UTC (permalink / raw)
To: gdb-patches
This series changes the GMP code in gmp-utils.h to add methods and
operators, and then to make the data members private. Ages ago I told
Simon I'd do this, then I dropped it for years.
I've only added exactly the operators that are needed right now. It's
simple to add more operators or more overloads as required.
Regression tested on x86-64 Fedora 36.
Tom
---
Tom Tromey (6):
Clean up gmp-utils.h includes
Add methods and operators to gdb_mpz
Rename gdb_mpz::val and make contents private
Add operators and methods to gdb_mpq
Rename gdb_mpq::val and make contents private
Hide the implementation of gdb_mpf
gdb/ada-lex.l | 9 +-
gdb/dwarf2/read.c | 46 +++----
gdb/gmp-utils.c | 61 ++++-----
gdb/gmp-utils.h | 248 ++++++++++++++++++++++++++++++------
gdb/unittests/gmp-utils-selftests.c | 89 +++++++------
gdb/valarith.c | 18 +--
gdb/valops.c | 15 +--
gdb/valprint.c | 2 +-
gdb/value.c | 3 +-
9 files changed, 325 insertions(+), 166 deletions(-)
---
base-commit: e98a23bfb3088fb129540d7fd26f3205e2de6f2a
change-id: 20230223-submit-gmp-hiding-8be8230612a0
Best regards,
--
Tom Tromey <tromey@adacore.com>
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 1/6] Clean up gmp-utils.h includes
2023-02-23 19:47 [PATCH 0/6] Make GMP data members private Tom Tromey
@ 2023-02-23 19:47 ` Tom Tromey
2023-02-23 19:48 ` [PATCH 2/6] Add methods and operators to gdb_mpz Tom Tromey
` (5 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2023-02-23 19:47 UTC (permalink / raw)
To: gdb-patches
gmp-utils.h includes "defs.h", but normally the rule in gdb is that
the .c files include this first. This patch changes this code to
match the rest of gdb.
---
gdb/gmp-utils.c | 1 +
gdb/gmp-utils.h | 2 --
gdb/unittests/gmp-utils-selftests.c | 1 +
3 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/gdb/gmp-utils.c b/gdb/gmp-utils.c
index b9c9b495398..8150fa5dcdf 100644
--- a/gdb/gmp-utils.c
+++ b/gdb/gmp-utils.c
@@ -15,6 +15,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
+#include "defs.h"
#include "gmp-utils.h"
/* See gmp-utils.h. */
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h
index 0e57471b2ef..0522f3453aa 100644
--- a/gdb/gmp-utils.h
+++ b/gdb/gmp-utils.h
@@ -20,8 +20,6 @@
#ifndef GMP_UTILS_H
#define GMP_UTILS_H
-#include "defs.h"
-
/* Include <stdio.h> and <stdarg.h> ahead of <gmp.h>, so as to get
access to GMP's various formatting functions. */
#include <stdio.h>
diff --git a/gdb/unittests/gmp-utils-selftests.c b/gdb/unittests/gmp-utils-selftests.c
index cc525a13fc0..e2ccd2ca094 100644
--- a/gdb/unittests/gmp-utils-selftests.c
+++ b/gdb/unittests/gmp-utils-selftests.c
@@ -17,6 +17,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
+#include "defs.h"
#include "gmp-utils.h"
#include "gdbsupport/selftest.h"
--
2.39.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 2/6] Add methods and operators to gdb_mpz
2023-02-23 19:47 [PATCH 0/6] Make GMP data members private Tom Tromey
2023-02-23 19:47 ` [PATCH 1/6] Clean up gmp-utils.h includes Tom Tromey
@ 2023-02-23 19:48 ` Tom Tromey
2023-02-23 19:48 ` [PATCH 3/6] Rename gdb_mpz::val and make contents private Tom Tromey
` (4 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2023-02-23 19:48 UTC (permalink / raw)
To: gdb-patches
This adds various methods and operators to gdb_mpz, as a step toward
hiding the implementation.
This only adds the operators that were needed. Many more could be
added as required.
---
gdb/ada-lex.l | 9 ++--
gdb/dwarf2/read.c | 46 +++++++++-----------
gdb/gmp-utils.h | 87 +++++++++++++++++++++++++++++++++++++
gdb/unittests/gmp-utils-selftests.c | 54 +++++++++++------------
gdb/valops.c | 2 +-
gdb/value.c | 3 +-
6 files changed, 140 insertions(+), 61 deletions(-)
diff --git a/gdb/ada-lex.l b/gdb/ada-lex.l
index a5cfb84dcae..69fc14f7107 100644
--- a/gdb/ada-lex.l
+++ b/gdb/ada-lex.l
@@ -422,14 +422,14 @@ processInt (struct parser_state *par_state, const char *base0,
int dig = fromhex (*num0);
if (dig >= base)
error (_("Invalid digit `%c' in based literal"), *num0);
- mpz_mul_ui (result.val, result.val, base);
- mpz_add_ui (result.val, result.val, dig);
+ result *= base;
+ result += dig;
++num0;
}
while (exp > 0)
{
- mpz_mul_ui (result.val, result.val, base);
+ result *= base;
exp -= 1;
}
@@ -462,8 +462,7 @@ processInt (struct parser_state *par_state, const char *base0,
return FLOAT;
}
- gdb_mpz maxval (ULONGEST_MAX);
- if (mpz_cmp (result.val, maxval.val) > 0)
+ if (result > gdb_mpz (ULONGEST_MAX))
error (_("Integer literal out of range"));
int int_bits = gdbarch_int_bit (par_state->gdbarch ());
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index a5bc1d89a2d..cef79b6531b 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -14756,10 +14756,10 @@ get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
&len);
if (ptr - blk->data + len <= blk->size)
{
- mpz_import (value->val, len,
- bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
- ? 1 : -1,
- 1, 0, 0, ptr);
+ value->read (gdb::make_array_view (ptr, len),
+ bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
+ ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE,
+ true);
return;
}
}
@@ -14770,10 +14770,10 @@ get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
else if (attr->form_is_block ())
{
dwarf_block *blk = attr->as_block ();
- mpz_import (value->val, blk->size,
- bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
- ? 1 : -1,
- 1, 0, 0, blk->data);
+ value->read (gdb::make_array_view (blk->data, blk->size),
+ bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
+ ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE,
+ true);
}
else
*value = gdb_mpz (attr->constant_value (1));
@@ -14825,19 +14825,19 @@ get_dwarf2_unsigned_rational_constant (struct die_info *die,
gdb_mpz denom (1);
get_dwarf2_rational_constant (die, cu, &num, &denom);
- if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
+ if (num < 0 && denom < 0)
{
- mpz_neg (num.val, num.val);
- mpz_neg (denom.val, denom.val);
+ num.negate ();
+ denom.negate ();
}
- else if (mpz_sgn (num.val) == -1)
+ else if (num < 0)
{
complaint (_("unexpected negative value for DW_AT_GNU_numerator"
" in DIE at %s"),
sect_offset_str (die->sect_off));
return;
}
- else if (mpz_sgn (denom.val) == -1)
+ else if (denom < 0)
{
complaint (_("unexpected negative value for DW_AT_GNU_denominator"
" in DIE at %s"),
@@ -14876,10 +14876,7 @@ ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
return false;
std::string copy (&encoding[start], k - start);
- if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
- return false;
-
- return true;
+ return result->set (copy.c_str (), 10);
}
/* Scan two numbers from ENCODING at OFFSET, assuming the string is of
@@ -14959,16 +14956,16 @@ finish_fixed_point_type (struct type *type, const char *suffix,
else if (attr->name == DW_AT_binary_scale)
{
LONGEST scale_exp = attr->constant_value (0);
- gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
+ gdb_mpz &num_or_denom = scale_exp > 0 ? scale_num : scale_denom;
- mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
+ num_or_denom <<= std::abs (scale_exp);
}
else if (attr->name == DW_AT_decimal_scale)
{
LONGEST scale_exp = attr->constant_value (0);
- gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
+ gdb_mpz &num_or_denom = scale_exp > 0 ? scale_num : scale_denom;
- mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
+ num_or_denom = gdb_mpz::pow (10, std::abs (scale_exp));
}
else if (attr->name == DW_AT_small)
{
@@ -14992,10 +14989,7 @@ finish_fixed_point_type (struct type *type, const char *suffix,
sect_offset_str (die->sect_off));
}
- gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
- mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
- mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
- mpq_canonicalize (scaling_factor.val);
+ type->fixed_point_info ().scaling_factor = gdb_mpq (scale_num, scale_denom);
}
/* The gnat-encoding suffix for fixed point. */
@@ -15076,7 +15070,7 @@ has_zero_over_zero_small_attribute (struct die_info *die,
gdb_mpz num (1), denom (1);
get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
- return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
+ return num == 0 && denom == 0;
}
/* Initialise and return a floating point type of size BITS suitable for
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h
index 0522f3453aa..dcb998a1656 100644
--- a/gdb/gmp-utils.h
+++ b/gdb/gmp-utils.h
@@ -89,6 +89,21 @@ struct gdb_mpz
return *this;
}
+ /* Initialize this value from a string and a base. Returns true if
+ the string was parsed successfully, false otherwise. */
+ bool set (const char *str, int base)
+ {
+ return mpz_set_str (val, str, base) != -1;
+ }
+
+ /* Return a new value that is BASE**EXP. */
+ static gdb_mpz pow (unsigned long base, unsigned long exp)
+ {
+ gdb_mpz result;
+ mpz_ui_pow_ui (result.val, base, exp);
+ return result;
+ }
+
/* Convert VAL to an integer of the given type.
The return type can signed or unsigned, with no size restriction. */
@@ -115,6 +130,56 @@ struct gdb_mpz
/* The destructor. */
~gdb_mpz () { mpz_clear (val); }
+ /* Negate this value in place. */
+ void negate ()
+ {
+ mpz_neg (val, val);
+ }
+
+ gdb_mpz &operator*= (long other)
+ {
+ mpz_mul_si (val, val, other);
+ return *this;
+ }
+
+ gdb_mpz &operator+= (unsigned long other)
+ {
+ mpz_add_ui (val, val, other);
+ return *this;
+ }
+
+ gdb_mpz &operator-= (unsigned long other)
+ {
+ mpz_sub_ui (val, val, other);
+ return *this;
+ }
+
+ gdb_mpz &operator<<= (unsigned long nbits)
+ {
+ mpz_mul_2exp (val, val, nbits);
+ return *this;
+ }
+
+ bool operator> (const gdb_mpz &other) const
+ {
+ return mpz_cmp (val, other.val) > 0;
+ }
+
+ bool operator< (int other) const
+ {
+ return mpz_cmp_si (val, other) < 0;
+ }
+
+ bool operator== (int other) const
+ {
+ return mpz_cmp_si (val, other) == 0;
+ }
+
+ bool operator== (const gdb_mpz &other) const
+ {
+ return mpz_cmp (val, other.val) == 0;
+ }
+
private:
/* Helper template for constructor and operator=. */
@@ -166,6 +231,14 @@ struct gdb_mpq
mpq_swap (val, from.val);
}
+ gdb_mpq (const gdb_mpz &num, const gdb_mpz &denom)
+ {
+ mpq_init (val);
+ mpz_set (mpq_numref (val), num.val);
+ mpz_set (mpq_denref (val), denom.val);
+ mpq_canonicalize (val);
+ }
+
/* Copy assignment operator. */
gdb_mpq &operator= (const gdb_mpq &from)
{
@@ -179,12 +252,26 @@ struct gdb_mpq
return *this;
}
+ gdb_mpq &operator= (const gdb_mpz &from)
+ {
+ mpq_set_z (val, from.val);
+ return *this;
+ }
+
/* Return a string representing VAL as "<numerator> / <denominator>". */
std::string str () const { return gmp_string_printf ("%Qd", val); }
/* Return VAL rounded to the nearest integer. */
gdb_mpz get_rounded () const;
+ /* Return this value as an integer, rounded toward zero. */
+ gdb_mpz as_integer () const
+ {
+ gdb_mpz result;
+ mpz_tdiv_q (result.val, mpq_numref (val), mpq_denref (val));
+ return result;
+ }
+
/* Set VAL from the contents of the given byte array (BUF), which
contains the unscaled value of a fixed point type object.
The byte size of the data is the size of BUF.
diff --git a/gdb/unittests/gmp-utils-selftests.c b/gdb/unittests/gmp-utils-selftests.c
index e2ccd2ca094..8e028fec88c 100644
--- a/gdb/unittests/gmp-utils-selftests.c
+++ b/gdb/unittests/gmp-utils-selftests.c
@@ -43,8 +43,8 @@ gdb_mpz_as_integer ()
/* Start with the smallest LONGEST */
l_expected = (LONGEST) 1 << (sizeof (LONGEST) * 8 - 1);
- mpz_ui_pow_ui (v.val, 2, sizeof (LONGEST) * 8 - 1);
- mpz_neg (v.val, v.val);
+ v = gdb_mpz::pow (2, sizeof (LONGEST) * 8 - 1);
+ v.negate ();
SELF_CHECK (v.as_integer<LONGEST> () == l_expected);
@@ -53,13 +53,13 @@ gdb_mpz_as_integer ()
for (int i = -256; i <= 256; i++)
{
l_expected = (LONGEST) i;
- mpz_set_si (v.val, i);
+ v = i;
SELF_CHECK (v.as_integer<LONGEST> () == l_expected);
if (i >= 0)
{
ul_expected = (ULONGEST) i;
- mpz_set_ui (v.val, i);
+ v = ul_expected;
SELF_CHECK (v.as_integer<ULONGEST> () == ul_expected);
}
}
@@ -68,16 +68,16 @@ gdb_mpz_as_integer ()
l_expected = LONGEST_MAX;
ul_expected = (ULONGEST) l_expected;
- mpz_ui_pow_ui (v.val, 2, sizeof (LONGEST) * 8 - 1);
- mpz_sub_ui (v.val, v.val, 1);
+ v = gdb_mpz::pow (2, sizeof (LONGEST) * 8 - 1);
+ v -= 1;
SELF_CHECK (v.as_integer<LONGEST> () == l_expected);
SELF_CHECK (v.as_integer<ULONGEST> () == ul_expected);
/* Try with ULONGEST_MAX. */
ul_expected = ULONGEST_MAX;
- mpz_ui_pow_ui (v.val, 2, sizeof (LONGEST) * 8);
- mpz_sub_ui (v.val, v.val, 1);
+ v = gdb_mpz::pow (2, sizeof (LONGEST) * 8);
+ v -= 1;
SELF_CHECK (v.as_integer<ULONGEST> () == ul_expected);
}
@@ -116,9 +116,9 @@ gdb_mpz_as_integer_out_of_range ()
gdb_mpz v;
/* Try LONGEST_MIN minus 1. */
- mpz_ui_pow_ui (v.val, 2, sizeof (LONGEST) * 8 - 1);
- mpz_neg (v.val, v.val);
- mpz_sub_ui (v.val, v.val, 1);
+ v = gdb_mpz::pow (2, sizeof (LONGEST) * 8 - 1);
+ v.negate ();
+ v -= 1;
check_as_integer_raises_out_of_range_error<ULONGEST> (v);
check_as_integer_raises_out_of_range_error<LONGEST> (v);
@@ -131,14 +131,14 @@ gdb_mpz_as_integer_out_of_range ()
/* Try LONGEST_MAX plus 1. */
v = LONGEST_MAX;
- mpz_add_ui (v.val, v.val, 1);
+ v += 1;
SELF_CHECK (v.as_integer<ULONGEST> () == (ULONGEST) LONGEST_MAX + 1);
check_as_integer_raises_out_of_range_error<LONGEST> (v);
/* Try ULONGEST_MAX plus 1. */
v = ULONGEST_MAX;
- mpz_add_ui (v.val, v.val, 1);
+ v += 1;
check_as_integer_raises_out_of_range_error<ULONGEST> (v);
check_as_integer_raises_out_of_range_error<LONGEST> (v);
@@ -170,8 +170,8 @@ store_and_read_back (T val, size_t buf_len, enum bfd_endian byte_order,
store_integer (buf, buf_len, byte_order, val);
/* Pre-initialize ACTUAL to something that's not the expected value. */
- mpz_set (actual.val, expected.val);
- mpz_sub_ui (actual.val, actual.val, 500);
+ actual = expected;
+ actual -= 500;
actual.read ({buf, buf_len}, byte_order, !std::is_signed<T>::value);
}
@@ -197,10 +197,10 @@ gdb_mpz_read_all_from_small ()
gdb_mpz expected, actual;
store_and_read_back (l, buf_len, BFD_ENDIAN_BIG, expected, actual);
- SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
store_and_read_back (l, buf_len, BFD_ENDIAN_LITTLE, expected, actual);
- SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
}
/* Do the same as above, but with an unsigned type. */
@@ -212,10 +212,10 @@ gdb_mpz_read_all_from_small ()
gdb_mpz expected, actual;
store_and_read_back (ul, buf_len, BFD_ENDIAN_BIG, expected, actual);
- SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
store_and_read_back (ul, buf_len, BFD_ENDIAN_LITTLE, expected, actual);
- SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
}
}
@@ -232,11 +232,11 @@ gdb_mpz_read_min_max ()
store_and_read_back (l_min, sizeof (LONGEST), BFD_ENDIAN_BIG,
expected, actual);
- SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
store_and_read_back (l_min, sizeof (LONGEST), BFD_ENDIAN_LITTLE,
expected, actual);
- SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
/* Same with LONGEST_MAX. */
@@ -244,11 +244,11 @@ gdb_mpz_read_min_max ()
store_and_read_back (l_max, sizeof (LONGEST), BFD_ENDIAN_BIG,
expected, actual);
- SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
store_and_read_back (l_max, sizeof (LONGEST), BFD_ENDIAN_LITTLE,
expected, actual);
- SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
/* Same with the smallest ULONGEST. */
@@ -256,11 +256,11 @@ gdb_mpz_read_min_max ()
store_and_read_back (ul_min, sizeof (ULONGEST), BFD_ENDIAN_BIG,
expected, actual);
- SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
store_and_read_back (ul_min, sizeof (ULONGEST), BFD_ENDIAN_LITTLE,
expected, actual);
- SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
/* Same with ULONGEST_MAX. */
@@ -268,11 +268,11 @@ gdb_mpz_read_min_max ()
store_and_read_back (ul_max, sizeof (ULONGEST), BFD_ENDIAN_BIG,
expected, actual);
- SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
store_and_read_back (ul_max, sizeof (ULONGEST), BFD_ENDIAN_LITTLE,
expected, actual);
- SELF_CHECK (mpz_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
}
/* A helper function which creates a gdb_mpz object from the given
diff --git a/gdb/valops.c b/gdb/valops.c
index 3a1b14c3d44..e0936d45cb3 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -354,7 +354,7 @@ value_to_gdb_mpq (struct value *value)
gdb_mpz vz;
vz.read (value->contents (), type_byte_order (type),
type->is_unsigned ());
- mpq_set_z (result.val, vz.val);
+ result = vz;
if (is_fixed_point_type (type))
mpq_mul (result.val, result.val,
diff --git a/gdb/value.c b/gdb/value.c
index d2c2a2d7859..672e0c7023a 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -2721,8 +2721,7 @@ unpack_long (struct type *type, const gdb_byte *valaddr)
byte_order, nosign,
type->fixed_point_scaling_factor ());
- gdb_mpz vz;
- mpz_tdiv_q (vz.val, mpq_numref (vq.val), mpq_denref (vq.val));
+ gdb_mpz vz = vq.as_integer ();
return vz.as_integer<LONGEST> ();
}
--
2.39.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 3/6] Rename gdb_mpz::val and make contents private
2023-02-23 19:47 [PATCH 0/6] Make GMP data members private Tom Tromey
2023-02-23 19:47 ` [PATCH 1/6] Clean up gmp-utils.h includes Tom Tromey
2023-02-23 19:48 ` [PATCH 2/6] Add methods and operators to gdb_mpz Tom Tromey
@ 2023-02-23 19:48 ` Tom Tromey
2023-02-23 19:48 ` [PATCH 4/6] Add operators and methods to gdb_mpq Tom Tromey
` (3 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2023-02-23 19:48 UTC (permalink / raw)
To: gdb-patches
This changes gdb_mpz to hide its data, and renames the data member
from 'val' to 'm_val', following gdb convention.
---
gdb/gmp-utils.c | 46 ++++++++++++++++++-------------------
gdb/gmp-utils.h | 70 +++++++++++++++++++++++++++++++--------------------------
2 files changed, 61 insertions(+), 55 deletions(-)
diff --git a/gdb/gmp-utils.c b/gdb/gmp-utils.c
index 8150fa5dcdf..aaa075e236e 100644
--- a/gdb/gmp-utils.c
+++ b/gdb/gmp-utils.c
@@ -46,7 +46,7 @@ void
gdb_mpz::read (gdb::array_view<const gdb_byte> buf, enum bfd_endian byte_order,
bool unsigned_p)
{
- mpz_import (val, 1 /* count */, -1 /* order */, buf.size () /* size */,
+ mpz_import (m_val, 1 /* count */, -1 /* order */, buf.size () /* size */,
byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */,
0 /* nails */, buf.data () /* op */);
@@ -57,9 +57,9 @@ gdb_mpz::read (gdb::array_view<const gdb_byte> buf, enum bfd_endian byte_order,
was in fact negative, we need to adjust VAL accordingly. */
gdb_mpz max;
- mpz_ui_pow_ui (max.val, 2, buf.size () * HOST_CHAR_BIT - 1);
- if (mpz_cmp (val, max.val) >= 0)
- mpz_submul_ui (val, max.val, 2);
+ mpz_ui_pow_ui (max.m_val, 2, buf.size () * HOST_CHAR_BIT - 1);
+ if (mpz_cmp (m_val, max.m_val) >= 0)
+ mpz_submul_ui (m_val, max.m_val, 2);
}
}
@@ -81,7 +81,7 @@ gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
{
gdb_assert (buf.size () > 0);
- if (mpz_sgn (val) == 0)
+ if (mpz_sgn (m_val) == 0)
{
/* Our value is zero, so no need to call mpz_export to do the work,
especially since mpz_export's documentation explicitly says
@@ -100,19 +100,19 @@ gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
{
lo = 0;
- mpz_ui_pow_ui (hi.val, 2, max_usable_bits);
- mpz_sub_ui (hi.val, hi.val, 1);
+ mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits);
+ mpz_sub_ui (hi.m_val, hi.m_val, 1);
}
else
{
- mpz_ui_pow_ui (lo.val, 2, max_usable_bits - 1);
- mpz_neg (lo.val, lo.val);
+ mpz_ui_pow_ui (lo.m_val, 2, max_usable_bits - 1);
+ mpz_neg (lo.m_val, lo.m_val);
- mpz_ui_pow_ui (hi.val, 2, max_usable_bits - 1);
- mpz_sub_ui (hi.val, hi.val, 1);
+ mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits - 1);
+ mpz_sub_ui (hi.m_val, hi.m_val, 1);
}
- if (mpz_cmp (val, lo.val) < 0 || mpz_cmp (val, hi.val) > 0)
+ if (mpz_cmp (m_val, lo.m_val) < 0 || mpz_cmp (m_val, hi.m_val) > 0)
error (_("Cannot export value %s as %zu-bits %s integer"
" (must be between %s and %s)"),
this->str ().c_str (),
@@ -121,17 +121,17 @@ gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
lo.str ().c_str (),
hi.str ().c_str ());
- gdb_mpz exported_val (val);
+ gdb_mpz exported_val (m_val);
- if (mpz_cmp_ui (exported_val.val, 0) < 0)
+ if (mpz_cmp_ui (exported_val.m_val, 0) < 0)
{
/* mpz_export does not handle signed values, so create a positive
value whose bit representation as an unsigned of the same length
would be the same as our negative value. */
gdb_mpz neg_offset;
- mpz_ui_pow_ui (neg_offset.val, 2, buf.size () * HOST_CHAR_BIT);
- mpz_add (exported_val.val, exported_val.val, neg_offset.val);
+ mpz_ui_pow_ui (neg_offset.m_val, 2, buf.size () * HOST_CHAR_BIT);
+ mpz_add (exported_val.m_val, exported_val.m_val, neg_offset.m_val);
}
/* Do the export into a buffer allocated by GMP itself; that way,
@@ -147,7 +147,7 @@ gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
size_t word_countp;
gdb::unique_xmalloc_ptr<void> exported
(mpz_export (NULL, &word_countp, -1 /* order */, buf.size () /* size */,
- endian, 0 /* nails */, exported_val.val));
+ endian, 0 /* nails */, exported_val.m_val));
gdb_assert (word_countp == 1);
@@ -170,19 +170,19 @@ gdb_mpq::get_rounded () const
towards zero. */
gdb_mpz quotient, remainder;
- mpz_fdiv_qr (quotient.val, remainder.val,
+ mpz_fdiv_qr (quotient.m_val, remainder.m_val,
mpq_numref (abs_val.val), mpq_denref (abs_val.val));
/* Multiply the remainder by 2, and see if it is greater or equal
to abs_val's denominator. If yes, round to the next integer. */
- mpz_mul_ui (remainder.val, remainder.val, 2);
- if (mpz_cmp (remainder.val, mpq_denref (abs_val.val)) >= 0)
- mpz_add_ui (quotient.val, quotient.val, 1);
+ mpz_mul_ui (remainder.m_val, remainder.m_val, 2);
+ if (mpz_cmp (remainder.m_val, mpq_denref (abs_val.val)) >= 0)
+ mpz_add_ui (quotient.m_val, quotient.m_val, 1);
/* Re-apply the sign if needed. */
if (mpq_sgn (val) < 0)
- mpz_neg (quotient.val, quotient.val);
+ mpz_neg (quotient.m_val, quotient.m_val);
return quotient;
}
@@ -197,7 +197,7 @@ gdb_mpq::read_fixed_point (gdb::array_view<const gdb_byte> buf,
gdb_mpz vz;
vz.read (buf, byte_order, unsigned_p);
- mpq_set_z (val, vz.val);
+ mpq_set_z (val, vz.m_val);
mpq_mul (val, val, scaling_factor.val);
}
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h
index dcb998a1656..601d9a6a1ec 100644
--- a/gdb/gmp-utils.h
+++ b/gdb/gmp-utils.h
@@ -31,25 +31,26 @@
std::string gmp_string_printf (const char *fmt, ...);
+struct gdb_mpq;
+struct gdb_mpf;
+
/* A class to make it easier to use GMP's mpz_t values within GDB. */
struct gdb_mpz
{
- mpz_t val;
-
/* Constructors. */
- gdb_mpz () { mpz_init (val); }
+ gdb_mpz () { mpz_init (m_val); }
explicit gdb_mpz (const mpz_t &from_val)
{
- mpz_init (val);
- mpz_set (val, from_val);
+ mpz_init (m_val);
+ mpz_set (m_val, from_val);
}
gdb_mpz (const gdb_mpz &from)
{
- mpz_init (val);
- mpz_set (val, from.val);
+ mpz_init (m_val);
+ mpz_set (m_val, from.m_val);
}
/* Initialize using the given integral value.
@@ -59,26 +60,26 @@ struct gdb_mpz
template<typename T, typename = gdb::Requires<std::is_integral<T>>>
explicit gdb_mpz (T src)
{
- mpz_init (val);
+ mpz_init (m_val);
set (src);
}
explicit gdb_mpz (gdb_mpz &&from)
{
- mpz_init (val);
- mpz_swap (val, from.val);
+ mpz_init (m_val);
+ mpz_swap (m_val, from.m_val);
}
gdb_mpz &operator= (const gdb_mpz &from)
{
- mpz_set (val, from.val);
+ mpz_set (m_val, from.m_val);
return *this;
}
gdb_mpz &operator= (gdb_mpz &&other)
{
- mpz_swap (val, other.val);
+ mpz_swap (m_val, other.m_val);
return *this;
}
@@ -93,14 +94,14 @@ struct gdb_mpz
the string was parsed successfully, false otherwise. */
bool set (const char *str, int base)
{
- return mpz_set_str (val, str, base) != -1;
+ return mpz_set_str (m_val, str, base) != -1;
}
/* Return a new value that is BASE**EXP. */
static gdb_mpz pow (unsigned long base, unsigned long exp)
{
gdb_mpz result;
- mpz_ui_pow_ui (result.val, base, exp);
+ mpz_ui_pow_ui (result.m_val, base, exp);
return result;
}
@@ -125,59 +126,59 @@ struct gdb_mpz
bool unsigned_p) const;
/* Return a string containing VAL. */
- std::string str () const { return gmp_string_printf ("%Zd", val); }
+ std::string str () const { return gmp_string_printf ("%Zd", m_val); }
/* The destructor. */
- ~gdb_mpz () { mpz_clear (val); }
+ ~gdb_mpz () { mpz_clear (m_val); }
/* Negate this value in place. */
void negate ()
{
- mpz_neg (val, val);
+ mpz_neg (m_val, m_val);
}
gdb_mpz &operator*= (long other)
{
- mpz_mul_si (val, val, other);
+ mpz_mul_si (m_val, m_val, other);
return *this;
}
gdb_mpz &operator+= (unsigned long other)
{
- mpz_add_ui (val, val, other);
+ mpz_add_ui (m_val, m_val, other);
return *this;
}
gdb_mpz &operator-= (unsigned long other)
{
- mpz_sub_ui (val, val, other);
+ mpz_sub_ui (m_val, m_val, other);
return *this;
}
gdb_mpz &operator<<= (unsigned long nbits)
{
- mpz_mul_2exp (val, val, nbits);
+ mpz_mul_2exp (m_val, m_val, nbits);
return *this;
}
bool operator> (const gdb_mpz &other) const
{
- return mpz_cmp (val, other.val) > 0;
+ return mpz_cmp (m_val, other.m_val) > 0;
}
bool operator< (int other) const
{
- return mpz_cmp_si (val, other) < 0;
+ return mpz_cmp_si (m_val, other) < 0;
}
bool operator== (int other) const
{
- return mpz_cmp_si (val, other) == 0;
+ return mpz_cmp_si (m_val, other) == 0;
}
bool operator== (const gdb_mpz &other) const
{
- return mpz_cmp (val, other.val) == 0;
+ return mpz_cmp (m_val, other.m_val) == 0;
}
private:
@@ -202,6 +203,11 @@ struct gdb_mpz
being exported. */
void safe_export (gdb::array_view<gdb_byte> buf,
int endian, bool unsigned_p) const;
+
+ friend struct gdb_mpq;
+ friend struct gdb_mpf;
+
+ mpz_t m_val;
};
/* A class to make it easier to use GMP's mpq_t values within GDB. */
@@ -234,8 +240,8 @@ struct gdb_mpq
gdb_mpq (const gdb_mpz &num, const gdb_mpz &denom)
{
mpq_init (val);
- mpz_set (mpq_numref (val), num.val);
- mpz_set (mpq_denref (val), denom.val);
+ mpz_set (mpq_numref (val), num.m_val);
+ mpz_set (mpq_denref (val), denom.m_val);
mpq_canonicalize (val);
}
@@ -254,7 +260,7 @@ struct gdb_mpq
gdb_mpq &operator= (const gdb_mpz &from)
{
- mpq_set_z (val, from.val);
+ mpq_set_z (val, from.m_val);
return *this;
}
@@ -268,7 +274,7 @@ struct gdb_mpq
gdb_mpz as_integer () const
{
gdb_mpz result;
- mpz_tdiv_q (result.val, mpq_numref (val), mpq_denref (val));
+ mpz_tdiv_q (result.m_val, mpq_numref (val), mpq_denref (val));
return result;
}
@@ -340,7 +346,7 @@ template<typename T>
void
gdb_mpz::set (T src)
{
- mpz_import (val, 1 /* count */, -1 /* order */,
+ mpz_import (m_val, 1 /* count */, -1 /* order */,
sizeof (T) /* size */, 0 /* endian (0 = native) */,
0 /* nails */, &src /* op */);
if (std::is_signed<T>::value && src < 0)
@@ -350,8 +356,8 @@ gdb_mpz::set (T src)
the correct negative value. */
gdb_mpz neg_offset;
- mpz_ui_pow_ui (neg_offset.val, 2, sizeof (T) * HOST_CHAR_BIT);
- mpz_sub (val, val, neg_offset.val);
+ mpz_ui_pow_ui (neg_offset.m_val, 2, sizeof (T) * HOST_CHAR_BIT);
+ mpz_sub (m_val, m_val, neg_offset.m_val);
}
}
--
2.39.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 4/6] Add operators and methods to gdb_mpq
2023-02-23 19:47 [PATCH 0/6] Make GMP data members private Tom Tromey
` (2 preceding siblings ...)
2023-02-23 19:48 ` [PATCH 3/6] Rename gdb_mpz::val and make contents private Tom Tromey
@ 2023-02-23 19:48 ` Tom Tromey
2023-02-23 19:48 ` [PATCH 5/6] Rename gdb_mpq::val and make contents private Tom Tromey
` (2 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2023-02-23 19:48 UTC (permalink / raw)
To: gdb-patches
This adds some operators and methods to gdb_mpq, in preparation for
making its implementation private.
This only adds the operators currently needed by gdb. More could be
added as necessary.
---
gdb/gmp-utils.h | 72 +++++++++++++++++++++++++++++++++++++
gdb/unittests/gmp-utils-selftests.c | 34 ++++++++----------
gdb/valarith.c | 18 +++++-----
gdb/valops.c | 13 +++----
4 files changed, 100 insertions(+), 37 deletions(-)
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h
index 601d9a6a1ec..dfb61302f6b 100644
--- a/gdb/gmp-utils.h
+++ b/gdb/gmp-utils.h
@@ -245,6 +245,13 @@ struct gdb_mpq
mpq_canonicalize (val);
}
+ gdb_mpq (long num, long denom)
+ {
+ mpq_init (val);
+ mpq_set_si (val, num, denom);
+ mpq_canonicalize (val);
+ }
+
/* Copy assignment operator. */
gdb_mpq &operator= (const gdb_mpq &from)
{
@@ -264,6 +271,67 @@ struct gdb_mpq
return *this;
}
+ gdb_mpq &operator= (double d)
+ {
+ mpq_set_d (val, d);
+ return *this;
+ }
+
+ /* Return the sign of this value. This returns -1 for a negative
+ value, 0 if the value is 0, and 1 for a positive value. */
+ int sgn () const
+ { return mpq_sgn (val); }
+
+ gdb_mpq operator+ (const gdb_mpq &other) const
+ {
+ gdb_mpq result;
+ mpq_add (result.val, val, other.val);
+ return result;
+ }
+
+ gdb_mpq operator- (const gdb_mpq &other) const
+ {
+ gdb_mpq result;
+ mpq_sub (result.val, val, other.val);
+ return result;
+ }
+
+ gdb_mpq operator* (const gdb_mpq &other) const
+ {
+ gdb_mpq result;
+ mpq_mul (result.val, val, other.val);
+ return result;
+ }
+
+ gdb_mpq operator/ (const gdb_mpq &other) const
+ {
+ gdb_mpq result;
+ mpq_div (result.val, val, other.val);
+ return result;
+ }
+
+ gdb_mpq &operator*= (const gdb_mpq &other)
+ {
+ mpq_mul (val, val, other.val);
+ return *this;
+ }
+
+ gdb_mpq &operator/= (const gdb_mpq &other)
+ {
+ mpq_div (val, val, other.val);
+ return *this;
+ }
+
+ bool operator== (const gdb_mpq &other) const
+ {
+ return mpq_cmp (val, other.val) == 0;
+ }
+
+ bool operator< (const gdb_mpq &other) const
+ {
+ return mpq_cmp (val, other.val) < 0;
+ }
+
/* Return a string representing VAL as "<numerator> / <denominator>". */
std::string str () const { return gmp_string_printf ("%Qd", val); }
@@ -278,6 +346,10 @@ struct gdb_mpq
return result;
}
+ /* Return this value converted to a host double. */
+ double as_double () const
+ { return mpq_get_d (val); }
+
/* Set VAL from the contents of the given byte array (BUF), which
contains the unscaled value of a fixed point type object.
The byte size of the data is the size of BUF.
diff --git a/gdb/unittests/gmp-utils-selftests.c b/gdb/unittests/gmp-utils-selftests.c
index 8e028fec88c..384ca2df212 100644
--- a/gdb/unittests/gmp-utils-selftests.c
+++ b/gdb/unittests/gmp-utils-selftests.c
@@ -399,8 +399,8 @@ read_fp_test (int unscaled, const gdb_mpq &scaling_factor,
actual.read_fixed_point ({buf, len}, byte_order, 0, scaling_factor);
- mpq_set_si (expected.val, unscaled, 1);
- mpq_mul (expected.val, expected.val, scaling_factor.val);
+ expected = gdb_mpq (unscaled, 1);
+ expected *= scaling_factor;
}
/* Perform various tests of the gdb_mpq::read_fixed_point method. */
@@ -409,38 +409,37 @@ static void
gdb_mpq_read_fixed_point ()
{
gdb_mpq expected, actual;
- gdb_mpq scaling_factor;
/* Pick an arbitrary scaling_factor; this operation is trivial enough
thanks to GMP that the value we use isn't really important. */
- mpq_set_ui (scaling_factor.val, 3, 5);
+ gdb_mpq scaling_factor (3, 5);
/* Try a few values, both negative and positive... */
read_fp_test (-256, scaling_factor, BFD_ENDIAN_BIG, expected, actual);
- SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
read_fp_test (-256, scaling_factor, BFD_ENDIAN_LITTLE, expected, actual);
- SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
read_fp_test (-1, scaling_factor, BFD_ENDIAN_BIG, expected, actual);
- SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
read_fp_test (-1, scaling_factor, BFD_ENDIAN_LITTLE, expected, actual);
- SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
read_fp_test (0, scaling_factor, BFD_ENDIAN_BIG, expected, actual);
- SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
read_fp_test (0, scaling_factor, BFD_ENDIAN_LITTLE, expected, actual);
- SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
read_fp_test (1, scaling_factor, BFD_ENDIAN_BIG, expected, actual);
- SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
read_fp_test (1, scaling_factor, BFD_ENDIAN_LITTLE, expected, actual);
- SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
read_fp_test (1025, scaling_factor, BFD_ENDIAN_BIG, expected, actual);
- SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
read_fp_test (1025, scaling_factor, BFD_ENDIAN_LITTLE, expected, actual);
- SELF_CHECK (mpq_cmp (actual.val, expected.val) == 0);
+ SELF_CHECK (actual == expected);
}
/* A helper function which builds a gdb_mpq object from the given
@@ -463,9 +462,7 @@ write_fp_test (int numerator, unsigned int denominator,
gdb_byte buf[len];
memset (buf, 0, len);
- gdb_mpq v;
- mpq_set_si (v.val, numerator, denominator);
- mpq_canonicalize (v.val);
+ gdb_mpq v (numerator, denominator);
v.write_fixed_point ({buf, len}, byte_order, 0, scaling_factor);
return extract_unsigned_integer (buf, len, byte_order);
@@ -479,8 +476,7 @@ gdb_mpq_write_fixed_point ()
/* Pick an arbitrary factor; this operations is sufficiently trivial
with the use of GMP that the value of this factor is not really
all that important. */
- gdb_mpq scaling_factor;
- mpq_set_ui (scaling_factor.val, 1, 3);
+ gdb_mpq scaling_factor (1, 3);
gdb_mpq vq;
diff --git a/gdb/valarith.c b/gdb/valarith.c
index a6a5f5102a2..f8ce508e825 100644
--- a/gdb/valarith.c
+++ b/gdb/valarith.c
@@ -883,43 +883,43 @@ fixed_point_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
switch (op)
{
case BINOP_ADD:
- mpq_add (res.val, v1.val, v2.val);
+ res = v1 + v2;
val = fixed_point_to_value (res);
break;
case BINOP_SUB:
- mpq_sub (res.val, v1.val, v2.val);
+ res = v1 - v2;
val = fixed_point_to_value (res);
break;
case BINOP_MIN:
- val = fixed_point_to_value (mpq_cmp (v1.val, v2.val) < 0 ? v1 : v2);
+ val = fixed_point_to_value (std::min (v1, v2));
break;
case BINOP_MAX:
- val = fixed_point_to_value (mpq_cmp (v1.val, v2.val) > 0 ? v1 : v2);
+ val = fixed_point_to_value (std::max (v1, v2));
break;
case BINOP_MUL:
- mpq_mul (res.val, v1.val, v2.val);
+ res = v1 * v2;
val = fixed_point_to_value (res);
break;
case BINOP_DIV:
- if (mpq_sgn (v2.val) == 0)
+ if (v2.sgn () == 0)
error (_("Division by zero"));
- mpq_div (res.val, v1.val, v2.val);
+ res = v1 / v2;
val = fixed_point_to_value (res);
break;
case BINOP_EQUAL:
val = value_from_ulongest (language_bool_type (language, gdbarch),
- mpq_cmp (v1.val, v2.val) == 0 ? 1 : 0);
+ v1 == v2 ? 1 : 0);
break;
case BINOP_LESS:
val = value_from_ulongest (language_bool_type (language, gdbarch),
- mpq_cmp (v1.val, v2.val) < 0 ? 1 : 0);
+ v1 < v2 ? 1 : 0);
break;
default:
diff --git a/gdb/valops.c b/gdb/valops.c
index e0936d45cb3..2cef24fc4c5 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -341,11 +341,7 @@ value_to_gdb_mpq (struct value *value)
gdb_mpq result;
if (is_floating_type (type))
- {
- double d = target_float_to_host_double (value->contents ().data (),
- type);
- mpq_set_d (result.val, d);
- }
+ result = target_float_to_host_double (value->contents ().data (), type);
else
{
gdb_assert (is_integral_type (type)
@@ -357,8 +353,7 @@ value_to_gdb_mpq (struct value *value)
result = vz;
if (is_fixed_point_type (type))
- mpq_mul (result.val, result.val,
- type->fixed_point_scaling_factor ().val);
+ result *= type->fixed_point_scaling_factor ();
}
return result;
@@ -386,7 +381,7 @@ value_cast_to_fixed_point (struct type *to_type, struct value *from_val)
/* Divide that value by the scaling factor to obtain the unscaled
value, first in rational form, and then in integer form. */
- mpq_div (vq.val, vq.val, to_type->fixed_point_scaling_factor ().val);
+ vq /= to_type->fixed_point_scaling_factor ();
gdb_mpz unscaled = vq.get_rounded ();
/* Finally, create the result value, and pack the unscaled value
@@ -559,7 +554,7 @@ value_cast (struct type *type, struct value *arg2)
struct value *v = value::allocate (to_type);
target_float_from_host_double (v->contents_raw ().data (),
- to_type, mpq_get_d (fp_val.val));
+ to_type, fp_val.as_double ());
return v;
}
--
2.39.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 5/6] Rename gdb_mpq::val and make contents private
2023-02-23 19:47 [PATCH 0/6] Make GMP data members private Tom Tromey
` (3 preceding siblings ...)
2023-02-23 19:48 ` [PATCH 4/6] Add operators and methods to gdb_mpq Tom Tromey
@ 2023-02-23 19:48 ` Tom Tromey
2023-02-23 19:48 ` [PATCH 6/6] Hide the implementation of gdb_mpf Tom Tromey
2023-03-14 14:15 ` [PATCH 0/6] Make GMP data members private Tom Tromey
6 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2023-02-23 19:48 UTC (permalink / raw)
To: gdb-patches
This changes gdb_mpq to hide its data, and renames the data member
from 'val' to 'm_val', following gdb convention.
---
gdb/gmp-utils.c | 18 +++++++--------
gdb/gmp-utils.h | 72 ++++++++++++++++++++++++++++++---------------------------
2 files changed, 47 insertions(+), 43 deletions(-)
diff --git a/gdb/gmp-utils.c b/gdb/gmp-utils.c
index aaa075e236e..d134bc32a1d 100644
--- a/gdb/gmp-utils.c
+++ b/gdb/gmp-utils.c
@@ -162,8 +162,8 @@ gdb_mpq::get_rounded () const
/* Work with a positive number so as to make the "floor" rounding
always round towards zero. */
- gdb_mpq abs_val (val);
- mpq_abs (abs_val.val, abs_val.val);
+ gdb_mpq abs_val (m_val);
+ mpq_abs (abs_val.m_val, abs_val.m_val);
/* Convert our rational number into a quotient and remainder,
with "floor" rounding, which in our case means rounding
@@ -171,17 +171,17 @@ gdb_mpq::get_rounded () const
gdb_mpz quotient, remainder;
mpz_fdiv_qr (quotient.m_val, remainder.m_val,
- mpq_numref (abs_val.val), mpq_denref (abs_val.val));
+ mpq_numref (abs_val.m_val), mpq_denref (abs_val.m_val));
/* Multiply the remainder by 2, and see if it is greater or equal
to abs_val's denominator. If yes, round to the next integer. */
mpz_mul_ui (remainder.m_val, remainder.m_val, 2);
- if (mpz_cmp (remainder.m_val, mpq_denref (abs_val.val)) >= 0)
+ if (mpz_cmp (remainder.m_val, mpq_denref (abs_val.m_val)) >= 0)
mpz_add_ui (quotient.m_val, quotient.m_val, 1);
/* Re-apply the sign if needed. */
- if (mpq_sgn (val) < 0)
+ if (mpq_sgn (m_val) < 0)
mpz_neg (quotient.m_val, quotient.m_val);
return quotient;
@@ -197,8 +197,8 @@ gdb_mpq::read_fixed_point (gdb::array_view<const gdb_byte> buf,
gdb_mpz vz;
vz.read (buf, byte_order, unsigned_p);
- mpq_set_z (val, vz.m_val);
- mpq_mul (val, val, scaling_factor.val);
+ mpq_set_z (m_val, vz.m_val);
+ mpq_mul (m_val, m_val, scaling_factor.m_val);
}
/* See gmp-utils.h. */
@@ -208,9 +208,9 @@ gdb_mpq::write_fixed_point (gdb::array_view<gdb_byte> buf,
enum bfd_endian byte_order, bool unsigned_p,
const gdb_mpq &scaling_factor) const
{
- gdb_mpq unscaled (val);
+ gdb_mpq unscaled (m_val);
- mpq_div (unscaled.val, unscaled.val, scaling_factor.val);
+ mpq_div (unscaled.m_val, unscaled.m_val, scaling_factor.m_val);
gdb_mpz unscaled_z = unscaled.get_rounded ();
unscaled_z.write (buf, byte_order, unsigned_p);
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h
index dfb61302f6b..7889756a0ea 100644
--- a/gdb/gmp-utils.h
+++ b/gdb/gmp-utils.h
@@ -214,126 +214,124 @@ struct gdb_mpz
struct gdb_mpq
{
- mpq_t val;
-
/* Constructors. */
- gdb_mpq () { mpq_init (val); }
+ gdb_mpq () { mpq_init (m_val); }
explicit gdb_mpq (const mpq_t &from_val)
{
- mpq_init (val);
- mpq_set (val, from_val);
+ mpq_init (m_val);
+ mpq_set (m_val, from_val);
}
gdb_mpq (const gdb_mpq &from)
{
- mpq_init (val);
- mpq_set (val, from.val);
+ mpq_init (m_val);
+ mpq_set (m_val, from.m_val);
}
explicit gdb_mpq (gdb_mpq &&from)
{
- mpq_init (val);
- mpq_swap (val, from.val);
+ mpq_init (m_val);
+ mpq_swap (m_val, from.m_val);
}
gdb_mpq (const gdb_mpz &num, const gdb_mpz &denom)
{
- mpq_init (val);
- mpz_set (mpq_numref (val), num.m_val);
- mpz_set (mpq_denref (val), denom.m_val);
- mpq_canonicalize (val);
+ mpq_init (m_val);
+ mpz_set (mpq_numref (m_val), num.m_val);
+ mpz_set (mpq_denref (m_val), denom.m_val);
+ mpq_canonicalize (m_val);
}
gdb_mpq (long num, long denom)
{
- mpq_init (val);
- mpq_set_si (val, num, denom);
- mpq_canonicalize (val);
+ mpq_init (m_val);
+ mpq_set_si (m_val, num, denom);
+ mpq_canonicalize (m_val);
}
/* Copy assignment operator. */
gdb_mpq &operator= (const gdb_mpq &from)
{
- mpq_set (val, from.val);
+ mpq_set (m_val, from.m_val);
return *this;
}
gdb_mpq &operator= (gdb_mpq &&from)
{
- mpq_swap (val, from.val);
+ mpq_swap (m_val, from.m_val);
return *this;
}
gdb_mpq &operator= (const gdb_mpz &from)
{
- mpq_set_z (val, from.m_val);
+ mpq_set_z (m_val, from.m_val);
return *this;
}
gdb_mpq &operator= (double d)
{
- mpq_set_d (val, d);
+ mpq_set_d (m_val, d);
return *this;
}
/* Return the sign of this value. This returns -1 for a negative
value, 0 if the value is 0, and 1 for a positive value. */
int sgn () const
- { return mpq_sgn (val); }
+ { return mpq_sgn (m_val); }
gdb_mpq operator+ (const gdb_mpq &other) const
{
gdb_mpq result;
- mpq_add (result.val, val, other.val);
+ mpq_add (result.m_val, m_val, other.m_val);
return result;
}
gdb_mpq operator- (const gdb_mpq &other) const
{
gdb_mpq result;
- mpq_sub (result.val, val, other.val);
+ mpq_sub (result.m_val, m_val, other.m_val);
return result;
}
gdb_mpq operator* (const gdb_mpq &other) const
{
gdb_mpq result;
- mpq_mul (result.val, val, other.val);
+ mpq_mul (result.m_val, m_val, other.m_val);
return result;
}
gdb_mpq operator/ (const gdb_mpq &other) const
{
gdb_mpq result;
- mpq_div (result.val, val, other.val);
+ mpq_div (result.m_val, m_val, other.m_val);
return result;
}
gdb_mpq &operator*= (const gdb_mpq &other)
{
- mpq_mul (val, val, other.val);
+ mpq_mul (m_val, m_val, other.m_val);
return *this;
}
gdb_mpq &operator/= (const gdb_mpq &other)
{
- mpq_div (val, val, other.val);
+ mpq_div (m_val, m_val, other.m_val);
return *this;
}
bool operator== (const gdb_mpq &other) const
{
- return mpq_cmp (val, other.val) == 0;
+ return mpq_cmp (m_val, other.m_val) == 0;
}
bool operator< (const gdb_mpq &other) const
{
- return mpq_cmp (val, other.val) < 0;
+ return mpq_cmp (m_val, other.m_val) < 0;
}
/* Return a string representing VAL as "<numerator> / <denominator>". */
- std::string str () const { return gmp_string_printf ("%Qd", val); }
+ std::string str () const { return gmp_string_printf ("%Qd", m_val); }
/* Return VAL rounded to the nearest integer. */
gdb_mpz get_rounded () const;
@@ -342,13 +340,13 @@ struct gdb_mpq
gdb_mpz as_integer () const
{
gdb_mpz result;
- mpz_tdiv_q (result.m_val, mpq_numref (val), mpq_denref (val));
+ mpz_tdiv_q (result.m_val, mpq_numref (m_val), mpq_denref (m_val));
return result;
}
/* Return this value converted to a host double. */
double as_double () const
- { return mpq_get_d (val); }
+ { return mpq_get_d (m_val); }
/* Set VAL from the contents of the given byte array (BUF), which
contains the unscaled value of a fixed point type object.
@@ -374,7 +372,13 @@ struct gdb_mpq
const gdb_mpq &scaling_factor) const;
/* The destructor. */
- ~gdb_mpq () { mpq_clear (val); }
+ ~gdb_mpq () { mpq_clear (m_val); }
+
+private:
+
+ friend struct gdb_mpf;
+
+ mpq_t m_val;
};
/* A class to make it easier to use GMP's mpf_t values within GDB.
@@ -405,7 +409,7 @@ struct gdb_mpf
gdb_mpq tmp_q;
tmp_q.read_fixed_point (buf, byte_order, unsigned_p, scaling_factor);
- mpf_set_q (val, tmp_q.val);
+ mpf_set_q (val, tmp_q.m_val);
}
/* The destructor. */
--
2.39.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 6/6] Hide the implementation of gdb_mpf
2023-02-23 19:47 [PATCH 0/6] Make GMP data members private Tom Tromey
` (4 preceding siblings ...)
2023-02-23 19:48 ` [PATCH 5/6] Rename gdb_mpq::val and make contents private Tom Tromey
@ 2023-02-23 19:48 ` Tom Tromey
2023-03-14 14:15 ` [PATCH 0/6] Make GMP data members private Tom Tromey
6 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2023-02-23 19:48 UTC (permalink / raw)
To: gdb-patches
This renames the data member of gdb_mpf and makes it private. It also
adds a single new method to aid in this change. Unlike the earlier
changes here, I did this one all together because gdb_mpf has very few
uses.
---
gdb/gmp-utils.h | 17 ++++++++++++-----
gdb/valprint.c | 2 +-
2 files changed, 13 insertions(+), 6 deletions(-)
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h
index 7889756a0ea..274c28c0ce8 100644
--- a/gdb/gmp-utils.h
+++ b/gdb/gmp-utils.h
@@ -388,10 +388,8 @@ struct gdb_mpq
struct gdb_mpf
{
- mpf_t val;
-
/* Constructors. */
- gdb_mpf () { mpf_init (val); }
+ gdb_mpf () { mpf_init (m_val); }
DISABLE_COPY_AND_ASSIGN (gdb_mpf);
@@ -409,11 +407,20 @@ struct gdb_mpf
gdb_mpq tmp_q;
tmp_q.read_fixed_point (buf, byte_order, unsigned_p, scaling_factor);
- mpf_set_q (val, tmp_q.m_val);
+ mpf_set_q (m_val, tmp_q.m_val);
}
+ /* Convert this value to a string. FMT is the format to use, and
+ should have a single '%' substitution. */
+ std::string str (const char *fmt) const
+ { return gmp_string_printf (fmt, m_val); }
+
/* The destructor. */
- ~gdb_mpf () { mpf_clear (val); }
+ ~gdb_mpf () { mpf_clear (m_val); }
+
+private:
+
+ mpf_t m_val;
};
/* See declaration above. */
diff --git a/gdb/valprint.c b/gdb/valprint.c
index 357db3815b0..b05d90dd26b 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -856,7 +856,7 @@ generic_val_print_fixed_point (struct value *val, struct ui_file *stream,
type->fixed_point_scaling_factor ());
const char *fmt = type->length () < 4 ? "%.11Fg" : "%.17Fg";
- std::string str = gmp_string_printf (fmt, f.val);
+ std::string str = f.str (fmt);
gdb_printf (stream, "%s", str.c_str ());
}
}
--
2.39.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 0/6] Make GMP data members private
2023-02-23 19:47 [PATCH 0/6] Make GMP data members private Tom Tromey
` (5 preceding siblings ...)
2023-02-23 19:48 ` [PATCH 6/6] Hide the implementation of gdb_mpf Tom Tromey
@ 2023-03-14 14:15 ` Tom Tromey
6 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2023-03-14 14:15 UTC (permalink / raw)
To: Tom Tromey via Gdb-patches; +Cc: Tom Tromey
>>>>> "Tom" == Tom Tromey via Gdb-patches <gdb-patches@sourceware.org> writes:
Tom> This series changes the GMP code in gmp-utils.h to add methods and
Tom> operators, and then to make the data members private. Ages ago I told
Tom> Simon I'd do this, then I dropped it for years.
Tom> I've only added exactly the operators that are needed right now. It's
Tom> simple to add more operators or more overloads as required.
Tom> Regression tested on x86-64 Fedora 36.
I rebased this and I am going to check it in now.
Tom
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2023-03-14 14:15 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-23 19:47 [PATCH 0/6] Make GMP data members private Tom Tromey
2023-02-23 19:47 ` [PATCH 1/6] Clean up gmp-utils.h includes Tom Tromey
2023-02-23 19:48 ` [PATCH 2/6] Add methods and operators to gdb_mpz Tom Tromey
2023-02-23 19:48 ` [PATCH 3/6] Rename gdb_mpz::val and make contents private Tom Tromey
2023-02-23 19:48 ` [PATCH 4/6] Add operators and methods to gdb_mpq Tom Tromey
2023-02-23 19:48 ` [PATCH 5/6] Rename gdb_mpq::val and make contents private Tom Tromey
2023-02-23 19:48 ` [PATCH 6/6] Hide the implementation of gdb_mpf Tom Tromey
2023-03-14 14:15 ` [PATCH 0/6] Make GMP data members private Tom Tromey
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).