1/* Printing operations with very long integers. 2 Copyright (C) 2012-2015 Free Software Foundation, Inc. 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" 24#include "tm.h" 25#include "hwint.h" 26#include "wide-int.h" 27#include "wide-int-print.h" 28 29/* 30 * public printing routines. 31 */ 32 33#define BLOCKS_NEEDED(PREC) \ 34 (((PREC) + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT) 35 36void 37print_dec (const wide_int_ref &wi, char *buf, signop sgn) 38{ 39 if (sgn == SIGNED) 40 print_decs (wi, buf); 41 else 42 print_decu (wi, buf); 43} 44 45void 46print_dec (const wide_int_ref &wi, FILE *file, signop sgn) 47{ 48 if (sgn == SIGNED) 49 print_decs (wi, file); 50 else 51 print_decu (wi, file); 52} 53 54 55/* Try to print the signed self in decimal to BUF if the number fits 56 in a HWI. Other print in hex. */ 57 58void 59print_decs (const wide_int_ref &wi, char *buf) 60{ 61 if ((wi.get_precision () <= HOST_BITS_PER_WIDE_INT) 62 || (wi.get_len () == 1)) 63 { 64 if (wi::neg_p (wi)) 65 sprintf (buf, "-" HOST_WIDE_INT_PRINT_UNSIGNED, 66 -(unsigned HOST_WIDE_INT) wi.to_shwi ()); 67 else 68 sprintf (buf, HOST_WIDE_INT_PRINT_DEC, wi.to_shwi ()); 69 } 70 else 71 print_hex (wi, buf); 72} 73 74/* Try to print the signed self in decimal to FILE if the number fits 75 in a HWI. Other print in hex. */ 76 77void 78print_decs (const wide_int_ref &wi, FILE *file) 79{ 80 char buf[WIDE_INT_PRINT_BUFFER_SIZE]; 81 print_decs (wi, buf); 82 fputs (buf, file); 83} 84 85/* Try to print the unsigned self in decimal to BUF if the number fits 86 in a HWI. Other print in hex. */ 87 88void 89print_decu (const wide_int_ref &wi, char *buf) 90{ 91 if ((wi.get_precision () <= HOST_BITS_PER_WIDE_INT) 92 || (wi.get_len () == 1 && !wi::neg_p (wi))) 93 sprintf (buf, HOST_WIDE_INT_PRINT_UNSIGNED, wi.to_uhwi ()); 94 else 95 print_hex (wi, buf); 96} 97 98/* Try to print the signed self in decimal to FILE if the number fits 99 in a HWI. Other print in hex. */ 100 101void 102print_decu (const wide_int_ref &wi, FILE *file) 103{ 104 char buf[WIDE_INT_PRINT_BUFFER_SIZE]; 105 print_decu (wi, buf); 106 fputs (buf, file); 107} 108 109void 110print_hex (const wide_int_ref &wi, char *buf) 111{ 112 int i = wi.get_len (); 113 114 if (wi == 0) 115 buf += sprintf (buf, "0x0"); 116 else 117 { 118 if (wi::neg_p (wi)) 119 { 120 int j; 121 /* If the number is negative, we may need to pad value with 122 0xFFF... because the leading elements may be missing and 123 we do not print a '-' with hex. */ 124 buf += sprintf (buf, "0x"); 125 for (j = BLOCKS_NEEDED (wi.get_precision ()); j > i; j--) 126 buf += sprintf (buf, HOST_WIDE_INT_PRINT_PADDED_HEX, (HOST_WIDE_INT) -1); 127 128 } 129 else 130 buf += sprintf (buf, "0x"HOST_WIDE_INT_PRINT_HEX_PURE, wi.elt (--i)); 131 132 while (--i >= 0) 133 buf += sprintf (buf, HOST_WIDE_INT_PRINT_PADDED_HEX, wi.elt (i)); 134 } 135} 136 137/* Print one big hex number to FILE. Note that some assemblers may not 138 accept this for large modes. */ 139void 140print_hex (const wide_int_ref &wi, FILE *file) 141{ 142 char buf[WIDE_INT_PRINT_BUFFER_SIZE]; 143 print_hex (wi, buf); 144 fputs (buf, file); 145} 146 147