GCC Middle and Back End API Reference
integer-with-unit.h
Go to the documentation of this file.
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
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_WITH_UNIT_H
22#define GCC_ANALYZER_INTEGER_WITH_UNIT_H
23
24#include "analyzer/integer.h"
25#include "analyzer/units.h"
26
27namespace ana {
28
29/* A unit-safe arbitrary precision integer e.g. "32_bytes" or "5_bits". */
30
31template<typename Unit>
33{
34public:
35 static constexpr const char *dump_type_name = Unit::dump_type_name;
36
38
39 explicit integer_with_unit (int i) : m_integer (i) {}
40
41 explicit integer_with_unit (integer i) : m_integer (std::move (i)) {}
42
43 const integer &get_integer () const { return m_integer; }
44
45 void
48 void
49 dump () const;
50
51 std::unique_ptr<json::object>
52 to_json () const;
53
54 std::unique_ptr<diagnostics::markup::node>
56
57 bool
58 is_zero_p () const { return m_integer.is_zero_p (); }
59
60 bool
61 is_one_p () const { return m_integer.is_one_p (); }
62
63 static int
65 const integer_with_unit<Unit> &rhs)
66 {
67 return integer::cmp (lhs.m_integer, rhs.m_integer);
68 }
69
72 {
73 m_integer += rhs.m_integer;
74 return *this;
75 }
76
79 {
80 m_integer -= rhs.m_integer;
81 return *this;
82 }
83
86 {
87 m_integer += 1;
88 return *this;
89 }
90
91 bool
92 operator< (const integer_with_unit<Unit> &rhs) const
93 {
94 return m_integer < rhs.m_integer;
95 }
96
97 bool
98 operator<= (const integer_with_unit<Unit> &rhs) const
99 {
100 return m_integer <= rhs.m_integer;
101 }
102
103 bool
105 {
106 return m_integer > rhs.m_integer;
107 }
108
109 bool
111 {
112 return m_integer >= rhs.m_integer;
113 }
114
117 {
119 }
120
121 /* Division. */
122
123 integer
125 {
126 return get_integer ().floor_div (other.get_integer ());
127 }
128
129 /* Modulus. */
130
133 {
135 (get_integer ().floor_mod (other.get_integer ()));
136 }
137
138 long
139 as_long () const
140 {
141 return m_integer.as_long ();
142 }
143
144 unsigned long
146 {
147 return m_integer.as_unsigned_long ();
148 }
149
150private:
152};
153
154/* User-defined literal operators. */
155
157operator ""_bit (unsigned long long v)
158{
159 gcc_checking_assert (v == 1);
161}
162
163inline integer_with_unit<units::bit>
164operator ""_bits (unsigned long long v)
165{
166 gcc_checking_assert (v != 1);
168}
169
170inline integer_with_unit<units::byte>
171operator ""_byte (unsigned long long v)
172{
173 gcc_checking_assert (v == 1);
175}
176
177inline integer_with_unit<units::byte>
178operator ""_bytes (unsigned long long v)
179{
180 gcc_checking_assert (v != 1);
182}
183
184/* Explicit conversions. */
185
186inline integer_with_unit<units::bit>
188{
189 return integer_with_unit<units::bit> (bytes.get_integer () * BITS_PER_UNIT);
190}
191
192bool
195
196/* Overloaded operators. */
197
198/* Comparisons of like units. */
199
200template<typename Unit>
201inline bool
203 const integer_with_unit<Unit> &rhs)
204{
205 return lhs.get_integer () == rhs.get_integer ();
206}
207
208template<typename Unit>
209inline bool
211 const integer_with_unit<Unit> &rhs)
212{
213 return lhs.get_integer () != rhs.get_integer ();
214}
215
216/* Comparisons of compatible units, implicitly converting to bits. */
217
218inline bool
221{
222 return lhs == bits_from_bytes (rhs);
223}
224
225inline bool
228{
229 return bits_from_bytes (lhs) == rhs;
230}
231
232inline bool
235{
236 return lhs != bits_from_bytes (rhs);
237}
238
239inline bool
242{
243 return bits_from_bytes (lhs) != rhs;
244}
245
246/* Addition of integer_with_unit of same unit. */
247
248template<typename Unit>
249inline integer_with_unit<Unit>
251 const integer_with_unit<Unit> &rhs)
252{
253 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
258inline integer_with_unit<units::bit>
261{
262 return lhs + bits_from_bytes (rhs);
263}
264
265inline integer_with_unit<units::bit>
268{
269 return bits_from_bytes (lhs) + rhs;
270}
271
272/* Negation. */
273
274template<typename Unit>
275inline integer_with_unit<Unit>
277{
278 return integer_with_unit<Unit> (-(i.get_integer ()));
279}
280
281/* Subtraction of integer_with_unit of same kind. */
282
283template<typename Unit>
284inline integer_with_unit<Unit>
286 const integer_with_unit<Unit> &rhs)
287{
288 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
294integer_with_unit<units::bit>
297{
298 return lhs - bits_from_bytes (rhs);
299}
300
301inline integer_with_unit<units::bit>
304{
305 return bits_from_bytes (lhs) - rhs;
306}
307
308/* Multiplication of integer_with_unit by a unitless value. */
309
310template<typename Unit>
311inline integer_with_unit<Unit>
313 const integer &rhs)
314{
315 return integer_with_unit<Unit> (lhs.get_integer () * rhs);
316}
317
318/* Multiplication of unitless value by a integer_with_unit. */
319
320template<typename Unit>
321inline integer_with_unit<Unit>
323 const integer_with_unit<Unit> &rhs)
324{
325 return integer_with_unit<Unit> (lhs * rhs.get_integer ());
326}
327
332
335{
337 (bit_offset.get_integer ().floor_div (BITS_PER_UNIT));
338}
339
342{
344 (bit_offset.get_integer ().floor_mod (BITS_PER_UNIT));
345}
346
347extern bool
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
358
361
364
367
368/* Functions for converting to older representation. */
369
372
375
378
381
382template <typename ConcreteValueType>
383inline bool
384eval_comparison_p (ConcreteValueType a,
385 enum tree_code op,
386 ConcreteValueType b)
387{
388 switch (op)
389 {
390 default:
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 */
Definition integer-with-unit.h:33
bool operator<=(const integer_with_unit< Unit > &rhs) const
Definition integer-with-unit.h:98
static int cmp(const integer_with_unit< Unit > &lhs, const integer_with_unit< Unit > &rhs)
Definition integer-with-unit.h:64
static constexpr const char * dump_type_name
Definition integer-with-unit.h:35
integer_with_unit< Unit > & operator-=(const integer_with_unit< Unit > &rhs)
Definition integer-with-unit.h:78
void dump_to_pp(pretty_printer *pp, units::role role=units::role::generic) const
integer_with_unit< Unit > & operator++()
Definition integer-with-unit.h:85
integer_with_unit< Unit > operator-() const
Definition integer-with-unit.h:116
integer m_integer
Definition integer-with-unit.h:151
std::unique_ptr< json::object > to_json() const
integer_with_unit< Unit > & operator+=(const integer_with_unit< Unit > &rhs)
Definition integer-with-unit.h:71
integer_with_unit(int i)
Definition integer-with-unit.h:39
const integer & get_integer() const
Definition integer-with-unit.h:43
bool operator>(const integer_with_unit< Unit > &rhs) const
Definition integer-with-unit.h:104
unsigned long as_unsigned_long() const
Definition integer-with-unit.h:145
bool is_zero_p() const
Definition integer-with-unit.h:58
integer_with_unit()
Definition integer-with-unit.h:37
integer floor_div(const integer_with_unit< Unit > &other) const
Definition integer-with-unit.h:124
integer_with_unit< Unit > floor_mod(const integer_with_unit< Unit > &other) const
Definition integer-with-unit.h:132
bool operator>=(const integer_with_unit< Unit > &rhs) const
Definition integer-with-unit.h:110
bool operator<(const integer_with_unit< Unit > &rhs) const
Definition integer-with-unit.h:92
integer_with_unit(integer i)
Definition integer-with-unit.h:41
long as_long() const
Definition integer-with-unit.h:139
std::unique_ptr< diagnostics::markup::node > make_markup_node() const
bool is_one_p() const
Definition integer-with-unit.h:61
Definition integer.h:36
long as_long() const
Definition integer.h:252
bool is_zero_p() const
Definition integer.h:72
integer floor_mod(const integer &other) const
Definition integer.h:242
static int cmp(const integer &lhs, const integer &rhs)
Definition integer.h:98
integer floor_div(const integer &other) const
Definition integer.h:230
bool is_one_p() const
Definition integer.h:78
unsigned long as_unsigned_long() const
Definition integer.h:258
Definition type.h:29
Definition pretty-print.h:241
const union tree_node * const_tree
Definition coretypes.h:98
tree_code
Definition genmatch.cc:1011
role
Definition units.h:60
@ generic
Definition units.h:61
Definition access-diagram.h:30
concrete_byte_offset round_bit_to_lowest_byte(concrete_bit_offset bit_offset)
Definition integer-with-unit.h:334
integer_with_unit< Unit > operator-(const integer_with_unit< Unit > &i)
Definition integer-with-unit.h:276
byte_size_t convert_to_byte_size_t(concrete_byte_size)
byte_offset_t convert_to_byte_offset_t(concrete_byte_offset)
offset_int byte_size_t
Definition common.h:275
integer_with_unit< units::bit > concrete_bit_offset
Definition integer-with-unit.h:329
bool operator!=(const integer_with_unit< Unit > &lhs, const integer_with_unit< Unit > &rhs)
Definition integer-with-unit.h:210
offset_int bit_offset_t
Definition common.h:272
concrete_bit_size convert_from_bit_size_t(bit_size_t)
integer_with_unit< Unit > operator+(const integer_with_unit< Unit > &lhs, const integer_with_unit< Unit > &rhs)
Definition integer-with-unit.h:250
bool operator==(const integer_with_unit< Unit > &lhs, const integer_with_unit< Unit > &rhs)
Definition integer-with-unit.h:202
bool maybe_bytes_from_bits(concrete_bit_range bits, concrete_byte_range *out)
bool eval_comparison_p(ConcreteValueType a, enum tree_code op, ConcreteValueType b)
Definition integer-with-unit.h:384
integer_with_unit< units::bit > concrete_bit_size
Definition integer-with-unit.h:328
integer_with_unit< Unit > operator*(const integer_with_unit< Unit > &lhs, const integer &rhs)
Definition integer-with-unit.h:312
integer_with_unit< units::byte > convert_from_byte_offset_t(byte_offset_t)
@ other
Definition common.h:535
concrete_bit_offset bit_within_byte(concrete_bit_offset bit_offset)
Definition integer-with-unit.h:341
offset_int byte_offset_t
Definition common.h:274
concrete_bit_range bits_from_bytes(concrete_byte_range bytes)
Definition concrete-ranges.h:121
integer_with_unit< units::byte > concrete_byte_offset
Definition integer-with-unit.h:331
bit_size_t convert_to_bit_size_t(concrete_bit_size)
bit_offset_t convert_to_bit_offset_t(concrete_bit_offset)
offset_int bit_size_t
Definition common.h:273
integer_with_unit< units::byte > convert_from_byte_size_t(byte_size_t)
integer_with_unit< units::byte > concrete_byte_size
Definition integer-with-unit.h:330
concrete_bit_offset convert_from_bit_offset_t(bit_offset_t)
bool get_type_size(const_tree type, concrete_bit_size *out)
i
Definition poly-int.h:776
Ca const poly_int< N, Cb > & b
Definition poly-int.h:771
Ca & a
Definition poly-int.h:770
Definition ira-emit.cc:158
#define gcc_unreachable()
Definition system.h:799
#define gcc_checking_assert(EXPR)
Definition system.h:779