diff options
author | Tom Tromey <tromey@adacore.com> | 2023-02-23 07:30:26 -0700 |
---|---|---|
committer | Tom Tromey <tromey@adacore.com> | 2023-03-14 08:16:39 -0600 |
commit | 302273ca843fc3ddbe8210e6d30459316bf04f0e (patch) | |
tree | 93807817bec50bb5d5700fdfb3f6603b0907ce0e /gdb/gmp-utils.h | |
parent | c8a67010d133374dfb3b4ed88da7953702a2091d (diff) | |
download | fsf-binutils-gdb-302273ca843fc3ddbe8210e6d30459316bf04f0e.zip fsf-binutils-gdb-302273ca843fc3ddbe8210e6d30459316bf04f0e.tar.gz fsf-binutils-gdb-302273ca843fc3ddbe8210e6d30459316bf04f0e.tar.bz2 |
Add methods and operators to gdb_mpz
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.
Diffstat (limited to 'gdb/gmp-utils.h')
-rw-r--r-- | gdb/gmp-utils.h | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h index 3f30b57..75e2273 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. |