1 /* Copyright (C) 2019-2020 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include "gmp-utils.h"
20 /* See gmp-utils.h. */
23 gmp_string_printf (const char *fmt
, ...)
28 int size
= gmp_vsnprintf (NULL
, 0, fmt
, vp
);
31 std::string
str (size
, '\0');
33 /* C++11 and later guarantee std::string uses contiguous memory and
34 always includes the terminating '\0'. */
36 gmp_vsprintf (&str
[0], fmt
, vp
);
42 /* See gmp-utils.h. */
45 gdb_mpz::read (const gdb_byte
*buf
, int len
, enum bfd_endian byte_order
,
48 mpz_import (val
, 1 /* count */, -1 /* order */, len
/* size */,
49 byte_order
== BFD_ENDIAN_BIG
? 1 : -1 /* endian */,
50 0 /* nails */, buf
/* op */);
54 /* The value was imported as if it was a positive value,
55 as mpz_import does not handle signs. If the original value
56 was in fact negative, we need to adjust VAL accordingly. */
59 mpz_ui_pow_ui (max
.val
, 2, len
* TARGET_CHAR_BIT
- 1);
60 if (mpz_cmp (val
, max
.val
) >= 0)
61 mpz_submul_ui (val
, max
.val
, 2);
65 /* See gmp-utils.h. */
68 gdb_mpz::write (gdb_byte
*buf
, int len
, enum bfd_endian byte_order
,
69 bool unsigned_p
) const
71 gdb_mpz
exported_val (val
);
73 if (mpz_cmp_ui (val
, 0) < 0)
75 /* mpz_export does not handle signed values, so create a positive
76 value whose bit representation as an unsigned of the same length
77 would be the same as our negative value. */
80 mpz_ui_pow_ui (neg_offset
.val
, 2, len
* TARGET_CHAR_BIT
);
81 mpz_add (exported_val
.val
, exported_val
.val
, neg_offset
.val
);
84 /* Start by clearing the buffer, as mpz_export only writes as many
85 bytes as it needs (including none, if the value to export is zero. */
87 mpz_export (buf
, NULL
/* count */, -1 /* order */, len
/* size */,
88 byte_order
== BFD_ENDIAN_BIG
? 1 : -1 /* endian */,
89 0 /* nails */, exported_val
.val
);
92 /* See gmp-utils.h. */
95 gdb_mpq::get_rounded () const
97 /* Work with a positive number so as to make the "floor" rounding
98 always round towards zero. */
100 gdb_mpq
abs_val (val
);
101 mpq_abs (abs_val
.val
, abs_val
.val
);
103 /* Convert our rational number into a quotient and remainder,
104 with "floor" rounding, which in our case means rounding
107 gdb_mpz quotient
, remainder
;
108 mpz_fdiv_qr (quotient
.val
, remainder
.val
,
109 mpq_numref (abs_val
.val
), mpq_denref (abs_val
.val
));
111 /* Multiply the remainder by 2, and see if it is greater or equal
112 to abs_val's denominator. If yes, round to the next integer. */
114 mpz_mul_ui (remainder
.val
, remainder
.val
, 2);
115 if (mpz_cmp (remainder
.val
, mpq_denref (abs_val
.val
)) >= 0)
116 mpz_add_ui (quotient
.val
, quotient
.val
, 1);
118 /* Re-apply the sign if needed. */
119 if (mpq_sgn (val
) < 0)
120 mpz_neg (quotient
.val
, quotient
.val
);
125 /* See gmp-utils.h. */
128 gdb_mpq::read_fixed_point (const gdb_byte
*buf
, int len
,
129 enum bfd_endian byte_order
, bool unsigned_p
,
130 const gdb_mpq
&scaling_factor
)
133 vz
.read (buf
, len
, byte_order
, unsigned_p
);
135 mpq_set_z (val
, vz
.val
);
136 mpq_mul (val
, val
, scaling_factor
.val
);
139 /* See gmp-utils.h. */
142 gdb_mpq::write_fixed_point (gdb_byte
*buf
, int len
,
143 enum bfd_endian byte_order
, bool unsigned_p
,
144 const gdb_mpq
&scaling_factor
) const
146 gdb_mpq
unscaled (val
);
148 mpq_div (unscaled
.val
, unscaled
.val
, scaling_factor
.val
);
150 gdb_mpz unscaled_z
= unscaled
.get_rounded ();
151 unscaled_z
.write (buf
, len
, byte_order
, unsigned_p
);
154 /* A wrapper around xrealloc that we can then register with GMP
155 as the "realloc" function. */
158 xrealloc_for_gmp (void *ptr
, size_t old_size
, size_t new_size
)
160 return xrealloc (ptr
, new_size
);
163 /* A wrapper around xfree that we can then register with GMP
164 as the "free" function. */
167 xfree_for_gmp (void *ptr
, size_t size
)
172 void _initialize_gmp_utils ();
175 _initialize_gmp_utils ()
177 /* Tell GMP to use GDB's memory management routines. */
178 mp_set_memory_functions (xmalloc
, xrealloc_for_gmp
, xfree_for_gmp
);
This page took 0.033738 seconds and 5 git commands to generate.