LCOV - code coverage report
Current view: top level - gcc - wide-int.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.2 % 1293 1218
Test Date: 2026-12-05 16:10:02 Functions: 90.2 % 82 74
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Operations with very long integers.
       2              :    Copyright (C) 2012-2026 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 "tm.h"
      25              : #include "tree.h"
      26              : #include "selftest.h"
      27              : 
      28              : 
      29              : #define HOST_BITS_PER_HALF_WIDE_INT 32
      30              : #if HOST_BITS_PER_HALF_WIDE_INT == HOST_BITS_PER_LONG
      31              : # define HOST_HALF_WIDE_INT long
      32              : #elif HOST_BITS_PER_HALF_WIDE_INT == HOST_BITS_PER_INT
      33              : # define HOST_HALF_WIDE_INT int
      34              : #else
      35              : #error Please add support for HOST_HALF_WIDE_INT
      36              : #endif
      37              : 
      38              : #define W_TYPE_SIZE HOST_BITS_PER_WIDE_INT
      39              : #if GCC_VERSION >= 3000 && defined (__SIZEOF_INT128__)
      40              : typedef unsigned HOST_HALF_WIDE_INT UHWtype;
      41              : typedef unsigned HOST_WIDE_INT UWtype;
      42              : typedef unsigned int UQItype __attribute__ ((mode (QI)));
      43              : typedef unsigned int USItype __attribute__ ((mode (SI)));
      44              : typedef unsigned int UDItype __attribute__ ((mode (DI)));
      45              : typedef unsigned int UDWtype __attribute__ ((mode (TI)));
      46              : #include "longlong.h"
      47              : #endif
      48              : 
      49              : static const HOST_WIDE_INT zeros[1] = {};
      50              : 
      51              : /*
      52              :  * Internal utilities.
      53              :  */
      54              : 
      55              : /* Quantities to deal with values that hold half of a wide int.  Used
      56              :    in multiply and divide.  */
      57              : #define HALF_INT_MASK ((HOST_WIDE_INT_1 << HOST_BITS_PER_HALF_WIDE_INT) - 1)
      58              : 
      59              : #define BLOCK_OF(TARGET) ((TARGET) / HOST_BITS_PER_WIDE_INT)
      60              : #define BLOCKS_NEEDED(PREC) (PREC ? CEIL (PREC, HOST_BITS_PER_WIDE_INT) : 1)
      61              : #define SIGN_MASK(X) ((HOST_WIDE_INT) (X) < 0 ? -1 : 0)
      62              : 
      63              : /* Return the value a VAL[I] if I < LEN, otherwise, return 0 or -1
      64              :    based on the top existing bit of VAL. */
      65              : 
      66              : static unsigned HOST_WIDE_INT
      67   4345241238 : safe_uhwi (const HOST_WIDE_INT *val, unsigned int len, unsigned int i)
      68              : {
      69   4345241238 :   return i < len ? val[i] : val[len - 1] < 0 ? HOST_WIDE_INT_M1 : 0;
      70              : }
      71              : 
      72              : /* Convert the integer in VAL to canonical form, returning its new length.
      73              :    LEN is the number of blocks currently in VAL and PRECISION is the number
      74              :    of bits in the integer it represents.
      75              : 
      76              :    This function only changes the representation, not the value.  */
      77              : static unsigned int
      78  22974970375 : canonize (HOST_WIDE_INT *val, unsigned int len, unsigned int precision)
      79              : {
      80  22974970375 :   unsigned int blocks_needed = BLOCKS_NEEDED (precision);
      81  22974970375 :   HOST_WIDE_INT top;
      82  22974970375 :   int i;
      83              : 
      84  22974970375 :   if (len > blocks_needed)
      85              :     len = blocks_needed;
      86              : 
      87  22974970375 :   if (len == 1)
      88              :     return 1;
      89              : 
      90   2800287736 :   top = val[len - 1];
      91   2800287736 :   if (len * HOST_BITS_PER_WIDE_INT > precision)
      92       406429 :     val[len - 1] = top = sext_hwi (top, precision % HOST_BITS_PER_WIDE_INT);
      93   2800287736 :   if (top != 0 && top != HOST_WIDE_INT_M1)
      94              :     return len;
      95              : 
      96              :   /* At this point we know that the top is either 0 or -1.  Find the
      97              :      first block that is not a copy of this.  */
      98   3925290425 :   for (i = len - 2; i >= 0; i--)
      99              :     {
     100   3113342770 :       HOST_WIDE_INT x = val[i];
     101   3113342770 :       if (x != top)
     102              :         {
     103   3364380914 :           if (SIGN_MASK (x) == top)
     104   1437112736 :             return i + 1;
     105              : 
     106              :           /* We need an extra block because the top bit block i does
     107              :              not match the extension.  */
     108    506833926 :           return i + 2;
     109              :         }
     110              :     }
     111              : 
     112              :   /* The number is 0 or -1.  */
     113              :   return 1;
     114              : }
     115              : 
     116              : /* VAL[0] is the unsigned result of an operation.  Canonize it by adding
     117              :    another 0 block if needed, and return number of blocks needed.  */
     118              : 
     119              : static inline unsigned int
     120    851290013 : canonize_uhwi (HOST_WIDE_INT *val, unsigned int precision)
     121              : {
     122      1117442 :   if (val[0] < 0 && precision > HOST_BITS_PER_WIDE_INT)
     123              :     {
     124        25615 :       val[1] = 0;
     125        25615 :       return 2;
     126              :     }
     127              :   return 1;
     128              : }
     129              : 
     130              : /*
     131              :  * Conversion routines in and out of wide_int.
     132              :  */
     133              : 
     134              : /* Copy XLEN elements from XVAL to VAL.  If NEED_CANON, canonize the
     135              :    result for an integer with precision PRECISION.  Return the length
     136              :    of VAL (after any canonization).  */
     137              : unsigned int
     138    200928466 : wi::from_array (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
     139              :                 unsigned int xlen, unsigned int precision, bool need_canon)
     140              : {
     141    798249646 :   for (unsigned i = 0; i < xlen; i++)
     142    597321180 :     val[i] = xval[i];
     143    200928466 :   return need_canon ? canonize (val, xlen, precision) : xlen;
     144              : }
     145              : 
     146              : /* Construct a wide int from a buffer of length LEN.  BUFFER will be
     147              :    read according to byte endianness and word endianness of the target.
     148              :    Only the lower BUFFER_LEN bytes of the result are set; the remaining
     149              :    high bytes are cleared.  */
     150              : wide_int
     151      3099912 : wi::from_buffer (const unsigned char *buffer, unsigned int buffer_len)
     152              : {
     153      3099912 :   unsigned int precision = buffer_len * BITS_PER_UNIT;
     154      3099912 :   wide_int result = wide_int::create (precision);
     155      3099912 :   unsigned int words = buffer_len / UNITS_PER_WORD;
     156              : 
     157              :   /* We have to clear all the bits ourself, as we merely or in values
     158              :      below.  */
     159      3099912 :   unsigned int len = BLOCKS_NEEDED (precision);
     160      3099912 :   HOST_WIDE_INT *val = result.write_val (0);
     161      6209224 :   for (unsigned int i = 0; i < len; ++i)
     162      3109312 :     val[i] = 0;
     163              : 
     164     19507612 :   for (unsigned int byte = 0; byte < buffer_len; byte++)
     165              :     {
     166     16407700 :       unsigned int offset;
     167     16407700 :       unsigned int index;
     168     16407700 :       unsigned int bitpos = byte * BITS_PER_UNIT;
     169     16407700 :       unsigned HOST_WIDE_INT value;
     170              : 
     171     16407700 :       if (buffer_len > UNITS_PER_WORD)
     172              :         {
     173     16407700 :           unsigned int word = byte / UNITS_PER_WORD;
     174              : 
     175     16407700 :           if (WORDS_BIG_ENDIAN)
     176              :             word = (words - 1) - word;
     177              : 
     178     16407700 :           offset = word * UNITS_PER_WORD;
     179              : 
     180     16407700 :           if (BYTES_BIG_ENDIAN)
     181              :             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
     182              :           else
     183     16407700 :             offset += byte % UNITS_PER_WORD;
     184              :         }
     185              :       else
     186              :         offset = BYTES_BIG_ENDIAN ? (buffer_len - 1) - byte : byte;
     187              : 
     188     16407700 :       value = (unsigned HOST_WIDE_INT) buffer[offset];
     189              : 
     190     16407700 :       index = bitpos / HOST_BITS_PER_WIDE_INT;
     191     16407700 :       val[index] |= value << (bitpos % HOST_BITS_PER_WIDE_INT);
     192              :     }
     193              : 
     194      3099912 :   result.set_len (canonize (val, len, precision));
     195              : 
     196      3099912 :   return result;
     197              : }
     198              : 
     199              : /* Sets RESULT from X, the sign is taken according to SGN.  */
     200              : void
     201    132839117 : wi::to_mpz (const wide_int_ref &x, mpz_t result, signop sgn)
     202              : {
     203    132839117 :   int len = x.get_len ();
     204    132839117 :   const HOST_WIDE_INT *v = x.get_val ();
     205    132839117 :   int excess = len * HOST_BITS_PER_WIDE_INT - x.get_precision ();
     206              : 
     207    132839117 :   if (wi::neg_p (x, sgn))
     208              :     {
     209              :       /* We use ones complement to avoid -x80..0 edge case that -
     210              :          won't work on.  */
     211     11083305 :       HOST_WIDE_INT *t = XALLOCAVEC (HOST_WIDE_INT, len);
     212     22196510 :       for (int i = 0; i < len; i++)
     213     11113205 :         t[i] = ~v[i];
     214     11083305 :       if (excess > 0)
     215      5433515 :         t[len - 1] = (unsigned HOST_WIDE_INT) t[len - 1] << excess >> excess;
     216     11083305 :       mpz_import (result, len, -1, sizeof (HOST_WIDE_INT), 0, 0, t);
     217     11083305 :       mpz_com (result, result);
     218              :     }
     219    121755812 :   else if (excess > 0)
     220              :     {
     221     71303880 :       HOST_WIDE_INT *t = XALLOCAVEC (HOST_WIDE_INT, len);
     222     71304720 :       for (int i = 0; i < len - 1; i++)
     223          840 :         t[i] = v[i];
     224     71303880 :       t[len - 1] = (unsigned HOST_WIDE_INT) v[len - 1] << excess >> excess;
     225     71303880 :       mpz_import (result, len, -1, sizeof (HOST_WIDE_INT), 0, 0, t);
     226              :     }
     227     50451932 :   else if (excess < 0 && wi::neg_p (x))
     228              :     {
     229        36491 :       int extra = CEIL (-excess, HOST_BITS_PER_WIDE_INT);
     230        36491 :       HOST_WIDE_INT *t = XALLOCAVEC (HOST_WIDE_INT, len + extra);
     231        72982 :       for (int i = 0; i < len; i++)
     232        36491 :         t[i] = v[i];
     233        75463 :       for (int i = 0; i < extra; i++)
     234        38972 :         t[len + i] = -1;
     235        36491 :       excess = (-excess) % HOST_BITS_PER_WIDE_INT;
     236        36491 :       if (excess)
     237          141 :         t[len + extra - 1] = (HOST_WIDE_INT_1U << excess) - 1;
     238        36491 :       mpz_import (result, len + extra, -1, sizeof (HOST_WIDE_INT), 0, 0, t);
     239              :     }
     240              :   else
     241     50415441 :     mpz_import (result, len, -1, sizeof (HOST_WIDE_INT), 0, 0, v);
     242    132839117 : }
     243              : 
     244              : /* Returns X converted to TYPE.  If WRAP is true, then out-of-range
     245              :    values of VAL will be wrapped; otherwise, they will be set to the
     246              :    appropriate minimum or maximum TYPE bound.  */
     247              : wide_int
     248     15777509 : wi::from_mpz (const_tree type, mpz_t x, bool wrap)
     249              : {
     250     15777509 :   size_t count, numb;
     251     15777509 :   unsigned int prec = TYPE_PRECISION (type);
     252     15777509 :   wide_int res = wide_int::create (prec);
     253              : 
     254     15777509 :   if (!wrap)
     255              :     {
     256     13214061 :       mpz_t min, max;
     257              : 
     258     13214061 :       mpz_init (min);
     259     13214061 :       mpz_init (max);
     260     13214061 :       get_type_static_bounds (type, min, max);
     261              : 
     262     13214061 :       if (mpz_cmp (x, min) < 0)
     263          220 :         mpz_set (x, min);
     264     13213841 :       else if (mpz_cmp (x, max) > 0)
     265          267 :         mpz_set (x, max);
     266              : 
     267     13214061 :       mpz_clear (min);
     268     13214061 :       mpz_clear (max);
     269              :     }
     270              : 
     271              :   /* Determine the number of unsigned HOST_WIDE_INTs that are required
     272              :      for representing the absolute value.  The code to calculate count is
     273              :      extracted from the GMP manual, section "Integer Import and Export":
     274              :      http://gmplib.org/manual/Integer-Import-and-Export.html  */
     275     15777509 :   numb = CHAR_BIT * sizeof (HOST_WIDE_INT);
     276     15777509 :   count = CEIL (mpz_sizeinbase (x, 2), numb);
     277     15777509 :   HOST_WIDE_INT *val = res.write_val (0);
     278              :   /* Read the absolute value.
     279              : 
     280              :      Write directly to the wide_int storage if possible, otherwise leave
     281              :      GMP to allocate the memory for us.  It might be slightly more efficient
     282              :      to use mpz_tdiv_r_2exp for the latter case, but the situation is
     283              :      pathological and it seems safer to operate on the original mpz value
     284              :      in all cases.  */
     285     15777543 :   void *valres = mpz_export (count <= WIDE_INT_MAX_INL_ELTS ? val : 0,
     286              :                              &count, -1, sizeof (HOST_WIDE_INT), 0, 0, x);
     287     15777509 :   if (count < 1)
     288              :     {
     289       259805 :       val[0] = 0;
     290       259805 :       count = 1;
     291              :     }
     292     15777509 :   count = MIN (count, BLOCKS_NEEDED (prec));
     293     15777509 :   if (valres != val)
     294              :     {
     295           34 :       memcpy (val, valres, count * sizeof (HOST_WIDE_INT));
     296           34 :       free (valres);
     297              :     }
     298              :   /* Zero-extend the absolute value to PREC bits.  */
     299     15777509 :   if (count < BLOCKS_NEEDED (prec) && val[count - 1] < 0)
     300         2453 :     val[count++] = 0;
     301              :   else
     302     15775056 :     count = canonize (val, count, prec);
     303     15777509 :   res.set_len (count);
     304              : 
     305     15777509 :   if (mpz_sgn (x) < 0)
     306        96660 :     res = -res;
     307              : 
     308     15777509 :   return res;
     309              : }
     310              : 
     311              : /*
     312              :  * Largest and smallest values in a mode.
     313              :  */
     314              : 
     315              : /* Return the largest SGNed number that is representable in PRECISION bits.
     316              : 
     317              :    TODO: There is still code from the double_int era that tries to
     318              :    make up for the fact that double int's could not represent the
     319              :    min and max values of all types.  This code should be removed
     320              :    because the min and max values can always be represented in
     321              :    wide_ints and int-csts.  */
     322              : wide_int
     323   5837667496 : wi::max_value (unsigned int precision, signop sgn)
     324              : {
     325   5837667496 :   gcc_checking_assert (precision != 0);
     326   5837667496 :   if (sgn == UNSIGNED)
     327              :     /* The unsigned max is just all ones.  */
     328   4386453706 :     return shwi (-1, precision);
     329              :   else
     330              :     /* The signed max is all ones except the top bit.  This must be
     331              :        explicitly represented.  */
     332   1451213790 :     return mask (precision - 1, false, precision);
     333              : }
     334              : 
     335              : /* Return the largest SGNed number that is representable in PRECISION bits.  */
     336              : wide_int
     337   7143465679 : wi::min_value (unsigned int precision, signop sgn)
     338              : {
     339   7143465679 :   gcc_checking_assert (precision != 0);
     340   7143465679 :   if (sgn == UNSIGNED)
     341   4235649219 :     return uhwi (0, precision);
     342              :   else
     343              :     /* The signed min is all zeros except the top bit.  This must be
     344              :        explicitly represented.  */
     345   2907816460 :     return wi::set_bit_in_zero (precision - 1, precision);
     346              : }
     347              : 
     348              : /*
     349              :  * Public utilities.
     350              :  */
     351              : 
     352              : /* Convert the number represented by XVAL, XLEN and XPRECISION, which has
     353              :    signedness SGN, to an integer that has PRECISION bits.  Store the blocks
     354              :    in VAL and return the number of blocks used.
     355              : 
     356              :    This function can handle both extension (PRECISION > XPRECISION)
     357              :    and truncation (PRECISION < XPRECISION).  */
     358              : unsigned int
     359  20109761950 : wi::force_to_size (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
     360              :                    unsigned int xlen, unsigned int xprecision,
     361              :                    unsigned int precision, signop sgn)
     362              : {
     363  20109761950 :   unsigned int blocks_needed = BLOCKS_NEEDED (precision);
     364  20109761950 :   unsigned int len = blocks_needed < xlen ? blocks_needed : xlen;
     365  40232607468 :   for (unsigned i = 0; i < len; i++)
     366  20122845518 :     val[i] = xval[i];
     367              : 
     368  20109761950 :   if (precision > xprecision)
     369              :     {
     370   5723453415 :       unsigned int small_xprecision = xprecision % HOST_BITS_PER_WIDE_INT;
     371              : 
     372              :       /* Expanding.  */
     373   5723453415 :       if (sgn == UNSIGNED)
     374              :         {
     375   2592145905 :           if (small_xprecision && len == BLOCKS_NEEDED (xprecision))
     376   1185908650 :             val[len - 1] = zext_hwi (val[len - 1], small_xprecision);
     377   1406237255 :           else if (val[len - 1] < 0)
     378              :             {
     379    239484262 :               while (len < BLOCKS_NEEDED (xprecision))
     380      1537217 :                 val[len++] = -1;
     381    237947045 :               if (small_xprecision)
     382        43544 :                 val[len - 1] = zext_hwi (val[len - 1], small_xprecision);
     383              :               else
     384    237903501 :                 val[len++] = 0;
     385              :             }
     386              :         }
     387              :       else
     388              :         {
     389   3131307510 :           if (small_xprecision && len == BLOCKS_NEEDED (xprecision))
     390   1236744915 :             val[len - 1] = sext_hwi (val[len - 1], small_xprecision);
     391              :         }
     392              :     }
     393  20109761950 :   len = canonize (val, len, precision);
     394              : 
     395  20109761950 :   return len;
     396              : }
     397              : 
     398              : /* This function hides the fact that we cannot rely on the bits beyond
     399              :    the precision.  This issue comes up in the relational comparisons
     400              :    where we do allow comparisons of values of different precisions.  */
     401              : static inline HOST_WIDE_INT
     402    277937670 : selt (const HOST_WIDE_INT *a, unsigned int len,
     403              :       unsigned int blocks_needed, unsigned int small_prec,
     404              :       unsigned int index, signop sgn)
     405              : {
     406    277937670 :   HOST_WIDE_INT val;
     407    277937670 :   if (index < len)
     408    222780461 :     val = a[index];
     409     55157209 :   else if (index < blocks_needed || sgn == SIGNED)
     410              :     /* Signed or within the precision.  */
     411     55157209 :     val = SIGN_MASK (a[len - 1]);
     412              :   else
     413              :     /* Unsigned extension beyond the precision. */
     414              :     val = 0;
     415              : 
     416    277937670 :   if (small_prec && index == blocks_needed - 1)
     417      1296460 :     return (sgn == SIGNED
     418      1296460 :             ? sext_hwi (val, small_prec)
     419       166828 :             : zext_hwi (val, small_prec));
     420              :   else
     421              :     return val;
     422              : }
     423              : 
     424              : /* Find the highest bit represented in a wide int.  This will in
     425              :    general have the same value as the sign bit.  */
     426              : static inline HOST_WIDE_INT
     427    675497480 : top_bit_of (const HOST_WIDE_INT *a, unsigned int len, unsigned int prec)
     428              : {
     429    675497480 :   int excess = len * HOST_BITS_PER_WIDE_INT - prec;
     430    675497480 :   unsigned HOST_WIDE_INT val = a[len - 1];
     431    675497480 :   if (excess > 0)
     432        43741 :     val <<= excess;
     433    675497480 :   return val >> (HOST_BITS_PER_WIDE_INT - 1);
     434              : }
     435              : 
     436              : /*
     437              :  * Comparisons, note that only equality is an operator.  The other
     438              :  * comparisons cannot be operators since they are inherently signed or
     439              :  * unsigned and C++ has no such operators.
     440              :  */
     441              : 
     442              : /* Return true if OP0 == OP1.  */
     443              : bool
     444      2389056 : wi::eq_p_large (const HOST_WIDE_INT *op0, unsigned int op0len,
     445              :                 const HOST_WIDE_INT *op1, unsigned int op1len,
     446              :                 unsigned int prec)
     447              : {
     448      2389056 :   int l0 = op0len - 1;
     449      2389056 :   unsigned int small_prec = prec & (HOST_BITS_PER_WIDE_INT - 1);
     450              : 
     451      2389056 :   if (op0len != op1len)
     452              :     return false;
     453              : 
     454      1251611 :   if (op0len == BLOCKS_NEEDED (prec) && small_prec)
     455              :     {
     456              :       /* It does not matter if we zext or sext here, we just have to
     457              :          do both the same way.  */
     458        60371 :       if (zext_hwi (op0 [l0], small_prec) != zext_hwi (op1 [l0], small_prec))
     459              :         return false;
     460        55868 :       l0--;
     461              :     }
     462              : 
     463      3818956 :   while (l0 >= 0)
     464      2625969 :     if (op0[l0] != op1[l0])
     465              :       return false;
     466              :     else
     467      2571848 :       l0--;
     468              : 
     469              :   return true;
     470              : }
     471              : 
     472              : /* Return true if OP0 < OP1 using signed comparisons.  */
     473              : bool
     474     26744716 : wi::lts_p_large (const HOST_WIDE_INT *op0, unsigned int op0len,
     475              :                  unsigned int precision,
     476              :                  const HOST_WIDE_INT *op1, unsigned int op1len)
     477              : {
     478     26744716 :   HOST_WIDE_INT s0, s1;
     479     26744716 :   unsigned HOST_WIDE_INT u0, u1;
     480     26744716 :   unsigned int blocks_needed = BLOCKS_NEEDED (precision);
     481     26744716 :   unsigned int small_prec = precision & (HOST_BITS_PER_WIDE_INT - 1);
     482     26744716 :   int l = MAX (op0len - 1, op1len - 1);
     483              : 
     484              :   /* Only the top block is compared as signed.  The rest are unsigned
     485              :      comparisons.  */
     486     26744716 :   s0 = selt (op0, op0len, blocks_needed, small_prec, l, SIGNED);
     487     26744716 :   s1 = selt (op1, op1len, blocks_needed, small_prec, l, SIGNED);
     488     26744716 :   if (s0 < s1)
     489              :     return true;
     490     25778229 :   if (s0 > s1)
     491              :     return false;
     492              : 
     493     22709831 :   l--;
     494     29410785 :   while (l >= 0)
     495              :     {
     496     23278672 :       u0 = selt (op0, op0len, blocks_needed, small_prec, l, SIGNED);
     497     23278672 :       u1 = selt (op1, op1len, blocks_needed, small_prec, l, SIGNED);
     498              : 
     499     23278672 :       if (u0 < u1)
     500              :         return true;
     501      8574086 :       if (u0 > u1)
     502              :         return false;
     503      6700954 :       l--;
     504              :     }
     505              : 
     506              :   return false;
     507              : }
     508              : 
     509              : /* Returns -1 if OP0 < OP1, 0 if OP0 == OP1 and 1 if OP0 > OP1 using
     510              :    signed compares.  */
     511              : int
     512      2671338 : wi::cmps_large (const HOST_WIDE_INT *op0, unsigned int op0len,
     513              :                 unsigned int precision,
     514              :                 const HOST_WIDE_INT *op1, unsigned int op1len)
     515              : {
     516      2671338 :   HOST_WIDE_INT s0, s1;
     517      2671338 :   unsigned HOST_WIDE_INT u0, u1;
     518      2671338 :   unsigned int blocks_needed = BLOCKS_NEEDED (precision);
     519      2671338 :   unsigned int small_prec = precision & (HOST_BITS_PER_WIDE_INT - 1);
     520      2671338 :   int l = MAX (op0len - 1, op1len - 1);
     521              : 
     522              :   /* Only the top block is compared as signed.  The rest are unsigned
     523              :      comparisons.  */
     524      2671338 :   s0 = selt (op0, op0len, blocks_needed, small_prec, l, SIGNED);
     525      2671338 :   s1 = selt (op1, op1len, blocks_needed, small_prec, l, SIGNED);
     526      2671338 :   if (s0 < s1)
     527              :     return -1;
     528      1213232 :   if (s0 > s1)
     529              :     return 1;
     530              : 
     531      1084680 :   l--;
     532      1896084 :   while (l >= 0)
     533              :     {
     534      1317530 :       u0 = selt (op0, op0len, blocks_needed, small_prec, l, SIGNED);
     535      1317530 :       u1 = selt (op1, op1len, blocks_needed, small_prec, l, SIGNED);
     536              : 
     537      1317530 :       if (u0 < u1)
     538              :         return -1;
     539       823746 :       if (u0 > u1)
     540              :         return 1;
     541       811404 :       l--;
     542              :     }
     543              : 
     544              :   return 0;
     545              : }
     546              : 
     547              : /* Return true if OP0 < OP1 using unsigned comparisons.  */
     548              : bool
     549     44161696 : wi::ltu_p_large (const HOST_WIDE_INT *op0, unsigned int op0len,
     550              :                  unsigned int precision,
     551              :                  const HOST_WIDE_INT *op1, unsigned int op1len)
     552              : {
     553     44161696 :   unsigned HOST_WIDE_INT x0;
     554     44161696 :   unsigned HOST_WIDE_INT x1;
     555     44161696 :   unsigned int blocks_needed = BLOCKS_NEEDED (precision);
     556     44161696 :   unsigned int small_prec = precision & (HOST_BITS_PER_WIDE_INT - 1);
     557     44161696 :   int l = MAX (op0len - 1, op1len - 1);
     558              : 
     559     67314082 :   while (l >= 0)
     560              :     {
     561     64796333 :       x0 = selt (op0, op0len, blocks_needed, small_prec, l, UNSIGNED);
     562     64796333 :       x1 = selt (op1, op1len, blocks_needed, small_prec, l, UNSIGNED);
     563     64796333 :       if (x0 < x1)
     564              :         return true;
     565     53089126 :       if (x0 > x1)
     566              :         return false;
     567     23152386 :       l--;
     568              :     }
     569              : 
     570              :   return false;
     571              : }
     572              : 
     573              : /* Returns -1 if OP0 < OP1, 0 if OP0 == OP1 and 1 if OP0 > OP1 using
     574              :    unsigned compares.  */
     575              : int
     576     11950206 : wi::cmpu_large (const HOST_WIDE_INT *op0, unsigned int op0len,
     577              :                 unsigned int precision,
     578              :                 const HOST_WIDE_INT *op1, unsigned int op1len)
     579              : {
     580     11950206 :   unsigned HOST_WIDE_INT x0;
     581     11950206 :   unsigned HOST_WIDE_INT x1;
     582     11950206 :   unsigned int blocks_needed = BLOCKS_NEEDED (precision);
     583     11950206 :   unsigned int small_prec = precision & (HOST_BITS_PER_WIDE_INT - 1);
     584     11950206 :   int l = MAX (op0len - 1, op1len - 1);
     585              : 
     586     21120249 :   while (l >= 0)
     587              :     {
     588     20160246 :       x0 = selt (op0, op0len, blocks_needed, small_prec, l, UNSIGNED);
     589     20160246 :       x1 = selt (op1, op1len, blocks_needed, small_prec, l, UNSIGNED);
     590     20160246 :       if (x0 < x1)
     591              :         return -1;
     592     10816154 :       if (x0 > x1)
     593              :         return 1;
     594      9170043 :       l--;
     595              :     }
     596              : 
     597              :   return 0;
     598              : }
     599              : 
     600              : /*
     601              :  * Extension.
     602              :  */
     603              : 
     604              : /* Sign-extend the number represented by XVAL and XLEN into VAL,
     605              :    starting at OFFSET.  Return the number of blocks in VAL.  Both XVAL
     606              :    and VAL have PRECISION bits.  */
     607              : unsigned int
     608      5968904 : wi::sext_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
     609              :                 unsigned int xlen, unsigned int precision, unsigned int offset)
     610              : {
     611      5968904 :   unsigned int len = offset / HOST_BITS_PER_WIDE_INT;
     612              :   /* Extending beyond the precision is a no-op.  If we have only stored
     613              :      OFFSET bits or fewer, the rest are already signs.  */
     614      5968904 :   if (offset >= precision || len >= xlen)
     615              :     {
     616    180226696 :       for (unsigned i = 0; i < xlen; ++i)
     617    174634984 :         val[i] = xval[i];
     618              :       return xlen;
     619              :     }
     620       377192 :   unsigned int suboffset = offset % HOST_BITS_PER_WIDE_INT;
     621      1722577 :   for (unsigned int i = 0; i < len; i++)
     622      1345385 :     val[i] = xval[i];
     623       377192 :   if (suboffset > 0)
     624              :     {
     625        43352 :       val[len] = sext_hwi (xval[len], suboffset);
     626        43352 :       len += 1;
     627              :     }
     628       377192 :   return canonize (val, len, precision);
     629              : }
     630              : 
     631              : /* Zero-extend the number represented by XVAL and XLEN into VAL,
     632              :    starting at OFFSET.  Return the number of blocks in VAL.  Both XVAL
     633              :    and VAL have PRECISION bits.  */
     634              : unsigned int
     635    766498363 : wi::zext_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
     636              :                 unsigned int xlen, unsigned int precision, unsigned int offset)
     637              : {
     638    766498363 :   unsigned int len = offset / HOST_BITS_PER_WIDE_INT;
     639              :   /* Extending beyond the precision is a no-op.  If we have only stored
     640              :      OFFSET bits or fewer, and the upper stored bit is zero, then there
     641              :      is nothing to do.  */
     642    766498363 :   if (offset >= precision || (len >= xlen && xval[xlen - 1] >= 0))
     643              :     {
     644   1240192761 :       for (unsigned i = 0; i < xlen; ++i)
     645    620527255 :         val[i] = xval[i];
     646              :       return xlen;
     647              :     }
     648    146832857 :   unsigned int suboffset = offset % HOST_BITS_PER_WIDE_INT;
     649    295168330 :   for (unsigned int i = 0; i < len; i++)
     650    148335473 :     val[i] = i < xlen ? xval[i] : -1;
     651    146832857 :   if (suboffset > 0)
     652        51961 :     val[len] = zext_hwi (len < xlen ? xval[len] : -1, suboffset);
     653              :   else
     654    146780896 :     val[len] = 0;
     655    146832857 :   return canonize (val, len + 1, precision);
     656              : }
     657              : 
     658              : /*
     659              :  * Masking, inserting, shifting, rotating.
     660              :  */
     661              : 
     662              : /* Insert WIDTH bits from Y into X starting at START.  */
     663              : wide_int
     664       365994 : wi::insert (const wide_int &x, const wide_int &y, unsigned int start,
     665              :             unsigned int width)
     666              : {
     667       365994 :   wide_int result;
     668       365994 :   wide_int mask;
     669       365994 :   wide_int tmp;
     670              : 
     671       365994 :   unsigned int precision = x.get_precision ();
     672       365994 :   if (start >= precision)
     673            0 :     return x;
     674              : 
     675       365994 :   gcc_checking_assert (precision >= width);
     676              : 
     677       365994 :   if (start + width >= precision)
     678       142444 :     width = precision - start;
     679              : 
     680       365994 :   mask = wi::shifted_mask (start, width, false, precision);
     681       365994 :   tmp = wi::lshift (wide_int::from (y, precision, UNSIGNED), start);
     682       365994 :   result = tmp & mask;
     683              : 
     684       365994 :   tmp = wi::bit_and_not (x, mask);
     685       365994 :   result = result | tmp;
     686              : 
     687       365994 :   return result;
     688       365994 : }
     689              : 
     690              : /* Copy the number represented by XVAL and XLEN into VAL, setting bit BIT.
     691              :    Return the number of blocks in VAL.  Both XVAL and VAL have PRECISION
     692              :    bits.  */
     693              : unsigned int
     694           88 : wi::set_bit_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
     695              :                    unsigned int xlen, unsigned int precision, unsigned int bit)
     696              : {
     697           88 :   unsigned int block = bit / HOST_BITS_PER_WIDE_INT;
     698           88 :   unsigned int subbit = bit % HOST_BITS_PER_WIDE_INT;
     699              : 
     700           88 :   if (block + 1 >= xlen)
     701              :     {
     702              :       /* The operation either affects the last current block or needs
     703              :          a new block.  */
     704           36 :       unsigned int len = block + 1;
     705           36 :       for (unsigned int i = 0; i < len; i++)
     706           48 :         val[i] = safe_uhwi (xval, xlen, i);
     707           12 :       val[block] |= HOST_WIDE_INT_1U << subbit;
     708              : 
     709              :       /* If the bit we just set is at the msb of the block, make sure
     710              :          that any higher bits are zeros.  */
     711           12 :       if (bit + 1 < precision && subbit == HOST_BITS_PER_WIDE_INT - 1)
     712              :         {
     713            0 :           val[len++] = 0;
     714            0 :           return len;
     715              :         }
     716           12 :       return canonize (val, len, precision);
     717              :     }
     718              :   else
     719              :     {
     720          380 :       for (unsigned int i = 0; i < xlen; i++)
     721          304 :         val[i] = xval[i];
     722           76 :       val[block] |= HOST_WIDE_INT_1U << subbit;
     723           76 :       return canonize (val, xlen, precision);
     724              :     }
     725              : }
     726              : 
     727              : /* Byte swap the integer represented by XVAL and XLEN into VAL.  Return
     728              :    the number of blocks in VAL.  Both XVAL and VAL have PRECISION bits.  */
     729              : unsigned int
     730         7596 : wi::bswap_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
     731              :                  unsigned int xlen, unsigned int precision)
     732              : {
     733         7596 :   unsigned int s, len = BLOCKS_NEEDED (precision);
     734              : 
     735              :   /* This is not a well defined operation if the precision is not a
     736              :      multiple of 8.  */
     737         7596 :   gcc_assert ((precision & 0x7) == 0);
     738              : 
     739         7596 :   memset (val, 0, sizeof (unsigned HOST_WIDE_INT) * len);
     740              : 
     741              :   /* Only swap the bytes that are not the padding.  */
     742        47967 :   for (s = 0; s < precision; s += 8)
     743              :     {
     744        40371 :       unsigned int d = precision - s - 8;
     745        40371 :       unsigned HOST_WIDE_INT byte;
     746              : 
     747        40371 :       unsigned int block = s / HOST_BITS_PER_WIDE_INT;
     748        40371 :       unsigned int offset = s & (HOST_BITS_PER_WIDE_INT - 1);
     749              : 
     750        40371 :       byte = (safe_uhwi (xval, xlen, block) >> offset) & 0xff;
     751              : 
     752        40371 :       block = d / HOST_BITS_PER_WIDE_INT;
     753        40371 :       offset = d & (HOST_BITS_PER_WIDE_INT - 1);
     754              : 
     755        40371 :       val[block] |= byte << offset;
     756              :     }
     757              : 
     758         7596 :   return canonize (val, len, precision);
     759              : }
     760              : 
     761              : /* Bitreverse the integer represented by XVAL and XLEN into VAL.  Return
     762              :    the number of blocks in VAL.  Both XVAL and VAL have PRECISION bits.  */
     763              : unsigned int
     764          213 : wi::bitreverse_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
     765              :                       unsigned int xlen, unsigned int precision)
     766              : {
     767          213 :   unsigned int s, len = BLOCKS_NEEDED (precision);
     768              : 
     769          213 :   memset (val, 0, sizeof (unsigned HOST_WIDE_INT) * len);
     770              : 
     771        16247 :   for (s = 0; s < precision; s++)
     772              :     {
     773        16034 :       unsigned int block = s / HOST_BITS_PER_WIDE_INT;
     774        16034 :       unsigned int offset = s & (HOST_BITS_PER_WIDE_INT - 1);
     775        32068 :       if (((safe_uhwi (xval, xlen, block) >> offset) & 1) != 0)
     776              :         {
     777         3885 :           unsigned int d = (precision - 1) - s;
     778         3885 :           block = d / HOST_BITS_PER_WIDE_INT;
     779         3885 :           offset = d & (HOST_BITS_PER_WIDE_INT - 1);
     780         3885 :           val[block] |= HOST_WIDE_INT_1U << offset;
     781              :         }
     782              :     }
     783              : 
     784          213 :   return canonize (val, len, precision);
     785              : }
     786              : 
     787              : /* Fill VAL with a mask where the lower WIDTH bits are ones and the bits
     788              :    above that up to PREC are zeros.  The result is inverted if NEGATE
     789              :    is true.  Return the number of blocks in VAL.  */
     790              : unsigned int
     791   2732255508 : wi::mask (HOST_WIDE_INT *val, unsigned int width, bool negate,
     792              :           unsigned int prec)
     793              : {
     794   2732255508 :   if (width >= prec)
     795              :     {
     796    577141518 :       val[0] = negate ? 0 : -1;
     797    577141518 :       return 1;
     798              :     }
     799   2155113990 :   else if (width == 0)
     800              :     {
     801     11778081 :       val[0] = negate ? -1 : 0;
     802     11778081 :       return 1;
     803              :     }
     804              : 
     805              :   unsigned int i = 0;
     806   2478473624 :   while (i < width / HOST_BITS_PER_WIDE_INT)
     807    670001436 :     val[i++] = negate ? 0 : -1;
     808              : 
     809   2143335909 :   unsigned int shift = width & (HOST_BITS_PER_WIDE_INT - 1);
     810   2143335909 :   if (shift != 0)
     811              :     {
     812   2123719378 :       HOST_WIDE_INT last = (HOST_WIDE_INT_1U << shift) - 1;
     813   2123719378 :       val[i++] = negate ? ~last : last;
     814              :     }
     815              :   else
     816     38982085 :     val[i++] = negate ? -1 : 0;
     817              : 
     818              :   return i;
     819              : }
     820              : 
     821              : /* Fill VAL with a mask where the lower START bits are zeros, the next WIDTH
     822              :    bits are ones, and the bits above that up to PREC are zeros.  The result
     823              :    is inverted if NEGATE is true.  Return the number of blocks in VAL.  */
     824              : unsigned int
     825   2926010305 : wi::shifted_mask (HOST_WIDE_INT *val, unsigned int start, unsigned int width,
     826              :                   bool negate, unsigned int prec)
     827              : {
     828   2926010305 :   if (start >= prec || width == 0)
     829              :     {
     830          741 :       val[0] = negate ? -1 : 0;
     831          741 :       return 1;
     832              :     }
     833              : 
     834   2926009564 :   if (width > prec - start)
     835              :     width = prec - start;
     836   2926009564 :   unsigned int end = start + width;
     837              : 
     838   2926009564 :   unsigned int i = 0;
     839   3686112609 :   while (i < start / HOST_BITS_PER_WIDE_INT)
     840   1520206088 :     val[i++] = negate ? -1 : 0;
     841              : 
     842   2926009564 :   unsigned int shift = start & (HOST_BITS_PER_WIDE_INT - 1);
     843   2926009564 :   if (shift)
     844              :     {
     845   2924684954 :       HOST_WIDE_INT block = (HOST_WIDE_INT_1U << shift) - 1;
     846   2924684954 :       shift += width;
     847   2924684954 :       if (shift < HOST_BITS_PER_WIDE_INT)
     848              :         {
     849              :           /* case 000111000 */
     850   1522867811 :           block = (HOST_WIDE_INT_1U << shift) - block - 1;
     851   1522867811 :           val[i++] = negate ? ~block : block;
     852   1522867811 :           return i;
     853              :         }
     854              :       else
     855              :         /* ...111000 */
     856   1401817143 :         val[i++] = negate ? block : ~block;
     857              :     }
     858              : 
     859   1403141753 :   if (end >= prec)
     860              :     {
     861   1401849651 :       if (!shift)
     862       330154 :         val[i++] = negate ? 0 : -1;
     863              :       return i;
     864              :     }
     865              : 
     866      1475795 :   while (i < end / HOST_BITS_PER_WIDE_INT)
     867              :     /* 1111111 */
     868       193886 :     val[i++] = negate ? 0 : -1;
     869              : 
     870      1292102 :   shift = end & (HOST_BITS_PER_WIDE_INT - 1);
     871      1292102 :   if (shift != 0)
     872              :     {
     873              :       /* 000011111 */
     874       978567 :       HOST_WIDE_INT block = (HOST_WIDE_INT_1U << shift) - 1;
     875       978567 :       val[i++] = negate ? ~block : block;
     876              :     }
     877              :   else
     878       455587 :     val[i++] = negate ? -1 : 0;
     879              : 
     880              :   return i;
     881              : }
     882              : 
     883              : /*
     884              :  * logical operations.
     885              :  */
     886              : 
     887              : /* Set VAL to OP0 & OP1.  Return the number of blocks used.  */
     888              : unsigned int
     889     32965980 : wi::and_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *op0,
     890              :                unsigned int op0len, const HOST_WIDE_INT *op1,
     891              :                unsigned int op1len, unsigned int prec)
     892              : {
     893     32965980 :   int l0 = op0len - 1;
     894     32965980 :   int l1 = op1len - 1;
     895     32965980 :   bool need_canon = true;
     896              : 
     897     32965980 :   unsigned int len = MAX (op0len, op1len);
     898     32965980 :   if (l0 > l1)
     899              :     {
     900     14102118 :       HOST_WIDE_INT op1mask = -top_bit_of (op1, op1len, prec);
     901     14102118 :       if (op1mask == 0)
     902              :         {
     903     32965980 :           l0 = l1;
     904     32965980 :           len = l1 + 1;
     905              :         }
     906              :       else
     907              :         {
     908       158364 :           need_canon = false;
     909       158364 :           while (l0 > l1)
     910              :             {
     911        79681 :               val[l0] = op0[l0];
     912        79681 :               l0--;
     913              :             }
     914              :         }
     915              :     }
     916     18863862 :   else if (l1 > l0)
     917              :     {
     918      7858452 :       HOST_WIDE_INT op0mask = -top_bit_of (op0, op0len, prec);
     919      7858452 :       if (op0mask == 0)
     920              :         len = l0 + 1;
     921              :       else
     922              :         {
     923       929744 :           need_canon = false;
     924       929744 :           while (l1 > l0)
     925              :             {
     926       487922 :               val[l1] = op1[l1];
     927       487922 :               l1--;
     928              :             }
     929              :         }
     930              :     }
     931              : 
     932     77189422 :   while (l0 >= 0)
     933              :     {
     934     44223442 :       val[l0] = op0[l0] & op1[l0];
     935     44223442 :       l0--;
     936              :     }
     937              : 
     938     32965980 :   if (need_canon)
     939     32445475 :     len = canonize (val, len, prec);
     940              : 
     941     32965980 :   return len;
     942              : }
     943              : 
     944              : /* Set VAL to OP0 & ~OP1.  Return the number of blocks used.  */
     945              : unsigned int
     946     96637366 : wi::and_not_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *op0,
     947              :                    unsigned int op0len, const HOST_WIDE_INT *op1,
     948              :                    unsigned int op1len, unsigned int prec)
     949              : {
     950     96637366 :   wide_int result;
     951     96637366 :   int l0 = op0len - 1;
     952     96637366 :   int l1 = op1len - 1;
     953     96637366 :   bool need_canon = true;
     954              : 
     955     96637366 :   unsigned int len = MAX (op0len, op1len);
     956     96637366 :   if (l0 > l1)
     957              :     {
     958     13805688 :       HOST_WIDE_INT op1mask = -top_bit_of (op1, op1len, prec);
     959     13805688 :       if (op1mask != 0)
     960              :         {
     961     96637366 :           l0 = l1;
     962     96637366 :           len = l1 + 1;
     963              :         }
     964              :       else
     965              :         {
     966     27017899 :           need_canon = false;
     967     27017899 :           while (l0 > l1)
     968              :             {
     969     13562419 :               val[l0] = op0[l0];
     970     13562419 :               l0--;
     971              :             }
     972              :         }
     973              :     }
     974     82831678 :   else if (l1 > l0)
     975              :     {
     976     81659568 :       HOST_WIDE_INT op0mask = -top_bit_of (op0, op0len, prec);
     977     81659568 :       if (op0mask == 0)
     978              :         len = l0 + 1;
     979              :       else
     980              :         {
     981       140527 :           need_canon = false;
     982       140527 :           while (l1 > l0)
     983              :             {
     984        71186 :               val[l1] = ~op1[l1];
     985        71186 :               l1--;
     986              :             }
     987              :         }
     988              :     }
     989              : 
     990    194608542 :   while (l0 >= 0)
     991              :     {
     992     97971176 :       val[l0] = op0[l0] & ~op1[l0];
     993     97971176 :       l0--;
     994              :     }
     995              : 
     996     96637366 :   if (need_canon)
     997     83112545 :     len = canonize (val, len, prec);
     998              : 
     999     96637366 :   return len;
    1000     96637366 : }
    1001              : 
    1002              : /* Set VAL to OP0 | OP1.  Return the number of blocks used.  */
    1003              : unsigned int
    1004    186844019 : wi::or_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *op0,
    1005              :               unsigned int op0len, const HOST_WIDE_INT *op1,
    1006              :               unsigned int op1len, unsigned int prec)
    1007              : {
    1008    186844019 :   wide_int result;
    1009    186844019 :   int l0 = op0len - 1;
    1010    186844019 :   int l1 = op1len - 1;
    1011    186844019 :   bool need_canon = true;
    1012              : 
    1013    186844019 :   unsigned int len = MAX (op0len, op1len);
    1014    186844019 :   if (l0 > l1)
    1015              :     {
    1016     65461426 :       HOST_WIDE_INT op1mask = -top_bit_of (op1, op1len, prec);
    1017     65461426 :       if (op1mask != 0)
    1018              :         {
    1019    186844019 :           l0 = l1;
    1020    186844019 :           len = l1 + 1;
    1021              :         }
    1022              :       else
    1023              :         {
    1024    231593585 :           need_canon = false;
    1025    231593585 :           while (l0 > l1)
    1026              :             {
    1027    166472012 :               val[l0] = op0[l0];
    1028    166472012 :               l0--;
    1029              :             }
    1030              :         }
    1031              :     }
    1032    121382593 :   else if (l1 > l0)
    1033              :     {
    1034     69932123 :       HOST_WIDE_INT op0mask = -top_bit_of (op0, op0len, prec);
    1035     69932123 :       if (op0mask != 0)
    1036              :         len = l0 + 1;
    1037              :       else
    1038              :         {
    1039    300054553 :           need_canon = false;
    1040    300054553 :           while (l1 > l0)
    1041              :             {
    1042    233993587 :               val[l1] = op1[l1];
    1043    233993587 :               l1--;
    1044              :             }
    1045              :         }
    1046              :     }
    1047              : 
    1048    593460251 :   while (l0 >= 0)
    1049              :     {
    1050    406616232 :       val[l0] = op0[l0] | op1[l0];
    1051    406616232 :       l0--;
    1052              :     }
    1053              : 
    1054    186844019 :   if (need_canon)
    1055     55661480 :     len = canonize (val, len, prec);
    1056              : 
    1057    186844019 :   return len;
    1058    186844019 : }
    1059              : 
    1060              : /* Set VAL to OP0 | ~OP1.  Return the number of blocks used.  */
    1061              : unsigned int
    1062            0 : wi::or_not_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *op0,
    1063              :                   unsigned int op0len, const HOST_WIDE_INT *op1,
    1064              :                   unsigned int op1len, unsigned int prec)
    1065              : {
    1066            0 :   wide_int result;
    1067            0 :   int l0 = op0len - 1;
    1068            0 :   int l1 = op1len - 1;
    1069            0 :   bool need_canon = true;
    1070              : 
    1071            0 :   unsigned int len = MAX (op0len, op1len);
    1072            0 :   if (l0 > l1)
    1073              :     {
    1074            0 :       HOST_WIDE_INT op1mask = -top_bit_of (op1, op1len, prec);
    1075            0 :       if (op1mask == 0)
    1076              :         {
    1077            0 :           l0 = l1;
    1078            0 :           len = l1 + 1;
    1079              :         }
    1080              :       else
    1081              :         {
    1082            0 :           need_canon = false;
    1083            0 :           while (l0 > l1)
    1084              :             {
    1085            0 :               val[l0] = op0[l0];
    1086            0 :               l0--;
    1087              :             }
    1088              :         }
    1089              :     }
    1090            0 :   else if (l1 > l0)
    1091              :     {
    1092            0 :       HOST_WIDE_INT op0mask = -top_bit_of (op0, op0len, prec);
    1093            0 :       if (op0mask != 0)
    1094              :         len = l0 + 1;
    1095              :       else
    1096              :         {
    1097            0 :           need_canon = false;
    1098            0 :           while (l1 > l0)
    1099              :             {
    1100            0 :               val[l1] = ~op1[l1];
    1101            0 :               l1--;
    1102              :             }
    1103              :         }
    1104              :     }
    1105              : 
    1106            0 :   while (l0 >= 0)
    1107              :     {
    1108            0 :       val[l0] = op0[l0] | ~op1[l0];
    1109            0 :       l0--;
    1110              :     }
    1111              : 
    1112            0 :   if (need_canon)
    1113            0 :     len = canonize (val, len, prec);
    1114              : 
    1115            0 :   return len;
    1116            0 : }
    1117              : 
    1118              : /* Set VAL to OP0 ^ OP1.  Return the number of blocks used.  */
    1119              : unsigned int
    1120     38344044 : wi::xor_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *op0,
    1121              :                unsigned int op0len, const HOST_WIDE_INT *op1,
    1122              :                unsigned int op1len, unsigned int prec)
    1123              : {
    1124     38344044 :   wide_int result;
    1125     38344044 :   int l0 = op0len - 1;
    1126     38344044 :   int l1 = op1len - 1;
    1127              : 
    1128     38344044 :   unsigned int len = MAX (op0len, op1len);
    1129     38344044 :   if (l0 > l1)
    1130              :     {
    1131      8331475 :       HOST_WIDE_INT op1mask = -top_bit_of (op1, op1len, prec);
    1132     16792117 :       while (l0 > l1)
    1133              :         {
    1134      8460642 :           val[l0] = op0[l0] ^ op1mask;
    1135      8460642 :           l0--;
    1136              :         }
    1137              :     }
    1138              : 
    1139     38344044 :   if (l1 > l0)
    1140              :     {
    1141     24043690 :       HOST_WIDE_INT op0mask = -top_bit_of (op0, op0len, prec);
    1142    115318835 :       while (l1 > l0)
    1143              :         {
    1144     91275145 :           val[l1] = op0mask ^ op1[l1];
    1145     91275145 :           l1--;
    1146              :         }
    1147              :     }
    1148              : 
    1149    150380155 :   while (l0 >= 0)
    1150              :     {
    1151    112036111 :       val[l0] = op0[l0] ^ op1[l0];
    1152    112036111 :       l0--;
    1153              :     }
    1154              : 
    1155     38344044 :   return canonize (val, len, prec);
    1156     38344044 : }
    1157              : 
    1158              : /*
    1159              :  * math
    1160              :  */
    1161              : 
    1162              : /* Set VAL to OP0 + OP1.  If OVERFLOW is nonnull, record in *OVERFLOW
    1163              :    whether the result overflows when OP0 and OP1 are treated as having
    1164              :    signedness SGN.  Return the number of blocks in VAL.  */
    1165              : unsigned int
    1166    134260921 : wi::add_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *op0,
    1167              :                unsigned int op0len, const HOST_WIDE_INT *op1,
    1168              :                unsigned int op1len, unsigned int prec,
    1169              :                signop sgn, wi::overflow_type *overflow)
    1170              : {
    1171    134260921 :   unsigned HOST_WIDE_INT o0 = 0;
    1172    134260921 :   unsigned HOST_WIDE_INT o1 = 0;
    1173    134260921 :   unsigned HOST_WIDE_INT x = 0;
    1174    134260921 :   unsigned HOST_WIDE_INT carry = 0;
    1175    134260921 :   unsigned HOST_WIDE_INT old_carry = 0;
    1176    134260921 :   unsigned HOST_WIDE_INT mask0, mask1;
    1177    134260921 :   unsigned int i;
    1178              : 
    1179    134260921 :   unsigned int len = MAX (op0len, op1len);
    1180    134260921 :   mask0 = -top_bit_of (op0, op0len, prec);
    1181    134260921 :   mask1 = -top_bit_of (op1, op1len, prec);
    1182              :   /* Add all of the explicitly defined elements.  */
    1183              : 
    1184    416085494 :   for (i = 0; i < len; i++)
    1185              :     {
    1186    281824573 :       o0 = i < op0len ? (unsigned HOST_WIDE_INT) op0[i] : mask0;
    1187    281824573 :       o1 = i < op1len ? (unsigned HOST_WIDE_INT) op1[i] : mask1;
    1188    281824573 :       x = o0 + o1 + carry;
    1189    281824573 :       val[i] = x;
    1190    281824573 :       old_carry = carry;
    1191    281824573 :       carry = carry == 0 ? x < o0 : x <= o0;
    1192              :     }
    1193              : 
    1194    134260921 :   if (len * HOST_BITS_PER_WIDE_INT < prec)
    1195              :     {
    1196    128759274 :       val[len] = mask0 + mask1 + carry;
    1197    128759274 :       len++;
    1198    128759274 :       if (overflow)
    1199     59280832 :         *overflow
    1200    118487120 :           = (sgn == UNSIGNED && carry) ? wi::OVF_OVERFLOW : wi::OVF_NONE;
    1201              :     }
    1202      5501647 :   else if (overflow)
    1203              :     {
    1204       534830 :       unsigned int shift = -prec % HOST_BITS_PER_WIDE_INT;
    1205       534830 :       if (sgn == SIGNED)
    1206              :         {
    1207       202365 :           unsigned HOST_WIDE_INT x = (val[len - 1] ^ o0) & (val[len - 1] ^ o1);
    1208       202365 :           if ((HOST_WIDE_INT) (x << shift) < 0)
    1209              :             {
    1210        38104 :               if (o0 > (unsigned HOST_WIDE_INT) val[len - 1])
    1211        12083 :                 *overflow = wi::OVF_UNDERFLOW;
    1212        26021 :               else if (o0 < (unsigned HOST_WIDE_INT) val[len - 1])
    1213        26021 :                 *overflow = wi::OVF_OVERFLOW;
    1214              :               else
    1215            0 :                 *overflow = wi::OVF_NONE;
    1216              :             }
    1217              :           else
    1218       164261 :             *overflow = wi::OVF_NONE;
    1219              :         }
    1220              :       else
    1221              :         {
    1222              :           /* Put the MSB of X and O0 and in the top of the HWI.  */
    1223       332465 :           x <<= shift;
    1224       332465 :           o0 <<= shift;
    1225       332465 :           if (old_carry)
    1226       197710 :             *overflow = (x <= o0) ? wi::OVF_OVERFLOW : wi::OVF_NONE;
    1227              :           else
    1228       455542 :             *overflow = (x < o0) ? wi::OVF_OVERFLOW : wi::OVF_NONE;
    1229              :         }
    1230              :     }
    1231              : 
    1232    134260921 :   return canonize (val, len, prec);
    1233              : }
    1234              : 
    1235              : /* Subroutines of the multiplication and division operations.  Unpack
    1236              :    the first IN_LEN HOST_WIDE_INTs in INPUT into 2 * IN_LEN
    1237              :    HOST_HALF_WIDE_INTs of RESULT.  The rest of RESULT is filled by
    1238              :    uncompressing the top bit of INPUT[IN_LEN - 1].  */
    1239              : static void
    1240     77239016 : wi_unpack (unsigned HOST_HALF_WIDE_INT *result, const HOST_WIDE_INT *input,
    1241              :            unsigned int in_len, unsigned int out_len,
    1242              :            unsigned int prec, signop sgn)
    1243              : {
    1244     77239016 :   unsigned int i;
    1245     77239016 :   unsigned int j = 0;
    1246     77239016 :   unsigned int small_prec = prec & (HOST_BITS_PER_WIDE_INT - 1);
    1247     77239016 :   unsigned int blocks_needed = BLOCKS_NEEDED (prec);
    1248     77239016 :   HOST_WIDE_INT mask;
    1249              : 
    1250     77239016 :   if (sgn == SIGNED)
    1251              :     {
    1252            0 :       mask = -top_bit_of ((const HOST_WIDE_INT *) input, in_len, prec);
    1253            0 :       mask &= HALF_INT_MASK;
    1254              :     }
    1255              :   else
    1256              :     mask = 0;
    1257              : 
    1258    163474214 :   for (i = 0; i < blocks_needed - 1; i++)
    1259              :     {
    1260     86235198 :       HOST_WIDE_INT x = safe_uhwi (input, in_len, i);
    1261     86235198 :       result[j++] = x;
    1262     86235198 :       result[j++] = x >> HOST_BITS_PER_HALF_WIDE_INT;
    1263              :     }
    1264              : 
    1265     77239016 :   HOST_WIDE_INT x = safe_uhwi (input, in_len, i);
    1266     77239016 :   if (small_prec)
    1267              :     {
    1268        29821 :       if (sgn == SIGNED)
    1269            0 :         x = sext_hwi (x, small_prec);
    1270              :       else
    1271        29821 :         x = zext_hwi (x, small_prec);
    1272              :     }
    1273     77239016 :   result[j++] = x;
    1274     77239016 :   result[j++] = x >> HOST_BITS_PER_HALF_WIDE_INT;
    1275              : 
    1276              :   /* Smear the sign bit.  */
    1277     77239016 :   while (j < out_len)
    1278            0 :     result[j++] = mask;
    1279     77239016 : }
    1280              : 
    1281              : /* The inverse of wi_unpack.  IN_LEN is the number of input
    1282              :    blocks and PRECISION is the precision of the result.  Return the
    1283              :    number of blocks in the canonicalized result.  */
    1284              : static unsigned int
    1285     41101451 : wi_pack (HOST_WIDE_INT *result,
    1286              :          const unsigned HOST_HALF_WIDE_INT *input,
    1287              :          unsigned int in_len, unsigned int precision)
    1288              : {
    1289     41101451 :   unsigned int i = 0;
    1290     41101451 :   unsigned int j = 0;
    1291     41101451 :   unsigned int blocks_needed = BLOCKS_NEEDED (precision);
    1292              : 
    1293    123384082 :   while (i + 1 < in_len)
    1294              :     {
    1295     82282631 :       result[j++] = ((unsigned HOST_WIDE_INT) input[i]
    1296     82282631 :                      | ((unsigned HOST_WIDE_INT) input[i + 1]
    1297     82282631 :                         << HOST_BITS_PER_HALF_WIDE_INT));
    1298     82282631 :       i += 2;
    1299              :     }
    1300              : 
    1301              :   /* Handle the case where in_len is odd.   For this we zero extend.  */
    1302     41101451 :   if (in_len & 1)
    1303      3163561 :     result[j++] = (unsigned HOST_WIDE_INT) input[i];
    1304     37937890 :   else if (j < blocks_needed)
    1305      1941319 :     result[j++] = 0;
    1306     41101451 :   return canonize (result, j, precision);
    1307              : }
    1308              : 
    1309              : /* Multiply Op1 by Op2.  If HIGH is set, only the upper half of the
    1310              :    result is returned.
    1311              : 
    1312              :    If HIGH is not set, throw away the upper half after the check is
    1313              :    made to see if it overflows.  Unfortunately there is no better way
    1314              :    to check for overflow than to do this.  If OVERFLOW is nonnull,
    1315              :    record in *OVERFLOW whether the result overflowed.  SGN controls
    1316              :    the signedness and is used to check overflow or if HIGH is set.
    1317              : 
    1318              :    NOTE: Overflow type for signed overflow is not yet implemented.  */
    1319              : unsigned int
    1320   1639843502 : wi::mul_internal (HOST_WIDE_INT *val, const HOST_WIDE_INT *op1val,
    1321              :                   unsigned int op1len, const HOST_WIDE_INT *op2val,
    1322              :                   unsigned int op2len, unsigned int prec, signop sgn,
    1323              :                   wi::overflow_type *overflow, bool high)
    1324              : {
    1325   1639843502 :   unsigned HOST_WIDE_INT o0, o1, k, t;
    1326   1639843502 :   unsigned int i;
    1327   1639843502 :   unsigned int j;
    1328              : 
    1329              :   /* If the top level routine did not really pass in an overflow, then
    1330              :      just make sure that we never attempt to set it.  */
    1331   1639843502 :   bool needs_overflow = (overflow != 0);
    1332   1639843502 :   if (needs_overflow)
    1333    478245585 :     *overflow = wi::OVF_NONE;
    1334              : 
    1335   1639843502 :   wide_int_ref op1 = wi::storage_ref (op1val, op1len, prec);
    1336   1639843502 :   wide_int_ref op2 = wi::storage_ref (op2val, op2len, prec);
    1337              : 
    1338              :   /* This is a surprisingly common case, so do it first.  */
    1339   1639843502 :   if (op1 == 0 || op2 == 0)
    1340              :     {
    1341    634545081 :       val[0] = 0;
    1342    634545081 :       return 1;
    1343              :     }
    1344              : 
    1345              : #ifdef umul_ppmm
    1346   1005298421 :   if (sgn == UNSIGNED)
    1347              :     {
    1348              :       /* If the inputs are single HWIs and the output has room for at
    1349              :          least two HWIs, we can use umul_ppmm directly.  */
    1350    975472774 :       if (prec >= HOST_BITS_PER_WIDE_INT * 2
    1351    908203955 :           && wi::fits_uhwi_p (op1)
    1352   1849377478 :           && wi::fits_uhwi_p (op2))
    1353              :         {
    1354              :           /* This case never overflows.  */
    1355    872624091 :           if (high)
    1356              :             {
    1357            0 :               val[0] = 0;
    1358            0 :               return 1;
    1359              :             }
    1360    872624091 :           umul_ppmm (val[1], val[0], op1.ulow (), op2.ulow ());
    1361    872624091 :           if (val[1] < 0 && prec > HOST_BITS_PER_WIDE_INT * 2)
    1362              :             {
    1363       196433 :               val[2] = 0;
    1364       196433 :               return 3;
    1365              :             }
    1366    885459399 :           return 1 + (val[1] != 0 || val[0] < 0);
    1367              :         }
    1368              :       /* Likewise if the output is a full single HWI, except that the
    1369              :          upper HWI of the result is only used for determining overflow.
    1370              :          (We handle this case inline when overflow isn't needed.)  */
    1371    102848683 :       else if (prec == HOST_BITS_PER_WIDE_INT)
    1372              :         {
    1373     56613254 :           unsigned HOST_WIDE_INT upper;
    1374     56613254 :           umul_ppmm (upper, val[0], op1.ulow (), op2.ulow ());
    1375     56613254 :           if (needs_overflow)
    1376              :             /* Unsigned overflow can only be +OVERFLOW.  */
    1377    110206355 :             *overflow = (upper != 0) ? wi::OVF_OVERFLOW : wi::OVF_NONE;
    1378     56613254 :           if (high)
    1379           28 :             val[0] = upper;
    1380              :           return 1;
    1381              :         }
    1382              :     }
    1383              : #endif
    1384              : 
    1385              :   /* Handle multiplications by 1.  */
    1386     76061076 :   if (op1 == 1)
    1387              :     {
    1388      9079424 :       if (high)
    1389              :         {
    1390          240 :           val[0] = wi::neg_p (op2, sgn) ? -1 : 0;
    1391          240 :           return 1;
    1392              :         }
    1393     18191420 :       for (i = 0; i < op2len; i++)
    1394      9112236 :         val[i] = op2val[i];
    1395              :       return op2len;
    1396              :     }
    1397     66981652 :   if (op2 == 1)
    1398              :     {
    1399     17523355 :       if (high)
    1400              :         {
    1401            0 :           val[0] = wi::neg_p (op1, sgn) ? -1 : 0;
    1402            0 :           return 1;
    1403              :         }
    1404     35144361 :       for (i = 0; i < op1len; i++)
    1405     17621006 :         val[i] = op1val[i];
    1406              :       return op1len;
    1407              :     }
    1408              : 
    1409              :   /* If we need to check for overflow, we can only do half wide
    1410              :      multiplies quickly because we need to look at the top bits to
    1411              :      check for the overflow.  */
    1412     49458297 :   if ((high || needs_overflow)
    1413     28292387 :       && (prec <= HOST_BITS_PER_HALF_WIDE_INT))
    1414              :     {
    1415     13573431 :       unsigned HOST_WIDE_INT r;
    1416              : 
    1417     13573431 :       if (sgn == SIGNED)
    1418              :         {
    1419      6568264 :           o0 = op1.to_shwi ();
    1420      6568264 :           o1 = op2.to_shwi ();
    1421              :         }
    1422              :       else
    1423              :         {
    1424      7005167 :           o0 = op1.to_uhwi ();
    1425      7005167 :           o1 = op2.to_uhwi ();
    1426              :         }
    1427              : 
    1428     13573431 :       r = o0 * o1;
    1429     13573431 :       if (needs_overflow)
    1430              :         {
    1431     13567786 :           if (sgn == SIGNED)
    1432              :             {
    1433      6563284 :               if ((HOST_WIDE_INT) r != sext_hwi (r, prec))
    1434              :                 /* FIXME: Signed overflow type is not implemented yet.  */
    1435      2569074 :                 *overflow = OVF_UNKNOWN;
    1436              :             }
    1437              :           else
    1438              :             {
    1439      7004502 :               if ((r >> prec) != 0)
    1440              :                 /* Unsigned overflow can only be +OVERFLOW.  */
    1441      4580072 :                 *overflow = OVF_OVERFLOW;
    1442              :             }
    1443              :         }
    1444     13573431 :       val[0] = high ? r >> prec : r;
    1445     13573431 :       return 1;
    1446              :     }
    1447              : 
    1448              :   /* The sizes here are scaled to support a 2x WIDE_INT_MAX_INL_PRECISION by 2x
    1449              :      WIDE_INT_MAX_INL_PRECISION yielding a 4x WIDE_INT_MAX_INL_PRECISION
    1450              :      result.  */
    1451              : 
    1452     14718956 :   unsigned HOST_HALF_WIDE_INT
    1453              :     ubuf[4 * WIDE_INT_MAX_INL_PRECISION / HOST_BITS_PER_HALF_WIDE_INT];
    1454     14718956 :   unsigned HOST_HALF_WIDE_INT
    1455              :     vbuf[4 * WIDE_INT_MAX_INL_PRECISION / HOST_BITS_PER_HALF_WIDE_INT];
    1456              :   /* The '2' in 'R' is because we are internally doing a full
    1457              :      multiply.  */
    1458     14718956 :   unsigned HOST_HALF_WIDE_INT
    1459              :     rbuf[2 * 4 * WIDE_INT_MAX_INL_PRECISION / HOST_BITS_PER_HALF_WIDE_INT];
    1460     50603816 :   const HOST_WIDE_INT mask
    1461              :     = (HOST_WIDE_INT_1 << HOST_BITS_PER_HALF_WIDE_INT) - 1;
    1462     50603816 :   unsigned HOST_HALF_WIDE_INT *u = ubuf;
    1463     50603816 :   unsigned HOST_HALF_WIDE_INT *v = vbuf;
    1464     50603816 :   unsigned HOST_HALF_WIDE_INT *r = rbuf;
    1465              : 
    1466     14718956 :   if (!high)
    1467     35884860 :     prec = MIN ((op1len + op2len + 1) * HOST_BITS_PER_WIDE_INT, prec);
    1468     35884866 :   unsigned int blocks_needed = BLOCKS_NEEDED (prec);
    1469     35884866 :   unsigned int half_blocks_needed = blocks_needed * 2;
    1470     35884866 :   if (UNLIKELY (prec > WIDE_INT_MAX_INL_PRECISION))
    1471              :     {
    1472        31582 :       unsigned HOST_HALF_WIDE_INT *buf
    1473        31582 :         = XALLOCAVEC (unsigned HOST_HALF_WIDE_INT, 4 * half_blocks_needed);
    1474        31582 :       u = buf;
    1475        31582 :       v = u + half_blocks_needed;
    1476        31582 :       r = v + half_blocks_needed;
    1477              :     }
    1478              : 
    1479              :   /* We do unsigned mul and then correct it.  */
    1480     35884866 :   wi_unpack (u, op1val, op1len, half_blocks_needed, prec, UNSIGNED);
    1481     35884866 :   wi_unpack (v, op2val, op2len, half_blocks_needed, prec, UNSIGNED);
    1482              : 
    1483              :   /* The 2 is for a full mult.  */
    1484     35884866 :   memset (r, 0, half_blocks_needed * 2
    1485     35884866 :           * HOST_BITS_PER_HALF_WIDE_INT / CHAR_BIT);
    1486              : 
    1487    190971310 :   for (j = 0; j < half_blocks_needed; j++)
    1488              :     {
    1489              :       k = 0;
    1490  11553156132 :       for (i = 0; i < half_blocks_needed; i++)
    1491              :         {
    1492  11398069688 :           t = ((unsigned HOST_WIDE_INT)u[i] * (unsigned HOST_WIDE_INT)v[j]
    1493  11398069688 :                + r[i + j] + k);
    1494  11398069688 :           r[i + j] = t & HALF_INT_MASK;
    1495  11398069688 :           k = t >> HOST_BITS_PER_HALF_WIDE_INT;
    1496              :         }
    1497    155086444 :       r[j + half_blocks_needed] = k;
    1498              :     }
    1499              : 
    1500     35884866 :   unsigned int shift;
    1501     35884866 :   if ((high || needs_overflow) && (shift = prec % HOST_BITS_PER_WIDE_INT) != 0)
    1502              :     {
    1503              :       /* The high or needs_overflow code assumes that the high bits
    1504              :          only appear from r[half_blocks_needed] up to
    1505              :          r[half_blocks_needed * 2 - 1].  If prec is not a multiple
    1506              :          of HOST_BITS_PER_WIDE_INT, shift the bits above prec up
    1507              :          to make that code simple.  */
    1508         3066 :       if (shift == HOST_BITS_PER_HALF_WIDE_INT)
    1509            4 :         memmove (&r[half_blocks_needed], &r[half_blocks_needed - 1],
    1510            4 :                  sizeof (r[0]) * half_blocks_needed);
    1511              :       else
    1512              :         {
    1513         3062 :           unsigned int skip = shift < HOST_BITS_PER_HALF_WIDE_INT;
    1514         3062 :           if (!skip)
    1515         2669 :             shift -= HOST_BITS_PER_HALF_WIDE_INT;
    1516        94270 :           for (i = 2 * half_blocks_needed - 1; i >= half_blocks_needed; i--)
    1517        91208 :             r[i] = ((r[i - skip] << (-shift % HOST_BITS_PER_HALF_WIDE_INT))
    1518        91208 :                     | (r[i - skip - 1] >> shift));
    1519              :         }
    1520              :     }
    1521              : 
    1522              :   /* We did unsigned math above.  For signed we must adjust the
    1523              :      product (assuming we need to see that).  */
    1524     35884866 :   if (sgn == SIGNED && (high || needs_overflow))
    1525              :     {
    1526     14629327 :       unsigned HOST_WIDE_INT b;
    1527     14629327 :       if (wi::neg_p (op1))
    1528              :         {
    1529              :           b = 0;
    1530     21640597 :           for (i = 0; i < half_blocks_needed; i++)
    1531              :             {
    1532     14486316 :               t = (unsigned HOST_WIDE_INT)r[i + half_blocks_needed]
    1533     14486316 :                 - (unsigned HOST_WIDE_INT)v[i] - b;
    1534     14486316 :               r[i + half_blocks_needed] = t & HALF_INT_MASK;
    1535     14486316 :               b = t >> (HOST_BITS_PER_WIDE_INT - 1);
    1536              :             }
    1537              :         }
    1538     14629327 :       if (wi::neg_p (op2))
    1539              :         {
    1540              :           b = 0;
    1541      5917666 :           for (i = 0; i < half_blocks_needed; i++)
    1542              :             {
    1543      3956008 :               t = (unsigned HOST_WIDE_INT)r[i + half_blocks_needed]
    1544      3956008 :                 - (unsigned HOST_WIDE_INT)u[i] - b;
    1545      3956008 :               r[i + half_blocks_needed] = t & HALF_INT_MASK;
    1546      3956008 :               b = t >> (HOST_BITS_PER_WIDE_INT - 1);
    1547              :             }
    1548              :         }
    1549              :     }
    1550              : 
    1551     35884866 :   if (needs_overflow)
    1552              :     {
    1553     14718950 :       HOST_WIDE_INT top;
    1554              : 
    1555              :       /* For unsigned, overflow is true if any of the top bits are set.
    1556              :          For signed, overflow is true if any of the top bits are not equal
    1557              :          to the sign bit.  */
    1558     14718950 :       if (sgn == UNSIGNED)
    1559              :         top = 0;
    1560              :       else
    1561              :         {
    1562     14629321 :           top = r[half_blocks_needed - 1
    1563     14629321 :                   - ((-prec % HOST_BITS_PER_WIDE_INT)
    1564     14629321 :                      >= HOST_BITS_PER_HALF_WIDE_INT)];
    1565     14629321 :           top = SIGN_MASK (((unsigned HOST_WIDE_INT) top)
    1566              :                            << (HOST_BITS_PER_WIDE_INT / 2
    1567              :                                + (-prec % HOST_BITS_PER_HALF_WIDE_INT)));
    1568     14629321 :           top &= mask;
    1569              :         }
    1570              : 
    1571     14718950 :       unsigned int end = half_blocks_needed * 2;
    1572     14718950 :       shift = prec % HOST_BITS_PER_WIDE_INT;
    1573     14718950 :       if (shift)
    1574              :         {
    1575              :           /* For overflow checking only look at the first prec bits
    1576              :              starting with r[half_blocks_needed].  */
    1577         3066 :           if (shift <= HOST_BITS_PER_HALF_WIDE_INT)
    1578          397 :             --end;
    1579         3066 :           shift %= HOST_BITS_PER_HALF_WIDE_INT;
    1580         3066 :           if (shift)
    1581              :             {
    1582         3062 :               if (top)
    1583         1333 :                 r[end - 1] |= ((~(unsigned HOST_HALF_WIDE_INT) 0) << shift);
    1584              :               else
    1585         1729 :                 r[end - 1] &= (((unsigned HOST_HALF_WIDE_INT) 1) << shift) - 1;
    1586              :             }
    1587              :         }
    1588     53900869 :       for (i = half_blocks_needed; i < end; i++)
    1589     39181919 :         if (((HOST_WIDE_INT)(r[i] & mask)) != top)
    1590              :           /* FIXME: Signed overflow type is not implemented yet.  */
    1591     18708623 :           *overflow = (sgn == UNSIGNED) ? wi::OVF_OVERFLOW : wi::OVF_UNKNOWN;
    1592              :     }
    1593              : 
    1594     35884866 :   int r_offset = high ? half_blocks_needed : 0;
    1595     35884866 :   return wi_pack (val, &r[r_offset], half_blocks_needed, prec);
    1596              : }
    1597              : 
    1598              : /* Compute the population count of X.  */
    1599              : int
    1600    152041023 : wi::popcount (const wide_int_ref &x)
    1601              : {
    1602    152041023 :   unsigned int i;
    1603    152041023 :   int count;
    1604              : 
    1605              :   /* The high order block is special if it is the last block and the
    1606              :      precision is not an even multiple of HOST_BITS_PER_WIDE_INT.  We
    1607              :      have to clear out any ones above the precision before doing
    1608              :      popcount on this block.  */
    1609    152041023 :   count = x.precision - x.len * HOST_BITS_PER_WIDE_INT;
    1610    152041023 :   unsigned int stop = x.len;
    1611    152041023 :   if (count < 0)
    1612              :     {
    1613     64456843 :       count = popcount_hwi (x.uhigh () << -count);
    1614     64456843 :       stop -= 1;
    1615              :     }
    1616              :   else
    1617              :     {
    1618     87584180 :       if (x.sign_mask () >= 0)
    1619     71908667 :         count = 0;
    1620              :     }
    1621              : 
    1622    240282129 :   for (i = 0; i < stop; ++i)
    1623     88241106 :     count += popcount_hwi (x.val[i]);
    1624              : 
    1625    152041023 :   return count;
    1626              : }
    1627              : 
    1628              : /* Set VAL to OP0 - OP1.  If OVERFLOW is nonnull, record in *OVERFLOW
    1629              :    whether the result overflows when OP0 and OP1 are treated as having
    1630              :    signedness SGN.  Return the number of blocks in VAL.  */
    1631              : unsigned int
    1632     60890549 : wi::sub_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *op0,
    1633              :                unsigned int op0len, const HOST_WIDE_INT *op1,
    1634              :                unsigned int op1len, unsigned int prec,
    1635              :                signop sgn, wi::overflow_type *overflow)
    1636              : {
    1637     60890549 :   unsigned HOST_WIDE_INT o0 = 0;
    1638     60890549 :   unsigned HOST_WIDE_INT o1 = 0;
    1639     60890549 :   unsigned HOST_WIDE_INT x = 0;
    1640              :   /* We implement subtraction as an in place negate and add.  Negation
    1641              :      is just inversion and add 1, so we can do the add of 1 by just
    1642              :      starting the borrow in of the first element at 1.  */
    1643     60890549 :   unsigned HOST_WIDE_INT borrow = 0;
    1644     60890549 :   unsigned HOST_WIDE_INT old_borrow = 0;
    1645              : 
    1646     60890549 :   unsigned HOST_WIDE_INT mask0, mask1;
    1647     60890549 :   unsigned int i;
    1648              : 
    1649     60890549 :   unsigned int len = MAX (op0len, op1len);
    1650     60890549 :   mask0 = -top_bit_of (op0, op0len, prec);
    1651     60890549 :   mask1 = -top_bit_of (op1, op1len, prec);
    1652              : 
    1653              :   /* Subtract all of the explicitly defined elements.  */
    1654    182821889 :   for (i = 0; i < len; i++)
    1655              :     {
    1656    121931340 :       o0 = i < op0len ? (unsigned HOST_WIDE_INT)op0[i] : mask0;
    1657    121931340 :       o1 = i < op1len ? (unsigned HOST_WIDE_INT)op1[i] : mask1;
    1658    121931340 :       x = o0 - o1 - borrow;
    1659    121931340 :       val[i] = x;
    1660    121931340 :       old_borrow = borrow;
    1661    121931340 :       borrow = borrow == 0 ? o0 < o1 : o0 <= o1;
    1662              :     }
    1663              : 
    1664     60890549 :   if (len * HOST_BITS_PER_WIDE_INT < prec)
    1665              :     {
    1666     59477401 :       val[len] = mask0 - mask1 - borrow;
    1667     59477401 :       len++;
    1668     59477401 :       if (overflow)
    1669      1422581 :         *overflow = (sgn == UNSIGNED && borrow) ? OVF_UNDERFLOW : OVF_NONE;
    1670              :     }
    1671      1413148 :   else if (overflow)
    1672              :     {
    1673       373678 :       unsigned int shift = -prec % HOST_BITS_PER_WIDE_INT;
    1674       373678 :       if (sgn == SIGNED)
    1675              :         {
    1676       274521 :           unsigned HOST_WIDE_INT x = (o0 ^ o1) & (val[len - 1] ^ o0);
    1677       274521 :           if ((HOST_WIDE_INT) (x << shift) < 0)
    1678              :             {
    1679        30578 :               if (o0 > o1)
    1680        16695 :                 *overflow = OVF_UNDERFLOW;
    1681        13883 :               else if (o0 < o1)
    1682        13883 :                 *overflow = OVF_OVERFLOW;
    1683              :               else
    1684            0 :                 *overflow = OVF_NONE;
    1685              :             }
    1686              :           else
    1687       243943 :             *overflow = OVF_NONE;
    1688              :         }
    1689              :       else
    1690              :         {
    1691              :           /* Put the MSB of X and O0 and in the top of the HWI.  */
    1692        99157 :           x <<= shift;
    1693        99157 :           o0 <<= shift;
    1694        99157 :           if (old_borrow)
    1695       111619 :             *overflow = (x >= o0) ? OVF_UNDERFLOW : OVF_NONE;
    1696              :           else
    1697        65843 :             *overflow = (x > o0) ? OVF_UNDERFLOW : OVF_NONE;
    1698              :         }
    1699              :     }
    1700              : 
    1701     60890549 :   return canonize (val, len, prec);
    1702              : }
    1703              : 
    1704              : 
    1705              : /*
    1706              :  * Division and Mod
    1707              :  */
    1708              : 
    1709              : /* Compute B_QUOTIENT and B_REMAINDER from B_DIVIDEND/B_DIVISOR.  The
    1710              :    algorithm is a small modification of the algorithm in Hacker's
    1711              :    Delight by Warren, which itself is a small modification of Knuth's
    1712              :    algorithm.  M is the number of significant elements of U however
    1713              :    there needs to be at least one extra element of B_DIVIDEND
    1714              :    allocated, N is the number of elements of B_DIVISOR.
    1715              :    Return new value for N.  */
    1716              : static int
    1717      2734642 : divmod_internal_2 (unsigned HOST_HALF_WIDE_INT *b_quotient,
    1718              :                    unsigned HOST_HALF_WIDE_INT *b_remainder,
    1719              :                    unsigned HOST_HALF_WIDE_INT *b_dividend,
    1720              :                    unsigned HOST_HALF_WIDE_INT *b_divisor,
    1721              :                    int m, int n)
    1722              : {
    1723              :   /* The "digits" are a HOST_HALF_WIDE_INT which the size of half of a
    1724              :      HOST_WIDE_INT and stored in the lower bits of each word.  This
    1725              :      algorithm should work properly on both 32 and 64 bit
    1726              :      machines.  */
    1727      2734642 :   unsigned HOST_WIDE_INT b = HOST_WIDE_INT_1U << HOST_BITS_PER_HALF_WIDE_INT;
    1728      2734642 :   unsigned HOST_WIDE_INT qhat;   /* Estimate of quotient digit.  */
    1729      2734642 :   unsigned HOST_WIDE_INT rhat;   /* A remainder.  */
    1730      2734642 :   unsigned HOST_WIDE_INT p;      /* Product of two digits.  */
    1731      2734642 :   HOST_WIDE_INT t, k;
    1732      2734642 :   int i, j, s;
    1733              : 
    1734              :   /* Single digit divisor.  */
    1735      2734642 :   if (n == 1)
    1736              :     {
    1737       837080 :       k = 0;
    1738      3454214 :       for (j = m - 1; j >= 0; j--)
    1739              :         {
    1740      2617134 :           b_quotient[j] = (k * b + b_dividend[j])/b_divisor[0];
    1741      2617134 :           k = ((k * b + b_dividend[j])
    1742      2617134 :                - ((unsigned HOST_WIDE_INT)b_quotient[j]
    1743      2617134 :                   * (unsigned HOST_WIDE_INT)b_divisor[0]));
    1744              :         }
    1745       837080 :       b_remainder[0] = k;
    1746       837080 :       return 1;
    1747              :     }
    1748              : 
    1749      1897562 :   s = clz_hwi (b_divisor[n-1]) - HOST_BITS_PER_HALF_WIDE_INT; /* CHECK clz */
    1750              : 
    1751      1897562 :   if (s)
    1752              :     {
    1753              :       /* Normalize B_DIVIDEND and B_DIVISOR.  Unlike the published
    1754              :          algorithm, we can overwrite b_dividend and b_divisor, so we do
    1755              :          that.  */
    1756      3850415 :       for (i = n - 1; i > 0; i--)
    1757      1957485 :         b_divisor[i] = (b_divisor[i] << s)
    1758      1957485 :           | (b_divisor[i-1] >> (HOST_BITS_PER_HALF_WIDE_INT - s));
    1759      1892930 :       b_divisor[0] = b_divisor[0] << s;
    1760              : 
    1761      1892930 :       b_dividend[m] = b_dividend[m-1] >> (HOST_BITS_PER_HALF_WIDE_INT - s);
    1762      5737203 :       for (i = m - 1; i > 0; i--)
    1763      3844273 :         b_dividend[i] = (b_dividend[i] << s)
    1764      3844273 :           | (b_dividend[i-1] >> (HOST_BITS_PER_HALF_WIDE_INT - s));
    1765      1892930 :       b_dividend[0] = b_dividend[0] << s;
    1766              :     }
    1767              : 
    1768              :   /* Main loop.  */
    1769      5690995 :   for (j = m - n; j >= 0; j--)
    1770              :     {
    1771      3793433 :       qhat = (b_dividend[j+n] * b + b_dividend[j+n-1]) / b_divisor[n-1];
    1772      3793433 :       rhat = (b_dividend[j+n] * b + b_dividend[j+n-1]) - qhat * b_divisor[n-1];
    1773      3808696 :     again:
    1774      3808696 :       if (qhat >= b || qhat * b_divisor[n-2] > b * rhat + b_dividend[j+n-2])
    1775              :         {
    1776        18203 :           qhat -= 1;
    1777        18203 :           rhat += b_divisor[n-1];
    1778        18203 :           if (rhat < b)
    1779        15263 :             goto again;
    1780              :         }
    1781              : 
    1782              :       /* Multiply and subtract.  */
    1783      3793433 :       k = 0;
    1784     11532721 :       for (i = 0; i < n; i++)
    1785              :         {
    1786      7739288 :           p = qhat * b_divisor[i];
    1787      7739288 :           t = b_dividend[i+j] - k - (p & HALF_INT_MASK);
    1788      7739288 :           b_dividend[i + j] = t;
    1789      7739288 :           k = ((p >> HOST_BITS_PER_HALF_WIDE_INT)
    1790      7739288 :                - (t >> HOST_BITS_PER_HALF_WIDE_INT));
    1791              :         }
    1792      3793433 :       t = b_dividend[j+n] - k;
    1793      3793433 :       b_dividend[j+n] = t;
    1794              : 
    1795      3793433 :       b_quotient[j] = qhat;
    1796      3793433 :       if (t < 0)
    1797              :         {
    1798         7774 :           b_quotient[j] -= 1;
    1799         7774 :           k = 0;
    1800        64408 :           for (i = 0; i < n; i++)
    1801              :             {
    1802        56634 :               t = (HOST_WIDE_INT)b_dividend[i+j] + b_divisor[i] + k;
    1803        56634 :               b_dividend[i+j] = t;
    1804        56634 :               k = t >> HOST_BITS_PER_HALF_WIDE_INT;
    1805              :             }
    1806         7774 :           b_dividend[j+n] += k;
    1807              :         }
    1808              :     }
    1809              :   /* If N > M, the main loop was skipped, quotient will be 0 and
    1810              :      we can't copy more than M half-limbs into the remainder, as they
    1811              :      aren't present in b_dividend (which has .  */
    1812      1897562 :   n = MIN (n, m);
    1813      1897562 :   if (s)
    1814      5733333 :     for (i = 0; i < n; i++)
    1815      3840403 :       b_remainder[i] = (b_dividend[i] >> s)
    1816      3840403 :         | (b_dividend[i+1] << (HOST_BITS_PER_HALF_WIDE_INT - s));
    1817              :   else
    1818        17198 :     for (i = 0; i < n; i++)
    1819        12566 :       b_remainder[i] = b_dividend[i];
    1820              :   return n;
    1821              : }
    1822              : 
    1823              : 
    1824              : /* Divide DIVIDEND by DIVISOR, which have signedness SGN, and truncate
    1825              :    the result.  If QUOTIENT is nonnull, store the value of the quotient
    1826              :    there and return the number of blocks in it.  The return value is
    1827              :    not defined otherwise.  If REMAINDER is nonnull, store the value
    1828              :    of the remainder there and store the number of blocks in
    1829              :    *REMAINDER_LEN.  If OFLOW is not null, store in *OFLOW whether
    1830              :    the division overflowed.  */
    1831              : unsigned int
    1832    670877804 : wi::divmod_internal (HOST_WIDE_INT *quotient, unsigned int *remainder_len,
    1833              :                      HOST_WIDE_INT *remainder,
    1834              :                      const HOST_WIDE_INT *dividend_val,
    1835              :                      unsigned int dividend_len, unsigned int dividend_prec,
    1836              :                      const HOST_WIDE_INT *divisor_val, unsigned int divisor_len,
    1837              :                      unsigned int divisor_prec, signop sgn,
    1838              :                      wi::overflow_type *oflow)
    1839              : {
    1840    670877804 :   unsigned int m, n;
    1841    670877804 :   bool dividend_neg = false;
    1842    670877804 :   bool divisor_neg = false;
    1843    670877804 :   bool overflow = false;
    1844    670877804 :   wide_int neg_dividend, neg_divisor;
    1845              : 
    1846    670877804 :   wide_int_ref dividend = wi::storage_ref (dividend_val, dividend_len,
    1847    670877804 :                                            dividend_prec);
    1848    670877804 :   wide_int_ref divisor = wi::storage_ref (divisor_val, divisor_len,
    1849    670877804 :                                           divisor_prec);
    1850    670877804 :   if (divisor == 0)
    1851              :     overflow = true;
    1852              : 
    1853              :   /* The smallest signed number / -1 causes overflow.  The dividend_len
    1854              :      check is for speed rather than correctness.  */
    1855    670877804 :   if (sgn == SIGNED
    1856     56947458 :       && dividend_len == BLOCKS_NEEDED (dividend_prec)
    1857     22655458 :       && divisor == -1
    1858    671782379 :       && wi::only_sign_bit_p (dividend))
    1859       222188 :     overflow = true;
    1860              : 
    1861              :   /* Handle the overflow cases.  Viewed as unsigned value, the quotient of
    1862              :      (signed min / -1) has the same representation as the original dividend.
    1863              :      We have traditionally made division by zero act as division by one,
    1864              :      so there too we use the original dividend.  */
    1865    670877804 :   if (overflow)
    1866              :     {
    1867       225135 :       if (remainder)
    1868              :         {
    1869         3876 :           *remainder_len = 1;
    1870         3876 :           remainder[0] = 0;
    1871              :         }
    1872       225135 :       if (oflow)
    1873       221201 :         *oflow = OVF_OVERFLOW;
    1874       225135 :       if (quotient)
    1875       448544 :         for (unsigned int i = 0; i < dividend_len; ++i)
    1876       225055 :           quotient[i] = dividend_val[i];
    1877              :       return dividend_len;
    1878              :     }
    1879              : 
    1880    670652669 :   if (oflow)
    1881    615747344 :     *oflow = OVF_NONE;
    1882              : 
    1883              :   /* Do it on the host if you can.  */
    1884    670652669 :   if (sgn == SIGNED
    1885     56722506 :       && wi::fits_shwi_p (dividend)
    1886    727254715 :       && wi::fits_shwi_p (divisor))
    1887              :     {
    1888     56601511 :       HOST_WIDE_INT o0 = dividend.to_shwi ();
    1889     56601511 :       HOST_WIDE_INT o1 = divisor.to_shwi ();
    1890              : 
    1891     56601511 :       if (o0 == HOST_WIDE_INT_MIN && o1 == -1)
    1892              :         {
    1893            0 :           gcc_checking_assert (dividend_prec > HOST_BITS_PER_WIDE_INT);
    1894            0 :           if (quotient)
    1895              :             {
    1896            0 :               quotient[0] = HOST_WIDE_INT_MIN;
    1897            0 :               quotient[1] = 0;
    1898              :             }
    1899            0 :           if (remainder)
    1900              :             {
    1901            0 :               remainder[0] = 0;
    1902            0 :               *remainder_len = 1;
    1903              :             }
    1904              :           return 2;
    1905              :         }
    1906              :       else
    1907              :         {
    1908     56601511 :           if (quotient)
    1909     34452005 :             quotient[0] = o0 / o1;
    1910     56601511 :           if (remainder)
    1911              :             {
    1912     22833464 :               remainder[0] = o0 % o1;
    1913     22833464 :               *remainder_len = 1;
    1914              :             }
    1915              :           return 1;
    1916              :         }
    1917              :     }
    1918              : 
    1919    614051158 :   if (sgn == UNSIGNED
    1920    613930163 :       && wi::fits_uhwi_p (dividend)
    1921   1225370979 :       && wi::fits_uhwi_p (divisor))
    1922              :     {
    1923    611316516 :       unsigned HOST_WIDE_INT o0 = dividend.to_uhwi ();
    1924    611316516 :       unsigned HOST_WIDE_INT o1 = divisor.to_uhwi ();
    1925    611316516 :       unsigned int quotient_len = 1;
    1926              : 
    1927    611316516 :       if (quotient)
    1928              :         {
    1929    600848764 :           quotient[0] = o0 / o1;
    1930    600848764 :           quotient_len = canonize_uhwi (quotient, dividend_prec);
    1931              :         }
    1932    611316516 :       if (remainder)
    1933              :         {
    1934    250441249 :           remainder[0] = o0 % o1;
    1935    250442111 :           *remainder_len = canonize_uhwi (remainder, dividend_prec);
    1936              :         }
    1937              :       return quotient_len;
    1938              :     }
    1939              : 
    1940              :   /* Make the divisor and dividend positive and remember what we
    1941              :      did.  */
    1942      2734642 :   if (sgn == SIGNED)
    1943              :     {
    1944       120995 :       if (wi::neg_p (dividend))
    1945              :         {
    1946        24890 :           neg_dividend = -dividend;
    1947        24890 :           dividend = neg_dividend;
    1948        24890 :           dividend_neg = true;
    1949              :         }
    1950       120995 :       if (wi::neg_p (divisor))
    1951              :         {
    1952         4440 :           neg_divisor = -divisor;
    1953         4440 :           divisor = neg_divisor;
    1954         4440 :           divisor_neg = true;
    1955              :         }
    1956              :     }
    1957              : 
    1958              :   unsigned HOST_HALF_WIDE_INT
    1959              :     b_quotient_buf[4 * WIDE_INT_MAX_INL_PRECISION
    1960              :                    / HOST_BITS_PER_HALF_WIDE_INT];
    1961              :   unsigned HOST_HALF_WIDE_INT
    1962              :     b_remainder_buf[4 * WIDE_INT_MAX_INL_PRECISION
    1963              :                     / HOST_BITS_PER_HALF_WIDE_INT];
    1964              :   unsigned HOST_HALF_WIDE_INT
    1965              :     b_dividend_buf[(4 * WIDE_INT_MAX_INL_PRECISION
    1966              :                     / HOST_BITS_PER_HALF_WIDE_INT) + 1];
    1967              :   unsigned HOST_HALF_WIDE_INT
    1968              :     b_divisor_buf[4 * WIDE_INT_MAX_INL_PRECISION
    1969              :                   / HOST_BITS_PER_HALF_WIDE_INT];
    1970      5291567 :   unsigned HOST_HALF_WIDE_INT *b_quotient = b_quotient_buf;
    1971      5291567 :   unsigned HOST_HALF_WIDE_INT *b_remainder = b_remainder_buf;
    1972      5291567 :   unsigned HOST_HALF_WIDE_INT *b_dividend = b_dividend_buf;
    1973      5291567 :   unsigned HOST_HALF_WIDE_INT *b_divisor = b_divisor_buf;
    1974              : 
    1975      2613647 :   if (sgn == SIGNED || dividend_val[dividend_len - 1] >= 0)
    1976      2677920 :     dividend_prec = MIN ((dividend_len + 1) * HOST_BITS_PER_WIDE_INT,
    1977              :                          dividend_prec);
    1978      2734642 :   if (sgn == SIGNED || divisor_val[divisor_len - 1] >= 0)
    1979      2732295 :     divisor_prec = MIN (divisor_len * HOST_BITS_PER_WIDE_INT, divisor_prec);
    1980      2734642 :   unsigned int dividend_blocks_needed = 2 * BLOCKS_NEEDED (dividend_prec);
    1981      2734642 :   unsigned int divisor_blocks_needed = 2 * BLOCKS_NEEDED (divisor_prec);
    1982      2734642 :   if (UNLIKELY (dividend_prec > WIDE_INT_MAX_INL_PRECISION)
    1983      2733935 :       || UNLIKELY (divisor_prec > WIDE_INT_MAX_INL_PRECISION))
    1984              :     {
    1985          824 :       unsigned HOST_HALF_WIDE_INT *buf
    1986          824 :         = XALLOCAVEC (unsigned HOST_HALF_WIDE_INT,
    1987              :                       3 * dividend_blocks_needed + 1
    1988              :                       + divisor_blocks_needed);
    1989          824 :       b_quotient = buf;
    1990          824 :       b_remainder = b_quotient + dividend_blocks_needed;
    1991          824 :       b_dividend = b_remainder + dividend_blocks_needed;
    1992          824 :       b_divisor = b_dividend + dividend_blocks_needed + 1;
    1993          824 :       memset (b_quotient, 0,
    1994              :               dividend_blocks_needed * sizeof (HOST_HALF_WIDE_INT));
    1995              :     }
    1996      2734642 :   wi_unpack (b_dividend, dividend.get_val (), dividend.get_len (),
    1997              :              dividend_blocks_needed, dividend_prec, UNSIGNED);
    1998      2734642 :   wi_unpack (b_divisor, divisor.get_val (), divisor.get_len (),
    1999              :              divisor_blocks_needed, divisor_prec, UNSIGNED);
    2000              : 
    2001      2734642 :   m = dividend_blocks_needed;
    2002      2734642 :   b_dividend[m] = 0;
    2003      5573457 :   while (m > 1 && b_dividend[m - 1] == 0)
    2004              :     m--;
    2005              : 
    2006              :   n = divisor_blocks_needed;
    2007      3581566 :   while (n > 1 && b_divisor[n - 1] == 0)
    2008              :     n--;
    2009              : 
    2010      2734642 :   if (b_quotient == b_quotient_buf)
    2011      2733818 :     memset (b_quotient_buf, 0, sizeof (b_quotient_buf));
    2012              : 
    2013      2734642 :   n = divmod_internal_2 (b_quotient, b_remainder, b_dividend, b_divisor, m, n);
    2014              : 
    2015      2734642 :   unsigned int quotient_len = 0;
    2016      2734642 :   if (quotient)
    2017              :     {
    2018      2673386 :       quotient_len = wi_pack (quotient, b_quotient, m, dividend_prec);
    2019              :       /* The quotient is neg if exactly one of the divisor or dividend is
    2020              :          neg.  */
    2021      2673386 :       if (dividend_neg != divisor_neg)
    2022        23804 :         quotient_len = wi::sub_large (quotient, zeros, 1, quotient,
    2023              :                                       quotient_len, dividend_prec,
    2024              :                                       UNSIGNED, 0);
    2025              :     }
    2026              : 
    2027      2734642 :   if (remainder)
    2028              :     {
    2029      2543199 :       *remainder_len = wi_pack (remainder, b_remainder, n, dividend_prec);
    2030              :       /* The remainder is always the same sign as the dividend.  */
    2031      2543199 :       if (dividend_neg)
    2032         2087 :         *remainder_len = wi::sub_large (remainder, zeros, 1, remainder,
    2033              :                                         *remainder_len, dividend_prec,
    2034              :                                         UNSIGNED, 0);
    2035              :     }
    2036              : 
    2037              :   return quotient_len;
    2038    670877804 : }
    2039              : 
    2040              : /*
    2041              :  * Shifting, rotating and extraction.
    2042              :  */
    2043              : 
    2044              : /* Left shift XVAL by SHIFT and store the result in VAL.  Return the
    2045              :    number of blocks in VAL.  Both XVAL and VAL have PRECISION bits.  */
    2046              : unsigned int
    2047   1880983628 : wi::lshift_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
    2048              :                   unsigned int xlen, unsigned int precision,
    2049              :                   unsigned int shift)
    2050              : {
    2051              :   /* Split the shift into a whole-block shift and a subblock shift.  */
    2052   1880983628 :   unsigned int skip = shift / HOST_BITS_PER_WIDE_INT;
    2053   1880983628 :   unsigned int small_shift = shift % HOST_BITS_PER_WIDE_INT;
    2054              : 
    2055              :   /* The whole-block shift fills with zeros.  */
    2056   1880983628 :   unsigned int len = BLOCKS_NEEDED (precision);
    2057   1880983628 :   len = MIN (xlen + skip + 1, len);
    2058   1883088018 :   for (unsigned int i = 0; i < skip; ++i)
    2059      2104390 :     val[i] = 0;
    2060              : 
    2061              :   /* It's easier to handle the simple block case specially.  */
    2062   1880983628 :   if (small_shift == 0)
    2063     53482270 :     for (unsigned int i = skip; i < len; ++i)
    2064     93943644 :       val[i] = safe_uhwi (xval, xlen, i - skip);
    2065              :   else
    2066              :     {
    2067              :       /* The first unfilled output block is a left shift of the first
    2068              :          block in XVAL.  The other output blocks contain bits from two
    2069              :          consecutive input blocks.  */
    2070              :       unsigned HOST_WIDE_INT carry = 0;
    2071   5665145298 :       for (unsigned int i = skip; i < len; ++i)
    2072              :         {
    2073   3790672118 :           unsigned HOST_WIDE_INT x = safe_uhwi (xval, xlen, i - skip);
    2074   3790672118 :           val[i] = (x << small_shift) | carry;
    2075   3790672118 :           carry = x >> (-small_shift % HOST_BITS_PER_WIDE_INT);
    2076              :         }
    2077              :     }
    2078   1880983628 :   return canonize (val, len, precision);
    2079              : }
    2080              : 
    2081              : /* Right shift XVAL by SHIFT and store the result in VAL.  LEN is the
    2082              :    number of blocks in VAL.  The input has XPRECISION bits and the
    2083              :    output has XPRECISION - SHIFT bits.  */
    2084              : static void
    2085    171635118 : rshift_large_common (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
    2086              :                      unsigned int xlen, unsigned int shift, unsigned int len)
    2087              : {
    2088              :   /* Split the shift into a whole-block shift and a subblock shift.  */
    2089    171635118 :   unsigned int skip = shift / HOST_BITS_PER_WIDE_INT;
    2090    171635118 :   unsigned int small_shift = shift % HOST_BITS_PER_WIDE_INT;
    2091              : 
    2092              :   /* It's easier to handle the simple block case specially.  */
    2093    171635118 :   if (small_shift == 0)
    2094      2699208 :     for (unsigned int i = 0; i < len; ++i)
    2095      3127594 :       val[i] = safe_uhwi (xval, xlen, i + skip);
    2096              :   else
    2097              :     {
    2098              :       /* Each output block but the last is a combination of two input blocks.
    2099              :          The last block is a right shift of the last block in XVAL.  */
    2100    170499707 :       unsigned HOST_WIDE_INT curr = safe_uhwi (xval, xlen, skip);
    2101    342502858 :       for (unsigned int i = 0; i < len; ++i)
    2102              :         {
    2103    172003151 :           val[i] = curr >> small_shift;
    2104    172003151 :           curr = safe_uhwi (xval, xlen, i + skip + 1);
    2105    172003151 :           val[i] |= curr << (-small_shift % HOST_BITS_PER_WIDE_INT);
    2106              :         }
    2107              :     }
    2108    171635118 : }
    2109              : 
    2110              : /* Logically right shift XVAL by SHIFT and store the result in VAL.
    2111              :    Return the number of blocks in VAL.  XVAL has XPRECISION bits and
    2112              :    VAL has PRECISION bits.  */
    2113              : unsigned int
    2114      2477211 : wi::lrshift_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
    2115              :                    unsigned int xlen, unsigned int xprecision,
    2116              :                    unsigned int precision, unsigned int shift)
    2117              : {
    2118              :   /* Work out how many blocks are needed to store the significant bits
    2119              :      (excluding the upper zeros or signs).  */
    2120      2477211 :   unsigned int blocks_needed = BLOCKS_NEEDED (xprecision - shift);
    2121      2477211 :   unsigned int len = blocks_needed;
    2122      2477211 :   if (len > xlen && xval[xlen - 1] >= 0)
    2123      2477211 :     len = xlen;
    2124              : 
    2125      2477211 :   rshift_large_common (val, xval, xlen, shift, len);
    2126              : 
    2127              :   /* The value we just created has precision XPRECISION - SHIFT.
    2128              :      Zero-extend it to wider precisions.  */
    2129      2477211 :   if (precision > xprecision - shift && len == blocks_needed)
    2130              :     {
    2131       598143 :       unsigned int small_prec = (xprecision - shift) % HOST_BITS_PER_WIDE_INT;
    2132       598143 :       if (small_prec)
    2133        58199 :         val[len - 1] = zext_hwi (val[len - 1], small_prec);
    2134       539944 :       else if (val[len - 1] < 0)
    2135              :         {
    2136              :           /* Add a new block with a zero. */
    2137       248164 :           val[len++] = 0;
    2138       248164 :           return len;
    2139              :         }
    2140              :     }
    2141      2229047 :   return canonize (val, len, precision);
    2142              : }
    2143              : 
    2144              : /* Arithmetically right shift XVAL by SHIFT and store the result in VAL.
    2145              :    Return the number of blocks in VAL.  XVAL has XPRECISION bits and
    2146              :    VAL has PRECISION bits.  */
    2147              : unsigned int
    2148    169157907 : wi::arshift_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
    2149              :                    unsigned int xlen, unsigned int xprecision,
    2150              :                    unsigned int precision, unsigned int shift)
    2151              : {
    2152              :   /* Work out how many blocks are needed to store the significant bits
    2153              :      (excluding the upper zeros or signs).  */
    2154    169157907 :   unsigned int blocks_needed = BLOCKS_NEEDED (xprecision - shift);
    2155    169157907 :   unsigned int len = MIN (xlen, blocks_needed);
    2156              : 
    2157    169157907 :   rshift_large_common (val, xval, xlen, shift, len);
    2158              : 
    2159              :   /* The value we just created has precision XPRECISION - SHIFT.
    2160              :      Sign-extend it to wider types.  */
    2161    169157907 :   if (precision > xprecision - shift && len == blocks_needed)
    2162              :     {
    2163        41818 :       unsigned int small_prec = (xprecision - shift) % HOST_BITS_PER_WIDE_INT;
    2164        41818 :       if (small_prec)
    2165         6120 :         val[len - 1] = sext_hwi (val[len - 1], small_prec);
    2166              :     }
    2167    169157907 :   return canonize (val, len, precision);
    2168              : }
    2169              : 
    2170              : /* Return the number of leading (upper) zeros in X.  */
    2171              : int
    2172   1191279154 : wi::clz (const wide_int_ref &x)
    2173              : {
    2174   1191279154 :   if (x.sign_mask () < 0)
    2175              :     /* The upper bit is set, so there are no leading zeros.  */
    2176              :     return 0;
    2177              : 
    2178              :   /* Calculate how many bits there above the highest represented block.  */
    2179    647863148 :   int count = x.precision - x.len * HOST_BITS_PER_WIDE_INT;
    2180              : 
    2181    647863148 :   unsigned HOST_WIDE_INT high = x.uhigh ();
    2182    647863148 :   if (count < 0)
    2183              :     /* The upper -COUNT bits of HIGH are not part of the value.
    2184              :        Clear them out.  */
    2185    277060270 :     high = (high << -count) >> -count;
    2186              : 
    2187              :   /* We don't need to look below HIGH.  Either HIGH is nonzero,
    2188              :      or the top bit of the block below is nonzero; clz_hwi is
    2189              :      HOST_BITS_PER_WIDE_INT in the latter case.  */
    2190   1291953542 :   return count + clz_hwi (high);
    2191              : }
    2192              : 
    2193              : /* Return the number of redundant sign bits in X.  (That is, the number
    2194              :    of bits immediately below the sign bit that have the same value as
    2195              :    the sign bit.)  */
    2196              : int
    2197     41523181 : wi::clrsb (const wide_int_ref &x)
    2198              : {
    2199              :   /* Calculate how many bits there above the highest represented block.  */
    2200     41523181 :   int count = x.precision - x.len * HOST_BITS_PER_WIDE_INT;
    2201              : 
    2202     41523181 :   unsigned HOST_WIDE_INT high = x.uhigh ();
    2203     41523181 :   unsigned HOST_WIDE_INT mask = -1;
    2204     41523181 :   if (count < 0)
    2205              :     {
    2206              :       /* The upper -COUNT bits of HIGH are not part of the value.
    2207              :          Clear them from both MASK and HIGH.  */
    2208      2361762 :       mask >>= -count;
    2209      2361762 :       high &= mask;
    2210              :     }
    2211              : 
    2212              :   /* If the top bit is 1, count the number of leading 1s.  If the top
    2213              :      bit is zero, count the number of leading zeros.  */
    2214     41523181 :   if (high > mask / 2)
    2215      1708248 :     high ^= mask;
    2216              : 
    2217              :   /* There are no sign bits below the top block, so we don't need to look
    2218              :      beyond HIGH.  Note that clz_hwi is HOST_BITS_PER_WIDE_INT when
    2219              :      HIGH is 0.  */
    2220     41523181 :   return count + clz_hwi (high) - 1;
    2221              : }
    2222              : 
    2223              : /* Return the number of trailing (lower) zeros in X.  */
    2224              : int
    2225    525828056 : wi::ctz (const wide_int_ref &x)
    2226              : {
    2227    525828056 :   if (x.len == 1 && x.ulow () == 0)
    2228     45929391 :     return x.precision;
    2229              : 
    2230              :   /* Having dealt with the zero case, there must be a block with a
    2231              :      nonzero bit.  We don't care about the bits above the first 1.  */
    2232              :   unsigned int i = 0;
    2233    480088088 :   while (x.val[i] == 0)
    2234       189423 :     ++i;
    2235    479898665 :   return i * HOST_BITS_PER_WIDE_INT + ctz_hwi (x.val[i]);
    2236              : }
    2237              : 
    2238              : /* If X is an exact power of 2, return the base-2 logarithm, otherwise
    2239              :    return -1.  */
    2240              : int
    2241     17747078 : wi::exact_log2 (const wide_int_ref &x)
    2242              : {
    2243              :   /* Reject cases where there are implicit -1 blocks above HIGH.  */
    2244     17747078 :   if (x.len * HOST_BITS_PER_WIDE_INT < x.precision && x.sign_mask () < 0)
    2245              :     return -1;
    2246              : 
    2247              :   /* Set CRUX to the index of the entry that should be nonzero.
    2248              :      If the top block is zero then the next lowest block (if any)
    2249              :      must have the high bit set.  */
    2250     17740533 :   unsigned int crux = x.len - 1;
    2251     17740533 :   if (crux > 0 && x.val[crux] == 0)
    2252        23909 :     crux -= 1;
    2253              : 
    2254              :   /* Check that all lower blocks are zero.  */
    2255     17741430 :   for (unsigned int i = 0; i < crux; ++i)
    2256         5904 :     if (x.val[i] != 0)
    2257              :       return -1;
    2258              : 
    2259              :   /* Get a zero-extended form of block CRUX.  */
    2260     17735526 :   unsigned HOST_WIDE_INT hwi = x.val[crux];
    2261     17735526 :   if ((crux + 1) * HOST_BITS_PER_WIDE_INT > x.precision)
    2262      4321128 :     hwi = zext_hwi (hwi, x.precision % HOST_BITS_PER_WIDE_INT);
    2263              : 
    2264              :   /* Now it's down to whether HWI is a power of 2.  */
    2265     17735526 :   int res = ::exact_log2 (hwi);
    2266     10373971 :   if (res >= 0)
    2267     10373971 :     res += crux * HOST_BITS_PER_WIDE_INT;
    2268              :   return res;
    2269              : }
    2270              : 
    2271              : /* Return the base-2 logarithm of X, rounding down.  Return -1 if X is 0.  */
    2272              : int
    2273     10849089 : wi::floor_log2 (const wide_int_ref &x)
    2274              : {
    2275     10849089 :   return x.precision - 1 - clz (x);
    2276              : }
    2277              : 
    2278              : /* Return the index of the first (lowest) set bit in X, counting from 1.
    2279              :    Return 0 if X is 0.  */
    2280              : int
    2281          491 : wi::ffs (const wide_int_ref &x)
    2282              : {
    2283          491 :   return eq_p (x, 0) ? 0 : ctz (x) + 1;
    2284              : }
    2285              : 
    2286              : /* Return true if sign-extending X to have precision PRECISION would give
    2287              :    the minimum signed value at that precision.  */
    2288              : bool
    2289     36318394 : wi::only_sign_bit_p (const wide_int_ref &x, unsigned int precision)
    2290              : {
    2291     36318394 :   return ctz (x) + 1 == int (precision);
    2292              : }
    2293              : 
    2294              : /* Return true if X represents the minimum signed value.  */
    2295              : bool
    2296     34219116 : wi::only_sign_bit_p (const wide_int_ref &x)
    2297              : {
    2298     34219116 :   return only_sign_bit_p (x, x.precision);
    2299              : }
    2300              : 
    2301              : /* Return VAL if VAL has no bits set outside MASK.  Otherwise round VAL
    2302              :    down to the previous value that has no bits set outside MASK.
    2303              :    This rounding wraps for signed values if VAL is negative and
    2304              :    the top bit of MASK is clear.
    2305              : 
    2306              :    For example, round_down_for_mask (6, 0xf1) would give 1 and
    2307              :    round_down_for_mask (24, 0xf1) would give 17.  */
    2308              : 
    2309              : wide_int
    2310     18760690 : wi::round_down_for_mask (const wide_int &val, const wide_int &mask)
    2311              : {
    2312              :   /* Get the bits in VAL that are outside the mask.  */
    2313     18760690 :   wide_int extra_bits = wi::bit_and_not (val, mask);
    2314     18760690 :   if (extra_bits == 0)
    2315     18734718 :     return val;
    2316              : 
    2317              :   /* Get a mask that includes the top bit in EXTRA_BITS and is all 1s
    2318              :      below that bit.  */
    2319        25972 :   unsigned int precision = val.get_precision ();
    2320        25972 :   wide_int lower_mask = wi::mask (precision - wi::clz (extra_bits),
    2321        25972 :                                   false, precision);
    2322              : 
    2323              :   /* Clear the bits that aren't in MASK, but ensure that all bits
    2324              :      in MASK below the top cleared bit are set.  */
    2325        25972 :   return (val & mask) | (mask & lower_mask);
    2326        25972 : }
    2327              : 
    2328              : /* Return VAL if VAL has no bits set outside MASK.  Otherwise round VAL
    2329              :    up to the next value that has no bits set outside MASK.  The rounding
    2330              :    wraps if there are no suitable values greater than VAL.
    2331              : 
    2332              :    For example, round_up_for_mask (6, 0xf1) would give 16 and
    2333              :    round_up_for_mask (24, 0xf1) would give 32.  */
    2334              : 
    2335              : wide_int
    2336     19297707 : wi::round_up_for_mask (const wide_int &val, const wide_int &mask)
    2337              : {
    2338              :   /* Get the bits in VAL that are outside the mask.  */
    2339     19297707 :   wide_int extra_bits = wi::bit_and_not (val, mask);
    2340     19297707 :   if (extra_bits == 0)
    2341     19292893 :     return val;
    2342              : 
    2343              :   /* Get a mask that is all 1s above the top bit in EXTRA_BITS.  */
    2344         4814 :   unsigned int precision = val.get_precision ();
    2345         4814 :   wide_int upper_mask = wi::mask (precision - wi::clz (extra_bits),
    2346         4814 :                                   true, precision);
    2347              : 
    2348              :   /* Get the bits of the mask that are above the top bit in EXTRA_BITS.  */
    2349         4814 :   upper_mask &= mask;
    2350              : 
    2351              :   /* Conceptually we need to:
    2352              : 
    2353              :      - clear bits of VAL outside UPPER_MASK
    2354              :      - add the lowest bit in UPPER_MASK to VAL (or add 0 if UPPER_MASK is 0)
    2355              :      - propagate the carry through the bits of VAL in UPPER_MASK
    2356              : 
    2357              :      If (~VAL & UPPER_MASK) is nonzero, the carry eventually
    2358              :      reaches that bit and the process leaves all lower bits clear.
    2359              :      If (~VAL & UPPER_MASK) is zero then the result is also zero.  */
    2360         4814 :   wide_int tmp = wi::bit_and_not (upper_mask, val);
    2361              : 
    2362         4814 :   return (val | tmp) & -tmp;
    2363         4814 : }
    2364              : 
    2365              : /* Compute the modular multiplicative inverse of A modulo B
    2366              :    using extended Euclid's algorithm.  Assumes A and B are coprime,
    2367              :    and that A and B have the same precision.  */
    2368              : wide_int
    2369         3873 : wi::mod_inv (const wide_int &a, const wide_int &b)
    2370              : {
    2371              :   /* Verify the assumption.  */
    2372         3873 :   gcc_checking_assert (wi::eq_p (wi::gcd (a, b), 1));
    2373              : 
    2374         3873 :   unsigned int p = a.get_precision () + 1;
    2375         3873 :   gcc_checking_assert (b.get_precision () + 1 == p);
    2376         3873 :   wide_int c = wide_int::from (a, p, UNSIGNED);
    2377         3873 :   wide_int d = wide_int::from (b, p, UNSIGNED);
    2378         3873 :   wide_int x0 = wide_int::from (0, p, UNSIGNED);
    2379         3873 :   wide_int x1 = wide_int::from (1, p, UNSIGNED);
    2380              : 
    2381         3873 :   if (wi::eq_p (b, 1))
    2382            0 :     return wide_int::from (1, p, UNSIGNED);
    2383              : 
    2384        19793 :   while (wi::gt_p (c, 1, UNSIGNED))
    2385              :     {
    2386        15920 :       wide_int t = d;
    2387        15920 :       wide_int q = wi::divmod_trunc (c, d, UNSIGNED, &d);
    2388        15920 :       c = t;
    2389        15920 :       wide_int s = x0;
    2390        15920 :       x0 = wi::sub (x1, wi::mul (q, x0));
    2391        15920 :       x1 = s;
    2392        15920 :     }
    2393         3873 :   if (wi::lt_p (x1, 0, SIGNED))
    2394         3388 :     x1 += d;
    2395         3873 :   return x1;
    2396         3873 : }
    2397              : 
    2398              : /* Return as many repetition of X.get_precision () bits from X
    2399              :    as fit into PREC.  */
    2400              : wide_int
    2401          965 : wi::bit_repeat (const wide_int_ref &x, unsigned int prec)
    2402              : {
    2403          965 :   unsigned int xprec = x.get_precision ();
    2404          965 :   if (xprec >= prec)
    2405            0 :     return wide_int::from (x, prec, UNSIGNED);
    2406          965 :   if (xprec <= HOST_BITS_PER_WIDE_INT && (HOST_BITS_PER_WIDE_INT % xprec) == 0)
    2407              :     {
    2408          337 :       unsigned HOST_WIDE_INT val = x.to_uhwi (xprec);
    2409         2091 :       while (xprec < HOST_BITS_PER_WIDE_INT)
    2410              :         {
    2411         1417 :           val |= val << xprec;
    2412         1417 :           xprec <<= 1;
    2413              :         }
    2414          337 :       wide_int ret = wide_int::create (prec);
    2415          337 :       HOST_WIDE_INT *v = ret.write_val (0);
    2416          337 :       unsigned int i = 0;
    2417          694 :       for (unsigned int p = 0; p < prec; p += HOST_BITS_PER_WIDE_INT)
    2418          357 :         v[i++] = val;
    2419          337 :       ret.set_len (i, false);
    2420          337 :       return ret;
    2421          337 :     }
    2422          628 :   wide_int ret = wide_int::from (x, prec, UNSIGNED), b;
    2423         3966 :   while (xprec < prec)
    2424              :     {
    2425         2710 :       ret |= (ret << xprec);
    2426         2710 :       xprec <<= 1;
    2427              :     }
    2428          628 :   return ret;
    2429          628 : }
    2430              : 
    2431              : /* Compute carry-less product of X and Y with precision PREC.  */
    2432              : 
    2433              : wide_int
    2434          336 : wi::clmul (const wide_int_ref &x, const wide_int_ref &y, unsigned int prec)
    2435              : {
    2436          336 :   wide_int a;
    2437          336 :   const wide_int_ref *b;
    2438          336 :   if (wi::popcount (x) >= wi::popcount (y))
    2439              :     {
    2440          238 :       a = wide_int::from (x, prec, UNSIGNED);
    2441          238 :       b = &y;
    2442              :     }
    2443              :   else
    2444              :     {
    2445           98 :       a = wide_int::from (y, prec, UNSIGNED);
    2446           98 :       b = &x;
    2447              :     }
    2448          336 :   unsigned int bprec = b->get_precision ();
    2449          336 :   wide_int r = wi::zero (prec);
    2450          336 :   unsigned int j = 0, k = 0;
    2451        29030 :   for (unsigned int i = 0; i < bprec; ++i)
    2452              :     {
    2453        28694 :       if (((((unsigned HOST_WIDE_INT) b->elt (k)) >> j) & 1) != 0)
    2454        13705 :         r ^= a << i;
    2455        28694 :       if (++j == HOST_BITS_PER_WIDE_INT)
    2456              :         {
    2457          393 :           j = 0;
    2458          393 :           ++k;
    2459              :         }
    2460              :     }
    2461          336 :   return r;
    2462          336 : }
    2463              : 
    2464              : /* Compute bit compression of X using mask M with precision PREC.
    2465              :    Based on "Compress, or Generalized Extract" in Hacker's Delight.  */
    2466              : 
    2467              : wide_int
    2468         1239 : wi::bit_compress (const wide_int_ref &x, const wide_int_ref &m,
    2469              :                   unsigned int prec)
    2470              : {
    2471         1239 :   wide_int r = wi::bit_and (x, m);
    2472         1239 :   wide_int mk = wi::lshift (wi::bit_not (m), 1);
    2473         1239 :   wide_int mm = wide_int::from (m, prec, UNSIGNED);
    2474         8031 :   for (unsigned int i = 1; i < prec; i <<= 1)
    2475              :     {
    2476         6792 :       wide_int mp = mk;
    2477        46838 :       for (unsigned int j = 1; j < prec; j <<= 1)
    2478        43918 :         mp = wi::bit_xor (mp, wi::lshift (mp, j));
    2479         6792 :       wide_int mv = wi::bit_and (mp, mm);
    2480         7144 :       mm = wi::bit_or (wi::bit_xor (mm, mv), wi::lrshift (mv, i));
    2481         6792 :       wide_int t = wi::bit_and (r, mv);
    2482         7144 :       r = wi::bit_or (wi::bit_xor (r, t), wi::lrshift (t, i));
    2483         6968 :       mk = wi::bit_and_not (mk, mp);
    2484         7144 :     }
    2485         1239 :   return r;
    2486         1239 : }
    2487              : 
    2488              : /* Compute bit expansion of X using mask M with precision PREC.
    2489              :    Based on "Expand, or Generalized Insert" in Hacker's Delight.  */
    2490              : 
    2491              : wide_int
    2492         1279 : wi::bit_expand (const wide_int_ref &x, const wide_int_ref &m,
    2493              :                 unsigned int prec)
    2494              : {
    2495         1279 :   if (prec == 1)
    2496            0 :     return wi::bit_and (x, m);
    2497         1279 :   unsigned log2_prec = ceil_log2 (prec);
    2498         1279 :   wide_int m0 = wide_int::from (m, prec, UNSIGNED);
    2499         1279 :   wide_int mk = wi::lshift (wi::bit_not (m), 1);
    2500         1279 :   wide_int mm = wide_int::from (m, prec, UNSIGNED);
    2501         1279 :   wide_int r = wide_int::from (x, prec, UNSIGNED);
    2502         1279 :   auto_vec<wide_int, 16> vec (log2_prec);
    2503         9630 :   for (unsigned int i = 0; i < log2_prec; ++i)
    2504              :     {
    2505         7072 :       wide_int mp = mk;
    2506        49078 :       for (unsigned int j = 1; j < prec; j <<= 1)
    2507        45878 :         mp = wi::bit_xor (mp, wi::lshift (mp, j));
    2508         7072 :      wide_int mv = wi::bit_and (mp, mm);
    2509         7072 :      vec.safe_push (mv);
    2510         7424 :       mm = wi::bit_or (wi::bit_xor (mm, mv), wi::lrshift (mv, 1U << i));
    2511         7248 :       mk = wi::bit_and_not (mk, mp);
    2512         7248 :     }
    2513         8351 :   for (int i = log2_prec - 1; i >= 0; --i)
    2514              :     {
    2515         7072 :       wide_int mv = vec.last ();
    2516         7072 :       vec.pop ();
    2517         7072 :       wide_int t = wi::lshift (r, 1U << i);
    2518         7600 :       r = wi::bit_or (wi::bit_and_not (r, mv), wi::bit_and (t, mv));
    2519         7248 :     }
    2520         1279 :   return wi::bit_and (r, m0);
    2521         1343 : }
    2522              : 
    2523              : /*
    2524              :  * Private utilities.
    2525              :  */
    2526              : 
    2527            0 : void gt_ggc_mx (widest_int *) { }
    2528            0 : void gt_pch_nx (widest_int *, void (*) (void *, void *), void *) { }
    2529            0 : void gt_pch_nx (widest_int *) { }
    2530              : 
    2531              : template void wide_int::dump () const;
    2532              : template void generic_wide_int <wide_int_ref_storage <false> >::dump () const;
    2533              : template void generic_wide_int <wide_int_ref_storage <true> >::dump () const;
    2534              : template void offset_int::dump () const;
    2535              : template void widest_int::dump () const;
    2536              : 
    2537              : /* We could add all the above ::dump variants here, but wide_int and
    2538              :    widest_int should handle the common cases.  Besides, you can always
    2539              :    call the dump method directly.  */
    2540              : 
    2541              : DEBUG_FUNCTION void
    2542            0 : debug (const wide_int &ref)
    2543              : {
    2544            0 :   ref.dump ();
    2545            0 : }
    2546              : 
    2547              : DEBUG_FUNCTION void
    2548            0 : debug (const wide_int *ptr)
    2549              : {
    2550            0 :   if (ptr)
    2551            0 :     debug (*ptr);
    2552              :   else
    2553            0 :     fprintf (stderr, "<nil>\n");
    2554            0 : }
    2555              : 
    2556              : DEBUG_FUNCTION void
    2557            0 : debug (const widest_int &ref)
    2558              : {
    2559            0 :   ref.dump ();
    2560            0 : }
    2561              : 
    2562              : DEBUG_FUNCTION void
    2563            0 : debug (const widest_int *ptr)
    2564              : {
    2565            0 :   if (ptr)
    2566            0 :     debug (*ptr);
    2567              :   else
    2568            0 :     fprintf (stderr, "<nil>\n");
    2569            0 : }
    2570              : 
    2571              : #if CHECKING_P
    2572              : 
    2573              : namespace selftest {
    2574              : 
    2575              : /* Selftests for wide ints.  We run these multiple times, once per type.  */
    2576              : 
    2577              : /* Helper function for building a test value.  */
    2578              : 
    2579              : template <class VALUE_TYPE>
    2580              : static VALUE_TYPE
    2581              : from_int (int i);
    2582              : 
    2583              : /* Specializations of the fixture for each wide-int type.  */
    2584              : 
    2585              : /* Specialization for VALUE_TYPE == wide_int.  */
    2586              : 
    2587              : template <>
    2588              : wide_int
    2589           20 : from_int (int i)
    2590              : {
    2591           20 :   return wi::shwi (i, 32);
    2592              : }
    2593              : 
    2594              : /* Specialization for VALUE_TYPE == offset_int.  */
    2595              : 
    2596              : template <>
    2597              : offset_int
    2598           20 : from_int (int i)
    2599              : {
    2600            0 :   return offset_int (i);
    2601              : }
    2602              : 
    2603              : /* Specialization for VALUE_TYPE == widest_int.  */
    2604              : 
    2605              : template <>
    2606              : widest_int
    2607           28 : from_int (int i)
    2608              : {
    2609            0 :   return widest_int (i);
    2610              : }
    2611              : 
    2612              : /* Verify that print_dec (WI, ..., SGN) gives the expected string
    2613              :    representation (using base 10).  */
    2614              : 
    2615              : static void
    2616          132 : assert_deceq (const char *expected, const wide_int_ref &wi, signop sgn)
    2617              : {
    2618          132 :   char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p = buf;
    2619          132 :   unsigned len;
    2620          132 :   if (print_dec_buf_size (wi, sgn, &len))
    2621            0 :     p = XALLOCAVEC (char, len);
    2622          132 :   print_dec (wi, p, sgn);
    2623          132 :   ASSERT_STREQ (expected, p);
    2624          132 : }
    2625              : 
    2626              : /* Likewise for base 16.  */
    2627              : 
    2628              : static void
    2629           72 : assert_hexeq (const char *expected, const wide_int_ref &wi)
    2630              : {
    2631           72 :   char buf[WIDE_INT_PRINT_BUFFER_SIZE], *p = buf;
    2632           72 :   unsigned len;
    2633           72 :   if (print_hex_buf_size (wi, &len))
    2634            0 :     p = XALLOCAVEC (char, len);
    2635           72 :   print_hex (wi, p);
    2636           72 :   ASSERT_STREQ (expected, p);
    2637           72 : }
    2638              : 
    2639              : /* Test cases.  */
    2640              : 
    2641              : /* Verify that print_dec and print_hex work for VALUE_TYPE.  */
    2642              : 
    2643              : template <class VALUE_TYPE>
    2644              : static void
    2645           12 : test_printing ()
    2646              : {
    2647           12 :   VALUE_TYPE a = from_int<VALUE_TYPE> (42);
    2648           12 :   assert_deceq ("42", a, SIGNED);
    2649           12 :   assert_hexeq ("0x2a", a);
    2650           12 :   assert_hexeq ("0x1fffffffffffffffff", wi::shwi (-1, 69));
    2651           12 :   assert_hexeq ("0xffffffffffffffff", wi::mask (64, false, 69));
    2652           12 :   assert_hexeq ("0xffffffffffffffff", wi::mask <widest_int> (64, false));
    2653              :   if (WIDE_INT_MAX_INL_PRECISION > 128)
    2654              :     {
    2655           12 :       assert_hexeq ("0x20000000000000000fffffffffffffffe",
    2656           24 :                     wi::lshift (1, 129) + wi::lshift (1, 64) - 2);
    2657           12 :       assert_hexeq ("0x200000000000004000123456789abcdef",
    2658           24 :                     wi::lshift (1, 129) + wi::lshift (1, 74)
    2659           44 :                     + wi::lshift (0x1234567, 32) + 0x89abcdef);
    2660              :     }
    2661           12 : }
    2662              : 
    2663              : /* Verify that various operations work correctly for VALUE_TYPE,
    2664              :    unary and binary, using both function syntax, and
    2665              :    overloaded-operators.  */
    2666              : 
    2667              : template <class VALUE_TYPE>
    2668              : static void
    2669           12 : test_ops ()
    2670              : {
    2671           12 :   VALUE_TYPE a = from_int<VALUE_TYPE> (7);
    2672           12 :   VALUE_TYPE b = from_int<VALUE_TYPE> (3);
    2673              : 
    2674              :   /* Using functions.  */
    2675           12 :   assert_deceq ("-7", wi::neg (a), SIGNED);
    2676           12 :   assert_deceq ("10", wi::add (a, b), SIGNED);
    2677           12 :   assert_deceq ("4", wi::sub (a, b), SIGNED);
    2678           12 :   assert_deceq ("-4", wi::sub (b, a), SIGNED);
    2679           12 :   assert_deceq ("21", wi::mul (a, b), SIGNED);
    2680              : 
    2681              :   /* Using operators.  */
    2682           12 :   assert_deceq ("-7", -a, SIGNED);
    2683           12 :   assert_deceq ("10", a + b, SIGNED);
    2684           12 :   assert_deceq ("4", a - b, SIGNED);
    2685           12 :   assert_deceq ("-4", b - a, SIGNED);
    2686           12 :   assert_deceq ("21", a * b, SIGNED);
    2687           12 : }
    2688              : 
    2689              : /* Verify that various comparisons work correctly for VALUE_TYPE.  */
    2690              : 
    2691              : template <class VALUE_TYPE>
    2692              : static void
    2693           12 : test_comparisons ()
    2694              : {
    2695           12 :   VALUE_TYPE a = from_int<VALUE_TYPE> (7);
    2696           12 :   VALUE_TYPE b = from_int<VALUE_TYPE> (3);
    2697              : 
    2698              :   /* == */
    2699           12 :   ASSERT_TRUE (wi::eq_p (a, a));
    2700           12 :   ASSERT_FALSE (wi::eq_p (a, b));
    2701              : 
    2702              :   /* != */
    2703           12 :   ASSERT_TRUE (wi::ne_p (a, b));
    2704           12 :   ASSERT_FALSE (wi::ne_p (a, a));
    2705              : 
    2706              :   /* < */
    2707           12 :   ASSERT_FALSE (wi::lts_p (a, a));
    2708           12 :   ASSERT_FALSE (wi::lts_p (a, b));
    2709           12 :   ASSERT_TRUE (wi::lts_p (b, a));
    2710              : 
    2711              :   /* <= */
    2712           12 :   ASSERT_TRUE (wi::les_p (a, a));
    2713           12 :   ASSERT_FALSE (wi::les_p (a, b));
    2714           12 :   ASSERT_TRUE (wi::les_p (b, a));
    2715              : 
    2716              :   /* > */
    2717           12 :   ASSERT_FALSE (wi::gts_p (a, a));
    2718           12 :   ASSERT_TRUE (wi::gts_p (a, b));
    2719           12 :   ASSERT_FALSE (wi::gts_p (b, a));
    2720              : 
    2721              :   /* >= */
    2722           12 :   ASSERT_TRUE (wi::ges_p (a, a));
    2723           12 :   ASSERT_TRUE (wi::ges_p (a, b));
    2724           12 :   ASSERT_FALSE (wi::ges_p (b, a));
    2725              : 
    2726              :   /* comparison */
    2727           12 :   ASSERT_EQ (-1, wi::cmps (b, a));
    2728           12 :   ASSERT_EQ (0, wi::cmps (a, a));
    2729           12 :   ASSERT_EQ (1, wi::cmps (a, b));
    2730           12 : }
    2731              : 
    2732              : /* Run all of the selftests, using the given VALUE_TYPE.  */
    2733              : 
    2734              : template <class VALUE_TYPE>
    2735           12 : static void run_all_wide_int_tests ()
    2736              : {
    2737           12 :   test_printing <VALUE_TYPE> ();
    2738           12 :   test_ops <VALUE_TYPE> ();
    2739           12 :   test_comparisons <VALUE_TYPE> ();
    2740           12 : }
    2741              : 
    2742              : /* Test overflow conditions.  */
    2743              : 
    2744              : static void
    2745            4 : test_overflow ()
    2746              : {
    2747            4 :   static int precs[] = { 31, 32, 33, 63, 64, 65, 127, 128 };
    2748            4 :   static int offsets[] = { 16, 1, 0 };
    2749           36 :   for (unsigned int i = 0; i < ARRAY_SIZE (precs); ++i)
    2750          128 :     for (unsigned int j = 0; j < ARRAY_SIZE (offsets); ++j)
    2751              :       {
    2752           96 :         int prec = precs[i];
    2753           96 :         int offset = offsets[j];
    2754           96 :         wi::overflow_type overflow;
    2755           96 :         wide_int sum, diff;
    2756              : 
    2757          192 :         sum = wi::add (wi::max_value (prec, UNSIGNED) - offset, 1,
    2758           96 :                        UNSIGNED, &overflow);
    2759           96 :         ASSERT_EQ (sum, -offset);
    2760           96 :         ASSERT_EQ (overflow != wi::OVF_NONE, offset == 0);
    2761              : 
    2762          192 :         sum = wi::add (1, wi::max_value (prec, UNSIGNED) - offset,
    2763           96 :                        UNSIGNED, &overflow);
    2764           96 :         ASSERT_EQ (sum, -offset);
    2765           96 :         ASSERT_EQ (overflow != wi::OVF_NONE, offset == 0);
    2766              : 
    2767          192 :         diff = wi::sub (wi::max_value (prec, UNSIGNED) - offset,
    2768           96 :                         wi::max_value (prec, UNSIGNED),
    2769           96 :                         UNSIGNED, &overflow);
    2770           96 :         ASSERT_EQ (diff, -offset);
    2771           96 :         ASSERT_EQ (overflow != wi::OVF_NONE, offset != 0);
    2772              : 
    2773          192 :         diff = wi::sub (wi::max_value (prec, UNSIGNED) - offset,
    2774          192 :                         wi::max_value (prec, UNSIGNED) - 1,
    2775           96 :                         UNSIGNED, &overflow);
    2776           96 :         ASSERT_EQ (diff, 1 - offset);
    2777           96 :         ASSERT_EQ (overflow != wi::OVF_NONE, offset > 1);
    2778           96 :     }
    2779            4 : }
    2780              : 
    2781              : /* Test the round_{down,up}_for_mask functions.  */
    2782              : 
    2783              : static void
    2784            4 : test_round_for_mask ()
    2785              : {
    2786            4 :   unsigned int prec = 18;
    2787            4 :   ASSERT_EQ (17, wi::round_down_for_mask (wi::shwi (17, prec),
    2788              :                                           wi::shwi (0xf1, prec)));
    2789            4 :   ASSERT_EQ (17, wi::round_up_for_mask (wi::shwi (17, prec),
    2790              :                                         wi::shwi (0xf1, prec)));
    2791              : 
    2792            4 :   ASSERT_EQ (1, wi::round_down_for_mask (wi::shwi (6, prec),
    2793              :                                          wi::shwi (0xf1, prec)));
    2794            4 :   ASSERT_EQ (16, wi::round_up_for_mask (wi::shwi (6, prec),
    2795              :                                         wi::shwi (0xf1, prec)));
    2796              : 
    2797            4 :   ASSERT_EQ (17, wi::round_down_for_mask (wi::shwi (24, prec),
    2798              :                                           wi::shwi (0xf1, prec)));
    2799            4 :   ASSERT_EQ (32, wi::round_up_for_mask (wi::shwi (24, prec),
    2800              :                                         wi::shwi (0xf1, prec)));
    2801              : 
    2802            4 :   ASSERT_EQ (0x011, wi::round_down_for_mask (wi::shwi (0x22, prec),
    2803              :                                              wi::shwi (0x111, prec)));
    2804            4 :   ASSERT_EQ (0x100, wi::round_up_for_mask (wi::shwi (0x22, prec),
    2805              :                                            wi::shwi (0x111, prec)));
    2806              : 
    2807            4 :   ASSERT_EQ (100, wi::round_down_for_mask (wi::shwi (101, prec),
    2808              :                                            wi::shwi (0xfc, prec)));
    2809            4 :   ASSERT_EQ (104, wi::round_up_for_mask (wi::shwi (101, prec),
    2810              :                                          wi::shwi (0xfc, prec)));
    2811              : 
    2812            4 :   ASSERT_EQ (0x2bc, wi::round_down_for_mask (wi::shwi (0x2c2, prec),
    2813              :                                              wi::shwi (0xabc, prec)));
    2814            4 :   ASSERT_EQ (0x800, wi::round_up_for_mask (wi::shwi (0x2c2, prec),
    2815              :                                            wi::shwi (0xabc, prec)));
    2816              : 
    2817            4 :   ASSERT_EQ (0xabc, wi::round_down_for_mask (wi::shwi (0xabd, prec),
    2818              :                                              wi::shwi (0xabc, prec)));
    2819            4 :   ASSERT_EQ (0, wi::round_up_for_mask (wi::shwi (0xabd, prec),
    2820              :                                        wi::shwi (0xabc, prec)));
    2821              : 
    2822            4 :   ASSERT_EQ (0xabc, wi::round_down_for_mask (wi::shwi (0x1000, prec),
    2823              :                                              wi::shwi (0xabc, prec)));
    2824            4 :   ASSERT_EQ (0, wi::round_up_for_mask (wi::shwi (0x1000, prec),
    2825              :                                        wi::shwi (0xabc, prec)));
    2826            4 : }
    2827              : 
    2828              : /* Run all of the selftests within this file, for all value types.  */
    2829              : 
    2830              : void
    2831            4 : wide_int_cc_tests ()
    2832              : {
    2833            4 :   run_all_wide_int_tests <wide_int> ();
    2834            4 :   run_all_wide_int_tests <offset_int> ();
    2835            4 :   run_all_wide_int_tests <widest_int> ();
    2836            4 :   test_overflow ();
    2837            4 :   test_round_for_mask ();
    2838            4 :   ASSERT_EQ (wi::mask (128, false, 128),
    2839              :              wi::shifted_mask (0, 128, false, 128));
    2840            4 :   ASSERT_EQ (wi::mask (128, true, 128),
    2841              :              wi::shifted_mask (0, 128, true, 128));
    2842            4 :   ASSERT_EQ (wi::multiple_of_p (from_int <widest_int> (1),
    2843              :                                 from_int <widest_int> (-128), UNSIGNED),
    2844              :              false);
    2845            4 : }
    2846              : 
    2847              : } // namespace selftest
    2848              : #endif /* CHECKING_P */
        

Generated by: LCOV version 2.6-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.