wide-int-print.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 3, or (at your option) any
  8. later version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "hwint.h"
  21. #include "wide-int.h"
  22. #include "wide-int-print.h"
  23. /*
  24. * public printing routines.
  25. */
  26. #define BLOCKS_NEEDED(PREC) \
  27. (((PREC) + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT)
  28. void
  29. print_dec (const wide_int_ref &wi, char *buf, signop sgn)
  30. {
  31. if (sgn == SIGNED)
  32. print_decs (wi, buf);
  33. else
  34. print_decu (wi, buf);
  35. }
  36. void
  37. print_dec (const wide_int_ref &wi, FILE *file, signop sgn)
  38. {
  39. if (sgn == SIGNED)
  40. print_decs (wi, file);
  41. else
  42. print_decu (wi, file);
  43. }
  44. /* Try to print the signed self in decimal to BUF if the number fits
  45. in a HWI. Other print in hex. */
  46. void
  47. print_decs (const wide_int_ref &wi, char *buf)
  48. {
  49. if ((wi.get_precision () <= HOST_BITS_PER_WIDE_INT)
  50. || (wi.get_len () == 1))
  51. {
  52. if (wi::neg_p (wi))
  53. sprintf (buf, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
  54. -(unsigned HOST_WIDE_INT) wi.to_shwi ());
  55. else
  56. sprintf (buf, HOST_WIDE_INT_PRINT_DEC, wi.to_shwi ());
  57. }
  58. else
  59. print_hex (wi, buf);
  60. }
  61. /* Try to print the signed self in decimal to FILE if the number fits
  62. in a HWI. Other print in hex. */
  63. void
  64. print_decs (const wide_int_ref &wi, FILE *file)
  65. {
  66. char buf[WIDE_INT_PRINT_BUFFER_SIZE];
  67. print_decs (wi, buf);
  68. fputs (buf, file);
  69. }
  70. /* Try to print the unsigned self in decimal to BUF if the number fits
  71. in a HWI. Other print in hex. */
  72. void
  73. print_decu (const wide_int_ref &wi, char *buf)
  74. {
  75. if ((wi.get_precision () <= HOST_BITS_PER_WIDE_INT)
  76. || (wi.get_len () == 1 && !wi::neg_p (wi)))
  77. sprintf (buf, HOST_WIDE_INT_PRINT_UNSIGNED, wi.to_uhwi ());
  78. else
  79. print_hex (wi, buf);
  80. }
  81. /* Try to print the signed self in decimal to FILE if the number fits
  82. in a HWI. Other print in hex. */
  83. void
  84. print_decu (const wide_int_ref &wi, FILE *file)
  85. {
  86. char buf[WIDE_INT_PRINT_BUFFER_SIZE];
  87. print_decu (wi, buf);
  88. fputs (buf, file);
  89. }
  90. void
  91. print_hex (const wide_int_ref &wi, char *buf)
  92. {
  93. int i = wi.get_len ();
  94. if (wi == 0)
  95. buf += sprintf (buf, "0x0");
  96. else
  97. {
  98. if (wi::neg_p (wi))
  99. {
  100. int j;
  101. /* If the number is negative, we may need to pad value with
  102. 0xFFF... because the leading elements may be missing and
  103. we do not print a '-' with hex. */
  104. buf += sprintf (buf, "0x");
  105. for (j = BLOCKS_NEEDED (wi.get_precision ()); j > i; j--)
  106. buf += sprintf (buf, HOST_WIDE_INT_PRINT_PADDED_HEX, (HOST_WIDE_INT) -1);
  107. }
  108. else
  109. buf += sprintf (buf, "0x"HOST_WIDE_INT_PRINT_HEX_PURE, wi.elt (--i));
  110. while (--i >= 0)
  111. buf += sprintf (buf, HOST_WIDE_INT_PRINT_PADDED_HEX, wi.elt (i));
  112. }
  113. }
  114. /* Print one big hex number to FILE. Note that some assemblers may not
  115. accept this for large modes. */
  116. void
  117. print_hex (const wide_int_ref &wi, FILE *file)
  118. {
  119. char buf[WIDE_INT_PRINT_BUFFER_SIZE];
  120. print_hex (wi, buf);
  121. fputs (buf, file);
  122. }