Bug Summary

File:build/gcc/wide-int.cc
Warning:line 1866, column 22
Division by zero

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-suse-linux -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name wide-int.cc -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model static -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/buildworker/marxinbox-gcc-clang-static-analyzer/objdir/gcc -resource-dir /usr/lib64/clang/15.0.7 -D IN_GCC -D HAVE_CONFIG_H -I . -I . -I /buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc -I /buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/. -I /buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/../include -I /buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/../libcpp/include -I /buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/../libcody -I /buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/../libdecnumber -I /buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/../libdecnumber/bid -I ../libdecnumber -I /buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/../libbacktrace -internal-isystem /usr/bin/../lib64/gcc/x86_64-suse-linux/13/../../../../include/c++/13 -internal-isystem /usr/bin/../lib64/gcc/x86_64-suse-linux/13/../../../../include/c++/13/x86_64-suse-linux -internal-isystem /usr/bin/../lib64/gcc/x86_64-suse-linux/13/../../../../include/c++/13/backward -internal-isystem /usr/lib64/clang/15.0.7/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib64/gcc/x86_64-suse-linux/13/../../../../x86_64-suse-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-narrowing -Wwrite-strings -Wno-long-long -Wno-variadic-macros -Wno-overlength-strings -fdeprecated-macro -fdebug-compilation-dir=/buildworker/marxinbox-gcc-clang-static-analyzer/objdir/gcc -ferror-limit 19 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=plist-html -analyzer-config silence-checkers=core.NullDereference -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /buildworker/marxinbox-gcc-clang-static-analyzer/objdir/clang-static-analyzer/2023-03-27-141847-20772-1/report-UcyiV1.plist -x c++ /buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/wide-int.cc

/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/wide-int.cc

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

/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/wide-int.h

