]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/wide-int-print.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / wide-int-print.cc
CommitLineData
e913b5cd 1/* Printing operations with very long integers.
fbd26352 2 Copyright (C) 2012-2019 Free Software Foundation, Inc.
e913b5cd 3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.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 the
9Free Software Foundation; either version 3, or (at your option) any
10later version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT
13ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for 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#include "config.h"
22#include "system.h"
23#include "coretypes.h"
e913b5cd 24
25/*
26 * public printing routines.
27 */
28
29#define BLOCKS_NEEDED(PREC) \
30 (((PREC) + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT)
31
ddb1be65 32void
28e557ef 33print_dec (const wide_int_ref &wi, char *buf, signop sgn)
e913b5cd 34{
35 if (sgn == SIGNED)
36 print_decs (wi, buf);
37 else
38 print_decu (wi, buf);
39}
40
ddb1be65 41void
28e557ef 42print_dec (const wide_int_ref &wi, FILE *file, signop sgn)
e913b5cd 43{
44 if (sgn == SIGNED)
45 print_decs (wi, file);
46 else
47 print_decu (wi, file);
48}
49
50
51/* Try to print the signed self in decimal to BUF if the number fits
52 in a HWI. Other print in hex. */
53
ddb1be65 54void
28e557ef 55print_decs (const wide_int_ref &wi, char *buf)
e913b5cd 56{
57 if ((wi.get_precision () <= HOST_BITS_PER_WIDE_INT)
58 || (wi.get_len () == 1))
59 {
796b6678 60 if (wi::neg_p (wi))
d69521d8 61 sprintf (buf, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
62 -(unsigned HOST_WIDE_INT) wi.to_shwi ());
e913b5cd 63 else
64 sprintf (buf, HOST_WIDE_INT_PRINT_DEC, wi.to_shwi ());
65 }
66 else
67 print_hex (wi, buf);
68}
69
70/* Try to print the signed self in decimal to FILE if the number fits
71 in a HWI. Other print in hex. */
72
ddb1be65 73void
28e557ef 74print_decs (const wide_int_ref &wi, FILE *file)
e913b5cd 75{
76 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
77 print_decs (wi, buf);
78 fputs (buf, file);
79}
80
81/* Try to print the unsigned self in decimal to BUF if the number fits
82 in a HWI. Other print in hex. */
83
ddb1be65 84void
28e557ef 85print_decu (const wide_int_ref &wi, char *buf)
e913b5cd 86{
87 if ((wi.get_precision () <= HOST_BITS_PER_WIDE_INT)
796b6678 88 || (wi.get_len () == 1 && !wi::neg_p (wi)))
e913b5cd 89 sprintf (buf, HOST_WIDE_INT_PRINT_UNSIGNED, wi.to_uhwi ());
90 else
91 print_hex (wi, buf);
92}
93
94/* Try to print the signed self in decimal to FILE if the number fits
95 in a HWI. Other print in hex. */
96
ddb1be65 97void
28e557ef 98print_decu (const wide_int_ref &wi, FILE *file)
e913b5cd 99{
100 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
101 print_decu (wi, buf);
102 fputs (buf, file);
103}
104
ddb1be65 105void
bf39e18e 106print_hex (const wide_int_ref &val, char *buf)
e913b5cd 107{
bf39e18e 108 if (val == 0)
e913b5cd 109 buf += sprintf (buf, "0x0");
110 else
111 {
bf39e18e 112 buf += sprintf (buf, "0x");
113 int start = ROUND_DOWN (val.get_precision (), HOST_BITS_PER_WIDE_INT);
114 int width = val.get_precision () - start;
115 bool first_p = true;
116 for (int i = start; i >= 0; i -= HOST_BITS_PER_WIDE_INT)
e913b5cd 117 {
bf39e18e 118 unsigned HOST_WIDE_INT uhwi = wi::extract_uhwi (val, i, width);
119 if (!first_p)
120 buf += sprintf (buf, HOST_WIDE_INT_PRINT_PADDED_HEX, uhwi);
121 else if (uhwi != 0)
122 {
123 buf += sprintf (buf, HOST_WIDE_INT_PRINT_HEX_PURE, uhwi);
124 first_p = false;
125 }
126 width = HOST_BITS_PER_WIDE_INT;
e913b5cd 127 }
e913b5cd 128 }
129}
130
131/* Print one big hex number to FILE. Note that some assemblers may not
132 accept this for large modes. */
ddb1be65 133void
28e557ef 134print_hex (const wide_int_ref &wi, FILE *file)
e913b5cd 135{
136 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
137 print_hex (wi, buf);
138 fputs (buf, file);
139}
140