Line data Source code
1 : /* Unit-safe arbitrary precision integers (e.g. "32 bytes").
2 : Copyright (C) 2019-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 : #include "analyzer/common.h"
22 :
23 : #include "selftest.h"
24 :
25 : #include "diagnostics/markup.h"
26 :
27 : #include "analyzer/integer-with-unit.h"
28 :
29 : #if ENABLE_ANALYZER
30 :
31 : namespace ana {
32 :
33 : template<>
34 : void
35 6309 : integer_with_unit<units::byte>::dump_to_pp (pretty_printer *pp,
36 : units::role role) const
37 : {
38 6309 : switch (role)
39 : {
40 0 : default:
41 0 : gcc_unreachable ();
42 3579 : case units::role::generic:
43 3579 : m_integer.dump_to_pp (pp);
44 3579 : break;
45 16 : case units::role::offset:
46 16 : pp_string (pp, "byte ");
47 16 : m_integer.dump_to_pp (pp);
48 16 : break;
49 2714 : case units::role::size:
50 2714 : if (m_integer == 1)
51 726 : pp_printf (pp, "1 byte");
52 : else
53 : {
54 1988 : m_integer.dump_to_pp (pp);
55 1988 : pp_string (pp, " bytes");
56 : }
57 : break;
58 : }
59 6309 : }
60 :
61 : template<>
62 : void
63 4283 : integer_with_unit<units::bit>::dump_to_pp (pretty_printer *pp,
64 : units::role role) const
65 : {
66 4283 : if (role != units::role::generic)
67 : {
68 : // Attempt to print as bytes
69 2782 : concrete_byte_size bytes;
70 2782 : if (maybe_bytes_from_bits (*this, &bytes))
71 : {
72 2706 : bytes.dump_to_pp (pp, role);
73 2706 : return;
74 : }
75 2706 : }
76 76 : switch (role)
77 : {
78 0 : default:
79 0 : gcc_unreachable ();
80 1501 : case units::role::generic:
81 1501 : m_integer.dump_to_pp (pp);
82 1501 : break;
83 12 : case units::role::offset:
84 12 : pp_string (pp, "bit ");
85 12 : m_integer.dump_to_pp (pp);
86 12 : break;
87 64 : case units::role::size:
88 64 : if (m_integer == 1)
89 24 : pp_printf (pp, "1 bit");
90 : else
91 : {
92 40 : m_integer.dump_to_pp (pp);
93 40 : pp_string (pp, " bits");
94 : }
95 : break;
96 : }
97 : }
98 :
99 : template<>
100 : DEBUG_FUNCTION void
101 0 : integer_with_unit<units::bit>::dump () const
102 : {
103 0 : pretty_printer pp;
104 0 : pp.set_output_stream (stderr);
105 0 : dump_to_pp (&pp, units::role::size);
106 0 : pp_newline (&pp);
107 0 : pp_flush (&pp);
108 0 : }
109 :
110 : template<>
111 : DEBUG_FUNCTION void
112 0 : integer_with_unit<units::byte>::dump () const
113 : {
114 0 : pretty_printer pp;
115 0 : pp.set_output_stream (stderr);
116 0 : dump_to_pp (&pp, units::role::size);
117 0 : pp_newline (&pp);
118 0 : pp_flush (&pp);
119 0 : }
120 :
121 : template<>
122 : std::unique_ptr<json::object>
123 410 : integer_with_unit<units::bit>::to_json () const
124 : {
125 410 : auto obj = std::make_unique<json::object> ();
126 410 : obj->set ("integer", m_integer.to_json ());
127 410 : obj->set_string ("unit", "bits");
128 410 : return obj;
129 : }
130 :
131 : template<>
132 : std::unique_ptr<json::object>
133 137 : integer_with_unit<units::byte>::to_json () const
134 : {
135 137 : auto obj = std::make_unique<json::object> ();
136 137 : obj->set ("integer", m_integer.to_json ());
137 137 : obj->set_string ("unit", "bytes");
138 137 : return obj;
139 : }
140 :
141 : template<typename Unit>
142 : std::unique_ptr<diagnostics::markup::node>
143 16 : integer_with_unit<Unit>::
144 : make_markup_node () const
145 : {
146 16 : return m_integer.make_markup_node ();
147 : }
148 :
149 : template
150 : std::unique_ptr<diagnostics::markup::node>
151 : integer_with_unit<units::bit>::
152 : make_markup_node () const;
153 :
154 : template
155 : std::unique_ptr<diagnostics::markup::node>
156 : integer_with_unit<units::byte>::
157 : make_markup_node () const;
158 :
159 : bool
160 908421 : maybe_bytes_from_bits (integer_with_unit<units::bit> bits,
161 : integer_with_unit<units::byte> *out)
162 : {
163 1816842 : if (bits.floor_mod (integer_with_unit<units::bit> (BITS_PER_UNIT))
164 2725263 : != 0_bits)
165 : return false;
166 1810588 : *out = integer_with_unit<units::byte>
167 1810588 : (bits.get_integer ().floor_div (BITS_PER_UNIT));
168 905294 : return true;
169 : }
170 :
171 : integer_with_unit<units::bit>
172 16758 : convert_from_bit_offset_t (bit_offset_t bits)
173 : {
174 16758 : unsigned HOST_WIDE_INT uhwi = bits.to_uhwi ();
175 16758 : return integer_with_unit<units::bit> (uhwi);
176 : }
177 :
178 : integer_with_unit<units::bit>
179 3960233 : convert_from_bit_size_t (bit_size_t bits)
180 : {
181 3960233 : unsigned HOST_WIDE_INT uhwi = bits.to_uhwi ();
182 3960233 : return integer_with_unit<units::bit> (uhwi);
183 : }
184 :
185 : integer_with_unit<units::byte>
186 0 : convert_from_byte_offset_t (byte_offset_t bytes)
187 : {
188 0 : unsigned HOST_WIDE_INT uhwi = bytes.to_uhwi ();
189 0 : return integer_with_unit<units::byte> (uhwi);
190 : }
191 :
192 : integer_with_unit<units::byte>
193 7133 : convert_from_byte_size_t (byte_size_t bytes)
194 : {
195 7133 : unsigned HOST_WIDE_INT uhwi = bytes.to_uhwi ();
196 7133 : return integer_with_unit<units::byte> (uhwi);
197 : }
198 :
199 : bit_offset_t
200 0 : convert_to_bit_offset_t (integer_with_unit<units::bit> new_val)
201 : {
202 0 : return bit_offset_t (new_val.as_long ());
203 : }
204 :
205 : bit_size_t
206 416 : convert_to_bit_size_t (integer_with_unit<units::bit> new_val)
207 : {
208 416 : return bit_offset_t (new_val.as_unsigned_long ());
209 : }
210 :
211 : byte_offset_t
212 45 : convert_to_byte_offset_t (integer_with_unit<units::byte> new_val)
213 : {
214 45 : gcc_assert (new_val >= 0_bytes);
215 72 : return byte_offset_t (new_val.get_integer ().as_unsigned_long ());
216 : }
217 :
218 : byte_size_t
219 0 : convert_to_byte_size_t (integer_with_unit<units::byte> new_val)
220 : {
221 0 : gcc_assert (new_val >= 0_bytes);
222 0 : return byte_offset_t (new_val.get_integer ().as_unsigned_long ());
223 : }
224 :
225 : #if CHECKING_P
226 :
227 : namespace selftest {
228 :
229 : /* Unit-independent tests. */
230 :
231 : template<typename Unit>
232 : static void
233 8 : test_rule_of_5 ()
234 : {
235 8 : integer_with_unit<Unit> a;
236 8 : ASSERT_EQ (a.get_integer (), 0);
237 :
238 8 : integer_with_unit<Unit> b (42);
239 8 : ASSERT_EQ (b.get_integer (), 42);
240 :
241 8 : integer_with_unit<Unit> c (b);
242 8 : ASSERT_EQ (c.get_integer (), 42);
243 : // Check that writes to b don't affect c after the copy:
244 8 : b = integer_with_unit<Unit> (43);
245 8 : ASSERT_EQ (b.get_integer (), 43);
246 8 : ASSERT_EQ (c.get_integer (), 42);
247 :
248 8 : integer_with_unit<Unit> d (std::move (c));
249 8 : ASSERT_EQ (d.get_integer (), 42);
250 8 : ASSERT_EQ (c.get_integer (), 0);
251 :
252 8 : c = integer_with_unit<Unit> (1066);
253 8 : ASSERT_EQ (c.get_integer (), 1066);
254 :
255 8 : a = std::move (d);
256 8 : ASSERT_EQ (a.get_integer (), 42);
257 8 : }
258 :
259 : /* Test addition, for scalars of the given unit. */
260 :
261 : template<typename Unit>
262 : static void
263 8 : test_addition ()
264 : {
265 8 : ASSERT_EQ (integer_with_unit<Unit> (1), integer_with_unit<Unit> (1));
266 8 : ASSERT_NE (integer_with_unit<Unit> (1), integer_with_unit<Unit> (2));
267 :
268 : /* Addition. */
269 8 : ASSERT_EQ (integer_with_unit<Unit> (4) + integer_with_unit<Unit> (3),
270 : integer_with_unit<Unit> (7));
271 :
272 : /* Subtraction. */
273 8 : ASSERT_EQ (integer_with_unit<Unit> (4) - integer_with_unit<Unit> (3),
274 : integer_with_unit<Unit> (1));
275 :
276 : /* Additive identity. */
277 8 : ASSERT_EQ (integer_with_unit<Unit> (3) + integer_with_unit<Unit> (0),
278 : integer_with_unit<Unit> (3));
279 8 : ASSERT_EQ (integer_with_unit<Unit> (2) - integer_with_unit<Unit> (0),
280 : integer_with_unit<Unit> (2));
281 :
282 : /* Negative values. */
283 8 : ASSERT_EQ (integer_with_unit<Unit> (4) - integer_with_unit<Unit> (5),
284 : integer_with_unit<Unit> (-1));
285 8 : ASSERT_EQ (integer_with_unit<Unit> (-1) - integer_with_unit<Unit> (-3),
286 : integer_with_unit<Unit> (2));
287 8 : }
288 :
289 : /* Test operator++, for scalars of the given unit. */
290 :
291 : template<typename Unit>
292 : static void
293 8 : test_auto_inc ()
294 : {
295 8 : integer_with_unit<Unit> i (0);
296 8 : ASSERT_EQ (i, integer_with_unit<Unit> (0));
297 :
298 8 : ++i;
299 8 : ASSERT_EQ (i, integer_with_unit<Unit> (1));
300 :
301 8 : ++i;
302 8 : ASSERT_EQ (i, integer_with_unit<Unit> (2));
303 8 : }
304 :
305 :
306 : /* Test multiplication, for scalars of the given unit by unitless integers. */
307 :
308 : template<typename Unit>
309 : static void
310 8 : test_scaling ()
311 : {
312 8 : ASSERT_EQ (integer_with_unit<Unit> (5) * 2,
313 : integer_with_unit<Unit> (10));
314 :
315 : /* Identity. */
316 8 : ASSERT_EQ (integer_with_unit<Unit> (7) * 1,
317 : integer_with_unit<Unit> (7));
318 :
319 : /* Zero. */
320 8 : ASSERT_EQ (integer_with_unit<Unit> (4) * 0,
321 : integer_with_unit<Unit> (0));
322 8 : }
323 :
324 : template<typename Unit>
325 : static void
326 8 : test_division ()
327 : {
328 8 : ASSERT_EQ (integer_with_unit<Unit> (0)
329 : .floor_div (integer_with_unit<Unit> (10)),
330 : integer (0));
331 8 : ASSERT_EQ (integer_with_unit<Unit> (24)
332 : .floor_div (integer_with_unit<Unit> (10)),
333 : integer (2));
334 8 : ASSERT_EQ (integer_with_unit<Unit> (3)
335 : .floor_div (integer_with_unit<Unit> (1)),
336 : integer (3));
337 8 : ASSERT_EQ (integer_with_unit<Unit> (-8)
338 : .floor_div (integer_with_unit<Unit> (10)),
339 : integer (-1));
340 8 : }
341 :
342 : template<typename Unit>
343 : static void
344 8 : test_modulus ()
345 : {
346 8 : ASSERT_EQ (integer_with_unit<Unit> (0)
347 : .floor_mod (integer_with_unit<Unit> (10)),
348 : integer_with_unit<Unit> (0));
349 8 : ASSERT_EQ (integer_with_unit<Unit> (24)
350 : .floor_mod (integer_with_unit<Unit> (10)),
351 : integer_with_unit<Unit> (4));
352 8 : ASSERT_EQ (integer_with_unit<Unit> (3)
353 : .floor_mod (integer_with_unit<Unit> (1)),
354 : integer_with_unit<Unit> (0));
355 8 : ASSERT_EQ (integer_with_unit<Unit> (-8)
356 : .floor_mod (integer_with_unit<Unit> (10)),
357 : integer_with_unit<Unit> (2));
358 8 : }
359 :
360 : template<typename Unit>
361 : static void
362 : test_dumping ();
363 :
364 : template<>
365 : void
366 4 : test_dumping<units::bit> ()
367 : {
368 4 : {
369 4 : auto seven_bits = 7_bits;
370 4 : {
371 4 : pretty_printer pp;
372 4 : seven_bits.dump_to_pp (&pp, units::role::offset);
373 4 : ASSERT_STREQ (pp_formatted_text (&pp), "bit 7");
374 4 : }
375 4 : {
376 4 : pretty_printer pp;
377 4 : seven_bits.dump_to_pp (&pp, units::role::size);
378 4 : ASSERT_STREQ (pp_formatted_text (&pp), "7 bits");
379 4 : }
380 4 : }
381 :
382 4 : {
383 4 : auto one_bit = 1_bit;
384 4 : {
385 4 : pretty_printer pp;
386 4 : one_bit.dump_to_pp (&pp, units::role::offset);
387 4 : ASSERT_STREQ (pp_formatted_text (&pp), "bit 1");
388 4 : }
389 4 : {
390 4 : pretty_printer pp;
391 4 : one_bit.dump_to_pp (&pp, units::role::size);
392 4 : ASSERT_STREQ (pp_formatted_text (&pp), "1 bit");
393 4 : }
394 4 : }
395 4 : }
396 :
397 : template<>
398 : void
399 4 : test_dumping<units::byte> ()
400 : {
401 4 : {
402 4 : auto seven_bytes = 7_bytes;
403 4 : {
404 4 : pretty_printer pp;
405 4 : seven_bytes.dump_to_pp (&pp, units::role::offset);
406 4 : ASSERT_STREQ (pp_formatted_text (&pp), "byte 7");
407 4 : }
408 4 : {
409 4 : pretty_printer pp;
410 4 : seven_bytes.dump_to_pp (&pp, units::role::size);
411 4 : ASSERT_STREQ (pp_formatted_text (&pp), "7 bytes");
412 4 : }
413 4 : }
414 :
415 4 : {
416 4 : auto one_byte = 1_byte;
417 4 : {
418 4 : pretty_printer pp;
419 4 : one_byte.dump_to_pp (&pp, units::role::offset);
420 4 : ASSERT_STREQ (pp_formatted_text (&pp), "byte 1");
421 4 : }
422 4 : {
423 4 : pretty_printer pp;
424 4 : one_byte.dump_to_pp (&pp, units::role::size);
425 4 : ASSERT_STREQ (pp_formatted_text (&pp), "1 byte");
426 4 : }
427 4 : }
428 4 : }
429 :
430 : /* Run all per-unit tests, for Unit. */
431 :
432 : template<typename Unit>
433 : static void
434 8 : per_unit_scalar_tests ()
435 : {
436 8 : test_rule_of_5<Unit> ();
437 8 : test_addition<Unit> ();
438 8 : test_auto_inc<Unit> ();
439 8 : test_scaling<Unit> ();
440 8 : test_division<Unit> ();
441 8 : test_modulus<Unit> ();
442 8 : test_dumping<Unit> ();
443 8 : }
444 :
445 : static void
446 4 : test_to_json ()
447 : {
448 4 : ASSERT_JSON_PRINT_EQ (*(32_bits).to_json (), true,
449 : ("{\"integer\": \"32\",\n"
450 : " \"unit\": \"bits\"}"));
451 4 : ASSERT_JSON_PRINT_EQ (*(1024_bytes).to_json (), true,
452 : ("{\"integer\": \"1024\",\n"
453 : " \"unit\": \"bytes\"}"));
454 4 : }
455 :
456 : static void
457 4 : test_comparison_of_compatible_mixed_units ()
458 : {
459 4 : ASSERT_EQ (integer_with_unit<units::bit> (8),
460 : integer_with_unit<units::byte> (1));
461 4 : ASSERT_NE (integer_with_unit<units::bit> (7),
462 : integer_with_unit<units::byte> (1));
463 4 : ASSERT_NE (integer_with_unit<units::bit> (9),
464 : integer_with_unit<units::byte> (1));
465 4 : }
466 :
467 : static void
468 4 : test_addition_of_compatible_mixed_units ()
469 : {
470 4 : ASSERT_EQ ((integer_with_unit<units::bit> (8)
471 : + integer_with_unit<units::byte> (1)),
472 : integer_with_unit<units::bit> (16));
473 :
474 4 : ASSERT_EQ ((integer_with_unit<units::byte> (1)
475 : - integer_with_unit<units::bit> (2)),
476 : integer_with_unit<units::bit> (6));
477 4 : }
478 :
479 : /* Unit-dependent tests. */
480 :
481 : static void
482 4 : test_udls ()
483 : {
484 4 : ASSERT_EQ (8_bits, 1_byte);
485 4 : ASSERT_NE (7_bits, 1_byte);
486 4 : ASSERT_NE (9_bits, 1_byte);
487 4 : ASSERT_EQ (1_byte + 1_bit, 9_bits);
488 4 : ASSERT_EQ (1_byte - 2_bits, 6_bits);
489 4 : ASSERT_EQ ((5_bits * 4) - 2_bytes, 4_bits);
490 4 : }
491 :
492 : /* Consider:
493 : |
494 : | struct coord { uint32_t x; uint32_t y; };
495 : | struct triangle { coord m_vertices[3]; };
496 : |
497 : and computing sizeof (triangle). */
498 :
499 : static void
500 4 : test_sizeof_triangle ()
501 : {
502 : /* In bytes. */
503 4 : {
504 4 : auto sizeof_x = 32_bits;
505 4 : auto sizeof_y = 32_bits;
506 :
507 4 : auto sizeof_coord = sizeof_x + sizeof_y;
508 4 : ASSERT_EQ (sizeof_coord, 64_bits);
509 :
510 4 : auto sizeof_triangle = sizeof_coord * 3;
511 4 : ASSERT_EQ (sizeof_triangle, 192_bits);
512 4 : }
513 :
514 : /* In bits. */
515 4 : {
516 4 : auto sizeof_x = 4_bytes;
517 4 : auto sizeof_y = 4_bytes;
518 :
519 4 : auto sizeof_coord = sizeof_x + sizeof_y;
520 4 : ASSERT_EQ (sizeof_coord, 8_bytes);
521 :
522 4 : auto sizeof_triangle = sizeof_coord * 3;
523 4 : ASSERT_EQ (sizeof_triangle, 24_bytes);
524 4 : }
525 4 : }
526 :
527 : static void
528 4 : test_round_bit_to_lowest_byte ()
529 : {
530 4 : ASSERT_EQ (round_bit_to_lowest_byte (0_bits), 0_bytes);
531 4 : ASSERT_EQ (round_bit_to_lowest_byte (1_bit), 0_bytes);
532 4 : ASSERT_EQ (round_bit_to_lowest_byte (7_bits), 0_bytes);
533 4 : ASSERT_EQ (round_bit_to_lowest_byte (8_bits), 1_byte);
534 4 : ASSERT_EQ (round_bit_to_lowest_byte (15_bits), 1_byte);
535 4 : ASSERT_EQ (round_bit_to_lowest_byte (16_bits), 2_bytes);
536 4 : ASSERT_EQ (round_bit_to_lowest_byte (23_bits), 2_bytes);
537 4 : ASSERT_EQ (round_bit_to_lowest_byte (24_bits), 3_bytes);
538 4 : }
539 :
540 : static void
541 4 : test_bit_within_byte ()
542 : {
543 4 : ASSERT_EQ (bit_within_byte (0_bits), 0_bits);
544 4 : ASSERT_EQ (bit_within_byte (1_bit), 1_bit);
545 4 : ASSERT_EQ (bit_within_byte (7_bits), 7_bits);
546 4 : ASSERT_EQ (bit_within_byte (8_bits), 0_bits);
547 4 : ASSERT_EQ (bit_within_byte (15_bits), 7_bits);
548 4 : ASSERT_EQ (bit_within_byte (16_bits), 0_bits);
549 4 : ASSERT_EQ (bit_within_byte (23_bits), 7_bits);
550 4 : ASSERT_EQ (bit_within_byte (24_bits), 0_bits);
551 4 : }
552 :
553 : /* Run all of the selftests within this file. */
554 :
555 : void
556 4 : analyzer_integer_with_unit_cc_tests ()
557 : {
558 4 : per_unit_scalar_tests<units::bit> ();
559 4 : per_unit_scalar_tests<units::byte> ();
560 :
561 4 : test_to_json ();
562 :
563 4 : test_comparison_of_compatible_mixed_units ();
564 4 : test_addition_of_compatible_mixed_units ();
565 :
566 4 : test_udls ();
567 :
568 4 : test_sizeof_triangle ();
569 :
570 4 : test_round_bit_to_lowest_byte ();
571 4 : test_bit_within_byte ();
572 4 : }
573 :
574 : } // namespace selftest
575 :
576 : #endif /* CHECKING_P */
577 :
578 : } // namespace ana
579 :
580 : #endif /* #if ENABLE_ANALYZER */
|