Line data Source code
1 : /* Unit-safe arbitrary precision integers (e.g. "32 bytes").
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_WITH_UNIT_H
22 : #define GCC_ANALYZER_INTEGER_WITH_UNIT_H
23 :
24 : #include "analyzer/integer.h"
25 : #include "analyzer/units.h"
26 :
27 : namespace ana {
28 :
29 : /* A unit-safe arbitrary precision integer e.g. "32_bytes" or "5_bits". */
30 :
31 : template<typename Unit>
32 115466046 : class integer_with_unit
33 : {
34 : public:
35 : static constexpr const char *dump_type_name = Unit::dump_type_name;
36 :
37 15808909 : integer_with_unit () : m_integer () {}
38 :
39 9727564 : explicit integer_with_unit (int i) : m_integer (i) {}
40 :
41 31503698 : explicit integer_with_unit (integer i) : m_integer (std::move (i)) {}
42 :
43 978643 : const integer &get_integer () const { return m_integer; }
44 :
45 : void
46 : dump_to_pp (pretty_printer *pp,
47 : units::role role = units::role::generic) const;
48 : void
49 : dump () const;
50 :
51 : std::unique_ptr<json::object>
52 : to_json () const;
53 :
54 : std::unique_ptr<diagnostics::markup::node>
55 : make_markup_node () const;
56 :
57 : bool
58 18178124 : is_zero_p () const { return m_integer.is_zero_p (); }
59 :
60 : bool
61 32429 : is_one_p () const { return m_integer.is_one_p (); }
62 :
63 : static int
64 3652970 : cmp (const integer_with_unit<Unit> &lhs,
65 : const integer_with_unit<Unit> &rhs)
66 : {
67 3652970 : return integer::cmp (lhs.m_integer, rhs.m_integer);
68 : }
69 :
70 : integer_with_unit<Unit>&
71 2490024 : operator+= (const integer_with_unit<Unit> &rhs)
72 : {
73 2490024 : m_integer += rhs.m_integer;
74 : return *this;
75 : }
76 :
77 : integer_with_unit<Unit>&
78 14144 : operator-= (const integer_with_unit<Unit> &rhs)
79 : {
80 14144 : m_integer -= rhs.m_integer;
81 : return *this;
82 : }
83 :
84 : integer_with_unit<Unit>&
85 789880 : operator++ ()
86 : {
87 789880 : m_integer += 1;
88 789880 : return *this;
89 : }
90 :
91 : bool
92 100146336 : operator< (const integer_with_unit<Unit> &rhs) const
93 : {
94 100146336 : return m_integer < rhs.m_integer;
95 : }
96 :
97 : bool
98 2365825 : operator<= (const integer_with_unit<Unit> &rhs) const
99 : {
100 2365825 : return m_integer <= rhs.m_integer;
101 : }
102 :
103 : bool
104 7296370 : operator> (const integer_with_unit<Unit> &rhs) const
105 : {
106 7296370 : return m_integer > rhs.m_integer;
107 : }
108 :
109 : bool
110 2727328 : operator>= (const integer_with_unit<Unit> &rhs) const
111 : {
112 2727328 : return m_integer >= rhs.m_integer;
113 : }
114 :
115 : integer_with_unit<Unit>
116 20318 : operator- () const
117 : {
118 20318 : return integer_with_unit<Unit> (-m_integer);
119 : }
120 :
121 : /* Division. */
122 :
123 : integer
124 13879 : floor_div (const integer_with_unit<Unit> &other) const
125 : {
126 13879 : return get_integer ().floor_div (other.get_integer ());
127 : }
128 :
129 : /* Modulus. */
130 :
131 : integer_with_unit<Unit>
132 911595 : floor_mod (const integer_with_unit<Unit> &other) const
133 : {
134 : return integer_with_unit<Unit>
135 911595 : (get_integer ().floor_mod (other.get_integer ()));
136 : }
137 :
138 : long
139 23733 : as_long () const
140 : {
141 23733 : return m_integer.as_long ();
142 : }
143 :
144 : unsigned long
145 70464 : as_unsigned_long () const
146 : {
147 58898 : return m_integer.as_unsigned_long ();
148 : }
149 :
150 : private:
151 : integer m_integer;
152 : };
153 :
154 : /* User-defined literal operators. */
155 :
156 : inline integer_with_unit<units::bit>
157 35259 : operator ""_bit (unsigned long long v)
158 : {
159 29795 : gcc_checking_assert (v == 1);
160 35163 : return integer_with_unit<units::bit> (v);
161 : }
162 :
163 : inline integer_with_unit<units::bit>
164 13249436 : operator ""_bits (unsigned long long v)
165 : {
166 8297849 : gcc_checking_assert (v != 1);
167 13249296 : return integer_with_unit<units::bit> (v);
168 : }
169 :
170 : inline integer_with_unit<units::byte>
171 1231974 : operator ""_byte (unsigned long long v)
172 : {
173 256169 : gcc_checking_assert (v == 1);
174 1231962 : return integer_with_unit<units::byte> (v);
175 : }
176 :
177 : inline integer_with_unit<units::byte>
178 321808 : operator ""_bytes (unsigned long long v)
179 : {
180 300076 : gcc_checking_assert (v != 1);
181 320912 : return integer_with_unit<units::byte> (v);
182 : }
183 :
184 : /* Explicit conversions. */
185 :
186 : inline integer_with_unit<units::bit>
187 646106 : bits_from_bytes (integer_with_unit<units::byte> bytes)
188 : {
189 1292212 : return integer_with_unit<units::bit> (bytes.get_integer () * BITS_PER_UNIT);
190 : }
191 :
192 : bool
193 : maybe_bytes_from_bits (integer_with_unit<units::bit> bits,
194 : integer_with_unit<units::byte> *out);
195 :
196 : /* Overloaded operators. */
197 :
198 : /* Comparisons of like units. */
199 :
200 : template<typename Unit>
201 : inline bool
202 7698634 : operator== (const integer_with_unit<Unit> &lhs,
203 : const integer_with_unit<Unit> &rhs)
204 : {
205 7697440 : return lhs.get_integer () == rhs.get_integer ();
206 : }
207 :
208 : template<typename Unit>
209 : inline bool
210 1385763 : operator!= (const integer_with_unit<Unit> &lhs,
211 : const integer_with_unit<Unit> &rhs)
212 : {
213 1385747 : return lhs.get_integer () != rhs.get_integer ();
214 : }
215 :
216 : /* Comparisons of compatible units, implicitly converting to bits. */
217 :
218 : inline bool
219 8 : operator== (const integer_with_unit<units::bit> &lhs,
220 : const integer_with_unit<units::byte> &rhs)
221 : {
222 8 : return lhs == bits_from_bytes (rhs);
223 : }
224 :
225 : inline bool
226 : operator== (const integer_with_unit<units::byte> &lhs,
227 : const integer_with_unit<units::bit> &rhs)
228 : {
229 : return bits_from_bytes (lhs) == rhs;
230 : }
231 :
232 : inline bool
233 16 : operator!= (const integer_with_unit<units::bit> &lhs,
234 : const integer_with_unit<units::byte> &rhs)
235 : {
236 16 : return lhs != bits_from_bytes (rhs);
237 : }
238 :
239 : inline bool
240 : operator!= (const integer_with_unit<units::byte> &lhs,
241 : const integer_with_unit<units::bit> &rhs)
242 : {
243 : return bits_from_bytes (lhs) != rhs;
244 : }
245 :
246 : /* Addition of integer_with_unit of same unit. */
247 :
248 : template<typename Unit>
249 : inline integer_with_unit<Unit>
250 14749150 : operator+ (const integer_with_unit<Unit> &lhs,
251 : const integer_with_unit<Unit> &rhs)
252 : {
253 29498300 : return integer_with_unit<Unit> (lhs.get_integer () + rhs.get_integer ());
254 : }
255 :
256 : /* Addition of integer_with_unit of compatible types, implicitly converting to bits. */
257 :
258 : inline integer_with_unit<units::bit>
259 4 : operator+ (const integer_with_unit<units::bit> &lhs,
260 : const integer_with_unit<units::byte> &rhs)
261 : {
262 4 : return lhs + bits_from_bytes (rhs);
263 : }
264 :
265 : inline integer_with_unit<units::bit>
266 4 : operator+ (const integer_with_unit<units::byte> &lhs,
267 : const integer_with_unit<units::bit> &rhs)
268 : {
269 4 : return bits_from_bytes (lhs) + rhs;
270 : }
271 :
272 : /* Negation. */
273 :
274 : template<typename Unit>
275 : inline integer_with_unit<Unit>
276 : operator- (const integer_with_unit<Unit> &i)
277 : {
278 : return integer_with_unit<Unit> (-(i.get_integer ()));
279 : }
280 :
281 : /* Subtraction of integer_with_unit of same kind. */
282 :
283 : template<typename Unit>
284 : inline integer_with_unit<Unit>
285 11066276 : operator- (const integer_with_unit<Unit> &lhs,
286 : const integer_with_unit<Unit> &rhs)
287 : {
288 22132552 : return integer_with_unit<Unit> (lhs.get_integer () - rhs.get_integer ());
289 : }
290 :
291 : /* Subtraction of integer_with_unit of compatible types,
292 : implicitly converting to bits. */
293 :
294 : integer_with_unit<units::bit>
295 4 : inline operator- (const integer_with_unit<units::bit> &lhs,
296 : const integer_with_unit<units::byte> &rhs)
297 : {
298 4 : return lhs - bits_from_bytes (rhs);
299 : }
300 :
301 : inline integer_with_unit<units::bit>
302 8 : operator- (const integer_with_unit<units::byte> &lhs,
303 : const integer_with_unit<units::bit> &rhs)
304 : {
305 8 : return bits_from_bytes (lhs) - rhs;
306 : }
307 :
308 : /* Multiplication of integer_with_unit by a unitless value. */
309 :
310 : template<typename Unit>
311 : inline integer_with_unit<Unit>
312 6548 : operator* (const integer_with_unit<Unit> &lhs,
313 : const integer &rhs)
314 : {
315 13096 : return integer_with_unit<Unit> (lhs.get_integer () * rhs);
316 : }
317 :
318 : /* Multiplication of unitless value by a integer_with_unit. */
319 :
320 : template<typename Unit>
321 : inline integer_with_unit<Unit>
322 10818 : operator* (const integer &lhs,
323 : const integer_with_unit<Unit> &rhs)
324 : {
325 21636 : return integer_with_unit<Unit> (lhs * rhs.get_integer ());
326 : }
327 :
328 : using concrete_bit_size = integer_with_unit<units::bit>;
329 : using concrete_bit_offset = integer_with_unit<units::bit>;
330 : using concrete_byte_size = integer_with_unit<units::byte>;
331 : using concrete_byte_offset = integer_with_unit<units::byte>;
332 :
333 : inline concrete_byte_offset
334 32 : round_bit_to_lowest_byte (concrete_bit_offset bit_offset)
335 : {
336 32 : return concrete_byte_offset
337 32 : (bit_offset.get_integer ().floor_div (BITS_PER_UNIT));
338 : }
339 :
340 : inline concrete_bit_offset
341 32 : bit_within_byte (concrete_bit_offset bit_offset)
342 : {
343 32 : return concrete_bit_offset
344 32 : (bit_offset.get_integer ().floor_mod (BITS_PER_UNIT));
345 : }
346 :
347 : extern bool
348 : get_type_size (const_tree type, concrete_bit_size *out);
349 :
350 : /* Functions for converting from older representation.
351 :
352 : Ultimately we'll want {bit,byte}_{offset,size}_t to be
353 : integer_with_unit<units::{bit,byte}>
354 : but for now these should help migrate the code. */
355 :
356 : concrete_bit_offset
357 : convert_from_bit_offset_t (bit_offset_t);
358 :
359 : concrete_bit_size
360 : convert_from_bit_size_t (bit_size_t);
361 :
362 : integer_with_unit<units::byte>
363 : convert_from_byte_offset_t (byte_offset_t);
364 :
365 : integer_with_unit<units::byte>
366 : convert_from_byte_size_t (byte_size_t);
367 :
368 : /* Functions for converting to older representation. */
369 :
370 : bit_offset_t
371 : convert_to_bit_offset_t (concrete_bit_offset);
372 :
373 : bit_size_t
374 : convert_to_bit_size_t (concrete_bit_size);
375 :
376 : byte_offset_t
377 : convert_to_byte_offset_t (concrete_byte_offset);
378 :
379 : byte_size_t
380 : convert_to_byte_size_t (concrete_byte_size);
381 :
382 : template <typename ConcreteValueType>
383 : inline bool
384 : eval_comparison_p (ConcreteValueType a,
385 : enum tree_code op,
386 : ConcreteValueType b)
387 : {
388 : switch (op)
389 : {
390 : default:
391 : gcc_unreachable ();
392 : case EQ_EXPR: return a == b;
393 : case NE_EXPR: return a != b;
394 : case GE_EXPR: return a >= b;
395 : case LE_EXPR: return a <= b;
396 : case GT_EXPR: return a > b;
397 : case LT_EXPR: return a < b;
398 : }
399 : }
400 :
401 : } // namespace ana
402 :
403 : #endif /* GCC_ANALYZER_INTEGER_WITH_UNIT_H */
|