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