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 if (len > 0)
2186 {
2187 HOST_WIDE_INT *xval = x.write_val (len);
2188 const HOST_WIDE_INT *yval = y.get_val ();
2189 unsigned int i = 0;
2190 do
2191 xval[i] = yval[i];
2192 while (++i < len);
2193 }
2194 /* For widest_int write_val is called with an exact value, not
2195 upper bound for len, so nothing is needed further. */
2196 if (!wi::int_traits <T1>::needs_write_val_arg)
2197 x.set_len (len, y.is_sign_extended);
2198}
2199
2200/* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
2201template <typename T>
2202inline bool
2203wi::fits_shwi_p (const T &x)
2204{
2205 WIDE_INT_REF_FOR (T) xi (x);
2206 return xi.len == 1;
2207}
2208
2209/* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
2210 precision. */
2211template <typename T>
2212inline bool
2213wi::fits_uhwi_p (const T &x)
2214{
2215 WIDE_INT_REF_FOR (T) xi (x);
2216 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
2217 return true;
2218 if (xi.len == 1)
2219 return xi.slow () >= 0;
2220 return xi.len == 2 && xi.uhigh () == 0;
2221}
2222
2223/* Return true if X is negative based on the interpretation of SGN.
2224 For UNSIGNED, this is always false. */
2225template <typename T>
2226inline bool
2227wi::neg_p (const T &x, signop sgn)
2228{
2229 WIDE_INT_REF_FOR (T) xi (x);
2230 if (sgn == UNSIGNED)
2231 return false;
2232 return xi.sign_mask () < 0;
2233}
2234
2235/* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
2236template <typename T>
2237inline HOST_WIDE_INT
2238wi::sign_mask (const T &x)
2239{
2240 WIDE_INT_REF_FOR (T) xi (x);
2241 return xi.sign_mask ();
2242}
2243
2244/* Return true if X == Y. X and Y must be binary-compatible. */
2245template <typename T1, typename T2>
2246inline bool
2247wi::eq_p (const T1 &x, const T2 &y)
2248{
2249 unsigned int precision = get_binary_precision (x, y);
2250 WIDE_INT_REF_FOR (T1) xi (x, precision);
2251 WIDE_INT_REF_FOR (T2) yi (y, precision);
2252 if (xi.is_sign_extended && yi.is_sign_extended)
2253 {
2254 /* This case reduces to array equality. */
2255 if (xi.len != yi.len)
2256 return false;
2257 unsigned int i = 0;
2258 do
2259 if (xi.val[i] != yi.val[i])
2260 return false;
2261 while (++i != xi.len);
2262 return true;
2263 }
2264 if (LIKELY (yi.len == 1))
2265 {
2266 /* XI is only equal to YI if it too has a single HWI. */
2267 if (xi.len != 1)
2268 return false;
2269 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
2270 with 0 are simple. */
2271 if (STATIC_CONSTANT_P (yi.val[0] == 0))
2272 return xi.val[0] == 0;
2273 /* Otherwise flush out any excess bits first. */
2274 unsigned HOST_WIDE_INT diff = xi.val[0] ^ yi.val[0];
2275 int excess = HOST_BITS_PER_WIDE_INT - precision;
2276 if (excess > 0)
2277 diff <<= excess;
2278 return diff == 0;
2279 }
2280 return eq_p_large (xi.val, xi.len, yi.val, yi.len, precision);
2281}
2282
2283/* Return true if X != Y. X and Y must be binary-compatible. */
2284template <typename T1, typename T2>
2285inline bool
2286wi::ne_p (const T1 &x, const T2 &y)
2287{
2288 return !eq_p (x, y);
2289}
2290
2291/* Return true if X < Y when both are treated as signed values. */
2292template <typename T1, typename T2>
2293inline bool
2294wi::lts_p (const T1 &x, const T2 &y)
2295{
2296 unsigned int precision = get_binary_precision (x, y);
2297 WIDE_INT_REF_FOR (T1) xi (x, precision);
2298 WIDE_INT_REF_FOR (T2) yi (y, precision);
2299 /* We optimize x < y, where y is 64 or fewer bits. */
2300 if (wi::fits_shwi_p (yi))
2301 {
2302 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
2303 if (STATIC_CONSTANT_P (yi.val[0] == 0))
2304 return neg_p (xi);
2305 /* If x fits directly into a shwi, we can compare directly. */
2306 if (wi::fits_shwi_p (xi))
2307 return xi.to_shwi () < yi.to_shwi ();
2308 /* If x doesn't fit and is negative, then it must be more
2309 negative than any value in y, and hence smaller than y. */
2310 if (neg_p (xi))
2311 return true;
2312 /* If x is positive, then it must be larger than any value in y,
2313 and hence greater than y. */
2314 return false;
2315 }
2316 /* Optimize the opposite case, if it can be detected at compile time. */
2317 if (STATIC_CONSTANT_P (xi.len == 1))
2318 /* If YI is negative it is lower than the least HWI.
2319 If YI is positive it is greater than the greatest HWI. */
2320 return !neg_p (yi);
2321 return lts_p_large (xi.val, xi.len, precision, yi.val, yi.len);
2322}
2323
2324/* Return true if X < Y when both are treated as unsigned values. */
2325template <typename T1, typename T2>
2326inline bool
2327wi::ltu_p (const T1 &x, const T2 &y)
2328{
2329 unsigned int precision = get_binary_precision (x, y);
2330 WIDE_INT_REF_FOR (T1) xi (x, precision);
2331 WIDE_INT_REF_FOR (T2) yi (y, precision);
2332 /* Optimize comparisons with constants. */
2333 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
2334 return xi.len == 1 && xi.to_uhwi () < (unsigned HOST_WIDE_INT) yi.val[0];
2335 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
2336 return yi.len != 1 || yi.to_uhwi () > (unsigned HOST_WIDE_INT) xi.val[0];
2337 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
2338 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
2339 values does not change the result. */
2340 if (LIKELY (xi.len + yi.len == 2))
2341 {
2342 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
2343 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2344 return xl < yl;
2345 }
2346 return ltu_p_large (xi.val, xi.len, precision, yi.val, yi.len);
2347}
2348
2349/* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
2350template <typename T1, typename T2>
2351inline bool
2352wi::lt_p (const T1 &x, const T2 &y, signop sgn)
2353{
2354 if (sgn == SIGNED)
2355 return lts_p (x, y);
2356 else
2357 return ltu_p (x, y);
2358}
2359
2360/* Return true if X <= Y when both are treated as signed values. */
2361template <typename T1, typename T2>
2362inline bool
2363wi::les_p (const T1 &x, const T2 &y)
2364{
2365 return !lts_p (y, x);
2366}
2367
2368/* Return true if X <= Y when both are treated as unsigned values. */
2369template <typename T1, typename T2>
2370inline bool
2371wi::leu_p (const T1 &x, const T2 &y)
2372{
2373 return !ltu_p (y, x);
2374}
2375
2376/* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
2377template <typename T1, typename T2>
2378inline bool
2379wi::le_p (const T1 &x, const T2 &y, signop sgn)
2380{
2381 if (sgn == SIGNED)
2382 return les_p (x, y);
2383 else
2384 return leu_p (x, y);
2385}
2386
2387/* Return true if X > Y when both are treated as signed values. */
2388template <typename T1, typename T2>
2389inline bool
2390wi::gts_p (const T1 &x, const T2 &y)
2391{
2392 return lts_p (y, x);
2393}
2394
2395/* Return true if X > Y when both are treated as unsigned values. */
2396template <typename T1, typename T2>
2397inline bool
2398wi::gtu_p (const T1 &x, const T2 &y)
2399{
2400 return ltu_p (y, x);
2401}
2402
2403/* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
2404template <typename T1, typename T2>
2405inline bool
2406wi::gt_p (const T1 &x, const T2 &y, signop sgn)
2407{
2408 if (sgn == SIGNED)
2409 return gts_p (x, y);
2410 else
2411 return gtu_p (x, y);
2412}
2413
2414/* Return true if X >= Y when both are treated as signed values. */
2415template <typename T1, typename T2>
2416inline bool
2417wi::ges_p (const T1 &x, const T2 &y)
2418{
2419 return !lts_p (x, y);
2420}
2421
2422/* Return true if X >= Y when both are treated as unsigned values. */
2423template <typename T1, typename T2>
2424inline bool
2425wi::geu_p (const T1 &x, const T2 &y)
2426{
2427 return !ltu_p (x, y);
2428}
2429
2430/* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
2431template <typename T1, typename T2>
2432inline bool
2433wi::ge_p (const T1 &x, const T2 &y, signop sgn)
2434{
2435 if (sgn == SIGNED)
2436 return ges_p (x, y);
2437 else
2438 return geu_p (x, y);
2439}
2440
2441/* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
2442 as signed values. */
2443template <typename T1, typename T2>
2444inline int
2445wi::cmps (const T1 &x, const T2 &y)
2446{
2447 unsigned int precision = get_binary_precision (x, y);
2448 WIDE_INT_REF_FOR (T1) xi (x, precision);
2449 WIDE_INT_REF_FOR (T2) yi (y, precision);
2450 if (wi::fits_shwi_p (yi))
2451 {
2452 /* Special case for comparisons with 0. */
2453 if (STATIC_CONSTANT_P (yi.val[0] == 0))
2454 return neg_p (xi) ? -1 : !(xi.len == 1 && xi.val[0] == 0);
2455 /* If x fits into a signed HWI, we can compare directly. */
2456 if (wi::fits_shwi_p (xi))
2457 {
2458 HOST_WIDE_INT xl = xi.to_shwi ();
2459 HOST_WIDE_INT yl = yi.to_shwi ();
2460 return xl < yl ? -1 : xl > yl;
2461 }
2462 /* If x doesn't fit and is negative, then it must be more
2463 negative than any signed HWI, and hence smaller than y. */
2464 if (neg_p (xi))
2465 return -1;
2466 /* If x is positive, then it must be larger than any signed HWI,
2467 and hence greater than y. */
2468 return 1;
2469 }
2470 /* Optimize the opposite case, if it can be detected at compile time. */
2471 if (STATIC_CONSTANT_P (xi.len == 1))
2472 /* If YI is negative it is lower than the least HWI.
2473 If YI is positive it is greater than the greatest HWI. */
2474 return neg_p (yi) ? 1 : -1;
2475 return cmps_large (xi.val, xi.len, precision, yi.val, yi.len);
2476}
2477
2478/* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
2479 as unsigned values. */
2480template <typename T1, typename T2>
2481inline int
2482wi::cmpu (const T1 &x, const T2 &y)
2483{
2484 unsigned int precision = get_binary_precision (x, y);
2485 WIDE_INT_REF_FOR (T1) xi (x, precision);
2486 WIDE_INT_REF_FOR (T2) yi (y, precision);
2487 /* Optimize comparisons with constants. */
2488 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
2489 {
2490 /* If XI doesn't fit in a HWI then it must be larger than YI. */
2491 if (xi.len != 1)
2492 return 1;
2493 /* Otherwise compare directly. */
2494 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
2495 unsigned HOST_WIDE_INT yl = yi.val[0];
2496 return xl < yl ? -1 : xl > yl;
2497 }
2498 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
2499 {
2500 /* If YI doesn't fit in a HWI then it must be larger than XI. */
2501 if (yi.len != 1)
2502 return -1;
2503 /* Otherwise compare directly. */
2504 unsigned HOST_WIDE_INT xl = xi.val[0];
2505 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2506 return xl < yl ? -1 : xl > yl;
2507 }
2508 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
2509 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
2510 values does not change the result. */
2511 if (LIKELY (xi.len + yi.len == 2))
2512 {
2513 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
2514 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
2515 return xl < yl ? -1 : xl > yl;
2516 }
2517 return cmpu_large (xi.val, xi.len, precision, yi.val, yi.len);
2518}
2519
2520/* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
2521 X and Y indicated by SGN. */
2522template <typename T1, typename T2>
2523inline int
2524wi::cmp (const T1 &x, const T2 &y, signop sgn)
2525{
2526 if (sgn == SIGNED)
2527 return cmps (x, y);
2528 else
2529 return cmpu (x, y);
2530}
2531
2532/* Return ~x. */
2533template <typename T>
2535wi::bit_not (const T &x)
2536{
2537 WI_UNARY_RESULT_VAR (result, val, T, x);
2538 WIDE_INT_REF_FOR (T) xi (x, get_precision (result));
2539 if (result.needs_write_val_arg)
2540 val = result.write_val (xi.len);
2541 for (unsigned int i = 0; i < xi.len; ++i)
2542 val[i] = ~xi.val[i];
2543 result.set_len (xi.len);
2544 return result;
2545}
2546
2547/* Return -x. */
2548template <typename T>
2549inline WI_UNARY_RESULT (T)
2550wi::neg (const T &x)
2551{
2552 return sub (0, x);
2553}
2554
2555/* Return -x. Indicate in *OVERFLOW if performing the negation would
2556 cause an overflow. */
2557template <typename T>
2558inline WI_UNARY_RESULT (T)
2559wi::neg (const T &x, overflow_type *overflow)
2560{
2561 *overflow = only_sign_bit_p (x) ? OVF_OVERFLOW : OVF_NONE;
2562 return sub (0, x);
2563}
2564
2565/* Return the absolute value of x. */
2566template <typename T>
2567inline WI_UNARY_RESULT (T)
2568wi::abs (const T &x)
2569{
2570 return neg_p (x) ? neg (x) : WI_UNARY_RESULT (T) (x);
2571}
2572
2573/* Return the result of sign-extending the low OFFSET bits of X. */
2574template <typename T>
2575inline WI_UNARY_RESULT (T)
2576wi::sext (const T &x, unsigned int offset)
2577{
2578 WI_UNARY_RESULT_VAR (result, val, T, x);
2579 unsigned int precision = get_precision (result);
2580 WIDE_INT_REF_FOR (T) xi (x, precision);
2581
2582 if (result.needs_write_val_arg)
2583 val = result.write_val (MAX (xi.len,
2584 CEIL (offset, HOST_BITS_PER_WIDE_INT)));
2585 if (offset <= HOST_BITS_PER_WIDE_INT)
2586 {
2587 val[0] = sext_hwi (xi.ulow (), offset);
2588 result.set_len (1, true);
2589 }
2590 else
2591 result.set_len (sext_large (val, xi.val, xi.len, precision, offset));
2592 return result;
2593}
2594
2595/* Return the result of zero-extending the low OFFSET bits of X. */
2596template <typename T>
2597inline WI_UNARY_RESULT (T)
2598wi::zext (const T &x, unsigned int offset)
2599{
2600 WI_UNARY_RESULT_VAR (result, val, T, x);
2601 unsigned int precision = get_precision (result);
2602 WIDE_INT_REF_FOR (T) xi (x, precision);
2603
2604 /* This is not just an optimization, it is actually required to
2605 maintain canonization. */
2606 if (offset >= precision)
2607 {
2608 wi::copy (result, xi);
2609 return result;
2610 }
2611
2612 if (result.needs_write_val_arg)
2613 val = result.write_val (MAX (xi.len,
2614 offset / HOST_BITS_PER_WIDE_INT + 1));
2615 /* In these cases we know that at least the top bit will be clear,
2616 so no sign extension is necessary. */
2617 if (offset < HOST_BITS_PER_WIDE_INT)
2618 {
2619 val[0] = zext_hwi (xi.ulow (), offset);
2620 result.set_len (1, true);
2621 }
2622 else
2623 result.set_len (zext_large (val, xi.val, xi.len, precision, offset), true);
2624 return result;
2625}
2626
2627/* Return the result of extending the low OFFSET bits of X according to
2628 signedness SGN. */
2629template <typename T>
2630inline WI_UNARY_RESULT (T)
2631wi::ext (const T &x, unsigned int offset, signop sgn)
2632{
2633 return sgn == SIGNED ? sext (x, offset) : zext (x, offset);
2634}
2635
2636/* Return an integer that represents X | (1 << bit). */
2637template <typename T>
2638inline WI_UNARY_RESULT (T)
2639wi::set_bit (const T &x, unsigned int bit)
2640{
2641 WI_UNARY_RESULT_VAR (result, val, T, x);
2642 unsigned int precision = get_precision (result);
2643 WIDE_INT_REF_FOR (T) xi (x, precision);
2644 if (result.needs_write_val_arg)
2645 val = result.write_val (MAX (xi.len,
2646 bit / HOST_BITS_PER_WIDE_INT + 1));
2648 {
2649 val[0] = xi.ulow () | (HOST_WIDE_INT_1U << bit);
2650 result.set_len (1);
2651 }
2652 else
2653 result.set_len (set_bit_large (val, xi.val, xi.len, precision, bit));
2654 return result;
2655}
2656
2657/* Byte swap the integer X.
2658 ??? This always swaps 8-bit octets, regardless of BITS_PER_UNIT.
2659 This function requires X's precision to be a multiple of 8 bits,
2660 so care needs to be taken for targets where BITS_PER_UNIT != 8. */
2661template <typename T>
2662inline WI_UNARY_RESULT (T)
2663wi::bswap (const T &x)
2664{
2665 WI_UNARY_RESULT_VAR (result, val, T, x);
2666 unsigned int precision = get_precision (result);
2667 WIDE_INT_REF_FOR (T) xi (x, precision);
2668 static_assert (!result.needs_write_val_arg,
2669 "bswap on widest_int makes no sense");
2670 result.set_len (bswap_large (val, xi.val, xi.len, precision));
2671 return result;
2672}
2673
2674/* Bitreverse the integer X. */
2675template <typename T>
2676inline WI_UNARY_RESULT (T)
2677wi::bitreverse (const T &x)
2678{
2679 WI_UNARY_RESULT_VAR (result, val, T, x);
2680 unsigned int precision = get_precision (result);
2681 WIDE_INT_REF_FOR (T) xi (x, precision);
2682 static_assert (!result.needs_write_val_arg,
2683 "bitreverse on widest_int makes no sense");
2684 result.set_len (bitreverse_large (val, xi.val, xi.len, precision));
2685 return result;
2686}
2687
2688/* Return the minimum of X and Y, treating them both as having
2689 signedness SGN. */
2690template <typename T1, typename T2>
2691inline WI_BINARY_RESULT (T1, T2)
2692wi::min (const T1 &x, const T2 &y, signop sgn)
2693{
2694 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2695 unsigned int precision = get_precision (result);
2696 if (wi::le_p (x, y, sgn))
2697 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2698 else
2699 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2700 return result;
2701}
2702
2703/* Return the minimum of X and Y, treating both as signed values. */
2704template <typename T1, typename T2>
2705inline WI_BINARY_RESULT (T1, T2)
2706wi::smin (const T1 &x, const T2 &y)
2707{
2708 return wi::min (x, y, SIGNED);
2709}
2710
2711/* Return the minimum of X and Y, treating both as unsigned values. */
2712template <typename T1, typename T2>
2713inline WI_BINARY_RESULT (T1, T2)
2714wi::umin (const T1 &x, const T2 &y)
2715{
2716 return wi::min (x, y, UNSIGNED);
2717}
2718
2719/* Return the maximum of X and Y, treating them both as having
2720 signedness SGN. */
2721template <typename T1, typename T2>
2722inline WI_BINARY_RESULT (T1, T2)
2723wi::max (const T1 &x, const T2 &y, signop sgn)
2724{
2725 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2726 unsigned int precision = get_precision (result);
2727 if (wi::ge_p (x, y, sgn))
2728 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2729 else
2730 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2731 return result;
2732}
2733
2734/* Return the maximum of X and Y, treating both as signed values. */
2735template <typename T1, typename T2>
2736inline WI_BINARY_RESULT (T1, T2)
2737wi::smax (const T1 &x, const T2 &y)
2738{
2739 return wi::max (x, y, SIGNED);
2740}
2741
2742/* Return the maximum of X and Y, treating both as unsigned values. */
2743template <typename T1, typename T2>
2744inline WI_BINARY_RESULT (T1, T2)
2745wi::umax (const T1 &x, const T2 &y)
2746{
2747 return wi::max (x, y, UNSIGNED);
2748}
2749
2750/* Return X & Y. */
2751template <typename T1, typename T2>
2752inline WI_BINARY_RESULT (T1, T2)
2753wi::bit_and (const T1 &x, const T2 &y)
2754{
2755 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2756 unsigned int precision = get_precision (result);
2757 WIDE_INT_REF_FOR (T1) xi (x, precision);
2758 WIDE_INT_REF_FOR (T2) yi (y, precision);
2759 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2760 if (result.needs_write_val_arg)
2761 val = result.write_val (MAX (xi.len, yi.len));
2762 if (LIKELY (xi.len + yi.len == 2))
2763 {
2764 val[0] = xi.ulow () & yi.ulow ();
2765 result.set_len (1, is_sign_extended);
2766 }
2767 else
2768 result.set_len (and_large (val, xi.val, xi.len, yi.val, yi.len,
2769 precision), is_sign_extended);
2770 return result;
2771}
2772
2773/* Return X & ~Y. */
2774template <typename T1, typename T2>
2775inline WI_BINARY_RESULT (T1, T2)
2776wi::bit_and_not (const T1 &x, const T2 &y)
2777{
2778 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2779 unsigned int precision = get_precision (result);
2780 WIDE_INT_REF_FOR (T1) xi (x, precision);
2781 WIDE_INT_REF_FOR (T2) yi (y, precision);
2782 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2783 if (result.needs_write_val_arg)
2784 val = result.write_val (MAX (xi.len, yi.len));
2785 if (LIKELY (xi.len + yi.len == 2))
2786 {
2787 val[0] = xi.ulow () & ~yi.ulow ();
2788 result.set_len (1, is_sign_extended);
2789 }
2790 else
2791 result.set_len (and_not_large (val, xi.val, xi.len, yi.val, yi.len,
2792 precision), is_sign_extended);
2793 return result;
2794}
2795
2796/* Return X | Y. */
2797template <typename T1, typename T2>
2798inline WI_BINARY_RESULT (T1, T2)
2799wi::bit_or (const T1 &x, const T2 &y)
2800{
2801 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2802 unsigned int precision = get_precision (result);
2803 WIDE_INT_REF_FOR (T1) xi (x, precision);
2804 WIDE_INT_REF_FOR (T2) yi (y, precision);
2805 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2806 if (result.needs_write_val_arg)
2807 val = result.write_val (MAX (xi.len, yi.len));
2808 if (LIKELY (xi.len + yi.len == 2))
2809 {
2810 val[0] = xi.ulow () | yi.ulow ();
2811 result.set_len (1, is_sign_extended);
2812 }
2813 else
2814 result.set_len (or_large (val, xi.val, xi.len,
2815 yi.val, yi.len, precision), is_sign_extended);
2816 return result;
2817}
2818
2819/* Return X | ~Y. */
2820template <typename T1, typename T2>
2821inline WI_BINARY_RESULT (T1, T2)
2822wi::bit_or_not (const T1 &x, const T2 &y)
2823{
2824 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2825 unsigned int precision = get_precision (result);
2826 WIDE_INT_REF_FOR (T1) xi (x, precision);
2827 WIDE_INT_REF_FOR (T2) yi (y, precision);
2828 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2829 if (result.needs_write_val_arg)
2830 val = result.write_val (MAX (xi.len, yi.len));
2831 if (LIKELY (xi.len + yi.len == 2))
2832 {
2833 val[0] = xi.ulow () | ~yi.ulow ();
2834 result.set_len (1, is_sign_extended);
2835 }
2836 else
2837 result.set_len (or_not_large (val, xi.val, xi.len, yi.val, yi.len,
2838 precision), is_sign_extended);
2839 return result;
2840}
2841
2842/* Return X ^ Y. */
2843template <typename T1, typename T2>
2844inline WI_BINARY_RESULT (T1, T2)
2845wi::bit_xor (const T1 &x, const T2 &y)
2846{
2847 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2848 unsigned int precision = get_precision (result);
2849 WIDE_INT_REF_FOR (T1) xi (x, precision);
2850 WIDE_INT_REF_FOR (T2) yi (y, precision);
2851 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2852 if (result.needs_write_val_arg)
2853 val = result.write_val (MAX (xi.len, yi.len));
2854 if (LIKELY (xi.len + yi.len == 2))
2855 {
2856 val[0] = xi.ulow () ^ yi.ulow ();
2857 result.set_len (1, is_sign_extended);
2858 }
2859 else
2860 result.set_len (xor_large (val, xi.val, xi.len,
2861 yi.val, yi.len, precision), is_sign_extended);
2862 return result;
2863}
2864
2865/* Return X + Y. */
2866template <typename T1, typename T2>
2867inline WI_BINARY_RESULT (T1, T2)
2868wi::add (const T1 &x, const T2 &y)
2869{
2870 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2871 unsigned int precision = get_precision (result);
2872 WIDE_INT_REF_FOR (T1) xi (x, precision);
2873 WIDE_INT_REF_FOR (T2) yi (y, precision);
2874 if (result.needs_write_val_arg)
2875 val = result.write_val (MAX (xi.len, yi.len) + 1);
2877 {
2878 val[0] = xi.ulow () + yi.ulow ();
2879 result.set_len (1);
2880 }
2881 /* If the precision is known at compile time to be greater than
2882 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2883 knowing that (a) all bits in those HWIs are significant and
2884 (b) the result has room for at least two HWIs. This provides
2885 a fast path for things like offset_int and widest_int.
2886
2887 The STATIC_CONSTANT_P test prevents this path from being
2888 used for wide_ints. wide_ints with precisions greater than
2889 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2890 point handling them inline. */
2892 && LIKELY (xi.len + yi.len == 2))
2893 {
2894 unsigned HOST_WIDE_INT xl = xi.ulow ();
2895 unsigned HOST_WIDE_INT yl = yi.ulow ();
2896 unsigned HOST_WIDE_INT resultl = xl + yl;
2897 val[0] = resultl;
2898 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2899 result.set_len (1 + (((resultl ^ xl) & (resultl ^ yl))
2900 >> (HOST_BITS_PER_WIDE_INT - 1)));
2901 }
2902 else
2903 result.set_len (add_large (val, xi.val, xi.len,
2904 yi.val, yi.len, precision,
2905 UNSIGNED, 0));
2906 return result;
2907}
2908
2909/* Return X + Y. Treat X and Y as having the signednes given by SGN
2910 and indicate in *OVERFLOW whether the operation overflowed. */
2911template <typename T1, typename T2>
2912inline WI_BINARY_RESULT (T1, T2)
2913wi::add (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
2914{
2915 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2916 unsigned int precision = get_precision (result);
2917 WIDE_INT_REF_FOR (T1) xi (x, precision);
2918 WIDE_INT_REF_FOR (T2) yi (y, precision);
2919 if (result.needs_write_val_arg)
2920 val = result.write_val (MAX (xi.len, yi.len) + 1);
2922 {
2923 unsigned HOST_WIDE_INT xl = xi.ulow ();
2924 unsigned HOST_WIDE_INT yl = yi.ulow ();
2925 unsigned HOST_WIDE_INT resultl = xl + yl;
2926 if (sgn == SIGNED)
2927 {
2928 if ((((resultl ^ xl) & (resultl ^ yl))
2929 >> (precision - 1)) & 1)
2930 {
2931 if (xl > resultl)
2932 *overflow = OVF_UNDERFLOW;
2933 else if (xl < resultl)
2934 *overflow = OVF_OVERFLOW;
2935 else
2936 *overflow = OVF_NONE;
2937 }
2938 else
2939 *overflow = OVF_NONE;
2940 }
2941 else
2942 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2943 < (xl << (HOST_BITS_PER_WIDE_INT - precision)))
2945 val[0] = resultl;
2946 result.set_len (1);
2947 }
2948 else
2949 result.set_len (add_large (val, xi.val, xi.len,
2950 yi.val, yi.len, precision,
2951 sgn, overflow));
2952 return result;
2953}
2954
2955/* Return X - Y. */
2956template <typename T1, typename T2>
2957inline WI_BINARY_RESULT (T1, T2)
2958wi::sub (const T1 &x, const T2 &y)
2959{
2960 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2961 unsigned int precision = get_precision (result);
2962 WIDE_INT_REF_FOR (T1) xi (x, precision);
2963 WIDE_INT_REF_FOR (T2) yi (y, precision);
2964 if (result.needs_write_val_arg)
2965 val = result.write_val (MAX (xi.len, yi.len) + 1);
2967 {
2968 val[0] = xi.ulow () - yi.ulow ();
2969 result.set_len (1);
2970 }
2971 /* If the precision is known at compile time to be greater than
2972 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2973 knowing that (a) all bits in those HWIs are significant and
2974 (b) the result has room for at least two HWIs. This provides
2975 a fast path for things like offset_int and widest_int.
2976
2977 The STATIC_CONSTANT_P test prevents this path from being
2978 used for wide_ints. wide_ints with precisions greater than
2979 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2980 point handling them inline. */
2982 && LIKELY (xi.len + yi.len == 2))
2983 {
2984 unsigned HOST_WIDE_INT xl = xi.ulow ();
2985 unsigned HOST_WIDE_INT yl = yi.ulow ();
2986 unsigned HOST_WIDE_INT resultl = xl - yl;
2987 val[0] = resultl;
2988 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2989 result.set_len (1 + (((resultl ^ xl) & (xl ^ yl))
2990 >> (HOST_BITS_PER_WIDE_INT - 1)));
2991 }
2992 else
2993 result.set_len (sub_large (val, xi.val, xi.len,
2994 yi.val, yi.len, precision,
2995 UNSIGNED, 0));
2996 return result;
2997}
2998
2999/* Return X - Y. Treat X and Y as having the signednes given by SGN
3000 and indicate in *OVERFLOW whether the operation overflowed. */
3001template <typename T1, typename T2>
3002inline WI_BINARY_RESULT (T1, T2)
3003wi::sub (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3004{
3005 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
3006 unsigned int precision = get_precision (result);
3007 WIDE_INT_REF_FOR (T1) xi (x, precision);
3008 WIDE_INT_REF_FOR (T2) yi (y, precision);
3009 if (result.needs_write_val_arg)
3010 val = result.write_val (MAX (xi.len, yi.len) + 1);
3012 {
3013 unsigned HOST_WIDE_INT xl = xi.ulow ();
3014 unsigned HOST_WIDE_INT yl = yi.ulow ();
3015 unsigned HOST_WIDE_INT resultl = xl - yl;
3016 if (sgn == SIGNED)
3017 {
3018 if ((((xl ^ yl) & (resultl ^ xl)) >> (precision - 1)) & 1)
3019 {
3020 if (xl > yl)
3021 *overflow = OVF_UNDERFLOW;
3022 else if (xl < yl)
3023 *overflow = OVF_OVERFLOW;
3024 else
3025 *overflow = OVF_NONE;
3026 }
3027 else
3028 *overflow = OVF_NONE;
3029 }
3030 else
3031 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
3032 > (xl << (HOST_BITS_PER_WIDE_INT - precision)))
3034 val[0] = resultl;
3035 result.set_len (1);
3036 }
3037 else
3038 result.set_len (sub_large (val, xi.val, xi.len,
3039 yi.val, yi.len, precision,
3040 sgn, overflow));
3041 return result;
3042}
3043
3044/* Return X * Y. */
3045template <typename T1, typename T2>
3046inline WI_BINARY_RESULT (T1, T2)
3047wi::mul (const T1 &x, const T2 &y)
3048{
3049 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
3050 unsigned int precision = get_precision (result);
3051 WIDE_INT_REF_FOR (T1) xi (x, precision);
3052 WIDE_INT_REF_FOR (T2) yi (y, precision);
3053 if (result.needs_write_val_arg)
3054 val = result.write_val (xi.len + yi.len + 2);
3056 {
3057 val[0] = xi.ulow () * yi.ulow ();
3058 result.set_len (1);
3059 }
3060 else
3061 result.set_len (mul_internal (val, xi.val, xi.len, yi.val, yi.len,
3062 precision, UNSIGNED, 0, false));
3063 return result;
3064}
3065
3066/* Return X * Y. Treat X and Y as having the signednes given by SGN
3067 and indicate in *OVERFLOW whether the operation overflowed. */
3068template <typename T1, typename T2>
3069inline WI_BINARY_RESULT (T1, T2)
3070wi::mul (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3071{
3072 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
3073 unsigned int precision = get_precision (result);
3074 WIDE_INT_REF_FOR (T1) xi (x, precision);
3075 WIDE_INT_REF_FOR (T2) yi (y, precision);
3076 if (result.needs_write_val_arg)
3077 val = result.write_val (xi.len + yi.len + 2);
3078 result.set_len (mul_internal (val, xi.val, xi.len,
3079 yi.val, yi.len, precision,
3080 sgn, overflow, false));
3081 return result;
3082}
3083
3084/* Return X * Y, treating both X and Y as signed values. Indicate in
3085 *OVERFLOW whether the operation overflowed. */
3086template <typename T1, typename T2>
3087inline WI_BINARY_RESULT (T1, T2)
3088wi::smul (const T1 &x, const T2 &y, overflow_type *overflow)
3089{
3090 return mul (x, y, SIGNED, overflow);
3091}
3092
3093/* Return X * Y, treating both X and Y as unsigned values. Indicate in
3094 *OVERFLOW if the result overflows. */
3095template <typename T1, typename T2>
3096inline WI_BINARY_RESULT (T1, T2)
3097wi::umul (const T1 &x, const T2 &y, overflow_type *overflow)
3098{
3099 return mul (x, y, UNSIGNED, overflow);
3100}
3101
3102/* Perform a widening multiplication of X and Y, extending the values
3103 according to SGN, and return the high part of the result. */
3104template <typename T1, typename T2>
3105inline WI_BINARY_RESULT (T1, T2)
3106wi::mul_high (const T1 &x, const T2 &y, signop sgn)
3107{
3108 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
3109 unsigned int precision = get_precision (result);
3110 WIDE_INT_REF_FOR (T1) xi (x, precision);
3111 WIDE_INT_REF_FOR (T2) yi (y, precision);
3112 static_assert (!result.needs_write_val_arg,
3113 "mul_high on widest_int doesn't make sense");
3114 result.set_len (mul_internal (val, xi.val, xi.len,
3115 yi.val, yi.len, precision,
3116 sgn, 0, true));
3117 return result;
3118}
3119
3120/* Return X / Y, rounding towards 0. Treat X and Y as having the
3121 signedness given by SGN. Indicate in *OVERFLOW if the result
3122 overflows. */
3123template <typename T1, typename T2>
3124inline WI_BINARY_RESULT (T1, T2)
3125wi::div_trunc (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3126{
3127 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3128 unsigned int precision = get_precision (quotient);
3129 WIDE_INT_REF_FOR (T1) xi (x, precision);
3130 WIDE_INT_REF_FOR (T2) yi (y);
3131
3132 if (quotient.needs_write_val_arg)
3133 quotient_val = quotient.write_val ((sgn == UNSIGNED
3134 && xi.val[xi.len - 1] < 0)
3135 ? CEIL (precision,
3137 : xi.len + 1);
3138 quotient.set_len (divmod_internal (quotient_val, 0, 0, xi.val, xi.len,
3139 precision,
3140 yi.val, yi.len, yi.precision,
3141 sgn, overflow));
3142 return quotient;
3143}
3144
3145/* Return X / Y, rounding towards 0. Treat X and Y as signed values. */
3146template <typename T1, typename T2>
3147inline WI_BINARY_RESULT (T1, T2)
3148wi::sdiv_trunc (const T1 &x, const T2 &y)
3149{
3150 return div_trunc (x, y, SIGNED);
3151}
3152
3153/* Return X / Y, rounding towards 0. Treat X and Y as unsigned values. */
3154template <typename T1, typename T2>
3155inline WI_BINARY_RESULT (T1, T2)
3156wi::udiv_trunc (const T1 &x, const T2 &y)
3157{
3158 return div_trunc (x, y, UNSIGNED);
3159}
3160
3161/* Return X / Y, rounding towards -inf. Treat X and Y as having the
3162 signedness given by SGN. Indicate in *OVERFLOW if the result
3163 overflows. */
3164template <typename T1, typename T2>
3165inline WI_BINARY_RESULT (T1, T2)
3166wi::div_floor (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3167{
3168 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3169 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3170 unsigned int precision = get_precision (quotient);
3171 WIDE_INT_REF_FOR (T1) xi (x, precision);
3172 WIDE_INT_REF_FOR (T2) yi (y);
3173
3174 unsigned int remainder_len;
3175 if (quotient.needs_write_val_arg)
3176 {
3177 unsigned int est_len;
3178 if (sgn == UNSIGNED && xi.val[xi.len - 1] < 0)
3179 est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
3180 else
3181 est_len = xi.len + 1;
3182 quotient_val = quotient.write_val (est_len);
3183 remainder_val = remainder.write_val (est_len);
3184 }
3185 quotient.set_len (divmod_internal (quotient_val,
3186 &remainder_len, remainder_val,
3187 xi.val, xi.len, precision,
3188 yi.val, yi.len, yi.precision, sgn,
3189 overflow));
3190 remainder.set_len (remainder_len);
3191 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
3192 return quotient - 1;
3193 return quotient;
3194}
3195
3196/* Return X / Y, rounding towards -inf. Treat X and Y as signed values. */
3197template <typename T1, typename T2>
3198inline WI_BINARY_RESULT (T1, T2)
3199wi::sdiv_floor (const T1 &x, const T2 &y)
3200{
3201 return div_floor (x, y, SIGNED);
3202}
3203
3204/* Return X / Y, rounding towards -inf. Treat X and Y as unsigned values. */
3205/* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
3206template <typename T1, typename T2>
3207inline WI_BINARY_RESULT (T1, T2)
3208wi::udiv_floor (const T1 &x, const T2 &y)
3209{
3210 return div_floor (x, y, UNSIGNED);
3211}
3212
3213/* Return X / Y, rounding towards +inf. Treat X and Y as having the
3214 signedness given by SGN. Indicate in *OVERFLOW if the result
3215 overflows. */
3216template <typename T1, typename T2>
3217inline WI_BINARY_RESULT (T1, T2)
3218wi::div_ceil (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3219{
3220 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3221 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3222 unsigned int precision = get_precision (quotient);
3223 WIDE_INT_REF_FOR (T1) xi (x, precision);
3224 WIDE_INT_REF_FOR (T2) yi (y);
3225
3226 unsigned int remainder_len;
3227 if (quotient.needs_write_val_arg)
3228 {
3229 unsigned int est_len;
3230 if (sgn == UNSIGNED && xi.val[xi.len - 1] < 0)
3231 est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
3232 else
3233 est_len = xi.len + 1;
3234 quotient_val = quotient.write_val (est_len);
3235 remainder_val = remainder.write_val (est_len);
3236 }
3237 quotient.set_len (divmod_internal (quotient_val,
3238 &remainder_len, remainder_val,
3239 xi.val, xi.len, precision,
3240 yi.val, yi.len, yi.precision, sgn,
3241 overflow));
3242 remainder.set_len (remainder_len);
3243 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
3244 return quotient + 1;
3245 return quotient;
3246}
3247
3248/* Return X / Y, rounding towards +inf. Treat X and Y as unsigned values. */
3249template <typename T1, typename T2>
3250inline WI_BINARY_RESULT (T1, T2)
3251wi::udiv_ceil (const T1 &x, const T2 &y)
3252{
3253 return div_ceil (x, y, UNSIGNED);
3254}
3255
3256/* Return X / Y, rounding towards nearest with ties away from zero.
3257 Treat X and Y as having the signedness given by SGN. Indicate
3258 in *OVERFLOW if the result overflows. */
3259template <typename T1, typename T2>
3260inline WI_BINARY_RESULT (T1, T2)
3261wi::div_round (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3262{
3263 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3264 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3265 unsigned int precision = get_precision (quotient);
3266 WIDE_INT_REF_FOR (T1) xi (x, precision);
3267 WIDE_INT_REF_FOR (T2) yi (y);
3268
3269 unsigned int remainder_len;
3270 if (quotient.needs_write_val_arg)
3271 {
3272 unsigned int est_len;
3273 if (sgn == UNSIGNED && xi.val[xi.len - 1] < 0)
3274 est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
3275 else
3276 est_len = xi.len + 1;
3277 quotient_val = quotient.write_val (est_len);
3278 remainder_val = remainder.write_val (est_len);
3279 }
3280 quotient.set_len (divmod_internal (quotient_val,
3281 &remainder_len, remainder_val,
3282 xi.val, xi.len, precision,
3283 yi.val, yi.len, yi.precision, sgn,
3284 overflow));
3285 remainder.set_len (remainder_len);
3286
3287 if (remainder != 0)
3288 {
3289 if (sgn == SIGNED)
3290 {
3291 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
3292 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
3293 {
3294 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
3295 return quotient - 1;
3296 else
3297 return quotient + 1;
3298 }
3299 }
3300 else
3301 {
3302 if (wi::geu_p (remainder, wi::sub (y, remainder)))
3303 return quotient + 1;
3304 }
3305 }
3306 return quotient;
3307}
3308
3309/* Return X / Y, rounding towards 0. Treat X and Y as having the
3310 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
3311template <typename T1, typename T2>
3312inline WI_BINARY_RESULT (T1, T2)
3313wi::divmod_trunc (const T1 &x, const T2 &y, signop sgn,
3314 WI_BINARY_RESULT (T1, T2) *remainder_ptr)
3315{
3316 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3317 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3318 unsigned int precision = get_precision (quotient);
3319 WIDE_INT_REF_FOR (T1) xi (x, precision);
3320 WIDE_INT_REF_FOR (T2) yi (y);
3321
3322 unsigned int remainder_len;
3323 if (quotient.needs_write_val_arg)
3324 {
3325 unsigned int est_len;
3326 if (sgn == UNSIGNED && xi.val[xi.len - 1] < 0)
3327 est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
3328 else
3329 est_len = xi.len + 1;
3330 quotient_val = quotient.write_val (est_len);
3331 remainder_val = remainder.write_val (est_len);
3332 }
3333 quotient.set_len (divmod_internal (quotient_val,
3334 &remainder_len, remainder_val,
3335 xi.val, xi.len, precision,
3336 yi.val, yi.len, yi.precision, sgn, 0));
3337 remainder.set_len (remainder_len);
3338
3339 *remainder_ptr = remainder;
3340 return quotient;
3341}
3342
3343/* Compute the greatest common divisor of two numbers A and B using
3344 Euclid's algorithm. */
3345template <typename T1, typename T2>
3346inline WI_BINARY_RESULT (T1, T2)
3347wi::gcd (const T1 &a, const T2 &b, signop sgn)
3348{
3349 T1 x, y, z;
3350
3351 x = wi::abs (a);
3352 y = wi::abs (b);
3353
3354 while (gt_p (x, 0, sgn))
3355 {
3356 z = mod_trunc (y, x, sgn);
3357 y = x;
3358 x = z;
3359 }
3360
3361 return y;
3362}
3363
3364/* Compute X / Y, rounding towards 0, and return the remainder.
3365 Treat X and Y as having the signedness given by SGN. Indicate
3366 in *OVERFLOW if the division overflows. */
3367template <typename T1, typename T2>
3368inline WI_BINARY_RESULT (T1, T2)
3369wi::mod_trunc (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3370{
3371 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3372 unsigned int precision = get_precision (remainder);
3373 WIDE_INT_REF_FOR (T1) xi (x, precision);
3374 WIDE_INT_REF_FOR (T2) yi (y);
3375
3376 unsigned int remainder_len;
3377 if (remainder.needs_write_val_arg)
3378 remainder_val = remainder.write_val ((sgn == UNSIGNED
3379 && xi.val[xi.len - 1] < 0)
3380 ? CEIL (precision,
3382 : xi.len + 1);
3383 divmod_internal (0, &remainder_len, remainder_val,
3384 xi.val, xi.len, precision,
3385 yi.val, yi.len, yi.precision, sgn, overflow);
3386 remainder.set_len (remainder_len);
3387
3388 return remainder;
3389}
3390
3391/* Compute X / Y, rounding towards 0, and return the remainder.
3392 Treat X and Y as signed values. */
3393template <typename T1, typename T2>
3394inline WI_BINARY_RESULT (T1, T2)
3395wi::smod_trunc (const T1 &x, const T2 &y)
3396{
3397 return mod_trunc (x, y, SIGNED);
3398}
3399
3400/* Compute X / Y, rounding towards 0, and return the remainder.
3401 Treat X and Y as unsigned values. */
3402template <typename T1, typename T2>
3403inline WI_BINARY_RESULT (T1, T2)
3404wi::umod_trunc (const T1 &x, const T2 &y)
3405{
3406 return mod_trunc (x, y, UNSIGNED);
3407}
3408
3409/* Compute X / Y, rounding towards -inf, and return the remainder.
3410 Treat X and Y as having the signedness given by SGN. Indicate
3411 in *OVERFLOW if the division overflows. */
3412template <typename T1, typename T2>
3413inline WI_BINARY_RESULT (T1, T2)
3414wi::mod_floor (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3415{
3416 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3417 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3418 unsigned int precision = get_precision (quotient);
3419 WIDE_INT_REF_FOR (T1) xi (x, precision);
3420 WIDE_INT_REF_FOR (T2) yi (y);
3421
3422 unsigned int remainder_len;
3423 if (quotient.needs_write_val_arg)
3424 {
3425 unsigned int est_len;
3426 if (sgn == UNSIGNED && xi.val[xi.len - 1] < 0)
3427 est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
3428 else
3429 est_len = xi.len + 1;
3430 quotient_val = quotient.write_val (est_len);
3431 remainder_val = remainder.write_val (est_len);
3432 }
3433 quotient.set_len (divmod_internal (quotient_val,
3434 &remainder_len, remainder_val,
3435 xi.val, xi.len, precision,
3436 yi.val, yi.len, yi.precision, sgn,
3437 overflow));
3438 remainder.set_len (remainder_len);
3439
3440 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
3441 return remainder + y;
3442 return remainder;
3443}
3444
3445/* Compute X / Y, rounding towards -inf, and return the remainder.
3446 Treat X and Y as unsigned values. */
3447/* ??? Why do we have both this and umod_trunc. Aren't they the same? */
3448template <typename T1, typename T2>
3449inline WI_BINARY_RESULT (T1, T2)
3450wi::umod_floor (const T1 &x, const T2 &y)
3451{
3452 return mod_floor (x, y, UNSIGNED);
3453}
3454
3455/* Compute X / Y, rounding towards +inf, and return the remainder.
3456 Treat X and Y as having the signedness given by SGN. Indicate
3457 in *OVERFLOW if the division overflows. */
3458template <typename T1, typename T2>
3459inline WI_BINARY_RESULT (T1, T2)
3460wi::mod_ceil (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3461{
3462 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3463 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3464 unsigned int precision = get_precision (quotient);
3465 WIDE_INT_REF_FOR (T1) xi (x, precision);
3466 WIDE_INT_REF_FOR (T2) yi (y);
3467
3468 unsigned int remainder_len;
3469 if (quotient.needs_write_val_arg)
3470 {
3471 unsigned int est_len;
3472 if (sgn == UNSIGNED && xi.val[xi.len - 1] < 0)
3473 est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
3474 else
3475 est_len = xi.len + 1;
3476 quotient_val = quotient.write_val (est_len);
3477 remainder_val = remainder.write_val (est_len);
3478 }
3479 quotient.set_len (divmod_internal (quotient_val,
3480 &remainder_len, remainder_val,
3481 xi.val, xi.len, precision,
3482 yi.val, yi.len, yi.precision, sgn,
3483 overflow));
3484 remainder.set_len (remainder_len);
3485
3486 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
3487 return remainder - y;
3488 return remainder;
3489}
3490
3491/* Compute X / Y, rounding towards nearest with ties away from zero,
3492 and return the remainder. Treat X and Y as having the signedness
3493 given by SGN. Indicate in *OVERFLOW if the division overflows. */
3494template <typename T1, typename T2>
3495inline WI_BINARY_RESULT (T1, T2)
3496wi::mod_round (const T1 &x, const T2 &y, signop sgn, overflow_type *overflow)
3497{
3498 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
3499 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
3500 unsigned int precision = get_precision (quotient);
3501 WIDE_INT_REF_FOR (T1) xi (x, precision);
3502 WIDE_INT_REF_FOR (T2) yi (y);
3503
3504 unsigned int remainder_len;
3505 if (quotient.needs_write_val_arg)
3506 {
3507 unsigned int est_len;
3508 if (sgn == UNSIGNED && xi.val[xi.len - 1] < 0)
3509 est_len = CEIL (precision, HOST_BITS_PER_WIDE_INT) + 1;
3510 else
3511 est_len = xi.len + 1;
3512 quotient_val = quotient.write_val (est_len);
3513 remainder_val = remainder.write_val (est_len);
3514 }
3515 quotient.set_len (divmod_internal (quotient_val,
3516 &remainder_len, remainder_val,
3517 xi.val, xi.len, precision,
3518 yi.val, yi.len, yi.precision, sgn,
3519 overflow));
3520 remainder.set_len (remainder_len);
3521
3522 if (remainder != 0)
3523 {
3524 if (sgn == SIGNED)
3525 {
3526 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
3527 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
3528 {
3529 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
3530 return remainder + y;
3531 else
3532 return remainder - y;
3533 }
3534 }
3535 else
3536 {
3537 if (wi::geu_p (remainder, wi::sub (y, remainder)))
3538 return remainder - y;
3539 }
3540 }
3541 return remainder;
3542}
3543
3544/* Return true if X is a multiple of Y. Treat X and Y as having the
3545 signedness given by SGN. */
3546template <typename T1, typename T2>
3547inline bool
3548wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn)
3549{
3550 return wi::mod_trunc (x, y, sgn) == 0;
3551}
3552
3553/* Return true if X is a multiple of Y, storing X / Y in *RES if so.
3554 Treat X and Y as having the signedness given by SGN. */
3555template <typename T1, typename T2>
3556inline bool
3557wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn,
3558 WI_BINARY_RESULT (T1, T2) *res)
3559{
3560 WI_BINARY_RESULT (T1, T2) remainder;
3561 WI_BINARY_RESULT (T1, T2) quotient
3562 = divmod_trunc (x, y, sgn, &remainder);
3563 if (remainder == 0)
3564 {
3565 *res = quotient;
3566 return true;
3567 }
3568 return false;
3569}
3570
3571/* Return X << Y. Return 0 if Y is greater than or equal to
3572 the precision of X. */
3573template <typename T1, typename T2>
3575wi::lshift (const T1 &x, const T2 &y)
3576{
3577 WI_UNARY_RESULT_VAR (result, val, T1, x);
3578 unsigned int precision = get_precision (result);
3579 WIDE_INT_REF_FOR (T1) xi (x, precision);
3580 WIDE_INT_REF_FOR (T2) yi (y);
3581 /* Handle the simple cases quickly. */
3582 if (geu_p (yi, precision))
3583 {
3584 if (result.needs_write_val_arg)
3585 val = result.write_val (1);
3586 val[0] = 0;
3587 result.set_len (1);
3588 }
3589 else
3590 {
3591 unsigned int shift = yi.to_uhwi ();
3592 if (result.needs_write_val_arg)
3593 val = result.write_val (xi.len + shift / HOST_BITS_PER_WIDE_INT + 1);
3594 /* For fixed-precision integers like offset_int and widest_int,
3595 handle the case where the shift value is constant and the
3596 result is a single nonnegative HWI (meaning that we don't
3597 need to worry about val[1]). This is particularly common
3598 for converting a byte count to a bit count.
3599
3600 For variable-precision integers like wide_int, handle HWI
3601 and sub-HWI integers inline. */
3602 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
3604 && xi.len == 1
3605 && IN_RANGE (xi.val[0], 0, HOST_WIDE_INT_MAX >> shift))
3607 {
3608 val[0] = xi.ulow () << shift;
3609 result.set_len (1);
3610 }
3611 else
3612 result.set_len (lshift_large (val, xi.val, xi.len,
3613 precision, shift));
3614 }
3615 return result;
3616}
3617
3618/* Return X >> Y, using a logical shift. Return 0 if Y is greater than
3619 or equal to the precision of X. */
3620template <typename T1, typename T2>
3621inline WI_UNARY_RESULT (T1)
3622wi::lrshift (const T1 &x, const T2 &y)
3623{
3624 WI_UNARY_RESULT_VAR (result, val, T1, x);
3625 /* Do things in the precision of the input rather than the output,
3626 since the result can be no larger than that. */
3627 WIDE_INT_REF_FOR (T1) xi (x);
3628 WIDE_INT_REF_FOR (T2) yi (y);
3629 /* Handle the simple cases quickly. */
3630 if (geu_p (yi, xi.precision))
3631 {
3632 if (result.needs_write_val_arg)
3633 val = result.write_val (1);
3634 val[0] = 0;
3635 result.set_len (1);
3636 }
3637 else
3638 {
3639 unsigned int shift = yi.to_uhwi ();
3640 if (result.needs_write_val_arg)
3641 {
3642 unsigned int est_len = xi.len;
3643 if (xi.val[xi.len - 1] < 0 && shift)
3644 /* Logical right shift of sign-extended value might need a very
3645 large precision e.g. for widest_int. */
3646 est_len = CEIL (xi.precision - shift, HOST_BITS_PER_WIDE_INT) + 1;
3647 val = result.write_val (est_len);
3648 }
3649 /* For fixed-precision integers like offset_int and widest_int,
3650 handle the case where the shift value is constant and the
3651 shifted value is a single nonnegative HWI (meaning that all
3652 bits above the HWI are zero). This is particularly common
3653 for converting a bit count to a byte count.
3654
3655 For variable-precision integers like wide_int, handle HWI
3656 and sub-HWI integers inline. */
3657 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
3658 ? (shift < HOST_BITS_PER_WIDE_INT
3659 && xi.len == 1
3660 && xi.val[0] >= 0)
3661 : xi.precision <= HOST_BITS_PER_WIDE_INT)
3662 {
3663 val[0] = xi.to_uhwi () >> shift;
3664 result.set_len (1);
3665 }
3666 else
3667 result.set_len (lrshift_large (val, xi.val, xi.len, xi.precision,
3668 get_precision (result), shift));
3669 }
3670 return result;
3671}
3672
3673/* Return X >> Y, using an arithmetic shift. Return a sign mask if
3674 Y is greater than or equal to the precision of X. */
3675template <typename T1, typename T2>
3676inline WI_UNARY_RESULT (T1)
3677wi::arshift (const T1 &x, const T2 &y)
3678{
3679 WI_UNARY_RESULT_VAR (result, val, T1, x);
3680 /* Do things in the precision of the input rather than the output,
3681 since the result can be no larger than that. */
3682 WIDE_INT_REF_FOR (T1) xi (x);
3683 WIDE_INT_REF_FOR (T2) yi (y);
3684 if (result.needs_write_val_arg)
3685 val = result.write_val (xi.len);
3686 /* Handle the simple cases quickly. */
3687 if (geu_p (yi, xi.precision))
3688 {
3689 val[0] = sign_mask (x);
3690 result.set_len (1);
3691 }
3692 else
3693 {
3694 unsigned int shift = yi.to_uhwi ();
3695 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
3696 {
3697 val[0] = sext_hwi (xi.ulow () >> shift, xi.precision - shift);
3698 result.set_len (1, true);
3699 }
3700 else
3701 result.set_len (arshift_large (val, xi.val, xi.len, xi.precision,
3702 get_precision (result), shift));
3703 }
3704 return result;
3705}
3706
3707/* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
3708 logical shift otherwise. */
3709template <typename T1, typename T2>
3710inline WI_UNARY_RESULT (T1)
3711wi::rshift (const T1 &x, const T2 &y, signop sgn)
3712{
3713 if (sgn == UNSIGNED)
3714 return lrshift (x, y);
3715 else
3716 return arshift (x, y);
3717}
3718
3719/* Return the result of rotating the low WIDTH bits of X left by Y
3720 bits and zero-extending the result. Use a full-width rotate if
3721 WIDTH is zero. */
3722template <typename T1, typename T2>
3723WI_UNARY_RESULT (T1)
3724wi::lrotate (const T1 &x, const T2 &y, unsigned int width)
3725{
3726 unsigned int precision = get_binary_precision (x, x);
3727 if (width == 0)
3728 width = precision;
3729 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
3730 WI_UNARY_RESULT (T1) left = wi::lshift (x, ymod);
3731 WI_UNARY_RESULT (T1) right
3732 = wi::lrshift (width != precision ? wi::zext (x, width) : x,
3733 wi::sub (width, ymod));
3734 if (width != precision)
3735 return wi::zext (left, width) | right;
3736 return left | right;
3737}
3738
3739/* Return the result of rotating the low WIDTH bits of X right by Y
3740 bits and zero-extending the result. Use a full-width rotate if
3741 WIDTH is zero. */
3742template <typename T1, typename T2>
3743WI_UNARY_RESULT (T1)
3744wi::rrotate (const T1 &x, const T2 &y, unsigned int width)
3745{
3746 unsigned int precision = get_binary_precision (x, x);
3747 if (width == 0)
3748 width = precision;
3749 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
3750 WI_UNARY_RESULT (T1) right
3751 = wi::lrshift (width != precision ? wi::zext (x, width) : x, ymod);
3752 WI_UNARY_RESULT (T1) left = wi::lshift (x, wi::sub (width, ymod));
3753 if (width != precision)
3754 return wi::zext (left, width) | right;
3755 return left | right;
3756}
3757
3758/* Return 0 if the number of 1s in X is even and 1 if the number of 1s
3759 is odd. */
3760inline int
3762{
3763 return popcount (x) & 1;
3764}
3765
3766/* Extract WIDTH bits from X, starting at BITPOS. */
3767template <typename T>
3768inline unsigned HOST_WIDE_INT
3769wi::extract_uhwi (const T &x, unsigned int bitpos, unsigned int width)
3770{
3771 unsigned precision = get_precision (x);
3772 if (precision < bitpos + width)
3773 precision = bitpos + width;
3774 WIDE_INT_REF_FOR (T) xi (x, precision);
3775
3776 /* Handle this rare case after the above, so that we assert about
3777 bogus BITPOS values. */
3778 if (width == 0)
3779 return 0;
3780
3781 unsigned int start = bitpos / HOST_BITS_PER_WIDE_INT;
3782 unsigned int shift = bitpos % HOST_BITS_PER_WIDE_INT;
3783 unsigned HOST_WIDE_INT res = xi.elt (start);
3784 res >>= shift;
3785 if (shift + width > HOST_BITS_PER_WIDE_INT)
3786 {
3787 unsigned HOST_WIDE_INT upper = xi.elt (start + 1);
3788 res |= upper << (-shift % HOST_BITS_PER_WIDE_INT);
3789 }
3790 return zext_hwi (res, width);
3791}
3792
3793/* Return the minimum precision needed to store X with sign SGN. */
3794template <typename T>
3795inline unsigned int
3796wi::min_precision (const T &x, signop sgn)
3797{
3798 if (sgn == SIGNED)
3799 return get_precision (x) - clrsb (x);
3800 else
3801 return get_precision (x) - clz (x);
3802}
3803
3804#define SIGNED_BINARY_PREDICATE(OP, F) \
3805 template <typename T1, typename T2> \
3806 inline WI_SIGNED_BINARY_PREDICATE_RESULT (T1, T2) \
3807 OP (const T1 &x, const T2 &y) \
3808 { \
3809 return wi::F (x, y); \
3810 }
3811
3812SIGNED_BINARY_PREDICATE (operator <, lts_p)
3813SIGNED_BINARY_PREDICATE (operator <=, les_p)
3814SIGNED_BINARY_PREDICATE (operator >, gts_p)
3815SIGNED_BINARY_PREDICATE (operator >=, ges_p)
3816
3817#undef SIGNED_BINARY_PREDICATE
3818
3819#define UNARY_OPERATOR(OP, F) \
3820 template<typename T> \
3821 WI_UNARY_RESULT (generic_wide_int<T>) \
3822 OP (const generic_wide_int<T> &x) \
3823 { \
3824 return wi::F (x); \
3825 }
3826
3827#define BINARY_PREDICATE(OP, F) \
3828 template<typename T1, typename T2> \
3829 WI_BINARY_PREDICATE_RESULT (T1, T2) \
3830 OP (const T1 &x, const T2 &y) \
3831 { \
3832 return wi::F (x, y); \
3833 }
3834
3835#define BINARY_OPERATOR(OP, F) \
3836 template<typename T1, typename T2> \
3837 WI_BINARY_OPERATOR_RESULT (T1, T2) \
3838 OP (const T1 &x, const T2 &y) \
3839 { \
3840 return wi::F (x, y); \
3841 }
3842
3843#define SHIFT_OPERATOR(OP, F) \
3844 template<typename T1, typename T2> \
3845 WI_BINARY_OPERATOR_RESULT (T1, T1) \
3846 OP (const T1 &x, const T2 &y) \
3847 { \
3848 return wi::F (x, y); \
3849 }
3850
3851UNARY_OPERATOR (operator ~, bit_not)
3852UNARY_OPERATOR (operator -, neg)
3853BINARY_PREDICATE (operator ==, eq_p)
3854BINARY_PREDICATE (operator !=, ne_p)
3855BINARY_OPERATOR (operator &, bit_and)
3856BINARY_OPERATOR (operator |, bit_or)
3857BINARY_OPERATOR (operator ^, bit_xor)
3858BINARY_OPERATOR (operator +, add)
3859BINARY_OPERATOR (operator -, sub)
3860BINARY_OPERATOR (operator *, mul)
3861SHIFT_OPERATOR (operator <<, lshift)
3862
3863#undef UNARY_OPERATOR
3864#undef BINARY_PREDICATE
3865#undef BINARY_OPERATOR
3866#undef SHIFT_OPERATOR
3867
3868template <typename T1, typename T2>
3870operator >> (const T1 &x, const T2 &y)
3871{
3872 return wi::arshift (x, y);
3873}
3874
3875template <typename T1, typename T2>
3876inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3877operator / (const T1 &x, const T2 &y)
3878{
3879 return wi::sdiv_trunc (x, y);
3880}
3881
3882template <typename T1, typename T2>
3883inline WI_SIGNED_SHIFT_RESULT (T1, T2)
3884operator % (const T1 &x, const T2 &y)
3885{
3886 return wi::smod_trunc (x, y);
3887}
3888
3889void gt_ggc_mx (generic_wide_int <wide_int_storage> *) = delete;
3890void gt_pch_nx (generic_wide_int <wide_int_storage> *) = delete;
3891void gt_pch_nx (generic_wide_int <wide_int_storage> *,
3892 gt_pointer_operator, void *) = delete;
3893
3894template<int N>
3895void
3896gt_ggc_mx (generic_wide_int <fixed_wide_int_storage <N> > *)
3897{
3898}
3899
3900template<int N>
3901void
3902gt_pch_nx (generic_wide_int <fixed_wide_int_storage <N> > *)
3903{
3904}
3905
3906template<int N>
3907void
3908gt_pch_nx (generic_wide_int <fixed_wide_int_storage <N> > *,
3909 gt_pointer_operator, void *)
3910{
3911}
3912
3913template<int N>
3914void gt_ggc_mx (generic_wide_int <widest_int_storage <N> > *) = delete;
3915
3916template<int N>
3917void gt_pch_nx (generic_wide_int <widest_int_storage <N> > *) = delete;
3918
3919template<int N>
3920void gt_pch_nx (generic_wide_int <widest_int_storage <N> > *,
3921 gt_pointer_operator, void *) = delete;
3922
3923template<int N>
3924void
3925gt_ggc_mx (trailing_wide_ints <N> *)
3926{
3927}
3928
3929template<int N>
3930void
3931gt_pch_nx (trailing_wide_ints <N> *)
3932{
3933}
3934
3935template<int N>
3936void
3937gt_pch_nx (trailing_wide_ints <N> *, gt_pointer_operator, void *)
3938{
3939}
3940
3941namespace wi
3942{
3943 /* Used for overloaded functions in which the only other acceptable
3944 scalar type is a pointer. It stops a plain 0 from being treated
3945 as a null pointer. */
3946 struct never_used1 {};
3947 struct never_used2 {};
3948
3949 wide_int min_value (unsigned int, signop);
3952 wide_int max_value (unsigned int, signop);
3955
3956 /* FIXME: this is target dependent, so should be elsewhere.
3957 It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
3958 wide_int from_buffer (const unsigned char *, unsigned int);
3959
3960#ifndef GENERATOR_FILE
3961 void to_mpz (const wide_int_ref &, mpz_t, signop);
3962#endif
3963
3964 wide_int mask (unsigned int, bool, unsigned int);
3965 wide_int shifted_mask (unsigned int, unsigned int, bool, unsigned int);
3966 wide_int set_bit_in_zero (unsigned int, unsigned int);
3967 wide_int insert (const wide_int &x, const wide_int &y, unsigned int,
3968 unsigned int);
3969 wide_int round_down_for_mask (const wide_int &, const wide_int &);
3970 wide_int round_up_for_mask (const wide_int &, const wide_int &);
3971
3972 wide_int mod_inv (const wide_int &a, const wide_int &b);
3973
3974 template <typename T>
3975 T mask (unsigned int, bool);
3976
3977 template <typename T>
3978 T shifted_mask (unsigned int, unsigned int, bool);
3979
3980 template <typename T>
3981 T set_bit_in_zero (unsigned int);
3982
3983 unsigned int mask (HOST_WIDE_INT *, unsigned int, bool, unsigned int);
3984 unsigned int shifted_mask (HOST_WIDE_INT *, unsigned int, unsigned int,
3985 bool, unsigned int);
3986 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
3987 unsigned int, unsigned int, bool);
3988
3989 wide_int bit_repeat (const wide_int_ref &, unsigned int);
3990 wide_int clmul (const wide_int_ref &, const wide_int_ref &, unsigned int);
3991}
3992
3993/* Return a PRECISION-bit integer in which the low WIDTH bits are set
3994 and the other bits are clear, or the inverse if NEGATE_P. */
3995inline wide_int
3996wi::mask (unsigned int width, bool negate_p, unsigned int precision)
3997{
3999 result.set_len (mask (result.write_val (0), width, negate_p, precision));
4000 return result;
4001}
4002
4003/* Return a PRECISION-bit integer in which the low START bits are clear,
4004 the next WIDTH bits are set, and the other bits are clear,
4005 or the inverse if NEGATE_P. */
4006inline wide_int
4007wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p,
4008 unsigned int precision)
4009{
4011 result.set_len (shifted_mask (result.write_val (0), start, width, negate_p,
4012 precision));
4013 return result;
4014}
4015
4016/* Return a PRECISION-bit integer in which bit BIT is set and all the
4017 others are clear. */
4018inline wide_int
4019wi::set_bit_in_zero (unsigned int bit, unsigned int precision)
4020{
4021 return shifted_mask (bit, 1, false, precision);
4022}
4023
4024/* Return an integer of type T in which the low WIDTH bits are set
4025 and the other bits are clear, or the inverse if NEGATE_P. */
4026template <typename T>
4027inline T
4028wi::mask (unsigned int width, bool negate_p)
4029{
4031 T result;
4032 result.set_len (mask (result.write_val (width / HOST_BITS_PER_WIDE_INT + 1),
4033 width, negate_p, wi::int_traits <T>::precision));
4034 return result;
4035}
4036
4037/* Return an integer of type T in which the low START bits are clear,
4038 the next WIDTH bits are set, and the other bits are clear, or the
4039 inverse if NEGATE_P. */
4040template <typename T>
4041inline T
4042wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p)
4043{
4045 T result;
4046 unsigned int prec = wi::int_traits <T>::precision;
4047 unsigned int est_len
4048 = result.needs_write_val_arg
4049 ? ((start + (width > prec - start ? prec - start : width))
4050 / HOST_BITS_PER_WIDE_INT + 1) : 0;
4051 result.set_len (shifted_mask (result.write_val (est_len), start, width,
4052 negate_p, prec));
4053 return result;
4054}
4055
4056/* Return an integer of type T in which bit BIT is set and all the
4057 others are clear. */
4058template <typename T>
4059inline T
4060wi::set_bit_in_zero (unsigned int bit)
4061{
4062 return shifted_mask <T> (bit, 1, false);
4063}
4064
4065/* Accumulate a set of overflows into OVERFLOW. */
4066
4067inline void
4069 wi::overflow_type suboverflow)
4070{
4071 if (!suboverflow)
4072 return;
4073 if (!overflow)
4074 overflow = suboverflow;
4075 else if (overflow != suboverflow)
4076 overflow = wi::OVF_UNKNOWN;
4077}
4078
4079#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:478
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:1066
int ctz(const wide_int_ref &)
Definition wide-int.cc:2229
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:3548
BINARY_FUNCTION udiv_floor(const T1 &, const T2 &)
wide_int shifted_mask(unsigned int, unsigned int, bool, unsigned int)
Definition wide-int.h:4007
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:2381
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:1008
wide_int mask(unsigned int, bool, unsigned int)
Definition wide-int.h:3996
unsigned int set_bit_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int, unsigned int)
Definition wide-int.cc:698
unsigned int bswap_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int)
Definition wide-int.cc:734
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:2445
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:2118
SHIFT_FUNCTION rrotate(const T1 &, const T2 &, unsigned int=0)
BINARY_FUNCTION div_ceil(const T1 &, const T2 &, signop, overflow_type *=0)
wide_int bit_repeat(const wide_int_ref &, unsigned int)
Definition wide-int.cc:2405
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:2238
int clrsb(const wide_int_ref &)
Definition wide-int.cc:2201
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:1124
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:2340
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:3769
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:2293
bool eq_p_large(const HOST_WIDE_INT *, unsigned int, const HOST_WIDE_INT *, unsigned int, unsigned int)
Definition wide-int.cc:448
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:142
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 &)
wide_int clmul(const wide_int_ref &, const wide_int_ref &, unsigned int)
Definition wide-int.cc:2438
UNARY_FUNCTION set_bit(const T &, unsigned int)
unsigned int min_precision(const T &, signop)
Definition wide-int.h:3796
int cmpu(const T1 &, const T2 &)
Definition wide-int.h:2482
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:2245
wide_int from_buffer(const unsigned char *, unsigned int)
Definition wide-int.cc:155
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:2152
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:893
int ffs(const wide_int_ref &)
Definition wide-int.cc:2285
int floor_log2(const wide_int_ref &)
Definition wide-int.cc:2277
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:1836
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:950
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:580
unsigned int bitreverse_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int)
Definition wide-int.cc:768
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:1170
unsigned int lshift_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int, unsigned int)
Definition wide-int.cc:2051
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:4068
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:668
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:1636
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:2389
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:363
int cmps_large(const HOST_WIDE_INT *, unsigned int, unsigned int, const HOST_WIDE_INT *, unsigned int)
Definition wide-int.cc:516
BINARY_FUNCTION mul(const T1 &, const T2 &)
wide_int round_down_for_mask(const wide_int &, const wide_int &)
Definition wide-int.cc:2314
BINARY_FUNCTION gcd(const T1 &, const T2 &, signop=UNSIGNED)
void to_mpz(const wide_int_ref &, mpz_t, signop)
Definition wide-int.cc:205
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:1324
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:2373
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:2176
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:2524
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:3761
unsigned int zext_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int, unsigned int)
Definition wide-int.cc:639
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:553
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:1604
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:4019
unsigned int sext_large(HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int, unsigned int)
Definition wide-int.cc:612
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:3946
Definition wide-int.h:3947
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:3835
#define ADDR_MAX_PRECISION
Definition wide-int.h:276
#define SIGNED_BINARY_PREDICATE(OP, F)
Definition wide-int.h:3804
#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:3871
#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:3819
#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:3843
#define WIDE_INT_MAX_HWIS(PRECISION)
Definition wide-int.h:284
#define UNARY_FUNCTION
Definition wide-int.h:562