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