Line data Source code
1 : /* Arbitrary precision integers.
2 : Copyright (C) 2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it
8 : under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful, but
13 : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #ifndef GCC_ANALYZER_INTEGER_H
22 : #define GCC_ANALYZER_INTEGER_H
23 :
24 : #include "pretty-print-markup.h"
25 :
26 : namespace ana {
27 :
28 : /* A class for arbitrary precision integers.
29 :
30 : This is a C++ wrapper around gmp's mpz_t, with integration with
31 : pretty_printer.
32 :
33 : We don't use gmp's gmpxx.h: it uses exceptions, which we can't use. */
34 :
35 : class integer
36 : {
37 : public:
38 : static constexpr const char *dump_type_name = "integer";
39 :
40 58012288 : integer ()
41 : {
42 17634857 : mpz_init (m_mpz);
43 : }
44 :
45 27708802 : integer (int i)
46 : {
47 26780444 : mpz_init_set_si (m_mpz, i);
48 : }
49 :
50 237132901 : ~integer ()
51 : {
52 121860529 : mpz_clear (m_mpz);
53 3617399 : }
54 :
55 97849851 : integer (const integer &other)
56 : {
57 59750466 : mpz_init_set (m_mpz, other.m_mpz);
58 : }
59 :
60 30843029 : integer (integer &&other)
61 : {
62 30843029 : mpz_init (m_mpz);
63 30695579 : std::swap (m_mpz, other.m_mpz);
64 : }
65 :
66 : /* Make it easier to use either integer or integer_with_unit
67 : in a single template. */
68 : const integer &
69 : get_integer () const { return *this; }
70 :
71 : bool
72 18437829 : is_zero_p () const
73 : {
74 18437829 : return mpz_cmp_si (m_mpz, 0) == 0;
75 : }
76 :
77 : bool
78 50552 : is_one_p () const
79 : {
80 50552 : return mpz_cmp_si (m_mpz, 1) == 0;
81 : }
82 :
83 : integer &
84 4537633 : operator= (const integer &other)
85 : {
86 2329231 : mpz_set (m_mpz, other.m_mpz);
87 4537633 : return *this;
88 : }
89 :
90 : integer &
91 14564746 : operator= (integer &&other)
92 : {
93 10376295 : std::swap (m_mpz, other.m_mpz);
94 11484485 : return *this;
95 : }
96 :
97 : static int
98 3706649 : cmp (const integer &lhs, const integer &rhs)
99 : {
100 3706649 : return mpz_cmp (lhs.m_mpz, rhs.m_mpz);
101 : }
102 :
103 : void
104 : dump_to_pp (pretty_printer *pp) const;
105 :
106 : void
107 : dump () const;
108 :
109 : std::unique_ptr<json::value>
110 : to_json () const;
111 :
112 : std::unique_ptr<diagnostics::markup::node>
113 : make_markup_node () const;
114 :
115 : /* Overloaded operators. */
116 :
117 : /* Comparison. */
118 :
119 : bool
120 7836136 : operator== (const integer &other) const
121 : {
122 7834684 : return mpz_cmp (m_mpz, other.m_mpz) == 0;
123 : }
124 :
125 : bool
126 21759 : operator== (int i) const
127 : {
128 21759 : return mpz_cmp_si (m_mpz, i) == 0;
129 : }
130 :
131 : bool
132 1395845 : operator!= (const integer &other) const
133 : {
134 1395845 : return mpz_cmp (m_mpz, other.m_mpz) != 0;
135 : }
136 :
137 : bool
138 1917408 : operator!= (int i) const
139 : {
140 1892598 : return mpz_cmp_si (m_mpz, i) != 0;
141 : }
142 :
143 : bool
144 102023998 : operator< (const integer &other) const
145 : {
146 102023998 : return mpz_cmp (m_mpz, other.m_mpz) < 0;
147 : }
148 :
149 : bool
150 375 : operator< (int i) const
151 : {
152 375 : return mpz_cmp_si (m_mpz, i) < 0;
153 : }
154 :
155 : bool
156 2422579 : operator<= (const integer &other) const
157 : {
158 2422579 : return mpz_cmp (m_mpz, other.m_mpz) <= 0;
159 : }
160 :
161 : bool
162 7415074 : operator> (const integer &other) const
163 : {
164 7415074 : return mpz_cmp (m_mpz, other.m_mpz) > 0;
165 : }
166 :
167 : bool
168 2817018 : operator>= (const integer &other) const
169 : {
170 2817018 : return mpz_cmp (m_mpz, other.m_mpz) >= 0;
171 : }
172 :
173 : /* Addition. */
174 :
175 : integer
176 18400467 : operator+ (const integer &other) const
177 : {
178 15632195 : integer result;
179 18400467 : mpz_add (result.m_mpz, m_mpz, other.m_mpz);
180 15334647 : return result;
181 : }
182 :
183 : integer &
184 3363368 : operator+= (const integer &other)
185 : {
186 3363368 : integer tmp = *this + other;
187 3363368 : *this = std::move (tmp);
188 3363368 : return *this;
189 3363368 : }
190 :
191 : /* Subtraction. */
192 :
193 : integer
194 11214111 : operator- (const integer &other) const
195 : {
196 11199670 : integer result;
197 11214111 : mpz_sub (result.m_mpz, m_mpz, other.m_mpz);
198 11199670 : return result;
199 : }
200 :
201 : integer
202 20402 : operator- () const
203 : {
204 20402 : integer result;
205 20402 : mpz_neg (result.m_mpz, m_mpz);
206 20402 : return result;
207 : }
208 :
209 : integer &
210 14441 : operator-= (const integer &other)
211 : {
212 14441 : integer tmp = *this - other;
213 14441 : *this = std::move (tmp);
214 14441 : return *this;
215 14441 : }
216 :
217 : /* Multiplication. */
218 :
219 : integer
220 1656744 : operator* (const integer &other) const
221 : {
222 1656744 : integer result;
223 1656744 : mpz_mul (result.m_mpz, m_mpz, other.m_mpz);
224 1656744 : return result;
225 : }
226 :
227 : /* Division. */
228 :
229 : integer
230 969485 : floor_div (const integer &other) const
231 : {
232 969485 : gcc_assert (other != 0);
233 :
234 969485 : integer result;
235 969485 : mpz_fdiv_q (result.m_mpz, m_mpz, other.m_mpz);
236 969485 : return result;
237 : }
238 :
239 : /* Modulus. */
240 :
241 : integer
242 947903 : floor_mod (const integer &other) const
243 : {
244 947903 : gcc_assert (other != 0);
245 :
246 947903 : integer result;
247 947903 : mpz_fdiv_r (result.m_mpz, m_mpz, other.m_mpz);
248 947903 : return result;
249 : }
250 :
251 : long
252 22736 : as_long () const
253 : {
254 22736 : return mpz_get_si (m_mpz);
255 : }
256 :
257 : unsigned long
258 75722 : as_unsigned_long () const
259 : {
260 63089 : return mpz_get_ui (m_mpz);
261 : }
262 :
263 : mpz_t &
264 134684 : access_mpz () { return m_mpz; }
265 :
266 : private:
267 : mpz_t m_mpz;
268 : };
269 :
270 : integer
271 : convert_from_integer_cst (const_tree int_cst);
272 :
273 : tree
274 : make_integer_cst_from_integer (tree type, const integer &value, bool wrap);
275 :
276 : using concrete_integer = integer;
277 :
278 1782 : class pp_element_integer : public pp_element
279 : {
280 : public:
281 3131 : pp_element_integer (integer val)
282 3131 : : m_val (std::move (val))
283 : {
284 3131 : }
285 :
286 3130 : void add_to_phase_2 (pp_markup::context &ctxt) final override
287 : {
288 3130 : m_val.dump_to_pp (&ctxt.m_pp);
289 3130 : }
290 :
291 : private:
292 : integer m_val;
293 : };
294 :
295 : } // namespace ana
296 :
297 : #endif /* GCC_ANALYZER_INTEGER_H */
|