GCC Middle and Back End API Reference
integer.h
Go to the documentation of this file.
1/* Arbitrary precision integers.
2 Copyright (C) 2026 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along 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
26namespace 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
36{
37public:
38 static constexpr const char *dump_type_name = "integer";
39
41 {
42 mpz_init (m_mpz);
43 }
44
45 integer (int i)
46 {
47 mpz_init_set_si (m_mpz, i);
48 }
49
51 {
52 mpz_clear (m_mpz);
53 }
54
55 integer (const integer &other)
56 {
57 mpz_init_set (m_mpz, other.m_mpz);
58 }
59
60 integer (integer &&other)
61 {
62 mpz_init (m_mpz);
63 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 is_zero_p () const
73 {
74 return mpz_cmp_si (m_mpz, 0) == 0;
75 }
76
77 bool
78 is_one_p () const
79 {
80 return mpz_cmp_si (m_mpz, 1) == 0;
81 }
82
83 integer &
84 operator= (const integer &other)
85 {
86 mpz_set (m_mpz, other.m_mpz);
87 return *this;
88 }
89
90 integer &
92 {
93 std::swap (m_mpz, other.m_mpz);
94 return *this;
95 }
96
97 static int
98 cmp (const integer &lhs, const integer &rhs)
99 {
100 return mpz_cmp (lhs.m_mpz, rhs.m_mpz);
101 }
102
103 void
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>
114
115 /* Overloaded operators. */
116
117 /* Comparison. */
118
119 bool
120 operator== (const integer &other) const
121 {
122 return mpz_cmp (m_mpz, other.m_mpz) == 0;
123 }
124
125 bool
126 operator== (int i) const
127 {
128 return mpz_cmp_si (m_mpz, i) == 0;
129 }
130
131 bool
132 operator!= (const integer &other) const
133 {
134 return mpz_cmp (m_mpz, other.m_mpz) != 0;
135 }
136
137 bool
138 operator!= (int i) const
139 {
140 return mpz_cmp_si (m_mpz, i) != 0;
141 }
142
143 bool
144 operator< (const integer &other) const
145 {
146 return mpz_cmp (m_mpz, other.m_mpz) < 0;
147 }
148
149 bool
150 operator< (int i) const
151 {
152 return mpz_cmp_si (m_mpz, i) < 0;
153 }
154
155 bool
156 operator<= (const integer &other) const
157 {
158 return mpz_cmp (m_mpz, other.m_mpz) <= 0;
159 }
160
161 bool
162 operator> (const integer &other) const
163 {
164 return mpz_cmp (m_mpz, other.m_mpz) > 0;
165 }
166
167 bool
168 operator>= (const integer &other) const
169 {
170 return mpz_cmp (m_mpz, other.m_mpz) >= 0;
171 }
172
173 /* Addition. */
174
175 integer
176 operator+ (const integer &other) const
177 {
178 integer result;
179 mpz_add (result.m_mpz, m_mpz, other.m_mpz);
180 return result;
181 }
182
183 integer &
184 operator+= (const integer &other)
185 {
186 integer tmp = *this + other;
187 *this = std::move (tmp);
188 return *this;
189 }
190
191 /* Subtraction. */
192
193 integer
194 operator- (const integer &other) const
195 {
196 integer result;
197 mpz_sub (result.m_mpz, m_mpz, other.m_mpz);
198 return result;
199 }
200
201 integer
203 {
204 integer result;
205 mpz_neg (result.m_mpz, m_mpz);
206 return result;
207 }
208
209 integer &
210 operator-= (const integer &other)
211 {
212 integer tmp = *this - other;
213 *this = std::move (tmp);
214 return *this;
215 }
216
217 /* Multiplication. */
218
219 integer
220 operator* (const integer &other) const
221 {
222 integer result;
223 mpz_mul (result.m_mpz, m_mpz, other.m_mpz);
224 return result;
225 }
226
227 /* Division. */
228
229 integer
230 floor_div (const integer &other) const
231 {
232 gcc_assert (other != 0);
233
234 integer result;
235 mpz_fdiv_q (result.m_mpz, m_mpz, other.m_mpz);
236 return result;
237 }
238
239 /* Modulus. */
240
241 integer
242 floor_mod (const integer &other) const
243 {
244 gcc_assert (other != 0);
245
246 integer result;
247 mpz_fdiv_r (result.m_mpz, m_mpz, other.m_mpz);
248 return result;
249 }
250
251 long
252 as_long () const
253 {
254 return mpz_get_si (m_mpz);
255 }
256
257 unsigned long
259 {
260 return mpz_get_ui (m_mpz);
261 }
262
263 mpz_t &
264 access_mpz () { return m_mpz; }
265
266private:
267 mpz_t m_mpz;
268};
269
272
273tree
275
277
279{
280public:
282 : m_val (std::move (val))
283 {
284 }
285
286 void add_to_phase_2 (pp_markup::context &ctxt) final override
287 {
288 m_val.dump_to_pp (&ctxt.m_pp);
289 }
290
291private:
293};
294
295} // namespace ana
296
297#endif /* GCC_ANALYZER_INTEGER_H */
Definition integer.h:36
void dump_to_pp(pretty_printer *pp) const
integer & operator+=(const integer &other)
Definition integer.h:184
std::unique_ptr< diagnostics::markup::node > make_markup_node() const
bool operator>=(const integer &other) const
Definition integer.h:168
bool operator<=(const integer &other) const
Definition integer.h:156
integer operator-() const
Definition integer.h:202
long as_long() const
Definition integer.h:252
~integer()
Definition integer.h:50
bool operator<(const integer &other) const
Definition integer.h:144
integer(integer &&other)
Definition integer.h:60
mpz_t m_mpz
Definition integer.h:267
integer(const integer &other)
Definition integer.h:55
integer operator+(const integer &other) const
Definition integer.h:176
integer()
Definition integer.h:40
bool is_zero_p() const
Definition integer.h:72
integer(int i)
Definition integer.h:45
bool operator>(const integer &other) const
Definition integer.h:162
std::unique_ptr< json::value > to_json() const
integer floor_mod(const integer &other) const
Definition integer.h:242
integer & operator=(const integer &other)
Definition integer.h:84
static int cmp(const integer &lhs, const integer &rhs)
Definition integer.h:98
void dump() const
integer floor_div(const integer &other) const
Definition integer.h:230
bool is_one_p() const
Definition integer.h:78
static constexpr const char * dump_type_name
Definition integer.h:38
const integer & get_integer() const
Definition integer.h:69
bool operator==(const integer &other) const
Definition integer.h:120
unsigned long as_unsigned_long() const
Definition integer.h:258
integer & operator-=(const integer &other)
Definition integer.h:210
bool operator!=(const integer &other) const
Definition integer.h:132
integer operator*(const integer &other) const
Definition integer.h:220
mpz_t & access_mpz()
Definition integer.h:264
pp_element_integer(integer val)
Definition integer.h:281
void add_to_phase_2(pp_markup::context &ctxt) final override
Definition integer.h:286
integer m_val
Definition integer.h:292
Definition type.h:29
Definition pretty-print-markup.h:30
Definition pretty-print.h:241
pp_markup::element pp_element
Definition coretypes.h:180
const union tree_node * const_tree
Definition coretypes.h:98
union tree_node * tree
Definition coretypes.h:97
Definition access-diagram.h:30
tree make_integer_cst_from_integer(tree type, const integer &value, bool wrap)
@ other
Definition common.h:535
integer concrete_integer
Definition integer.h:276
integer convert_from_integer_cst(const_tree int_cst)
i
Definition poly-int.h:776
Definition ira-emit.cc:158
#define gcc_assert(EXPR)
Definition system.h:772