1 /* Printing operations with very long integers.
2 Copyright (C) 2012-2023 Free Software Foundation, Inc.
3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 This file is part of GCC.
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
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
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/>. */
23 #include "coretypes.h"
24 #include "pretty-print.h"
27 * public printing routines.
30 #define BLOCKS_NEEDED(PREC) \
31 (((PREC) + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT)
34 print_dec (const wide_int_ref
&wi
, char *buf
, signop sgn
)
43 print_dec (const wide_int_ref
&wi
, FILE *file
, signop sgn
)
46 print_decs (wi
, file
);
48 print_decu (wi
, file
);
52 /* Try to print the signed self in decimal to BUF. */
55 print_decs (const wide_int_ref
&wi
, char *buf
)
57 if (wi
.get_precision () <= HOST_BITS_PER_WIDE_INT
|| wi
.get_len () == 1)
60 sprintf (buf
, "-" HOST_WIDE_INT_PRINT_UNSIGNED
,
61 -(unsigned HOST_WIDE_INT
) wi
.to_shwi ());
63 sprintf (buf
, HOST_WIDE_INT_PRINT_DEC
, wi
.to_shwi ());
65 else if (wi::neg_p (wi
))
67 widest2_int w
= widest2_int::from (wi
, SIGNED
);
69 print_decu (-w
, buf
+ 1);
75 /* Try to print the signed self in decimal to FILE. */
78 print_decs (const wide_int_ref
&wi
, FILE *file
)
80 char buf
[WIDE_INT_PRINT_BUFFER_SIZE
], *p
= buf
;
82 if (print_decs_buf_size (wi
, &len
))
83 p
= XALLOCAVEC (char, len
);
88 /* Try to print the unsigned self in decimal to BUF. */
91 print_decu (const wide_int_ref
&wi
, char *buf
)
93 if ((wi
.get_precision () <= HOST_BITS_PER_WIDE_INT
)
94 || (wi
.get_len () == 1 && !wi::neg_p (wi
)))
95 sprintf (buf
, HOST_WIDE_INT_PRINT_UNSIGNED
, wi
.to_uhwi ());
98 widest2_int w
= widest2_int::from (wi
, UNSIGNED
), r
;
99 widest2_int ten19
= HOST_WIDE_INT_UC (10000000000000000000);
100 char buf2
[20], next1
[19], next2
[19];
102 /* In order to avoid dividing this twice, print the 19 decimal
103 digit chunks in reverse order into buffer and then reorder
105 while (wi::gtu_p (w
, ten19
))
107 w
= wi::divmod_trunc (w
, ten19
, UNSIGNED
, &r
);
108 sprintf (buf
+ c
* 19, "%019" PRIu64
, r
.to_uhwi ());
111 l
= sprintf (buf2
, HOST_WIDE_INT_PRINT_UNSIGNED
, w
.to_uhwi ());
112 buf
[c
* 19 + l
] = '\0';
113 memcpy (next1
, buf
, 19);
114 memcpy (buf
, buf2
, l
);
115 for (i
= 0; i
< c
/ 2; ++i
)
117 memcpy (next2
, buf
+ (c
- i
- 1) * 19, 19);
118 memcpy (buf
+ l
+ (c
- i
- 1) * 19, next1
, 19);
119 memcpy (next1
, buf
+ (i
+ 1) * 19, 19);
120 memcpy (buf
+ l
+ i
* 19, next2
, 19);
123 memcpy (buf
+ l
+ i
* 19, next1
, 19);
127 /* Try to print the signed self in decimal to FILE. */
130 print_decu (const wide_int_ref
&wi
, FILE *file
)
132 char buf
[WIDE_INT_PRINT_BUFFER_SIZE
], *p
= buf
;
134 if (print_decu_buf_size (wi
, &len
))
135 p
= XALLOCAVEC (char, len
);
141 print_hex (const wide_int_ref
&val
, char *buf
)
144 buf
+= sprintf (buf
, "0x0");
147 buf
+= sprintf (buf
, "0x");
148 int start
= ROUND_DOWN (val
.get_precision (), HOST_BITS_PER_WIDE_INT
);
149 int width
= val
.get_precision () - start
;
151 for (int i
= start
; i
>= 0; i
-= HOST_BITS_PER_WIDE_INT
)
153 unsigned HOST_WIDE_INT uhwi
= wi::extract_uhwi (val
, i
, width
);
155 buf
+= sprintf (buf
, HOST_WIDE_INT_PRINT_PADDED_HEX
, uhwi
);
158 buf
+= sprintf (buf
, HOST_WIDE_INT_PRINT_HEX_PURE
, uhwi
);
161 width
= HOST_BITS_PER_WIDE_INT
;
166 /* Print one big hex number to FILE. Note that some assemblers may not
167 accept this for large modes. */
169 print_hex (const wide_int_ref
&wi
, FILE *file
)
171 char buf
[WIDE_INT_PRINT_BUFFER_SIZE
], *p
= buf
;
173 if (print_hex_buf_size (wi
, &len
))
174 p
= XALLOCAVEC (char, len
);
179 /* Print larger precision wide_int. Not defined as inline in a header
180 together with pp_wide_int because XALLOCAVEC will make it uninlinable. */
183 pp_wide_int_large (pretty_printer
*pp
, const wide_int_ref
&w
, signop sgn
)
186 print_dec_buf_size (w
, sgn
, &len
);
187 char *buf
= XALLOCAVEC (char, len
);
188 print_dec (w
, buf
, sgn
);