GCC Middle and Back End API Reference
wide-int.h
Go to the documentation of this file.
1/* Operations with very long integers. -*- C++ -*-
2 Copyright (C) 2012-2026 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 targeted 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 For precisions up to WIDE_INT_MAX_INL_PRECISION, it uses an inline
57 buffer in the type, for larger precisions up to WIDEST_INT_MAX_PRECISION
58 it uses a pointer to heap allocated buffer.
59
60 2) offset_int. This is a fixed-precision integer that can hold
61 any address offset, measured in either bits or bytes, with at
62 least one extra sign bit. At the moment the maximum address
63 size GCC supports is 64 bits. With 8-bit bytes and an extra
64 sign bit, offset_int therefore needs to have at least 68 bits
65 of precision. We round this up to 128 bits for efficiency.
66 Values of type T are converted to this precision by sign- or
67 zero-extending them based on the signedness of T.
68
69 The extra sign bit means that offset_int is effectively a signed
70 128-bit integer, i.e. it behaves like int128_t.
71
72 Since the values are logically signed, there is no need to
73 distinguish between signed and unsigned operations. Sign-sensitive
74 comparison operators <, <=, > and >= are therefore supported.
75 Shift operators << and >> are also supported, with >> being
76 an _arithmetic_ right shift.
77
78 [ Note that, even though offset_int is effectively int128_t,
79 it can still be useful to use unsigned comparisons like
80 wi::leu_p (a, b) as a more efficient short-hand for
81 "a >= 0 && a <= b". ]
82
83 3) widest_int. This representation is an approximation of
84 infinite precision math. However, it is not really infinite
85 precision math as in the GMP library. It is really finite
86 precision math where the precision is WIDEST_INT_MAX_PRECISION.
87
88 Like offset_int, widest_int is wider than all the values that
89 it needs to represent, so the integers are logically signed.
90 Sign-sensitive comparison operators <, <=, > and >= are supported,
91 as are << and >>.
92
93 There are several places in the GCC where this should/must be used:
94
95 * Code that does induction variable optimizations. This code
96 works with induction variables of many different types at the
97 same time. Because of this, it ends up doing many different
98 calculations where the operands are not compatible types. The
99 widest_int makes this easy, because it provides a field where
100 nothing is lost when converting from any variable,
101
102 * There are a small number of passes that currently use the
103 widest_int that should use the default. These should be
104 changed.
105
106 There are surprising features of offset_int and widest_int
107 that the users should be careful about:
108
109 1) Shifts and rotations are just weird. You have to specify a
110 precision in which the shift or rotate is to happen in. The bits
111 above this precision are zeroed. While this is what you
112 want, it is clearly non obvious.
113
114 2) Larger precision math sometimes does not produce the same
115 answer as would be expected for doing the math at the proper
116 precision. In particular, a multiply followed by a divide will
117 produce a different answer if the first product is larger than
118 what can be represented in the input precision.
119
120 The offset_int and the widest_int flavors are more expensive
121 than the default wide int, so in addition to the caveats with these
122 two, the default is the preferred representation.
123
124 All three flavors of wide_int are represented as a vector of
125 HOST_WIDE_INTs. The default and widest_int vectors contain enough elements
126 to hold a value of MAX_BITSIZE_MODE_ANY_INT bits. offset_int contains only
127 enough elements to hold ADDR_MAX_PRECISION bits. The values are stored
128 in the vector with the least significant HOST_BITS_PER_WIDE_INT bits
129 in element 0.
130
131 The default wide_int contains three fields: the vector (VAL),
132 the precision and a length (LEN). The length is the number of HWIs
133 needed to represent the value. widest_int and offset_int have a
134 constant precision that cannot be changed, so they only store the
135 VAL and LEN fields.
136
137 Since most integers used in a compiler are small values, it is
138 generally profitable to use a representation of the value that is
139 as small as possible. LEN is used to indicate the number of
140 elements of the vector that are in use. The numbers are stored as
141 sign extended numbers as a means of compression. Leading
142 HOST_WIDE_INTs that contain strings of either -1 or 0 are removed
143 as long as they can be reconstructed from the top bit that is being
144 represented.
145
146 The precision and length of a wide_int are always greater than 0.
147 Any bits in a wide_int above the precision are sign-extended from the
148 most significant bit. For example, a 4-bit value 0x8 is represented as
149 VAL = { 0xf...fff8 }. However, as an optimization, we allow other integer
150 constants to be represented with undefined bits above the precision.
151 This allows INTEGER_CSTs to be pre-extended according to TYPE_SIGN,
152 so that the INTEGER_CST representation can be used both in TYPE_PRECISION
153 and in wider precisions.
154
155 There are constructors to create the various forms of wide_int from
156 trees, rtl and constants. For trees the options are:
157
158 tree t = ...;
159 wi::to_wide (t) // Treat T as a wide_int
160 wi::to_offset (t) // Treat T as an offset_int
161 wi::to_widest (t) // Treat T as a widest_int
162
163 All three are light-weight accessors that should have no overhead
164 in release builds. If it is useful for readability reasons to
165 store the result in a temporary variable, the preferred method is:
166
167 wi::tree_to_wide_ref twide = wi::to_wide (t);
168 wi::tree_to_offset_ref toffset = wi::to_offset (t);
169 wi::tree_to_widest_ref twidest = wi::to_widest (t);
170
171 To make an rtx into a wide_int, you have to pair it with a mode.
172 The canonical way to do this is with rtx_mode_t as in:
173
174 rtx r = ...
175 wide_int x = rtx_mode_t (r, mode);
176
177 Similarly, a wide_int can only be constructed from a host value if
178 the target precision is given explicitly, such as in:
179
180 wide_int x = wi::shwi (c, prec); // sign-extend C if necessary
181 wide_int y = wi::uhwi (c, prec); // zero-extend C if necessary
182
183 However, offset_int and widest_int have an inherent precision and so
184 can be initialized directly from a host value:
185
186 offset_int x = (int) c; // sign-extend C
187 widest_int x = (unsigned int) c; // zero-extend C
188
189 It is also possible to do arithmetic directly on rtx_mode_ts and
190 constants. For example:
191
192 wi::add (r1, r2); // add equal-sized rtx_mode_ts r1 and r2
193 wi::add (r1, 1); // add 1 to rtx_mode_t r1
194 wi::lshift (1, 100); // 1 << 100 as a widest_int
195
196 Many binary operations place restrictions on the combinations of inputs,
197 using the following rules:
198
199 - {rtx, wide_int} op {rtx, wide_int} -> wide_int
200 The inputs must be the same precision. The result is a wide_int
201 of the same precision
202
203 - {rtx, wide_int} op (un)signed HOST_WIDE_INT -> wide_int
204 (un)signed HOST_WIDE_INT op {rtx, wide_int} -> wide_int
205 The HOST_WIDE_INT is extended or truncated to the precision of
206 the other input. The result is a wide_int of the same precision
207 as that input.
208
209 - (un)signed HOST_WIDE_INT op (un)signed HOST_WIDE_INT -> widest_int
210 The inputs are extended to widest_int precision and produce a
211 widest_int result.
212
213 - offset_int op offset_int -> offset_int
214 offset_int op (un)signed HOST_WIDE_INT -> offset_int
215 (un)signed HOST_WIDE_INT op offset_int -> offset_int
216
217 - widest_int op widest_int -> widest_int
218 widest_int op (un)signed HOST_WIDE_INT -> widest_int
219 (un)signed HOST_WIDE_INT op widest_int -> widest_int
220
221 Other combinations like:
222
223 - widest_int op offset_int and
224 - wide_int op offset_int
225
226 are not allowed. The inputs should instead be extended or truncated
227 so that they match.
228
229 The inputs to comparison functions like wi::eq_p and wi::lts_p
230 follow the same compatibility rules, although their return types
231 are different. Unary functions on X produce the same result as
232 a binary operation X + X. Shift functions X op Y also produce
233 the same result as X + X; the precision of the shift amount Y
234 can be arbitrarily different from X. */
235
236/* The MAX_BITSIZE_MODE_ANY_INT is automatically generated by a very
237 early examination of the target's mode file. The WIDE_INT_MAX_INL_ELTS
238 can accommodate at least 1 more bit so that unsigned numbers of that
239 mode can be represented as a signed value. Note that it is still
240 possible to create fixed_wide_ints that have precisions greater than
241 MAX_BITSIZE_MODE_ANY_INT. This can be useful when representing a
242 double-width multiplication result, for example. */
243#define WIDE_INT_MAX_INL_ELTS \
244 ((MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT) \
245 / HOST_BITS_PER_WIDE_INT)
246
247#define WIDE_INT_MAX_INL_PRECISION \
248 (WIDE_INT_MAX_INL_ELTS * HOST_BITS_PER_WIDE_INT)
249
250/* Precision of wide_int and largest _BitInt precision + 1 we can
251 support. */
252#define WIDE_INT_MAX_ELTS 1024
253#define WIDE_INT_MAX_PRECISION (WIDE_INT_MAX_ELTS * HOST_BITS_PER_WIDE_INT)
254
255/* Precision of widest_int. */
256#define WIDEST_INT_MAX_ELTS 2048
257#define WIDEST_INT_MAX_PRECISION (WIDEST_INT_MAX_ELTS * HOST_BITS_PER_WIDE_INT)
258
260
261/* This is the max size of any pointer on any machine. It does not
262 seem to be as easy to sniff this out of the machine description as
263 it is for MAX_BITSIZE_MODE_ANY_INT since targets may support
264 multiple address sizes and may have different address sizes for
265 different address spaces. However, currently the largest pointer
266 on any platform is 64 bits. When that changes, then it is likely
267 that a target hook should be defined so that targets can make this
268 value larger for those targets. */
269#define ADDR_MAX_BITSIZE 64
270
271/* This is the internal precision used when doing any address
272 arithmetic. The '4' is really 3 + 1. Three of the bits are for
273 the number of extra bits needed to do bit addresses and the other bit
274 is to allow everything to be signed without loosing any precision.
275 Then everything is rounded up to the next HWI for efficiency. */
276#define ADDR_MAX_PRECISION \
277 ((ADDR_MAX_BITSIZE + 4 + HOST_BITS_PER_WIDE_INT - 1) \
278 & ~(HOST_BITS_PER_WIDE_INT - 1))
279
280/* The number of HWIs needed to store an offset_int. */
281#define OFFSET_INT_ELTS (ADDR_MAX_PRECISION / HOST_BITS_PER_WIDE_INT)
282
283/* The max number of HWIs needed to store a wide_int of PRECISION. */
284#define WIDE_INT_MAX_HWIS(PRECISION) \
285 ((PRECISION + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT)
286
287/* The type of result produced by a binary operation on types T1 and T2.
288 Defined purely for brevity. */
289#define WI_BINARY_RESULT(T1, T2) \
290 typename wi::binary_traits <T1, T2>::result_type
291
292/* Likewise for binary operators, which excludes the case in which neither
293 T1 nor T2 is a wide-int-based type. */
294#define WI_BINARY_OPERATOR_RESULT(T1, T2) \
295 typename wi::binary_traits <T1, T2>::operator_result
296
297/* The type of result produced by T1 << T2. Leads to substitution failure
298 if the operation isn't supported. Defined purely for brevity. */
299#define WI_SIGNED_SHIFT_RESULT(T1, T2) \
300 typename wi::binary_traits <T1, T2>::signed_shift_result_type
301
302/* The type of result produced by a sign-agnostic binary predicate on
303 types T1 and T2. This is bool if wide-int operations make sense for
304 T1 and T2 and leads to substitution failure otherwise. */
305#define WI_BINARY_PREDICATE_RESULT(T1, T2) \
306 typename wi::binary_traits <T1, T2>::predicate_result
307
308/* The type of result produced by a signed binary predicate on types T1 and T2.
309 This is bool if signed comparisons make sense for T1 and T2 and leads to
310 substitution failure otherwise. */
311#define WI_SIGNED_BINARY_PREDICATE_RESULT(T1, T2) \
312 typename wi::binary_traits <T1, T2>::signed_predicate_result
313
314/* The type of result produced by a unary operation on type T. */
315#define WI_UNARY_RESULT(T) \
316 typename wi::binary_traits <T, T>::result_type
317
318/* Define a variable RESULT to hold the result of a binary operation on
319 X and Y, which have types T1 and T2 respectively. Define VAL to
320 point to the blocks of RESULT. Once the user of the macro has
321 filled in VAL, it should call RESULT.set_len to set the number
322 of initialized blocks. */
323#define WI_BINARY_RESULT_VAR(RESULT, VAL, T1, X, T2, Y) \
324 WI_BINARY_RESULT (T1, T2) RESULT = \
325 wi::int_traits <WI_BINARY_RESULT (T1, T2)>::get_binary_result (X, Y); \
326 HOST_WIDE_INT *VAL = RESULT.write_val (0)
327
328/* Similar for the result of a unary operation on X, which has type T. */
329#define WI_UNARY_RESULT_VAR(RESULT, VAL, T, X) \
330 WI_UNARY_RESULT (T) RESULT = \
331 wi::int_traits <WI_UNARY_RESULT (T)>::get_binary_result (X, X); \
332 HOST_WIDE_INT *VAL = RESULT.write_val (0)
333
334template <typename T> class generic_wide_int;
335template <int N> class fixed_wide_int_storage;
336class wide_int_storage;
337template <int N> class widest_int_storage;
338
339/* An N-bit integer. Until we can use typedef templates, use this instead. */
340#define FIXED_WIDE_INT(N) \
341 generic_wide_int < fixed_wide_int_storage <N> >
342
343typedef generic_wide_int <wide_int_storage> wide_int;
345typedef generic_wide_int <widest_int_storage <WIDEST_INT_MAX_PRECISION> > widest_int;
346typedef generic_wide_int <widest_int_storage <WIDEST_INT_MAX_PRECISION * 2> > widest2_int;
347
348/* wi::storage_ref can be a reference to a primitive type,
349 so this is the conservatively-correct setting. */
350template <bool SE, bool HDP = true>
352
353typedef generic_wide_int <wide_int_ref_storage <false> > wide_int_ref;
354
355/* This can be used instead of wide_int_ref if the referenced value is
356 known to have type T. It carries across properties of T's representation,
357 such as whether excess upper bits in a HWI are defined, and can therefore
358 help avoid redundant work.
359
360 The macro could be replaced with a template typedef, once we're able
361 to use those. */
362#define WIDE_INT_REF_FOR(T) \
363 generic_wide_int \
364 <wide_int_ref_storage <wi::int_traits <T>::is_sign_extended, \
365 wi::int_traits <T>::host_dependent_precision> >
366
367namespace wi
368{
369 /* Operations that calculate overflow do so even for
370 TYPE_OVERFLOW_WRAPS types. For example, adding 1 to +MAX_INT in
371 an unsigned int is 0 and does not overflow in C/C++, but wi::add
372 will set the overflow argument in case it's needed for further
373 analysis.
374
375 For operations that require overflow, these are the different
376 types of overflow. */
381 /* There was an overflow, but we are unsure whether it was an
382 overflow or an underflow. */
384 };
385
386 /* Classifies an integer based on its precision. */
388 /* The integer has both a precision and defined signedness. This allows
389 the integer to be converted to any width, since we know whether to fill
390 any extra bits with zeros or signs. */
392
393 /* The integer has a variable precision but no defined signedness. */
395
396 /* The integer has a constant precision (known at GCC compile time),
397 is signed and all elements are in inline buffer. */
399
400 /* Like INL_CONST_PRECISION, but elements can be heap allocated for
401 larger lengths. */
403 };
404
405 /* This class, which has no default implementation, is expected to
406 provide the following members:
407
408 static const enum precision_type precision_type;
409 Classifies the type of T.
410
411 static const unsigned int precision;
412 Only defined if precision_type == INL_CONST_PRECISION or
413 precision_type == CONST_PRECISION. Specifies the
414 precision of all integers of type T.
415
416 static const bool host_dependent_precision;
417 True if the precision of T depends (or can depend) on the host.
418
419 static unsigned int get_precision (const T &x)
420 Return the number of bits in X.
421
422 static wi::storage_ref *decompose (HOST_WIDE_INT *scratch,
423 unsigned int precision, const T &x)
424 Decompose X as a PRECISION-bit integer, returning the associated
425 wi::storage_ref. SCRATCH is available as scratch space if needed.
426 The routine should assert that PRECISION is acceptable. */
427 template <typename T> struct int_traits;
428
429 /* This class provides a single type, result_type, which specifies the
430 type of integer produced by a binary operation whose inputs have
431 types T1 and T2. The definition should be symmetric. */
432 template <typename T1, typename T2,
433 enum precision_type P1 = int_traits <T1>::precision_type,
434 enum precision_type P2 = int_traits <T2>::precision_type>
436
437 /* Specify the result type for each supported combination of binary
438 inputs. Note that INL_CONST_PRECISION, CONST_PRECISION and
439 VAR_PRECISION cannot be mixed, in order to give stronger type
440 checking. When both inputs are INL_CONST_PRECISION or both are
441 CONST_PRECISION, they must have the same precision. */
442 template <typename T1, typename T2>
444 {
446 /* Don't define operators for this combination. */
447 };
448
449 template <typename T1, typename T2>
456
457 template <typename T1, typename T2>
459 {
460 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
461 so as not to confuse gengtype. */
463 <int_traits <T2>::precision> > result_type;
465 typedef bool predicate_result;
468 };
469
470 template <typename T1, typename T2>
480
481 template <typename T1, typename T2>
488
489 template <typename T1, typename T2>
491 {
492 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
493 so as not to confuse gengtype. */
495 <int_traits <T1>::precision> > result_type;
497 typedef bool predicate_result;
500 };
501
502 template <typename T1, typename T2>
512
513 template <typename T1, typename T2>
515 {
516 STATIC_ASSERT (int_traits <T1>::precision == int_traits <T2>::precision);
517 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
518 so as not to confuse gengtype. */
520 <int_traits <T1>::precision> > result_type;
522 typedef bool predicate_result;
525 };
526
527 template <typename T1, typename T2>
529 {
530 STATIC_ASSERT (int_traits <T1>::precision == int_traits <T2>::precision);
532 <int_traits <T1>::precision> > result_type;
534 typedef bool predicate_result;
537 };
538
539 template <typename T1, typename T2>
546}
547
548/* Public functions for querying and operating on integers. */
549namespace wi
550{
551 template <typename T>
552 unsigned int get_precision (const T &);
553
554 template <typename T1, typename T2>
555 unsigned int get_binary_precision (const T1 &, const T2 &);
556
557 template <typename T1, typename T2>
558 void copy (T1 &, const T2 &);
559
560#define UNARY_PREDICATE \
561 template <typename T> bool
562#define UNARY_FUNCTION \
563 template <typename T> WI_UNARY_RESULT (T)
564#define BINARY_PREDICATE \
565 template <typename T1, typename T2> bool
566#define BINARY_FUNCTION \
567 template <typename T1, typename T2> WI_BINARY_RESULT (T1, T2)
568#define SHIFT_FUNCTION \
569 template <typename T1, typename T2> WI_UNARY_RESULT (T1)
570
574
575 template <typename T>
576 HOST_WIDE_INT sign_mask (const T &);
577
578 BINARY_PREDICATE eq_p (const T1 &, const T2 &);
579 BINARY_PREDICATE ne_p (const T1 &, const T2 &);
580 BINARY_PREDICATE lt_p (const T1 &, const T2 &, signop);
581 BINARY_PREDICATE lts_p (const T1 &, const T2 &);
582 BINARY_PREDICATE ltu_p (const T1 &, const T2 &);
583 BINARY_PREDICATE le_p (const T1 &, const T2 &, signop);
584 BINARY_PREDICATE les_p (const T1 &, const T2 &);
585 BINARY_PREDICATE leu_p (const T1 &, const T2 &);
586 BINARY_PREDICATE gt_p (const T1 &, const T2 &, signop);
587 BINARY_PREDICATE gts_p (const T1 &, const T2 &);
588 BINARY_PREDICATE gtu_p (const T1 &, const T2 &);
589 BINARY_PREDICATE ge_p (const T1 &, const T2 &, signop);
590 BINARY_PREDICATE ges_p (const T1 &, const T2 &);
591 BINARY_PREDICATE geu_p (const T1 &, const T2 &);
592
593 template <typename T1, typename T2>
594 int cmp (const T1 &, const T2 &, signop);
595
596 template <typename T1, typename T2>
597 int cmps (const T1 &, const T2 &);
598
599 template <typename T1, typename T2>
600 int cmpu (const T1 &, const T2 &);
601
603 UNARY_FUNCTION neg (const T &);
605 UNARY_FUNCTION abs (const T &);
606 UNARY_FUNCTION ext (const T &, unsigned int, signop);
607 UNARY_FUNCTION sext (const T &, unsigned int);
608 UNARY_FUNCTION zext (const T &, unsigned int);
609 UNARY_FUNCTION set_bit (const T &, unsigned int);
612
613 BINARY_FUNCTION min (const T1 &, const T2 &, signop);
614 BINARY_FUNCTION smin (const T1 &, const T2 &);
615 BINARY_FUNCTION umin (const T1 &, const T2 &);
616 BINARY_FUNCTION max (const T1 &, const T2 &, signop);
617 BINARY_FUNCTION smax (const T1 &, const T2 &);
618 BINARY_FUNCTION umax (const T1 &, const T2 &);
619
620 BINARY_FUNCTION bit_and (const T1 &, const T2 &);
621 BINARY_FUNCTION bit_and_not (const T1 &, const T2 &);
622 BINARY_FUNCTION bit_or (const T1 &, const T2 &);
623 BINARY_FUNCTION bit_or_not (const T1 &, const T2 &);
624 BINARY_FUNCTION bit_xor (const T1 &, const T2 &);
625 BINARY_FUNCTION add (const T1 &, const T2 &);
626 BINARY_FUNCTION add (const T1 &, const T2 &, signop, overflow_type *);
627 BINARY_FUNCTION sub (const T1 &, const T2 &);
628 BINARY_FUNCTION sub (const T1 &, const T2 &, signop, overflow_type *);
629 BINARY_FUNCTION mul (const T1 &, const T2 &);
630 BINARY_FUNCTION mul (const T1 &, const T2 &, signop, overflow_type *);
631 BINARY_FUNCTION smul (const T1 &, const T2 &, overflow_type *);
632 BINARY_FUNCTION umul (const T1 &, const T2 &, overflow_type *);
633 BINARY_FUNCTION mul_high (const T1 &, const T2 &, signop);
634 BINARY_FUNCTION div_trunc (const T1 &, const T2 &, signop,
635 overflow_type * = 0);
636 BINARY_FUNCTION sdiv_trunc (const T1 &, const T2 &);
637 BINARY_FUNCTION udiv_trunc (const T1 &, const T2 &);
638 BINARY_FUNCTION div_floor (const T1 &, const T2 &, signop,
639 overflow_type * = 0);
640 BINARY_FUNCTION udiv_floor (const T1 &, const T2 &);
641 BINARY_FUNCTION sdiv_floor (const T1 &, const T2 &);
642 BINARY_FUNCTION div_ceil (const T1 &, const T2 &, signop,
643 overflow_type * = 0);
644 BINARY_FUNCTION udiv_ceil (const T1 &, const T2 &);
645 BINARY_FUNCTION div_round (const T1 &, const T2 &, signop,
646 overflow_type * = 0);
647 BINARY_FUNCTION divmod_trunc (const T1 &, const T2 &, signop,
648 WI_BINARY_RESULT (T1, T2) *);
649 BINARY_FUNCTION gcd (const T1 &, const T2 &, signop = UNSIGNED);
650 BINARY_FUNCTION mod_trunc (const T1 &, const T2 &, signop,
651 overflow_type * = 0);
652 BINARY_FUNCTION smod_trunc (const T1 &, const T2 &);
653 BINARY_FUNCTION umod_trunc (const T1 &, const T2 &);
654 BINARY_FUNCTION mod_floor (const T1 &, const T2 &, signop,
655 overflow_type * = 0);
656 BINARY_FUNCTION umod_floor (const T1 &, const T2 &);
657 BINARY_FUNCTION mod_ceil (const T1 &, const T2 &, signop,
658 overflow_type * = 0);
659 BINARY_FUNCTION mod_round (const T1 &, const T2 &, signop,
660 overflow_type * = 0);
661
662 template <typename T1, typename T2>
663 bool multiple_of_p (const T1 &, const T2 &, signop);
664
665 template <typename T1, typename T2>
666 bool multiple_of_p (const T1 &, const T2 &, signop,
667 WI_BINARY_RESULT (T1, T2) *);
668
669 SHIFT_FUNCTION lshift (const T1 &, const T2 &);
670 SHIFT_FUNCTION lrshift (const T1 &, const T2 &);
671 SHIFT_FUNCTION arshift (const T1 &, const T2 &);
672 SHIFT_FUNCTION rshift (const T1 &, const T2 &, signop sgn);
673 SHIFT_FUNCTION lrotate (const T1 &, const T2 &, unsigned int = 0);
674 SHIFT_FUNCTION rrotate (const T1 &, const T2 &, unsigned int = 0);
675
676#undef SHIFT_FUNCTION
677#undef BINARY_PREDICATE
678#undef BINARY_FUNCTION
679#undef UNARY_PREDICATE
680#undef UNARY_FUNCTION
681
682 bool only_sign_bit_p (const wide_int_ref &, unsigned int);
683 bool only_sign_bit_p (const wide_int_ref &);
684 int clz (const wide_int_ref &);
685 int clrsb (const wide_int_ref &);
686 int ctz (const wide_int_ref &);
687 int exact_log2 (const wide_int_ref &);
688 int floor_log2 (const wide_int_ref &);
689 int ffs (const wide_int_ref &);
690 int popcount (const wide_int_ref &);
691 int parity (const wide_int_ref &);
692
693 template <typename T>
694 unsigned HOST_WIDE_INT extract_uhwi (const T &, unsigned int, unsigned int);
695
696 template <typename T>
697 unsigned int min_precision (const T &, signop);
698
699 static inline void accumulate_overflow (overflow_type &, overflow_type);
700}
701
702namespace wi
703{
704 /* Contains the components of a decomposed integer for easy, direct
705 access. */
707 {
708 public:
710 storage_ref (const HOST_WIDE_INT *, unsigned int, unsigned int);
711
712 const HOST_WIDE_INT *val;
713 unsigned int len;
714 unsigned int precision;
715
716 /* Provide enough trappings for this class to act as storage for
717 generic_wide_int. */
718 unsigned int get_len () const;
719 unsigned int get_precision () const;
720 const HOST_WIDE_INT *get_val () const;
721 };
722}
723
724inline::wi::storage_ref::storage_ref (const HOST_WIDE_INT *val_in,
725 unsigned int len_in,
726 unsigned int precision_in)
727 : val (val_in), len (len_in), precision (precision_in)
728{
729}
730
731inline unsigned int
733{
734 return len;
735}
736
737inline unsigned int
739{
740 return precision;
741}
742
743inline const HOST_WIDE_INT *
745{
746 return val;
747}
748
749/* This class defines an integer type using the storage provided by the
750 template argument. The storage class must provide the following
751 functions:
752
753 unsigned int get_precision () const
754 Return the number of bits in the integer.
755
756 HOST_WIDE_INT *get_val () const
757 Return a pointer to the array of blocks that encodes the integer.
758
759 unsigned int get_len () const
760 Return the number of blocks in get_val (). If this is smaller
761 than the number of blocks implied by get_precision (), the
762 remaining blocks are sign extensions of block get_len () - 1.
763
764 Although not required by generic_wide_int itself, writable storage
765 classes can also provide the following functions:
766
767 HOST_WIDE_INT *write_val (unsigned int)
768 Get a modifiable version of get_val (). The argument should be
769 upper estimation for LEN (ignored by all storages but
770 widest_int_storage).
771
772 unsigned int set_len (unsigned int len)
773 Set the value returned by get_len () to LEN. */
774template <typename storage>
775class GTY(()) generic_wide_int : public storage
776{
777public:
779
780 template <typename T>
781 generic_wide_int (const T &);
782
783 template <typename T>
784 generic_wide_int (const T &, unsigned int);
785
786 /* Conversions. */
787 HOST_WIDE_INT to_shwi (unsigned int) const;
788 HOST_WIDE_INT to_shwi () const;
789 unsigned HOST_WIDE_INT to_uhwi (unsigned int) const;
790 unsigned HOST_WIDE_INT to_uhwi () const;
791 HOST_WIDE_INT to_short_addr () const;
792
793 /* Public accessors for the interior of a wide int. */
794 HOST_WIDE_INT sign_mask () const;
795 HOST_WIDE_INT elt (unsigned int) const;
796 HOST_WIDE_INT sext_elt (unsigned int) const;
797 unsigned HOST_WIDE_INT ulow () const;
798 unsigned HOST_WIDE_INT uhigh () const;
799 HOST_WIDE_INT slow () const;
800 HOST_WIDE_INT shigh () const;
801
802 template <typename T>
803 generic_wide_int &operator = (const T &);
804
805#define ASSIGNMENT_OPERATOR(OP, F) \
806 template <typename T> \
807 generic_wide_int &OP (const T &c) { return (*this = wi::F (*this, c)); }
808
809/* Restrict these to cases where the shift operator is defined. */
810#define SHIFT_ASSIGNMENT_OPERATOR(OP, OP2) \
811 template <typename T> \
812 generic_wide_int &OP (const T &c) { return (*this = *this OP2 c); }
813
814#define INCDEC_OPERATOR(OP, DELTA) \
815 generic_wide_int &OP () { *this += DELTA; return *this; }
816
817 ASSIGNMENT_OPERATOR (operator &=, bit_and)
818 ASSIGNMENT_OPERATOR (operator |=, bit_or)
819 ASSIGNMENT_OPERATOR (operator ^=, bit_xor)
820 ASSIGNMENT_OPERATOR (operator +=, add)
821 ASSIGNMENT_OPERATOR (operator -=, sub)
822 ASSIGNMENT_OPERATOR (operator *=, mul)
823 ASSIGNMENT_OPERATOR (operator <<=, lshift)
824 SHIFT_ASSIGNMENT_OPERATOR (operator >>=, >>)
825 INCDEC_OPERATOR (operator ++, 1)
826 INCDEC_OPERATOR (operator --, -1)
827
828#undef SHIFT_ASSIGNMENT_OPERATOR
829#undef ASSIGNMENT_OPERATOR
830#undef INCDEC_OPERATOR
831
832 /* Debugging functions. */
833 void dump () const;
834
835 static const bool is_sign_extended
836 = wi::int_traits <generic_wide_int <storage> >::is_sign_extended;
837 static const bool needs_write_val_arg
838 = wi::int_traits <generic_wide_int <storage> >::needs_write_val_arg;
839};
840
841template <typename storage>
843
844template <typename storage>
845template <typename T>
847 : storage (x)
848{
849}
850
851template <typename storage>
852template <typename T>
854 unsigned int precision)
855 : storage (x, precision)
856{
857}
858
859/* Return THIS as a signed HOST_WIDE_INT, sign-extending from PRECISION.
860 If THIS does not fit in PRECISION, the information is lost. */
861template <typename storage>
862inline HOST_WIDE_INT
863generic_wide_int <storage>::to_shwi (unsigned int precision) const
864{
865 if (precision < HOST_BITS_PER_WIDE_INT)
866 return sext_hwi (this->get_val ()[0], precision);
867 else
868 return this->get_val ()[0];
869}
870
871/* Return THIS as a signed HOST_WIDE_INT, in its natural precision. */
872template <typename storage>
873inline HOST_WIDE_INT
875{
877 return this->get_val ()[0];
878 else
879 return to_shwi (this->get_precision ());
880}
881
882/* Return THIS as an unsigned HOST_WIDE_INT, zero-extending from
883 PRECISION. If THIS does not fit in PRECISION, the information
884 is lost. */
885template <typename storage>
886inline unsigned HOST_WIDE_INT
887generic_wide_int <storage>::to_uhwi (unsigned int precision) const
888{
889 if (precision < HOST_BITS_PER_WIDE_INT)
890 return zext_hwi (this->get_val ()[0], precision);
891 else
892 return this->get_val ()[0];
893}
894
895/* Return THIS as an signed HOST_WIDE_INT, in its natural precision. */
896template <typename storage>
897inline unsigned HOST_WIDE_INT
899{
900 return to_uhwi (this->get_precision ());
901}
902
903/* TODO: The compiler is half converted from using HOST_WIDE_INT to
904 represent addresses to using offset_int to represent addresses.
905 We use to_short_addr at the interface from new code to old,
906 unconverted code. */
907template <typename storage>
908inline HOST_WIDE_INT
910{
911 return this->get_val ()[0];
912}
913
914/* Return the implicit value of blocks above get_len (). */
915template <typename storage>
916inline HOST_WIDE_INT
918{
919 unsigned int len = this->get_len ();
920 gcc_assert (len > 0);
921
922 unsigned HOST_WIDE_INT high = this->get_val ()[len - 1];
923 if (!is_sign_extended)
924 {
925 unsigned int precision = this->get_precision ();
926 int excess = len * HOST_BITS_PER_WIDE_INT - precision;
927 if (excess > 0)
928 high <<= excess;
929 }
930 return (HOST_WIDE_INT) (high) < 0 ? -1 : 0;
931}
932
933/* Return the signed value of the least-significant explicitly-encoded
934 block. */
935template <typename storage>
936inline HOST_WIDE_INT
938{
939 return this->get_val ()[0];
940}
941
942/* Return the signed value of the most-significant explicitly-encoded
943 block. */
944template <typename storage>
945inline HOST_WIDE_INT
947{
948 return this->get_val ()[this->get_len () - 1];
949}
950
951/* Return the unsigned value of the least-significant
952 explicitly-encoded block. */
953template <typename storage>
954inline unsigned HOST_WIDE_INT
956{
957 return this->get_val ()[0];
958}
959
960/* Return the unsigned value of the most-significant
961 explicitly-encoded block. */
962template <typename storage>
963inline unsigned HOST_WIDE_INT
965{
966 return this->get_val ()[this->get_len () - 1];
967}
968
969/* Return block I, which might be implicitly or explicit encoded. */
970template <typename storage>
971inline HOST_WIDE_INT
973{
974 if (i >= this->get_len ())
975 return sign_mask ();
976 else
977 return this->get_val ()[i];
978}
979
980/* Like elt, but sign-extend beyond the upper bit, instead of returning
981 the raw encoding. */
982template <typename storage>
983inline HOST_WIDE_INT
985{
986 HOST_WIDE_INT elt_i = elt (i);
987 if (!is_sign_extended)
988 {
989 unsigned int precision = this->get_precision ();
990 unsigned int lsb = i * HOST_BITS_PER_WIDE_INT;
991 if (precision - lsb < HOST_BITS_PER_WIDE_INT)
992 elt_i = sext_hwi (elt_i, precision - lsb);
993 }
994 return elt_i;
995}
996
997template <typename storage>
998template <typename T>
999inline generic_wide_int <storage> &
1000generic_wide_int <storage>::operator = (const T &x)
1001{
1002 storage::operator = (x);
1003 return *this;
1004}
1005
1006/* Dump the contents of the integer to stderr, for debugging. */
1007template <typename storage>
1008void
1010{
1011 unsigned int len = this->get_len ();
1012 const HOST_WIDE_INT *val = this->get_val ();
1013 unsigned int precision = this->get_precision ();
1014 fprintf (stderr, "[");
1015 if (len * HOST_BITS_PER_WIDE_INT < precision)
1016 fprintf (stderr, "...,");
1017 for (unsigned int i = 0; i < len - 1; ++i)
1018 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX ",", val[len - 1 - i]);
1019 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX "], precision = %d\n",
1020 val[0], precision);
1021}
1022
1023namespace wi
1024{
1025 template <typename storage>
1026 struct int_traits < generic_wide_int <storage> >
1027 : public wi::int_traits <storage>
1028 {
1029 static unsigned int get_precision (const generic_wide_int <storage> &);
1030 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
1031 const generic_wide_int <storage> &);
1032 };
1033}
1034
1035template <typename storage>
1036inline unsigned int
1037wi::int_traits < generic_wide_int <storage> >::
1038get_precision (const generic_wide_int <storage> &x)
1039{
1040 return x.get_precision ();
1041}
1042
1043template <typename storage>
1044inline wi::storage_ref
1045wi::int_traits < generic_wide_int <storage> >::
1046decompose (HOST_WIDE_INT *, unsigned int precision,
1047 const generic_wide_int <storage> &x)
1048{
1049 gcc_checking_assert (precision == x.get_precision ());
1050 return wi::storage_ref (x.get_val (), x.get_len (), precision);
1051}
1052
1053/* Provide the storage for a wide_int_ref. This acts like a read-only
1054 wide_int, with the optimization that VAL is normally a pointer to
1055 another integer's storage, so that no array copy is needed. */
1056template <bool SE, bool HDP>
1058{
1059private:
1060 /* Scratch space that can be used when decomposing the original integer.
1061 It must live as long as this object. */
1062 HOST_WIDE_INT scratch[2];
1063
1064public:
1066
1068
1069 template <typename T>
1070 wide_int_ref_storage (const T &);
1071
1072 template <typename T>
1073 wide_int_ref_storage (const T &, unsigned int);
1074};
1075
1076/* Create a reference from an existing reference. */
1077template <bool SE, bool HDP>
1078inline wide_int_ref_storage <SE, HDP>::
1079wide_int_ref_storage (const wi::storage_ref &x)
1080 : storage_ref (x)
1081{}
1082
1083/* Create a reference to integer X in its natural precision. Note
1084 that the natural precision is host-dependent for primitive
1085 types. */
1086template <bool SE, bool HDP>
1087template <typename T>
1089 : storage_ref (wi::int_traits <T>::decompose (scratch,
1090 wi::get_precision (x), x))
1091{
1092}
1093
1094/* Create a reference to integer X in precision PRECISION. */
1095template <bool SE, bool HDP>
1096template <typename T>
1097inline wide_int_ref_storage <SE, HDP>::
1098wide_int_ref_storage (const T &x, unsigned int precision)
1099 : storage_ref (wi::int_traits <T>::decompose (scratch, precision, x))
1100{
1101}
1102
1103namespace wi
1104{
1105 template <bool SE, bool HDP>
1107 {
1109 static const bool host_dependent_precision = HDP;
1110 static const bool is_sign_extended = SE;
1111 static const bool needs_write_val_arg = false;
1112 };
1113}
1114
1115namespace wi
1116{
1117 unsigned int force_to_size (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1118 unsigned int, unsigned int, unsigned int,
1119 signop sgn);
1120 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1121 unsigned int, unsigned int, bool = true);
1122}
1123
1124/* The storage used by wide_int. */
1126{
1127private:
1128 union
1129 {
1130 HOST_WIDE_INT val[WIDE_INT_MAX_INL_ELTS];
1131 HOST_WIDE_INT *valp;
1132 } GTY((skip)) u;
1133 unsigned int len;
1134 unsigned int precision;
1135
1136public:
1138 template <typename T>
1139 wide_int_storage (const T &);
1142
1143 /* The standard generic_wide_int storage methods. */
1144 unsigned int get_precision () const;
1145 const HOST_WIDE_INT *get_val () const;
1146 unsigned int get_len () const;
1147 HOST_WIDE_INT *write_val (unsigned int);
1148 void set_len (unsigned int, bool = false);
1149
1150 wide_int_storage &operator = (const wide_int_storage &);
1151 template <typename T>
1152 wide_int_storage &operator = (const T &);
1153
1154 static wide_int from (const wide_int_ref &, unsigned int, signop);
1155 static wide_int from_array (const HOST_WIDE_INT *, unsigned int,
1156 unsigned int, bool = true);
1157 static wide_int create (unsigned int);
1158};
1159
1160namespace wi
1161{
1162 template <>
1164 {
1166 /* Guaranteed by a static assert in the wide_int_storage constructor. */
1167 static const bool host_dependent_precision = false;
1168 static const bool is_sign_extended = true;
1169 static const bool needs_write_val_arg = false;
1170 template <typename T1, typename T2>
1171 static wide_int get_binary_result (const T1 &, const T2 &);
1172 template <typename T1, typename T2>
1173 static unsigned int get_binary_precision (const T1 &, const T2 &);
1174 };
1175}
1176
1178
1179/* Initialize the storage from integer X, in its natural precision.
1180 Note that we do not allow integers with host-dependent precision
1181 to become wide_ints; wide_ints must always be logically independent
1182 of the host. */
1183template <typename T>
1195
1197{
1198 memcpy (this, &x, sizeof (wide_int_storage));
1200 {
1201 u.valp = XNEWVEC (HOST_WIDE_INT, CEIL (precision, HOST_BITS_PER_WIDE_INT));
1202 memcpy (u.valp, x.u.valp, len * sizeof (HOST_WIDE_INT));
1203 }
1204}
1205
1207{
1209 XDELETEVEC (u.valp);
1210}
1211
1212inline wide_int_storage&
1214{
1216 {
1217 if (this == &x)
1218 return *this;
1219 XDELETEVEC (u.valp);
1220 }
1221 memcpy (this, &x, sizeof (wide_int_storage));
1223 {
1224 u.valp = XNEWVEC (HOST_WIDE_INT, CEIL (x.precision, HOST_BITS_PER_WIDE_INT));
1225 memcpy (u.valp, x.u.valp, len * sizeof (HOST_WIDE_INT));
1226 }
1227 return *this;
1228}
1229
1230template <typename T>
1231inline wide_int_storage&
1233{
1237 WIDE_INT_REF_FOR (T) xi (x);
1238 if (UNLIKELY (precision != xi.precision))
1239 {
1241 XDELETEVEC (u.valp);
1242 precision = xi.precision;
1244 u.valp = XNEWVEC (HOST_WIDE_INT,
1246 }
1247 wi::copy (*this, xi);
1248 return *this;
1249}
1250
1251inline unsigned int
1253{
1254 return precision;
1255}
1256
1257inline const HOST_WIDE_INT *
1259{
1260 return UNLIKELY (precision > WIDE_INT_MAX_INL_PRECISION) ? u.valp : u.val;
1261}
1262
1263inline unsigned int
1265{
1266 return len;
1267}
1268
1269inline HOST_WIDE_INT *
1271{
1272 return UNLIKELY (precision > WIDE_INT_MAX_INL_PRECISION) ? u.valp : u.val;
1273}
1274
1275inline void
1276wide_int_storage::set_len (unsigned int l, bool is_sign_extended)
1277{
1278 len = l;
1279 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > precision)
1280 {
1281 HOST_WIDE_INT &v = write_val (len)[len - 1];
1283 }
1284}
1285
1286/* Treat X as having signedness SGN and convert it to a PRECISION-bit
1287 number. */
1288inline wide_int
1290 signop sgn)
1291{
1293 result.set_len (wi::force_to_size (result.write_val (x.len), x.val, x.len,
1294 x.precision, precision, sgn));
1295 return result;
1296}
1297
1298/* Create a wide_int from the explicit block encoding given by VAL and
1299 LEN. PRECISION is the precision of the integer. NEED_CANON_P is
1300 true if the encoding may have redundant trailing blocks. */
1301inline wide_int
1302wide_int_storage::from_array (const HOST_WIDE_INT *val, unsigned int len,
1303 unsigned int precision, bool need_canon_p)
1304{
1306 result.set_len (wi::from_array (result.write_val (len), val, len, precision,
1307 need_canon_p));
1308 return result;
1309}
1310
1311/* Return an uninitialized wide_int with precision PRECISION. */
1312inline wide_int
1314{
1315 wide_int x;
1316 x.precision = precision;
1318 x.u.valp = XNEWVEC (HOST_WIDE_INT,
1320 return x;
1321}
1322
1323template <typename T1, typename T2>
1324inline wide_int
1326{
1327 /* This shouldn't be used for two flexible-precision inputs. */
1328 STATIC_ASSERT (wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION
1329 || wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION);
1330 if (wi::int_traits <T1>::precision_type == FLEXIBLE_PRECISION)
1332 else
1334}
1335
1336template <typename T1, typename T2>
1337inline unsigned int
1339 const T2 &y)
1340{
1341 /* This shouldn't be used for two flexible-precision inputs. */
1342 STATIC_ASSERT (wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION
1343 || wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION);
1344 if (wi::int_traits <T1>::precision_type == FLEXIBLE_PRECISION)
1345 return wi::get_precision (y);
1346 else
1347 return wi::get_precision (x);
1348}
1349
1350/* The storage used by FIXED_WIDE_INT (N). */
1351template <int N>
1353{
1354private:
1355 HOST_WIDE_INT val[WIDE_INT_MAX_HWIS (N)];
1356 unsigned int len;
1357
1358public:
1360 template <typename T>
1361 fixed_wide_int_storage (const T &);
1362
1363 /* The standard generic_wide_int storage methods. */
1364 unsigned int get_precision () const;
1365 const HOST_WIDE_INT *get_val () const;
1366 unsigned int get_len () const;
1367 HOST_WIDE_INT *write_val (unsigned int);
1368 void set_len (unsigned int, bool = false);
1369
1370 static FIXED_WIDE_INT (N) from (const wide_int_ref &, signop);
1371 static FIXED_WIDE_INT (N) from_array (const HOST_WIDE_INT *, unsigned int,
1372 bool = true);
1373};
1374
1375namespace wi
1376{
1377 template <int N>
1379 {
1381 static const bool host_dependent_precision = false;
1382 static const bool is_sign_extended = true;
1383 static const bool needs_write_val_arg = false;
1384 static const unsigned int precision = N;
1385 template <typename T1, typename T2>
1386 static FIXED_WIDE_INT (N) get_binary_result (const T1 &, const T2 &);
1387 template <typename T1, typename T2>
1388 static unsigned int get_binary_precision (const T1 &, const T2 &);
1389 };
1390}
1391
1392/* Initialize the storage from integer X, in precision N. */
1393template <int N>
1394template <typename T>
1396{
1397 /* Check for type compatibility. We don't want to initialize a
1398 fixed-width integer from something like a wide_int. */
1399 WI_BINARY_RESULT (T, FIXED_WIDE_INT (N)) *assertion ATTRIBUTE_UNUSED;
1400 wi::copy (*this, WIDE_INT_REF_FOR (T) (x, N));
1401}
1402
1403template <int N>
1404inline unsigned int
1406{
1407 return N;
1408}
1409
1410template <int N>
1411inline const HOST_WIDE_INT *
1413{
1414 return val;
1415}
1416
1417template <int N>
1418inline unsigned int
1420{
1421 return len;
1422}
1423
1424template <int N>
1425inline HOST_WIDE_INT *
1427{
1428 return val;
1429}
1430
1431template <int N>
1432inline void
1434{
1435 len = l;
1436 /* There are no excess bits in val[len - 1]. */
1438}
1439
1440/* Treat X as having signedness SGN and convert it to an N-bit number. */
1441template <int N>
1443fixed_wide_int_storage <N>::from (const wide_int_ref &x, signop sgn)
1444{
1445 FIXED_WIDE_INT (N) result;
1446 result.set_len (wi::force_to_size (result.write_val (x.len), x.val, x.len,
1447 x.precision, N, sgn));
1448 return result;
1449}
1450
1451/* Create a FIXED_WIDE_INT (N) from the explicit block encoding given by
1452 VAL and LEN. NEED_CANON_P is true if the encoding may have redundant
1453 trailing blocks. */
1454template <int N>
1455inline FIXED_WIDE_INT (N)
1456fixed_wide_int_storage <N>::from_array (const HOST_WIDE_INT *val,
1457 unsigned int len,
1458 bool need_canon_p)
1459{
1460 FIXED_WIDE_INT (N) result;
1461 result.set_len (wi::from_array (result.write_val (len), val, len,
1462 N, need_canon_p));
1463 return result;
1464}
1465
1466template <int N>
1467template <typename T1, typename T2>
1469wi::int_traits < fixed_wide_int_storage <N> >::
1470get_binary_result (const T1 &, const T2 &)
1471{
1472 return FIXED_WIDE_INT (N) ();
1473}
1474
1475template <int N>
1476template <typename T1, typename T2>
1477inline unsigned int
1478wi::int_traits < fixed_wide_int_storage <N> >::
1479get_binary_precision (const T1 &, const T2 &)
1480{
1481 return N;
1482}
1483
1484#define WIDEST_INT(N) generic_wide_int < widest_int_storage <N> >
1485
1486/* The storage used by widest_int. */
1487template <int N>
1489{
1490private:
1491 union
1492 {
1493 HOST_WIDE_INT val[WIDE_INT_MAX_INL_ELTS];
1494 HOST_WIDE_INT *valp;
1495 } GTY((skip)) u;
1496 unsigned int len;
1497
1498public:
1501 template <typename T>
1502 widest_int_storage (const T &);
1504 widest_int_storage &operator = (const widest_int_storage &);
1505 template <typename T>
1506 inline widest_int_storage& operator = (const T &);
1507
1508 /* The standard generic_wide_int storage methods. */
1509 unsigned int get_precision () const;
1510 const HOST_WIDE_INT *get_val () const;
1511 unsigned int get_len () const;
1512 HOST_WIDE_INT *write_val (unsigned int);
1513 void set_len (unsigned int, bool = false);
1514
1515 static WIDEST_INT (N) from (const wide_int_ref &, signop);
1516 static WIDEST_INT (N) from_array (const HOST_WIDE_INT *, unsigned int,
1517 bool = true);
1518};
1519
1520namespace wi
1521{
1522 template <int N>
1524 {
1526 static const bool host_dependent_precision = false;
1527 static const bool is_sign_extended = true;
1528 static const bool needs_write_val_arg = true;
1529 static const unsigned int precision = N;
1530 template <typename T1, typename T2>
1531 static WIDEST_INT (N) get_binary_result (const T1 &, const T2 &);
1532 template <typename T1, typename T2>
1533 static unsigned int get_binary_precision (const T1 &, const T2 &);
1534 };
1535}
1536
1537template <int N>
1539
1540/* Initialize the storage from integer X, in precision N. */
1541template <int N>
1542template <typename T>
1544{
1545 /* Check for type compatibility. We don't want to initialize a
1546 widest integer from something like a wide_int. */
1547 WI_BINARY_RESULT (T, WIDEST_INT (N)) *assertion ATTRIBUTE_UNUSED;
1548 wi::copy (*this, WIDE_INT_REF_FOR (T) (x, N));
1549}
1550
1551template <int N>
1552inline
1554{
1555 memcpy (this, &x, sizeof (widest_int_storage));
1557 {
1558 u.valp = XNEWVEC (HOST_WIDE_INT, len);
1559 memcpy (u.valp, x.u.valp, len * sizeof (HOST_WIDE_INT));
1560 }
1561}
1562
1563template <int N>
1564inline widest_int_storage <N>::~widest_int_storage ()
1565{
1567 XDELETEVEC (u.valp);
1568}
1569
1570template <int N>
1571inline widest_int_storage <N>&
1572widest_int_storage <N>::operator = (const widest_int_storage &x)
1573{
1575 {
1576 if (this == &x)
1577 return *this;
1578 XDELETEVEC (u.valp);
1579 }
1580 memcpy (this, &x, sizeof (widest_int_storage));
1582 {
1583 u.valp = XNEWVEC (HOST_WIDE_INT, len);
1584 memcpy (u.valp, x.u.valp, len * sizeof (HOST_WIDE_INT));
1585 }
1586 return *this;
1587}
1588
1589template <int N>
1590template <typename T>
1591inline widest_int_storage <N>&
1592widest_int_storage <N>::operator = (const T &x)
1593{
1594 /* Check for type compatibility. We don't want to assign a
1595 widest integer from something like a wide_int. */
1596 WI_BINARY_RESULT (T, WIDEST_INT (N)) *assertion ATTRIBUTE_UNUSED;
1598 XDELETEVEC (u.valp);
1599 len = 0;
1600 wi::copy (*this, WIDE_INT_REF_FOR (T) (x, N));
1601 return *this;
1602}
1603
1604template <int N>
1605inline unsigned int
1607{
1608 return N;
1609}
1610
1611template <int N>
1612inline const HOST_WIDE_INT *
1614{
1615 return UNLIKELY (len > WIDE_INT_MAX_INL_ELTS) ? u.valp : u.val;
1616}
1617
1618template <int N>
1619inline unsigned int
1621{
1622 return len;
1623}
1624
1625template <int N>
1626inline HOST_WIDE_INT *
1628{
1630 XDELETEVEC (u.valp);
1631 len = l;
1633 {
1634 u.valp = XNEWVEC (HOST_WIDE_INT, l);
1635 return u.valp;
1636 }
1637 else if (CHECKING_P && l < WIDE_INT_MAX_INL_ELTS)
1638 u.val[l] = HOST_WIDE_INT_UC (0xbaaaaaaddeadbeef);
1639 return u.val;
1640}
1641
1642template <int N>
1643inline void
1645{
1646 gcc_checking_assert (l <= len);
1648 && l <= WIDE_INT_MAX_INL_ELTS)
1649 {
1650 HOST_WIDE_INT *valp = u.valp;
1651 memcpy (u.val, valp, l * sizeof (u.val[0]));
1652 XDELETEVEC (valp);
1653 }
1654 else if (len && len < WIDE_INT_MAX_INL_ELTS)
1655 gcc_checking_assert ((unsigned HOST_WIDE_INT) u.val[len]
1656 == HOST_WIDE_INT_UC (0xbaaaaaaddeadbeef));
1657 len = l;
1658 /* There are no excess bits in val[len - 1]. */
1660}
1661
1662/* Treat X as having signedness SGN and convert it to an N-bit number. */
1663template <int N>
1665widest_int_storage <N>::from (const wide_int_ref &x, signop sgn)
1666{
1667 WIDEST_INT (N) result;
1668 unsigned int exp_len = x.len;
1669 unsigned int prec = result.get_precision ();
1670 if (sgn == UNSIGNED && prec > x.precision && x.val[x.len - 1] < 0)
1671 exp_len = CEIL (x.precision, HOST_BITS_PER_WIDE_INT) + 1;
1672 result.set_len (wi::force_to_size (result.write_val (exp_len), x.val, x.len,
1673 x.precision, prec, sgn));
1674 return result;
1675}
1676
1677/* Create a WIDEST_INT (N) from the explicit block encoding given by
1678 VAL and LEN. NEED_CANON_P is true if the encoding may have redundant
1679 trailing blocks. */
1680template <int N>
1681inline WIDEST_INT (N)
1682widest_int_storage <N>::from_array (const HOST_WIDE_INT *val,
1683 unsigned int len,
1684 bool need_canon_p)
1685{
1686 WIDEST_INT (N) result;
1687 result.set_len (wi::from_array (result.write_val (len), val, len,
1688 result.get_precision (), need_canon_p));
1689 return result;
1690}
1691
1692template <int N>
1693template <typename T1, typename T2>
1695wi::int_traits < widest_int_storage <N> >::
1696get_binary_result (const T1 &, const T2 &)
1697{
1698 return WIDEST_INT (N) ();
1699}
1700
1701template <int N>
1702template <typename T1, typename T2>
1703inline unsigned int
1704wi::int_traits < widest_int_storage <N> >::
1705get_binary_precision (const T1 &, const T2 &)
1706{
1707 return N;
1708}
1709
1710/* A reference to one element of a trailing_wide_ints structure. */
1712{
1713private:
1714 /* The precision of the integer, which is a fixed property of the
1715 parent trailing_wide_ints. */
1716 unsigned int m_precision;
1717
1718 /* A pointer to the length field. */
1719 unsigned short *m_len;
1720
1721 /* A pointer to the HWI array. There are enough elements to hold all
1722 values of precision M_PRECISION. */
1723 HOST_WIDE_INT *m_val;
1724
1725public:
1726 trailing_wide_int_storage (unsigned int, unsigned short *, HOST_WIDE_INT *);
1727
1728 /* The standard generic_wide_int storage methods. */
1729 unsigned int get_len () const;
1730 unsigned int get_precision () const;
1731 const HOST_WIDE_INT *get_val () const;
1732 HOST_WIDE_INT *write_val (unsigned int);
1733 void set_len (unsigned int, bool = false);
1734
1735 template <typename T>
1737};
1738
1739typedef generic_wide_int <trailing_wide_int_storage> trailing_wide_int;
1740
1741/* trailing_wide_int behaves like a wide_int. */
1742namespace wi
1743{
1744 template <>
1746 : public int_traits <wide_int_storage> {};
1747}
1748
1749/* A variable-length array of wide_int-like objects that can be put
1750 at the end of a variable-sized structure. The number of objects is
1751 at most N and can be set at runtime by using set_precision().
1752
1753 Use extra_size to calculate how many bytes beyond the
1754 sizeof need to be allocated. Use set_precision to initialize the
1755 structure. */
1756template <int N>
1758{
1759private:
1760 /* The shared precision of each number. */
1761 unsigned short m_precision;
1762
1763 /* The shared maximum length of each number. */
1764 unsigned short m_max_len;
1765
1766 /* The number of elements. */
1767 unsigned char m_num_elements;
1768
1769 /* The current length of each number. */
1770 unsigned short m_len[N];
1771
1772 /* The variable-length part of the structure, which always contains
1773 at least one HWI. Element I starts at index I * M_MAX_LEN. */
1774 HOST_WIDE_INT m_val[1];
1775
1776public:
1778
1779 void set_precision (unsigned int precision, unsigned int num_elements = N);
1780 unsigned int get_precision () const { return m_precision; }
1781 unsigned int num_elements () const { return m_num_elements; }
1782 trailing_wide_int operator [] (unsigned int);
1783 const_reference operator [] (unsigned int) const;
1784 static size_t extra_size (unsigned int precision,
1785 unsigned int num_elements = N);
1786 size_t extra_size () const { return extra_size (m_precision,
1787 m_num_elements); }
1788};
1789
1791trailing_wide_int_storage (unsigned int precision, unsigned short *len,
1792 HOST_WIDE_INT *val)
1793 : m_precision (precision), m_len (len), m_val (val)
1794{
1795}
1796
1797inline unsigned int
1799{
1800 return *m_len;
1801}
1802
1803inline unsigned int
1808
1809inline const HOST_WIDE_INT *
1811{
1812 return m_val;
1813}
1814
1815inline HOST_WIDE_INT *
1817{
1818 return m_val;
1819}
1820
1821inline void
1822trailing_wide_int_storage::set_len (unsigned int len, bool is_sign_extended)
1823{
1824 *m_len = len;
1825 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > m_precision)
1826 m_val[len - 1] = sext_hwi (m_val[len - 1],
1828}
1829
1830template <typename T>
1833{
1834 WIDE_INT_REF_FOR (T) xi (x, m_precision);
1835 wi::copy (*this, xi);
1836 return *this;
1837}
1838
1839/* Initialize the structure and record that all elements have precision
1840 PRECISION. NUM_ELEMENTS can be no more than N. */
1841template <int N>
1842inline void
1844 unsigned int num_elements)
1845{
1848 m_precision = precision;
1849 m_max_len = WIDE_INT_MAX_HWIS (precision);
1850}
1851
1852/* Return a reference to element INDEX. */
1853template <int N>
1854inline trailing_wide_int
1855trailing_wide_ints <N>::operator [] (unsigned int index)
1856{
1858 &m_val[index * m_max_len]);
1859}
1860
1861template <int N>
1862inline typename trailing_wide_ints <N>::const_reference
1863trailing_wide_ints <N>::operator [] (unsigned int index) const
1864{
1865 return wi::storage_ref (&m_val[index * m_max_len],
1866 m_len[index], m_precision);
1867}
1868
1869/* Return how many extra bytes need to be added to the end of the
1870 structure in order to handle NUM_ELEMENTS wide_ints of precision
1871 PRECISION. NUM_ELEMENTS is the number of elements, and defaults
1872 to N. */
1873template <int N>
1874inline size_t
1876 unsigned int num_elements)
1877{
1878 unsigned int max_len = WIDE_INT_MAX_HWIS (precision);
1880 return (num_elements * max_len - 1) * sizeof (HOST_WIDE_INT);
1881}
1882
1883/* This macro is used in structures that end with a trailing_wide_ints field
1884 called FIELD. It declares get_NAME() and set_NAME() methods to access
1885 element I of FIELD. */
1886#define TRAILING_WIDE_INT_ACCESSOR(NAME, FIELD, I) \
1887 trailing_wide_int get_##NAME () { return FIELD[I]; } \
1888 template <typename T> void set_##NAME (const T &x) { FIELD[I] = x; }
1889
1890namespace wi
1891{
1892 /* Implementation of int_traits for primitive integer types like "int". */
1893 template <typename T, bool signed_p>
1895 {
1897 static const bool host_dependent_precision = true;
1898 static const bool is_sign_extended = true;
1899 static const bool needs_write_val_arg = false;
1900 static unsigned int get_precision (T);
1901 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int, T);
1902 };
1903}
1904
1905template <typename T, bool signed_p>
1906inline unsigned int
1908{
1909 return sizeof (T) * CHAR_BIT;
1910}
1911
1912template <typename T, bool signed_p>
1913inline wi::storage_ref
1915 unsigned int precision, T x)
1916{
1917 scratch[0] = x;
1918 if (signed_p || scratch[0] >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1919 return wi::storage_ref (scratch, 1, precision);
1920 scratch[1] = 0;
1921 return wi::storage_ref (scratch, 2, precision);
1922}
1923
1924/* Allow primitive C types to be used in wi:: routines. */
1925namespace wi
1926{
1927 template <>
1928 struct int_traits <unsigned char>
1929 : public primitive_int_traits <unsigned char, false> {};
1930
1931 template <>
1932 struct int_traits <unsigned short>
1933 : public primitive_int_traits <unsigned short, false> {};
1934
1935 template <>
1936 struct int_traits <int>
1937 : public primitive_int_traits <int, true> {};
1938
1939 template <>
1940 struct int_traits <unsigned int>
1941 : public primitive_int_traits <unsigned int, false> {};
1942
1943 template <>
1944 struct int_traits <long>
1945 : public primitive_int_traits <long, true> {};
1946
1947 template <>
1948 struct int_traits <unsigned long>
1949 : public primitive_int_traits <unsigned long, false> {};
1950
1951 template <>
1952 struct int_traits <long long>
1953 : public primitive_int_traits <long long, true> {};
1954
1955 template <>
1956 struct int_traits <unsigned long long>
1957 : public primitive_int_traits <unsigned long long, false> {};
1958}
1959
1960namespace wi
1961{
1962 /* Stores HWI-sized integer VAL, treating it as having signedness SGN
1963 and precision PRECISION. */
1965 {
1966 public:
1968 hwi_with_prec (HOST_WIDE_INT, unsigned int, signop);
1969 HOST_WIDE_INT val;
1970 unsigned int precision;
1972 };
1973
1974 hwi_with_prec shwi (HOST_WIDE_INT, unsigned int);
1975 hwi_with_prec uhwi (unsigned HOST_WIDE_INT, unsigned int);
1976
1977 hwi_with_prec minus_one (unsigned int);
1978 hwi_with_prec zero (unsigned int);
1979 hwi_with_prec one (unsigned int);
1980 hwi_with_prec two (unsigned int);
1981}
1982
1983inline wi::hwi_with_prec::hwi_with_prec (HOST_WIDE_INT v, unsigned int p,
1984 signop s)
1985 : precision (p), sgn (s)
1986{
1988 val = sext_hwi (v, precision);
1989 else
1990 val = v;
1991}
1992
1993/* Return a signed integer that has value VAL and precision PRECISION. */
1994inline wi::hwi_with_prec
1995wi::shwi (HOST_WIDE_INT val, unsigned int precision)
1996{
1997 return hwi_with_prec (val, precision, SIGNED);
1998}
1999
2000/* Return an unsigned integer that has value VAL and precision PRECISION. */
2001inline wi::hwi_with_prec
2002wi::uhwi (unsigned HOST_WIDE_INT val, unsigned int precision)
2003{
2004 return hwi_with_prec (val, precision, UNSIGNED);
2005}
2006
2007/* Return a wide int of -1 with precision PRECISION. */
2008inline wi::hwi_with_prec
2010{
2011 return wi::shwi (-1, precision);
2012}
2013
2014/* Return a wide int of 0 with precision PRECISION. */
2015inline wi::hwi_with_prec
2016wi::zero (unsigned int precision)
2017{
2018 return wi::shwi (0, precision);
2019}
2020
2021/* Return a wide int of 1 with precision PRECISION. */
2022inline wi::hwi_with_prec
2023wi::one (unsigned int precision)
2024{
2025 return wi::shwi (1, precision);
2026}
2027
2028/* Return a wide int of 2 with precision PRECISION. */
2029inline wi::hwi_with_prec
2030wi::two (unsigned int precision)
2031{
2032 return wi::shwi (2, precision);
2033}
2034
2035namespace wi
2036{
2037 /* ints_for<T>::zero (X) returns a zero that, when assigned to a T,
2038 gives that T the same precision as X. */
2039 template<typename T, precision_type = int_traits<T>::precision_type>
2041 {
2042 static int zero (const T &) { return 0; }
2043 };
2044
2045 template<typename T>
2047 {
2048 static hwi_with_prec zero (const T &);
2049 };
2050}
2051
2052template<typename T>
2053inline wi::hwi_with_prec
2055{
2056 return wi::zero (wi::get_precision (x));
2057}
2058
2059namespace wi
2060{
2061 template <>
2063 {
2065 /* hwi_with_prec has an explicitly-given precision, rather than the
2066 precision of HOST_WIDE_INT. */
2067 static const bool host_dependent_precision = false;
2068 static const bool is_sign_extended = true;
2069 static const bool needs_write_val_arg = false;
2070 static unsigned int get_precision (const wi::hwi_with_prec &);
2071 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
2072 const wi::hwi_with_prec &);
2073 };
2074}
2075
2076inline unsigned int
2078{
2079 return x.precision;
2080}
2081
2082inline wi::storage_ref
2083wi::int_traits <wi::hwi_with_prec>::
2084decompose (HOST_WIDE_INT *scratch, unsigned int precision,
2085 const wi::hwi_with_prec &x)
2086{
2088 scratch[0] = x.val;
2089 if (x.sgn == SIGNED || x.val >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
2090 return wi::storage_ref (scratch, 1, precision);
2091 scratch[1] = 0;
2092 return wi::storage_ref (scratch, 2, precision);
2093}
2094
2095/* Private functions for handling large cases out of line. They take
2096 individual length and array parameters because that is cheaper for
2097 the inline caller than constructing an object on the stack and
2098 passing a reference to it. (Although many callers use wide_int_refs,
2099 we generally want those to be removed by SRA.) */
2100namespace wi
2101{
2102 bool eq_p_large (const HOST_WIDE_INT *, unsigned int,
2103 const HOST_WIDE_INT *, unsigned int, unsigned int);
2104 bool lts_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
2105 const HOST_WIDE_INT *, unsigned int);
2106 bool ltu_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
2107 const HOST_WIDE_INT *, unsigned int);
2108 int cmps_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
2109 const HOST_WIDE_INT *, unsigned int);
2110 int cmpu_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
2111 const HOST_WIDE_INT *, unsigned int);
2112 unsigned int sext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
2113 unsigned int, unsigned int, unsigned int);
2114 unsigned int zext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
2115 unsigned int, unsigned int, unsigned int);
2116 unsigned int set_bit_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
2117 unsigned int, unsigned int, unsigned int);
2118 unsigned int bswap_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
2119 unsigned int, unsigned int);
2120 unsigned int bitreverse_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
2121 unsigned int, unsigned int);
2122
2123 unsigned int lshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
2124 unsigned int, unsigned int, unsigned int);
2125 unsigned int lrshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
2126 unsigned int, unsigned int, unsigned int,
2127 unsigned int);
2128 unsigned int arshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
2129 unsigned int, unsigned int, unsigned int,
2130 unsigned int);
2131 unsigned int and_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
2132 const HOST_WIDE_INT *, unsigned int, unsigned int);
2133 unsigned int and_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
2134 unsigned int, const HOST_WIDE_INT *,
2135 unsigned int, unsigned int);
2136 unsigned int or_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
2137 const HOST_WIDE_INT *, unsigned int, unsigned int);
2138 unsigned int or_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
2139 unsigned int, const HOST_WIDE_INT *,
2140 unsigned int, unsigned int);
2141 unsigned int xor_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
2142 const HOST_WIDE_INT *, unsigned int, unsigned int);
2143 unsigned int add_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
2144 const HOST_WIDE_INT *, unsigned int, unsigned int,
2146 unsigned int sub_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
2147 const HOST_WIDE_INT *, unsigned int, unsigned int,
2149 unsigned int mul_internal (HOST_WIDE_INT *, const HOST_WIDE_INT *,
2150 unsigned int, const HOST_WIDE_INT *,
2151 unsigned int, unsigned int, signop,
2152 overflow_type *, bool);
2153 unsigned int divmod_internal (HOST_WIDE_INT *, unsigned int *,
2154 HOST_WIDE_INT *, const HOST_WIDE_INT *,
2155 unsigned int, unsigned int,
2156 const HOST_WIDE_INT *,
2157 unsigned int, unsigned int,
2159}
2160
2161/* Return the number of bits that integer X can hold. */
2162template <typename T>
2163inline unsigned int
2165{
2166 return wi::int_traits <T>::get_precision (x);
2167}
2168
2169/* Return the number of bits that the result of a binary operation can
2170 hold when the input operands are X and Y. */
2171template <typename T1, typename T2>
2172inline unsigned int
2173wi::get_binary_precision (const T1 &x, const T2 &y)
2174{
2175 using res_traits = wi::int_traits <WI_BINARY_RESULT (T1, T2)>;
2176 return res_traits::get_binary_precision (x, y);
2177}
2178
2179/* Copy the contents of Y to X, but keeping X's current precision. */
2180template <typename T1, typename T2>
2181inline void
2182wi::copy (T1 &x, const T2 &y)
2183{
2184 unsigned int len = y.get_len ();
2185 HOST_WIDE_INT *xval = x.write_val (len);
2186 const HOST_WIDE_INT *yval = y.get_val ();
2187 unsigned int i = 0;
2188 do
2189 xval[i] = yval[i];
2190 while (++i < len);
2191 /* For widest_int write_val is called with an exact value, not
2192 upper bound for len, so nothing is needed further. */
2193 if (!wi::int_traits <T1>::needs_write_val_arg)
2194 x.set_len (len, y.is_sign_extended);
2195}
2196
2197/* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
2198template <typename T>
2199inline bool
2200wi::fits_shwi_p (const T &x)
2201{
2202 WIDE_INT_REF_FOR (T) xi (x);
2203 return xi.len == 1;
2204}
2205
2206/* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
2207 precision. */
2208template <typename T>
2209inline bool
2210wi::fits_uhwi_p (const T &x)
2211{
2212 WIDE_INT_REF_FOR (T) xi (x);
2213 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
2214 return true;
2215 if (xi.len == 1)
2216 return xi.slow () >= 0;
2217 return xi.len == 2 && xi.uhigh () == 0;
2218}
2219
2220/* Return true if X is negative based on the interpretation of SGN.
2221 For UNSIGNED, this is always false. */
2222template <typename T>
2223inline bool
2224wi::neg_p (const T &x, signop sgn)
2225{
2226 WIDE_INT_REF_FOR (T) xi (x);
2227 if (sgn == UNSIGNED)
2228 return false;
2229 return xi.sign_mask () < 0;
2230}
2231
2232/* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
2233template <typename T>
2234inline HOST_WIDE_INT
2235wi::sign_mask (const T &x)
2236{
2237 WIDE_INT_REF_FOR (T) xi (x);
2238 return xi.sign_mask ();
2239}
2240
2241/* Return true if X == Y. X and Y must be binary-compatible. */
2242template <typename T1, typename T2>
2243inline bool
2244wi::eq_p (const T1 &x, const T2 &y)
2245{
2246 unsigned int precision = get_binary_precision (x, y);
2247 WIDE_INT_REF_FOR (T1) xi (x, precision);
2248 WIDE_INT_REF_FOR (T2) yi (y, precision);
2249 if (xi.is_sign_extended && yi.is_sign_extended)
2250 {
2251 /* This case reduces to array equality. */
2252 if (xi.len != yi.len)
2253 return false;
2254 unsigned int i = 0;
2255 do
2256 if (xi.val[i] != yi.val[i])
2257 return false;
2258 while (++i != xi.len);
2259 return true;
2260 }
2261 if (LIKELY (yi.len == 1))
2262 {
2263 /* XI is only equal to YI if it too has a single HWI. */
2264 if (xi.len != 1)
2265 return false;
2266 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
2267 with 0 are simple. */
2268 if (STATIC_CONSTANT_P (yi.val[0] == 0))
2269 return xi.val[0] == 0;
2270 /* Otherwise flush out any excess bits first. */
2271 unsigned HOST_WIDE_INT diff = xi.val[0] ^ yi.val[0];
2272 int excess = HOST_BITS_PER_WIDE_INT - precision;
2273 if (excess > 0)
2274 diff <<= excess;
2275 return diff == 0;
2276 }
2277 return eq_p_large (xi.val, xi.len, yi.val, yi.len, precision);
2278}
2279
2280/* Return true if X != Y. X and Y must be binary-compatible. */
2281template <typename T1, typename T2>
2282inline bool
2283wi::ne_p (const T1 &x, const T2 &y)
2284{
2285 return !eq_p (x, y);
2286}
2287
2288/* Return true if X < Y when both are treated as signed values. */
2289template <typename T1, typename T2>
2290inline bool
2291wi::lts_p (const T1 &x, const T2 &y)
2292{
2293 unsigned int precision = get_binary_precision (x, y);
2294 WIDE_INT_REF_FOR (T1) xi (x, precision);
2295 WIDE_INT_REF_FOR (T2) yi (y, precision);
2296 /* We optimize x < y, where y is 64 or fewer bits. */
2297 if (wi::fits_shwi_p (yi))
2298 {
2299 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
2300 if (STATIC_CONSTANT_P (yi.val[0] == 0))
2301 return neg_p (xi);
2302 /* If x fits directly into a shwi, we can compare directly. */
2303 if (wi::fits_shwi_p (xi))
2304 return xi.to_shwi () < yi.to_shwi ();
2305 /* If x doesn't fit and is negative, then it must be more
2306 negative than any value in y, and hence smaller than y. */
2307 if (neg_p (xi))
2308 return true;
2309 /* If x is positive, then it must be larger than any value in y,
2310 and hence greater than y. */
2311 return false;
2312 }
2313 /* Optimize the opposite case, if it can be detected at compile time. */
2314 if (STATIC_CONSTANT_P (xi.len == 1))
2315 /* If YI is negative it is lower than the least HWI.
2316 If YI is positive it is greater than the greatest HWI. */
2317 return !neg_p (yi);
2318 return lts_p_large (xi.val, xi.len, precision, yi.val, yi.len);
2319}
2320
2321/* Return true if X < Y when both are treated as unsigned values. */
2322template <typename T1, typename T2>
2323inline bool
2324wi::ltu_p (const T1 &x, const T2 &y)
2325{
2326 unsigned int precision = get_binary_precision (x, y);
2327 WIDE_INT_REF_FOR (T1) xi (x, precision);
2328 WIDE_INT_REF_FOR (T2) yi (y, precision);
2329 /* Optimize comparisons with constants. */
2330 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
2331 return xi.len == 1 && xi.to_uhwi () < (unsigned HOST_WIDE_INT) yi.val[0];
2332 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
2333 return yi.len != 1 || yi.to_uhwi () > (unsigned HOST_WIDE_INT) xi.val[0];
2334 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
2335 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
2336 values does not change the result. */
2337 if (LIKELY (xi.len + yi.len == 2))
2338 {
2339 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
2340 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2341 return xl < yl;
2342 }
2343 return ltu_p_large (xi.val, xi.len, precision, yi.val, yi.len);
2344}
2345
2346/* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
2347template <typename T1, typename T2>
2348inline bool
2349wi::lt_p (const T1 &x, const T2 &y, signop sgn)
2350{
2351 if (sgn == SIGNED)
2352 return lts_p (x, y);
2353 else
2354 return ltu_p (x, y);
2355}
2356
2357/* Return true if X <= Y when both are treated as signed values. */
2358template <typename T1, typename T2>
2359inline bool
2360wi::les_p (const T1 &x, const T2 &y)
2361{
2362 return !lts_p (y, x);
2363}
2364
2365/* Return true if X <= Y when both are treated as unsigned values. */
2366template <typename T1, typename T2>
2367inline bool
2368wi::leu_p (const T1 &x, const T2 &y)
2369{
2370 return !ltu_p (y, x);
2371}
2372
2373/* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
2374template <typename T1, typename T2>
2375inline bool
2376wi::le_p (const T1 &x, const T2 &y, signop sgn)
2377{
2378 if (sgn == SIGNED)
2379 return les_p (x, y);
2380 else
2381 return leu_p (x, y);
2382}
2383
2384/* Return true if X > Y when both are treated as signed values. */
2385template <typename T1, typename T2>
2386inline bool
2387wi::gts_p (const T1 &x, const T2 &y)
2388{
2389 return lts_p (y, x);
2390}
2391
2392/* Return true if X > Y when both are treated as unsigned values. */
2393template <typename T1, typename T2>
2394inline bool
2395wi::gtu_p (const T1 &x, const T2 &y)
2396{
2397 return ltu_p (y, x);
2398}
2399
2400/* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
2401template <typename T1, typename T2>
2402inline bool
2403wi::gt_p (const T1 &x, const T2 &y, signop sgn)
2404{
2405 if (sgn == SIGNED)
2406 return gts_p (x, y);
2407 else
2408 return gtu_p (x, y);
2409}
2410
2411/* Return true if X >= Y when both are treated as signed values. */
2412template <typename T1, typename T2>
2413inline bool
2414wi::ges_p (const T1 &x, const T2 &y)
2415{
2416 return !lts_p (x, y);
2417}
2418
2419/* Return true if X >= Y when both are treated as unsigned values. */
2420template <typename T1, typename T2>
2421inline bool
2422wi::geu_p (const T1 &x, const T2 &y)
2423{
2424 return !ltu_p (x, y);
2425}
2426
2427/* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
2428template <typename T1, typename T2>
2429inline bool
2430wi::ge_p (const T1 &x, const T2 &y, signop sgn)
2431{
2432 if (sgn == SIGNED)
2433 return ges_p (x, y);
2434 else
2435 return geu_p (x, y);
2436}
2437
2438/* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
2439 as signed values. */
2440template <typename T1, typename T2>
2441inline int
2442wi::cmps (const T1 &x, const T2 &y)
2443{
2444 unsigned int precision = get_binary_precision (x, y);
2445 WIDE_INT_REF_FOR (T1) xi (x, precision);
2446 WIDE_INT_REF_FOR (T2) yi (y, precision);
2447 if (wi::fits_shwi_p (yi))
2448 {
2449 /* Special case for comparisons with 0. */
2450 if (STATIC_CONSTANT_P (yi.val[0] == 0))
2451 return neg_p (xi) ? -1 : !(xi.len == 1 && xi.val[0] == 0);
2452 /* If x fits into a signed HWI, we can compare directly. */
2453 if (wi::fits_shwi_p (xi))
2454 {
2455 HOST_WIDE_INT xl = xi.to_shwi ();
2456 HOST_WIDE_INT yl = yi.to_shwi ();
2457 return xl < yl ? -1 : xl > yl;
2458 }
2459 /* If x doesn't fit and is negative, then it must be more
2460 negative than any signed HWI, and hence smaller than y. */
2461 if (neg_p (xi))
2462 return -1;
2463 /* If x is positive, then it must be larger than any signed HWI,
2464 and hence greater than y. */
2465 return 1;
2466 }
2467 /* Optimize the opposite case, if it can be detected at compile time. */
2468 if (STATIC_CONSTANT_P (xi.len == 1))
2469 /* If YI is negative it is lower than the least HWI.
2470 If YI is positive it is greater than the greatest HWI. */
2471 return neg_p (yi) ? 1 : -1;
2472 return cmps_large (xi.val, xi.len, precision, yi.val, yi.len);
2473}
2474
2475/* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
2476 as unsigned values. */
2477template <typename T1, typename T2>
2478inline int
2479wi::cmpu (const T1 &x, const T2 &y)
2480{
2481 unsigned int precision = get_binary_precision (x, y);
2482 WIDE_INT_REF_FOR (T1) xi (x, precision);
2483 WIDE_INT_REF_FOR (T2) yi (y, precision);
2484 /* Optimize comparisons with constants. */
2485 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
2486 {
2487 /* If XI doesn't fit in a HWI then it must be larger than YI. */
2488 if (xi.len != 1)
2489 return 1;
2490 /* Otherwise compare directly. */
2491 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
2492 unsigned HOST_WIDE_INT yl = yi.val[0];
2493 return xl < yl ? -1 : xl > yl;
2494 }
2495 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
2496 {
2497 /* If YI doesn't fit in a HWI then it must be larger than XI. */
2498 if (yi.len != 1)
2499 return -1;
2500 /* Otherwise compare directly. */
2501 unsigned HOST_WIDE_INT xl = xi.val[0];
2502 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2503 return xl < yl ? -1 : xl > yl;
2504 }
2505 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
2506 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
2507 values does not change the result. */
2508 if (LIKELY (xi.len + yi.len == 2))
2509 {
2510 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
2511 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2512 return xl < yl ? -1 : xl > yl;
2513 }
2514 return cmpu_large (xi.val, xi.len, precision, yi.val, yi.len);
2515}
2516
2517/* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
2518 X and Y indicated by SGN. */
2519template <typename T1, typename T2>
2520inline int
2521wi::cmp (const T1 &x, const T2 &y, signop sgn)
2522{
2523 if (sgn == SIGNED)
2524 return cmps (x, y);
2525 else
2526 return cmpu (x, y);
2527}
2528
2529/* Return ~x. */
2530template <typename T>
2532wi::bit_not (const T &x)
2533{
2534 WI_UNARY_RESULT_VAR (result, val, T, x);
2535 WIDE_INT_REF_FOR (T) xi (x, get_precision (result));
2536 if (result.needs_write_val_arg)
2537 val = result.write_val (xi.len);
2538 for (unsigned int i = 0; i < xi.len; ++i)
2539 val[i] = ~xi.val[i];
2540 result.set_len (xi.len);
2541 return result;
2542}
2543
2544/* Return -x. */
2545template <typename T>
2546inline WI_UNARY_RESULT (T)
2547wi::neg (const T &x)
2548{
2549 return sub (0, x);
2550}
2551
2552/* Return -x. Indicate in *OVERFLOW if performing the negation would
2553 cause an overflow. */
2554template <typename T>
2555inline WI_UNARY_RESULT (T)
2556wi::neg (const T &x, overflow_type *overflow)
2557{
2558 *overflow = only_sign_bit_p (x) ? OVF_OVERFLOW : OVF_NONE;
2559 return sub (0, x);
2560}
2561
2562/* Return the absolute value of x. */
2563template <typename T>
2564inline WI_UNARY_RESULT (T)
2565wi::abs (const T &x)
2566{
2567 return neg_p (x) ? neg (x) : WI_UNARY_RESULT (T) (x);
2568}
2569
2570/* Return the result of sign-extending the low OFFSET bits of X. */
2571template <typename T>
2572inline WI_UNARY_RESULT (T)
2573wi::sext (const T &x, unsigned int offset)
2574{
2575 WI_UNARY_RESULT_VAR (result, val, T, x);
2576 unsigned int precision = get_precision (result);
2577 WIDE_INT_REF_FOR (T) xi (x, precision);
2578
2579 if (result.needs_write_val_arg)
2580 val = result.write_val (MAX (xi.len,
2581 CEIL (offset, HOST_BITS_PER_WIDE_INT)));
2582 if (offset <= HOST_BITS_PER_WIDE_INT)
2583 {
2584 val[0] = sext_hwi (xi.ulow (), offset);
2585 result.set_len (1, true);
2586 }
2587 else
2588 result.set_len (sext_large (val, xi.val, xi.len, precision, offset));
2589 return result;
2590}
2591
2592/* Return the result of zero-extending the low OFFSET bits of X. */
2593template <typename T>
2594inline WI_UNARY_RESULT (T)
2595wi::zext (const T &x, unsigned int offset)
2596{
2597 WI_UNARY_RESULT_VAR (result, val, T, x);
2598 unsigned int precision = get_precision (result);
2599 WIDE_INT_REF_FOR (T) xi (x, precision);
2600
2601 /* This is not just an optimization, it is actually required to
2602 maintain canonization. */
2603 if (offset >= precision)
2604 {
2605 wi::copy (result, xi);
2606 return result;
2607 }
2608
2609 if (result.needs_write_val_arg)
2610 val = result.write_val (MAX (xi.len,
2611 offset / HOST_BITS_PER_WIDE_INT + 1));
2612 /* In these cases we know that at least the top bit will be clear,
2613 so no sign extension is necessary. */
2614 if (offset < HOST_BITS_PER_WIDE_INT)
2615 {
2616 val[0] = zext_hwi (xi.ulow (), offset);
2617 result.set_len (1, true);
2618 }
2619 else
2620 result.set_len (zext_large (val, xi.val, xi.len, precision, offset), true);
2621 return result;
2622}
2623
2624/* Return the result of extending the low OFFSET bits of X according to
2625 signedness SGN. */
2626template <typename T>
2627inline WI_UNARY_RESULT (T)
2628wi::ext (const T &x, unsigned int offset, signop sgn)
2629{
2630 return sgn == SIGNED ? sext (x, offset) : zext (x, offset);
2631}
2632
2633/* Return an integer that represents X | (1 << bit). */
2634template <typename T>
2635inline WI_UNARY_RESULT (T)
2636wi::set_bit (const T &x, unsigned int bit)
2637{
2638 WI_UNARY_RESULT_VAR (result, val, T, x);
2639 unsigned int precision = get_precision (result);
2640 WIDE_INT_REF_FOR (T) xi (x, precision);
2641 if (result.needs_write_val_arg)
2642 val = result.write_val (MAX (xi.len,
2643 bit / HOST_BITS_PER_WIDE_INT + 1));
2645 {
2646 val[0] = xi.ulow () | (HOST_WIDE_INT_1U << bit);
2647 result.set_len (1);
2648 }
2649 else
2650 result.set_len (set_bit_large (val, xi.val, xi.len, precision, bit));
2651 return result;
2652}
2653
2654/* Byte swap the integer X.
2655 ??? This always swaps 8-bit octets, regardless of BITS_PER_UNIT.
2656 This function requires X's precision to be a multiple of 8 bits,
2657 so care needs to be taken for targets where BITS_PER_UNIT != 8. */
2658template <typename T>
2659inline WI_UNARY_RESULT (T)
2660wi::bswap (const T &x)
2661{
2662 WI_UNARY_RESULT_VAR (result, val, T, x);
2663 unsigned int precision = get_precision (result);
2664 WIDE_INT_REF_FOR (T) xi (x, precision);
2665 static_assert (!result.needs_write_val_arg,
2666 "bswap on widest_int makes no sense");
2667 result.set_len (bswap_large (val, xi.val, xi.len, precision));
2668 return result;
2669}
2670
2671/* Bitreverse the integer X. */
2672template <typename T>
2673inline WI_UNARY_RESULT (T)
2674wi::bitreverse (const T &x)
2675{
2676 WI_UNARY_RESULT_VAR (result, val, T, x);
2677 unsigned int precision = get_precision (result);
2678 WIDE_INT_REF_FOR (T) xi (x, precision);
2679 static_assert (!result.needs_write_val_arg,
2680 "bitreverse on widest_int makes no sense");
2681 result.set_len (bitreverse_large (val, xi.val, xi.len, precision));
2682 return result;
2683}
2684
2685/* Return the minimum of X and Y, treating them both as having
2686 signedness SGN. */
2687template <typename T1, typename T2>
2688inline WI_BINARY_RESULT (T1, T2)
2689wi::min (const T1 &x, const T2 &y, signop sgn)
2690{
2691 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2692 unsigned int precision = get_precision (result);
2693 if (wi::le_p (x, y, sgn))
2694 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2695 else
2696 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2697 return result;
2698}
2699
2700/* Return the minimum of X and Y, treating both as signed values. */
2701template <typename T1, typename T2>
2702inline WI_BINARY_RESULT (T1, T2)
2703wi::smin (const T1 &x, const T2 &y)
2704{
2705 return wi::min (x, y, SIGNED);
2706}
2707
2708/* Return the minimum of X and Y, treating both as unsigned values. */
2709template <typename T1, typename T2>
2710inline WI_BINARY_RESULT (T1, T2)
2711wi::umin (const T1 &x, const T2 &y)
2712{
2713 return wi::min (x, y, UNSIGNED);
2714}
2715
2716/* Return the maximum of X and Y, treating them both as having
2717 signedness SGN. */
2718template <typename T1, typename T2>
2719inline WI_BINARY_RESULT (T1, T2)
2720wi::max (const T1 &x, const T2 &y, signop sgn)
2721{
2722 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2723 unsigned int precision = get_precision (result);
2724 if (wi::ge_p (x, y, sgn))
2725 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2726 else
2727 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2728 return result;
2729}
2730
2731/* Return the maximum of X and Y, treating both as signed values. */
2732template <typename T1, typename T2>
2733inline WI_BINARY_RESULT (T1, T2)
2734wi::smax (const T1 &x, const T2 &y)
2735{
2736 return wi::max (x, y, SIGNED);
2737}
2738
2739/* Return the maximum of X and Y, treating both as unsigned values. */
2740template <typename T1, typename T2>
2741inline WI_BINARY_RESULT (T1, T2)
2742wi::umax (const T1 &x, const T2 &y)
2743{
2744 return wi::max (x, y, UNSIGNED);
2745}
2746
2747/* Return X & Y. */
2748template <typename T1, typename T2>
2749inline WI_BINARY_RESULT (T1, T2)
2750wi::bit_and (const T1 &x, const T2 &y)
2751{
2752 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2753 unsigned int precision = get_precision (result);
2754 WIDE_INT_REF_FOR (T1) xi (x, precision);
2755 WIDE_INT_REF_FOR (T2) yi (y, precision);
2756 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2757 if (result.needs_write_val_arg)
2758 val = result.write_val (MAX (xi.len, yi.len));
2759 if (LIKELY (xi.len + yi.len == 2))
2760 {
2761 val[0] = xi.ulow () & yi.ulow ();
2762 result.set_len (1, is_sign_extended);
2763 }
2764 else
2765 result.set_len (and_large (val, xi.val, xi.len, yi.val, yi.len,
2766 precision), is_sign_extended);
2767 return result;
2768}
2769
2770/* Return X & ~Y. */
2771template <typename T1, typename T2>
2772inline WI_BINARY_RESULT (T1, T2)
2773wi::bit_and_not (const T1 &x, const T2 &y)
2774{
2775 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2776 unsigned int precision = get_precision (result);
2777 WIDE_INT_REF_FOR (T1) xi (x, precision);
2778 WIDE_INT_REF_FOR (T2) yi (y, precision);
2779 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2780 if (result.needs_write_val_arg)
2781 val = result.write_val (MAX (xi.len, yi.len));
2782 if (LIKELY (xi.len + yi.len == 2))
2783 {
2784 val[0] = xi.ulow () & ~yi.ulow ();
2785 result.set_len (1, is_sign_extended);
2786 }
2787 else
2788 result.set_len (and_not_large (val, xi.val, xi.len, yi.val, yi.len,
2789 precision), is_sign_extended);
2790 return result;
2791}
2792
2793/* Return X | Y. */
2794template <typename T1, typename T2>
2795inline WI_BINARY_RESULT (T1, T2)
2796wi::bit_or (const T1 &x, const T2 &y)
2797{
2798 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2799 unsigned int precision = get_precision (result);
2800 WIDE_INT_REF_FOR (T1) xi (x, precision);
2801 WIDE_INT_REF_FOR (T2) yi (y, precision);
2802 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2803 if (result.needs_write_val_arg)
2804 val = result.write_val (MAX (xi.len, yi.len));
2805 if (LIKELY (xi.len + yi.len == 2))
2806 {
2807 val[0] = xi.ulow () | yi.ulow ();
2808 result.set_len (1, is_sign_extended);
2809 }
2810 else
2811 result.set_len (or_large (val, xi.val, xi.len,
2812 yi.val, yi.len, precision), is_sign_extended);
2813 return result;
2814}
2815
2816/* Return X | ~Y. */
2817template <typename T1, typename T2>
2818inline WI_BINARY_RESULT (T1, T2)
2819wi::bit_or_not (const T1 &x, const T2 &y)
2820{
2821 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2822 unsigned int precision = get_precision (result);
2823 WIDE_INT_REF_FOR (T1) xi (x, precision);
2824 WIDE_INT_REF_FOR (T2) yi (y, precision);
2825 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2826 if (result.needs_write_val_arg)
2827 val = result.write_val (MAX (xi.len, yi.len));
2828 if (LIKELY (xi.len + yi.len == 2))
2829 {
2830 val[0] = xi.ulow () | ~yi.ulow ();
2831 result.set_len (1, is_sign_extended);
2832 }
2833 else
2834 result.set_len (or_not_large (val, xi.val, xi.len, yi.val, yi.len,
2835 precision), is_sign_extended);
2836 return result;
2837}
2838
2839/* Return X ^ Y. */
2840template <typename T1, typename T2>
2841inline WI_BINARY_RESULT (T1, T2)
2842wi::bit_xor (const T1 &x, const T2 &y)
2843{
2844 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2845 unsigned int precision = get_precision (result);
2846 WIDE_INT_REF_FOR (T1) xi (x, precision);
2847 WIDE_INT_REF_FOR (T2) yi (y, precision);
2848 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2849 if (result.needs_write_val_arg)
2850 val = result.write_val (MAX (xi.len, yi.len));
2851 if (LIKELY (xi.len + yi.len == 2))
2852 {
2853 val[0] = xi.ulow () ^ yi.ulow ();
2854 result.set_len (1, is_sign_extended);
2855 }
2856 else
2857 result.set_len (xor_large (val, xi.val, xi.len,
2858 yi.val, yi.len, precision), is_sign_extended);
2859 return result;
2860}
2861
2862/* Return X + Y. */
2863template <typename T1, typename T2>
2864inline WI_BINARY_RESULT (T1, T2)
2865wi::add (const T1 &x, const T2 &y)
2866{
2867 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2868 unsigned int precision = get_precision (result);
2869 WIDE_INT_REF_FOR (T1) xi (x, precision);
2870 WIDE_INT_REF_FOR (T2) yi (y, precision);
2871 if (result.needs_write_val_arg)
2872 val = result.write_val (MAX (xi.len, yi.len) + 1);
2874 {
2875 val[0] = xi.ulow () + yi.ulow ();
2876 result.set_len (1);
2877 }
2878 /* If the precision is known at compile time to be greater than
2879 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2880 knowing that (a) all bits in those HWIs are significant and
2881 (b) the result has room for at least two HWIs. This provides
2882 a fast path for things like offset_int and widest_int.
2883
2884 The STATIC_CONSTANT_P test prevents this path from being
2885 used for wide_ints. wide_ints with precisions greater than
2886 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2887 point handling them inline. */
2889 && LIKELY (xi.len + yi.len == 2))
2890 {
2891 unsigned HOST_WIDE_INT xl = xi.ulow ();
2892 unsigned HOST_WIDE_INT yl = yi.ulow ();
2893 unsigned HOST_WIDE_INT resultl = xl + yl;
2894 val[0] = resultl;
2895 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2896 result.set_len (1 + (((resultl ^ xl) & (resultl ^ yl))
2897 >> (HOST_BITS_PER_WIDE_INT - 1)));
2898 }
2899 else
2900 result.set_len (add_large (val, xi.val, xi.len,
2901 yi.val, yi.len, precision,
2902 UNSIGNED, 0));
2903 return result;
2904}
2905
2906/* Return X + Y. Treat X and Y as having the signednes given by SGN
2907 and indicate in *OVERFLOW whether the operation overflowed. */
2908template <typename T1, typename T2>
2909inline WI_BINARY_RESULT (T1, T2)
2910wi::add (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2911{
2912 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2913 unsigned int precision = get_precision (result);
2914 WIDE_INT_REF_FOR (T1) xi (x, precision);
2915 WIDE_INT_REF_FOR (T2) yi (y, precision);
2916 if (result.needs_write_val_arg)
2917 val = result.write_val (MAX (xi.len, yi.len) + 1);
2919 {
2920 unsigned HOST_WIDE_INT xl = xi.ulow ();
2921 unsigned HOST_WIDE_INT yl = yi.ulow ();
2922 unsigned HOST_WIDE_INT resultl = xl + yl;
2923 if (sgn == SIGNED)
2924 {
2925 if ((((resultl ^ xl) & (resultl ^ yl))
2926 >> (precision - 1)) & 1)
2927 {
2928 if (xl > resultl)
2929 *overflow = OVF_UNDERFLOW;
2930 else if (xl < resultl)
2931 *overflow = OVF_OVERFLOW;
2932 else
2933 *overflow = OVF_NONE;
2934 }
2935 else
2936 *overflow = OVF_NONE;
2937 }
2938 else
2939 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2940 < (xl << (HOST_BITS_PER_WIDE_INT - precision)))
2942 val[0] = resultl;
2943 result.set_len (1);
2944 }
2945 else
2946 result.set_len (add_large (val, xi.val, xi.len,
2947 yi.val, yi.len, precision,
2948 sgn, overflow));
2949 return result;
2950}
2951
2952/* Return X - Y. */
2953template <typename T1, typename T2>
2954inline WI_BINARY_RESULT (T1, T2)
2955wi::sub (const T1 &x, const T2 &y)
2956{
2957 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2958 unsigned int precision = get_precision (result);
2959 WIDE_INT_REF_FOR (T1) xi (x, precision);
2960 WIDE_INT_REF_FOR (T2) yi (y, precision);
2961 if (result.needs_write_val_arg)
2962 val = result.write_val (MAX (xi.len, yi.len) + 1);
2964 {
2965 val[0] = xi.ulow () - yi.ulow ();
2966 result.set_len (1);
2967 }
2968 /* If the precision is known at compile time to be greater than
2969 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2970 knowing that (a) all bits in those HWIs are significant and
2971 (b) the result has room for at least two HWIs. This provides
2972 a fast path for things like offset_int and widest_int.
2973
2974 The STATIC_CONSTANT_P test prevents this path from being
2975 used for wide_ints. wide_ints with precisions greater than
2976 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2977 point handling them inline. */
2979 && LIKELY (xi.len + yi.len == 2))
2980 {
2981 unsigned HOST_WIDE_INT xl = xi.ulow ();
2982 unsigned HOST_WIDE_INT yl = yi.ulow ();
2983 unsigned HOST_WIDE_INT resultl = xl - yl;
2984 val[0] = resultl;
2985 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2986 result.set_len (1 + (((resultl ^ xl) & (xl ^ yl))
2987 >> (HOST_BITS_PER_WIDE_INT - 1)));
2988 }
2989 else
2990 result.set_len (sub_large (val, xi.val, xi.len,
2991 yi.val, yi.len, precision,
2992 UNSIGNED, 0));
2993 return result;
2994}
2995
2996/* Return X - Y. Treat X and Y as having the signednes given by SGN
2997 and indicate in *OVERFLOW whether the operation overflowed. */
2998template <typename T1, typename T2>
2999inline WI_BINARY_RESULT (T1, T2)
3000wi::sub (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3001{
3002 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
3003 unsigned int precision = get_precision (result);
3004 WIDE_INT_REF_FOR (T1) xi (x, precision);
3005 WIDE_INT_REF_FOR (T2) yi (y, precision);
3006 if (result.needs_write_val_arg)
3007 val = result.write_val (MAX (xi.len, yi.len) + 1);
3009 {
3010 unsigned HOST_WIDE_INT xl = xi.ulow ();
3011 unsigned HOST_WIDE_INT yl = yi.ulow ();
3012 unsigned HOST_WIDE_INT resultl = xl - yl;
3013 if (sgn == SIGNED)
3014 {
3015 if ((((xl ^ yl) & (resultl ^ xl)) >> (precision - 1)) & 1)
3016 {
3017 if (xl > yl)
3018 *overflow = OVF_UNDERFLOW;
3019 else if (xl < yl)
3020 *overflow = OVF_OVERFLOW;
3021 else
3022 *overflow = OVF_NONE;
3023 }
3024 else
3025 *overflow = OVF_NONE;
3026 }
3027 else
3028 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
3029 > (xl << (HOST_BITS_PER_WIDE_INT - precision)))
3031 val[0] = resultl;
3032 result.set_len (1);
3033 }
3034 else
3035 result.set_len (sub_large (val, xi.val, xi.len,
3036 yi.val, yi.len, precision,
3037 sgn, overflow));
3038 return result;
3039}
3040
3041/* Return X * Y. */
3042template <typename T1, typename T2>
3043inline WI_BINARY_RESULT (T1, T2)
3044wi::mul (const T1 &x, const T2 &y)
3045{
3046 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
3047 unsigned int precision = get_precision (result);
3048 WIDE_INT_REF_FOR (T1) xi (x, precision);
3049 WIDE_INT_REF_FOR (T2) yi (y, precision);
3050 if (result.needs_write_val_arg)
3051 val = result.write_val (xi.len + yi.len + 2);
3053 {
3054 val[0] = xi.ulow () * yi.ulow ();
3055 result.set_len (1);
3056 }
3057 else
3058 result.set_len (mul_internal (val, xi.val, xi.len, yi.val, yi.len,
3059 precision, UNSIGNED, 0, false));
3060 return result;
3061}
3062
3063/* Return X * Y. Treat X and Y as having the signednes given by SGN
3064 and indicate in *OVERFLOW whether the operation overflowed. */
3065template <typename T1, typename T2>
3066inline WI_BINARY_RESULT (T1, T2)
3067wi::mul (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3068{
3069 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
3070 unsigned int precision = get_precision (result);
3071 WIDE_INT_REF_FOR (T1) xi (x, precision);
3072 WIDE_INT_REF_FOR (T2) yi (y, precision);
3073 if (result.needs_write_val_arg)
3074 val = result.write_val (xi.len + yi.len + 2);
3075 result.set_len (mul_internal (val, xi.val, xi.len,
3076 yi.val, yi.len, precision,
3077 sgn, overflow, false));
3078 return result;
3079}
3080
3081/* Return X * Y, treating both X and Y as signed values. Indicate in
3082 *OVERFLOW whether the operation overflowed. */
3083template <typename T1, typename T2>
3084inline WI_BINARY_RESULT (T1, T2)
3085wi::smul (const T1 &x, const T2 &y, overflow_type *overflow)
3086{
3087 return mul (x, y, SIGNED, overflow);
3088}
3089
3090/* Return X * Y, treating both X and Y as unsigned values. Indicate in
3091 *OVERFLOW if the result overflows. */
3092template <typename T1, typename T2>
3093inline WI_BINARY_RESULT (T1, T2)
3094wi::umul (const T1 &x, const T2 &y, overflow_type *overflow)
3095{
3096 return mul (x, y, UNSIGNED, overflow);
3097}
3098
3099/* Perform a widening multiplication of X and Y, extending the values
3100 according to SGN, and return the high part of the result. */
3101template <typename T1, typename T2>
3102inline WI_BINARY_RESULT (T1, T2)
3103wi::mul_high (const T1 &x, const T2 &y, signop sgn)
3104{
3105 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
3106 unsigned int precision = get_precision (result);
3107 WIDE_INT_REF_FOR (T1) xi (x, precision);
3108 WIDE_INT_REF_FOR (T2) yi (y, precision);
3109 static_assert (!result.needs_write_val_arg,
3110 "mul_high on widest_int doesn't make sense");
3111 result.set_len (mul_internal (val, xi.val, xi.len,
3112 yi.val, yi.len, precision,
3113 sgn, 0, true));
3114 return result;
3115}
3116
3117/* Return X / Y, rounding towards 0. Treat X and Y as having the
3118 signedness given by SGN. Indicate in *OVERFLOW if the result
3119 overflows. */
3120template <typename T1, typename T2>
3121inline WI_BINARY_RESULT (T1, T2)
3122wi::div_trunc (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3123{
3124 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3125 unsigned int precision = get_precision (quotient);
3126 WIDE_INT_REF_FOR (T1) xi (x, precision);
3127 WIDE_INT_REF_FOR (T2) yi (y);
3128
3129 if (quotient.needs_write_val_arg)
3130 quotient_val = quotient.write_val ((sgn == UNSIGNED
3131 && xi.val[xi.len - 1] < 0)
3132 ? CEIL (precision,
3134 : xi.len + 1);
3135 quotient.set_len (divmod_internal (quotient_val, 0, 0, xi.val, xi.len,
3136 precision,
3137 yi.val, yi.len, yi.precision,
3138 sgn, overflow));
3139 return quotient;
3140}
3141
3142/* Return X / Y, rounding towards 0. Treat X and Y as signed values. */
3143template <typename T1, typename T2>
3144inline WI_BINARY_RESULT (T1, T2)
3145wi::sdiv_trunc (const T1 &x, const T2 &y)
3146{
3147 return div_trunc (x, y, SIGNED);
3148}
3149
3150/* Return X / Y, rounding towards 0. Treat X and Y as unsigned values. */
3151template <typename T1, typename T2>
3152inline WI_BINARY_RESULT (T1, T2)
3153wi::udiv_trunc (const T1 &x, const T2 &y)
3154{
3155 return div_trunc (x, y, UNSIGNED);
3156}
3157
3158/* Return X / Y, rounding towards -inf. Treat X and Y as having the
3159 signedness given by SGN. Indicate in *OVERFLOW if the result
3160 overflows. */
3161template <typename T1, typename T2>
3162inline WI_BINARY_RESULT (T1, T2)
3163wi::div_floor (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3164{
3165 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3166 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3167 unsigned int precision = get_precision (quotient);
3168 WIDE_INT_REF_FOR (T1) xi (x, precision);
3169 WIDE_INT_REF_FOR (T2) yi (y);
3170
3171 unsigned int remainder_len;
3172 if (quotient.needs_write_val_arg)
3173 {
3174 unsigned int est_len;
3175 if (sgn == UNSIGNED && xi.val[xi.len - 1] < 0)
3176 est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
3177 else
3178 est_len = xi.len + 1;
3179 quotient_val = quotient.write_val (est_len);
3180 remainder_val = remainder.write_val (est_len);
3181 }
3182 quotient.set_len (divmod_internal (quotient_val,
3183 &remainder_len, remainder_val,
3184 xi.val, xi.len, precision,
3185 yi.val, yi.len, yi.precision, sgn,
3186 overflow));
3187 remainder.set_len (remainder_len);
3188 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
3189 return quotient - 1;
3190 return quotient;
3191}
3192
3193/* Return X / Y, rounding towards -inf. Treat X and Y as signed values. */
3194template <typename T1, typename T2>
3195inline WI_BINARY_RESULT (T1, T2)
3196wi::sdiv_floor (const T1 &x, const T2 &y)
3197{
3198 return div_floor (x, y, SIGNED);
3199}
3200
3201/* Return X / Y, rounding towards -inf. Treat X and Y as unsigned values. */
3202/* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
3203template <typename T1, typename T2>
3204inline WI_BINARY_RESULT (T1, T2)
3205wi::udiv_floor (const T1 &x, const T2 &y)
3206{
3207 return div_floor (x, y, UNSIGNED);
3208}
3209
3210/* Return X / Y, rounding towards +inf. Treat X and Y as having the
3211 signedness given by SGN. Indicate in *OVERFLOW if the result
3212 overflows. */
3213template <typename T1, typename T2>
3214inline WI_BINARY_RESULT (T1, T2)
3215wi::div_ceil (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3216{
3217 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3218 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3219 unsigned int precision = get_precision (quotient);
3220 WIDE_INT_REF_FOR (T1) xi (x, precision);
3221 WIDE_INT_REF_FOR (T2) yi (y);
3222
3223 unsigned int remainder_len;
3224 if (quotient.needs_write_val_arg)
3225 {
3226 unsigned int est_len;
3227 if (sgn == UNSIGNED && xi.val[xi.len - 1] < 0)
3228 est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
3229 else
3230 est_len = xi.len + 1;
3231 quotient_val = quotient.write_val (est_len);
3232 remainder_val = remainder.write_val (est_len);
3233 }
3234 quotient.set_len (divmod_internal (quotient_val,
3235 &remainder_len, remainder_val,
3236 xi.val, xi.len, precision,
3237 yi.val, yi.len, yi.precision, sgn,
3238 overflow));
3239 remainder.set_len (remainder_len);
3240 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
3241 return quotient + 1;
3242 return quotient;
3243}
3244
3245/* Return X / Y, rounding towards +inf. Treat X and Y as unsigned values. */
3246template <typename T1, typename T2>
3247inline WI_BINARY_RESULT (T1, T2)
3248wi::udiv_ceil (const T1 &x, const T2 &y)
3249{
3250 return div_ceil (x, y, UNSIGNED);
3251}
3252
3253/* Return X / Y, rounding towards nearest with ties away from zero.
3254 Treat X and Y as having the signedness given by SGN. Indicate
3255 in *OVERFLOW if the result overflows. */
3256template <typename T1, typename T2>
3257inline WI_BINARY_RESULT (T1, T2)
3258wi::div_round (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3259{
3260 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3261 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3262 unsigned int precision = get_precision (quotient);
3263 WIDE_INT_REF_FOR (T1) xi (x, precision);
3264 WIDE_INT_REF_FOR (T2) yi (y);
3265
3266 unsigned int remainder_len;
3267 if (quotient.needs_write_val_arg)
3268 {
3269 unsigned int est_len;
3270 if (sgn == UNSIGNED && xi.val[xi.len - 1] < 0)
3271 est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
3272 else
3273 est_len = xi.len + 1;
3274 quotient_val = quotient.write_val (est_len);
3275 remainder_val = remainder.write_val (est_len);
3276 }
3277 quotient.set_len (divmod_internal (quotient_val,
3278 &remainder_len, remainder_val,
3279 xi.val, xi.len, precision,
3280 yi.val, yi.len, yi.precision, sgn,
3281 overflow));
3282 remainder.set_len (remainder_len);
3283
3284 if (remainder != 0)
3285 {
3286 if (sgn == SIGNED)
3287 {
3288 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
3289 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
3290 {
3291 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
3292 return quotient - 1;
3293 else
3294 return quotient + 1;
3295 }
3296 }
3297 else
3298 {
3299 if (wi::geu_p (remainder, wi::sub (y, remainder)))
3300 return quotient + 1;
3301 }
3302 }
3303 return quotient;
3304}
3305
3306/* Return X / Y, rounding towards 0. Treat X and Y as having the
3307 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
3308template <typename T1, typename T2>
3309inline WI_BINARY_RESULT (T1, T2)
3310wi::divmod_trunc (const T1 &x, const T2 &y, signop sgn,
3311 WI_BINARY_RESULT (T1, T2) *remainder_ptr)
3312{
3313 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3314 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3315 unsigned int precision = get_precision (quotient);
3316 WIDE_INT_REF_FOR (T1) xi (x, precision);
3317 WIDE_INT_REF_FOR (T2) yi (y);
3318
3319 unsigned int remainder_len;
3320 if (quotient.needs_write_val_arg)
3321 {
3322 unsigned int est_len;
3323 if (sgn == UNSIGNED && xi.val[xi.len - 1] < 0)
3324 est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
3325 else
3326 est_len = xi.len + 1;
3327 quotient_val = quotient.write_val (est_len);
3328 remainder_val = remainder.write_val (est_len);
3329 }
3330 quotient.set_len (divmod_internal (quotient_val,
3331 &remainder_len, remainder_val,
3332 xi.val, xi.len, precision,
3333 yi.val, yi.len, yi.precision, sgn, 0));
3334 remainder.set_len (remainder_len);
3335
3336 *remainder_ptr = remainder;
3337 return quotient;
3338}
3339
3340/* Compute the greatest common divisor of two numbers A and B using
3341 Euclid's algorithm. */
3342template <typename T1, typename T2>
3343inline WI_BINARY_RESULT (T1, T2)
3344wi::gcd (const T1 &a, const T2 &b, signop sgn)
3345{
3346 T1 x, y, z;
3347
3348 x = wi::abs (a);
3349 y = wi::abs (b);
3350
3351 while (gt_p (x, 0, sgn))
3352 {
3353 z = mod_trunc (y, x, sgn);
3354 y = x;
3355 x = z;
3356 }
3357
3358 return y;
3359}
3360
3361/* Compute X / Y, rounding towards 0, and return the remainder.
3362 Treat X and Y as having the signedness given by SGN. Indicate
3363 in *OVERFLOW if the division overflows. */
3364template <typename T1, typename T2>
3365inline WI_BINARY_RESULT (T1, T2)
3366wi::mod_trunc (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3367{
3368 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3369 unsigned int precision = get_precision (remainder);
3370 WIDE_INT_REF_FOR (T1) xi (x, precision);
3371 WIDE_INT_REF_FOR (T2) yi (y);
3372
3373 unsigned int remainder_len;
3374 if (remainder.needs_write_val_arg)
3375 remainder_val = remainder.write_val ((sgn == UNSIGNED
3376 && xi.val[xi.len - 1] < 0)
3377 ? CEIL (precision,
3379 : xi.len + 1);
3380 divmod_internal (0, &remainder_len, remainder_val,
3381 xi.val, xi.len, precision,
3382 yi.val, yi.len, yi.precision, sgn, overflow);
3383 remainder.set_len (remainder_len);
3384
3385 return remainder;
3386}
3387
3388/* Compute X / Y, rounding towards 0, and return the remainder.
3389 Treat X and Y as signed values. */
3390template <typename T1, typename T2>
3391inline WI_BINARY_RESULT (T1, T2)
3392wi::smod_trunc (const T1 &x, const T2 &y)
3393{
3394 return mod_trunc (x, y, SIGNED);
3395}
3396
3397/* Compute X / Y, rounding towards 0, and return the remainder.
3398 Treat X and Y as unsigned values. */
3399template <typename T1, typename T2>
3400inline WI_BINARY_RESULT (T1, T2)
3401wi::umod_trunc (const T1 &x, const T2 &y)
3402{
3403 return mod_trunc (x, y, UNSIGNED);
3404}
3405
3406/* Compute X / Y, rounding towards -inf, and return the remainder.
3407 Treat X and Y as having the signedness given by SGN. Indicate
3408 in *OVERFLOW if the division overflows. */
3409template <typename T1, typename T2>
3410inline WI_BINARY_RESULT (T1, T2)
3411wi::mod_floor (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3412{
3413 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3414 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3415 unsigned int precision = get_precision (quotient);
3416 WIDE_INT_REF_FOR (T1) xi (x, precision);
3417 WIDE_INT_REF_FOR (T2) yi (y);
3418
3419 unsigned int remainder_len;
3420 if (quotient.needs_write_val_arg)
3421 {
3422 unsigned int est_len;
3423 if (sgn == UNSIGNED && xi.val[xi.len - 1] < 0)
3424 est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
3425 else
3426 est_len = xi.len + 1;
3427 quotient_val = quotient.write_val (est_len);
3428 remainder_val = remainder.write_val (est_len);
3429 }
3430 quotient.set_len (divmod_internal (quotient_val,
3431 &remainder_len, remainder_val,
3432 xi.val, xi.len, precision,
3433 yi.val, yi.len, yi.precision, sgn,
3434 overflow));
3435 remainder.set_len (remainder_len);
3436
3437 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
3438 return remainder + y;
3439 return remainder;
3440}
3441
3442/* Compute X / Y, rounding towards -inf, and return the remainder.
3443 Treat X and Y as unsigned values. */
3444/* ??? Why do we have both this and umod_trunc. Aren't they the same? */
3445template <typename T1, typename T2>
3446inline WI_BINARY_RESULT (T1, T2)
3447wi::umod_floor (const T1 &x, const T2 &y)
3448{
3449 return mod_floor (x, y, UNSIGNED);
3450}
3451
3452/* Compute X / Y, rounding towards +inf, and return the remainder.
3453 Treat X and Y as having the signedness given by SGN. Indicate
3454 in *OVERFLOW if the division overflows. */
3455template <typename T1, typename T2>
3456inline WI_BINARY_RESULT (T1, T2)
3457wi::mod_ceil (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3458{
3459 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3460 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3461 unsigned int precision = get_precision (quotient);
3462 WIDE_INT_REF_FOR (T1) xi (x, precision);
3463 WIDE_INT_REF_FOR (T2) yi (y);
3464
3465 unsigned int remainder_len;
3466 if (quotient.needs_write_val_arg)
3467 {
3468 unsigned int est_len;
3469 if (sgn == UNSIGNED && xi.val[xi.len - 1] < 0)
3470 est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
3471 else
3472 est_len = xi.len + 1;
3473 quotient_val = quotient.write_val (est_len);
3474 remainder_val = remainder.write_val (est_len);
3475 }
3476 quotient.set_len (divmod_internal (quotient_val,
3477 &remainder_len, remainder_val,
3478 xi.val, xi.len, precision,
3479 yi.val, yi.len, yi.precision, sgn,
3480 overflow));
3481 remainder.set_len (remainder_len);
3482
3483 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
3484 return remainder - y;
3485 return remainder;
3486}
3487
3488/* Compute X / Y, rounding towards nearest with ties away from zero,
3489 and return the remainder. Treat X and Y as having the signedness
3490 given by SGN. Indicate in *OVERFLOW if the division overflows. */
3491template <typename T1, typename T2>
3492inline WI_BINARY_RESULT (T1, T2)
3493wi::mod_round (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3494{
3495 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3496 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3497 unsigned int precision = get_precision (quotient);
3498 WIDE_INT_REF_FOR (T1) xi (x, precision);
3499 WIDE_INT_REF_FOR (T2) yi (y);
3500
3501 unsigned int remainder_len;
3502 if (quotient.needs_write_val_arg)
3503 {
3504 unsigned int est_len;
3505 if (sgn == UNSIGNED && xi.val[xi.len - 1] < 0)
3506 est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
3507 else
3508 est_len = xi.len + 1;
3509 quotient_val = quotient.write_val (est_len);
3510 remainder_val = remainder.write_val (est_len);
3511 }
3512 quotient.set_len (divmod_internal (quotient_val,
3513 &remainder_len, remainder_val,
3514 xi.val, xi.len, precision,
3515 yi.val, yi.len, yi.precision, sgn,
3516 overflow));
3517 remainder.set_len (remainder_len);
3518
3519 if (remainder != 0)
3520 {
3521 if (sgn == SIGNED)
3522 {
3523 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
3524 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
3525 {
3526 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
3527 return remainder + y;
3528 else
3529 return remainder - y;
3530 }
3531 }
3532 else
3533 {
3534 if (wi::geu_p (remainder, wi::sub (y, remainder)))
3535 return remainder - y;
3536 }
3537 }
3538 return remainder;
3539}
3540
3541/* Return true if X is a multiple of Y. Treat X and Y as having the
3542 signedness given by SGN. */
3543template <typename T1, typename T2>
3544inline bool
3545wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn)
3546{
3547 return wi::mod_trunc (x, y, sgn) == 0;
3548}
3549
3550/* Return true if X is a multiple of Y, storing X / Y in *RES if so.
3551 Treat X and Y as having the signedness given by SGN. */
3552template <typename T1, typename T2>
3553inline bool
3554wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn,
3555 WI_BINARY_RESULT (T1, T2) *res)
3556{
3557 WI_BINARY_RESULT (T1, T2) remainder;
3558 WI_BINARY_RESULT (T1, T2) quotient
3559 = divmod_trunc (x, y, sgn, &remainder);
3560 if (remainder == 0)
3561 {
3562 *res = quotient;
3563 return true;
3564 }
3565 return false;
3566}
3567
3568/* Return X << Y. Return 0 if Y is greater than or equal to
3569 the precision of X. */
3570template <typename T1, typename T2>
3572wi::lshift (const T1 &x, const T2 &y)
3573{
3574 WI_UNARY_RESULT_VAR (result, val, T1, x);
3575 unsigned int precision = get_precision (result);
3576 WIDE_INT_REF_FOR (T1) xi (x, precision);
3577 WIDE_INT_REF_FOR (T2) yi (y);
3578 /* Handle the simple cases quickly. */
3579 if (geu_p (yi, precision))
3580 {
3581 if (result.needs_write_val_arg)
3582 val = result.write_val (1);
3583 val[0] = 0;
3584 result.set_len (1);
3585 }
3586 else
3587 {
3588 unsigned int shift = yi.to_uhwi ();
3589 if (result.needs_write_val_arg)
3590 val = result.write_val (xi.len + shift / HOST_BITS_PER_WIDE_INT + 1);
3591 /* For fixed-precision integers like offset_int and widest_int,
3592 handle the case where the shift value is constant and the
3593 result is a single nonnegative HWI (meaning that we don't
3594 need to worry about val[1]). This is particularly common
3595 for converting a byte count to a bit count.
3596
3597 For variable-precision integers like wide_int, handle HWI
3598 and sub-HWI integers inline. */
3599 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
3601 && xi.len == 1
3602 && IN_RANGE (xi.val[0], 0, HOST_WIDE_INT_MAX >> shift))
3604 {
3605 val[0] = xi.ulow () << shift;
3606 result.set_len (1);
3607 }
3608 else
3609 result.set_len (lshift_large (val, xi.val, xi.len,
3610 precision, shift));
3611 }
3612 return result;
3613}
3614
3615/* Return X >> Y, using a logical shift. Return 0 if Y is greater than
3616 or equal to the precision of X. */
3617template <typename T1, typename T2>
3618inline WI_UNARY_RESULT (T1)
3619wi::lrshift (const T1 &x, const T2 &y)
3620{
3621 WI_UNARY_RESULT_VAR (result, val, T1, x);
3622 /* Do things in the precision of the input rather than the output,
3623 since the result can be no larger than that. */
3624 WIDE_INT_REF_FOR (T1) xi (x);
3625 WIDE_INT_REF_FOR (T2) yi (y);
3626 /* Handle the simple cases quickly. */
3627 if (geu_p (yi, xi.precision))
3628 {
3629 if (result.needs_write_val_arg)
3630 val = result.write_val (1);
3631 val[0] = 0;
3632 result.set_len (1);
3633 }
3634 else
3635 {
3636 unsigned int shift = yi.to_uhwi ();
3637 if (result.needs_write_val_arg)
3638 {
3639 unsigned int est_len = xi.len;
3640 if (xi.val[xi.len - 1] < 0 && shift)
3641 /* Logical right shift of sign-extended value might need a very
3642 large precision e.g. for widest_int. */
3643 est_len = CEIL (xi.precision - shift, HOST_BITS_PER_WIDE_INT) + 1;
3644 val = result.write_val (est_len);
3645 }
3646 /* For fixed-precision integers like offset_int and widest_int,
3647 handle the case where the shift value is constant and the
3648 shifted value is a single nonnegative HWI (meaning that all
3649 bits above the HWI are zero). This is particularly common
3650 for converting a bit count to a byte count.
3651
3652 For variable-precision integers like wide_int, handle HWI
3653 and sub-HWI integers inline. */
3654 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
3655 ? (shift < HOST_BITS_PER_WIDE_INT
3656 && xi.len == 1
3657 && xi.val[0] >= 0)
3658 : xi.precision <= HOST_BITS_PER_WIDE_INT)
3659 {
3660 val[0] = xi.to_uhwi () >> shift;
3661 result.set_len (1);
3662 }
3663 else
3664 result.set_len (lrshift_large (val, xi.val, xi.len, xi.precision,
3665 get_precision (result), shift));
3666 }
3667 return result;
3668}
3669
3670/* Return X >> Y, using an arithmetic shift. Return a sign mask if
3671 Y is greater than or equal to the precision of X. */
3672template <typename T1, typename T2>
3673inline WI_UNARY_RESULT (T1)
3674wi::arshift (const T1 &x, const T2 &y)
3675{
3676 WI_UNARY_RESULT_VAR (result, val, T1, x);
3677 /* Do things in the precision of the input rather than the output,
3678 since the result can be no larger than that. */
3679 WIDE_INT_REF_FOR (T1) xi (x);
3680 WIDE_INT_REF_FOR (T2) yi (y);
3681 if (result.needs_write_val_arg)
3682 val = result.write_val (xi.len);
3683 /* Handle the simple cases quickly. */
3684 if (geu_p (yi, xi.precision))
3685 {
3686 val[0] = sign_mask (x);
3687 result.set_len (1);
3688 }
3689 else
3690 {
3691 unsigned int shift = yi.to_uhwi ();
3692 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
3693 {
3694 val[0] = sext_hwi (xi.ulow () >> shift, xi.precision - shift);
3695 result.set_len (1, true);
3696 }
3697 else
3698 result.set_len (arshift_large (val, xi.val, xi.len, xi.precision,
3699 get_precision (result), shift));
3700 }
3701 return result;
3702}
3703
3704/* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
3705 logical shift otherwise. */
3706template <typename T1, typename T2>
3707inline WI_UNARY_RESULT (T1)
3708wi::rshift (const T1 &x, const T2 &y, signop sgn)
3709{
3710 if (sgn == UNSIGNED)
3711 return lrshift (x, y);
3712 else
3713 return arshift (x, y);
3714}
3715
3716/* Return the result of rotating the low WIDTH bits of X left by Y
3717 bits and zero-extending the result. Use a full-width rotate if
3718 WIDTH is zero. */
3719template <typename T1, typename T2>
3720WI_UNARY_RESULT (T1)
3721wi::lrotate (const T1 &x, const T2 &y, unsigned int width)
3722{
3723 unsigned int precision = get_binary_precision (x, x);
3724 if (width == 0)
3725 width = precision;
3726 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
3727 WI_UNARY_RESULT (T1) left = wi::lshift (x, ymod);
3728 WI_UNARY_RESULT (T1) right
3729 = wi::lrshift (width != precision ? wi::zext (x, width) : x,
3730 wi::sub (width, ymod));
3731 if (width != precision)
3732 return wi::zext (left, width) | right;
3733 return left | right;
3734}
3735
3736/* Return the result of rotating the low WIDTH bits of X right by Y
3737 bits and zero-extending the result. Use a full-width rotate if
3738 WIDTH is zero. */
3739template <typename T1, typename T2>
3740WI_UNARY_RESULT (T1)
3741wi::rrotate (const T1 &x, const T2 &y, unsigned int width)
3742{
3743 unsigned int precision = get_binary_precision (x, x);
3744 if (width == 0)
3745 width = precision;
3746 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
3747 WI_UNARY_RESULT (T1) right
3748 = wi::lrshift (width != precision ? wi::zext (x, width) : x, ymod);
3749 WI_UNARY_RESULT (T1) left = wi::lshift (x, wi::sub (width, ymod));
3750 if (width != precision)
3751 return wi::zext (left, width) | right;
3752 return left | right;
3753}
3754
3755/* Return 0 if the number of 1s in X is even and 1 if the number of 1s
3756 is odd. */
3757inline int
3759{
3760 return popcount (x) & 1;
3761}
3762
3763/* Extract WIDTH bits from X, starting at BITPOS. */
3764template <typename T>
3765inline unsigned HOST_WIDE_INT
3766wi::extract_uhwi (const T &x, unsigned int bitpos, unsigned int width)
3767{
3768 unsigned precision = get_precision (x);
3769 if (precision < bitpos + width)
3770 precision = bitpos + width;
3771 WIDE_INT_REF_FOR (T) xi (x, precision);
3772
3773 /* Handle this rare case after the above, so that we assert about
3774 bogus BITPOS values. */
3775 if (width == 0)
3776 return 0;
3777
3778 unsigned int start = bitpos / HOST_BITS_PER_WIDE_INT;
3779 unsigned int shift = bitpos % HOST_BITS_PER_WIDE_INT;
3780 unsigned HOST_WIDE_INT res = xi.elt (start);
3781 res >>= shift;
3782 if (shift + width > HOST_BITS_PER_WIDE_INT)
3783 {
3784 unsigned HOST_WIDE_INT upper = xi.elt (start + 1);
3785 res |= upper << (-shift % HOST_BITS_PER_WIDE_INT);
3786 }
3787 return zext_hwi (res, width);
3788}
3789
3790/* Return the minimum precision needed to store X with sign SGN. */
3791template <typename T>
3792inline unsigned int
3793wi::min_precision (const T &x, signop sgn)
3794{
3795 if (sgn == SIGNED)
3796 return get_precision (x) - clrsb (x);
3797 else
3798 return get_precision (x) - clz (x);
3799}
3800
3801#define SIGNED_BINARY_PREDICATE(OP, F) \
3802 template <typename T1, typename T2> \
3803 inline WI_SIGNED_BINARY_PREDICATE_RESULT (T1, T2) \
3804 OP (const T1 &x, const T2 &y) \
3805 { \
3806 return wi::F (x, y); \
3807 }
3808
3809SIGNED_BINARY_PREDICATE (operator <, lts_p)
3810SIGNED_BINARY_PREDICATE (operator <=, les_p)
3811SIGNED_BINARY_PREDICATE (operator >, gts_p)
3812SIGNED_BINARY_PREDICATE (operator >=, ges_p)
3813
3814#undef SIGNED_BINARY_PREDICATE
3815
3816#define UNARY_OPERATOR(OP, F) \
3817 template<typename T> \
3818 WI_UNARY_RESULT (generic_wide_int<T>) \
3819 OP (const generic_wide_int<T> &x) \
3820 { \
3821 return wi::F (x); \
3822 }
3823
3824#define BINARY_PREDICATE(OP, F) \
3825 template<typename T1, typename T2> \
3826 WI_BINARY_PREDICATE_RESULT (T1, T2) \
3827 OP (const T1 &x, const T2 &y) \
3828 { \
3829 return wi::F (x, y); \
3830 }
3831
3832#define BINARY_OPERATOR(OP, F) \
3833 template<typename T1, typename T2> \
3834 WI_BINARY_OPERATOR_RESULT (T1, T2) \
3835 OP (const T1 &x, const T2 &y) \
3836 { \
3837 return wi::F (x, y); \
3838 }
3839
3840#define SHIFT_OPERATOR(OP, F) \
3841 template<typename T1, typename T2> \
3842 WI_BINARY_OPERATOR_RESULT (T1, T1) \
3843 OP (const T1 &x, const T2 &y) \
3844 { \
3845 return wi::F (x, y); \
3846 }
3847
3848UNARY_OPERATOR (operator ~, bit_not)
3849UNARY_OPERATOR (operator -, neg)
3850BINARY_PREDICATE (operator ==, eq_p)
3851BINARY_PREDICATE (operator !=, ne_p)
3852BINARY_OPERATOR (operator &, bit_and)
3853BINARY_OPERATOR (operator |, bit_or)
3854BINARY_OPERATOR (operator ^, bit_xor)
3855BINARY_OPERATOR (operator +, add)
3856BINARY_OPERATOR (operator -, sub)
3857BINARY_OPERATOR (operator *, mul)
3858SHIFT_OPERATOR (operator <<, lshift)
3859
3860#undef UNARY_OPERATOR
3861#undef BINARY_PREDICATE
3862#undef BINARY_OPERATOR
3863#undef SHIFT_OPERATOR
3864
3865template <typename T1, typename T2>
3867operator >> (const T1 &x, const T2 &y)
3868{
3869 return wi::arshift (x, y);
3870}
3871
3872template <typename T1, typename T2>
3873inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3874operator / (const T1 &x, const T2 &y)
3875{
3876 return wi::sdiv_trunc (x, y);
3877}
3878
3879template <typename T1, typename T2>
3880inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3881operator % (const T1 &x, const T2 &y)
3882{
3883 return wi::smod_trunc (x, y);
3884}
3885
3886void gt_ggc_mx (generic_wide_int <wide_int_storage> *) = delete;
3887void gt_pch_nx (generic_wide_int <wide_int_storage> *) = delete;
3888void gt_pch_nx (generic_wide_int <wide_int_storage> *,
3889 gt_pointer_operator, void *) = delete;
3890
3891template<int N>
3892void
3893gt_ggc_mx (generic_wide_int <fixed_wide_int_storage <N> > *)
3894{
3895}
3896
3897template<int N>
3898void
3899gt_pch_nx (generic_wide_int <fixed_wide_int_storage <N> > *)
3900{
3901}
3902
3903template<int N>
3904void
3905gt_pch_nx (generic_wide_int <fixed_wide_int_storage <N> > *,
3906 gt_pointer_operator, void *)
3907{
3908}
3909
3910template<int N>
3911void gt_ggc_mx (generic_wide_int <widest_int_storage <N> > *) = delete;
3912
3913template<int N>
3914void gt_pch_nx (generic_wide_int <widest_int_storage <N> > *) = delete;
3915
3916template<int N>
3917void gt_pch_nx (generic_wide_int <widest_int_storage <N> > *,
3918 gt_pointer_operator, void *) = delete;
3919
3920template<int N>
3921void
3922gt_ggc_mx (trailing_wide_ints <N> *)
3923{
3924}
3925
3926template<int N>
3927void
3928gt_pch_nx (trailing_wide_ints <N> *)
3929{
3930}
3931
3932template<int N>
3933void
3934gt_pch_nx (trailing_wide_ints <N> *, gt_pointer_operator, void *)
3935{
3936}
3937
3938namespace wi
3939{
3940 /* Used for overloaded functions in which the only other acceptable
3941 scalar type is a pointer. It stops a plain 0 from being treated
3942 as a null pointer. */
3943 struct never_used1 {};
3944 struct never_used2 {};
3945
3946 wide_int min_value (unsigned int, signop);
3949 wide_int max_value (unsigned int, signop);
3952
3953 /* FIXME: this is target dependent, so should be elsewhere.
3954 It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
3955 wide_int from_buffer (const unsigned char *, unsigned int);
3956
3957#ifndef GENERATOR_FILE
3958 void to_mpz (const wide_int_ref &, mpz_t, signop);
3959#endif
3960
3961 wide_int mask (unsigned int, bool, unsigned int);
3962 wide_int shifted_mask (unsigned int, unsigned int, bool, unsigned int);
3963 wide_int set_bit_in_zero (unsigned int, unsigned int);
3964 wide_int insert (const wide_int &x, const wide_int &y, unsigned int,
3965 unsigned int);
3966 wide_int round_down_for_mask (const wide_int &, const wide_int &);
3967 wide_int round_up_for_mask (const wide_int &, const wide_int &);
3968
3969 wide_int mod_inv (const wide_int &a, const wide_int &b);
3970
3971 template <typename T>
3972 T mask (unsigned int, bool);
3973
3974 template <typename T>
3975 T shifted_mask (unsigned int, unsigned int, bool);
3976
3977 template <typename T>
3978 T set_bit_in_zero (unsigned int);
3979
3980 unsigned int mask (HOST_WIDE_INT *, unsigned int, bool, unsigned int);
3981 unsigned int shifted_mask (HOST_WIDE_INT *, unsigned int, unsigned int,
3982 bool, unsigned int);
3983 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
3984 unsigned int, unsigned int, bool);
3985}
3986
3987/* Return a PRECISION-bit integer in which the low WIDTH bits are set
3988 and the other bits are clear, or the inverse if NEGATE_P. */
3989inline wide_int
3990wi::mask (unsigned int width, bool negate_p, unsigned int precision)
3991{
3993 result.set_len (mask (result.write_val (0), width, negate_p, precision));
3994 return result;
3995}
3996
3997/* Return a PRECISION-bit integer in which the low START bits are clear,
3998 the next WIDTH bits are set, and the other bits are clear,
3999 or the inverse if NEGATE_P. */
4000inline wide_int
4001wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p,
4002 unsigned int precision)
4003{
4005 result.set_len (shifted_mask (result.write_val (0), start, width, negate_p,
4006 precision));
4007 return result;
4008}
4009
4010/* Return a PRECISION-bit integer in which bit BIT is set and all the
4011 others are clear. */
4012inline wide_int
4013wi::set_bit_in_zero (unsigned int bit, unsigned int precision)
4014{
4015 return shifted_mask (bit, 1, false, precision);
4016}
4017
4018/* Return an integer of type T in which the low WIDTH bits are set
4019 and the other bits are clear, or the inverse if NEGATE_P. */
4020template <typename T>
4021inline T
4022wi::mask (unsigned int width, bool negate_p)
4023{
4025 T result;
4026 result.set_len (mask (result.write_val (width / HOST_BITS_PER_WIDE_INT + 1),
4027 width, negate_p, wi::int_traits <T>::precision));
4028 return result;
4029}
4030
4031/* Return an integer of type T in which the low START bits are clear,
4032 the next WIDTH bits are set, and the other bits are clear, or the
4033 inverse if NEGATE_P. */
4034template <typename T>
4035inline T
4036wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p)
4037{
4039 T result;
4040 unsigned int prec = wi::int_traits <T>::precision;
4041 unsigned int est_len
4042 = result.needs_write_val_arg
4043 ? ((start + (width > prec - start ? prec - start : width))
4044 / HOST_BITS_PER_WIDE_INT + 1) : 0;
4045 result.set_len (shifted_mask (result.write_val (est_len), start, width,
4046 negate_p, prec));
4047 return result;
4048}
4049
4050/* Return an integer of type T in which bit BIT is set and all the
4051 others are clear. */
4052template <typename T>
4053inline T
4054wi::set_bit_in_zero (unsigned int bit)
4055{
4056 return shifted_mask <T> (bit, 1, false);
4057}
4058
4059/* Accumulate a set of overflows into OVERFLOW. */
4060
4061inline void
4063 wi::overflow_type suboverflow)
4064{
4065 if (!suboverflow)
4066 return;
4067 if (!overflow)
4068 overflow = suboverflow;
4069 else if (overflow != suboverflow)
4070 overflow = wi::OVF_UNKNOWN;
4071}
4072
4073#endif /* WIDE_INT_H */
void gt_pch_nx(bbitmap< N > *)
Definition bbitmap.h:226
void gt_ggc_mx(bbitmap< N > *)
Definition bbitmap.h:220
Definition wide-int.h:1353
static unsigned int
Definition wide-int.h:1371
static unsigned bool
Definition wide-int.h:1372
static FIXED_WIDE_INT(N) from(const wide_int_ref &
HOST_WIDE_INT * write_val(unsigned int)
Definition wide-int.h:1426
fixed_wide_int_storage()=default
const HOST_WIDE_INT * get_val() const
Definition wide-int.h:1412
unsigned int get_precision() const
Definition wide-int.h:1405
void set_len(unsigned int, bool=false)
Definition wide-int.h:1433
HOST_WIDE_INT val[WIDE_INT_MAX_HWIS(N)]
Definition wide-int.h:1355
static FIXED_WIDE_INT(N) from_array(const HOST_WIDE_INT *
unsigned int len
Definition wide-int.h:1356
static signop
Definition wide-int.h:1370
unsigned int get_len() const
Definition wide-int.h:1419
Definition wide-int.h:776
HOST_WIDE_INT to_shwi() const
Definition wide-int.h:874
generic_wide_int()
Definition wide-int.h:842
HOST_WIDE_INT elt(unsigned int) const
Definition wide-int.h:972
static const bool needs_write_val_arg
Definition wide-int.h:838
void dump() const
Definition wide-int.h:1009
unsigned HOST_WIDE_INT to_uhwi() const
Definition wide-int.h:898
unsigned HOST_WIDE_INT ulow() const
Definition wide-int.h:955
unsigned HOST_WIDE_INT to_uhwi(unsigned int) const
Definition wide-int.h:887
HOST_WIDE_INT to_shwi(unsigned int) const
Definition wide-int.h:863
unsigned HOST_WIDE_INT uhigh() const
Definition wide-int.h:964
generic_wide_int(const T &)
Definition wide-int.h:846
HOST_WIDE_INT to_short_addr() const
Definition wide-int.h:909
HOST_WIDE_INT sext_elt(unsigned int) const
Definition wide-int.h:984
generic_wide_int(const T &, unsigned int)
Definition wide-int.h:853
HOST_WIDE_INT shigh() const
Definition wide-int.h:946
HOST_WIDE_INT sign_mask() const
Definition wide-int.h:917
HOST_WIDE_INT slow() const
Definition wide-int.h:937
Definition wide-int.h:1712
void set_len(unsigned int, bool=false)
Definition wide-int.h:1822
trailing_wide_int_storage(unsigned int, unsigned short *, HOST_WIDE_INT *)
Definition wide-int.h:1791
const HOST_WIDE_INT * get_val() const
Definition wide-int.h:1810
unsigned int m_precision
Definition wide-int.h:1716
trailing_wide_int_storage & operator=(const T &)
Definition wide-int.h:1832
unsigned int get_len() const
Definition wide-int.h:1798
unsigned int get_precision() const
Definition wide-int.h:1804
HOST_WIDE_INT * write_val(unsigned int)
Definition wide-int.h:1816
HOST_WIDE_INT * m_val
Definition wide-int.h:1723
unsigned short * m_len
Definition wide-int.h:1719
Definition wide-int.h:1965
signop sgn
Definition wide-int.h:1971
hwi_with_prec()
Definition wide-int.h:1967
unsigned int precision
Definition wide-int.h:1970
HOST_WIDE_INT val
Definition wide-int.h:1969
Definition wide-int.h:707
unsigned int precision
Definition wide-int.h:714
unsigned int len
Definition wide-int.h:713
storage_ref()
Definition wide-int.h:709
unsigned int get_len() const
Definition wide-int.h:732
const HOST_WIDE_INT * get_val() const
Definition wide-int.h:744
storage_ref(const HOST_WIDE_INT *, unsigned int, unsigned int)
const HOST_WIDE_INT * val
Definition wide-int.h:712
unsigned int get_precision() const
Definition wide-int.h:738
Definition wide-int.h:1058
HOST_WIDE_INT scratch[2]
Definition wide-int.h:1062
wide_int_ref_storage()
Definition wide-int.h:1065
Definition wide-int.h:1126
static wide_int create(unsigned int)
Definition wide-int.h:1313
const HOST_WIDE_INT * get_val() const
Definition wide-int.h:1258
wide_int_storage()
Definition wide-int.h:1177
static wide_int from(const wide_int_ref &, unsigned int, signop)
Definition wide-int.h:1289
~wide_int_storage()
Definition wide-int.h:1206
unsigned int get_precision() const
Definition wide-int.h:1252
unsigned int len
Definition wide-int.h:1133
void set_len(unsigned int, bool=false)
Definition wide-int.h:1276
unsigned int get_len() const
Definition wide-int.h:1264
HOST_WIDE_INT * write_val(unsigned int)
Definition wide-int.h:1270
union wide_int_storage::@332066064303330275027227150040015331132257367372 u
wide_int_storage & operator=(const wide_int_storage &)
Definition wide-int.h:1213
static wide_int from_array(const HOST_WIDE_INT *, unsigned int, unsigned int, bool=true)
Definition wide-int.h:1302
unsigned int precision
Definition wide-int.h:1134
Definition wide-int.h:1489
void set_len(unsigned int, bool=false)
Definition wide-int.h:1644
static unsigned bool
Definition wide-int.h:1517
const HOST_WIDE_INT * get_val() const
Definition wide-int.h:1613
union widest_int_storage::@161345252340175325254017364252252150142051207163 u
widest_int_storage()
Definition wide-int.h:1538
widest_int_storage & operator=(const widest_int_storage &)
Definition wide-int.h:1572
~widest_int_storage()
Definition wide-int.h:1564
static WIDEST_INT(N) from(const wide_int_ref &
unsigned int get_precision() const
Definition wide-int.h:1606
unsigned int len
Definition wide-int.h:1496
static signop
Definition wide-int.h:1515
unsigned int get_len() const
Definition wide-int.h:1620
HOST_WIDE_INT * write_val(unsigned int)
Definition wide-int.h:1627
#define GTY(x)
Definition coretypes.h:41
void(*) gt_pointer_operator(void *, void *, void *)
Definition coretypes.h:477
#define CHAR_BIT
Definition genautomata.cc:120
#define N
Definition gensupport.cc:202
#define HOST_WIDE_INT_1U
Definition hwint.h:53
#define HOST_WIDE_INT_PRINT_HEX
Definition hwint.h:96
HOST_WIDE_INT sext_hwi(HOST_WIDE_INT src, unsigned int prec)
Definition hwint.h:292
#define HOST_WIDE_INT_MAX
Definition hwint.h:258
#define HOST_BITS_PER_WIDE_INT
Definition hwint.h:36
unsigned HOST_WIDE_INT zext_hwi(unsigned HOST_WIDE_INT src, unsigned int prec)
Definition hwint.h:319
#define HOST_WIDE_INT_UC(X)
Definition hwint.h:49
Definition double-int.h:439
BINARY_FUNCTION udiv_ceil(const T1 &, const T2 &)
bool lts_p_large(const HOST_WIDE_INT *, unsigned int, unsigned int, const HOST_WIDE_INT *, unsigned int)
Definition wide-int.cc:479
unsigned int or_not_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, const HOST_WIDE_INT *, unsigned int, unsigned int)
Definition wide-int.cc:1067
int ctz(const wide_int_ref &)
Definition wide-int.cc:2230
BINARY_FUNCTION umod_trunc(const T1 &, const T2 &)
BINARY_FUNCTION umul(const T1 &, const T2 &, overflow_type *)
bool multiple_of_p(const T1 &, const T2 &, signop)
Definition wide-int.h:3545
BINARY_FUNCTION udiv_floor(const T1 &, const T2 &)
wide_int shifted_mask(unsigned int, unsigned int, bool, unsigned int)
Definition wide-int.h:4001
unsigned int get_binary_precision(const T1 &, const T2 &)
Definition wide-int.h:2173
poly_int< N, hwi_with_prec > uhwi(const poly_int< N, unsigned HOST_WIDE_INT > &a, unsigned int precision)
Definition poly-int.h:733
UNARY_FUNCTION ext(const T &, unsigned int, signop)
precision_type
Definition wide-int.h:387
@ INL_CONST_PRECISION
Definition wide-int.h:398
@ VAR_PRECISION
Definition wide-int.h:394
@ CONST_PRECISION
Definition wide-int.h:402
@ FLEXIBLE_PRECISION
Definition wide-int.h:391
wide_int min_value(machine_mode, signop)
Definition rtl.h:2380
unsigned int or_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, const HOST_WIDE_INT *, unsigned int, unsigned int)
Definition wide-int.cc:1009
wide_int mask(unsigned int, bool, unsigned int)
Definition wide-int.h:3990
unsigned int set_bit_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int, unsigned int)
Definition wide-int.cc:699
unsigned int bswap_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int)
Definition wide-int.cc:735
BINARY_FUNCTION div_trunc(const T1 &, const T2 &, signop, overflow_type *=0)
UNARY_PREDICATE neg_p(const T &, signop=SIGNED)
int cmps(const T1 &, const T2 &)
Definition wide-int.h:2442
BINARY_FUNCTION umod_floor(const T1 &, const T2 &)
BINARY_PREDICATE le_p(const T1 &, const T2 &, signop)
unsigned int lrshift_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int, unsigned int, unsigned int)
Definition wide-int.cc:2119
SHIFT_FUNCTION rrotate(const T1 &, const T2 &, unsigned int=0)
BINARY_FUNCTION div_ceil(const T1 &, const T2 &, signop, overflow_type *=0)
BINARY_FUNCTION sdiv_floor(const T1 &, const T2 &)
UNARY_PREDICATE fits_shwi_p(const T &)
HOST_WIDE_INT sign_mask(const T &)
Definition wide-int.h:2235
int clrsb(const wide_int_ref &)
Definition wide-int.cc:2202
UNARY_FUNCTION zext(const T &, unsigned int)
unsigned int xor_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, const HOST_WIDE_INT *, unsigned int, unsigned int)
Definition wide-int.cc:1125
BINARY_FUNCTION div_floor(const T1 &, const T2 &, signop, overflow_type *=0)
wide_int round_up_for_mask(const wide_int &, const wide_int &)
Definition wide-int.cc:2341
SHIFT_FUNCTION rshift(const T1 &, const T2 &, signop sgn)
SHIFT_FUNCTION arshift(const T1 &, const T2 &)
unsigned HOST_WIDE_INT extract_uhwi(const T &, unsigned int, unsigned int)
Definition wide-int.h:3766
BINARY_FUNCTION div_round(const T1 &, const T2 &, signop, overflow_type *=0)
BINARY_FUNCTION umax(const T1 &, const T2 &)
BINARY_FUNCTION smin(const T1 &, const T2 &)
bool only_sign_bit_p(const wide_int_ref &, unsigned int)
Definition wide-int.cc:2294
bool eq_p_large(const HOST_WIDE_INT *, unsigned int, const HOST_WIDE_INT *, unsigned int, unsigned int)
Definition wide-int.cc:449
BINARY_FUNCTION bit_and_not(const T1 &, const T2 &)
unsigned int from_array(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int, bool=true)
Definition wide-int.cc:143
BINARY_FUNCTION sub(const T1 &, const T2 &)
BINARY_FUNCTION mod_round(const T1 &, const T2 &, signop, overflow_type *=0)
BINARY_FUNCTION smod_trunc(const T1 &, const T2 &)
UNARY_FUNCTION bitreverse(const T &)
UNARY_FUNCTION set_bit(const T &, unsigned int)
unsigned int min_precision(const T &, signop)
Definition wide-int.h:3793
int cmpu(const T1 &, const T2 &)
Definition wide-int.h:2479
BINARY_PREDICATE ges_p(const T1 &, const T2 &)
BINARY_FUNCTION mod_ceil(const T1 &, const T2 &, signop, overflow_type *=0)
UNARY_FUNCTION neg(const T &)
int exact_log2(const wide_int_ref &)
Definition wide-int.cc:2246
wide_int from_buffer(const unsigned char *, unsigned int)
Definition wide-int.cc:156
BINARY_FUNCTION mul_high(const T1 &, const T2 &, signop)
hwi_with_prec minus_one(unsigned int)
Definition wide-int.h:2009
unsigned int arshift_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int, unsigned int, unsigned int)
Definition wide-int.cc:2153
hwi_with_prec zero(unsigned int)
Definition wide-int.h:2016
SHIFT_FUNCTION lrotate(const T1 &, const T2 &, unsigned int=0)
unsigned int and_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, const HOST_WIDE_INT *, unsigned int, unsigned int)
Definition wide-int.cc:894
int ffs(const wide_int_ref &)
Definition wide-int.cc:2286
int floor_log2(const wide_int_ref &)
Definition wide-int.cc:2278
BINARY_PREDICATE lt_p(const T1 &, const T2 &, signop)
unsigned int divmod_internal(HOST_WIDE_INT *, unsigned int *, HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int, const HOST_WIDE_INT *, unsigned int, unsigned int, signop, overflow_type *)
Definition wide-int.cc:1837
BINARY_FUNCTION bit_or(const T1 &, const T2 &)
BINARY_PREDICATE les_p(const T1 &, const T2 &)
unsigned int get_precision(const T &)
Definition wide-int.h:2164
unsigned int and_not_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, const HOST_WIDE_INT *, unsigned int, unsigned int)
Definition wide-int.cc:951
BINARY_FUNCTION bit_xor(const T1 &, const T2 &)
int cmpu_large(const HOST_WIDE_INT *, unsigned int, unsigned int, const HOST_WIDE_INT *, unsigned int)
Definition wide-int.cc:581
unsigned int bitreverse_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int)
Definition wide-int.cc:769
hwi_with_prec two(unsigned int)
Definition wide-int.h:2030
UNARY_FUNCTION bswap(const T &)
unsigned int add_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, const HOST_WIDE_INT *, unsigned int, unsigned int, signop, overflow_type *)
Definition wide-int.cc:1171
unsigned int lshift_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int, unsigned int)
Definition wide-int.cc:2052
Ca unsigned int precision
Definition poly-int.h:746
BINARY_PREDICATE ge_p(const T1 &, const T2 &, signop)
static void accumulate_overflow(overflow_type &, overflow_type)
Definition wide-int.h:4062
BINARY_PREDICATE lts_p(const T1 &, const T2 &)
BINARY_FUNCTION bit_and(const T1 &, const T2 &)
UNARY_FUNCTION abs(const T &)
wide_int insert(const wide_int &x, const wide_int &y, unsigned int, unsigned int)
Definition wide-int.cc:669
unsigned int sub_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, const HOST_WIDE_INT *, unsigned int, unsigned int, signop, overflow_type *)
Definition wide-int.cc:1637
BINARY_FUNCTION smul(const T1 &, const T2 &, overflow_type *)
Ca & a
Definition poly-int.h:745
UNARY_FUNCTION bit_not(const T &)
BINARY_PREDICATE geu_p(const T1 &, const T2 &)
wide_int max_value(machine_mode, signop)
Definition rtl.h:2388
UNARY_PREDICATE fits_uhwi_p(const T &)
unsigned int force_to_size(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int, unsigned int, signop sgn)
Definition wide-int.cc:364
int cmps_large(const HOST_WIDE_INT *, unsigned int, unsigned int, const HOST_WIDE_INT *, unsigned int)
Definition wide-int.cc:517
BINARY_FUNCTION mul(const T1 &, const T2 &)
wide_int round_down_for_mask(const wide_int &, const wide_int &)
Definition wide-int.cc:2315
BINARY_FUNCTION gcd(const T1 &, const T2 &, signop=UNSIGNED)
void to_mpz(const wide_int_ref &, mpz_t, signop)
Definition wide-int.cc:206
unsigned int mul_internal(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, const HOST_WIDE_INT *, unsigned int, unsigned int, signop, overflow_type *, bool)
Definition wide-int.cc:1325
BINARY_PREDICATE gts_p(const T1 &, const T2 &)
BINARY_FUNCTION umin(const T1 &, const T2 &)
void copy(T1 &, const T2 &)
Definition wide-int.h:2182
SHIFT_FUNCTION lrshift(const T1 &, const T2 &)
BINARY_PREDICATE gt_p(const T1 &, const T2 &, signop)
wide_int mod_inv(const wide_int &a, const wide_int &b)
Definition wide-int.cc:2374
BINARY_PREDICATE gtu_p(const T1 &, const T2 &)
UNARY_FUNCTION sext(const T &, unsigned int)
SHIFT_FUNCTION lshift(const T1 &, const T2 &)
int clz(const wide_int_ref &)
Definition wide-int.cc:2177
BINARY_FUNCTION udiv_trunc(const T1 &, const T2 &)
BINARY_PREDICATE ltu_p(const T1 &, const T2 &)
int cmp(const T1 &, const T2 &, signop)
Definition wide-int.h:2521
BINARY_FUNCTION bit_or_not(const T1 &, const T2 &)
BINARY_FUNCTION divmod_trunc(const T1 &, const T2 &, signop, WI_BINARY_RESULT(T1, T2) *)
BINARY_FUNCTION add(const T1 &, const T2 &)
BINARY_PREDICATE ne_p(const T1 &, const T2 &)
BINARY_PREDICATE leu_p(const T1 &, const T2 &)
int parity(const wide_int_ref &)
Definition wide-int.h:3758
unsigned int zext_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int, unsigned int)
Definition wide-int.cc:640
BINARY_FUNCTION smax(const T1 &, const T2 &)
BINARY_FUNCTION mod_trunc(const T1 &, const T2 &, signop, overflow_type *=0)
BINARY_FUNCTION min(const T1 &, const T2 &, signop)
i
Definition poly-int.h:750
bool ltu_p_large(const HOST_WIDE_INT *, unsigned int, unsigned int, const HOST_WIDE_INT *, unsigned int)
Definition wide-int.cc:554
BINARY_FUNCTION sdiv_trunc(const T1 &, const T2 &)
overflow_type
Definition wide-int.h:377
@ OVF_UNKNOWN
Definition wide-int.h:383
@ OVF_NONE
Definition wide-int.h:378
@ OVF_UNDERFLOW
Definition wide-int.h:379
@ OVF_OVERFLOW
Definition wide-int.h:380
BINARY_PREDICATE eq_p(const T1 &, const T2 &)
int popcount(const wide_int_ref &)
Definition wide-int.cc:1605
BINARY_FUNCTION mod_floor(const T1 &, const T2 &, signop, overflow_type *=0)
hwi_with_prec one(unsigned int)
Definition wide-int.h:2023
wide_int set_bit_in_zero(unsigned int, unsigned int)
Definition wide-int.h:4013
unsigned int sext_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int, unsigned int)
Definition wide-int.cc:613
BINARY_FUNCTION max(const T1 &, const T2 &, signop)
poly_int< N, hwi_with_prec > shwi(const poly_int< N, HOST_WIDE_INT > &a, unsigned int precision)
Definition poly-int.h:721
i
Definition poly-int.h:776
Ca const poly_int< N, Cb > & b
Definition poly-int.h:771
signop
Definition signop.h:28
@ UNSIGNED
Definition signop.h:30
@ SIGNED
Definition signop.h:29
Definition wide-int.h:1758
static size_t extra_size(unsigned int precision, unsigned int num_elements=N)
Definition wide-int.h:1875
trailing_wide_int operator[](unsigned int)
Definition wide-int.h:1855
size_t extra_size() const
Definition wide-int.h:1786
unsigned char m_num_elements
Definition wide-int.h:1767
unsigned short m_len[N]
Definition wide-int.h:1770
unsigned short m_max_len
Definition wide-int.h:1764
HOST_WIDE_INT m_val[1]
Definition wide-int.h:1774
unsigned int num_elements() const
Definition wide-int.h:1781
unsigned short m_precision
Definition wide-int.h:1761
typedef WIDE_INT_REF_FOR(trailing_wide_int_storage) const _reference
void set_precision(unsigned int precision, unsigned int num_elements=N)
Definition wide-int.h:1843
unsigned int get_precision() const
Definition wide-int.h:1780
result_type operator_result
Definition wide-int.h:533
STATIC_ASSERT(int_traits< T1 >::precision==int_traits< T2 >::precision)
result_type signed_shift_result_type
Definition wide-int.h:535
generic_wide_int< widest_int_storage< int_traits< T1 >::precision > > result_type
Definition wide-int.h:532
result_type signed_shift_result_type
Definition wide-int.h:509
generic_wide_int< widest_int_storage< int_traits< T1 >::precision > > result_type
Definition wide-int.h:506
result_type signed_shift_result_type
Definition wide-int.h:477
generic_wide_int< widest_int_storage< int_traits< T2 >::precision > > result_type
Definition wide-int.h:474
generic_wide_int< fixed_wide_int_storage< int_traits< T2 >::precision > > result_type
Definition wide-int.h:463
generic_wide_int< fixed_wide_int_storage< int_traits< T1 >::precision > > result_type
Definition wide-int.h:495
generic_wide_int< fixed_wide_int_storage< int_traits< T1 >::precision > > result_type
Definition wide-int.h:520
STATIC_ASSERT(int_traits< T1 >::precision==int_traits< T2 >::precision)
result_type operator_result
Definition wide-int.h:543
Definition wide-int.h:435
static const bool needs_write_val_arg
Definition wide-int.h:1383
static const bool is_sign_extended
Definition wide-int.h:1382
static const bool host_dependent_precision
Definition wide-int.h:1381
static FIXED_WIDE_INT(N) get_binary_result(const T1 &
static const unsigned int precision
Definition wide-int.h:1384
static const T2 &static unsigned int get_binary_precision(const T1 &, const T2 &)
static enum precision_type precision_type
Definition wide-int.h:1380
static wi::storage_ref decompose(HOST_WIDE_INT *, unsigned int, const generic_wide_int< storage > &)
Definition wide-int.h:1046
static unsigned int get_precision(const generic_wide_int< storage > &)
Definition wide-int.h:1038
static enum precision_type precision_type
Definition wide-int.h:2064
static unsigned int get_precision(const wi::hwi_with_prec &)
Definition wide-int.h:2077
static const bool is_sign_extended
Definition wide-int.h:2068
static const bool host_dependent_precision
Definition wide-int.h:2067
static const bool needs_write_val_arg
Definition wide-int.h:2069
static wi::storage_ref decompose(HOST_WIDE_INT *, unsigned int, const wi::hwi_with_prec &)
Definition wide-int.h:2084
static const bool is_sign_extended
Definition wide-int.h:1110
static const bool host_dependent_precision
Definition wide-int.h:1109
static const bool needs_write_val_arg
Definition wide-int.h:1111
static enum precision_type precision_type
Definition wide-int.h:1108
static const bool needs_write_val_arg
Definition wide-int.h:1169
static const bool host_dependent_precision
Definition wide-int.h:1167
static enum precision_type precision_type
Definition wide-int.h:1165
static const bool is_sign_extended
Definition wide-int.h:1168
static unsigned int get_binary_precision(const T1 &, const T2 &)
Definition wide-int.h:1338
static wide_int get_binary_result(const T1 &, const T2 &)
Definition wide-int.h:1325
static const bool needs_write_val_arg
Definition wide-int.h:1528
static const bool host_dependent_precision
Definition wide-int.h:1526
static enum precision_type precision_type
Definition wide-int.h:1525
static WIDEST_INT(N) get_binary_result(const T1 &
static const bool is_sign_extended
Definition wide-int.h:1527
static const unsigned int precision
Definition wide-int.h:1529
static const T2 &static unsigned int get_binary_precision(const T1 &, const T2 &)
Definition wide-int.h:427
static hwi_with_prec zero(const T &)
Definition wide-int.h:2041
static int zero(const T &)
Definition wide-int.h:2042
Definition wide-int.h:3943
Definition wide-int.h:3944
Definition wide-int.h:1895
static const bool is_sign_extended
Definition wide-int.h:1898
static const bool host_dependent_precision
Definition wide-int.h:1897
static wi::storage_ref decompose(HOST_WIDE_INT *, unsigned int, T)
Definition wide-int.h:1914
static const bool needs_write_val_arg
Definition wide-int.h:1899
static unsigned int get_precision(T)
Definition wide-int.h:1907
static enum precision_type precision_type
Definition wide-int.h:1896
#define gcc_assert(EXPR)
Definition system.h:772
#define CEIL(x, y)
Definition system.h:403
#define IN_RANGE(VALUE, LOWER, UPPER)
Definition system.h:327
#define STATIC_CONSTANT_P(X)
Definition system.h:815
#define UNLIKELY(x)
Definition system.h:721
#define STATIC_ASSERT(X)
Definition system.h:822
#define gcc_checking_assert(EXPR)
Definition system.h:779
#define MAX(X, Y)
Definition system.h:400
#define LIKELY(x)
Definition system.h:720
generic_wide_int< widest_int_storage< WIDEST_INT_MAX_PRECISION > > widest_int
Definition wide-int.h:345
#define WIDEST_INT(N)
Definition wide-int.h:1484
#define UNARY_PREDICATE
Definition wide-int.h:560
#define ASSIGNMENT_OPERATOR(OP, F)
Definition wide-int.h:805
#define SHIFT_FUNCTION
Definition wide-int.h:568
#define WIDE_INT_REF_FOR(T)
Definition wide-int.h:362
generic_wide_int< wide_int_ref_storage< false > > wide_int_ref
Definition wide-int.h:353
generic_wide_int< widest_int_storage< WIDEST_INT_MAX_PRECISION *2 > > widest2_int
Definition wide-int.h:346
generic_wide_int< trailing_wide_int_storage > trailing_wide_int
Definition wide-int.h:1739
#define WI_SIGNED_SHIFT_RESULT(T1, T2)
Definition wide-int.h:299
#define WIDE_INT_MAX_ELTS
Definition wide-int.h:252
#define BINARY_OPERATOR(OP, F)
Definition wide-int.h:3832
#define ADDR_MAX_PRECISION
Definition wide-int.h:276
#define SIGNED_BINARY_PREDICATE(OP, F)
Definition wide-int.h:3801
#define INCDEC_OPERATOR(OP, DELTA)
Definition wide-int.h:814
#define WI_BINARY_RESULT(T1, T2)
Definition wide-int.h:289
#define WI_UNARY_RESULT_VAR(RESULT, VAL, T, X)
Definition wide-int.h:329
#define BINARY_FUNCTION
Definition wide-int.h:566
#define SHIFT_ASSIGNMENT_OPERATOR(OP, OP2)
Definition wide-int.h:810
generic_wide_int< wide_int_storage > wide_int
Definition wide-int.h:343
const T2 & y
Definition wide-int.h:3868
#define FIXED_WIDE_INT(N)
Definition wide-int.h:340
#define WI_BINARY_RESULT_VAR(RESULT, VAL, T1, X, T2, Y)
Definition wide-int.h:323
STATIC_ASSERT(WIDE_INT_MAX_INL_ELTS< WIDE_INT_MAX_ELTS)
#define BINARY_PREDICATE
Definition wide-int.h:564
#define WIDE_INT_MAX_INL_ELTS
Definition wide-int.h:243
#define UNARY_OPERATOR(OP, F)
Definition wide-int.h:3816
#define WIDE_INT_MAX_INL_PRECISION
Definition wide-int.h:247
#define WI_UNARY_RESULT(T)
Definition wide-int.h:315
#define SHIFT_OPERATOR(OP, F)
Definition wide-int.h:3840
#define WIDE_INT_MAX_HWIS(PRECISION)
Definition wide-int.h:284
#define UNARY_FUNCTION
Definition wide-int.h:562