LCOV - code coverage report
Current view: top level - gcc - wide-int-print.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.8 % 95 92
Test Date: 2024-04-20 14:03:02 Functions: 100.0 % 9 9
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Printing operations with very long integers.
       2                 :             :    Copyright (C) 2012-2024 Free Software Foundation, Inc.
       3                 :             :    Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
       4                 :             : 
       5                 :             : This file is part of GCC.
       6                 :             : 
       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.
      11                 :             : 
      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.
      16                 :             : 
      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/>.  */
      20                 :             : 
      21                 :             : #include "config.h"
      22                 :             : #include "system.h"
      23                 :             : #include "coretypes.h"
      24                 :             : #include "pretty-print.h"
      25                 :             : 
      26                 :             : /*
      27                 :             :  * public printing routines.
      28                 :             :  */
      29                 :             : 
      30                 :             : #define BLOCKS_NEEDED(PREC) \
      31                 :             :   (((PREC) + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT)
      32                 :             : 
      33                 :             : void
      34                 :      267949 : print_dec (const wide_int_ref &wi, char *buf, signop sgn)
      35                 :             : {
      36                 :      267949 :   if (sgn == SIGNED)
      37                 :       14279 :     print_decs (wi, buf);
      38                 :             :   else
      39                 :      253670 :     print_decu (wi, buf);
      40                 :      267949 : }
      41                 :             : 
      42                 :             : void
      43                 :        3823 : print_dec (const wide_int_ref &wi, FILE *file, signop sgn)
      44                 :             : {
      45                 :        3823 :   if (sgn == SIGNED)
      46                 :        3466 :     print_decs (wi, file);
      47                 :             :   else
      48                 :         357 :     print_decu (wi, file);
      49                 :        3823 : }
      50                 :             : 
      51                 :             : 
      52                 :             : /* Try to print the signed self in decimal to BUF.  */
      53                 :             : 
      54                 :             : void
      55                 :       18266 : print_decs (const wide_int_ref &wi, char *buf)
      56                 :             : {
      57                 :       18266 :   if (wi.get_precision () <= HOST_BITS_PER_WIDE_INT || wi.get_len () == 1)
      58                 :             :     {
      59                 :       18242 :       if (wi::neg_p (wi))
      60                 :        2843 :         sprintf (buf, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
      61                 :        2843 :                  -(unsigned HOST_WIDE_INT) wi.to_shwi ());
      62                 :             :       else
      63                 :       15399 :         sprintf (buf, HOST_WIDE_INT_PRINT_DEC, wi.to_shwi ());
      64                 :             :     }
      65                 :          24 :   else if (wi::neg_p (wi))
      66                 :             :     {
      67                 :          12 :       widest2_int w = widest2_int::from (wi, SIGNED);
      68                 :          12 :       *buf = '-';
      69                 :          12 :       print_decu (-w, buf + 1);
      70                 :          12 :     }
      71                 :             :   else
      72                 :          12 :     print_decu (wi, buf);
      73                 :       18266 : }
      74                 :             : 
      75                 :             : /* Try to print the signed self in decimal to FILE.  */
      76                 :             : 
      77                 :             : void
      78                 :        3987 : print_decs (const wide_int_ref &wi, FILE *file)
      79                 :             : {
      80                 :        3987 :   char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p = buf;
      81                 :        3987 :   unsigned len;
      82                 :        3987 :   if (print_decs_buf_size (wi, &len))
      83                 :           0 :     p = XALLOCAVEC (char, len);
      84                 :        3987 :   print_decs (wi, p);
      85                 :        3987 :   fputs (p, file);
      86                 :        3987 : }
      87                 :             : 
      88                 :             : /* Try to print the unsigned self in decimal to BUF.  */
      89                 :             : 
      90                 :             : void
      91                 :      349926 : print_decu (const wide_int_ref &wi, char *buf)
      92                 :             : {
      93                 :      349926 :   if ((wi.get_precision () <= HOST_BITS_PER_WIDE_INT)
      94                 :      349926 :       || (wi.get_len () == 1 && !wi::neg_p (wi)))
      95                 :      348268 :     sprintf (buf, HOST_WIDE_INT_PRINT_UNSIGNED, wi.to_uhwi ());
      96                 :             :   else
      97                 :             :     {
      98                 :        1658 :       widest2_int w = widest2_int::from (wi, UNSIGNED), r;
      99                 :        1658 :       widest2_int ten19 = HOST_WIDE_INT_UC (10000000000000000000);
     100                 :        1658 :       char buf2[20], next1[19], next2[19];
     101                 :        1658 :       size_t l, c = 0, i;
     102                 :             :       /* In order to avoid dividing this twice, print the 19 decimal
     103                 :             :          digit chunks in reverse order into buffer and then reorder
     104                 :             :          them in-place.  */
     105                 :        3982 :       while (wi::gtu_p (w, ten19))
     106                 :             :         {
     107                 :        2324 :           w = wi::divmod_trunc (w, ten19, UNSIGNED, &r);
     108                 :        2324 :           sprintf (buf + c * 19, "%019" PRIu64, r.to_uhwi ());
     109                 :        2324 :           ++c;
     110                 :             :         }
     111                 :        1658 :       l = sprintf (buf2, HOST_WIDE_INT_PRINT_UNSIGNED, w.to_uhwi ());
     112                 :        1658 :       buf[c * 19 + l] = '\0';
     113                 :        1658 :       memcpy (next1, buf, 19);
     114                 :        1658 :       memcpy (buf, buf2, l);
     115                 :        2329 :       for (i = 0; i < c / 2; ++i)
     116                 :             :         {
     117                 :         671 :           memcpy (next2, buf + (c - i - 1) * 19, 19);
     118                 :         671 :           memcpy (buf + l + (c - i - 1) * 19, next1, 19);
     119                 :         671 :           memcpy (next1, buf + (i + 1) * 19, 19);
     120                 :         671 :           memcpy (buf + l + i * 19, next2, 19);
     121                 :             :         }
     122                 :        1658 :       if (c & 1)
     123                 :         982 :         memcpy (buf + l + i * 19, next1, 19);
     124                 :        1658 :     }
     125                 :      349926 : }
     126                 :             : 
     127                 :             : /* Try to print the signed self in decimal to FILE.  */
     128                 :             : 
     129                 :             : void
     130                 :       96206 : print_decu (const wide_int_ref &wi, FILE *file)
     131                 :             : {
     132                 :       96206 :   char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p = buf;
     133                 :       96206 :   unsigned len;
     134                 :       96206 :   if (print_decu_buf_size (wi, &len))
     135                 :           0 :     p = XALLOCAVEC (char, len);
     136                 :       96206 :   print_decu (wi, p);
     137                 :       96206 :   fputs (p, file);
     138                 :       96206 : }
     139                 :             : 
     140                 :             : void
     141                 :      180440 : print_hex (const wide_int_ref &val, char *buf)
     142                 :             : {
     143                 :      180440 :   if (val == 0)
     144                 :       47744 :     buf += sprintf (buf, "0x0");
     145                 :             :   else
     146                 :             :     {
     147                 :      132696 :       buf += sprintf (buf, "0x");
     148                 :      132696 :       int start = ROUND_DOWN (val.get_precision (), HOST_BITS_PER_WIDE_INT);
     149                 :      132696 :       int width = val.get_precision () - start;
     150                 :      132696 :       bool first_p = true;
     151                 :     1617214 :       for (int i = start; i >= 0; i -= HOST_BITS_PER_WIDE_INT)
     152                 :             :         {
     153                 :     1484518 :           unsigned HOST_WIDE_INT uhwi = wi::extract_uhwi (val, i, width);
     154                 :     1484518 :           if (!first_p)
     155                 :         619 :             buf += sprintf (buf, HOST_WIDE_INT_PRINT_PADDED_HEX, uhwi);
     156                 :     1483899 :           else if (uhwi != 0)
     157                 :             :             {
     158                 :      132696 :               buf += sprintf (buf, HOST_WIDE_INT_PRINT_HEX_PURE, uhwi);
     159                 :      132696 :               first_p = false;
     160                 :             :             }
     161                 :     1484518 :           width = HOST_BITS_PER_WIDE_INT;
     162                 :             :         }
     163                 :             :     }
     164                 :      180440 : }
     165                 :             : 
     166                 :             : /* Print one big hex number to FILE.  Note that some assemblers may not
     167                 :             :    accept this for large modes.  */
     168                 :             : void
     169                 :      174954 : print_hex (const wide_int_ref &wi, FILE *file)
     170                 :             : {
     171                 :      174954 :   char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p = buf;
     172                 :      174954 :   unsigned len;
     173                 :      174954 :   if (print_hex_buf_size (wi, &len))
     174                 :           0 :     p = XALLOCAVEC (char, len);
     175                 :      174954 :   print_hex (wi, p);
     176                 :      174954 :   fputs (p, file);
     177                 :      174954 : }
     178                 :             : 
     179                 :             : /* Print larger precision wide_int.  Not defined as inline in a header
     180                 :             :    together with pp_wide_int because XALLOCAVEC will make it uninlinable.  */
     181                 :             : 
     182                 :             : void
     183                 :          20 : pp_wide_int_large (pretty_printer *pp, const wide_int_ref &w, signop sgn)
     184                 :             : {
     185                 :          20 :   unsigned int len;
     186                 :          20 :   print_dec_buf_size (w, sgn, &len);
     187                 :          20 :   char *buf = XALLOCAVEC (char, len);
     188                 :          20 :   print_dec (w, buf, sgn);
     189                 :          20 :   pp_string (pp, buf);
     190                 :          20 : }
        

Generated by: LCOV version 2.1-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.