1/* Operations with very long integers. -*- C++ -*-
2 Copyright (C) 2012-2023 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 3, or (at your option) any
9later version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT
12ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#ifndef WIDE_INT_H
21#define WIDE_INT_H
22
23/* wide-int.[cc|h] implements a class that efficiently performs
24 mathematical operations on finite precision integers. wide_ints
25 are designed to be transient - they are not for long term storage
26 of values. There is tight integration between wide_ints and the
27 other longer storage GCC representations (rtl and tree).
28
29 The actual precision of a wide_int depends on the flavor. There
30 are three predefined flavors:
31
32 1) wide_int (the default). This flavor does the math in the
33 precision of its input arguments. It is assumed (and checked)
34 that the precisions of the operands and results are consistent.
35 This is the most efficient flavor. It is not possible to examine
36 bits above the precision that has been specified. Because of
37 this, the default flavor has semantics that are simple to
38 understand and in general model the underlying hardware that the
39 compiler is targetted for.
40
41 This flavor must be used at the RTL level of gcc because there
42 is, in general, not enough information in the RTL representation
43 to extend a value beyond the precision specified in the mode.
44
45 This flavor should also be used at the TREE and GIMPLE levels of
46 the compiler except for the circumstances described in the
47 descriptions of the other two flavors.
48
49 The default wide_int representation does not contain any
50 information inherent about signedness of the represented value,
51 so it can be used to represent both signed and unsigned numbers.
52 For operations where the results depend on signedness (full width
53 multiply, division, shifts, comparisons, and operations that need
54 overflow detected), the signedness must be specified separately.
55
56 2) offset_int. This is a fixed-precision integer that can hold
57 any address offset, measured in either bits or bytes, with at
58 least one extra sign bit. At the moment the maximum address
59 size GCC supports is 64 bits. With 8-bit bytes and an extra
60 sign bit, offset_int therefore needs to have at least 68 bits
61 of precision. We round this up to 128 bits for efficiency.
62 Values of type T are converted to this precision by sign- or
63 zero-extending them based on the signedness of T.
64
65 The extra sign bit means that offset_int is effectively a signed
66 128-bit integer, i.e. it behaves like int128_t.
67
68 Since the values are logically signed, there is no need to
69 distinguish between signed and unsigned operations. Sign-sensitive
70 comparison operators <, <=, > and >= are therefore supported.
71 Shift operators << and >> are also supported, with >> being
72 an _arithmetic_ right shift.
73
74 [ Note that, even though offset_int is effectively int128_t,
75 it can still be useful to use unsigned comparisons like
76 wi::leu_p (a, b) as a more efficient short-hand for
77 "a >= 0 && a <= b". ]
78
79 3) widest_int. This representation is an approximation of
80 infinite precision math. However, it is not really infinite
81 precision math as in the GMP library. It is really finite
82 precision math where the precision is 4 times the size of the
83 largest integer that the target port can represent.
84
85 Like offset_int, widest_int is wider than all the values that
86 it needs to represent, so the integers are logically signed.
87 Sign-sensitive comparison operators <, <=, > and >= are supported,
88 as are << and >>.
89
90 There are several places in the GCC where this should/must be used:
91
92 * Code that does induction variable optimizations. This code
93 works with induction variables of many different types at the
94 same time. Because of this, it ends up doing many different
95 calculations where the operands are not compatible types. The
96 widest_int makes this easy, because it provides a field where
97 nothing is lost when converting from any variable,
98
99 * There are a small number of passes that currently use the
100 widest_int that should use the default. These should be
101 changed.
102
103 There are surprising features of offset_int and widest_int
104 that the users should be careful about:
105
106 1) Shifts and rotations are just weird. You have to specify a
107 precision in which the shift or rotate is to happen in. The bits
108 above this precision are zeroed. While this is what you
109 want, it is clearly non obvious.
110
111 2) Larger precision math sometimes does not produce the same
112 answer as would be expected for doing the math at the proper
113 precision. In particular, a multiply followed by a divide will
114 produce a different answer if the first product is larger than
115 what can be represented in the input precision.
116
117 The offset_int and the widest_int flavors are more expensive
118 than the default wide int, so in addition to the caveats with these
119 two, the default is the prefered representation.
120
121 All three flavors of wide_int are represented as a vector of
122 HOST_WIDE_INTs. The default and widest_int vectors contain enough elements
123 to hold a value of MAX_BITSIZE_MODE_ANY_INT bits. offset_int contains only
124 enough elements to hold ADDR_MAX_PRECISION bits. The values are stored
125 in the vector with the least significant HOST_BITS_PER_WIDE_INT bits
126 in element 0.
127
128 The default wide_int contains three fields: the vector (VAL),
129 the precision and a length (LEN). The length is the number of HWIs
130 needed to represent the value. widest_int and offset_int have a
131 constant precision that cannot be changed, so they only store the
132 VAL and LEN fields.
133
134 Since most integers used in a compiler are small values, it is
135 generally profitable to use a representation of the value that is
136 as small as possible. LEN is used to indicate the number of
137 elements of the vector that are in use. The numbers are stored as
138 sign extended numbers as a means of compression. Leading
139 HOST_WIDE_INTs that contain strings of either -1 or 0 are removed
140 as long as they can be reconstructed from the top bit that is being
141 represented.
142
143 The precision and length of a wide_int are always greater than 0.
144 Any bits in a wide_int above the precision are sign-extended from the
145 most significant bit. For example, a 4-bit value 0x8 is represented as
146 VAL = { 0xf...fff8 }. However, as an optimization, we allow other integer
147 constants to be represented with undefined bits above the precision.
148 This allows INTEGER_CSTs to be pre-extended according to TYPE_SIGN,
149 so that the INTEGER_CST representation can be used both in TYPE_PRECISION
150 and in wider precisions.
151
152 There are constructors to create the various forms of wide_int from
153 trees, rtl and constants. For trees the options are:
154
155 tree t = ...;
156 wi::to_wide (t) // Treat T as a wide_int
157 wi::to_offset (t) // Treat T as an offset_int
158 wi::to_widest (t) // Treat T as a widest_int
159
160 All three are light-weight accessors that should have no overhead
161 in release builds. If it is useful for readability reasons to
162 store the result in a temporary variable, the preferred method is:
163
164 wi::tree_to_wide_ref twide = wi::to_wide (t);
165 wi::tree_to_offset_ref toffset = wi::to_offset (t);
166 wi::tree_to_widest_ref twidest = wi::to_widest (t);
167
168 To make an rtx into a wide_int, you have to pair it with a mode.
169 The canonical way to do this is with rtx_mode_t as in:
170
171 rtx r = ...
172 wide_int x = rtx_mode_t (r, mode);
173
174 Similarly, a wide_int can only be constructed from a host value if
175 the target precision is given explicitly, such as in:
176
177 wide_int x = wi::shwi (c, prec); // sign-extend C if necessary
178 wide_int y = wi::uhwi (c, prec); // zero-extend C if necessary
179
180 However, offset_int and widest_int have an inherent precision and so
181 can be initialized directly from a host value:
182
183 offset_int x = (int) c; // sign-extend C
184 widest_int x = (unsigned int) c; // zero-extend C
185
186 It is also possible to do arithmetic directly on rtx_mode_ts and
187 constants. For example:
188
189 wi::add (r1, r2); // add equal-sized rtx_mode_ts r1 and r2
190 wi::add (r1, 1); // add 1 to rtx_mode_t r1
191 wi::lshift (1, 100); // 1 << 100 as a widest_int
192
193 Many binary operations place restrictions on the combinations of inputs,
194 using the following rules:
195
196 - {rtx, wide_int} op {rtx, wide_int} -> wide_int
197 The inputs must be the same precision. The result is a wide_int
198 of the same precision
199
200 - {rtx, wide_int} op (un)signed HOST_WIDE_INT -> wide_int
201 (un)signed HOST_WIDE_INT op {rtx, wide_int} -> wide_int
202 The HOST_WIDE_INT is extended or truncated to the precision of
203 the other input. The result is a wide_int of the same precision
204 as that input.
205
206 - (un)signed HOST_WIDE_INT op (un)signed HOST_WIDE_INT -> widest_int
207 The inputs are extended to widest_int precision and produce a
208 widest_int result.
209
210 - offset_int op offset_int -> offset_int
211 offset_int op (un)signed HOST_WIDE_INT -> offset_int
212 (un)signed HOST_WIDE_INT op offset_int -> offset_int
213
214 - widest_int op widest_int -> widest_int
215 widest_int op (un)signed HOST_WIDE_INT -> widest_int
216 (un)signed HOST_WIDE_INT op widest_int -> widest_int
217
218 Other combinations like:
219
220 - widest_int op offset_int and
221 - wide_int op offset_int
222
223 are not allowed. The inputs should instead be extended or truncated
224 so that they match.
225
226 The inputs to comparison functions like wi::eq_p and wi::lts_p
227 follow the same compatibility rules, although their return types
228 are different. Unary functions on X produce the same result as
229 a binary operation X + X. Shift functions X op Y also produce
230 the same result as X + X; the precision of the shift amount Y
231 can be arbitrarily different from X. */
232
233/* The MAX_BITSIZE_MODE_ANY_INT is automatically generated by a very
234 early examination of the target's mode file. The WIDE_INT_MAX_ELTS
235 can accomodate at least 1 more bit so that unsigned numbers of that
236 mode can be represented as a signed value. Note that it is still
237 possible to create fixed_wide_ints that have precisions greater than
238 MAX_BITSIZE_MODE_ANY_INT. This can be useful when representing a
239 double-width multiplication result, for example. */
240#define WIDE_INT_MAX_ELTS(((64*(8)) + 64) / 64) \
241 ((MAX_BITSIZE_MODE_ANY_INT(64*(8)) + HOST_BITS_PER_WIDE_INT64) / HOST_BITS_PER_WIDE_INT64)
242
243#define WIDE_INT_MAX_PRECISION((((64*(8)) + 64) / 64) * 64) (WIDE_INT_MAX_ELTS(((64*(8)) + 64) / 64) * HOST_BITS_PER_WIDE_INT64)
244
245/* This is the max size of any pointer on any machine. It does not
246 seem to be as easy to sniff this out of the machine description as
247 it is for MAX_BITSIZE_MODE_ANY_INT since targets may support
248 multiple address sizes and may have different address sizes for
249 different address spaces. However, currently the largest pointer
250 on any platform is 64 bits. When that changes, then it is likely
251 that a target hook should be defined so that targets can make this
252 value larger for those targets. */
253#define ADDR_MAX_BITSIZE64 64
254
255/* This is the internal precision used when doing any address
256 arithmetic. The '4' is really 3 + 1. Three of the bits are for
257 the number of extra bits needed to do bit addresses and the other bit
258 is to allow everything to be signed without loosing any precision.
259 Then everything is rounded up to the next HWI for efficiency. */
260#define ADDR_MAX_PRECISION((64 + 4 + 64 - 1) & ~(64 - 1)) \
261 ((ADDR_MAX_BITSIZE64 + 4 + HOST_BITS_PER_WIDE_INT64 - 1) \
262 & ~(HOST_BITS_PER_WIDE_INT64 - 1))
263
264/* The number of HWIs needed to store an offset_int. */
265#define OFFSET_INT_ELTS(((64 + 4 + 64 - 1) & ~(64 - 1)) / 64) (ADDR_MAX_PRECISION((64 + 4 + 64 - 1) & ~(64 - 1)) / HOST_BITS_PER_WIDE_INT64)
266
267/* The type of result produced by a binary operation on types T1 and T2.
268 Defined purely for brevity. */
269#define WI_BINARY_RESULT(T1, T2)typename wi::binary_traits <T1, T2>::result_type \
270 typename wi::binary_traits <T1, T2>::result_type
271
272/* Likewise for binary operators, which excludes the case in which neither
273 T1 nor T2 is a wide-int-based type. */
274#define WI_BINARY_OPERATOR_RESULT(T1, T2)typename wi::binary_traits <T1, T2>::operator_result \
275 typename wi::binary_traits <T1, T2>::operator_result
276
277/* The type of result produced by T1 << T2. Leads to substitution failure
278 if the operation isn't supported. Defined purely for brevity. */
279#define WI_SIGNED_SHIFT_RESULT(T1, T2)typename wi::binary_traits <T1, T2>::signed_shift_result_type \
280 typename wi::binary_traits <T1, T2>::signed_shift_result_type
281
282/* The type of result produced by a sign-agnostic binary predicate on
283 types T1 and T2. This is bool if wide-int operations make sense for
284 T1 and T2 and leads to substitution failure otherwise. */
285#define WI_BINARY_PREDICATE_RESULT(T1, T2)typename wi::binary_traits <T1, T2>::predicate_result \
286 typename wi::binary_traits <T1, T2>::predicate_result
287
288/* The type of result produced by a signed binary predicate on types T1 and T2.
289 This is bool if signed comparisons make sense for T1 and T2 and leads to
290 substitution failure otherwise. */
291#define WI_SIGNED_BINARY_PREDICATE_RESULT(T1, T2)typename wi::binary_traits <T1, T2>::signed_predicate_result \
292 typename wi::binary_traits <T1, T2>::signed_predicate_result
293
294/* The type of result produced by a unary operation on type T. */
295#define WI_UNARY_RESULT(T)typename wi::binary_traits <T, T>::result_type \
296 typename wi::binary_traits <T, T>::result_type
297
298/* Define a variable RESULT to hold the result of a binary operation on
299 X and Y, which have types T1 and T2 respectively. Define VAL to
300 point to the blocks of RESULT. Once the user of the macro has
301 filled in VAL, it should call RESULT.set_len to set the number
302 of initialized blocks. */
303#define WI_BINARY_RESULT_VAR(RESULT, VAL, T1, X, T2, Y)typename wi::binary_traits <T1, T2>::result_type RESULT
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (X, Y); long *VAL = RESULT
.write_val ()
\
304 WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type RESULT = \
305 wi::int_traits <WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type>::get_binary_result (X, Y); \
306 HOST_WIDE_INTlong *VAL = RESULT.write_val ()
307
308/* Similar for the result of a unary operation on X, which has type T. */
309#define WI_UNARY_RESULT_VAR(RESULT, VAL, T, X)typename wi::binary_traits <T, T>::result_type RESULT =
wi::int_traits <typename wi::binary_traits <T, T>::
result_type>::get_binary_result (X, X); long *VAL = RESULT
.write_val ()
\
310 WI_UNARY_RESULT (T)typename wi::binary_traits <T, T>::result_type RESULT = \
311 wi::int_traits <WI_UNARY_RESULT (T)typename wi::binary_traits <T, T>::result_type>::get_binary_result (X, X); \
312 HOST_WIDE_INTlong *VAL = RESULT.write_val ()
313
314template <typename T> class generic_wide_int;
315template <int N> class fixed_wide_int_storage;
316class wide_int_storage;
317
318/* An N-bit integer. Until we can use typedef templates, use this instead. */
319#define FIXED_WIDE_INT(N)generic_wide_int < fixed_wide_int_storage <N> > \
320 generic_wide_int < fixed_wide_int_storage <N> >
321
322typedef generic_wide_int <wide_int_storage> wide_int;
323typedef FIXED_WIDE_INT (ADDR_MAX_PRECISION)generic_wide_int < fixed_wide_int_storage <((64 + 4 + 64
- 1) & ~(64 - 1))> >
offset_int;
324typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION)generic_wide_int < fixed_wide_int_storage <((((64*(8)) +
64) / 64) * 64)> >
widest_int;
325/* Spelled out explicitly (rather than through FIXED_WIDE_INT)
326 so as not to confuse gengtype. */
327typedef generic_wide_int < fixed_wide_int_storage <WIDE_INT_MAX_PRECISION((((64*(8)) + 64) / 64) * 64) * 2> > widest2_int;
328
329/* wi::storage_ref can be a reference to a primitive type,
330 so this is the conservatively-correct setting. */
331template <bool SE, bool HDP = true>
332class wide_int_ref_storage;
333
334typedef generic_wide_int <wide_int_ref_storage <false> > wide_int_ref;
335
336/* This can be used instead of wide_int_ref if the referenced value is
337 known to have type T. It carries across properties of T's representation,
338 such as whether excess upper bits in a HWI are defined, and can therefore
339 help avoid redundant work.
340
341 The macro could be replaced with a template typedef, once we're able
342 to use those. */
343#define WIDE_INT_REF_FOR(T)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T>::is_sign_extended, wi::int_traits <T>::host_dependent_precision
> >
\
344 generic_wide_int \
345 <wide_int_ref_storage <wi::int_traits <T>::is_sign_extended, \
346 wi::int_traits <T>::host_dependent_precision> >
347
348namespace wi
349{
350 /* Operations that calculate overflow do so even for
351 TYPE_OVERFLOW_WRAPS types. For example, adding 1 to +MAX_INT in
352 an unsigned int is 0 and does not overflow in C/C++, but wi::add
353 will set the overflow argument in case it's needed for further
354 analysis.
355
356 For operations that require overflow, these are the different
357 types of overflow. */
358 enum overflow_type {
359 OVF_NONE = 0,
360 OVF_UNDERFLOW = -1,
361 OVF_OVERFLOW = 1,
362 /* There was an overflow, but we are unsure whether it was an
363 overflow or an underflow. */
364 OVF_UNKNOWN = 2
365 };
366
367 /* Classifies an integer based on its precision. */
368 enum precision_type {
369 /* The integer has both a precision and defined signedness. This allows
370 the integer to be converted to any width, since we know whether to fill
371 any extra bits with zeros or signs. */
372 FLEXIBLE_PRECISION,
373
374 /* The integer has a variable precision but no defined signedness. */
375 VAR_PRECISION,
376
377 /* The integer has a constant precision (known at GCC compile time)
378 and is signed. */
379 CONST_PRECISION
380 };
381
382 /* This class, which has no default implementation, is expected to
383 provide the following members:
384
385 static const enum precision_type precision_type;
386 Classifies the type of T.
387
388 static const unsigned int precision;
389 Only defined if precision_type == CONST_PRECISION. Specifies the
390 precision of all integers of type T.
391
392 static const bool host_dependent_precision;
393 True if the precision of T depends (or can depend) on the host.
394
395 static unsigned int get_precision (const T &x)
396 Return the number of bits in X.
397
398 static wi::storage_ref *decompose (HOST_WIDE_INT *scratch,
399 unsigned int precision, const T &x)
400 Decompose X as a PRECISION-bit integer, returning the associated
401 wi::storage_ref. SCRATCH is available as scratch space if needed.
402 The routine should assert that PRECISION is acceptable. */
403 template <typename T> struct int_traits;
404
405 /* This class provides a single type, result_type, which specifies the
406 type of integer produced by a binary operation whose inputs have
407 types T1 and T2. The definition should be symmetric. */
408 template <typename T1, typename T2,
409 enum precision_type P1 = int_traits <T1>::precision_type,
410 enum precision_type P2 = int_traits <T2>::precision_type>
411 struct binary_traits;
412
413 /* Specify the result type for each supported combination of binary
414 inputs. Note that CONST_PRECISION and VAR_PRECISION cannot be
415 mixed, in order to give stronger type checking. When both inputs
416 are CONST_PRECISION, they must have the same precision. */
417 template <typename T1, typename T2>
418 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, FLEXIBLE_PRECISION>
419 {
420 typedef widest_int result_type;
421 /* Don't define operators for this combination. */
422 };
423
424 template <typename T1, typename T2>
425 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, VAR_PRECISION>
426 {
427 typedef wide_int result_type;
428 typedef result_type operator_result;
429 typedef bool predicate_result;
430 };
431
432 template <typename T1, typename T2>
433 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, CONST_PRECISION>
434 {
435 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
436 so as not to confuse gengtype. */
437 typedef generic_wide_int < fixed_wide_int_storage
438 <int_traits <T2>::precision> > result_type;
439 typedef result_type operator_result;
440 typedef bool predicate_result;
441 typedef result_type signed_shift_result_type;
442 typedef bool signed_predicate_result;
443 };
444
445 template <typename T1, typename T2>
446 struct binary_traits <T1, T2, VAR_PRECISION, FLEXIBLE_PRECISION>
447 {
448 typedef wide_int result_type;
449 typedef result_type operator_result;
450 typedef bool predicate_result;
451 };
452
453 template <typename T1, typename T2>
454 struct binary_traits <T1, T2, CONST_PRECISION, FLEXIBLE_PRECISION>
455 {
456 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
457 so as not to confuse gengtype. */
458 typedef generic_wide_int < fixed_wide_int_storage
459 <int_traits <T1>::precision> > result_type;
460 typedef result_type operator_result;
461 typedef bool predicate_result;
462 typedef result_type signed_shift_result_type;
463 typedef bool signed_predicate_result;
464 };
465
466 template <typename T1, typename T2>
467 struct binary_traits <T1, T2, CONST_PRECISION, CONST_PRECISION>
468 {
469 STATIC_ASSERT (int_traits <T1>::precision == int_traits <T2>::precision)static_assert ((int_traits <T1>::precision == int_traits
<T2>::precision), "int_traits <T1>::precision == int_traits <T2>::precision"
)
;
470 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
471 so as not to confuse gengtype. */
472 typedef generic_wide_int < fixed_wide_int_storage
473 <int_traits <T1>::precision> > result_type;
474 typedef result_type operator_result;
475 typedef bool predicate_result;
476 typedef result_type signed_shift_result_type;
477 typedef bool signed_predicate_result;
478 };
479
480 template <typename T1, typename T2>
481 struct binary_traits <T1, T2, VAR_PRECISION, VAR_PRECISION>
482 {
483 typedef wide_int result_type;
484 typedef result_type operator_result;
485 typedef bool predicate_result;
486 };
487}
488
489/* Public functions for querying and operating on integers. */
490namespace wi
491{
492 template <typename T>
493 unsigned int get_precision (const T &);
494
495 template <typename T1, typename T2>
496 unsigned int get_binary_precision (const T1 &, const T2 &);
497
498 template <typename T1, typename T2>
499 void copy (T1 &, const T2 &);
500
501#define UNARY_PREDICATE \
502 template <typename T> bool
503#define UNARY_FUNCTION \
504 template <typename T> WI_UNARY_RESULT (T)typename wi::binary_traits <T, T>::result_type
505#define BINARY_PREDICATE \
506 template <typename T1, typename T2> bool
507#define BINARY_FUNCTION \
508 template <typename T1, typename T2> WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
509#define SHIFT_FUNCTION \
510 template <typename T1, typename T2> WI_UNARY_RESULT (T1)typename wi::binary_traits <T1, T1>::result_type
511
512 UNARY_PREDICATE fits_shwi_p (const T &);
513 UNARY_PREDICATE fits_uhwi_p (const T &);
514 UNARY_PREDICATE neg_p (const T &, signop = SIGNED);
515
516 template <typename T>
517 HOST_WIDE_INTlong sign_mask (const T &);
518
519 BINARY_PREDICATE eq_p (const T1 &, const T2 &);
520 BINARY_PREDICATE ne_p (const T1 &, const T2 &);
521 BINARY_PREDICATE lt_p (const T1 &, const T2 &, signop);
522 BINARY_PREDICATE lts_p (const T1 &, const T2 &);
523 BINARY_PREDICATE ltu_p (const T1 &, const T2 &);
524 BINARY_PREDICATE le_p (const T1 &, const T2 &, signop);
525 BINARY_PREDICATE les_p (const T1 &, const T2 &);
526 BINARY_PREDICATE leu_p (const T1 &, const T2 &);
527 BINARY_PREDICATE gt_p (const T1 &, const T2 &, signop);
528 BINARY_PREDICATE gts_p (const T1 &, const T2 &);
529 BINARY_PREDICATE gtu_p (const T1 &, const T2 &);
530 BINARY_PREDICATE ge_p (const T1 &, const T2 &, signop);
531 BINARY_PREDICATE ges_p (const T1 &, const T2 &);
532 BINARY_PREDICATE geu_p (const T1 &, const T2 &);
533
534 template <typename T1, typename T2>
535 int cmp (const T1 &, const T2 &, signop);
536
537 template <typename T1, typename T2>
538 int cmps (const T1 &, const T2 &);
539
540 template <typename T1, typename T2>
541 int cmpu (const T1 &, const T2 &);
542
543 UNARY_FUNCTION bit_not (const T &);
544 UNARY_FUNCTION neg (const T &);
545 UNARY_FUNCTION neg (const T &, overflow_type *);
546 UNARY_FUNCTION abs (const T &);
547 UNARY_FUNCTION ext (const T &, unsigned int, signop);
548 UNARY_FUNCTION sext (const T &, unsigned int);
549 UNARY_FUNCTION zext (const T &, unsigned int);
550 UNARY_FUNCTION set_bit (const T &, unsigned int);
551
552 BINARY_FUNCTION min (const T1 &, const T2 &, signop);
553 BINARY_FUNCTION smin (const T1 &, const T2 &);
554 BINARY_FUNCTION umin (const T1 &, const T2 &);
555 BINARY_FUNCTION max (const T1 &, const T2 &, signop);
556 BINARY_FUNCTION smax (const T1 &, const T2 &);
557 BINARY_FUNCTION umax (const T1 &, const T2 &);
558
559 BINARY_FUNCTION bit_and (const T1 &, const T2 &);
560 BINARY_FUNCTION bit_and_not (const T1 &, const T2 &);
561 BINARY_FUNCTION bit_or (const T1 &, const T2 &);
562 BINARY_FUNCTION bit_or_not (const T1 &, const T2 &);
563 BINARY_FUNCTION bit_xor (const T1 &, const T2 &);
564 BINARY_FUNCTION add (const T1 &, const T2 &);
565 BINARY_FUNCTION add (const T1 &, const T2 &, signop, overflow_type *);
566 BINARY_FUNCTION sub (const T1 &, const T2 &);
567 BINARY_FUNCTION sub (const T1 &, const T2 &, signop, overflow_type *);
568 BINARY_FUNCTION mul (const T1 &, const T2 &);
569 BINARY_FUNCTION mul (const T1 &, const T2 &, signop, overflow_type *);
570 BINARY_FUNCTION smul (const T1 &, const T2 &, overflow_type *);
571 BINARY_FUNCTION umul (const T1 &, const T2 &, overflow_type *);
572 BINARY_FUNCTION mul_high (const T1 &, const T2 &, signop);
573 BINARY_FUNCTION div_trunc (const T1 &, const T2 &, signop,
574 overflow_type * = 0);
575 BINARY_FUNCTION sdiv_trunc (const T1 &, const T2 &);
576 BINARY_FUNCTION udiv_trunc (const T1 &, const T2 &);
577 BINARY_FUNCTION div_floor (const T1 &, const T2 &, signop,
578 overflow_type * = 0);
579 BINARY_FUNCTION udiv_floor (const T1 &, const T2 &);
580 BINARY_FUNCTION sdiv_floor (const T1 &, const T2 &);
581 BINARY_FUNCTION div_ceil (const T1 &, const T2 &, signop,
582 overflow_type * = 0);
583 BINARY_FUNCTION udiv_ceil (const T1 &, const T2 &);
584 BINARY_FUNCTION div_round (const T1 &, const T2 &, signop,
585 overflow_type * = 0);
586 BINARY_FUNCTION divmod_trunc (const T1 &, const T2 &, signop,
587 WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type *);
588 BINARY_FUNCTION gcd (const T1 &, const T2 &, signop = UNSIGNED);
589 BINARY_FUNCTION mod_trunc (const T1 &, const T2 &, signop,
590 overflow_type * = 0);
591 BINARY_FUNCTION smod_trunc (const T1 &, const T2 &);
592 BINARY_FUNCTION umod_trunc (const T1 &, const T2 &);
593 BINARY_FUNCTION mod_floor (const T1 &, const T2 &, signop,
594 overflow_type * = 0);
595 BINARY_FUNCTION umod_floor (const T1 &, const T2 &);
596 BINARY_FUNCTION mod_ceil (const T1 &, const T2 &, signop,
597 overflow_type * = 0);
598 BINARY_FUNCTION mod_round (const T1 &, const T2 &, signop,
599 overflow_type * = 0);
600
601 template <typename T1, typename T2>
602 bool multiple_of_p (const T1 &, const T2 &, signop);
603
604 template <typename T1, typename T2>
605 bool multiple_of_p (const T1 &, const T2 &, signop,
606 WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type *);
607
608 SHIFT_FUNCTION lshift (const T1 &, const T2 &);
609 SHIFT_FUNCTION lrshift (const T1 &, const T2 &);
610 SHIFT_FUNCTION arshift (const T1 &, const T2 &);
611 SHIFT_FUNCTION rshift (const T1 &, const T2 &, signop sgn);
612 SHIFT_FUNCTION lrotate (const T1 &, const T2 &, unsigned int = 0);
613 SHIFT_FUNCTION rrotate (const T1 &, const T2 &, unsigned int = 0);
614
615#undef SHIFT_FUNCTION
616#undef BINARY_PREDICATE
617#undef BINARY_FUNCTION
618#undef UNARY_PREDICATE
619#undef UNARY_FUNCTION
620
621 bool only_sign_bit_p (const wide_int_ref &, unsigned int);
622 bool only_sign_bit_p (const wide_int_ref &);
623 int clz (const wide_int_ref &);
624 int clrsb (const wide_int_ref &);
625 int ctz (const wide_int_ref &);
626 int exact_log2 (const wide_int_ref &);
627 int floor_log2 (const wide_int_ref &);
628 int ffs (const wide_int_ref &);
629 int popcount (const wide_int_ref &);
630 int parity (const wide_int_ref &);
631
632 template <typename T>
633 unsigned HOST_WIDE_INTlong extract_uhwi (const T &, unsigned int, unsigned int);
634
635 template <typename T>
636 unsigned int min_precision (const T &, signop);
637
638 static inline void accumulate_overflow (overflow_type &, overflow_type);
639}
640
641namespace wi
642{
643 /* Contains the components of a decomposed integer for easy, direct
644 access. */
645 class storage_ref
646 {
647 public:
648 storage_ref () {}
649 storage_ref (const HOST_WIDE_INTlong *, unsigned int, unsigned int);
650
651 const HOST_WIDE_INTlong *val;
652 unsigned int len;
653 unsigned int precision;
654
655 /* Provide enough trappings for this class to act as storage for
656 generic_wide_int. */
657 unsigned int get_len () const;
658 unsigned int get_precision () const;
659 const HOST_WIDE_INTlong *get_val () const;
660 };
661}
662
663inline::wi::storage_ref::storage_ref (const HOST_WIDE_INTlong *val_in,
664 unsigned int len_in,
665 unsigned int precision_in)
666 : val (val_in), len (len_in), precision (precision_in)
667{
668}
669
670inline unsigned int
671wi::storage_ref::get_len () const
672{
673 return len;
674}
675
676inline unsigned int
677wi::storage_ref::get_precision () const
678{
679 return precision;
680}
681
682inline const HOST_WIDE_INTlong *
683wi::storage_ref::get_val () const
684{
685 return val;
686}
687
688/* This class defines an integer type using the storage provided by the
689 template argument. The storage class must provide the following
690 functions:
691
692 unsigned int get_precision () const
693 Return the number of bits in the integer.
694
695 HOST_WIDE_INT *get_val () const
696 Return a pointer to the array of blocks that encodes the integer.
697
698 unsigned int get_len () const
699 Return the number of blocks in get_val (). If this is smaller
700 than the number of blocks implied by get_precision (), the
701 remaining blocks are sign extensions of block get_len () - 1.
702
703 Although not required by generic_wide_int itself, writable storage
704 classes can also provide the following functions:
705
706 HOST_WIDE_INT *write_val ()
707 Get a modifiable version of get_val ()
708
709 unsigned int set_len (unsigned int len)
710 Set the value returned by get_len () to LEN. */
711template <typename storage>
712class GTY(()) generic_wide_int : public storage
713{
714public:
715 generic_wide_int ();
716
717 template <typename T>
718 generic_wide_int (const T &);
719
720 template <typename T>
721 generic_wide_int (const T &, unsigned int);
722
723 /* Conversions. */
724 HOST_WIDE_INTlong to_shwi (unsigned int) const;
725 HOST_WIDE_INTlong to_shwi () const;
726 unsigned HOST_WIDE_INTlong to_uhwi (unsigned int) const;
727 unsigned HOST_WIDE_INTlong to_uhwi () const;
728 HOST_WIDE_INTlong to_short_addr () const;
729
730 /* Public accessors for the interior of a wide int. */
731 HOST_WIDE_INTlong sign_mask () const;
732 HOST_WIDE_INTlong elt (unsigned int) const;
733 HOST_WIDE_INTlong sext_elt (unsigned int) const;
734 unsigned HOST_WIDE_INTlong ulow () const;
735 unsigned HOST_WIDE_INTlong uhigh () const;
736 HOST_WIDE_INTlong slow () const;
737 HOST_WIDE_INTlong shigh () const;
738
739 template <typename T>
740 generic_wide_int &operator = (const T &);
741
742#define ASSIGNMENT_OPERATOR(OP, F) \
743 template <typename T> \
744 generic_wide_int &OP (const T &c) { return (*this = wi::F (*this, c)); }
745
746/* Restrict these to cases where the shift operator is defined. */
747#define SHIFT_ASSIGNMENT_OPERATOR(OP, OP2) \
748 template <typename T> \
749 generic_wide_int &OP (const T &c) { return (*this = *this OP2 c); }
750
751#define INCDEC_OPERATOR(OP, DELTA) \
752 generic_wide_int &OP () { *this += DELTA; return *this; }
753
754 ASSIGNMENT_OPERATOR (operator &=, bit_and)
755 ASSIGNMENT_OPERATOR (operator |=, bit_or)
756 ASSIGNMENT_OPERATOR (operator ^=, bit_xor)
757 ASSIGNMENT_OPERATOR (operator +=, add)
758 ASSIGNMENT_OPERATOR (operator -=, sub)
759 ASSIGNMENT_OPERATOR (operator *=, mul)
760 ASSIGNMENT_OPERATOR (operator <<=, lshift)
761 SHIFT_ASSIGNMENT_OPERATOR (operator >>=, >>)
762 INCDEC_OPERATOR (operator ++, 1)
763 INCDEC_OPERATOR (operator --, -1)
764
765#undef SHIFT_ASSIGNMENT_OPERATOR
766#undef ASSIGNMENT_OPERATOR
767#undef INCDEC_OPERATOR
768
769 /* Debugging functions. */
770 void dump () const;
771
772 static const bool is_sign_extended
773 = wi::int_traits <generic_wide_int <storage> >::is_sign_extended;
774};
775
776template <typename storage>
777inline generic_wide_int <storage>::generic_wide_int () {}
778
779template <typename storage>
780template <typename T>
781inline generic_wide_int <storage>::generic_wide_int (const T &x)
782 : storage (x)
783{
784}
785
786template <typename storage>
787template <typename T>
788inline generic_wide_int <storage>::generic_wide_int (const T &x,
789 unsigned int precision)
790 : storage (x, precision)
791{
792}
793
794/* Return THIS as a signed HOST_WIDE_INT, sign-extending from PRECISION.
795 If THIS does not fit in PRECISION, the information is lost. */
796template <typename storage>
797inline HOST_WIDE_INTlong
798generic_wide_int <storage>::to_shwi (unsigned int precision) const
799{
800 if (precision < HOST_BITS_PER_WIDE_INT64)
801 return sext_hwi (this->get_val ()[0], precision);
802 else
803 return this->get_val ()[0];
804}
805
806/* Return THIS as a signed HOST_WIDE_INT, in its natural precision. */
807template <typename storage>
808inline HOST_WIDE_INTlong
809generic_wide_int <storage>::to_shwi () const
810{
811 if (is_sign_extended)
812 return this->get_val ()[0];
813 else
814 return to_shwi (this->get_precision ());
815}
816
817/* Return THIS as an unsigned HOST_WIDE_INT, zero-extending from
818 PRECISION. If THIS does not fit in PRECISION, the information
819 is lost. */
820template <typename storage>
821inline unsigned HOST_WIDE_INTlong
822generic_wide_int <storage>::to_uhwi (unsigned int precision) const
823{
824 if (precision
12.1
'precision' is < HOST_BITS_PER_WIDE_INT
12.1
'precision' is < HOST_BITS_PER_WIDE_INT
12.1
'precision' is < HOST_BITS_PER_WIDE_INT
< HOST_BITS_PER_WIDE_INT64)
13
Taking true branch
825 return zext_hwi (this->get_val ()[0], precision);
14
Calling 'zext_hwi'
18
Returning from 'zext_hwi'
19
Returning zero
826 else
827 return this->get_val ()[0];
828}
829
830/* Return THIS as an signed HOST_WIDE_INT, in its natural precision. */
831template <typename storage>
832inline unsigned HOST_WIDE_INTlong
833generic_wide_int <storage>::to_uhwi () const
834{
835 return to_uhwi (this->get_precision ());
12
Calling 'generic_wide_int::to_uhwi'
20
Returning from 'generic_wide_int::to_uhwi'
21
Returning zero
836}
837
838/* TODO: The compiler is half converted from using HOST_WIDE_INT to
839 represent addresses to using offset_int to represent addresses.
840 We use to_short_addr at the interface from new code to old,
841 unconverted code. */
842template <typename storage>
843inline HOST_WIDE_INTlong
844generic_wide_int <storage>::to_short_addr () const
845{
846 return this->get_val ()[0];
847}
848
849/* Return the implicit value of blocks above get_len (). */
850template <typename storage>
851inline HOST_WIDE_INTlong
852generic_wide_int <storage>::sign_mask () const
853{
854 unsigned int len = this->get_len ();
855 gcc_assert (len > 0)((void)(!(len > 0) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/wide-int.h"
, 855, __FUNCTION__), 0 : 0))
;
856
857 unsigned HOST_WIDE_INTlong high = this->get_val ()[len - 1];
858 if (!is_sign_extended)
859 {
860 unsigned int precision = this->get_precision ();
861 int excess = len * HOST_BITS_PER_WIDE_INT64 - precision;
862 if (excess > 0)
863 high <<= excess;
864 }
865 return (HOST_WIDE_INTlong) (high) < 0 ? -1 : 0;
866}
867
868/* Return the signed value of the least-significant explicitly-encoded
869 block. */
870template <typename storage>
871inline HOST_WIDE_INTlong
872generic_wide_int <storage>::slow () const
873{
874 return this->get_val ()[0];
875}
876
877/* Return the signed value of the most-significant explicitly-encoded
878 block. */
879template <typename storage>
880inline HOST_WIDE_INTlong
881generic_wide_int <storage>::shigh () const
882{
883 return this->get_val ()[this->get_len () - 1];
884}
885
886/* Return the unsigned value of the least-significant
887 explicitly-encoded block. */
888template <typename storage>
889inline unsigned HOST_WIDE_INTlong
890generic_wide_int <storage>::ulow () const
891{
892 return this->get_val ()[0];
893}
894
895/* Return the unsigned value of the most-significant
896 explicitly-encoded block. */
897template <typename storage>
898inline unsigned HOST_WIDE_INTlong
899generic_wide_int <storage>::uhigh () const
900{
901 return this->get_val ()[this->get_len () - 1];
902}
903
904/* Return block I, which might be implicitly or explicit encoded. */
905template <typename storage>
906inline HOST_WIDE_INTlong
907generic_wide_int <storage>::elt (unsigned int i) const
908{
909 if (i >= this->get_len ())
910 return sign_mask ();
911 else
912 return this->get_val ()[i];
913}
914
915/* Like elt, but sign-extend beyond the upper bit, instead of returning
916 the raw encoding. */
917template <typename storage>
918inline HOST_WIDE_INTlong
919generic_wide_int <storage>::sext_elt (unsigned int i) const
920{
921 HOST_WIDE_INTlong elt_i = elt (i);
922 if (!is_sign_extended)
923 {
924 unsigned int precision = this->get_precision ();
925 unsigned int lsb = i * HOST_BITS_PER_WIDE_INT64;
926 if (precision - lsb < HOST_BITS_PER_WIDE_INT64)
927 elt_i = sext_hwi (elt_i, precision - lsb);
928 }
929 return elt_i;
930}
931
932template <typename storage>
933template <typename T>
934inline generic_wide_int <storage> &
935generic_wide_int <storage>::operator = (const T &x)
936{
937 storage::operator = (x);
938 return *this;
939}
940
941/* Dump the contents of the integer to stderr, for debugging. */
942template <typename storage>
943void
944generic_wide_int <storage>::dump () const
945{
946 unsigned int len = this->get_len ();
947 const HOST_WIDE_INTlong *val = this->get_val ();
948 unsigned int precision = this->get_precision ();
949 fprintf (stderrstderr, "[");
950 if (len * HOST_BITS_PER_WIDE_INT64 < precision)
951 fprintf (stderrstderr, "...,");
952 for (unsigned int i = 0; i < len - 1; ++i)
953 fprintf (stderrstderr, HOST_WIDE_INT_PRINT_HEX"%#" "l" "x" ",", val[len - 1 - i]);
954 fprintf (stderrstderr, HOST_WIDE_INT_PRINT_HEX"%#" "l" "x" "], precision = %d\n",
955 val[0], precision);
956}
957
958namespace wi
959{
960 template <typename storage>
961 struct int_traits < generic_wide_int <storage> >
962 : public wi::int_traits <storage>
963 {
964 static unsigned int get_precision (const generic_wide_int <storage> &);
965 static wi::storage_ref decompose (HOST_WIDE_INTlong *, unsigned int,
966 const generic_wide_int <storage> &);
967 };
968}
969
970template <typename storage>
971inline unsigned int
972wi::int_traits < generic_wide_int <storage> >::
973get_precision (const generic_wide_int <storage> &x)
974{
975 return x.get_precision ();
976}
977
978template <typename storage>
979inline wi::storage_ref
980wi::int_traits < generic_wide_int <storage> >::
981decompose (HOST_WIDE_INTlong *, unsigned int precision,
982 const generic_wide_int <storage> &x)
983{
984 gcc_checking_assert (precision == x.get_precision ())((void)(!(precision == x.get_precision ()) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/wide-int.h"
, 984, __FUNCTION__), 0 : 0))
;
985 return wi::storage_ref (x.get_val (), x.get_len (), precision);
986}
987
988/* Provide the storage for a wide_int_ref. This acts like a read-only
989 wide_int, with the optimization that VAL is normally a pointer to
990 another integer's storage, so that no array copy is needed. */
991template <bool SE, bool HDP>
992class wide_int_ref_storage : public wi::storage_ref
993{
994private:
995 /* Scratch space that can be used when decomposing the original integer.
996 It must live as long as this object. */
997 HOST_WIDE_INTlong scratch[2];
998
999public:
1000 wide_int_ref_storage () {}
1001
1002 wide_int_ref_storage (const wi::storage_ref &);
1003
1004 template <typename T>
1005 wide_int_ref_storage (const T &);
1006
1007 template <typename T>
1008 wide_int_ref_storage (const T &, unsigned int);
1009};
1010
1011/* Create a reference from an existing reference. */
1012template <bool SE, bool HDP>
1013inline wide_int_ref_storage <SE, HDP>::
1014wide_int_ref_storage (const wi::storage_ref &x)
1015 : storage_ref (x)
1016{}
1017
1018/* Create a reference to integer X in its natural precision. Note
1019 that the natural precision is host-dependent for primitive
1020 types. */
1021template <bool SE, bool HDP>
1022template <typename T>
1023inline wide_int_ref_storage <SE, HDP>::wide_int_ref_storage (const T &x)
1024 : storage_ref (wi::int_traits <T>::decompose (scratch,
1025 wi::get_precision (x), x))
1026{
1027}
1028
1029/* Create a reference to integer X in precision PRECISION. */
1030template <bool SE, bool HDP>
1031template <typename T>
1032inline wide_int_ref_storage <SE, HDP>::
1033wide_int_ref_storage (const T &x, unsigned int precision)
1034 : storage_ref (wi::int_traits <T>::decompose (scratch, precision, x))
1035{
1036}
1037
1038namespace wi
1039{
1040 template <bool SE, bool HDP>
1041 struct int_traits <wide_int_ref_storage <SE, HDP> >
1042 {
1043 static const enum precision_type precision_type = VAR_PRECISION;
1044 static const bool host_dependent_precision = HDP;
1045 static const bool is_sign_extended = SE;
1046 };
1047}
1048
1049namespace wi
1050{
1051 unsigned int force_to_size (HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *,
1052 unsigned int, unsigned int, unsigned int,
1053 signop sgn);
1054 unsigned int from_array (HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *,
1055 unsigned int, unsigned int, bool = true);
1056}
1057
1058/* The storage used by wide_int. */
1059class GTY(()) wide_int_storage
1060{
1061private:
1062 HOST_WIDE_INTlong val[WIDE_INT_MAX_ELTS(((64*(8)) + 64) / 64)];
1063 unsigned int len;
1064 unsigned int precision;
1065
1066public:
1067 wide_int_storage ();
1068 template <typename T>
1069 wide_int_storage (const T &);
1070
1071 /* The standard generic_wide_int storage methods. */
1072 unsigned int get_precision () const;
1073 const HOST_WIDE_INTlong *get_val () const;
1074 unsigned int get_len () const;
1075 HOST_WIDE_INTlong *write_val ();
1076 void set_len (unsigned int, bool = false);
1077
1078 template <typename T>
1079 wide_int_storage &operator = (const T &);
1080
1081 static wide_int from (const wide_int_ref &, unsigned int, signop);
1082 static wide_int from_array (const HOST_WIDE_INTlong *, unsigned int,
1083 unsigned int, bool = true);
1084 static wide_int create (unsigned int);
1085
1086 /* FIXME: target-dependent, so should disappear. */
1087 wide_int bswap () const;
1088};
1089
1090namespace wi
1091{
1092 template <>
1093 struct int_traits <wide_int_storage>
1094 {
1095 static const enum precision_type precision_type = VAR_PRECISION;
1096 /* Guaranteed by a static assert in the wide_int_storage constructor. */
1097 static const bool host_dependent_precision = false;
1098 static const bool is_sign_extended = true;
1099 template <typename T1, typename T2>
1100 static wide_int get_binary_result (const T1 &, const T2 &);
1101 };
1102}
1103
1104inline wide_int_storage::wide_int_storage () {}
1105
1106/* Initialize the storage from integer X, in its natural precision.
1107 Note that we do not allow integers with host-dependent precision
1108 to become wide_ints; wide_ints must always be logically independent
1109 of the host. */
1110template <typename T>
1111inline wide_int_storage::wide_int_storage (const T &x)
1112{
1113 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision)static_assert ((!wi::int_traits<T>::host_dependent_precision
), "!wi::int_traits<T>::host_dependent_precision")
; }
1114 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION)static_assert ((wi::int_traits<T>::precision_type != wi
::CONST_PRECISION), "wi::int_traits<T>::precision_type != wi::CONST_PRECISION"
)
; }
1115 WIDE_INT_REF_FOR (T)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T>::is_sign_extended, wi::int_traits <T>::host_dependent_precision
> >
xi (x);
1116 precision = xi.precision;
1117 wi::copy (*this, xi);
1118}
1119
1120template <typename T>
1121inline wide_int_storage&
1122wide_int_storage::operator = (const T &x)
1123{
1124 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision)static_assert ((!wi::int_traits<T>::host_dependent_precision
), "!wi::int_traits<T>::host_dependent_precision")
; }
1125 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION)static_assert ((wi::int_traits<T>::precision_type != wi
::CONST_PRECISION), "wi::int_traits<T>::precision_type != wi::CONST_PRECISION"
)
; }
1126 WIDE_INT_REF_FOR (T)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T>::is_sign_extended, wi::int_traits <T>::host_dependent_precision
> >
xi (x);
1127 precision = xi.precision;
1128 wi::copy (*this, xi);
1129 return *this;
1130}
1131
1132inline unsigned int
1133wide_int_storage::get_precision () const
1134{
1135 return precision;
1136}
1137
1138inline const HOST_WIDE_INTlong *
1139wide_int_storage::get_val () const
1140{
1141 return val;
1142}
1143
1144inline unsigned int
1145wide_int_storage::get_len () const
1146{
1147 return len;
1148}
1149
1150inline HOST_WIDE_INTlong *
1151wide_int_storage::write_val ()
1152{
1153 return val;
1154}
1155
1156inline void
1157wide_int_storage::set_len (unsigned int l, bool is_sign_extended)
1158{
1159 len = l;
1160 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT64 > precision)
1161 val[len - 1] = sext_hwi (val[len - 1],
1162 precision % HOST_BITS_PER_WIDE_INT64);
1163}
1164
1165/* Treat X as having signedness SGN and convert it to a PRECISION-bit
1166 number. */
1167inline wide_int
1168wide_int_storage::from (const wide_int_ref &x, unsigned int precision,
1169 signop sgn)
1170{
1171 wide_int result = wide_int::create (precision);
1172 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1173 x.precision, precision, sgn));
1174 return result;
1175}
1176
1177/* Create a wide_int from the explicit block encoding given by VAL and
1178 LEN. PRECISION is the precision of the integer. NEED_CANON_P is
1179 true if the encoding may have redundant trailing blocks. */
1180inline wide_int
1181wide_int_storage::from_array (const HOST_WIDE_INTlong *val, unsigned int len,
1182 unsigned int precision, bool need_canon_p)
1183{
1184 wide_int result = wide_int::create (precision);
1185 result.set_len (wi::from_array (result.write_val (), val, len, precision,
1186 need_canon_p));
1187 return result;
1188}
1189
1190/* Return an uninitialized wide_int with precision PRECISION. */
1191inline wide_int
1192wide_int_storage::create (unsigned int precision)
1193{
1194 wide_int x;
1195 x.precision = precision;
1196 return x;
1197}
1198
1199template <typename T1, typename T2>
1200inline wide_int
1201wi::int_traits <wide_int_storage>::get_binary_result (const T1 &x, const T2 &y)
1202{
1203 /* This shouldn't be used for two flexible-precision inputs. */
1204 STATIC_ASSERT (wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISIONstatic_assert ((wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION
|| wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION
), "wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION || wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION"
)
1205 || wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION)static_assert ((wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION
|| wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION
), "wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION || wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION"
)
;
1206 if (wi::int_traits <T1>::precision_type == FLEXIBLE_PRECISION)
1207 return wide_int::create (wi::get_precision (y));
1208 else
1209 return wide_int::create (wi::get_precision (x));
1210}
1211
1212/* The storage used by FIXED_WIDE_INT (N). */
1213template <int N>
1214class GTY(()) fixed_wide_int_storage
1215{
1216private:
1217 HOST_WIDE_INTlong val[(N + HOST_BITS_PER_WIDE_INT64 + 1) / HOST_BITS_PER_WIDE_INT64];
1218 unsigned int len;
1219
1220public:
1221 fixed_wide_int_storage ();
1222 template <typename T>
1223 fixed_wide_int_storage (const T &);
1224
1225 /* The standard generic_wide_int storage methods. */
1226 unsigned int get_precision () const;
1227 const HOST_WIDE_INTlong *get_val () const;
1228 unsigned int get_len () const;
1229 HOST_WIDE_INTlong *write_val ();
1230 void set_len (unsigned int, bool = false);
1231
1232 static FIXED_WIDE_INT (N)generic_wide_int < fixed_wide_int_storage <N> > from (const wide_int_ref &, signop);
1233 static FIXED_WIDE_INT (N)generic_wide_int < fixed_wide_int_storage <N> > from_array (const HOST_WIDE_INTlong *, unsigned int,
1234 bool = true);
1235};
1236
1237namespace wi
1238{
1239 template <int N>
1240 struct int_traits < fixed_wide_int_storage <N> >
1241 {
1242 static const enum precision_type precision_type = CONST_PRECISION;
1243 static const bool host_dependent_precision = false;
1244 static const bool is_sign_extended = true;
1245 static const unsigned int precision = N;
1246 template <typename T1, typename T2>
1247 static FIXED_WIDE_INT (N)generic_wide_int < fixed_wide_int_storage <N> > get_binary_result (const T1 &, const T2 &);
1248 };
1249}
1250
1251template <int N>
1252inline fixed_wide_int_storage <N>::fixed_wide_int_storage () {}
1253
1254/* Initialize the storage from integer X, in precision N. */
1255template <int N>
1256template <typename T>
1257inline fixed_wide_int_storage <N>::fixed_wide_int_storage (const T &x)
1258{
1259 /* Check for type compatibility. We don't want to initialize a
1260 fixed-width integer from something like a wide_int. */
1261 WI_BINARY_RESULT (T, FIXED_WIDE_INT (N))typename wi::binary_traits <T, generic_wide_int < fixed_wide_int_storage
<N> > >::result_type
*assertion ATTRIBUTE_UNUSED__attribute__ ((__unused__));
1262 wi::copy (*this, WIDE_INT_REF_FOR (T)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T>::is_sign_extended, wi::int_traits <T>::host_dependent_precision
> >
(x, N));
1263}
1264
1265template <int N>
1266inline unsigned int
1267fixed_wide_int_storage <N>::get_precision () const
1268{
1269 return N;
1270}
1271
1272template <int N>
1273inline const HOST_WIDE_INTlong *
1274fixed_wide_int_storage <N>::get_val () const
1275{
1276 return val;
1277}
1278
1279template <int N>
1280inline unsigned int
1281fixed_wide_int_storage <N>::get_len () const
1282{
1283 return len;
1284}
1285
1286template <int N>
1287inline HOST_WIDE_INTlong *
1288fixed_wide_int_storage <N>::write_val ()
1289{
1290 return val;
1291}
1292
1293template <int N>
1294inline void
1295fixed_wide_int_storage <N>::set_len (unsigned int l, bool)
1296{
1297 len = l;
1298 /* There are no excess bits in val[len - 1]. */
1299 STATIC_ASSERT (N % HOST_BITS_PER_WIDE_INT == 0)static_assert ((N % 64 == 0), "N % HOST_BITS_PER_WIDE_INT == 0"
)
;
1300}
1301
1302/* Treat X as having signedness SGN and convert it to an N-bit number. */
1303template <int N>
1304inline FIXED_WIDE_INT (N)generic_wide_int < fixed_wide_int_storage <N> >
1305fixed_wide_int_storage <N>::from (const wide_int_ref &x, signop sgn)
1306{
1307 FIXED_WIDE_INT (N)generic_wide_int < fixed_wide_int_storage <N> > result;
1308 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1309 x.precision, N, sgn));
1310 return result;
1311}
1312
1313/* Create a FIXED_WIDE_INT (N) from the explicit block encoding given by
1314 VAL and LEN. NEED_CANON_P is true if the encoding may have redundant
1315 trailing blocks. */
1316template <int N>
1317inline FIXED_WIDE_INT (N)generic_wide_int < fixed_wide_int_storage <N> >
1318fixed_wide_int_storage <N>::from_array (const HOST_WIDE_INTlong *val,
1319 unsigned int len,
1320 bool need_canon_p)
1321{
1322 FIXED_WIDE_INT (N)generic_wide_int < fixed_wide_int_storage <N> > result;
1323 result.set_len (wi::from_array (result.write_val (), val, len,
1324 N, need_canon_p));
1325 return result;
1326}
1327
1328template <int N>
1329template <typename T1, typename T2>
1330inline FIXED_WIDE_INT (N)generic_wide_int < fixed_wide_int_storage <N> >
1331wi::int_traits < fixed_wide_int_storage <N> >::
1332get_binary_result (const T1 &, const T2 &)
1333{
1334 return FIXED_WIDE_INT (N)generic_wide_int < fixed_wide_int_storage <N> > ();
1335}
1336
1337/* A reference to one element of a trailing_wide_ints structure. */
1338class trailing_wide_int_storage
1339{
1340private:
1341 /* The precision of the integer, which is a fixed property of the
1342 parent trailing_wide_ints. */
1343 unsigned int m_precision;
1344
1345 /* A pointer to the length field. */
1346 unsigned char *m_len;
1347
1348 /* A pointer to the HWI array. There are enough elements to hold all
1349 values of precision M_PRECISION. */
1350 HOST_WIDE_INTlong *m_val;
1351
1352public:
1353 trailing_wide_int_storage (unsigned int, unsigned char *, HOST_WIDE_INTlong *);
1354
1355 /* The standard generic_wide_int storage methods. */
1356 unsigned int get_len () const;
1357 unsigned int get_precision () const;
1358 const HOST_WIDE_INTlong *get_val () const;
1359 HOST_WIDE_INTlong *write_val ();
1360 void set_len (unsigned int, bool = false);
1361
1362 template <typename T>
1363 trailing_wide_int_storage &operator = (const T &);
1364};
1365
1366typedef generic_wide_int <trailing_wide_int_storage> trailing_wide_int;
1367
1368/* trailing_wide_int behaves like a wide_int. */
1369namespace wi
1370{
1371 template <>
1372 struct int_traits <trailing_wide_int_storage>
1373 : public int_traits <wide_int_storage> {};
1374}
1375
1376/* A variable-length array of wide_int-like objects that can be put
1377 at the end of a variable-sized structure. The number of objects is
1378 at most N and can be set at runtime by using set_precision().
1379
1380 Use extra_size to calculate how many bytes beyond the
1381 sizeof need to be allocated. Use set_precision to initialize the
1382 structure. */
1383template <int N>
1384struct GTY((user)) trailing_wide_ints
1385{
1386private:
1387 /* The shared precision of each number. */
1388 unsigned short m_precision;
1389
1390 /* The shared maximum length of each number. */
1391 unsigned char m_max_len;
1392
1393 /* The number of elements. */
1394 unsigned char m_num_elements;
1395
1396 /* The current length of each number.
1397 Avoid char array so the whole structure is not a typeless storage
1398 that will, in turn, turn off TBAA on gimple, trees and RTL. */
1399 struct {unsigned char len;} m_len[N];
1400
1401 /* The variable-length part of the structure, which always contains
1402 at least one HWI. Element I starts at index I * M_MAX_LEN. */
1403 HOST_WIDE_INTlong m_val[1];
1404
1405public:
1406 typedef WIDE_INT_REF_FOR (trailing_wide_int_storage)generic_wide_int <wide_int_ref_storage <wi::int_traits <
trailing_wide_int_storage>::is_sign_extended, wi::int_traits
<trailing_wide_int_storage>::host_dependent_precision>
>
const_reference;
1407
1408 void set_precision (unsigned int precision, unsigned int num_elements = N);
1409 unsigned int get_precision () const { return m_precision; }
1410 unsigned int num_elements () const { return m_num_elements; }
1411 trailing_wide_int operator [] (unsigned int);
1412 const_reference operator [] (unsigned int) const;
1413 static size_t extra_size (unsigned int precision,
1414 unsigned int num_elements = N);
1415 size_t extra_size () const { return extra_size (m_precision,
1416 m_num_elements); }
1417};
1418
1419inline trailing_wide_int_storage::
1420trailing_wide_int_storage (unsigned int precision, unsigned char *len,
1421 HOST_WIDE_INTlong *val)
1422 : m_precision (precision), m_len (len), m_val (val)
1423{
1424}
1425
1426inline unsigned int
1427trailing_wide_int_storage::get_len () const
1428{
1429 return *m_len;
1430}
1431
1432inline unsigned int
1433trailing_wide_int_storage::get_precision () const
1434{
1435 return m_precision;
1436}
1437
1438inline const HOST_WIDE_INTlong *
1439trailing_wide_int_storage::get_val () const
1440{
1441 return m_val;
1442}
1443
1444inline HOST_WIDE_INTlong *
1445trailing_wide_int_storage::write_val ()
1446{
1447 return m_val;
1448}
1449
1450inline void
1451trailing_wide_int_storage::set_len (unsigned int len, bool is_sign_extended)
1452{
1453 *m_len = len;
1454 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT64 > m_precision)
1455 m_val[len - 1] = sext_hwi (m_val[len - 1],
1456 m_precision % HOST_BITS_PER_WIDE_INT64);
1457}
1458
1459template <typename T>
1460inline trailing_wide_int_storage &
1461trailing_wide_int_storage::operator = (const T &x)
1462{
1463 WIDE_INT_REF_FOR (T)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T>::is_sign_extended, wi::int_traits <T>::host_dependent_precision
> >
xi (x, m_precision);
1464 wi::copy (*this, xi);
1465 return *this;
1466}
1467
1468/* Initialize the structure and record that all elements have precision
1469 PRECISION. NUM_ELEMENTS can be no more than N. */
1470template <int N>
1471inline void
1472trailing_wide_ints <N>::set_precision (unsigned int precision,
1473 unsigned int num_elements)
1474{
1475 gcc_checking_assert (num_elements <= N)((void)(!(num_elements <= N) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/wide-int.h"
, 1475, __FUNCTION__), 0 : 0))
;
1476 m_num_elements = num_elements;
1477 m_precision = precision;
1478 m_max_len = ((precision + HOST_BITS_PER_WIDE_INT64 - 1)
1479 / HOST_BITS_PER_WIDE_INT64);
1480}
1481
1482/* Return a reference to element INDEX. */
1483template <int N>
1484inline trailing_wide_int
1485trailing_wide_ints <N>::operator [] (unsigned int index)
1486{
1487 return trailing_wide_int_storage (m_precision, &m_len[index].len,
1488 &m_val[index * m_max_len]);
1489}
1490
1491template <int N>
1492inline typename trailing_wide_ints <N>::const_reference
1493trailing_wide_ints <N>::operator [] (unsigned int index) const
1494{
1495 return wi::storage_ref (&m_val[index * m_max_len],
1496 m_len[index].len, m_precision);
1497}
1498
1499/* Return how many extra bytes need to be added to the end of the
1500 structure in order to handle NUM_ELEMENTS wide_ints of precision
1501 PRECISION. NUM_ELEMENTS is the number of elements, and defaults
1502 to N. */
1503template <int N>
1504inline size_t
1505trailing_wide_ints <N>::extra_size (unsigned int precision,
1506 unsigned int num_elements)
1507{
1508 unsigned int max_len = ((precision + HOST_BITS_PER_WIDE_INT64 - 1)
1509 / HOST_BITS_PER_WIDE_INT64);
1510 gcc_checking_assert (num_elements <= N)((void)(!(num_elements <= N) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/wide-int.h"
, 1510, __FUNCTION__), 0 : 0))
;
1511 return (num_elements * max_len - 1) * sizeof (HOST_WIDE_INTlong);
1512}
1513
1514/* This macro is used in structures that end with a trailing_wide_ints field
1515 called FIELD. It declares get_NAME() and set_NAME() methods to access
1516 element I of FIELD. */
1517#define TRAILING_WIDE_INT_ACCESSOR(NAME, FIELD, I)trailing_wide_int get_NAME () { return FIELD[I]; } template <
typename T> void set_NAME (const T &x) { FIELD[I] = x;
}
\
1518 trailing_wide_int get_##NAME () { return FIELD[I]; } \
1519 template <typename T> void set_##NAME (const T &x) { FIELD[I] = x; }
1520
1521namespace wi
1522{
1523 /* Implementation of int_traits for primitive integer types like "int". */
1524 template <typename T, bool signed_p>
1525 struct primitive_int_traits
1526 {
1527 static const enum precision_type precision_type = FLEXIBLE_PRECISION;
1528 static const bool host_dependent_precision = true;
1529 static const bool is_sign_extended = true;
1530 static unsigned int get_precision (T);
1531 static wi::storage_ref decompose (HOST_WIDE_INTlong *, unsigned int, T);
1532 };
1533}
1534
1535template <typename T, bool signed_p>
1536inline unsigned int
1537wi::primitive_int_traits <T, signed_p>::get_precision (T)
1538{
1539 return sizeof (T) * CHAR_BIT8;
1540}
1541
1542template <typename T, bool signed_p>
1543inline wi::storage_ref
1544wi::primitive_int_traits <T, signed_p>::decompose (HOST_WIDE_INTlong *scratch,
1545 unsigned int precision, T x)
1546{
1547 scratch[0] = x;
1548 if (signed_p || scratch[0] >= 0 || precision <= HOST_BITS_PER_WIDE_INT64)
1549 return wi::storage_ref (scratch, 1, precision);
1550 scratch[1] = 0;
1551 return wi::storage_ref (scratch, 2, precision);
1552}
1553
1554/* Allow primitive C types to be used in wi:: routines. */
1555namespace wi
1556{
1557 template <>
1558 struct int_traits <unsigned char>
1559 : public primitive_int_traits <unsigned char, false> {};
1560
1561 template <>
1562 struct int_traits <unsigned short>
1563 : public primitive_int_traits <unsigned short, false> {};
1564
1565 template <>
1566 struct int_traits <int>
1567 : public primitive_int_traits <int, true> {};
1568
1569 template <>
1570 struct int_traits <unsigned int>
1571 : public primitive_int_traits <unsigned int, false> {};
1572
1573 template <>
1574 struct int_traits <long>
1575 : public primitive_int_traits <long, true> {};
1576
1577 template <>
1578 struct int_traits <unsigned long>
1579 : public primitive_int_traits <unsigned long, false> {};
1580
1581#if defined HAVE_LONG_LONG1
1582 template <>
1583 struct int_traits <long long>
1584 : public primitive_int_traits <long long, true> {};
1585
1586 template <>
1587 struct int_traits <unsigned long long>
1588 : public primitive_int_traits <unsigned long long, false> {};
1589#endif
1590}
1591
1592namespace wi
1593{
1594 /* Stores HWI-sized integer VAL, treating it as having signedness SGN
1595 and precision PRECISION. */
1596 class hwi_with_prec
1597 {
1598 public:
1599 hwi_with_prec () {}
1600 hwi_with_prec (HOST_WIDE_INTlong, unsigned int, signop);
1601 HOST_WIDE_INTlong val;
1602 unsigned int precision;
1603 signop sgn;
1604 };
1605
1606 hwi_with_prec shwi (HOST_WIDE_INTlong, unsigned int);
1607 hwi_with_prec uhwi (unsigned HOST_WIDE_INTlong, unsigned int);
1608
1609 hwi_with_prec minus_one (unsigned int);
1610 hwi_with_prec zero (unsigned int);
1611 hwi_with_prec one (unsigned int);
1612 hwi_with_prec two (unsigned int);
1613}
1614
1615inline wi::hwi_with_prec::hwi_with_prec (HOST_WIDE_INTlong v, unsigned int p,
1616 signop s)
1617 : precision (p), sgn (s)
1618{
1619 if (precision < HOST_BITS_PER_WIDE_INT64)
1620 val = sext_hwi (v, precision);
1621 else
1622 val = v;
1623}
1624
1625/* Return a signed integer that has value VAL and precision PRECISION. */
1626inline wi::hwi_with_prec
1627wi::shwi (HOST_WIDE_INTlong val, unsigned int precision)
1628{
1629 return hwi_with_prec (val, precision, SIGNED);
1630}
1631
1632/* Return an unsigned integer that has value VAL and precision PRECISION. */
1633inline wi::hwi_with_prec
1634wi::uhwi (unsigned HOST_WIDE_INTlong val, unsigned int precision)
1635{
1636 return hwi_with_prec (val, precision, UNSIGNED);
1637}
1638
1639/* Return a wide int of -1 with precision PRECISION. */
1640inline wi::hwi_with_prec
1641wi::minus_one (unsigned int precision)
1642{
1643 return wi::shwi (-1, precision);
1644}
1645
1646/* Return a wide int of 0 with precision PRECISION. */
1647inline wi::hwi_with_prec
1648wi::zero (unsigned int precision)
1649{
1650 return wi::shwi (0, precision);
1651}
1652
1653/* Return a wide int of 1 with precision PRECISION. */
1654inline wi::hwi_with_prec
1655wi::one (unsigned int precision)
1656{
1657 return wi::shwi (1, precision);
1658}
1659
1660/* Return a wide int of 2 with precision PRECISION. */
1661inline wi::hwi_with_prec
1662wi::two (unsigned int precision)
1663{
1664 return wi::shwi (2, precision);
1665}
1666
1667namespace wi
1668{
1669 /* ints_for<T>::zero (X) returns a zero that, when asssigned to a T,
1670 gives that T the same precision as X. */
1671 template<typename T, precision_type = int_traits<T>::precision_type>
1672 struct ints_for
1673 {
1674 static int zero (const T &) { return 0; }
1675 };
1676
1677 template<typename T>
1678 struct ints_for<T, VAR_PRECISION>
1679 {
1680 static hwi_with_prec zero (const T &);
1681 };
1682}
1683
1684template<typename T>
1685inline wi::hwi_with_prec
1686wi::ints_for<T, wi::VAR_PRECISION>::zero (const T &x)
1687{
1688 return wi::zero (wi::get_precision (x));
1689}
1690
1691namespace wi
1692{
1693 template <>
1694 struct int_traits <wi::hwi_with_prec>
1695 {
1696 static const enum precision_type precision_type = VAR_PRECISION;
1697 /* hwi_with_prec has an explicitly-given precision, rather than the
1698 precision of HOST_WIDE_INT. */
1699 static const bool host_dependent_precision = false;
1700 static const bool is_sign_extended = true;
1701 static unsigned int get_precision (const wi::hwi_with_prec &);
1702 static wi::storage_ref decompose (HOST_WIDE_INTlong *, unsigned int,
1703 const wi::hwi_with_prec &);
1704 };
1705}
1706
1707inline unsigned int
1708wi::int_traits <wi::hwi_with_prec>::get_precision (const wi::hwi_with_prec &x)
1709{
1710 return x.precision;
1711}
1712
1713inline wi::storage_ref
1714wi::int_traits <wi::hwi_with_prec>::
1715decompose (HOST_WIDE_INTlong *scratch, unsigned int precision,
1716 const wi::hwi_with_prec &x)
1717{
1718 gcc_checking_assert (precision == x.precision)((void)(!(precision == x.precision) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/wide-int.h"
, 1718, __FUNCTION__), 0 : 0))
;
1719 scratch[0] = x.val;
1720 if (x.sgn == SIGNED || x.val >= 0 || precision <= HOST_BITS_PER_WIDE_INT64)
1721 return wi::storage_ref (scratch, 1, precision);
1722 scratch[1] = 0;
1723 return wi::storage_ref (scratch, 2, precision);
1724}
1725
1726/* Private functions for handling large cases out of line. They take
1727 individual length and array parameters because that is cheaper for
1728 the inline caller than constructing an object on the stack and
1729 passing a reference to it. (Although many callers use wide_int_refs,
1730 we generally want those to be removed by SRA.) */
1731namespace wi
1732{
1733 bool eq_p_large (const HOST_WIDE_INTlong *, unsigned int,
1734 const HOST_WIDE_INTlong *, unsigned int, unsigned int);
1735 bool lts_p_large (const HOST_WIDE_INTlong *, unsigned int, unsigned int,
1736 const HOST_WIDE_INTlong *, unsigned int);
1737 bool ltu_p_large (const HOST_WIDE_INTlong *, unsigned int, unsigned int,
1738 const HOST_WIDE_INTlong *, unsigned int);
1739 int cmps_large (const HOST_WIDE_INTlong *, unsigned int, unsigned int,
1740 const HOST_WIDE_INTlong *, unsigned int);
1741 int cmpu_large (const HOST_WIDE_INTlong *, unsigned int, unsigned int,
1742 const HOST_WIDE_INTlong *, unsigned int);
1743 unsigned int sext_large (HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *,
1744 unsigned int,
1745 unsigned int, unsigned int);
1746 unsigned int zext_large (HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *,
1747 unsigned int,
1748 unsigned int, unsigned int);
1749 unsigned int set_bit_large (HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *,
1750 unsigned int, unsigned int, unsigned int);
1751 unsigned int lshift_large (HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *,
1752 unsigned int, unsigned int, unsigned int);
1753 unsigned int lrshift_large (HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *,
1754 unsigned int, unsigned int, unsigned int,
1755 unsigned int);
1756 unsigned int arshift_large (HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *,
1757 unsigned int, unsigned int, unsigned int,
1758 unsigned int);
1759 unsigned int and_large (HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *, unsigned int,
1760 const HOST_WIDE_INTlong *, unsigned int, unsigned int);
1761 unsigned int and_not_large (HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *,
1762 unsigned int, const HOST_WIDE_INTlong *,
1763 unsigned int, unsigned int);
1764 unsigned int or_large (HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *, unsigned int,
1765 const HOST_WIDE_INTlong *, unsigned int, unsigned int);
1766 unsigned int or_not_large (HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *,
1767 unsigned int, const HOST_WIDE_INTlong *,
1768 unsigned int, unsigned int);
1769 unsigned int xor_large (HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *, unsigned int,
1770 const HOST_WIDE_INTlong *, unsigned int, unsigned int);
1771 unsigned int add_large (HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *, unsigned int,
1772 const HOST_WIDE_INTlong *, unsigned int, unsigned int,
1773 signop, overflow_type *);
1774 unsigned int sub_large (HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *, unsigned int,
1775 const HOST_WIDE_INTlong *, unsigned int, unsigned int,
1776 signop, overflow_type *);
1777 unsigned int mul_internal (HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *,
1778 unsigned int, const HOST_WIDE_INTlong *,
1779 unsigned int, unsigned int, signop,
1780 overflow_type *, bool);
1781 unsigned int divmod_internal (HOST_WIDE_INTlong *, unsigned int *,
1782 HOST_WIDE_INTlong *, const HOST_WIDE_INTlong *,
1783 unsigned int, unsigned int,
1784 const HOST_WIDE_INTlong *,
1785 unsigned int, unsigned int,
1786 signop, overflow_type *);
1787}
1788
1789/* Return the number of bits that integer X can hold. */
1790template <typename T>
1791inline unsigned int
1792wi::get_precision (const T &x)
1793{
1794 return wi::int_traits <T>::get_precision (x);
1795}
1796
1797/* Return the number of bits that the result of a binary operation can
1798 hold when the input operands are X and Y. */
1799template <typename T1, typename T2>
1800inline unsigned int
1801wi::get_binary_precision (const T1 &x, const T2 &y)
1802{
1803 return get_precision (wi::int_traits <WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type>::
1804 get_binary_result (x, y));
1805}
1806
1807/* Copy the contents of Y to X, but keeping X's current precision. */
1808template <typename T1, typename T2>
1809inline void
1810wi::copy (T1 &x, const T2 &y)
1811{
1812 HOST_WIDE_INTlong *xval = x.write_val ();
1813 const HOST_WIDE_INTlong *yval = y.get_val ();
1814 unsigned int len = y.get_len ();
1815 unsigned int i = 0;
1816 do
1817 xval[i] = yval[i];
1818 while (++i < len);
1819 x.set_len (len, y.is_sign_extended);
1820}
1821
1822/* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
1823template <typename T>
1824inline bool
1825wi::fits_shwi_p (const T &x)
1826{
1827 WIDE_INT_REF_FOR (T)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T>::is_sign_extended, wi::int_traits <T>::host_dependent_precision
> >
xi (x);
1828 return xi.len == 1;
1829}
1830
1831/* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
1832 precision. */
1833template <typename T>
1834inline bool
1835wi::fits_uhwi_p (const T &x)
1836{
1837 WIDE_INT_REF_FOR (T)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T>::is_sign_extended, wi::int_traits <T>::host_dependent_precision
> >
xi (x);
1838 if (xi.precision <= HOST_BITS_PER_WIDE_INT64)
1839 return true;
1840 if (xi.len == 1)
1841 return xi.slow () >= 0;
1842 return xi.len == 2 && xi.uhigh () == 0;
1843}
1844
1845/* Return true if X is negative based on the interpretation of SGN.
1846 For UNSIGNED, this is always false. */
1847template <typename T>
1848inline bool
1849wi::neg_p (const T &x, signop sgn)
1850{
1851 WIDE_INT_REF_FOR (T)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T>::is_sign_extended, wi::int_traits <T>::host_dependent_precision
> >
xi (x);
1852 if (sgn == UNSIGNED)
1853 return false;
1854 return xi.sign_mask () < 0;
1855}
1856
1857/* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
1858template <typename T>
1859inline HOST_WIDE_INTlong
1860wi::sign_mask (const T &x)
1861{
1862 WIDE_INT_REF_FOR (T)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T>::is_sign_extended, wi::int_traits <T>::host_dependent_precision
> >
xi (x);
1863 return xi.sign_mask ();
1864}
1865
1866/* Return true if X == Y. X and Y must be binary-compatible. */
1867template <typename T1, typename T2>
1868inline bool
1869wi::eq_p (const T1 &x, const T2 &y)
1870{
1871 unsigned int precision = get_binary_precision (x, y);
1872 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
1873 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
1874 if (xi.is_sign_extended && yi.is_sign_extended)
1875 {
1876 /* This case reduces to array equality. */
1877 if (xi.len != yi.len)
1878 return false;
1879 unsigned int i = 0;
1880 do
1881 if (xi.val[i] != yi.val[i])
1882 return false;
1883 while (++i != xi.len);
1884 return true;
1885 }
1886 if (LIKELY (yi.len == 1)(__builtin_expect ((yi.len == 1), 1)))
1887 {
1888 /* XI is only equal to YI if it too has a single HWI. */
1889 if (xi.len != 1)
1890 return false;
1891 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
1892 with 0 are simple. */
1893 if (STATIC_CONSTANT_P (yi.val[0] == 0)(__builtin_constant_p (yi.val[0] == 0) && (yi.val[0] ==
0))
)
1894 return xi.val[0] == 0;
1895 /* Otherwise flush out any excess bits first. */
1896 unsigned HOST_WIDE_INTlong diff = xi.val[0] ^ yi.val[0];
1897 int excess = HOST_BITS_PER_WIDE_INT64 - precision;
1898 if (excess > 0)
1899 diff <<= excess;
1900 return diff == 0;
1901 }
1902 return eq_p_large (xi.val, xi.len, yi.val, yi.len, precision);
1903}
1904
1905/* Return true if X != Y. X and Y must be binary-compatible. */
1906template <typename T1, typename T2>
1907inline bool
1908wi::ne_p (const T1 &x, const T2 &y)
1909{
1910 return !eq_p (x, y);
1911}
1912
1913/* Return true if X < Y when both are treated as signed values. */
1914template <typename T1, typename T2>
1915inline bool
1916wi::lts_p (const T1 &x, const T2 &y)
1917{
1918 unsigned int precision = get_binary_precision (x, y);
1919 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
1920 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
1921 /* We optimize x < y, where y is 64 or fewer bits. */
1922 if (wi::fits_shwi_p (yi))
1923 {
1924 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
1925 if (STATIC_CONSTANT_P (yi.val[0] == 0)(__builtin_constant_p (yi.val[0] == 0) && (yi.val[0] ==
0))
)
1926 return neg_p (xi);
1927 /* If x fits directly into a shwi, we can compare directly. */
1928 if (wi::fits_shwi_p (xi))
1929 return xi.to_shwi () < yi.to_shwi ();
1930 /* If x doesn't fit and is negative, then it must be more
1931 negative than any value in y, and hence smaller than y. */
1932 if (neg_p (xi))
1933 return true;
1934 /* If x is positive, then it must be larger than any value in y,
1935 and hence greater than y. */
1936 return false;
1937 }
1938 /* Optimize the opposite case, if it can be detected at compile time. */
1939 if (STATIC_CONSTANT_P (xi.len == 1)(__builtin_constant_p (xi.len == 1) && (xi.len == 1)))
1940 /* If YI is negative it is lower than the least HWI.
1941 If YI is positive it is greater than the greatest HWI. */
1942 return !neg_p (yi);
1943 return lts_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1944}
1945
1946/* Return true if X < Y when both are treated as unsigned values. */
1947template <typename T1, typename T2>
1948inline bool
1949wi::ltu_p (const T1 &x, const T2 &y)
1950{
1951 unsigned int precision = get_binary_precision (x, y);
1952 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
1953 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
1954 /* Optimize comparisons with constants. */
1955 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0)(__builtin_constant_p (yi.len == 1 && yi.val[0] >=
0) && (yi.len == 1 && yi.val[0] >= 0))
)
1956 return xi.len == 1 && xi.to_uhwi () < (unsigned HOST_WIDE_INTlong) yi.val[0];
1957 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0)(__builtin_constant_p (xi.len == 1 && xi.val[0] >=
0) && (xi.len == 1 && xi.val[0] >= 0))
)
1958 return yi.len != 1 || yi.to_uhwi () > (unsigned HOST_WIDE_INTlong) xi.val[0];
1959 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1960 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1961 values does not change the result. */
1962 if (LIKELY (xi.len + yi.len == 2)(__builtin_expect ((xi.len + yi.len == 2), 1)))
1963 {
1964 unsigned HOST_WIDE_INTlong xl = xi.to_uhwi ();
1965 unsigned HOST_WIDE_INTlong yl = yi.to_uhwi ();
1966 return xl < yl;
1967 }
1968 return ltu_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1969}
1970
1971/* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
1972template <typename T1, typename T2>
1973inline bool
1974wi::lt_p (const T1 &x, const T2 &y, signop sgn)
1975{
1976 if (sgn == SIGNED)
1977 return lts_p (x, y);
1978 else
1979 return ltu_p (x, y);
1980}
1981
1982/* Return true if X <= Y when both are treated as signed values. */
1983template <typename T1, typename T2>
1984inline bool
1985wi::les_p (const T1 &x, const T2 &y)
1986{
1987 return !lts_p (y, x);
1988}
1989
1990/* Return true if X <= Y when both are treated as unsigned values. */
1991template <typename T1, typename T2>
1992inline bool
1993wi::leu_p (const T1 &x, const T2 &y)
1994{
1995 return !ltu_p (y, x);
1996}
1997
1998/* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
1999template <typename T1, typename T2>
2000inline bool
2001wi::le_p (const T1 &x, const T2 &y, signop sgn)
2002{
2003 if (sgn == SIGNED)
2004 return les_p (x, y);
2005 else
2006 return leu_p (x, y);
2007}
2008
2009/* Return true if X > Y when both are treated as signed values. */
2010template <typename T1, typename T2>
2011inline bool
2012wi::gts_p (const T1 &x, const T2 &y)
2013{
2014 return lts_p (y, x);
2015}
2016
2017/* Return true if X > Y when both are treated as unsigned values. */
2018template <typename T1, typename T2>
2019inline bool
2020wi::gtu_p (const T1 &x, const T2 &y)
2021{
2022 return ltu_p (y, x);
2023}
2024
2025/* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
2026template <typename T1, typename T2>
2027inline bool
2028wi::gt_p (const T1 &x, const T2 &y, signop sgn)
2029{
2030 if (sgn == SIGNED)
2031 return gts_p (x, y);
2032 else
2033 return gtu_p (x, y);
2034}
2035
2036/* Return true if X >= Y when both are treated as signed values. */
2037template <typename T1, typename T2>
2038inline bool
2039wi::ges_p (const T1 &x, const T2 &y)
2040{
2041 return !lts_p (x, y);
2042}
2043
2044/* Return true if X >= Y when both are treated as unsigned values. */
2045template <typename T1, typename T2>
2046inline bool
2047wi::geu_p (const T1 &x, const T2 &y)
2048{
2049 return !ltu_p (x, y);
2050}
2051
2052/* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
2053template <typename T1, typename T2>
2054inline bool
2055wi::ge_p (const T1 &x, const T2 &y, signop sgn)
2056{
2057 if (sgn == SIGNED)
2058 return ges_p (x, y);
2059 else
2060 return geu_p (x, y);
2061}
2062
2063/* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
2064 as signed values. */
2065template <typename T1, typename T2>
2066inline int
2067wi::cmps (const T1 &x, const T2 &y)
2068{
2069 unsigned int precision = get_binary_precision (x, y);
2070 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2071 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
2072 if (wi::fits_shwi_p (yi))
2073 {
2074 /* Special case for comparisons with 0. */
2075 if (STATIC_CONSTANT_P (yi.val[0] == 0)(__builtin_constant_p (yi.val[0] == 0) && (yi.val[0] ==
0))
)
2076 return neg_p (xi) ? -1 : !(xi.len == 1 && xi.val[0] == 0);
2077 /* If x fits into a signed HWI, we can compare directly. */
2078 if (wi::fits_shwi_p (xi))
2079 {
2080 HOST_WIDE_INTlong xl = xi.to_shwi ();
2081 HOST_WIDE_INTlong yl = yi.to_shwi ();
2082 return xl < yl ? -1 : xl > yl;
2083 }
2084 /* If x doesn't fit and is negative, then it must be more
2085 negative than any signed HWI, and hence smaller than y. */
2086 if (neg_p (xi))
2087 return -1;
2088 /* If x is positive, then it must be larger than any signed HWI,
2089 and hence greater than y. */
2090 return 1;
2091 }
2092 /* Optimize the opposite case, if it can be detected at compile time. */
2093 if (STATIC_CONSTANT_P (xi.len == 1)(__builtin_constant_p (xi.len == 1) && (xi.len == 1)))
2094 /* If YI is negative it is lower than the least HWI.
2095 If YI is positive it is greater than the greatest HWI. */
2096 return neg_p (yi) ? 1 : -1;
2097 return cmps_large (xi.val, xi.len, precision, yi.val, yi.len);
2098}
2099
2100/* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
2101 as unsigned values. */
2102template <typename T1, typename T2>
2103inline int
2104wi::cmpu (const T1 &x, const T2 &y)
2105{
2106 unsigned int precision = get_binary_precision (x, y);
2107 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2108 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
2109 /* Optimize comparisons with constants. */
2110 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0)(__builtin_constant_p (yi.len == 1 && yi.val[0] >=
0) && (yi.len == 1 && yi.val[0] >= 0))
)
2111 {
2112 /* If XI doesn't fit in a HWI then it must be larger than YI. */
2113 if (xi.len != 1)
2114 return 1;
2115 /* Otherwise compare directly. */
2116 unsigned HOST_WIDE_INTlong xl = xi.to_uhwi ();
2117 unsigned HOST_WIDE_INTlong yl = yi.val[0];
2118 return xl < yl ? -1 : xl > yl;
2119 }
2120 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0)(__builtin_constant_p (xi.len == 1 && xi.val[0] >=
0) && (xi.len == 1 && xi.val[0] >= 0))
)
2121 {
2122 /* If YI doesn't fit in a HWI then it must be larger than XI. */
2123 if (yi.len != 1)
2124 return -1;
2125 /* Otherwise compare directly. */
2126 unsigned HOST_WIDE_INTlong xl = xi.val[0];
2127 unsigned HOST_WIDE_INTlong yl = yi.to_uhwi ();
2128 return xl < yl ? -1 : xl > yl;
2129 }
2130 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
2131 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
2132 values does not change the result. */
2133 if (LIKELY (xi.len + yi.len == 2)(__builtin_expect ((xi.len + yi.len == 2), 1)))
2134 {
2135 unsigned HOST_WIDE_INTlong xl = xi.to_uhwi ();
2136 unsigned HOST_WIDE_INTlong yl = yi.to_uhwi ();
2137 return xl < yl ? -1 : xl > yl;
2138 }
2139 return cmpu_large (xi.val, xi.len, precision, yi.val, yi.len);
2140}
2141
2142/* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
2143 X and Y indicated by SGN. */
2144template <typename T1, typename T2>
2145inline int
2146wi::cmp (const T1 &x, const T2 &y, signop sgn)
2147{
2148 if (sgn == SIGNED)
2149 return cmps (x, y);
2150 else
2151 return cmpu (x, y);
2152}
2153
2154/* Return ~x. */
2155template <typename T>
2156inline WI_UNARY_RESULT (T)typename wi::binary_traits <T, T>::result_type
2157wi::bit_not (const T &x)
2158{
2159 WI_UNARY_RESULT_VAR (result, val, T, x)typename wi::binary_traits <T, T>::result_type result =
wi::int_traits <typename wi::binary_traits <T, T>::
result_type>::get_binary_result (x, x); long *val = result
.write_val ()
;
2160 WIDE_INT_REF_FOR (T)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T>::is_sign_extended, wi::int_traits <T>::host_dependent_precision
> >
xi (x, get_precision (result));
2161 for (unsigned int i = 0; i < xi.len; ++i)
2162 val[i] = ~xi.val[i];
2163 result.set_len (xi.len);
2164 return result;
2165}
2166
2167/* Return -x. */
2168template <typename T>
2169inline WI_UNARY_RESULT (T)typename wi::binary_traits <T, T>::result_type
2170wi::neg (const T &x)
2171{
2172 return sub (0, x);
2173}
2174
2175/* Return -x. Indicate in *OVERFLOW if performing the negation would
2176 cause an overflow. */
2177template <typename T>
2178inline WI_UNARY_RESULT (T)typename wi::binary_traits <T, T>::result_type
2179wi::neg (const T &x, overflow_type *overflow)
2180{
2181 *overflow = only_sign_bit_p (x) ? OVF_OVERFLOW : OVF_NONE;
2182 return sub (0, x);
2183}
2184
2185/* Return the absolute value of x. */
2186template <typename T>
2187inline WI_UNARY_RESULT (T)typename wi::binary_traits <T, T>::result_type
2188wi::abs (const T &x)
2189{
2190 return neg_p (x) ? neg (x) : WI_UNARY_RESULT (T)typename wi::binary_traits <T, T>::result_type (x);
2191}
2192
2193/* Return the result of sign-extending the low OFFSET bits of X. */
2194template <typename T>
2195inline WI_UNARY_RESULT (T)typename wi::binary_traits <T, T>::result_type
2196wi::sext (const T &x, unsigned int offset)
2197{
2198 WI_UNARY_RESULT_VAR (result, val, T, x)typename wi::binary_traits <T, T>::result_type result =
wi::int_traits <typename wi::binary_traits <T, T>::
result_type>::get_binary_result (x, x); long *val = result
.write_val ()
;
2199 unsigned int precision = get_precision (result);
2200 WIDE_INT_REF_FOR (T)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T>::is_sign_extended, wi::int_traits <T>::host_dependent_precision
> >
xi (x, precision);
2201
2202 if (offset <= HOST_BITS_PER_WIDE_INT64)
2203 {
2204 val[0] = sext_hwi (xi.ulow (), offset);
2205 result.set_len (1, true);
2206 }
2207 else
2208 result.set_len (sext_large (val, xi.val, xi.len, precision, offset));
2209 return result;
2210}
2211
2212/* Return the result of zero-extending the low OFFSET bits of X. */
2213template <typename T>
2214inline WI_UNARY_RESULT (T)typename wi::binary_traits <T, T>::result_type
2215wi::zext (const T &x, unsigned int offset)
2216{
2217 WI_UNARY_RESULT_VAR (result, val, T, x)typename wi::binary_traits <T, T>::result_type result =
wi::int_traits <typename wi::binary_traits <T, T>::
result_type>::get_binary_result (x, x); long *val = result
.write_val ()
;
2218 unsigned int precision = get_precision (result);
2219 WIDE_INT_REF_FOR (T)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T>::is_sign_extended, wi::int_traits <T>::host_dependent_precision
> >
xi (x, precision);
2220
2221 /* This is not just an optimization, it is actually required to
2222 maintain canonization. */
2223 if (offset >= precision)
2224 {
2225 wi::copy (result, xi);
2226 return result;
2227 }
2228
2229 /* In these cases we know that at least the top bit will be clear,
2230 so no sign extension is necessary. */
2231 if (offset < HOST_BITS_PER_WIDE_INT64)
2232 {
2233 val[0] = zext_hwi (xi.ulow (), offset);
2234 result.set_len (1, true);
2235 }
2236 else
2237 result.set_len (zext_large (val, xi.val, xi.len, precision, offset), true);
2238 return result;
2239}
2240
2241/* Return the result of extending the low OFFSET bits of X according to
2242 signedness SGN. */
2243template <typename T>
2244inline WI_UNARY_RESULT (T)typename wi::binary_traits <T, T>::result_type
2245wi::ext (const T &x, unsigned int offset, signop sgn)
2246{
2247 return sgn == SIGNED ? sext (x, offset) : zext (x, offset);
2248}
2249
2250/* Return an integer that represents X | (1 << bit). */
2251template <typename T>
2252inline WI_UNARY_RESULT (T)typename wi::binary_traits <T, T>::result_type
2253wi::set_bit (const T &x, unsigned int bit)
2254{
2255 WI_UNARY_RESULT_VAR (result, val, T, x)typename wi::binary_traits <T, T>::result_type result =
wi::int_traits <typename wi::binary_traits <T, T>::
result_type>::get_binary_result (x, x); long *val = result
.write_val ()
;
2256 unsigned int precision = get_precision (result);
2257 WIDE_INT_REF_FOR (T)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T>::is_sign_extended, wi::int_traits <T>::host_dependent_precision
> >
xi (x, precision);
2258 if (precision <= HOST_BITS_PER_WIDE_INT64)
2259 {
2260 val[0] = xi.ulow () | (HOST_WIDE_INT_1U1UL << bit);
2261 result.set_len (1);
2262 }
2263 else
2264 result.set_len (set_bit_large (val, xi.val, xi.len, precision, bit));
2265 return result;
2266}
2267
2268/* Return the mininum of X and Y, treating them both as having
2269 signedness SGN. */
2270template <typename T1, typename T2>
2271inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2272wi::min (const T1 &x, const T2 &y, signop sgn)
2273{
2274 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type result
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *val __attribute__
((__unused__)) = result.write_val ()
;
2275 unsigned int precision = get_precision (result);
2276 if (wi::le_p (x, y, sgn))
2277 wi::copy (result, WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
(x, precision));
2278 else
2279 wi::copy (result, WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
(y, precision));
2280 return result;
2281}
2282
2283/* Return the minimum of X and Y, treating both as signed values. */
2284template <typename T1, typename T2>
2285inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2286wi::smin (const T1 &x, const T2 &y)
2287{
2288 return wi::min (x, y, SIGNED);
2289}
2290
2291/* Return the minimum of X and Y, treating both as unsigned values. */
2292template <typename T1, typename T2>
2293inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2294wi::umin (const T1 &x, const T2 &y)
2295{
2296 return wi::min (x, y, UNSIGNED);
2297}
2298
2299/* Return the maxinum of X and Y, treating them both as having
2300 signedness SGN. */
2301template <typename T1, typename T2>
2302inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2303wi::max (const T1 &x, const T2 &y, signop sgn)
2304{
2305 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type result
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *val __attribute__
((__unused__)) = result.write_val ()
;
2306 unsigned int precision = get_precision (result);
2307 if (wi::ge_p (x, y, sgn))
2308 wi::copy (result, WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
(x, precision));
2309 else
2310 wi::copy (result, WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
(y, precision));
2311 return result;
2312}
2313
2314/* Return the maximum of X and Y, treating both as signed values. */
2315template <typename T1, typename T2>
2316inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2317wi::smax (const T1 &x, const T2 &y)
2318{
2319 return wi::max (x, y, SIGNED);
2320}
2321
2322/* Return the maximum of X and Y, treating both as unsigned values. */
2323template <typename T1, typename T2>
2324inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2325wi::umax (const T1 &x, const T2 &y)
2326{
2327 return wi::max (x, y, UNSIGNED);
2328}
2329
2330/* Return X & Y. */
2331template <typename T1, typename T2>
2332inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2333wi::bit_and (const T1 &x, const T2 &y)
2334{
2335 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type result
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *val = result
.write_val ()
;
2336 unsigned int precision = get_precision (result);
2337 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2338 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
2339 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2340 if (LIKELY (xi.len + yi.len == 2)(__builtin_expect ((xi.len + yi.len == 2), 1)))
2341 {
2342 val[0] = xi.ulow () & yi.ulow ();
2343 result.set_len (1, is_sign_extended);
2344 }
2345 else
2346 result.set_len (and_large (val, xi.val, xi.len, yi.val, yi.len,
2347 precision), is_sign_extended);
2348 return result;
2349}
2350
2351/* Return X & ~Y. */
2352template <typename T1, typename T2>
2353inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2354wi::bit_and_not (const T1 &x, const T2 &y)
2355{
2356 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type result
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *val = result
.write_val ()
;
2357 unsigned int precision = get_precision (result);
2358 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2359 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
2360 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2361 if (LIKELY (xi.len + yi.len == 2)(__builtin_expect ((xi.len + yi.len == 2), 1)))
2362 {
2363 val[0] = xi.ulow () & ~yi.ulow ();
2364 result.set_len (1, is_sign_extended);
2365 }
2366 else
2367 result.set_len (and_not_large (val, xi.val, xi.len, yi.val, yi.len,
2368 precision), is_sign_extended);
2369 return result;
2370}
2371
2372/* Return X | Y. */
2373template <typename T1, typename T2>
2374inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2375wi::bit_or (const T1 &x, const T2 &y)
2376{
2377 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type result
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *val = result
.write_val ()
;
2378 unsigned int precision = get_precision (result);
2379 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2380 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
2381 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2382 if (LIKELY (xi.len + yi.len == 2)(__builtin_expect ((xi.len + yi.len == 2), 1)))
2383 {
2384 val[0] = xi.ulow () | yi.ulow ();
2385 result.set_len (1, is_sign_extended);
2386 }
2387 else
2388 result.set_len (or_large (val, xi.val, xi.len,
2389 yi.val, yi.len, precision), is_sign_extended);
2390 return result;
2391}
2392
2393/* Return X | ~Y. */
2394template <typename T1, typename T2>
2395inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2396wi::bit_or_not (const T1 &x, const T2 &y)
2397{
2398 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type result
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *val = result
.write_val ()
;
2399 unsigned int precision = get_precision (result);
2400 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2401 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
2402 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2403 if (LIKELY (xi.len + yi.len == 2)(__builtin_expect ((xi.len + yi.len == 2), 1)))
2404 {
2405 val[0] = xi.ulow () | ~yi.ulow ();
2406 result.set_len (1, is_sign_extended);
2407 }
2408 else
2409 result.set_len (or_not_large (val, xi.val, xi.len, yi.val, yi.len,
2410 precision), is_sign_extended);
2411 return result;
2412}
2413
2414/* Return X ^ Y. */
2415template <typename T1, typename T2>
2416inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2417wi::bit_xor (const T1 &x, const T2 &y)
2418{
2419 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type result
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *val = result
.write_val ()
;
2420 unsigned int precision = get_precision (result);
2421 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2422 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
2423 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2424 if (LIKELY (xi.len + yi.len == 2)(__builtin_expect ((xi.len + yi.len == 2), 1)))
2425 {
2426 val[0] = xi.ulow () ^ yi.ulow ();
2427 result.set_len (1, is_sign_extended);
2428 }
2429 else
2430 result.set_len (xor_large (val, xi.val, xi.len,
2431 yi.val, yi.len, precision), is_sign_extended);
2432 return result;
2433}
2434
2435/* Return X + Y. */
2436template <typename T1, typename T2>
2437inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2438wi::add (const T1 &x, const T2 &y)
2439{
2440 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type result
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *val = result
.write_val ()
;
2441 unsigned int precision = get_precision (result);
2442 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2443 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
2444 if (precision <= HOST_BITS_PER_WIDE_INT64)
2445 {
2446 val[0] = xi.ulow () + yi.ulow ();
2447 result.set_len (1);
2448 }
2449 /* If the precision is known at compile time to be greater than
2450 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2451 knowing that (a) all bits in those HWIs are significant and
2452 (b) the result has room for at least two HWIs. This provides
2453 a fast path for things like offset_int and widest_int.
2454
2455 The STATIC_CONSTANT_P test prevents this path from being
2456 used for wide_ints. wide_ints with precisions greater than
2457 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2458 point handling them inline. */
2459 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)(__builtin_constant_p (precision > 64) && (precision
> 64))
2460 && LIKELY (xi.len + yi.len == 2)(__builtin_expect ((xi.len + yi.len == 2), 1)))
2461 {
2462 unsigned HOST_WIDE_INTlong xl = xi.ulow ();
2463 unsigned HOST_WIDE_INTlong yl = yi.ulow ();
2464 unsigned HOST_WIDE_INTlong resultl = xl + yl;
2465 val[0] = resultl;
2466 val[1] = (HOST_WIDE_INTlong) resultl < 0 ? 0 : -1;
2467 result.set_len (1 + (((resultl ^ xl) & (resultl ^ yl))
2468 >> (HOST_BITS_PER_WIDE_INT64 - 1)));
2469 }
2470 else
2471 result.set_len (add_large (val, xi.val, xi.len,
2472 yi.val, yi.len, precision,
2473 UNSIGNED, 0));
2474 return result;
2475}
2476
2477/* Return X + Y. Treat X and Y as having the signednes given by SGN
2478 and indicate in *OVERFLOW whether the operation overflowed. */
2479template <typename T1, typename T2>
2480inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2481wi::add (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2482{
2483 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type result
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *val = result
.write_val ()
;
2484 unsigned int precision = get_precision (result);
2485 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2486 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
2487 if (precision <= HOST_BITS_PER_WIDE_INT64)
2488 {
2489 unsigned HOST_WIDE_INTlong xl = xi.ulow ();
2490 unsigned HOST_WIDE_INTlong yl = yi.ulow ();
2491 unsigned HOST_WIDE_INTlong resultl = xl + yl;
2492 if (sgn == SIGNED)
2493 {
2494 if ((((resultl ^ xl) & (resultl ^ yl))
2495 >> (precision - 1)) & 1)
2496 {
2497 if (xl > resultl)
2498 *overflow = OVF_UNDERFLOW;
2499 else if (xl < resultl)
2500 *overflow = OVF_OVERFLOW;
2501 else
2502 *overflow = OVF_NONE;
2503 }
2504 else
2505 *overflow = OVF_NONE;
2506 }
2507 else
2508 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT64 - precision))
2509 < (xl << (HOST_BITS_PER_WIDE_INT64 - precision)))
2510 ? OVF_OVERFLOW : OVF_NONE;
2511 val[0] = resultl;
2512 result.set_len (1);
2513 }
2514 else
2515 result.set_len (add_large (val, xi.val, xi.len,
2516 yi.val, yi.len, precision,
2517 sgn, overflow));
2518 return result;
2519}
2520
2521/* Return X - Y. */
2522template <typename T1, typename T2>
2523inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2524wi::sub (const T1 &x, const T2 &y)
2525{
2526 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type result
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *val = result
.write_val ()
;
2527 unsigned int precision = get_precision (result);
2528 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2529 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
2530 if (precision <= HOST_BITS_PER_WIDE_INT64)
2531 {
2532 val[0] = xi.ulow () - yi.ulow ();
2533 result.set_len (1);
2534 }
2535 /* If the precision is known at compile time to be greater than
2536 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2537 knowing that (a) all bits in those HWIs are significant and
2538 (b) the result has room for at least two HWIs. This provides
2539 a fast path for things like offset_int and widest_int.
2540
2541 The STATIC_CONSTANT_P test prevents this path from being
2542 used for wide_ints. wide_ints with precisions greater than
2543 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2544 point handling them inline. */
2545 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)(__builtin_constant_p (precision > 64) && (precision
> 64))
2546 && LIKELY (xi.len + yi.len == 2)(__builtin_expect ((xi.len + yi.len == 2), 1)))
2547 {
2548 unsigned HOST_WIDE_INTlong xl = xi.ulow ();
2549 unsigned HOST_WIDE_INTlong yl = yi.ulow ();
2550 unsigned HOST_WIDE_INTlong resultl = xl - yl;
2551 val[0] = resultl;
2552 val[1] = (HOST_WIDE_INTlong) resultl < 0 ? 0 : -1;
2553 result.set_len (1 + (((resultl ^ xl) & (xl ^ yl))
2554 >> (HOST_BITS_PER_WIDE_INT64 - 1)));
2555 }
2556 else
2557 result.set_len (sub_large (val, xi.val, xi.len,
2558 yi.val, yi.len, precision,
2559 UNSIGNED, 0));
2560 return result;
2561}
2562
2563/* Return X - Y. Treat X and Y as having the signednes given by SGN
2564 and indicate in *OVERFLOW whether the operation overflowed. */
2565template <typename T1, typename T2>
2566inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2567wi::sub (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2568{
2569 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type result
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *val = result
.write_val ()
;
2570 unsigned int precision = get_precision (result);
2571 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2572 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
2573 if (precision <= HOST_BITS_PER_WIDE_INT64)
2574 {
2575 unsigned HOST_WIDE_INTlong xl = xi.ulow ();
2576 unsigned HOST_WIDE_INTlong yl = yi.ulow ();
2577 unsigned HOST_WIDE_INTlong resultl = xl - yl;
2578 if (sgn == SIGNED)
2579 {
2580 if ((((xl ^ yl) & (resultl ^ xl)) >> (precision - 1)) & 1)
2581 {
2582 if (xl > yl)
2583 *overflow = OVF_UNDERFLOW;
2584 else if (xl < yl)
2585 *overflow = OVF_OVERFLOW;
2586 else
2587 *overflow = OVF_NONE;
2588 }
2589 else
2590 *overflow = OVF_NONE;
2591 }
2592 else
2593 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT64 - precision))
2594 > (xl << (HOST_BITS_PER_WIDE_INT64 - precision)))
2595 ? OVF_UNDERFLOW : OVF_NONE;
2596 val[0] = resultl;
2597 result.set_len (1);
2598 }
2599 else
2600 result.set_len (sub_large (val, xi.val, xi.len,
2601 yi.val, yi.len, precision,
2602 sgn, overflow));
2603 return result;
2604}
2605
2606/* Return X * Y. */
2607template <typename T1, typename T2>
2608inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2609wi::mul (const T1 &x, const T2 &y)
2610{
2611 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type result
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *val = result
.write_val ()
;
2612 unsigned int precision = get_precision (result);
2613 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2614 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
2615 if (precision <= HOST_BITS_PER_WIDE_INT64)
2616 {
2617 val[0] = xi.ulow () * yi.ulow ();
2618 result.set_len (1);
2619 }
2620 else
2621 result.set_len (mul_internal (val, xi.val, xi.len, yi.val, yi.len,
2622 precision, UNSIGNED, 0, false));
2623 return result;
2624}
2625
2626/* Return X * Y. Treat X and Y as having the signednes given by SGN
2627 and indicate in *OVERFLOW whether the operation overflowed. */
2628template <typename T1, typename T2>
2629inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2630wi::mul (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2631{
2632 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type result
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *val = result
.write_val ()
;
2633 unsigned int precision = get_precision (result);
2634 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2635 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
2636 result.set_len (mul_internal (val, xi.val, xi.len,
2637 yi.val, yi.len, precision,
2638 sgn, overflow, false));
2639 return result;
2640}
2641
2642/* Return X * Y, treating both X and Y as signed values. Indicate in
2643 *OVERFLOW whether the operation overflowed. */
2644template <typename T1, typename T2>
2645inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2646wi::smul (const T1 &x, const T2 &y, overflow_type *overflow)
2647{
2648 return mul (x, y, SIGNED, overflow);
2649}
2650
2651/* Return X * Y, treating both X and Y as unsigned values. Indicate in
2652 *OVERFLOW if the result overflows. */
2653template <typename T1, typename T2>
2654inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2655wi::umul (const T1 &x, const T2 &y, overflow_type *overflow)
2656{
2657 return mul (x, y, UNSIGNED, overflow);
2658}
2659
2660/* Perform a widening multiplication of X and Y, extending the values
2661 according to SGN, and return the high part of the result. */
2662template <typename T1, typename T2>
2663inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2664wi::mul_high (const T1 &x, const T2 &y, signop sgn)
2665{
2666 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type result
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *val = result
.write_val ()
;
2667 unsigned int precision = get_precision (result);
2668 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2669 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y, precision);
2670 result.set_len (mul_internal (val, xi.val, xi.len,
2671 yi.val, yi.len, precision,
2672 sgn, 0, true));
2673 return result;
2674}
2675
2676/* Return X / Y, rouding towards 0. Treat X and Y as having the
2677 signedness given by SGN. Indicate in *OVERFLOW if the result
2678 overflows. */
2679template <typename T1, typename T2>
2680inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2681wi::div_trunc (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2682{
2683 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type quotient
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *quotient_val
= quotient.write_val ()
;
2684 unsigned int precision = get_precision (quotient);
2685 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2686 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y);
2687
2688 quotient.set_len (divmod_internal (quotient_val, 0, 0, xi.val, xi.len,
2689 precision,
2690 yi.val, yi.len, yi.precision,
2691 sgn, overflow));
2692 return quotient;
2693}
2694
2695/* Return X / Y, rouding towards 0. Treat X and Y as signed values. */
2696template <typename T1, typename T2>
2697inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2698wi::sdiv_trunc (const T1 &x, const T2 &y)
2699{
2700 return div_trunc (x, y, SIGNED);
2701}
2702
2703/* Return X / Y, rouding towards 0. Treat X and Y as unsigned values. */
2704template <typename T1, typename T2>
2705inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2706wi::udiv_trunc (const T1 &x, const T2 &y)
2707{
2708 return div_trunc (x, y, UNSIGNED);
2709}
2710
2711/* Return X / Y, rouding towards -inf. Treat X and Y as having the
2712 signedness given by SGN. Indicate in *OVERFLOW if the result
2713 overflows. */
2714template <typename T1, typename T2>
2715inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2716wi::div_floor (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2717{
2718 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type quotient
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *quotient_val
= quotient.write_val ()
;
2719 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type remainder
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *remainder_val
= remainder.write_val ()
;
2720 unsigned int precision = get_precision (quotient);
2721 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2722 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y);
2723
2724 unsigned int remainder_len;
2725 quotient.set_len (divmod_internal (quotient_val,
2726 &remainder_len, remainder_val,
2727 xi.val, xi.len, precision,
2728 yi.val, yi.len, yi.precision, sgn,
2729 overflow));
2730 remainder.set_len (remainder_len);
2731 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2732 return quotient - 1;
2733 return quotient;
2734}
2735
2736/* Return X / Y, rouding towards -inf. Treat X and Y as signed values. */
2737template <typename T1, typename T2>
2738inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2739wi::sdiv_floor (const T1 &x, const T2 &y)
2740{
2741 return div_floor (x, y, SIGNED);
2742}
2743
2744/* Return X / Y, rouding towards -inf. Treat X and Y as unsigned values. */
2745/* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
2746template <typename T1, typename T2>
2747inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2748wi::udiv_floor (const T1 &x, const T2 &y)
2749{
2750 return div_floor (x, y, UNSIGNED);
2751}
2752
2753/* Return X / Y, rouding towards +inf. Treat X and Y as having the
2754 signedness given by SGN. Indicate in *OVERFLOW if the result
2755 overflows. */
2756template <typename T1, typename T2>
2757inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2758wi::div_ceil (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2759{
2760 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type quotient
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *quotient_val
= quotient.write_val ()
;
2761 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type remainder
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *remainder_val
= remainder.write_val ()
;
2762 unsigned int precision = get_precision (quotient);
2763 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2764 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y);
2765
2766 unsigned int remainder_len;
2767 quotient.set_len (divmod_internal (quotient_val,
2768 &remainder_len, remainder_val,
2769 xi.val, xi.len, precision,
2770 yi.val, yi.len, yi.precision, sgn,
2771 overflow));
2772 remainder.set_len (remainder_len);
2773 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2774 return quotient + 1;
2775 return quotient;
2776}
2777
2778/* Return X / Y, rouding towards +inf. Treat X and Y as unsigned values. */
2779template <typename T1, typename T2>
2780inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2781wi::udiv_ceil (const T1 &x, const T2 &y)
2782{
2783 return div_ceil (x, y, UNSIGNED);
2784}
2785
2786/* Return X / Y, rouding towards nearest with ties away from zero.
2787 Treat X and Y as having the signedness given by SGN. Indicate
2788 in *OVERFLOW if the result overflows. */
2789template <typename T1, typename T2>
2790inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2791wi::div_round (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2792{
2793 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type quotient
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *quotient_val
= quotient.write_val ()
;
2794 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type remainder
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *remainder_val
= remainder.write_val ()
;
2795 unsigned int precision = get_precision (quotient);
2796 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2797 WIDE_INT_REF_FOR (T2)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T2>::is_sign_extended, wi::int_traits <T2>::host_dependent_precision
> >
yi (y);
2798
2799 unsigned int remainder_len;
2800 quotient.set_len (divmod_internal (quotient_val,
2801 &remainder_len, remainder_val,
2802 xi.val, xi.len, precision,
2803 yi.val, yi.len, yi.precision, sgn,
2804 overflow));
2805 remainder.set_len (remainder_len);
2806
2807 if (remainder != 0)
2808 {
2809 if (sgn == SIGNED)
2810 {
2811 WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type abs_remainder = wi::abs (remainder);
2812 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2813 {
2814 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2815 return quotient - 1;
2816 else
2817 return quotient + 1;
2818 }
2819 }
2820 else
2821 {
2822 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2823 return quotient + 1;
2824 }
2825 }
2826 return quotient;
2827}
2828
2829/* Return X / Y, rouding towards 0. Treat X and Y as having the
2830 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
2831template <typename T1, typename T2>
2832inline WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type
2833wi::divmod_trunc (const T1 &x, const T2 &y, signop sgn,
2834 WI_BINARY_RESULT (T1, T2)typename wi::binary_traits <T1, T2>::result_type *remainder_ptr)
2835{
2836 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type quotient
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *quotient_val
= quotient.write_val ()
;
2837 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y)typename wi::binary_traits <T1, T2>::result_type remainder
= wi::int_traits <typename wi::binary_traits <T1, T2>
::result_type>::get_binary_result (x, y); long *remainder_val
= remainder.write_val ()
;
2838 unsigned int precision = get_precision (quotient);
2839 WIDE_INT_REF_FOR (T1)generic_wide_int <wide_int_ref_storage <wi::int_traits <
T1>::is_sign_extended, wi::int_traits <T1>::host_dependent_precision
> >
xi (x, precision);
2840