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 57067548 : integer ()
41 : {
42 17336242 : mpz_init (m_mpz);
43 : }
44 :
45 27231710 : integer (int i)
46 : {
47 26310585 : mpz_init_set_si (m_mpz, i);
48 : }
49 :
50 233700219 : ~integer ()
51 : {
52 120091157 : mpz_clear (m_mpz);
53 3555919 : }
54 :
55 96459261 : integer (const integer &other)
56 : {
57 58914784 : mpz_init_set (m_mpz, other.m_mpz);
58 : }
59 :
60 30385984 : integer (integer &&other)
61 : {
62 30385984 : mpz_init (m_mpz);
63 30246052 : 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 18208800 : is_zero_p () const
73 : {
74 18208800 : return mpz_cmp_si (m_mpz, 0) == 0;
75 : }
76 :
77 : bool
78 49723 : is_one_p () const
79 : {
80 49723 : return mpz_cmp_si (m_mpz, 1) == 0;
81 : }
82 :
83 : integer &
84 4479463 : operator= (const integer &other)
85 : {
86 2307603 : mpz_set (m_mpz, other.m_mpz);
87 4479463 : return *this;
88 : }
89 :
90 : integer &
91 14292077 : operator= (integer &&other)
92 : {
93 10194183 : std::swap (m_mpz, other.m_mpz);
94 11293903 : return *this;
95 : }
96 :
97 : static int
98 3653626 : cmp (const integer &lhs, const integer &rhs)
99 : {
100 3653626 : 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 7698798 : operator== (const integer &other) const
121 : {
122 7697354 : return mpz_cmp (m_mpz, other.m_mpz) == 0;
123 : }
124 :
125 : bool
126 21338 : operator== (int i) const
127 : {
128 21338 : return mpz_cmp_si (m_mpz, i) == 0;
129 : }
130 :
131 : bool
132 1385779 : operator!= (const integer &other) const
133 : {
134 1385779 : return mpz_cmp (m_mpz, other.m_mpz) != 0;
135 : }
136 :
137 : bool
138 1902061 : operator!= (int i) const
139 : {
140 1877461 : return mpz_cmp_si (m_mpz, i) != 0;
141 : }
142 :
143 : bool
144 100263362 : operator< (const integer &other) const
145 : {
146 100263362 : return mpz_cmp (m_mpz, other.m_mpz) < 0;
147 : }
148 :
149 : bool
150 347 : operator< (int i) const
151 : {
152 347 : return mpz_cmp_si (m_mpz, i) < 0;
153 : }
154 :
155 : bool
156 2365924 : operator<= (const integer &other) const
157 : {
158 2365924 : return mpz_cmp (m_mpz, other.m_mpz) <= 0;
159 : }
160 :
161 : bool
162 7298902 : operator> (const integer &other) const
163 : {
164 7298902 : return mpz_cmp (m_mpz, other.m_mpz) > 0;
165 : }
166 :
167 : bool
168 2727364 : operator>= (const integer &other) const
169 : {
170 2727364 : return mpz_cmp (m_mpz, other.m_mpz) >= 0;
171 : }
172 :
173 : /* Addition. */
174 :
175 : integer
176 18029835 : operator+ (const integer &other) const
177 : {
178 15342428 : integer result;
179 18029835 : mpz_add (result.m_mpz, m_mpz, other.m_mpz);
180 15045805 : return result;
181 : }
182 :
183 : integer &
184 3280653 : operator+= (const integer &other)
185 : {
186 3280653 : integer tmp = *this + other;
187 3280653 : *this = std::move (tmp);
188 3280653 : return *this;
189 3280653 : }
190 :
191 : /* Subtraction. */
192 :
193 : integer
194 11080440 : operator- (const integer &other) const
195 : {
196 11066296 : integer result;
197 11080440 : mpz_sub (result.m_mpz, m_mpz, other.m_mpz);
198 11066296 : return result;
199 : }
200 :
201 : integer
202 20342 : operator- () const
203 : {
204 20342 : integer result;
205 20342 : mpz_neg (result.m_mpz, m_mpz);
206 20342 : return result;
207 : }
208 :
209 : integer &
210 14144 : operator-= (const integer &other)
211 : {
212 14144 : integer tmp = *this - other;
213 14144 : *this = std::move (tmp);
214 14144 : return *this;
215 14144 : }
216 :
217 : /* Multiplication. */
218 :
219 : integer
220 1647726 : operator* (const integer &other) const
221 : {
222 1647726 : integer result;
223 1647726 : mpz_mul (result.m_mpz, m_mpz, other.m_mpz);
224 1647726 : return result;
225 : }
226 :
227 : /* Division. */
228 :
229 : integer
230 961756 : floor_div (const integer &other) const
231 : {
232 961756 : gcc_assert (other != 0);
233 :
234 961756 : integer result;
235 961756 : mpz_fdiv_q (result.m_mpz, m_mpz, other.m_mpz);
236 961756 : return result;
237 : }
238 :
239 : /* Modulus. */
240 :
241 : integer
242 940285 : floor_mod (const integer &other) const
243 : {
244 940285 : gcc_assert (other != 0);
245 :
246 940285 : integer result;
247 940285 : mpz_fdiv_r (result.m_mpz, m_mpz, other.m_mpz);
248 940285 : return result;
249 : }
250 :
251 : long
252 23784 : as_long () const
253 : {
254 23784 : return mpz_get_si (m_mpz);
255 : }
256 :
257 : unsigned long
258 72460 : as_unsigned_long () const
259 : {
260 61452 : return mpz_get_ui (m_mpz);
261 : }
262 :
263 : mpz_t &
264 134072 : 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 1746 : class pp_element_integer : public pp_element
279 : {
280 : public:
281 3079 : pp_element_integer (integer val)
282 3079 : : m_val (std::move (val))
283 : {
284 3079 : }
285 :
286 3078 : void add_to_phase_2 (pp_markup::context &ctxt) final override
287 : {
288 3078 : m_val.dump_to_pp (&ctxt.m_pp);
289 3078 : }
290 :
291 : private:
292 : integer m_val;
293 : };
294 :
295 : } // namespace ana
296 :
297 : #endif /* GCC_ANALYZER_INTEGER_H */
|