[gcc/testsuite]
[official-gcc.git] / gcc / wide-int-print.cc
blob36d8ad863f5192767c68700fe03609647bf281fb
1 /* Printing operations with very long integers.
2 Copyright (C) 2012-2017 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
10 later version.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
26 * public printing routines.
29 #define BLOCKS_NEEDED(PREC) \
30 (((PREC) + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT)
32 void
33 print_dec (const wide_int_ref &wi, char *buf, signop sgn)
35 if (sgn == SIGNED)
36 print_decs (wi, buf);
37 else
38 print_decu (wi, buf);
41 void
42 print_dec (const wide_int_ref &wi, FILE *file, signop sgn)
44 if (sgn == SIGNED)
45 print_decs (wi, file);
46 else
47 print_decu (wi, file);
51 /* Try to print the signed self in decimal to BUF if the number fits
52 in a HWI. Other print in hex. */
54 void
55 print_decs (const wide_int_ref &wi, char *buf)
57 if ((wi.get_precision () <= HOST_BITS_PER_WIDE_INT)
58 || (wi.get_len () == 1))
60 if (wi::neg_p (wi))
61 sprintf (buf, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
62 -(unsigned HOST_WIDE_INT) wi.to_shwi ());
63 else
64 sprintf (buf, HOST_WIDE_INT_PRINT_DEC, wi.to_shwi ());
66 else
67 print_hex (wi, buf);
70 /* Try to print the signed self in decimal to FILE if the number fits
71 in a HWI. Other print in hex. */
73 void
74 print_decs (const wide_int_ref &wi, FILE *file)
76 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
77 print_decs (wi, buf);
78 fputs (buf, file);
81 /* Try to print the unsigned self in decimal to BUF if the number fits
82 in a HWI. Other print in hex. */
84 void
85 print_decu (const wide_int_ref &wi, char *buf)
87 if ((wi.get_precision () <= HOST_BITS_PER_WIDE_INT)
88 || (wi.get_len () == 1 && !wi::neg_p (wi)))
89 sprintf (buf, HOST_WIDE_INT_PRINT_UNSIGNED, wi.to_uhwi ());
90 else
91 print_hex (wi, buf);
94 /* Try to print the signed self in decimal to FILE if the number fits
95 in a HWI. Other print in hex. */
97 void
98 print_decu (const wide_int_ref &wi, FILE *file)
100 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
101 print_decu (wi, buf);
102 fputs (buf, file);
105 void
106 print_hex (const wide_int_ref &wi, char *buf)
108 int i = wi.get_len ();
110 if (wi == 0)
111 buf += sprintf (buf, "0x0");
112 else
114 if (wi::neg_p (wi))
116 int j;
117 /* If the number is negative, we may need to pad value with
118 0xFFF... because the leading elements may be missing and
119 we do not print a '-' with hex. */
120 buf += sprintf (buf, "0x");
121 for (j = BLOCKS_NEEDED (wi.get_precision ()); j > i; j--)
122 buf += sprintf (buf, HOST_WIDE_INT_PRINT_PADDED_HEX, HOST_WIDE_INT_M1);
125 else
126 buf += sprintf (buf, "0x" HOST_WIDE_INT_PRINT_HEX_PURE, wi.elt (--i));
128 while (--i >= 0)
129 buf += sprintf (buf, HOST_WIDE_INT_PRINT_PADDED_HEX, wi.elt (i));
133 /* Print one big hex number to FILE. Note that some assemblers may not
134 accept this for large modes. */
135 void
136 print_hex (const wide_int_ref &wi, FILE *file)
138 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
139 print_hex (wi, buf);
140 fputs (buf, file);