From: Richard Sandiford Date: Thu, 26 Oct 2017 16:09:17 +0000 (+0000) Subject: Stop print_hex from printing bits above the precision X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=7984457f8295811880c37e7861aa7c0454ce9845;p=gcc.git Stop print_hex from printing bits above the precision 2017-10-26 Richard Sandiford gcc/ * wide-int-print.cc (print_hex): Loop based on extract_uhwi. Don't print any bits outside the precision of the value. * wide-int.cc (test_printing): Add some new tests. From-SVN: r254109 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 95d9e39a43e..9cf528c4335 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2017-10-26 Richard Sandiford + + * wide-int-print.cc (print_hex): Loop based on extract_uhwi. + Don't print any bits outside the precision of the value. + * wide-int.cc (test_printing): Add some new tests. + 2017-10-26 Rainer Orth * configure.ac (gcc_cv_as_ix86_xbrace_comment): Check if assembler diff --git a/gcc/wide-int-print.cc b/gcc/wide-int-print.cc index 36d8ad863f5..8874e819685 100644 --- a/gcc/wide-int-print.cc +++ b/gcc/wide-int-print.cc @@ -103,30 +103,28 @@ print_decu (const wide_int_ref &wi, FILE *file) } void -print_hex (const wide_int_ref &wi, char *buf) +print_hex (const wide_int_ref &val, char *buf) { - int i = wi.get_len (); - - if (wi == 0) + if (val == 0) buf += sprintf (buf, "0x0"); else { - if (wi::neg_p (wi)) + buf += sprintf (buf, "0x"); + int start = ROUND_DOWN (val.get_precision (), HOST_BITS_PER_WIDE_INT); + int width = val.get_precision () - start; + bool first_p = true; + for (int i = start; i >= 0; i -= HOST_BITS_PER_WIDE_INT) { - int j; - /* If the number is negative, we may need to pad value with - 0xFFF... because the leading elements may be missing and - we do not print a '-' with hex. */ - buf += sprintf (buf, "0x"); - for (j = BLOCKS_NEEDED (wi.get_precision ()); j > i; j--) - buf += sprintf (buf, HOST_WIDE_INT_PRINT_PADDED_HEX, HOST_WIDE_INT_M1); - + unsigned HOST_WIDE_INT uhwi = wi::extract_uhwi (val, i, width); + if (!first_p) + buf += sprintf (buf, HOST_WIDE_INT_PRINT_PADDED_HEX, uhwi); + else if (uhwi != 0) + { + buf += sprintf (buf, HOST_WIDE_INT_PRINT_HEX_PURE, uhwi); + first_p = false; + } + width = HOST_BITS_PER_WIDE_INT; } - else - buf += sprintf (buf, "0x" HOST_WIDE_INT_PRINT_HEX_PURE, wi.elt (--i)); - - while (--i >= 0) - buf += sprintf (buf, HOST_WIDE_INT_PRINT_PADDED_HEX, wi.elt (i)); } } diff --git a/gcc/wide-int.cc b/gcc/wide-int.cc index 1a1a68c1943..ba0fd25b093 100644 --- a/gcc/wide-int.cc +++ b/gcc/wide-int.cc @@ -2253,6 +2253,17 @@ test_printing () VALUE_TYPE a = from_int (42); assert_deceq ("42", a, SIGNED); assert_hexeq ("0x2a", a); + assert_hexeq ("0x1fffffffffffffffff", wi::shwi (-1, 69)); + assert_hexeq ("0xffffffffffffffff", wi::mask (64, false, 69)); + assert_hexeq ("0xffffffffffffffff", wi::mask (64, false)); + if (WIDE_INT_MAX_PRECISION > 128) + { + assert_hexeq ("0x20000000000000000fffffffffffffffe", + wi::lshift (1, 129) + wi::lshift (1, 64) - 2); + assert_hexeq ("0x200000000000004000123456789abcdef", + wi::lshift (1, 129) + wi::lshift (1, 74) + + wi::lshift (0x1234567, 32) + 0x89abcdef); + } } /* Verify that various operations work correctly for VALUE_TYPE,