diff options
author | Tom Tromey <tromey@adacore.com> | 2023-02-23 10:34:22 -0700 |
---|---|---|
committer | Tom Tromey <tromey@adacore.com> | 2023-03-14 08:16:39 -0600 |
commit | 7607de943130608a0798a550581b15331d140825 (patch) | |
tree | cc6210f9401bd09698df64c6e4eadd6d731bb7e0 /gdb/gmp-utils.h | |
parent | 7aeae94f88791399ca4b50f850c36180de420e92 (diff) | |
download | gdb-7607de943130608a0798a550581b15331d140825.zip gdb-7607de943130608a0798a550581b15331d140825.tar.gz gdb-7607de943130608a0798a550581b15331d140825.tar.bz2 |
Add operators and methods to gdb_mpq
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.
Diffstat (limited to 'gdb/gmp-utils.h')
-rw-r--r-- | gdb/gmp-utils.h | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h index 3f43f5f..381ea9d 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. |