change and rename gmp_string_asprintf to return an std::string
[deliverable/binutils-gdb.git] / gdb / gmp-utils.h
1 /* Miscellaneous routines making it easier to use GMP within GDB's framework.
2
3 Copyright (C) 2019-2020 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #ifndef GMP_UTILS_H
21 #define GMP_UTILS_H
22
23 #include "defs.h"
24
25 /* Include <stdio.h> and <stdarg.h> ahead of <gmp.h>, so as to get
26 access to GMP's various formatting functions. */
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <gmp.h>
30 #include "gdbsupport/traits.h"
31
32 /* Same as gmp_asprintf, but returning an std::string. */
33
34 std::string gmp_string_printf (const char *fmt, ...);
35
36 /* A class to make it easier to use GMP's mpz_t values within GDB. */
37
38 struct gdb_mpz
39 {
40 mpz_t val;
41
42 /* Constructors. */
43 gdb_mpz () { mpz_init (val); }
44
45 explicit gdb_mpz (const mpz_t &from_val)
46 {
47 mpz_init (val);
48 mpz_set (val, from_val);
49 }
50
51 gdb_mpz (const gdb_mpz &from)
52 {
53 mpz_init (val);
54 mpz_set (val, from.val);
55 }
56
57 /* Initialize using the given integral value.
58
59 The main advantage of this method is that it handles both signed
60 and unsigned types, with no size restriction. */
61 template<typename T, typename = gdb::Requires<std::is_integral<T>>>
62 explicit gdb_mpz (T src)
63 {
64 mpz_init (val);
65 set (src);
66 }
67
68 explicit gdb_mpz (gdb_mpz &&from)
69 {
70 mpz_init (val);
71 mpz_swap (val, from.val);
72 }
73
74
75 gdb_mpz &operator= (const gdb_mpz &from)
76 {
77 mpz_set (val, from.val);
78 return *this;
79 }
80
81 gdb_mpz &operator= (gdb_mpz &&other)
82 {
83 mpz_swap (val, other.val);
84 return *this;
85 }
86
87 template<typename T, typename = gdb::Requires<std::is_integral<T>>>
88 gdb_mpz &operator= (T src)
89 {
90 set (src);
91 return *this;
92 }
93
94 /* Convert VAL to an integer of the given type.
95
96 The return type can signed or unsigned, with no size restriction. */
97 template<typename T> T as_integer () const;
98
99 /* Set VAL by importing the number stored in the byte buffer (BUF),
100 given its size (LEN) and BYTE_ORDER.
101
102 UNSIGNED_P indicates whether the number has an unsigned type. */
103 void read (const gdb_byte *buf, int len, enum bfd_endian byte_order,
104 bool unsigned_p);
105
106 /* Write VAL into BUF as a LEN-bytes number with the given BYTE_ORDER.
107
108 UNSIGNED_P indicates whether the number has an unsigned type. */
109 void write (gdb_byte *buf, int len, enum bfd_endian byte_order,
110 bool unsigned_p) const;
111
112 /* Return a string containing VAL. */
113 std::string str () const { return gmp_string_printf ("%Zd", val); }
114
115 /* The destructor. */
116 ~gdb_mpz () { mpz_clear (val); }
117
118 private:
119
120 /* Helper template for constructor and operator=. */
121 template<typename T> void set (T src);
122 };
123
124 /* A class to make it easier to use GMP's mpq_t values within GDB. */
125
126 struct gdb_mpq
127 {
128 mpq_t val;
129
130 /* Constructors. */
131 gdb_mpq () { mpq_init (val); }
132
133 explicit gdb_mpq (const mpq_t &from_val)
134 {
135 mpq_init (val);
136 mpq_set (val, from_val);
137 }
138
139 gdb_mpq (const gdb_mpq &from)
140 {
141 mpq_init (val);
142 mpq_set (val, from.val);
143 }
144
145 explicit gdb_mpq (gdb_mpq &&from)
146 {
147 mpq_init (val);
148 mpq_swap (val, from.val);
149 }
150
151 /* Copy assignment operator. */
152 gdb_mpq &operator= (const gdb_mpq &from)
153 {
154 mpq_set (val, from.val);
155 return *this;
156 }
157
158 gdb_mpq &operator= (gdb_mpq &&from)
159 {
160 mpq_swap (val, from.val);
161 return *this;
162 }
163
164 /* Return a string representing VAL as "<numerator> / <denominator>". */
165 std::string str () const { return gmp_string_printf ("%Qd", val); }
166
167 /* Return VAL rounded to the nearest integer. */
168 gdb_mpz get_rounded () const;
169
170 /* Set VAL from the contents of the given buffer (BUF), which
171 contains the unscaled value of a fixed point type object
172 with the given size (LEN) and byte order (BYTE_ORDER).
173
174 UNSIGNED_P indicates whether the number has an unsigned type.
175 SCALING_FACTOR is the scaling factor to apply after having
176 read the unscaled value from our buffer. */
177 void read_fixed_point (const gdb_byte *buf, int len,
178 enum bfd_endian byte_order, bool unsigned_p,
179 const gdb_mpq &scaling_factor);
180
181 /* Write VAL into BUF as a LEN-bytes fixed point value following
182 the given BYTE_ORDER.
183
184 UNSIGNED_P indicates whether the number has an unsigned type.
185 SCALING_FACTOR is the scaling factor to apply before writing
186 the unscaled value to our buffer. */
187 void write_fixed_point (gdb_byte *buf, int len,
188 enum bfd_endian byte_order, bool unsigned_p,
189 const gdb_mpq &scaling_factor) const;
190
191 /* The destructor. */
192 ~gdb_mpq () { mpq_clear (val); }
193 };
194
195 /* A class to make it easier to use GMP's mpf_t values within GDB.
196
197 Should MPFR become a required dependency, we should probably
198 drop this class in favor of using MPFR. */
199
200 struct gdb_mpf
201 {
202 mpf_t val;
203
204 /* Constructors. */
205 gdb_mpf () { mpf_init (val); }
206
207 DISABLE_COPY_AND_ASSIGN (gdb_mpf);
208
209 /* Set VAL from the contents of the given buffer (BUF), which
210 contains the unscaled value of a fixed point type object
211 with the given size (LEN) and byte order (BYTE_ORDER).
212
213 UNSIGNED_P indicates whether the number has an unsigned type.
214 SCALING_FACTOR is the scaling factor to apply after having
215 read the unscaled value from our buffer. */
216 void read_fixed_point (const gdb_byte *buf, int len,
217 enum bfd_endian byte_order, bool unsigned_p,
218 const gdb_mpq &scaling_factor)
219 {
220 gdb_mpq tmp_q;
221
222 tmp_q.read_fixed_point (buf, len, byte_order, unsigned_p, scaling_factor);
223 mpf_set_q (val, tmp_q.val);
224 }
225
226 /* The destructor. */
227 ~gdb_mpf () { mpf_clear (val); }
228 };
229
230 /* See declaration above. */
231
232 template<typename T>
233 void
234 gdb_mpz::set (T src)
235 {
236 mpz_import (val, 1 /* count */, -1 /* order */,
237 sizeof (T) /* size */, 0 /* endian (0 = native) */,
238 0 /* nails */, &src /* op */);
239 if (std::is_signed<T>::value && src < 0)
240 {
241 /* mpz_import does not handle the sign, so our value was imported
242 as an unsigned. Adjust that imported value so as to make it
243 the correct negative value. */
244 gdb_mpz neg_offset;
245
246 mpz_ui_pow_ui (neg_offset.val, 2, sizeof (T) * HOST_CHAR_BIT);
247 mpz_sub (val, val, neg_offset.val);
248 }
249 }
250
251 /* See declaration above. */
252
253 template<typename T>
254 T
255 gdb_mpz::as_integer () const
256 {
257 /* Initialize RESULT, because mpz_export only write the minimum
258 number of bytes, including none if our value is zero! */
259 T result = 0;
260
261 gdb_mpz exported_val (val);
262 if (std::is_signed<T>::value && mpz_cmp_ui (val, 0) < 0)
263 {
264 /* We want to use mpz_export to set the return value, but
265 this function does not handle the sign. So give exported_val
266 a value which is at the same time positive, and has the same
267 bit representation as our negative value. */
268 gdb_mpz neg_offset;
269
270 mpz_ui_pow_ui (neg_offset.val, 2, sizeof (T) * HOST_CHAR_BIT);
271 mpz_add (exported_val.val, exported_val.val, neg_offset.val);
272 }
273
274 mpz_export (&result, NULL /* count */, -1 /* order */,
275 sizeof (T) /* size */, 0 /* endian (0 = native) */,
276 0 /* nails */, exported_val.val);
277 return result;
278 }
279
280 #endif
This page took 0.035888 seconds and 4 git commands to generate.