xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/wide-int.cc (revision 5dd36a3bc8bf2a9dec29ceb6349550414570c447)
1 /* Operations with very long integers.
2    Copyright (C) 2012-2017 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 #include "wide-int-print.h"
28 
29 
30 #define HOST_BITS_PER_HALF_WIDE_INT 32
31 #if HOST_BITS_PER_HALF_WIDE_INT == HOST_BITS_PER_LONG
32 # define HOST_HALF_WIDE_INT long
33 #elif HOST_BITS_PER_HALF_WIDE_INT == HOST_BITS_PER_INT
34 # define HOST_HALF_WIDE_INT int
35 #else
36 #error Please add support for HOST_HALF_WIDE_INT
37 #endif
38 
39 #define W_TYPE_SIZE HOST_BITS_PER_WIDE_INT
40 /* Do not include longlong.h when compiler is clang-based. See PR61146.  */
41 #if GCC_VERSION >= 3000 && (W_TYPE_SIZE == 32 || defined (__SIZEOF_INT128__)) && !defined(__clang__)
42 typedef unsigned HOST_HALF_WIDE_INT UHWtype;
43 typedef unsigned HOST_WIDE_INT UWtype;
44 typedef unsigned int UQItype __attribute__ ((mode (QI)));
45 typedef unsigned int USItype __attribute__ ((mode (SI)));
46 typedef unsigned int UDItype __attribute__ ((mode (DI)));
47 #if W_TYPE_SIZE == 32
48 typedef unsigned int UDWtype __attribute__ ((mode (DI)));
49 #else
50 typedef unsigned int UDWtype __attribute__ ((mode (TI)));
51 #endif
52 #include "longlong.h"
53 #endif
54 
55 static const HOST_WIDE_INT zeros[WIDE_INT_MAX_ELTS] = {};
56 
57 /*
58  * Internal utilities.
59  */
60 
61 /* Quantities to deal with values that hold half of a wide int.  Used
62    in multiply and divide.  */
63 #define HALF_INT_MASK ((HOST_WIDE_INT_1 << HOST_BITS_PER_HALF_WIDE_INT) - 1)
64 
65 #define BLOCK_OF(TARGET) ((TARGET) / HOST_BITS_PER_WIDE_INT)
66 #define BLOCKS_NEEDED(PREC) \
67   (PREC ? (((PREC) + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT) : 1)
68 #define SIGN_MASK(X) ((HOST_WIDE_INT) (X) < 0 ? -1 : 0)
69 
70 /* Return the value a VAL[I] if I < LEN, otherwise, return 0 or -1
71    based on the top existing bit of VAL. */
72 
73 static unsigned HOST_WIDE_INT
74 safe_uhwi (const HOST_WIDE_INT *val, unsigned int len, unsigned int i)
75 {
76   return i < len ? val[i] : val[len - 1] < 0 ? HOST_WIDE_INT_M1 : 0;
77 }
78 
79 /* Convert the integer in VAL to canonical form, returning its new length.
80    LEN is the number of blocks currently in VAL and PRECISION is the number
81    of bits in the integer it represents.
82 
83    This function only changes the representation, not the value.  */
84 static unsigned int
85 canonize (HOST_WIDE_INT *val, unsigned int len, unsigned int precision)
86 {
87   unsigned int blocks_needed = BLOCKS_NEEDED (precision);
88   HOST_WIDE_INT top;
89   int i;
90 
91   if (len > blocks_needed)
92     len = blocks_needed;
93 
94   if (len == 1)
95     return len;
96 
97   top = val[len - 1];
98   if (len * HOST_BITS_PER_WIDE_INT > precision)
99     val[len - 1] = top = sext_hwi (top, precision % HOST_BITS_PER_WIDE_INT);
100   if (top != 0 && top != (HOST_WIDE_INT)-1)
101     return len;
102 
103   /* At this point we know that the top is either 0 or -1.  Find the
104      first block that is not a copy of this.  */
105   for (i = len - 2; i >= 0; i--)
106     {
107       HOST_WIDE_INT x = val[i];
108       if (x != top)
109 	{
110 	  if (SIGN_MASK (x) == top)
111 	    return i + 1;
112 
113 	  /* We need an extra block because the top bit block i does
114 	     not match the extension.  */
115 	  return i + 2;
116 	}
117     }
118 
119   /* The number is 0 or -1.  */
120   return 1;
121 }
122 
123 /* VAL[0] is the unsigned result of an operation.  Canonize it by adding
124    another 0 block if needed, and return number of blocks needed.  */
125 
126 static inline unsigned int
127 canonize_uhwi (HOST_WIDE_INT *val, unsigned int precision)
128 {
129   if (val[0] < 0 && precision > HOST_BITS_PER_WIDE_INT)
130     {
131       val[1] = 0;
132       return 2;
133     }
134   return 1;
135 }
136 
137 /*
138  * Conversion routines in and out of wide_int.
139  */
140 
141 /* Copy XLEN elements from XVAL to VAL.  If NEED_CANON, canonize the
142    result for an integer with precision PRECISION.  Return the length
143    of VAL (after any canonization.  */
144 unsigned int
145 wi::from_array (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
146 		unsigned int xlen, unsigned int precision, bool need_canon)
147 {
148   for (unsigned i = 0; i < xlen; i++)
149     val[i] = xval[i];
150   return need_canon ? canonize (val, xlen, precision) : xlen;
151 }
152 
153 /* Construct a wide int from a buffer of length LEN.  BUFFER will be
154    read according to byte endianness and word endianness of the target.
155    Only the lower BUFFER_LEN bytes of the result are set; the remaining
156    high bytes are cleared.  */
157 wide_int
158 wi::from_buffer (const unsigned char *buffer, unsigned int buffer_len)
159 {
160   unsigned int precision = buffer_len * BITS_PER_UNIT;
161   wide_int result = wide_int::create (precision);
162   unsigned int words = buffer_len / UNITS_PER_WORD;
163 
164   /* We have to clear all the bits ourself, as we merely or in values
165      below.  */
166   unsigned int len = BLOCKS_NEEDED (precision);
167   HOST_WIDE_INT *val = result.write_val ();
168   for (unsigned int i = 0; i < len; ++i)
169     val[i] = 0;
170 
171   for (unsigned int byte = 0; byte < buffer_len; byte++)
172     {
173       unsigned int offset;
174       unsigned int index;
175       unsigned int bitpos = byte * BITS_PER_UNIT;
176       unsigned HOST_WIDE_INT value;
177 
178       if (buffer_len > UNITS_PER_WORD)
179 	{
180 	  unsigned int word = byte / UNITS_PER_WORD;
181 
182 	  if (WORDS_BIG_ENDIAN)
183 	    word = (words - 1) - word;
184 
185 	  offset = word * UNITS_PER_WORD;
186 
187 	  if (BYTES_BIG_ENDIAN)
188 	    offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
189 	  else
190 	    offset += byte % UNITS_PER_WORD;
191 	}
192       else
193 	offset = BYTES_BIG_ENDIAN ? (buffer_len - 1) - byte : byte;
194 
195       value = (unsigned HOST_WIDE_INT) buffer[offset];
196 
197       index = bitpos / HOST_BITS_PER_WIDE_INT;
198       val[index] |= value << (bitpos % HOST_BITS_PER_WIDE_INT);
199     }
200 
201   result.set_len (canonize (val, len, precision));
202 
203   return result;
204 }
205 
206 /* Sets RESULT from X, the sign is taken according to SGN.  */
207 void
208 wi::to_mpz (const wide_int_ref &x, mpz_t result, signop sgn)
209 {
210   int len = x.get_len ();
211   const HOST_WIDE_INT *v = x.get_val ();
212   int excess = len * HOST_BITS_PER_WIDE_INT - x.get_precision ();
213 
214   if (wi::neg_p (x, sgn))
215     {
216       /* We use ones complement to avoid -x80..0 edge case that -
217 	 won't work on.  */
218       HOST_WIDE_INT *t = XALLOCAVEC (HOST_WIDE_INT, len);
219       for (int i = 0; i < len; i++)
220 	t[i] = ~v[i];
221       if (excess > 0)
222 	t[len - 1] = (unsigned HOST_WIDE_INT) t[len - 1] << excess >> excess;
223       mpz_import (result, len, -1, sizeof (HOST_WIDE_INT), 0, 0, t);
224       mpz_com (result, result);
225     }
226   else if (excess > 0)
227     {
228       HOST_WIDE_INT *t = XALLOCAVEC (HOST_WIDE_INT, len);
229       for (int i = 0; i < len - 1; i++)
230 	t[i] = v[i];
231       t[len - 1] = (unsigned HOST_WIDE_INT) v[len - 1] << excess >> excess;
232       mpz_import (result, len, -1, sizeof (HOST_WIDE_INT), 0, 0, t);
233     }
234   else
235     mpz_import (result, len, -1, sizeof (HOST_WIDE_INT), 0, 0, v);
236 }
237 
238 /* Returns X converted to TYPE.  If WRAP is true, then out-of-range
239    values of VAL will be wrapped; otherwise, they will be set to the
240    appropriate minimum or maximum TYPE bound.  */
241 wide_int
242 wi::from_mpz (const_tree type, mpz_t x, bool wrap)
243 {
244   size_t count, numb;
245   unsigned int prec = TYPE_PRECISION (type);
246   wide_int res = wide_int::create (prec);
247 
248   if (!wrap)
249     {
250       mpz_t min, max;
251 
252       mpz_init (min);
253       mpz_init (max);
254       get_type_static_bounds (type, min, max);
255 
256       if (mpz_cmp (x, min) < 0)
257 	mpz_set (x, min);
258       else if (mpz_cmp (x, max) > 0)
259 	mpz_set (x, max);
260 
261       mpz_clear (min);
262       mpz_clear (max);
263     }
264 
265   /* Determine the number of unsigned HOST_WIDE_INTs that are required
266      for representing the absolute value.  The code to calculate count is
267      extracted from the GMP manual, section "Integer Import and Export":
268      http://gmplib.org/manual/Integer-Import-and-Export.html  */
269   numb = CHAR_BIT * sizeof (HOST_WIDE_INT);
270   count = (mpz_sizeinbase (x, 2) + numb - 1) / numb;
271   HOST_WIDE_INT *val = res.write_val ();
272   /* Read the absolute value.
273 
274      Write directly to the wide_int storage if possible, otherwise leave
275      GMP to allocate the memory for us.  It might be slightly more efficient
276      to use mpz_tdiv_r_2exp for the latter case, but the situation is
277      pathological and it seems safer to operate on the original mpz value
278      in all cases.  */
279   void *valres = mpz_export (count <= WIDE_INT_MAX_ELTS ? val : 0,
280 			     &count, -1, sizeof (HOST_WIDE_INT), 0, 0, x);
281   if (count < 1)
282     {
283       val[0] = 0;
284       count = 1;
285     }
286   count = MIN (count, BLOCKS_NEEDED (prec));
287   if (valres != val)
288     {
289       memcpy (val, valres, count * sizeof (HOST_WIDE_INT));
290       free (valres);
291     }
292   /* Zero-extend the absolute value to PREC bits.  */
293   if (count < BLOCKS_NEEDED (prec) && val[count - 1] < 0)
294     val[count++] = 0;
295   else
296     count = canonize (val, count, prec);
297   res.set_len (count);
298 
299   if (mpz_sgn (x) < 0)
300     res = -res;
301 
302   return res;
303 }
304 
305 /*
306  * Largest and smallest values in a mode.
307  */
308 
309 /* Return the largest SGNed number that is representable in PRECISION bits.
310 
311    TODO: There is still code from the double_int era that trys to
312    make up for the fact that double int's could not represent the
313    min and max values of all types.  This code should be removed
314    because the min and max values can always be represented in
315    wide_ints and int-csts.  */
316 wide_int
317 wi::max_value (unsigned int precision, signop sgn)
318 {
319   gcc_checking_assert (precision != 0);
320   if (sgn == UNSIGNED)
321     /* The unsigned max is just all ones.  */
322     return shwi (-1, precision);
323   else
324     /* The signed max is all ones except the top bit.  This must be
325        explicitly represented.  */
326     return mask (precision - 1, false, precision);
327 }
328 
329 /* Return the largest SGNed number that is representable in PRECISION bits.  */
330 wide_int
331 wi::min_value (unsigned int precision, signop sgn)
332 {
333   gcc_checking_assert (precision != 0);
334   if (sgn == UNSIGNED)
335     return uhwi (0, precision);
336   else
337     /* The signed min is all zeros except the top bit.  This must be
338        explicitly represented.  */
339     return wi::set_bit_in_zero (precision - 1, precision);
340 }
341 
342 /*
343  * Public utilities.
344  */
345 
346 /* Convert the number represented by XVAL, XLEN and XPRECISION, which has
347    signedness SGN, to an integer that has PRECISION bits.  Store the blocks
348    in VAL and return the number of blocks used.
349 
350    This function can handle both extension (PRECISION > XPRECISION)
351    and truncation (PRECISION < XPRECISION).  */
352 unsigned int
353 wi::force_to_size (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
354 		   unsigned int xlen, unsigned int xprecision,
355 		   unsigned int precision, signop sgn)
356 {
357   unsigned int blocks_needed = BLOCKS_NEEDED (precision);
358   unsigned int len = blocks_needed < xlen ? blocks_needed : xlen;
359   for (unsigned i = 0; i < len; i++)
360     val[i] = xval[i];
361 
362   if (precision > xprecision)
363     {
364       unsigned int small_xprecision = xprecision % HOST_BITS_PER_WIDE_INT;
365 
366       /* Expanding.  */
367       if (sgn == UNSIGNED)
368 	{
369 	  if (small_xprecision && len == BLOCKS_NEEDED (xprecision))
370 	    val[len - 1] = zext_hwi (val[len - 1], small_xprecision);
371 	  else if (val[len - 1] < 0)
372 	    {
373 	      while (len < BLOCKS_NEEDED (xprecision))
374 		val[len++] = -1;
375 	      if (small_xprecision)
376 		val[len - 1] = zext_hwi (val[len - 1], small_xprecision);
377 	      else
378 		val[len++] = 0;
379 	    }
380 	}
381       else
382 	{
383 	  if (small_xprecision && len == BLOCKS_NEEDED (xprecision))
384 	    val[len - 1] = sext_hwi (val[len - 1], small_xprecision);
385 	}
386     }
387   len = canonize (val, len, precision);
388 
389   return len;
390 }
391 
392 /* This function hides the fact that we cannot rely on the bits beyond
393    the precision.  This issue comes up in the relational comparisions
394    where we do allow comparisons of values of different precisions.  */
395 static inline HOST_WIDE_INT
396 selt (const HOST_WIDE_INT *a, unsigned int len,
397       unsigned int blocks_needed, unsigned int small_prec,
398       unsigned int index, signop sgn)
399 {
400   HOST_WIDE_INT val;
401   if (index < len)
402     val = a[index];
403   else if (index < blocks_needed || sgn == SIGNED)
404     /* Signed or within the precision.  */
405     val = SIGN_MASK (a[len - 1]);
406   else
407     /* Unsigned extension beyond the precision. */
408     val = 0;
409 
410   if (small_prec && index == blocks_needed - 1)
411     return (sgn == SIGNED
412 	    ? sext_hwi (val, small_prec)
413 	    : zext_hwi (val, small_prec));
414   else
415     return val;
416 }
417 
418 /* Find the highest bit represented in a wide int.  This will in
419    general have the same value as the sign bit.  */
420 static inline HOST_WIDE_INT
421 top_bit_of (const HOST_WIDE_INT *a, unsigned int len, unsigned int prec)
422 {
423   int excess = len * HOST_BITS_PER_WIDE_INT - prec;
424   unsigned HOST_WIDE_INT val = a[len - 1];
425   if (excess > 0)
426     val <<= excess;
427   return val >> (HOST_BITS_PER_WIDE_INT - 1);
428 }
429 
430 /*
431  * Comparisons, note that only equality is an operator.  The other
432  * comparisons cannot be operators since they are inherently signed or
433  * unsigned and C++ has no such operators.
434  */
435 
436 /* Return true if OP0 == OP1.  */
437 bool
438 wi::eq_p_large (const HOST_WIDE_INT *op0, unsigned int op0len,
439 		const HOST_WIDE_INT *op1, unsigned int op1len,
440 		unsigned int prec)
441 {
442   int l0 = op0len - 1;
443   unsigned int small_prec = prec & (HOST_BITS_PER_WIDE_INT - 1);
444 
445   if (op0len != op1len)
446     return false;
447 
448   if (op0len == BLOCKS_NEEDED (prec) && small_prec)
449     {
450       /* It does not matter if we zext or sext here, we just have to
451 	 do both the same way.  */
452       if (zext_hwi (op0 [l0], small_prec) != zext_hwi (op1 [l0], small_prec))
453 	return false;
454       l0--;
455     }
456 
457   while (l0 >= 0)
458     if (op0[l0] != op1[l0])
459       return false;
460     else
461       l0--;
462 
463   return true;
464 }
465 
466 /* Return true if OP0 < OP1 using signed comparisons.  */
467 bool
468 wi::lts_p_large (const HOST_WIDE_INT *op0, unsigned int op0len,
469 		 unsigned int precision,
470 		 const HOST_WIDE_INT *op1, unsigned int op1len)
471 {
472   HOST_WIDE_INT s0, s1;
473   unsigned HOST_WIDE_INT u0, u1;
474   unsigned int blocks_needed = BLOCKS_NEEDED (precision);
475   unsigned int small_prec = precision & (HOST_BITS_PER_WIDE_INT - 1);
476   int l = MAX (op0len - 1, op1len - 1);
477 
478   /* Only the top block is compared as signed.  The rest are unsigned
479      comparisons.  */
480   s0 = selt (op0, op0len, blocks_needed, small_prec, l, SIGNED);
481   s1 = selt (op1, op1len, blocks_needed, small_prec, l, SIGNED);
482   if (s0 < s1)
483     return true;
484   if (s0 > s1)
485     return false;
486 
487   l--;
488   while (l >= 0)
489     {
490       u0 = selt (op0, op0len, blocks_needed, small_prec, l, SIGNED);
491       u1 = selt (op1, op1len, blocks_needed, small_prec, l, SIGNED);
492 
493       if (u0 < u1)
494 	return true;
495       if (u0 > u1)
496 	return false;
497       l--;
498     }
499 
500   return false;
501 }
502 
503 /* Returns -1 if OP0 < OP1, 0 if OP0 == OP1 and 1 if OP0 > OP1 using
504    signed compares.  */
505 int
506 wi::cmps_large (const HOST_WIDE_INT *op0, unsigned int op0len,
507 		unsigned int precision,
508 		const HOST_WIDE_INT *op1, unsigned int op1len)
509 {
510   HOST_WIDE_INT s0, s1;
511   unsigned HOST_WIDE_INT u0, u1;
512   unsigned int blocks_needed = BLOCKS_NEEDED (precision);
513   unsigned int small_prec = precision & (HOST_BITS_PER_WIDE_INT - 1);
514   int l = MAX (op0len - 1, op1len - 1);
515 
516   /* Only the top block is compared as signed.  The rest are unsigned
517      comparisons.  */
518   s0 = selt (op0, op0len, blocks_needed, small_prec, l, SIGNED);
519   s1 = selt (op1, op1len, blocks_needed, small_prec, l, SIGNED);
520   if (s0 < s1)
521     return -1;
522   if (s0 > s1)
523     return 1;
524 
525   l--;
526   while (l >= 0)
527     {
528       u0 = selt (op0, op0len, blocks_needed, small_prec, l, SIGNED);
529       u1 = selt (op1, op1len, blocks_needed, small_prec, l, SIGNED);
530 
531       if (u0 < u1)
532 	return -1;
533       if (u0 > u1)
534 	return 1;
535       l--;
536     }
537 
538   return 0;
539 }
540 
541 /* Return true if OP0 < OP1 using unsigned comparisons.  */
542 bool
543 wi::ltu_p_large (const HOST_WIDE_INT *op0, unsigned int op0len,
544 		 unsigned int precision,
545 		 const HOST_WIDE_INT *op1, unsigned int op1len)
546 {
547   unsigned HOST_WIDE_INT x0;
548   unsigned HOST_WIDE_INT x1;
549   unsigned int blocks_needed = BLOCKS_NEEDED (precision);
550   unsigned int small_prec = precision & (HOST_BITS_PER_WIDE_INT - 1);
551   int l = MAX (op0len - 1, op1len - 1);
552 
553   while (l >= 0)
554     {
555       x0 = selt (op0, op0len, blocks_needed, small_prec, l, UNSIGNED);
556       x1 = selt (op1, op1len, blocks_needed, small_prec, l, UNSIGNED);
557       if (x0 < x1)
558 	return true;
559       if (x0 > x1)
560 	return false;
561       l--;
562     }
563 
564   return false;
565 }
566 
567 /* Returns -1 if OP0 < OP1, 0 if OP0 == OP1 and 1 if OP0 > OP1 using
568    unsigned compares.  */
569 int
570 wi::cmpu_large (const HOST_WIDE_INT *op0, unsigned int op0len,
571 		unsigned int precision,
572 		const HOST_WIDE_INT *op1, unsigned int op1len)
573 {
574   unsigned HOST_WIDE_INT x0;
575   unsigned HOST_WIDE_INT x1;
576   unsigned int blocks_needed = BLOCKS_NEEDED (precision);
577   unsigned int small_prec = precision & (HOST_BITS_PER_WIDE_INT - 1);
578   int l = MAX (op0len - 1, op1len - 1);
579 
580   while (l >= 0)
581     {
582       x0 = selt (op0, op0len, blocks_needed, small_prec, l, UNSIGNED);
583       x1 = selt (op1, op1len, blocks_needed, small_prec, l, UNSIGNED);
584       if (x0 < x1)
585 	return -1;
586       if (x0 > x1)
587 	return 1;
588       l--;
589     }
590 
591   return 0;
592 }
593 
594 /*
595  * Extension.
596  */
597 
598 /* Sign-extend the number represented by XVAL and XLEN into VAL,
599    starting at OFFSET.  Return the number of blocks in VAL.  Both XVAL
600    and VAL have PRECISION bits.  */
601 unsigned int
602 wi::sext_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
603 		unsigned int xlen, unsigned int precision, unsigned int offset)
604 {
605   unsigned int len = offset / HOST_BITS_PER_WIDE_INT;
606   /* Extending beyond the precision is a no-op.  If we have only stored
607      OFFSET bits or fewer, the rest are already signs.  */
608   if (offset >= precision || len >= xlen)
609     {
610       for (unsigned i = 0; i < xlen; ++i)
611 	val[i] = xval[i];
612       return xlen;
613     }
614   unsigned int suboffset = offset % HOST_BITS_PER_WIDE_INT;
615   for (unsigned int i = 0; i < len; i++)
616     val[i] = xval[i];
617   if (suboffset > 0)
618     {
619       val[len] = sext_hwi (xval[len], suboffset);
620       len += 1;
621     }
622   return canonize (val, len, precision);
623 }
624 
625 /* Zero-extend the number represented by XVAL and XLEN into VAL,
626    starting at OFFSET.  Return the number of blocks in VAL.  Both XVAL
627    and VAL have PRECISION bits.  */
628 unsigned int
629 wi::zext_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
630 		unsigned int xlen, unsigned int precision, unsigned int offset)
631 {
632   unsigned int len = offset / HOST_BITS_PER_WIDE_INT;
633   /* Extending beyond the precision is a no-op.  If we have only stored
634      OFFSET bits or fewer, and the upper stored bit is zero, then there
635      is nothing to do.  */
636   if (offset >= precision || (len >= xlen && xval[xlen - 1] >= 0))
637     {
638       for (unsigned i = 0; i < xlen; ++i)
639 	val[i] = xval[i];
640       return xlen;
641     }
642   unsigned int suboffset = offset % HOST_BITS_PER_WIDE_INT;
643   for (unsigned int i = 0; i < len; i++)
644     val[i] = i < xlen ? xval[i] : -1;
645   if (suboffset > 0)
646     val[len] = zext_hwi (len < xlen ? xval[len] : -1, suboffset);
647   else
648     val[len] = 0;
649   return canonize (val, len + 1, precision);
650 }
651 
652 /*
653  * Masking, inserting, shifting, rotating.
654  */
655 
656 /* Insert WIDTH bits from Y into X starting at START.  */
657 wide_int
658 wi::insert (const wide_int &x, const wide_int &y, unsigned int start,
659 	    unsigned int width)
660 {
661   wide_int result;
662   wide_int mask;
663   wide_int tmp;
664 
665   unsigned int precision = x.get_precision ();
666   if (start >= precision)
667     return x;
668 
669   gcc_checking_assert (precision >= width);
670 
671   if (start + width >= precision)
672     width = precision - start;
673 
674   mask = wi::shifted_mask (start, width, false, precision);
675   tmp = wi::lshift (wide_int::from (y, precision, UNSIGNED), start);
676   result = tmp & mask;
677 
678   tmp = wi::bit_and_not (x, mask);
679   result = result | tmp;
680 
681   return result;
682 }
683 
684 /* Copy the number represented by XVAL and XLEN into VAL, setting bit BIT.
685    Return the number of blocks in VAL.  Both XVAL and VAL have PRECISION
686    bits.  */
687 unsigned int
688 wi::set_bit_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
689 		   unsigned int xlen, unsigned int precision, unsigned int bit)
690 {
691   unsigned int block = bit / HOST_BITS_PER_WIDE_INT;
692   unsigned int subbit = bit % HOST_BITS_PER_WIDE_INT;
693 
694   if (block + 1 >= xlen)
695     {
696       /* The operation either affects the last current block or needs
697 	 a new block.  */
698       unsigned int len = block + 1;
699       for (unsigned int i = 0; i < len; i++)
700 	val[i] = safe_uhwi (xval, xlen, i);
701       val[block] |= HOST_WIDE_INT_1U << subbit;
702 
703       /* If the bit we just set is at the msb of the block, make sure
704 	 that any higher bits are zeros.  */
705       if (bit + 1 < precision && subbit == HOST_BITS_PER_WIDE_INT - 1)
706 	val[len++] = 0;
707       return len;
708     }
709   else
710     {
711       for (unsigned int i = 0; i < xlen; i++)
712 	val[i] = xval[i];
713       val[block] |= HOST_WIDE_INT_1U << subbit;
714       return canonize (val, xlen, precision);
715     }
716 }
717 
718 /* bswap THIS.  */
719 wide_int
720 wide_int_storage::bswap () const
721 {
722   wide_int result = wide_int::create (precision);
723   unsigned int i, s;
724   unsigned int len = BLOCKS_NEEDED (precision);
725   unsigned int xlen = get_len ();
726   const HOST_WIDE_INT *xval = get_val ();
727   HOST_WIDE_INT *val = result.write_val ();
728 
729   /* This is not a well defined operation if the precision is not a
730      multiple of 8.  */
731   gcc_assert ((precision & 0x7) == 0);
732 
733   for (i = 0; i < len; i++)
734     val[i] = 0;
735 
736   /* Only swap the bytes that are not the padding.  */
737   for (s = 0; s < precision; s += 8)
738     {
739       unsigned int d = precision - s - 8;
740       unsigned HOST_WIDE_INT byte;
741 
742       unsigned int block = s / HOST_BITS_PER_WIDE_INT;
743       unsigned int offset = s & (HOST_BITS_PER_WIDE_INT - 1);
744 
745       byte = (safe_uhwi (xval, xlen, block) >> offset) & 0xff;
746 
747       block = d / HOST_BITS_PER_WIDE_INT;
748       offset = d & (HOST_BITS_PER_WIDE_INT - 1);
749 
750       val[block] |= byte << offset;
751     }
752 
753   result.set_len (canonize (val, len, precision));
754   return result;
755 }
756 
757 /* Fill VAL with a mask where the lower WIDTH bits are ones and the bits
758    above that up to PREC are zeros.  The result is inverted if NEGATE
759    is true.  Return the number of blocks in VAL.  */
760 unsigned int
761 wi::mask (HOST_WIDE_INT *val, unsigned int width, bool negate,
762 	  unsigned int prec)
763 {
764   if (width >= prec)
765     {
766       val[0] = negate ? 0 : -1;
767       return 1;
768     }
769   else if (width == 0)
770     {
771       val[0] = negate ? -1 : 0;
772       return 1;
773     }
774 
775   unsigned int i = 0;
776   while (i < width / HOST_BITS_PER_WIDE_INT)
777     val[i++] = negate ? 0 : -1;
778 
779   unsigned int shift = width & (HOST_BITS_PER_WIDE_INT - 1);
780   if (shift != 0)
781     {
782       HOST_WIDE_INT last = (HOST_WIDE_INT_1U << shift) - 1;
783       val[i++] = negate ? ~last : last;
784     }
785   else
786     val[i++] = negate ? -1 : 0;
787 
788   return i;
789 }
790 
791 /* Fill VAL with a mask where the lower START bits are zeros, the next WIDTH
792    bits are ones, and the bits above that up to PREC are zeros.  The result
793    is inverted if NEGATE is true.  Return the number of blocks in VAL.  */
794 unsigned int
795 wi::shifted_mask (HOST_WIDE_INT *val, unsigned int start, unsigned int width,
796 		  bool negate, unsigned int prec)
797 {
798   if (start >= prec || width == 0)
799     {
800       val[0] = negate ? -1 : 0;
801       return 1;
802     }
803 
804   if (width > prec - start)
805     width = prec - start;
806   unsigned int end = start + width;
807 
808   unsigned int i = 0;
809   while (i < start / HOST_BITS_PER_WIDE_INT)
810     val[i++] = negate ? -1 : 0;
811 
812   unsigned int shift = start & (HOST_BITS_PER_WIDE_INT - 1);
813   if (shift)
814     {
815       HOST_WIDE_INT block = (HOST_WIDE_INT_1U << shift) - 1;
816       shift += width;
817       if (shift < HOST_BITS_PER_WIDE_INT)
818 	{
819 	  /* case 000111000 */
820 	  block = (HOST_WIDE_INT_1U << shift) - block - 1;
821 	  val[i++] = negate ? ~block : block;
822 	  return i;
823 	}
824       else
825 	/* ...111000 */
826 	val[i++] = negate ? block : ~block;
827     }
828 
829   while (i < end / HOST_BITS_PER_WIDE_INT)
830     /* 1111111 */
831     val[i++] = negate ? 0 : -1;
832 
833   shift = end & (HOST_BITS_PER_WIDE_INT - 1);
834   if (shift != 0)
835     {
836       /* 000011111 */
837       HOST_WIDE_INT block = (HOST_WIDE_INT_1U << shift) - 1;
838       val[i++] = negate ? ~block : block;
839     }
840   else if (end < prec)
841     val[i++] = negate ? -1 : 0;
842 
843   return i;
844 }
845 
846 /*
847  * logical operations.
848  */
849 
850 /* Set VAL to OP0 & OP1.  Return the number of blocks used.  */
851 unsigned int
852 wi::and_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *op0,
853 	       unsigned int op0len, const HOST_WIDE_INT *op1,
854 	       unsigned int op1len, unsigned int prec)
855 {
856   int l0 = op0len - 1;
857   int l1 = op1len - 1;
858   bool need_canon = true;
859 
860   unsigned int len = MAX (op0len, op1len);
861   if (l0 > l1)
862     {
863       HOST_WIDE_INT op1mask = -top_bit_of (op1, op1len, prec);
864       if (op1mask == 0)
865 	{
866 	  l0 = l1;
867 	  len = l1 + 1;
868 	}
869       else
870 	{
871 	  need_canon = false;
872 	  while (l0 > l1)
873 	    {
874 	      val[l0] = op0[l0];
875 	      l0--;
876 	    }
877 	}
878     }
879   else if (l1 > l0)
880     {
881       HOST_WIDE_INT op0mask = -top_bit_of (op0, op0len, prec);
882       if (op0mask == 0)
883 	len = l0 + 1;
884       else
885 	{
886 	  need_canon = false;
887 	  while (l1 > l0)
888 	    {
889 	      val[l1] = op1[l1];
890 	      l1--;
891 	    }
892 	}
893     }
894 
895   while (l0 >= 0)
896     {
897       val[l0] = op0[l0] & op1[l0];
898       l0--;
899     }
900 
901   if (need_canon)
902     len = canonize (val, len, prec);
903 
904   return len;
905 }
906 
907 /* Set VAL to OP0 & ~OP1.  Return the number of blocks used.  */
908 unsigned int
909 wi::and_not_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *op0,
910 		   unsigned int op0len, const HOST_WIDE_INT *op1,
911 		   unsigned int op1len, unsigned int prec)
912 {
913   wide_int result;
914   int l0 = op0len - 1;
915   int l1 = op1len - 1;
916   bool need_canon = true;
917 
918   unsigned int len = MAX (op0len, op1len);
919   if (l0 > l1)
920     {
921       HOST_WIDE_INT op1mask = -top_bit_of (op1, op1len, prec);
922       if (op1mask != 0)
923 	{
924 	  l0 = l1;
925 	  len = l1 + 1;
926 	}
927       else
928 	{
929 	  need_canon = false;
930 	  while (l0 > l1)
931 	    {
932 	      val[l0] = op0[l0];
933 	      l0--;
934 	    }
935 	}
936     }
937   else if (l1 > l0)
938     {
939       HOST_WIDE_INT op0mask = -top_bit_of (op0, op0len, prec);
940       if (op0mask == 0)
941 	len = l0 + 1;
942       else
943 	{
944 	  need_canon = false;
945 	  while (l1 > l0)
946 	    {
947 	      val[l1] = ~op1[l1];
948 	      l1--;
949 	    }
950 	}
951     }
952 
953   while (l0 >= 0)
954     {
955       val[l0] = op0[l0] & ~op1[l0];
956       l0--;
957     }
958 
959   if (need_canon)
960     len = canonize (val, len, prec);
961 
962   return len;
963 }
964 
965 /* Set VAL to OP0 | OP1.  Return the number of blocks used.  */
966 unsigned int
967 wi::or_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *op0,
968 	      unsigned int op0len, const HOST_WIDE_INT *op1,
969 	      unsigned int op1len, unsigned int prec)
970 {
971   wide_int result;
972   int l0 = op0len - 1;
973   int l1 = op1len - 1;
974   bool need_canon = true;
975 
976   unsigned int len = MAX (op0len, op1len);
977   if (l0 > l1)
978     {
979       HOST_WIDE_INT op1mask = -top_bit_of (op1, op1len, prec);
980       if (op1mask != 0)
981 	{
982 	  l0 = l1;
983 	  len = l1 + 1;
984 	}
985       else
986 	{
987 	  need_canon = false;
988 	  while (l0 > l1)
989 	    {
990 	      val[l0] = op0[l0];
991 	      l0--;
992 	    }
993 	}
994     }
995   else if (l1 > l0)
996     {
997       HOST_WIDE_INT op0mask = -top_bit_of (op0, op0len, prec);
998       if (op0mask != 0)
999 	len = l0 + 1;
1000       else
1001 	{
1002 	  need_canon = false;
1003 	  while (l1 > l0)
1004 	    {
1005 	      val[l1] = op1[l1];
1006 	      l1--;
1007 	    }
1008 	}
1009     }
1010 
1011   while (l0 >= 0)
1012     {
1013       val[l0] = op0[l0] | op1[l0];
1014       l0--;
1015     }
1016 
1017   if (need_canon)
1018     len = canonize (val, len, prec);
1019 
1020   return len;
1021 }
1022 
1023 /* Set VAL to OP0 | ~OP1.  Return the number of blocks used.  */
1024 unsigned int
1025 wi::or_not_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *op0,
1026 		  unsigned int op0len, const HOST_WIDE_INT *op1,
1027 		  unsigned int op1len, unsigned int prec)
1028 {
1029   wide_int result;
1030   int l0 = op0len - 1;
1031   int l1 = op1len - 1;
1032   bool need_canon = true;
1033 
1034   unsigned int len = MAX (op0len, op1len);
1035   if (l0 > l1)
1036     {
1037       HOST_WIDE_INT op1mask = -top_bit_of (op1, op1len, prec);
1038       if (op1mask == 0)
1039 	{
1040 	  l0 = l1;
1041 	  len = l1 + 1;
1042 	}
1043       else
1044 	{
1045 	  need_canon = false;
1046 	  while (l0 > l1)
1047 	    {
1048 	      val[l0] = op0[l0];
1049 	      l0--;
1050 	    }
1051 	}
1052     }
1053   else if (l1 > l0)
1054     {
1055       HOST_WIDE_INT op0mask = -top_bit_of (op0, op0len, prec);
1056       if (op0mask != 0)
1057 	len = l0 + 1;
1058       else
1059 	{
1060 	  need_canon = false;
1061 	  while (l1 > l0)
1062 	    {
1063 	      val[l1] = ~op1[l1];
1064 	      l1--;
1065 	    }
1066 	}
1067     }
1068 
1069   while (l0 >= 0)
1070     {
1071       val[l0] = op0[l0] | ~op1[l0];
1072       l0--;
1073     }
1074 
1075   if (need_canon)
1076     len = canonize (val, len, prec);
1077 
1078   return len;
1079 }
1080 
1081 /* Set VAL to OP0 ^ OP1.  Return the number of blocks used.  */
1082 unsigned int
1083 wi::xor_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *op0,
1084 	       unsigned int op0len, const HOST_WIDE_INT *op1,
1085 	       unsigned int op1len, unsigned int prec)
1086 {
1087   wide_int result;
1088   int l0 = op0len - 1;
1089   int l1 = op1len - 1;
1090 
1091   unsigned int len = MAX (op0len, op1len);
1092   if (l0 > l1)
1093     {
1094       HOST_WIDE_INT op1mask = -top_bit_of (op1, op1len, prec);
1095       while (l0 > l1)
1096 	{
1097 	  val[l0] = op0[l0] ^ op1mask;
1098 	  l0--;
1099 	}
1100     }
1101 
1102   if (l1 > l0)
1103     {
1104       HOST_WIDE_INT op0mask = -top_bit_of (op0, op0len, prec);
1105       while (l1 > l0)
1106 	{
1107 	  val[l1] = op0mask ^ op1[l1];
1108 	  l1--;
1109 	}
1110     }
1111 
1112   while (l0 >= 0)
1113     {
1114       val[l0] = op0[l0] ^ op1[l0];
1115       l0--;
1116     }
1117 
1118   return canonize (val, len, prec);
1119 }
1120 
1121 /*
1122  * math
1123  */
1124 
1125 /* Set VAL to OP0 + OP1.  If OVERFLOW is nonnull, record in *OVERFLOW
1126    whether the result overflows when OP0 and OP1 are treated as having
1127    signedness SGN.  Return the number of blocks in VAL.  */
1128 unsigned int
1129 wi::add_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *op0,
1130 	       unsigned int op0len, const HOST_WIDE_INT *op1,
1131 	       unsigned int op1len, unsigned int prec,
1132 	       signop sgn, bool *overflow)
1133 {
1134   unsigned HOST_WIDE_INT o0 = 0;
1135   unsigned HOST_WIDE_INT o1 = 0;
1136   unsigned HOST_WIDE_INT x = 0;
1137   unsigned HOST_WIDE_INT carry = 0;
1138   unsigned HOST_WIDE_INT old_carry = 0;
1139   unsigned HOST_WIDE_INT mask0, mask1;
1140   unsigned int i;
1141 
1142   unsigned int len = MAX (op0len, op1len);
1143   mask0 = -top_bit_of (op0, op0len, prec);
1144   mask1 = -top_bit_of (op1, op1len, prec);
1145   /* Add all of the explicitly defined elements.  */
1146 
1147   for (i = 0; i < len; i++)
1148     {
1149       o0 = i < op0len ? (unsigned HOST_WIDE_INT) op0[i] : mask0;
1150       o1 = i < op1len ? (unsigned HOST_WIDE_INT) op1[i] : mask1;
1151       x = o0 + o1 + carry;
1152       val[i] = x;
1153       old_carry = carry;
1154       carry = carry == 0 ? x < o0 : x <= o0;
1155     }
1156 
1157   if (len * HOST_BITS_PER_WIDE_INT < prec)
1158     {
1159       val[len] = mask0 + mask1 + carry;
1160       len++;
1161       if (overflow)
1162 	*overflow = false;
1163     }
1164   else if (overflow)
1165     {
1166       unsigned int shift = -prec % HOST_BITS_PER_WIDE_INT;
1167       if (sgn == SIGNED)
1168 	{
1169 	  unsigned HOST_WIDE_INT x = (val[len - 1] ^ o0) & (val[len - 1] ^ o1);
1170 	  *overflow = (HOST_WIDE_INT) (x << shift) < 0;
1171 	}
1172       else
1173 	{
1174 	  /* Put the MSB of X and O0 and in the top of the HWI.  */
1175 	  x <<= shift;
1176 	  o0 <<= shift;
1177 	  if (old_carry)
1178 	    *overflow = (x <= o0);
1179 	  else
1180 	    *overflow = (x < o0);
1181 	}
1182     }
1183 
1184   return canonize (val, len, prec);
1185 }
1186 
1187 /* Subroutines of the multiplication and division operations.  Unpack
1188    the first IN_LEN HOST_WIDE_INTs in INPUT into 2 * IN_LEN
1189    HOST_HALF_WIDE_INTs of RESULT.  The rest of RESULT is filled by
1190    uncompressing the top bit of INPUT[IN_LEN - 1].  */
1191 static void
1192 wi_unpack (unsigned HOST_HALF_WIDE_INT *result, const HOST_WIDE_INT *input,
1193 	   unsigned int in_len, unsigned int out_len,
1194 	   unsigned int prec, signop sgn)
1195 {
1196   unsigned int i;
1197   unsigned int j = 0;
1198   unsigned int small_prec = prec & (HOST_BITS_PER_WIDE_INT - 1);
1199   unsigned int blocks_needed = BLOCKS_NEEDED (prec);
1200   HOST_WIDE_INT mask;
1201 
1202   if (sgn == SIGNED)
1203     {
1204       mask = -top_bit_of ((const HOST_WIDE_INT *) input, in_len, prec);
1205       mask &= HALF_INT_MASK;
1206     }
1207   else
1208     mask = 0;
1209 
1210   for (i = 0; i < blocks_needed - 1; i++)
1211     {
1212       HOST_WIDE_INT x = safe_uhwi (input, in_len, i);
1213       result[j++] = x;
1214       result[j++] = x >> HOST_BITS_PER_HALF_WIDE_INT;
1215     }
1216 
1217   HOST_WIDE_INT x = safe_uhwi (input, in_len, i);
1218   if (small_prec)
1219     {
1220       if (sgn == SIGNED)
1221 	x = sext_hwi (x, small_prec);
1222       else
1223 	x = zext_hwi (x, small_prec);
1224     }
1225   result[j++] = x;
1226   result[j++] = x >> HOST_BITS_PER_HALF_WIDE_INT;
1227 
1228   /* Smear the sign bit.  */
1229   while (j < out_len)
1230     result[j++] = mask;
1231 }
1232 
1233 /* The inverse of wi_unpack.  IN_LEN is the number of input
1234    blocks and PRECISION is the precision of the result.  Return the
1235    number of blocks in the canonicalized result.  */
1236 static unsigned int
1237 wi_pack (HOST_WIDE_INT *result,
1238 	 const unsigned HOST_HALF_WIDE_INT *input,
1239 	 unsigned int in_len, unsigned int precision)
1240 {
1241   unsigned int i = 0;
1242   unsigned int j = 0;
1243   unsigned int blocks_needed = BLOCKS_NEEDED (precision);
1244 
1245   while (i + 1 < in_len)
1246     {
1247       result[j++] = ((unsigned HOST_WIDE_INT) input[i]
1248 		     | ((unsigned HOST_WIDE_INT) input[i + 1]
1249 			<< HOST_BITS_PER_HALF_WIDE_INT));
1250       i += 2;
1251     }
1252 
1253   /* Handle the case where in_len is odd.   For this we zero extend.  */
1254   if (in_len & 1)
1255     result[j++] = (unsigned HOST_WIDE_INT) input[i];
1256   else if (j < blocks_needed)
1257     result[j++] = 0;
1258   return canonize (result, j, precision);
1259 }
1260 
1261 /* Multiply Op1 by Op2.  If HIGH is set, only the upper half of the
1262    result is returned.
1263 
1264    If HIGH is not set, throw away the upper half after the check is
1265    made to see if it overflows.  Unfortunately there is no better way
1266    to check for overflow than to do this.  If OVERFLOW is nonnull,
1267    record in *OVERFLOW whether the result overflowed.  SGN controls
1268    the signedness and is used to check overflow or if HIGH is set.  */
1269 unsigned int
1270 wi::mul_internal (HOST_WIDE_INT *val, const HOST_WIDE_INT *op1val,
1271 		  unsigned int op1len, const HOST_WIDE_INT *op2val,
1272 		  unsigned int op2len, unsigned int prec, signop sgn,
1273 		  bool *overflow, bool high)
1274 {
1275   unsigned HOST_WIDE_INT o0, o1, k, t;
1276   unsigned int i;
1277   unsigned int j;
1278   unsigned int blocks_needed = BLOCKS_NEEDED (prec);
1279   unsigned int half_blocks_needed = blocks_needed * 2;
1280   /* The sizes here are scaled to support a 2x largest mode by 2x
1281      largest mode yielding a 4x largest mode result.  This is what is
1282      needed by vpn.  */
1283 
1284   unsigned HOST_HALF_WIDE_INT
1285     u[4 * MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_HALF_WIDE_INT];
1286   unsigned HOST_HALF_WIDE_INT
1287     v[4 * MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_HALF_WIDE_INT];
1288   /* The '2' in 'R' is because we are internally doing a full
1289      multiply.  */
1290   unsigned HOST_HALF_WIDE_INT
1291     r[2 * 4 * MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_HALF_WIDE_INT];
1292   HOST_WIDE_INT mask = ((HOST_WIDE_INT)1 << HOST_BITS_PER_HALF_WIDE_INT) - 1;
1293 
1294   /* If the top level routine did not really pass in an overflow, then
1295      just make sure that we never attempt to set it.  */
1296   bool needs_overflow = (overflow != 0);
1297   if (needs_overflow)
1298     *overflow = false;
1299 
1300   wide_int_ref op1 = wi::storage_ref (op1val, op1len, prec);
1301   wide_int_ref op2 = wi::storage_ref (op2val, op2len, prec);
1302 
1303   /* This is a surprisingly common case, so do it first.  */
1304   if (op1 == 0 || op2 == 0)
1305     {
1306       val[0] = 0;
1307       return 1;
1308     }
1309 
1310 #ifdef umul_ppmm
1311   if (sgn == UNSIGNED)
1312     {
1313       /* If the inputs are single HWIs and the output has room for at
1314 	 least two HWIs, we can use umul_ppmm directly.  */
1315       if (prec >= HOST_BITS_PER_WIDE_INT * 2
1316 	  && wi::fits_uhwi_p (op1)
1317 	  && wi::fits_uhwi_p (op2))
1318 	{
1319 	  /* This case never overflows.  */
1320 	  if (high)
1321 	    {
1322 	      val[0] = 0;
1323 	      return 1;
1324 	    }
1325 	  umul_ppmm (val[1], val[0], op1.ulow (), op2.ulow ());
1326 	  if (val[1] < 0 && prec > HOST_BITS_PER_WIDE_INT * 2)
1327 	    {
1328 	      val[2] = 0;
1329 	      return 3;
1330 	    }
1331 	  return 1 + (val[1] != 0 || val[0] < 0);
1332 	}
1333       /* Likewise if the output is a full single HWI, except that the
1334 	 upper HWI of the result is only used for determining overflow.
1335 	 (We handle this case inline when overflow isn't needed.)  */
1336       else if (prec == HOST_BITS_PER_WIDE_INT)
1337 	{
1338 	  unsigned HOST_WIDE_INT upper;
1339 	  umul_ppmm (upper, val[0], op1.ulow (), op2.ulow ());
1340 	  if (needs_overflow)
1341 	    *overflow = (upper != 0);
1342 	  if (high)
1343 	    val[0] = upper;
1344 	  return 1;
1345 	}
1346     }
1347 #endif
1348 
1349   /* Handle multiplications by 1.  */
1350   if (op1 == 1)
1351     {
1352       if (high)
1353 	{
1354 	  val[0] = wi::neg_p (op2, sgn) ? -1 : 0;
1355 	  return 1;
1356 	}
1357       for (i = 0; i < op2len; i++)
1358 	val[i] = op2val[i];
1359       return op2len;
1360     }
1361   if (op2 == 1)
1362     {
1363       if (high)
1364 	{
1365 	  val[0] = wi::neg_p (op1, sgn) ? -1 : 0;
1366 	  return 1;
1367 	}
1368       for (i = 0; i < op1len; i++)
1369 	val[i] = op1val[i];
1370       return op1len;
1371     }
1372 
1373   /* If we need to check for overflow, we can only do half wide
1374      multiplies quickly because we need to look at the top bits to
1375      check for the overflow.  */
1376   if ((high || needs_overflow)
1377       && (prec <= HOST_BITS_PER_HALF_WIDE_INT))
1378     {
1379       unsigned HOST_WIDE_INT r;
1380 
1381       if (sgn == SIGNED)
1382 	{
1383 	  o0 = op1.to_shwi ();
1384 	  o1 = op2.to_shwi ();
1385 	}
1386       else
1387 	{
1388 	  o0 = op1.to_uhwi ();
1389 	  o1 = op2.to_uhwi ();
1390 	}
1391 
1392       r = o0 * o1;
1393       if (needs_overflow)
1394 	{
1395 	  if (sgn == SIGNED)
1396 	    {
1397 	      if ((HOST_WIDE_INT) r != sext_hwi (r, prec))
1398 		*overflow = true;
1399 	    }
1400 	  else
1401 	    {
1402 	      if ((r >> prec) != 0)
1403 		*overflow = true;
1404 	    }
1405 	}
1406       val[0] = high ? r >> prec : r;
1407       return 1;
1408     }
1409 
1410   /* We do unsigned mul and then correct it.  */
1411   wi_unpack (u, op1val, op1len, half_blocks_needed, prec, SIGNED);
1412   wi_unpack (v, op2val, op2len, half_blocks_needed, prec, SIGNED);
1413 
1414   /* The 2 is for a full mult.  */
1415   memset (r, 0, half_blocks_needed * 2
1416 	  * HOST_BITS_PER_HALF_WIDE_INT / CHAR_BIT);
1417 
1418   for (j = 0; j < half_blocks_needed; j++)
1419     {
1420       k = 0;
1421       for (i = 0; i < half_blocks_needed; i++)
1422 	{
1423 	  t = ((unsigned HOST_WIDE_INT)u[i] * (unsigned HOST_WIDE_INT)v[j]
1424 	       + r[i + j] + k);
1425 	  r[i + j] = t & HALF_INT_MASK;
1426 	  k = t >> HOST_BITS_PER_HALF_WIDE_INT;
1427 	}
1428       r[j + half_blocks_needed] = k;
1429     }
1430 
1431   /* We did unsigned math above.  For signed we must adjust the
1432      product (assuming we need to see that).  */
1433   if (sgn == SIGNED && (high || needs_overflow))
1434     {
1435       unsigned HOST_WIDE_INT b;
1436       if (wi::neg_p (op1))
1437 	{
1438 	  b = 0;
1439 	  for (i = 0; i < half_blocks_needed; i++)
1440 	    {
1441 	      t = (unsigned HOST_WIDE_INT)r[i + half_blocks_needed]
1442 		- (unsigned HOST_WIDE_INT)v[i] - b;
1443 	      r[i + half_blocks_needed] = t & HALF_INT_MASK;
1444 	      b = t >> (HOST_BITS_PER_WIDE_INT - 1);
1445 	    }
1446 	}
1447       if (wi::neg_p (op2))
1448 	{
1449 	  b = 0;
1450 	  for (i = 0; i < half_blocks_needed; i++)
1451 	    {
1452 	      t = (unsigned HOST_WIDE_INT)r[i + half_blocks_needed]
1453 		- (unsigned HOST_WIDE_INT)u[i] - b;
1454 	      r[i + half_blocks_needed] = t & HALF_INT_MASK;
1455 	      b = t >> (HOST_BITS_PER_WIDE_INT - 1);
1456 	    }
1457 	}
1458     }
1459 
1460   if (needs_overflow)
1461     {
1462       HOST_WIDE_INT top;
1463 
1464       /* For unsigned, overflow is true if any of the top bits are set.
1465 	 For signed, overflow is true if any of the top bits are not equal
1466 	 to the sign bit.  */
1467       if (sgn == UNSIGNED)
1468 	top = 0;
1469       else
1470 	{
1471 	  top = r[(half_blocks_needed) - 1];
1472 	  top = SIGN_MASK (top << (HOST_BITS_PER_WIDE_INT / 2));
1473 	  top &= mask;
1474 	}
1475 
1476       for (i = half_blocks_needed; i < half_blocks_needed * 2; i++)
1477 	if (((HOST_WIDE_INT)(r[i] & mask)) != top)
1478 	  *overflow = true;
1479     }
1480 
1481   int r_offset = high ? half_blocks_needed : 0;
1482   return wi_pack (val, &r[r_offset], half_blocks_needed, prec);
1483 }
1484 
1485 /* Compute the population count of X.  */
1486 int
1487 wi::popcount (const wide_int_ref &x)
1488 {
1489   unsigned int i;
1490   int count;
1491 
1492   /* The high order block is special if it is the last block and the
1493      precision is not an even multiple of HOST_BITS_PER_WIDE_INT.  We
1494      have to clear out any ones above the precision before doing
1495      popcount on this block.  */
1496   count = x.precision - x.len * HOST_BITS_PER_WIDE_INT;
1497   unsigned int stop = x.len;
1498   if (count < 0)
1499     {
1500       count = popcount_hwi (x.uhigh () << -count);
1501       stop -= 1;
1502     }
1503   else
1504     {
1505       if (x.sign_mask () >= 0)
1506 	count = 0;
1507     }
1508 
1509   for (i = 0; i < stop; ++i)
1510     count += popcount_hwi (x.val[i]);
1511 
1512   return count;
1513 }
1514 
1515 /* Set VAL to OP0 - OP1.  If OVERFLOW is nonnull, record in *OVERFLOW
1516    whether the result overflows when OP0 and OP1 are treated as having
1517    signedness SGN.  Return the number of blocks in VAL.  */
1518 unsigned int
1519 wi::sub_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *op0,
1520 	       unsigned int op0len, const HOST_WIDE_INT *op1,
1521 	       unsigned int op1len, unsigned int prec,
1522 	       signop sgn, bool *overflow)
1523 {
1524   unsigned HOST_WIDE_INT o0 = 0;
1525   unsigned HOST_WIDE_INT o1 = 0;
1526   unsigned HOST_WIDE_INT x = 0;
1527   /* We implement subtraction as an in place negate and add.  Negation
1528      is just inversion and add 1, so we can do the add of 1 by just
1529      starting the borrow in of the first element at 1.  */
1530   unsigned HOST_WIDE_INT borrow = 0;
1531   unsigned HOST_WIDE_INT old_borrow = 0;
1532 
1533   unsigned HOST_WIDE_INT mask0, mask1;
1534   unsigned int i;
1535 
1536   unsigned int len = MAX (op0len, op1len);
1537   mask0 = -top_bit_of (op0, op0len, prec);
1538   mask1 = -top_bit_of (op1, op1len, prec);
1539 
1540   /* Subtract all of the explicitly defined elements.  */
1541   for (i = 0; i < len; i++)
1542     {
1543       o0 = i < op0len ? (unsigned HOST_WIDE_INT)op0[i] : mask0;
1544       o1 = i < op1len ? (unsigned HOST_WIDE_INT)op1[i] : mask1;
1545       x = o0 - o1 - borrow;
1546       val[i] = x;
1547       old_borrow = borrow;
1548       borrow = borrow == 0 ? o0 < o1 : o0 <= o1;
1549     }
1550 
1551   if (len * HOST_BITS_PER_WIDE_INT < prec)
1552     {
1553       val[len] = mask0 - mask1 - borrow;
1554       len++;
1555       if (overflow)
1556 	*overflow = false;
1557     }
1558   else if (overflow)
1559     {
1560       unsigned int shift = -prec % HOST_BITS_PER_WIDE_INT;
1561       if (sgn == SIGNED)
1562 	{
1563 	  unsigned HOST_WIDE_INT x = (o0 ^ o1) & (val[len - 1] ^ o0);
1564 	  *overflow = (HOST_WIDE_INT) (x << shift) < 0;
1565 	}
1566       else
1567 	{
1568 	  /* Put the MSB of X and O0 and in the top of the HWI.  */
1569 	  x <<= shift;
1570 	  o0 <<= shift;
1571 	  if (old_borrow)
1572 	    *overflow = (x >= o0);
1573 	  else
1574 	    *overflow = (x > o0);
1575 	}
1576     }
1577 
1578   return canonize (val, len, prec);
1579 }
1580 
1581 
1582 /*
1583  * Division and Mod
1584  */
1585 
1586 /* Compute B_QUOTIENT and B_REMAINDER from B_DIVIDEND/B_DIVISOR.  The
1587    algorithm is a small modification of the algorithm in Hacker's
1588    Delight by Warren, which itself is a small modification of Knuth's
1589    algorithm.  M is the number of significant elements of U however
1590    there needs to be at least one extra element of B_DIVIDEND
1591    allocated, N is the number of elements of B_DIVISOR.  */
1592 static void
1593 divmod_internal_2 (unsigned HOST_HALF_WIDE_INT *b_quotient,
1594 		   unsigned HOST_HALF_WIDE_INT *b_remainder,
1595 		   unsigned HOST_HALF_WIDE_INT *b_dividend,
1596 		   unsigned HOST_HALF_WIDE_INT *b_divisor,
1597 		   int m, int n)
1598 {
1599   /* The "digits" are a HOST_HALF_WIDE_INT which the size of half of a
1600      HOST_WIDE_INT and stored in the lower bits of each word.  This
1601      algorithm should work properly on both 32 and 64 bit
1602      machines.  */
1603   unsigned HOST_WIDE_INT b
1604     = (unsigned HOST_WIDE_INT)1 << HOST_BITS_PER_HALF_WIDE_INT;
1605   unsigned HOST_WIDE_INT qhat;   /* Estimate of quotient digit.  */
1606   unsigned HOST_WIDE_INT rhat;   /* A remainder.  */
1607   unsigned HOST_WIDE_INT p;      /* Product of two digits.  */
1608   HOST_WIDE_INT t, k;
1609   int i, j, s;
1610 
1611   /* Single digit divisor.  */
1612   if (n == 1)
1613     {
1614       k = 0;
1615       for (j = m - 1; j >= 0; j--)
1616 	{
1617 	  b_quotient[j] = (k * b + b_dividend[j])/b_divisor[0];
1618 	  k = ((k * b + b_dividend[j])
1619 	       - ((unsigned HOST_WIDE_INT)b_quotient[j]
1620 		  * (unsigned HOST_WIDE_INT)b_divisor[0]));
1621 	}
1622       b_remainder[0] = k;
1623       return;
1624     }
1625 
1626   s = clz_hwi (b_divisor[n-1]) - HOST_BITS_PER_HALF_WIDE_INT; /* CHECK clz */
1627 
1628   if (s)
1629     {
1630       /* Normalize B_DIVIDEND and B_DIVISOR.  Unlike the published
1631 	 algorithm, we can overwrite b_dividend and b_divisor, so we do
1632 	 that.  */
1633       for (i = n - 1; i > 0; i--)
1634 	b_divisor[i] = (b_divisor[i] << s)
1635 	  | (b_divisor[i-1] >> (HOST_BITS_PER_HALF_WIDE_INT - s));
1636       b_divisor[0] = b_divisor[0] << s;
1637 
1638       b_dividend[m] = b_dividend[m-1] >> (HOST_BITS_PER_HALF_WIDE_INT - s);
1639       for (i = m - 1; i > 0; i--)
1640 	b_dividend[i] = (b_dividend[i] << s)
1641 	  | (b_dividend[i-1] >> (HOST_BITS_PER_HALF_WIDE_INT - s));
1642       b_dividend[0] = b_dividend[0] << s;
1643     }
1644 
1645   /* Main loop.  */
1646   for (j = m - n; j >= 0; j--)
1647     {
1648       qhat = (b_dividend[j+n] * b + b_dividend[j+n-1]) / b_divisor[n-1];
1649       rhat = (b_dividend[j+n] * b + b_dividend[j+n-1]) - qhat * b_divisor[n-1];
1650     again:
1651       if (qhat >= b || qhat * b_divisor[n-2] > b * rhat + b_dividend[j+n-2])
1652 	{
1653 	  qhat -= 1;
1654 	  rhat += b_divisor[n-1];
1655 	  if (rhat < b)
1656 	    goto again;
1657 	}
1658 
1659       /* Multiply and subtract.  */
1660       k = 0;
1661       for (i = 0; i < n; i++)
1662 	{
1663 	  p = qhat * b_divisor[i];
1664 	  t = b_dividend[i+j] - k - (p & HALF_INT_MASK);
1665 	  b_dividend[i + j] = t;
1666 	  k = ((p >> HOST_BITS_PER_HALF_WIDE_INT)
1667 	       - (t >> HOST_BITS_PER_HALF_WIDE_INT));
1668 	}
1669       t = b_dividend[j+n] - k;
1670       b_dividend[j+n] = t;
1671 
1672       b_quotient[j] = qhat;
1673       if (t < 0)
1674 	{
1675 	  b_quotient[j] -= 1;
1676 	  k = 0;
1677 	  for (i = 0; i < n; i++)
1678 	    {
1679 	      t = (HOST_WIDE_INT)b_dividend[i+j] + b_divisor[i] + k;
1680 	      b_dividend[i+j] = t;
1681 	      k = t >> HOST_BITS_PER_HALF_WIDE_INT;
1682 	    }
1683 	  b_dividend[j+n] += k;
1684 	}
1685     }
1686   if (s)
1687     for (i = 0; i < n; i++)
1688       b_remainder[i] = (b_dividend[i] >> s)
1689 	| (b_dividend[i+1] << (HOST_BITS_PER_HALF_WIDE_INT - s));
1690   else
1691     for (i = 0; i < n; i++)
1692       b_remainder[i] = b_dividend[i];
1693 }
1694 
1695 
1696 /* Divide DIVIDEND by DIVISOR, which have signedness SGN, and truncate
1697    the result.  If QUOTIENT is nonnull, store the value of the quotient
1698    there and return the number of blocks in it.  The return value is
1699    not defined otherwise.  If REMAINDER is nonnull, store the value
1700    of the remainder there and store the number of blocks in
1701    *REMAINDER_LEN.  If OFLOW is not null, store in *OFLOW whether
1702    the division overflowed.  */
1703 unsigned int
1704 wi::divmod_internal (HOST_WIDE_INT *quotient, unsigned int *remainder_len,
1705 		     HOST_WIDE_INT *remainder,
1706 		     const HOST_WIDE_INT *dividend_val,
1707 		     unsigned int dividend_len, unsigned int dividend_prec,
1708 		     const HOST_WIDE_INT *divisor_val, unsigned int divisor_len,
1709 		     unsigned int divisor_prec, signop sgn,
1710 		     bool *oflow)
1711 {
1712   unsigned int dividend_blocks_needed = 2 * BLOCKS_NEEDED (dividend_prec);
1713   unsigned int divisor_blocks_needed = 2 * BLOCKS_NEEDED (divisor_prec);
1714   unsigned HOST_HALF_WIDE_INT
1715     b_quotient[4 * MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_HALF_WIDE_INT];
1716   unsigned HOST_HALF_WIDE_INT
1717     b_remainder[4 * MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_HALF_WIDE_INT];
1718   unsigned HOST_HALF_WIDE_INT
1719     b_dividend[(4 * MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_HALF_WIDE_INT) + 1];
1720   unsigned HOST_HALF_WIDE_INT
1721     b_divisor[4 * MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_HALF_WIDE_INT];
1722   unsigned int m, n;
1723   bool dividend_neg = false;
1724   bool divisor_neg = false;
1725   bool overflow = false;
1726   wide_int neg_dividend, neg_divisor;
1727 
1728   wide_int_ref dividend = wi::storage_ref (dividend_val, dividend_len,
1729 					   dividend_prec);
1730   wide_int_ref divisor = wi::storage_ref (divisor_val, divisor_len,
1731 					  divisor_prec);
1732   if (divisor == 0)
1733     overflow = true;
1734 
1735   /* The smallest signed number / -1 causes overflow.  The dividend_len
1736      check is for speed rather than correctness.  */
1737   if (sgn == SIGNED
1738       && dividend_len == BLOCKS_NEEDED (dividend_prec)
1739       && divisor == -1
1740       && wi::only_sign_bit_p (dividend))
1741     overflow = true;
1742 
1743   /* Handle the overflow cases.  Viewed as unsigned value, the quotient of
1744      (signed min / -1) has the same representation as the orignal dividend.
1745      We have traditionally made division by zero act as division by one,
1746      so there too we use the original dividend.  */
1747   if (overflow)
1748     {
1749       if (remainder)
1750 	{
1751 	  *remainder_len = 1;
1752 	  remainder[0] = 0;
1753 	}
1754       if (oflow != 0)
1755 	*oflow = true;
1756       if (quotient)
1757 	for (unsigned int i = 0; i < dividend_len; ++i)
1758 	  quotient[i] = dividend_val[i];
1759       return dividend_len;
1760     }
1761 
1762   if (oflow)
1763     *oflow = false;
1764 
1765   /* Do it on the host if you can.  */
1766   if (sgn == SIGNED
1767       && wi::fits_shwi_p (dividend)
1768       && wi::fits_shwi_p (divisor))
1769     {
1770       HOST_WIDE_INT o0 = dividend.to_shwi ();
1771       HOST_WIDE_INT o1 = divisor.to_shwi ();
1772 
1773       if (o0 == HOST_WIDE_INT_MIN && o1 == -1)
1774 	{
1775 	  gcc_checking_assert (dividend_prec > HOST_BITS_PER_WIDE_INT);
1776 	  if (quotient)
1777 	    {
1778 	      quotient[0] = HOST_WIDE_INT_MIN;
1779 	      quotient[1] = 0;
1780 	    }
1781 	  if (remainder)
1782 	    {
1783 	      remainder[0] = 0;
1784 	      *remainder_len = 1;
1785 	    }
1786 	  return 2;
1787 	}
1788       else
1789 	{
1790 	  if (quotient)
1791 	    quotient[0] = o0 / o1;
1792 	  if (remainder)
1793 	    {
1794 	      remainder[0] = o0 % o1;
1795 	      *remainder_len = 1;
1796 	    }
1797 	  return 1;
1798 	}
1799     }
1800 
1801   if (sgn == UNSIGNED
1802       && wi::fits_uhwi_p (dividend)
1803       && wi::fits_uhwi_p (divisor))
1804     {
1805       unsigned HOST_WIDE_INT o0 = dividend.to_uhwi ();
1806       unsigned HOST_WIDE_INT o1 = divisor.to_uhwi ();
1807       unsigned int quotient_len = 1;
1808 
1809       if (quotient)
1810 	{
1811 	  quotient[0] = o0 / o1;
1812 	  quotient_len = canonize_uhwi (quotient, dividend_prec);
1813 	}
1814       if (remainder)
1815 	{
1816 	  remainder[0] = o0 % o1;
1817 	  *remainder_len = canonize_uhwi (remainder, dividend_prec);
1818 	}
1819       return quotient_len;
1820     }
1821 
1822   /* Make the divisor and dividend positive and remember what we
1823      did.  */
1824   if (sgn == SIGNED)
1825     {
1826       if (wi::neg_p (dividend))
1827 	{
1828 	  neg_dividend = -dividend;
1829 	  dividend = neg_dividend;
1830 	  dividend_neg = true;
1831 	}
1832       if (wi::neg_p (divisor))
1833 	{
1834 	  neg_divisor = -divisor;
1835 	  divisor = neg_divisor;
1836 	  divisor_neg = true;
1837 	}
1838     }
1839 
1840   wi_unpack (b_dividend, dividend.get_val (), dividend.get_len (),
1841 	     dividend_blocks_needed, dividend_prec, sgn);
1842   wi_unpack (b_divisor, divisor.get_val (), divisor.get_len (),
1843 	     divisor_blocks_needed, divisor_prec, sgn);
1844 
1845   m = dividend_blocks_needed;
1846   b_dividend[m] = 0;
1847   while (m > 1 && b_dividend[m - 1] == 0)
1848     m--;
1849 
1850   n = divisor_blocks_needed;
1851   while (n > 1 && b_divisor[n - 1] == 0)
1852     n--;
1853 
1854   memset (b_quotient, 0, sizeof (b_quotient));
1855 
1856   divmod_internal_2 (b_quotient, b_remainder, b_dividend, b_divisor, m, n);
1857 
1858   unsigned int quotient_len = 0;
1859   if (quotient)
1860     {
1861       quotient_len = wi_pack (quotient, b_quotient, m, dividend_prec);
1862       /* The quotient is neg if exactly one of the divisor or dividend is
1863 	 neg.  */
1864       if (dividend_neg != divisor_neg)
1865 	quotient_len = wi::sub_large (quotient, zeros, 1, quotient,
1866 				      quotient_len, dividend_prec,
1867 				      UNSIGNED, 0);
1868     }
1869 
1870   if (remainder)
1871     {
1872       *remainder_len = wi_pack (remainder, b_remainder, n, dividend_prec);
1873       /* The remainder is always the same sign as the dividend.  */
1874       if (dividend_neg)
1875 	*remainder_len = wi::sub_large (remainder, zeros, 1, remainder,
1876 					*remainder_len, dividend_prec,
1877 					UNSIGNED, 0);
1878     }
1879 
1880   return quotient_len;
1881 }
1882 
1883 /*
1884  * Shifting, rotating and extraction.
1885  */
1886 
1887 /* Left shift XVAL by SHIFT and store the result in VAL.  Return the
1888    number of blocks in VAL.  Both XVAL and VAL have PRECISION bits.  */
1889 unsigned int
1890 wi::lshift_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
1891 		  unsigned int xlen, unsigned int precision,
1892 		  unsigned int shift)
1893 {
1894   /* Split the shift into a whole-block shift and a subblock shift.  */
1895   unsigned int skip = shift / HOST_BITS_PER_WIDE_INT;
1896   unsigned int small_shift = shift % HOST_BITS_PER_WIDE_INT;
1897 
1898   /* The whole-block shift fills with zeros.  */
1899   unsigned int len = BLOCKS_NEEDED (precision);
1900   for (unsigned int i = 0; i < skip; ++i)
1901     val[i] = 0;
1902 
1903   /* It's easier to handle the simple block case specially.  */
1904   if (small_shift == 0)
1905     for (unsigned int i = skip; i < len; ++i)
1906       val[i] = safe_uhwi (xval, xlen, i - skip);
1907   else
1908     {
1909       /* The first unfilled output block is a left shift of the first
1910 	 block in XVAL.  The other output blocks contain bits from two
1911 	 consecutive input blocks.  */
1912       unsigned HOST_WIDE_INT carry = 0;
1913       for (unsigned int i = skip; i < len; ++i)
1914 	{
1915 	  unsigned HOST_WIDE_INT x = safe_uhwi (xval, xlen, i - skip);
1916 	  val[i] = (x << small_shift) | carry;
1917 	  carry = x >> (-small_shift % HOST_BITS_PER_WIDE_INT);
1918 	}
1919     }
1920   return canonize (val, len, precision);
1921 }
1922 
1923 /* Right shift XVAL by SHIFT and store the result in VAL.  Return the
1924    number of blocks in VAL.  The input has XPRECISION bits and the
1925    output has XPRECISION - SHIFT bits.  */
1926 static unsigned int
1927 rshift_large_common (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
1928 		     unsigned int xlen, unsigned int xprecision,
1929 		     unsigned int shift)
1930 {
1931   /* Split the shift into a whole-block shift and a subblock shift.  */
1932   unsigned int skip = shift / HOST_BITS_PER_WIDE_INT;
1933   unsigned int small_shift = shift % HOST_BITS_PER_WIDE_INT;
1934 
1935   /* Work out how many blocks are needed to store the significant bits
1936      (excluding the upper zeros or signs).  */
1937   unsigned int len = BLOCKS_NEEDED (xprecision - shift);
1938 
1939   /* It's easier to handle the simple block case specially.  */
1940   if (small_shift == 0)
1941     for (unsigned int i = 0; i < len; ++i)
1942       val[i] = safe_uhwi (xval, xlen, i + skip);
1943   else
1944     {
1945       /* Each output block but the last is a combination of two input blocks.
1946 	 The last block is a right shift of the last block in XVAL.  */
1947       unsigned HOST_WIDE_INT curr = safe_uhwi (xval, xlen, skip);
1948       for (unsigned int i = 0; i < len; ++i)
1949 	{
1950 	  val[i] = curr >> small_shift;
1951 	  curr = safe_uhwi (xval, xlen, i + skip + 1);
1952 	  val[i] |= curr << (-small_shift % HOST_BITS_PER_WIDE_INT);
1953 	}
1954     }
1955   return len;
1956 }
1957 
1958 /* Logically right shift XVAL by SHIFT and store the result in VAL.
1959    Return the number of blocks in VAL.  XVAL has XPRECISION bits and
1960    VAL has PRECISION bits.  */
1961 unsigned int
1962 wi::lrshift_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
1963 		   unsigned int xlen, unsigned int xprecision,
1964 		   unsigned int precision, unsigned int shift)
1965 {
1966   unsigned int len = rshift_large_common (val, xval, xlen, xprecision, shift);
1967 
1968   /* The value we just created has precision XPRECISION - SHIFT.
1969      Zero-extend it to wider precisions.  */
1970   if (precision > xprecision - shift)
1971     {
1972       unsigned int small_prec = (xprecision - shift) % HOST_BITS_PER_WIDE_INT;
1973       if (small_prec)
1974 	val[len - 1] = zext_hwi (val[len - 1], small_prec);
1975       else if (val[len - 1] < 0)
1976 	{
1977 	  /* Add a new block with a zero. */
1978 	  val[len++] = 0;
1979 	  return len;
1980 	}
1981     }
1982   return canonize (val, len, precision);
1983 }
1984 
1985 /* Arithmetically right shift XVAL by SHIFT and store the result in VAL.
1986    Return the number of blocks in VAL.  XVAL has XPRECISION bits and
1987    VAL has PRECISION bits.  */
1988 unsigned int
1989 wi::arshift_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
1990 		   unsigned int xlen, unsigned int xprecision,
1991 		   unsigned int precision, unsigned int shift)
1992 {
1993   unsigned int len = rshift_large_common (val, xval, xlen, xprecision, shift);
1994 
1995   /* The value we just created has precision XPRECISION - SHIFT.
1996      Sign-extend it to wider types.  */
1997   if (precision > xprecision - shift)
1998     {
1999       unsigned int small_prec = (xprecision - shift) % HOST_BITS_PER_WIDE_INT;
2000       if (small_prec)
2001 	val[len - 1] = sext_hwi (val[len - 1], small_prec);
2002     }
2003   return canonize (val, len, precision);
2004 }
2005 
2006 /* Return the number of leading (upper) zeros in X.  */
2007 int
2008 wi::clz (const wide_int_ref &x)
2009 {
2010   /* Calculate how many bits there above the highest represented block.  */
2011   int count = x.precision - x.len * HOST_BITS_PER_WIDE_INT;
2012 
2013   unsigned HOST_WIDE_INT high = x.uhigh ();
2014   if (count < 0)
2015     /* The upper -COUNT bits of HIGH are not part of the value.
2016        Clear them out.  */
2017     high = (high << -count) >> -count;
2018   else if (x.sign_mask () < 0)
2019     /* The upper bit is set, so there are no leading zeros.  */
2020     return 0;
2021 
2022   /* We don't need to look below HIGH.  Either HIGH is nonzero,
2023      or the top bit of the block below is nonzero; clz_hwi is
2024      HOST_BITS_PER_WIDE_INT in the latter case.  */
2025   return count + clz_hwi (high);
2026 }
2027 
2028 /* Return the number of redundant sign bits in X.  (That is, the number
2029    of bits immediately below the sign bit that have the same value as
2030    the sign bit.)  */
2031 int
2032 wi::clrsb (const wide_int_ref &x)
2033 {
2034   /* Calculate how many bits there above the highest represented block.  */
2035   int count = x.precision - x.len * HOST_BITS_PER_WIDE_INT;
2036 
2037   unsigned HOST_WIDE_INT high = x.uhigh ();
2038   unsigned HOST_WIDE_INT mask = -1;
2039   if (count < 0)
2040     {
2041       /* The upper -COUNT bits of HIGH are not part of the value.
2042 	 Clear them from both MASK and HIGH.  */
2043       mask >>= -count;
2044       high &= mask;
2045     }
2046 
2047   /* If the top bit is 1, count the number of leading 1s.  If the top
2048      bit is zero, count the number of leading zeros.  */
2049   if (high > mask / 2)
2050     high ^= mask;
2051 
2052   /* There are no sign bits below the top block, so we don't need to look
2053      beyond HIGH.  Note that clz_hwi is HOST_BITS_PER_WIDE_INT when
2054      HIGH is 0.  */
2055   return count + clz_hwi (high) - 1;
2056 }
2057 
2058 /* Return the number of trailing (lower) zeros in X.  */
2059 int
2060 wi::ctz (const wide_int_ref &x)
2061 {
2062   if (x.len == 1 && x.ulow () == 0)
2063     return x.precision;
2064 
2065   /* Having dealt with the zero case, there must be a block with a
2066      nonzero bit.  We don't care about the bits above the first 1.  */
2067   unsigned int i = 0;
2068   while (x.val[i] == 0)
2069     ++i;
2070   return i * HOST_BITS_PER_WIDE_INT + ctz_hwi (x.val[i]);
2071 }
2072 
2073 /* If X is an exact power of 2, return the base-2 logarithm, otherwise
2074    return -1.  */
2075 int
2076 wi::exact_log2 (const wide_int_ref &x)
2077 {
2078   /* Reject cases where there are implicit -1 blocks above HIGH.  */
2079   if (x.len * HOST_BITS_PER_WIDE_INT < x.precision && x.sign_mask () < 0)
2080     return -1;
2081 
2082   /* Set CRUX to the index of the entry that should be nonzero.
2083      If the top block is zero then the next lowest block (if any)
2084      must have the high bit set.  */
2085   unsigned int crux = x.len - 1;
2086   if (crux > 0 && x.val[crux] == 0)
2087     crux -= 1;
2088 
2089   /* Check that all lower blocks are zero.  */
2090   for (unsigned int i = 0; i < crux; ++i)
2091     if (x.val[i] != 0)
2092       return -1;
2093 
2094   /* Get a zero-extended form of block CRUX.  */
2095   unsigned HOST_WIDE_INT hwi = x.val[crux];
2096   if ((crux + 1) * HOST_BITS_PER_WIDE_INT > x.precision)
2097     hwi = zext_hwi (hwi, x.precision % HOST_BITS_PER_WIDE_INT);
2098 
2099   /* Now it's down to whether HWI is a power of 2.  */
2100   int res = ::exact_log2 (hwi);
2101   if (res >= 0)
2102     res += crux * HOST_BITS_PER_WIDE_INT;
2103   return res;
2104 }
2105 
2106 /* Return the base-2 logarithm of X, rounding down.  Return -1 if X is 0.  */
2107 int
2108 wi::floor_log2 (const wide_int_ref &x)
2109 {
2110   return x.precision - 1 - clz (x);
2111 }
2112 
2113 /* Return the index of the first (lowest) set bit in X, counting from 1.
2114    Return 0 if X is 0.  */
2115 int
2116 wi::ffs (const wide_int_ref &x)
2117 {
2118   return eq_p (x, 0) ? 0 : ctz (x) + 1;
2119 }
2120 
2121 /* Return true if sign-extending X to have precision PRECISION would give
2122    the minimum signed value at that precision.  */
2123 bool
2124 wi::only_sign_bit_p (const wide_int_ref &x, unsigned int precision)
2125 {
2126   return ctz (x) + 1 == int (precision);
2127 }
2128 
2129 /* Return true if X represents the minimum signed value.  */
2130 bool
2131 wi::only_sign_bit_p (const wide_int_ref &x)
2132 {
2133   return only_sign_bit_p (x, x.precision);
2134 }
2135 
2136 /*
2137  * Private utilities.
2138  */
2139 
2140 void gt_ggc_mx (widest_int *) { }
2141 void gt_pch_nx (widest_int *, void (*) (void *, void *), void *) { }
2142 void gt_pch_nx (widest_int *) { }
2143 
2144 template void wide_int::dump () const;
2145 template void generic_wide_int <wide_int_ref_storage <false> >::dump () const;
2146 template void generic_wide_int <wide_int_ref_storage <true> >::dump () const;
2147 template void offset_int::dump () const;
2148 template void widest_int::dump () const;
2149 
2150 
2151 #if CHECKING_P
2152 
2153 namespace selftest {
2154 
2155 /* Selftests for wide ints.  We run these multiple times, once per type.  */
2156 
2157 /* Helper function for building a test value.  */
2158 
2159 template <class VALUE_TYPE>
2160 static VALUE_TYPE
2161 from_int (int i);
2162 
2163 /* Specializations of the fixture for each wide-int type.  */
2164 
2165 /* Specialization for VALUE_TYPE == wide_int.  */
2166 
2167 template <>
2168 wide_int
2169 from_int (int i)
2170 {
2171   return wi::shwi (i, 32);
2172 }
2173 
2174 /* Specialization for VALUE_TYPE == offset_int.  */
2175 
2176 template <>
2177 offset_int
2178 from_int (int i)
2179 {
2180   return offset_int (i);
2181 }
2182 
2183 /* Specialization for VALUE_TYPE == widest_int.  */
2184 
2185 template <>
2186 widest_int
2187 from_int (int i)
2188 {
2189   return widest_int (i);
2190 }
2191 
2192 /* Verify that print_dec (WI, ..., SGN) gives the expected string
2193    representation (using base 10).  */
2194 
2195 static void
2196 assert_deceq (const char *expected, const wide_int_ref &wi, signop sgn)
2197 {
2198   char buf[WIDE_INT_PRINT_BUFFER_SIZE];
2199   print_dec (wi, buf, sgn);
2200   ASSERT_STREQ (expected, buf);
2201 }
2202 
2203 /* Likewise for base 16.  */
2204 
2205 static void
2206 assert_hexeq (const char *expected, const wide_int_ref &wi)
2207 {
2208   char buf[WIDE_INT_PRINT_BUFFER_SIZE];
2209   print_hex (wi, buf);
2210   ASSERT_STREQ (expected, buf);
2211 }
2212 
2213 /* Test cases.  */
2214 
2215 /* Verify that print_dec and print_hex work for VALUE_TYPE.  */
2216 
2217 template <class VALUE_TYPE>
2218 static void
2219 test_printing ()
2220 {
2221   VALUE_TYPE a = from_int<VALUE_TYPE> (42);
2222   assert_deceq ("42", a, SIGNED);
2223   assert_hexeq ("0x2a", a);
2224 }
2225 
2226 /* Verify that various operations work correctly for VALUE_TYPE,
2227    unary and binary, using both function syntax, and
2228    overloaded-operators.  */
2229 
2230 template <class VALUE_TYPE>
2231 static void
2232 test_ops ()
2233 {
2234   VALUE_TYPE a = from_int<VALUE_TYPE> (7);
2235   VALUE_TYPE b = from_int<VALUE_TYPE> (3);
2236 
2237   /* Using functions.  */
2238   assert_deceq ("-7", wi::neg (a), SIGNED);
2239   assert_deceq ("10", wi::add (a, b), SIGNED);
2240   assert_deceq ("4", wi::sub (a, b), SIGNED);
2241   assert_deceq ("-4", wi::sub (b, a), SIGNED);
2242   assert_deceq ("21", wi::mul (a, b), SIGNED);
2243 
2244   /* Using operators.  */
2245   assert_deceq ("-7", -a, SIGNED);
2246   assert_deceq ("10", a + b, SIGNED);
2247   assert_deceq ("4", a - b, SIGNED);
2248   assert_deceq ("-4", b - a, SIGNED);
2249   assert_deceq ("21", a * b, SIGNED);
2250 }
2251 
2252 /* Verify that various comparisons work correctly for VALUE_TYPE.  */
2253 
2254 template <class VALUE_TYPE>
2255 static void
2256 test_comparisons ()
2257 {
2258   VALUE_TYPE a = from_int<VALUE_TYPE> (7);
2259   VALUE_TYPE b = from_int<VALUE_TYPE> (3);
2260 
2261   /* == */
2262   ASSERT_TRUE (wi::eq_p (a, a));
2263   ASSERT_FALSE (wi::eq_p (a, b));
2264 
2265   /* != */
2266   ASSERT_TRUE (wi::ne_p (a, b));
2267   ASSERT_FALSE (wi::ne_p (a, a));
2268 
2269   /* < */
2270   ASSERT_FALSE (wi::lts_p (a, a));
2271   ASSERT_FALSE (wi::lts_p (a, b));
2272   ASSERT_TRUE (wi::lts_p (b, a));
2273 
2274   /* <= */
2275   ASSERT_TRUE (wi::les_p (a, a));
2276   ASSERT_FALSE (wi::les_p (a, b));
2277   ASSERT_TRUE (wi::les_p (b, a));
2278 
2279   /* > */
2280   ASSERT_FALSE (wi::gts_p (a, a));
2281   ASSERT_TRUE (wi::gts_p (a, b));
2282   ASSERT_FALSE (wi::gts_p (b, a));
2283 
2284   /* >= */
2285   ASSERT_TRUE (wi::ges_p (a, a));
2286   ASSERT_TRUE (wi::ges_p (a, b));
2287   ASSERT_FALSE (wi::ges_p (b, a));
2288 
2289   /* comparison */
2290   ASSERT_EQ (-1, wi::cmps (b, a));
2291   ASSERT_EQ (0, wi::cmps (a, a));
2292   ASSERT_EQ (1, wi::cmps (a, b));
2293 }
2294 
2295 /* Run all of the selftests, using the given VALUE_TYPE.  */
2296 
2297 template <class VALUE_TYPE>
2298 static void run_all_wide_int_tests ()
2299 {
2300   test_printing <VALUE_TYPE> ();
2301   test_ops <VALUE_TYPE> ();
2302   test_comparisons <VALUE_TYPE> ();
2303 }
2304 
2305 /* Run all of the selftests within this file, for all value types.  */
2306 
2307 void
2308 wide_int_cc_tests ()
2309 {
2310  run_all_wide_int_tests <wide_int> ();
2311  run_all_wide_int_tests <offset_int> ();
2312  run_all_wide_int_tests <widest_int> ();
2313 }
2314 
2315 } // namespace selftest
2316 #endif /* CHECKING_P */
2317