From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 814A9385840A; Tue, 14 Mar 2023 14:19:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 814A9385840A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1678803586; bh=Ddf5m27mG/lF5oUeGlv9ubtgTEX1t+Xvzfio3nSOcQo=; h=From:To:Subject:Date:From; b=cTyyPTOREL1RfYuIkBF16LBnJPTCe2a02+NCbqc1mOEV11Rqj7BU6K/dypN6jSBpn hsOoRqY2bJZuBcHhXCqzQR5h779Aephop5ZNqK07bu3ir93v1/bD92VKzYuOY0iBev 5BMnBFGdVeB5fwabh+F7cB0yQaV/tnOapo6Tpec4= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Add operators and methods to gdb_mpq X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 7aeae94f88791399ca4b50f850c36180de420e92 X-Git-Newrev: 7607de943130608a0798a550581b15331d140825 Message-Id: <20230314141946.814A9385840A@sourceware.org> Date: Tue, 14 Mar 2023 14:19:46 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D7607de943130= 608a0798a550581b15331d140825 commit 7607de943130608a0798a550581b15331d140825 Author: Tom Tromey Date: Thu Feb 23 10:34:22 2023 -0700 Add operators and methods to gdb_mpq =20 This adds some operators and methods to gdb_mpq, in preparation for making its implementation private. =20 This only adds the operators currently needed by gdb. More could be added as necessary. Diff: --- 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 3f43f5f835b..381ea9dbc72 100644 --- a/gdb/gmp-utils.h +++ b/gdb/gmp-utils.h @@ -245,6 +245,13 @@ struct gdb_mpq mpq_canonicalize (val); } =20 + gdb_mpq (long num, long denom) + { + mpq_init (val); + mpq_set_si (val, num, denom); + mpq_canonicalize (val); + } + /* Copy assignment operator. */ gdb_mpq &operator=3D (const gdb_mpq &from) { @@ -264,6 +271,67 @@ struct gdb_mpq return *this; } =20 + gdb_mpq &operator=3D (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*=3D (const gdb_mpq &other) + { + mpq_mul (val, val, other.val); + return *this; + } + + gdb_mpq &operator/=3D (const gdb_mpq &other) + { + mpq_div (val, val, other.val); + return *this; + } + + bool operator=3D=3D (const gdb_mpq &other) const + { + return mpq_cmp (val, other.val) =3D=3D 0; + } + + bool operator< (const gdb_mpq &other) const + { + return mpq_cmp (val, other.val) < 0; + } + /* Return a string representing VAL as " / ". */ std::string str () const { return gmp_string_printf ("%Qd", val); } =20 @@ -278,6 +346,10 @@ struct gdb_mpq return result; } =20 + /* 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_fact= or, =20 actual.read_fixed_point ({buf, len}, byte_order, 0, scaling_factor); =20 - mpq_set_si (expected.val, unscaled, 1); - mpq_mul (expected.val, expected.val, scaling_factor.val); + expected =3D gdb_mpq (unscaled, 1); + expected *=3D scaling_factor; } =20 /* 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; =20 /* 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); =20 /* Try a few values, both negative and positive... */ =20 read_fp_test (-256, scaling_factor, BFD_ENDIAN_BIG, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) =3D=3D 0); + SELF_CHECK (actual =3D=3D expected); read_fp_test (-256, scaling_factor, BFD_ENDIAN_LITTLE, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) =3D=3D 0); + SELF_CHECK (actual =3D=3D expected); =20 read_fp_test (-1, scaling_factor, BFD_ENDIAN_BIG, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) =3D=3D 0); + SELF_CHECK (actual =3D=3D expected); read_fp_test (-1, scaling_factor, BFD_ENDIAN_LITTLE, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) =3D=3D 0); + SELF_CHECK (actual =3D=3D expected); =20 read_fp_test (0, scaling_factor, BFD_ENDIAN_BIG, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) =3D=3D 0); + SELF_CHECK (actual =3D=3D expected); read_fp_test (0, scaling_factor, BFD_ENDIAN_LITTLE, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) =3D=3D 0); + SELF_CHECK (actual =3D=3D expected); =20 read_fp_test (1, scaling_factor, BFD_ENDIAN_BIG, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) =3D=3D 0); + SELF_CHECK (actual =3D=3D expected); read_fp_test (1, scaling_factor, BFD_ENDIAN_LITTLE, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) =3D=3D 0); + SELF_CHECK (actual =3D=3D expected); =20 read_fp_test (1025, scaling_factor, BFD_ENDIAN_BIG, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) =3D=3D 0); + SELF_CHECK (actual =3D=3D expected); read_fp_test (1025, scaling_factor, BFD_ENDIAN_LITTLE, expected, actual); - SELF_CHECK (mpq_cmp (actual.val, expected.val) =3D=3D 0); + SELF_CHECK (actual =3D=3D expected); } =20 /* 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); =20 - 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); =20 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); =20 gdb_mpq vq; =20 diff --git a/gdb/valarith.c b/gdb/valarith.c index e0a3461aaa2..b3321e4ff75 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 =3D v1 + v2; val =3D fixed_point_to_value (res); break; =20 case BINOP_SUB: - mpq_sub (res.val, v1.val, v2.val); + res =3D v1 - v2; val =3D fixed_point_to_value (res); break; =20 case BINOP_MIN: - val =3D fixed_point_to_value (mpq_cmp (v1.val, v2.val) < 0 ? v1 : v2= ); + val =3D fixed_point_to_value (std::min (v1, v2)); break; =20 case BINOP_MAX: - val =3D fixed_point_to_value (mpq_cmp (v1.val, v2.val) > 0 ? v1 : v2= ); + val =3D fixed_point_to_value (std::max (v1, v2)); break; =20 case BINOP_MUL: - mpq_mul (res.val, v1.val, v2.val); + res =3D v1 * v2; val =3D fixed_point_to_value (res); break; =20 case BINOP_DIV: - if (mpq_sgn (v2.val) =3D=3D 0) + if (v2.sgn () =3D=3D 0) error (_("Division by zero")); - mpq_div (res.val, v1.val, v2.val); + res =3D v1 / v2; val =3D fixed_point_to_value (res); break; =20 case BINOP_EQUAL: val =3D value_from_ulongest (language_bool_type (language, gdbarch), - mpq_cmp (v1.val, v2.val) =3D=3D 0 ? 1 : 0); + v1 =3D=3D v2 ? 1 : 0); break; =20 case BINOP_LESS: val =3D value_from_ulongest (language_bool_type (language, gdbarch), - mpq_cmp (v1.val, v2.val) < 0 ? 1 : 0); + v1 < v2 ? 1 : 0); break; =20 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) =20 gdb_mpq result; if (is_floating_type (type)) - { - double d =3D target_float_to_host_double (value->contents ().data (), - type); - mpq_set_d (result.val, d); - } + result =3D target_float_to_host_double (value->contents ().data (), ty= pe); else { gdb_assert (is_integral_type (type) @@ -357,8 +353,7 @@ value_to_gdb_mpq (struct value *value) result =3D vz; =20 if (is_fixed_point_type (type)) - mpq_mul (result.val, result.val, - type->fixed_point_scaling_factor ().val); + result *=3D type->fixed_point_scaling_factor (); } =20 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. */ =20 - mpq_div (vq.val, vq.val, to_type->fixed_point_scaling_factor ().val); + vq /=3D to_type->fixed_point_scaling_factor (); gdb_mpz unscaled =3D vq.get_rounded (); =20 /* Finally, create the result value, and pack the unscaled value @@ -559,7 +554,7 @@ value_cast (struct type *type, struct value *arg2) =20 struct value *v =3D 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; }