Line data Source code
1 : /* Printing operations with very long integers.
2 : Copyright (C) 2012-2026 Free Software Foundation, Inc.
3 : Contributed by Kenneth Zadeck <zadeck@naturalbridge.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 the
9 : Free Software Foundation; either version 3, or (at your option) any
10 : later version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT
13 : ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : 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 "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "pretty-print.h"
25 :
26 : /*
27 : * public printing routines.
28 : */
29 :
30 : #define BLOCKS_NEEDED(PREC) \
31 : (((PREC) + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT)
32 :
33 : void
34 228066 : print_dec (const wide_int_ref &wi, char *buf, signop sgn)
35 : {
36 228066 : if (sgn == SIGNED)
37 25495 : print_decs (wi, buf);
38 : else
39 202571 : print_decu (wi, buf);
40 228066 : }
41 :
42 : void
43 3612 : print_dec (const wide_int_ref &wi, FILE *file, signop sgn)
44 : {
45 3612 : if (sgn == SIGNED)
46 3253 : print_decs (wi, file);
47 : else
48 359 : print_decu (wi, file);
49 3612 : }
50 :
51 :
52 : /* Try to print the signed self in decimal to BUF. */
53 :
54 : void
55 29635 : print_decs (const wide_int_ref &wi, char *buf)
56 : {
57 29635 : if (wi.get_precision () <= HOST_BITS_PER_WIDE_INT || wi.get_len () == 1)
58 : {
59 29602 : if (wi::neg_p (wi))
60 3904 : sprintf (buf, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
61 3904 : -(unsigned HOST_WIDE_INT) wi.to_shwi ());
62 : else
63 25698 : sprintf (buf, HOST_WIDE_INT_PRINT_DEC, wi.to_shwi ());
64 : }
65 33 : else if (wi::neg_p (wi))
66 : {
67 17 : widest2_int w = widest2_int::from (wi, SIGNED);
68 17 : *buf = '-';
69 17 : print_decu (-w, buf + 1);
70 17 : }
71 : else
72 16 : print_decu (wi, buf);
73 29635 : }
74 :
75 : /* Try to print the signed self in decimal to FILE. */
76 :
77 : void
78 4140 : print_decs (const wide_int_ref &wi, FILE *file)
79 : {
80 4140 : char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p = buf;
81 4140 : unsigned len;
82 4140 : if (print_decs_buf_size (wi, &len))
83 0 : p = XALLOCAVEC (char, len);
84 4140 : print_decs (wi, p);
85 4140 : fputs (p, file);
86 4140 : }
87 :
88 : /* Try to print the unsigned self in decimal to BUF. */
89 :
90 : void
91 333299 : print_decu (const wide_int_ref &wi, char *buf)
92 : {
93 333299 : if ((wi.get_precision () <= HOST_BITS_PER_WIDE_INT)
94 333299 : || (wi.get_len () == 1 && !wi::neg_p (wi)))
95 331158 : sprintf (buf, HOST_WIDE_INT_PRINT_UNSIGNED, wi.to_uhwi ());
96 : else
97 : {
98 2141 : widest2_int w = widest2_int::from (wi, UNSIGNED), r;
99 2141 : widest2_int ten19 = HOST_WIDE_INT_UC (10000000000000000000);
100 2141 : char buf2[20], next1[19], next2[19];
101 2141 : size_t l, c = 0, i;
102 : /* In order to avoid dividing this twice, print the 19 decimal
103 : digit chunks in reverse order into buffer and then reorder
104 : them in-place. */
105 4887 : while (wi::geu_p (w, ten19))
106 : {
107 2746 : w = wi::divmod_trunc (w, ten19, UNSIGNED, &r);
108 2746 : sprintf (buf + c * 19, "%019" PRIu64, r.to_uhwi ());
109 2746 : ++c;
110 : }
111 2141 : l = sprintf (buf2, HOST_WIDE_INT_PRINT_UNSIGNED, w.to_uhwi ());
112 2141 : gcc_assert (l < sizeof (buf2));
113 2141 : buf[c * 19 + l] = '\0';
114 2141 : memcpy (next1, buf, 19);
115 2141 : memcpy (buf, buf2, l);
116 2754 : for (i = 0; i < c / 2; ++i)
117 : {
118 613 : memcpy (next2, buf + (c - i - 1) * 19, 19);
119 613 : memcpy (buf + l + (c - i - 1) * 19, next1, 19);
120 613 : memcpy (next1, buf + (i + 1) * 19, 19);
121 613 : memcpy (buf + l + i * 19, next2, 19);
122 : }
123 2141 : if (c & 1)
124 1520 : memcpy (buf + l + i * 19, next1, 19);
125 2141 : }
126 333299 : }
127 :
128 : /* Try to print the signed self in decimal to FILE. */
129 :
130 : void
131 130671 : print_decu (const wide_int_ref &wi, FILE *file)
132 : {
133 130671 : char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p = buf;
134 130671 : unsigned len;
135 130671 : if (print_decu_buf_size (wi, &len))
136 0 : p = XALLOCAVEC (char, len);
137 130671 : print_decu (wi, p);
138 130671 : fputs (p, file);
139 130671 : }
140 :
141 : void
142 237218 : print_hex (const wide_int_ref &val, char *buf)
143 : {
144 237218 : if (val == 0)
145 63578 : buf += sprintf (buf, "0x0");
146 : else
147 : {
148 173640 : buf += sprintf (buf, "0x");
149 173640 : int start = ROUND_DOWN (val.get_precision (), HOST_BITS_PER_WIDE_INT);
150 173640 : int width = val.get_precision () - start;
151 173640 : bool first_p = true;
152 2351887 : for (int i = start; i >= 0; i -= HOST_BITS_PER_WIDE_INT)
153 : {
154 2178247 : unsigned HOST_WIDE_INT uhwi = wi::extract_uhwi (val, i, width);
155 2178247 : if (!first_p)
156 1061 : buf += sprintf (buf, HOST_WIDE_INT_PRINT_PADDED_HEX, uhwi);
157 2177186 : else if (uhwi != 0)
158 : {
159 173640 : buf += sprintf (buf, HOST_WIDE_INT_PRINT_HEX_PURE, uhwi);
160 173640 : first_p = false;
161 : }
162 2178247 : width = HOST_BITS_PER_WIDE_INT;
163 : }
164 : }
165 237218 : }
166 :
167 : /* Print one big hex number to FILE. Note that some assemblers may not
168 : accept this for large modes. */
169 : void
170 219197 : print_hex (const wide_int_ref &wi, FILE *file)
171 : {
172 219197 : char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p = buf;
173 219197 : unsigned len;
174 219197 : if (print_hex_buf_size (wi, &len))
175 0 : p = XALLOCAVEC (char, len);
176 219197 : print_hex (wi, p);
177 219197 : fputs (p, file);
178 219197 : }
179 :
180 : /* Print larger precision wide_int. Not defined as inline in a header
181 : together with pp_wide_int because XALLOCAVEC will make it uninlinable. */
182 :
183 : void
184 0 : pp_wide_int_large (pretty_printer *pp, const wide_int_ref &w, signop sgn)
185 : {
186 0 : unsigned int len;
187 0 : print_dec_buf_size (w, sgn, &len);
188 0 : char *buf = XALLOCAVEC (char, len);
189 0 : print_dec (w, buf, sgn);
190 0 : pp_string (pp, buf);
191 0 : }
|