[ARM/AArch64][testsuite] Add vmovl tests.
[official-gcc.git] / gcc / wide-int.h
blob46f45453c015b893e55c708db27168e979acb1d5
1 /* Operations with very long integers. -*- C++ -*-
2 Copyright (C) 2012-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef WIDE_INT_H
21 #define WIDE_INT_H
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).
29 The actual precision of a wide_int depends on the flavor. There
30 are three predefined flavors:
32 1) wide_int (the default). This flavor does the math in the
33 precision of its input arguments. It is assumed (and checked)
34 that the precisions of the operands and results are consistent.
35 This is the most efficient flavor. It is not possible to examine
36 bits above the precision that has been specified. Because of
37 this, the default flavor has semantics that are simple to
38 understand and in general model the underlying hardware that the
39 compiler is targetted for.
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.
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.
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.
56 2) offset_int. This is a fixed size representation that is
57 guaranteed to be large enough to compute any bit or byte sized
58 address calculation on the target. Currently the value is 64 + 4
59 bits rounded up to the next number even multiple of
60 HOST_BITS_PER_WIDE_INT (but this can be changed when the first
61 port needs more than 64 bits for the size of a pointer).
63 This flavor can be used for all address math on the target. In
64 this representation, the values are sign or zero extended based
65 on their input types to the internal precision. All math is done
66 in this precision and then the values are truncated to fit in the
67 result type. Unlike most gimple or rtl intermediate code, it is
68 not useful to perform the address arithmetic at the same
69 precision in which the operands are represented because there has
70 been no effort by the front ends to convert most addressing
71 arithmetic to canonical types.
73 3) widest_int. This representation is an approximation of
74 infinite precision math. However, it is not really infinite
75 precision math as in the GMP library. It is really finite
76 precision math where the precision is 4 times the size of the
77 largest integer that the target port can represent.
79 widest_int is supposed to be wider than any number that it needs to
80 store, meaning that there is always at least one leading sign bit.
81 All widest_int values are therefore signed.
83 There are several places in the GCC where this should/must be used:
85 * Code that does induction variable optimizations. This code
86 works with induction variables of many different types at the
87 same time. Because of this, it ends up doing many different
88 calculations where the operands are not compatible types. The
89 widest_int makes this easy, because it provides a field where
90 nothing is lost when converting from any variable,
92 * There are a small number of passes that currently use the
93 widest_int that should use the default. These should be
94 changed.
96 There are surprising features of offset_int and widest_int
97 that the users should be careful about:
99 1) Shifts and rotations are just weird. You have to specify a
100 precision in which the shift or rotate is to happen in. The bits
101 above this precision are zeroed. While this is what you
102 want, it is clearly non obvious.
104 2) Larger precision math sometimes does not produce the same
105 answer as would be expected for doing the math at the proper
106 precision. In particular, a multiply followed by a divide will
107 produce a different answer if the first product is larger than
108 what can be represented in the input precision.
110 The offset_int and the widest_int flavors are more expensive
111 than the default wide int, so in addition to the caveats with these
112 two, the default is the prefered representation.
114 All three flavors of wide_int are represented as a vector of
115 HOST_WIDE_INTs. The default and widest_int vectors contain enough elements
116 to hold a value of MAX_BITSIZE_MODE_ANY_INT bits. offset_int contains only
117 enough elements to hold ADDR_MAX_PRECISION bits. The values are stored
118 in the vector with the least significant HOST_BITS_PER_WIDE_INT bits
119 in element 0.
121 The default wide_int contains three fields: the vector (VAL),
122 the precision and a length (LEN). The length is the number of HWIs
123 needed to represent the value. widest_int and offset_int have a
124 constant precision that cannot be changed, so they only store the
125 VAL and LEN fields.
127 Since most integers used in a compiler are small values, it is
128 generally profitable to use a representation of the value that is
129 as small as possible. LEN is used to indicate the number of
130 elements of the vector that are in use. The numbers are stored as
131 sign extended numbers as a means of compression. Leading
132 HOST_WIDE_INTs that contain strings of either -1 or 0 are removed
133 as long as they can be reconstructed from the top bit that is being
134 represented.
136 The precision and length of a wide_int are always greater than 0.
137 Any bits in a wide_int above the precision are sign-extended from the
138 most significant bit. For example, a 4-bit value 0x8 is represented as
139 VAL = { 0xf...fff8 }. However, as an optimization, we allow other integer
140 constants to be represented with undefined bits above the precision.
141 This allows INTEGER_CSTs to be pre-extended according to TYPE_SIGN,
142 so that the INTEGER_CST representation can be used both in TYPE_PRECISION
143 and in wider precisions.
145 There are constructors to create the various forms of wide_int from
146 trees, rtl and constants. For trees you can simply say:
148 tree t = ...;
149 wide_int x = t;
151 However, a little more syntax is required for rtl constants since
152 they do not have an explicit precision. To make an rtl into a
153 wide_int, you have to pair it with a mode. The canonical way to do
154 this is with std::make_pair as in:
156 rtx r = ...
157 wide_int x = std::make_pair (r, mode);
159 Similarly, a wide_int can only be constructed from a host value if
160 the target precision is given explicitly, such as in:
162 wide_int x = wi::shwi (c, prec); // sign-extend C if necessary
163 wide_int y = wi::uhwi (c, prec); // zero-extend C if necessary
165 However, offset_int and widest_int have an inherent precision and so
166 can be initialized directly from a host value:
168 offset_int x = (int) c; // sign-extend C
169 widest_int x = (unsigned int) c; // zero-extend C
171 It is also possible to do arithmetic directly on trees, rtxes and
172 constants. For example:
174 wi::add (t1, t2); // add equal-sized INTEGER_CSTs t1 and t2
175 wi::add (t1, 1); // add 1 to INTEGER_CST t1
176 wi::add (r1, r2); // add equal-sized rtx constants r1 and r2
177 wi::lshift (1, 100); // 1 << 100 as a widest_int
179 Many binary operations place restrictions on the combinations of inputs,
180 using the following rules:
182 - {tree, rtx, wide_int} op {tree, rtx, wide_int} -> wide_int
183 The inputs must be the same precision. The result is a wide_int
184 of the same precision
186 - {tree, rtx, wide_int} op (un)signed HOST_WIDE_INT -> wide_int
187 (un)signed HOST_WIDE_INT op {tree, rtx, wide_int} -> wide_int
188 The HOST_WIDE_INT is extended or truncated to the precision of
189 the other input. The result is a wide_int of the same precision
190 as that input.
192 - (un)signed HOST_WIDE_INT op (un)signed HOST_WIDE_INT -> widest_int
193 The inputs are extended to widest_int precision and produce a
194 widest_int result.
196 - offset_int op offset_int -> offset_int
197 offset_int op (un)signed HOST_WIDE_INT -> offset_int
198 (un)signed HOST_WIDE_INT op offset_int -> offset_int
200 - widest_int op widest_int -> widest_int
201 widest_int op (un)signed HOST_WIDE_INT -> widest_int
202 (un)signed HOST_WIDE_INT op widest_int -> widest_int
204 Other combinations like:
206 - widest_int op offset_int and
207 - wide_int op offset_int
209 are not allowed. The inputs should instead be extended or truncated
210 so that they match.
212 The inputs to comparison functions like wi::eq_p and wi::lts_p
213 follow the same compatibility rules, although their return types
214 are different. Unary functions on X produce the same result as
215 a binary operation X + X. Shift functions X op Y also produce
216 the same result as X + X; the precision of the shift amount Y
217 can be arbitrarily different from X. */
219 #include "system.h"
220 #include "hwint.h"
221 #include "signop.h"
222 #include "insn-modes.h"
224 /* The MAX_BITSIZE_MODE_ANY_INT is automatically generated by a very
225 early examination of the target's mode file. The WIDE_INT_MAX_ELTS
226 can accomodate at least 1 more bit so that unsigned numbers of that
227 mode can be represented as a signed value. Note that it is still
228 possible to create fixed_wide_ints that have precisions greater than
229 MAX_BITSIZE_MODE_ANY_INT. This can be useful when representing a
230 double-width multiplication result, for example. */
231 #define WIDE_INT_MAX_ELTS \
232 ((MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT) / HOST_BITS_PER_WIDE_INT)
234 #define WIDE_INT_MAX_PRECISION (WIDE_INT_MAX_ELTS * HOST_BITS_PER_WIDE_INT)
236 /* This is the max size of any pointer on any machine. It does not
237 seem to be as easy to sniff this out of the machine description as
238 it is for MAX_BITSIZE_MODE_ANY_INT since targets may support
239 multiple address sizes and may have different address sizes for
240 different address spaces. However, currently the largest pointer
241 on any platform is 64 bits. When that changes, then it is likely
242 that a target hook should be defined so that targets can make this
243 value larger for those targets. */
244 #define ADDR_MAX_BITSIZE 64
246 /* This is the internal precision used when doing any address
247 arithmetic. The '4' is really 3 + 1. Three of the bits are for
248 the number of extra bits needed to do bit addresses and the other bit
249 is to allow everything to be signed without loosing any precision.
250 Then everything is rounded up to the next HWI for efficiency. */
251 #define ADDR_MAX_PRECISION \
252 ((ADDR_MAX_BITSIZE + 4 + HOST_BITS_PER_WIDE_INT - 1) \
253 & ~(HOST_BITS_PER_WIDE_INT - 1))
255 /* The number of HWIs needed to store an offset_int. */
256 #define OFFSET_INT_ELTS (ADDR_MAX_PRECISION / HOST_BITS_PER_WIDE_INT)
258 /* The type of result produced by a binary operation on types T1 and T2.
259 Defined purely for brevity. */
260 #define WI_BINARY_RESULT(T1, T2) \
261 typename wi::binary_traits <T1, T2>::result_type
263 /* The type of result produced by a unary operation on type T. */
264 #define WI_UNARY_RESULT(T) \
265 typename wi::unary_traits <T>::result_type
267 /* Define a variable RESULT to hold the result of a binary operation on
268 X and Y, which have types T1 and T2 respectively. Define VAL to
269 point to the blocks of RESULT. Once the user of the macro has
270 filled in VAL, it should call RESULT.set_len to set the number
271 of initialized blocks. */
272 #define WI_BINARY_RESULT_VAR(RESULT, VAL, T1, X, T2, Y) \
273 WI_BINARY_RESULT (T1, T2) RESULT = \
274 wi::int_traits <WI_BINARY_RESULT (T1, T2)>::get_binary_result (X, Y); \
275 HOST_WIDE_INT *VAL = RESULT.write_val ()
277 /* Similar for the result of a unary operation on X, which has type T. */
278 #define WI_UNARY_RESULT_VAR(RESULT, VAL, T, X) \
279 WI_UNARY_RESULT (T) RESULT = \
280 wi::int_traits <WI_UNARY_RESULT (T)>::get_binary_result (X, X); \
281 HOST_WIDE_INT *VAL = RESULT.write_val ()
283 template <typename T> class generic_wide_int;
284 template <int N> struct fixed_wide_int_storage;
285 class wide_int_storage;
287 /* An N-bit integer. Until we can use typedef templates, use this instead. */
288 #define FIXED_WIDE_INT(N) \
289 generic_wide_int < fixed_wide_int_storage <N> >
291 typedef generic_wide_int <wide_int_storage> wide_int;
292 typedef FIXED_WIDE_INT (ADDR_MAX_PRECISION) offset_int;
293 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION) widest_int;
295 template <bool SE>
296 struct wide_int_ref_storage;
298 typedef generic_wide_int <wide_int_ref_storage <false> > wide_int_ref;
300 /* This can be used instead of wide_int_ref if the referenced value is
301 known to have type T. It carries across properties of T's representation,
302 such as whether excess upper bits in a HWI are defined, and can therefore
303 help avoid redundant work.
305 The macro could be replaced with a template typedef, once we're able
306 to use those. */
307 #define WIDE_INT_REF_FOR(T) \
308 generic_wide_int \
309 <wide_int_ref_storage <wi::int_traits <T>::is_sign_extended> >
311 namespace wi
313 /* Classifies an integer based on its precision. */
314 enum precision_type {
315 /* The integer has both a precision and defined signedness. This allows
316 the integer to be converted to any width, since we know whether to fill
317 any extra bits with zeros or signs. */
318 FLEXIBLE_PRECISION,
320 /* The integer has a variable precision but no defined signedness. */
321 VAR_PRECISION,
323 /* The integer has a constant precision (known at GCC compile time)
324 but no defined signedness. */
325 CONST_PRECISION
328 /* This class, which has no default implementation, is expected to
329 provide the following members:
331 static const enum precision_type precision_type;
332 Classifies the type of T.
334 static const unsigned int precision;
335 Only defined if precision_type == CONST_PRECISION. Specifies the
336 precision of all integers of type T.
338 static const bool host_dependent_precision;
339 True if the precision of T depends (or can depend) on the host.
341 static unsigned int get_precision (const T &x)
342 Return the number of bits in X.
344 static wi::storage_ref *decompose (HOST_WIDE_INT *scratch,
345 unsigned int precision, const T &x)
346 Decompose X as a PRECISION-bit integer, returning the associated
347 wi::storage_ref. SCRATCH is available as scratch space if needed.
348 The routine should assert that PRECISION is acceptable. */
349 template <typename T> struct int_traits;
351 /* This class provides a single type, result_type, which specifies the
352 type of integer produced by a binary operation whose inputs have
353 types T1 and T2. The definition should be symmetric. */
354 template <typename T1, typename T2,
355 enum precision_type P1 = int_traits <T1>::precision_type,
356 enum precision_type P2 = int_traits <T2>::precision_type>
357 struct binary_traits;
359 /* The result of a unary operation on T is the same as the result of
360 a binary operation on two values of type T. */
361 template <typename T>
362 struct unary_traits : public binary_traits <T, T> {};
364 /* Specify the result type for each supported combination of binary
365 inputs. Note that CONST_PRECISION and VAR_PRECISION cannot be
366 mixed, in order to give stronger type checking. When both inputs
367 are CONST_PRECISION, they must have the same precision. */
368 template <>
369 template <typename T1, typename T2>
370 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, FLEXIBLE_PRECISION>
372 typedef widest_int result_type;
375 template <>
376 template <typename T1, typename T2>
377 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, VAR_PRECISION>
379 typedef wide_int result_type;
382 template <>
383 template <typename T1, typename T2>
384 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, CONST_PRECISION>
386 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
387 so as not to confuse gengtype. */
388 typedef generic_wide_int < fixed_wide_int_storage
389 <int_traits <T2>::precision> > result_type;
392 template <>
393 template <typename T1, typename T2>
394 struct binary_traits <T1, T2, VAR_PRECISION, FLEXIBLE_PRECISION>
396 typedef wide_int result_type;
399 template <>
400 template <typename T1, typename T2>
401 struct binary_traits <T1, T2, CONST_PRECISION, FLEXIBLE_PRECISION>
403 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
404 so as not to confuse gengtype. */
405 typedef generic_wide_int < fixed_wide_int_storage
406 <int_traits <T1>::precision> > result_type;
409 template <>
410 template <typename T1, typename T2>
411 struct binary_traits <T1, T2, CONST_PRECISION, CONST_PRECISION>
413 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
414 so as not to confuse gengtype. */
415 STATIC_ASSERT (int_traits <T1>::precision == int_traits <T2>::precision);
416 typedef generic_wide_int < fixed_wide_int_storage
417 <int_traits <T1>::precision> > result_type;
420 template <>
421 template <typename T1, typename T2>
422 struct binary_traits <T1, T2, VAR_PRECISION, VAR_PRECISION>
424 typedef wide_int result_type;
428 /* Public functions for querying and operating on integers. */
429 namespace wi
431 template <typename T>
432 unsigned int get_precision (const T &);
434 template <typename T1, typename T2>
435 unsigned int get_binary_precision (const T1 &, const T2 &);
437 template <typename T1, typename T2>
438 void copy (T1 &, const T2 &);
440 #define UNARY_PREDICATE \
441 template <typename T> bool
442 #define UNARY_FUNCTION \
443 template <typename T> WI_UNARY_RESULT (T)
444 #define BINARY_PREDICATE \
445 template <typename T1, typename T2> bool
446 #define BINARY_FUNCTION \
447 template <typename T1, typename T2> WI_BINARY_RESULT (T1, T2)
448 #define SHIFT_FUNCTION \
449 template <typename T1, typename T2> WI_UNARY_RESULT (T1)
451 UNARY_PREDICATE fits_shwi_p (const T &);
452 UNARY_PREDICATE fits_uhwi_p (const T &);
453 UNARY_PREDICATE neg_p (const T &, signop = SIGNED);
455 template <typename T>
456 HOST_WIDE_INT sign_mask (const T &);
458 BINARY_PREDICATE eq_p (const T1 &, const T2 &);
459 BINARY_PREDICATE ne_p (const T1 &, const T2 &);
460 BINARY_PREDICATE lt_p (const T1 &, const T2 &, signop);
461 BINARY_PREDICATE lts_p (const T1 &, const T2 &);
462 BINARY_PREDICATE ltu_p (const T1 &, const T2 &);
463 BINARY_PREDICATE le_p (const T1 &, const T2 &, signop);
464 BINARY_PREDICATE les_p (const T1 &, const T2 &);
465 BINARY_PREDICATE leu_p (const T1 &, const T2 &);
466 BINARY_PREDICATE gt_p (const T1 &, const T2 &, signop);
467 BINARY_PREDICATE gts_p (const T1 &, const T2 &);
468 BINARY_PREDICATE gtu_p (const T1 &, const T2 &);
469 BINARY_PREDICATE ge_p (const T1 &, const T2 &, signop);
470 BINARY_PREDICATE ges_p (const T1 &, const T2 &);
471 BINARY_PREDICATE geu_p (const T1 &, const T2 &);
473 template <typename T1, typename T2>
474 int cmp (const T1 &, const T2 &, signop);
476 template <typename T1, typename T2>
477 int cmps (const T1 &, const T2 &);
479 template <typename T1, typename T2>
480 int cmpu (const T1 &, const T2 &);
482 UNARY_FUNCTION bit_not (const T &);
483 UNARY_FUNCTION neg (const T &);
484 UNARY_FUNCTION neg (const T &, bool *);
485 UNARY_FUNCTION abs (const T &);
486 UNARY_FUNCTION ext (const T &, unsigned int, signop);
487 UNARY_FUNCTION sext (const T &, unsigned int);
488 UNARY_FUNCTION zext (const T &, unsigned int);
489 UNARY_FUNCTION set_bit (const T &, unsigned int);
491 BINARY_FUNCTION min (const T1 &, const T2 &, signop);
492 BINARY_FUNCTION smin (const T1 &, const T2 &);
493 BINARY_FUNCTION umin (const T1 &, const T2 &);
494 BINARY_FUNCTION max (const T1 &, const T2 &, signop);
495 BINARY_FUNCTION smax (const T1 &, const T2 &);
496 BINARY_FUNCTION umax (const T1 &, const T2 &);
498 BINARY_FUNCTION bit_and (const T1 &, const T2 &);
499 BINARY_FUNCTION bit_and_not (const T1 &, const T2 &);
500 BINARY_FUNCTION bit_or (const T1 &, const T2 &);
501 BINARY_FUNCTION bit_or_not (const T1 &, const T2 &);
502 BINARY_FUNCTION bit_xor (const T1 &, const T2 &);
503 BINARY_FUNCTION add (const T1 &, const T2 &);
504 BINARY_FUNCTION add (const T1 &, const T2 &, signop, bool *);
505 BINARY_FUNCTION sub (const T1 &, const T2 &);
506 BINARY_FUNCTION sub (const T1 &, const T2 &, signop, bool *);
507 BINARY_FUNCTION mul (const T1 &, const T2 &);
508 BINARY_FUNCTION mul (const T1 &, const T2 &, signop, bool *);
509 BINARY_FUNCTION smul (const T1 &, const T2 &, bool *);
510 BINARY_FUNCTION umul (const T1 &, const T2 &, bool *);
511 BINARY_FUNCTION mul_high (const T1 &, const T2 &, signop);
512 BINARY_FUNCTION div_trunc (const T1 &, const T2 &, signop, bool * = 0);
513 BINARY_FUNCTION sdiv_trunc (const T1 &, const T2 &);
514 BINARY_FUNCTION udiv_trunc (const T1 &, const T2 &);
515 BINARY_FUNCTION div_floor (const T1 &, const T2 &, signop, bool * = 0);
516 BINARY_FUNCTION udiv_floor (const T1 &, const T2 &);
517 BINARY_FUNCTION sdiv_floor (const T1 &, const T2 &);
518 BINARY_FUNCTION div_ceil (const T1 &, const T2 &, signop, bool * = 0);
519 BINARY_FUNCTION div_round (const T1 &, const T2 &, signop, bool * = 0);
520 BINARY_FUNCTION divmod_trunc (const T1 &, const T2 &, signop,
521 WI_BINARY_RESULT (T1, T2) *);
522 BINARY_FUNCTION mod_trunc (const T1 &, const T2 &, signop, bool * = 0);
523 BINARY_FUNCTION smod_trunc (const T1 &, const T2 &);
524 BINARY_FUNCTION umod_trunc (const T1 &, const T2 &);
525 BINARY_FUNCTION mod_floor (const T1 &, const T2 &, signop, bool * = 0);
526 BINARY_FUNCTION umod_floor (const T1 &, const T2 &);
527 BINARY_FUNCTION mod_ceil (const T1 &, const T2 &, signop, bool * = 0);
528 BINARY_FUNCTION mod_round (const T1 &, const T2 &, signop, bool * = 0);
530 template <typename T1, typename T2>
531 bool multiple_of_p (const T1 &, const T2 &, signop);
533 template <typename T1, typename T2>
534 bool multiple_of_p (const T1 &, const T2 &, signop,
535 WI_BINARY_RESULT (T1, T2) *);
537 SHIFT_FUNCTION lshift (const T1 &, const T2 &);
538 SHIFT_FUNCTION lrshift (const T1 &, const T2 &);
539 SHIFT_FUNCTION arshift (const T1 &, const T2 &);
540 SHIFT_FUNCTION rshift (const T1 &, const T2 &, signop sgn);
541 SHIFT_FUNCTION lrotate (const T1 &, const T2 &, unsigned int = 0);
542 SHIFT_FUNCTION rrotate (const T1 &, const T2 &, unsigned int = 0);
544 #undef SHIFT_FUNCTION
545 #undef BINARY_PREDICATE
546 #undef BINARY_FUNCTION
547 #undef UNARY_PREDICATE
548 #undef UNARY_FUNCTION
550 bool only_sign_bit_p (const wide_int_ref &, unsigned int);
551 bool only_sign_bit_p (const wide_int_ref &);
552 int clz (const wide_int_ref &);
553 int clrsb (const wide_int_ref &);
554 int ctz (const wide_int_ref &);
555 int exact_log2 (const wide_int_ref &);
556 int floor_log2 (const wide_int_ref &);
557 int ffs (const wide_int_ref &);
558 int popcount (const wide_int_ref &);
559 int parity (const wide_int_ref &);
561 template <typename T>
562 unsigned HOST_WIDE_INT extract_uhwi (const T &, unsigned int, unsigned int);
564 template <typename T>
565 unsigned int min_precision (const T &, signop);
568 namespace wi
570 /* Contains the components of a decomposed integer for easy, direct
571 access. */
572 struct storage_ref
574 storage_ref (const HOST_WIDE_INT *, unsigned int, unsigned int);
576 const HOST_WIDE_INT *val;
577 unsigned int len;
578 unsigned int precision;
580 /* Provide enough trappings for this class to act as storage for
581 generic_wide_int. */
582 unsigned int get_len () const;
583 unsigned int get_precision () const;
584 const HOST_WIDE_INT *get_val () const;
588 inline::wi::storage_ref::storage_ref (const HOST_WIDE_INT *val_in,
589 unsigned int len_in,
590 unsigned int precision_in)
591 : val (val_in), len (len_in), precision (precision_in)
595 inline unsigned int
596 wi::storage_ref::get_len () const
598 return len;
601 inline unsigned int
602 wi::storage_ref::get_precision () const
604 return precision;
607 inline const HOST_WIDE_INT *
608 wi::storage_ref::get_val () const
610 return val;
613 /* This class defines an integer type using the storage provided by the
614 template argument. The storage class must provide the following
615 functions:
617 unsigned int get_precision () const
618 Return the number of bits in the integer.
620 HOST_WIDE_INT *get_val () const
621 Return a pointer to the array of blocks that encodes the integer.
623 unsigned int get_len () const
624 Return the number of blocks in get_val (). If this is smaller
625 than the number of blocks implied by get_precision (), the
626 remaining blocks are sign extensions of block get_len () - 1.
628 Although not required by generic_wide_int itself, writable storage
629 classes can also provide the following functions:
631 HOST_WIDE_INT *write_val ()
632 Get a modifiable version of get_val ()
634 unsigned int set_len (unsigned int len)
635 Set the value returned by get_len () to LEN. */
636 template <typename storage>
637 class GTY(()) generic_wide_int : public storage
639 public:
640 generic_wide_int ();
642 template <typename T>
643 generic_wide_int (const T &);
645 template <typename T>
646 generic_wide_int (const T &, unsigned int);
648 /* Conversions. */
649 HOST_WIDE_INT to_shwi (unsigned int) const;
650 HOST_WIDE_INT to_shwi () const;
651 unsigned HOST_WIDE_INT to_uhwi (unsigned int) const;
652 unsigned HOST_WIDE_INT to_uhwi () const;
653 HOST_WIDE_INT to_short_addr () const;
655 /* Public accessors for the interior of a wide int. */
656 HOST_WIDE_INT sign_mask () const;
657 HOST_WIDE_INT elt (unsigned int) const;
658 unsigned HOST_WIDE_INT ulow () const;
659 unsigned HOST_WIDE_INT uhigh () const;
660 HOST_WIDE_INT slow () const;
661 HOST_WIDE_INT shigh () const;
663 template <typename T>
664 generic_wide_int &operator = (const T &);
666 #define BINARY_PREDICATE(OP, F) \
667 template <typename T> \
668 bool OP (const T &c) const { return wi::F (*this, c); }
670 #define UNARY_OPERATOR(OP, F) \
671 WI_UNARY_RESULT (generic_wide_int) OP () const { return wi::F (*this); }
673 #define BINARY_OPERATOR(OP, F) \
674 template <typename T> \
675 WI_BINARY_RESULT (generic_wide_int, T) \
676 OP (const T &c) const { return wi::F (*this, c); }
678 #define ASSIGNMENT_OPERATOR(OP, F) \
679 template <typename T> \
680 generic_wide_int &OP (const T &c) { return (*this = wi::F (*this, c)); }
682 #define INCDEC_OPERATOR(OP, DELTA) \
683 generic_wide_int &OP () { *this += DELTA; return *this; }
685 UNARY_OPERATOR (operator ~, bit_not)
686 UNARY_OPERATOR (operator -, neg)
687 BINARY_PREDICATE (operator ==, eq_p)
688 BINARY_PREDICATE (operator !=, ne_p)
689 BINARY_OPERATOR (operator &, bit_and)
690 BINARY_OPERATOR (and_not, bit_and_not)
691 BINARY_OPERATOR (operator |, bit_or)
692 BINARY_OPERATOR (or_not, bit_or_not)
693 BINARY_OPERATOR (operator ^, bit_xor)
694 BINARY_OPERATOR (operator +, add)
695 BINARY_OPERATOR (operator -, sub)
696 BINARY_OPERATOR (operator *, mul)
697 ASSIGNMENT_OPERATOR (operator &=, bit_and)
698 ASSIGNMENT_OPERATOR (operator |=, bit_or)
699 ASSIGNMENT_OPERATOR (operator ^=, bit_xor)
700 ASSIGNMENT_OPERATOR (operator +=, add)
701 ASSIGNMENT_OPERATOR (operator -=, sub)
702 ASSIGNMENT_OPERATOR (operator *=, mul)
703 INCDEC_OPERATOR (operator ++, 1)
704 INCDEC_OPERATOR (operator --, -1)
706 #undef BINARY_PREDICATE
707 #undef UNARY_OPERATOR
708 #undef BINARY_OPERATOR
709 #undef ASSIGNMENT_OPERATOR
710 #undef INCDEC_OPERATOR
712 /* Debugging functions. */
713 void dump () const;
715 static const bool is_sign_extended
716 = wi::int_traits <generic_wide_int <storage> >::is_sign_extended;
719 template <typename storage>
720 inline generic_wide_int <storage>::generic_wide_int () {}
722 template <typename storage>
723 template <typename T>
724 inline generic_wide_int <storage>::generic_wide_int (const T &x)
725 : storage (x)
729 template <typename storage>
730 template <typename T>
731 inline generic_wide_int <storage>::generic_wide_int (const T &x,
732 unsigned int precision)
733 : storage (x, precision)
737 /* Return THIS as a signed HOST_WIDE_INT, sign-extending from PRECISION.
738 If THIS does not fit in PRECISION, the information is lost. */
739 template <typename storage>
740 inline HOST_WIDE_INT
741 generic_wide_int <storage>::to_shwi (unsigned int precision) const
743 if (precision < HOST_BITS_PER_WIDE_INT)
744 return sext_hwi (this->get_val ()[0], precision);
745 else
746 return this->get_val ()[0];
749 /* Return THIS as a signed HOST_WIDE_INT, in its natural precision. */
750 template <typename storage>
751 inline HOST_WIDE_INT
752 generic_wide_int <storage>::to_shwi () const
754 if (is_sign_extended)
755 return this->get_val ()[0];
756 else
757 return to_shwi (this->get_precision ());
760 /* Return THIS as an unsigned HOST_WIDE_INT, zero-extending from
761 PRECISION. If THIS does not fit in PRECISION, the information
762 is lost. */
763 template <typename storage>
764 inline unsigned HOST_WIDE_INT
765 generic_wide_int <storage>::to_uhwi (unsigned int precision) const
767 if (precision < HOST_BITS_PER_WIDE_INT)
768 return zext_hwi (this->get_val ()[0], precision);
769 else
770 return this->get_val ()[0];
773 /* Return THIS as an signed HOST_WIDE_INT, in its natural precision. */
774 template <typename storage>
775 inline unsigned HOST_WIDE_INT
776 generic_wide_int <storage>::to_uhwi () const
778 return to_uhwi (this->get_precision ());
781 /* TODO: The compiler is half converted from using HOST_WIDE_INT to
782 represent addresses to using offset_int to represent addresses.
783 We use to_short_addr at the interface from new code to old,
784 unconverted code. */
785 template <typename storage>
786 inline HOST_WIDE_INT
787 generic_wide_int <storage>::to_short_addr () const
789 return this->get_val ()[0];
792 /* Return the implicit value of blocks above get_len (). */
793 template <typename storage>
794 inline HOST_WIDE_INT
795 generic_wide_int <storage>::sign_mask () const
797 unsigned int len = this->get_len ();
798 unsigned HOST_WIDE_INT high = this->get_val ()[len - 1];
799 if (!is_sign_extended)
801 unsigned int precision = this->get_precision ();
802 int excess = len * HOST_BITS_PER_WIDE_INT - precision;
803 if (excess > 0)
804 high <<= excess;
806 return (HOST_WIDE_INT) (high) < 0 ? -1 : 0;
809 /* Return the signed value of the least-significant explicitly-encoded
810 block. */
811 template <typename storage>
812 inline HOST_WIDE_INT
813 generic_wide_int <storage>::slow () const
815 return this->get_val ()[0];
818 /* Return the signed value of the most-significant explicitly-encoded
819 block. */
820 template <typename storage>
821 inline HOST_WIDE_INT
822 generic_wide_int <storage>::shigh () const
824 return this->get_val ()[this->get_len () - 1];
827 /* Return the unsigned value of the least-significant
828 explicitly-encoded block. */
829 template <typename storage>
830 inline unsigned HOST_WIDE_INT
831 generic_wide_int <storage>::ulow () const
833 return this->get_val ()[0];
836 /* Return the unsigned value of the most-significant
837 explicitly-encoded block. */
838 template <typename storage>
839 inline unsigned HOST_WIDE_INT
840 generic_wide_int <storage>::uhigh () const
842 return this->get_val ()[this->get_len () - 1];
845 /* Return block I, which might be implicitly or explicit encoded. */
846 template <typename storage>
847 inline HOST_WIDE_INT
848 generic_wide_int <storage>::elt (unsigned int i) const
850 if (i >= this->get_len ())
851 return sign_mask ();
852 else
853 return this->get_val ()[i];
856 template <typename storage>
857 template <typename T>
858 generic_wide_int <storage> &
859 generic_wide_int <storage>::operator = (const T &x)
861 storage::operator = (x);
862 return *this;
865 /* Dump the contents of the integer to stderr, for debugging. */
866 template <typename storage>
867 void
868 generic_wide_int <storage>::dump () const
870 unsigned int len = this->get_len ();
871 const HOST_WIDE_INT *val = this->get_val ();
872 unsigned int precision = this->get_precision ();
873 fprintf (stderr, "[");
874 if (len * HOST_BITS_PER_WIDE_INT < precision)
875 fprintf (stderr, "...,");
876 for (unsigned int i = 0; i < len - 1; ++i)
877 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX ",", val[len - 1 - i]);
878 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX "], precision = %d\n",
879 val[0], precision);
882 namespace wi
884 template <>
885 template <typename storage>
886 struct int_traits < generic_wide_int <storage> >
887 : public wi::int_traits <storage>
889 static unsigned int get_precision (const generic_wide_int <storage> &);
890 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
891 const generic_wide_int <storage> &);
895 template <typename storage>
896 inline unsigned int
897 wi::int_traits < generic_wide_int <storage> >::
898 get_precision (const generic_wide_int <storage> &x)
900 return x.get_precision ();
903 template <typename storage>
904 inline wi::storage_ref
905 wi::int_traits < generic_wide_int <storage> >::
906 decompose (HOST_WIDE_INT *, unsigned int precision,
907 const generic_wide_int <storage> &x)
909 gcc_checking_assert (precision == x.get_precision ());
910 return wi::storage_ref (x.get_val (), x.get_len (), precision);
913 /* Provide the storage for a wide_int_ref. This acts like a read-only
914 wide_int, with the optimization that VAL is normally a pointer to
915 another integer's storage, so that no array copy is needed. */
916 template <bool SE>
917 struct wide_int_ref_storage : public wi::storage_ref
919 private:
920 /* Scratch space that can be used when decomposing the original integer.
921 It must live as long as this object. */
922 HOST_WIDE_INT scratch[2];
924 public:
925 wide_int_ref_storage (const wi::storage_ref &);
927 template <typename T>
928 wide_int_ref_storage (const T &);
930 template <typename T>
931 wide_int_ref_storage (const T &, unsigned int);
934 /* Create a reference from an existing reference. */
935 template <bool SE>
936 inline wide_int_ref_storage <SE>::
937 wide_int_ref_storage (const wi::storage_ref &x)
938 : storage_ref (x)
941 /* Create a reference to integer X in its natural precision. Note
942 that the natural precision is host-dependent for primitive
943 types. */
944 template <bool SE>
945 template <typename T>
946 inline wide_int_ref_storage <SE>::wide_int_ref_storage (const T &x)
947 : storage_ref (wi::int_traits <T>::decompose (scratch,
948 wi::get_precision (x), x))
952 /* Create a reference to integer X in precision PRECISION. */
953 template <bool SE>
954 template <typename T>
955 inline wide_int_ref_storage <SE>::wide_int_ref_storage (const T &x,
956 unsigned int precision)
957 : storage_ref (wi::int_traits <T>::decompose (scratch, precision, x))
961 namespace wi
963 template <>
964 template <bool SE>
965 struct int_traits <wide_int_ref_storage <SE> >
967 static const enum precision_type precision_type = VAR_PRECISION;
968 /* wi::storage_ref can be a reference to a primitive type,
969 so this is the conservatively-correct setting. */
970 static const bool host_dependent_precision = true;
971 static const bool is_sign_extended = SE;
975 namespace wi
977 unsigned int force_to_size (HOST_WIDE_INT *, const HOST_WIDE_INT *,
978 unsigned int, unsigned int, unsigned int,
979 signop sgn);
980 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
981 unsigned int, unsigned int, bool = true);
984 /* The storage used by wide_int. */
985 class GTY(()) wide_int_storage
987 private:
988 HOST_WIDE_INT val[WIDE_INT_MAX_ELTS];
989 unsigned int len;
990 unsigned int precision;
992 public:
993 wide_int_storage ();
994 template <typename T>
995 wide_int_storage (const T &);
997 /* The standard generic_wide_int storage methods. */
998 unsigned int get_precision () const;
999 const HOST_WIDE_INT *get_val () const;
1000 unsigned int get_len () const;
1001 HOST_WIDE_INT *write_val ();
1002 void set_len (unsigned int, bool = false);
1004 static wide_int from (const wide_int_ref &, unsigned int, signop);
1005 static wide_int from_array (const HOST_WIDE_INT *, unsigned int,
1006 unsigned int, bool = true);
1007 static wide_int create (unsigned int);
1009 /* FIXME: target-dependent, so should disappear. */
1010 wide_int bswap () const;
1013 namespace wi
1015 template <>
1016 struct int_traits <wide_int_storage>
1018 static const enum precision_type precision_type = VAR_PRECISION;
1019 /* Guaranteed by a static assert in the wide_int_storage constructor. */
1020 static const bool host_dependent_precision = false;
1021 static const bool is_sign_extended = true;
1022 template <typename T1, typename T2>
1023 static wide_int get_binary_result (const T1 &, const T2 &);
1027 inline wide_int_storage::wide_int_storage () {}
1029 /* Initialize the storage from integer X, in its natural precision.
1030 Note that we do not allow integers with host-dependent precision
1031 to become wide_ints; wide_ints must always be logically independent
1032 of the host. */
1033 template <typename T>
1034 inline wide_int_storage::wide_int_storage (const T &x)
1036 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision); }
1037 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION); }
1038 WIDE_INT_REF_FOR (T) xi (x);
1039 precision = xi.precision;
1040 wi::copy (*this, xi);
1043 inline unsigned int
1044 wide_int_storage::get_precision () const
1046 return precision;
1049 inline const HOST_WIDE_INT *
1050 wide_int_storage::get_val () const
1052 return val;
1055 inline unsigned int
1056 wide_int_storage::get_len () const
1058 return len;
1061 inline HOST_WIDE_INT *
1062 wide_int_storage::write_val ()
1064 return val;
1067 inline void
1068 wide_int_storage::set_len (unsigned int l, bool is_sign_extended)
1070 len = l;
1071 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > precision)
1072 val[len - 1] = sext_hwi (val[len - 1],
1073 precision % HOST_BITS_PER_WIDE_INT);
1076 /* Treat X as having signedness SGN and convert it to a PRECISION-bit
1077 number. */
1078 inline wide_int
1079 wide_int_storage::from (const wide_int_ref &x, unsigned int precision,
1080 signop sgn)
1082 wide_int result = wide_int::create (precision);
1083 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1084 x.precision, precision, sgn));
1085 return result;
1088 /* Create a wide_int from the explicit block encoding given by VAL and
1089 LEN. PRECISION is the precision of the integer. NEED_CANON_P is
1090 true if the encoding may have redundant trailing blocks. */
1091 inline wide_int
1092 wide_int_storage::from_array (const HOST_WIDE_INT *val, unsigned int len,
1093 unsigned int precision, bool need_canon_p)
1095 wide_int result = wide_int::create (precision);
1096 result.set_len (wi::from_array (result.write_val (), val, len, precision,
1097 need_canon_p));
1098 return result;
1101 /* Return an uninitialized wide_int with precision PRECISION. */
1102 inline wide_int
1103 wide_int_storage::create (unsigned int precision)
1105 wide_int x;
1106 x.precision = precision;
1107 return x;
1110 template <typename T1, typename T2>
1111 inline wide_int
1112 wi::int_traits <wide_int_storage>::get_binary_result (const T1 &x, const T2 &y)
1114 /* This shouldn't be used for two flexible-precision inputs. */
1115 STATIC_ASSERT (wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION
1116 || wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION);
1117 if (wi::int_traits <T1>::precision_type == FLEXIBLE_PRECISION)
1118 return wide_int::create (wi::get_precision (y));
1119 else
1120 return wide_int::create (wi::get_precision (x));
1123 /* The storage used by FIXED_WIDE_INT (N). */
1124 template <int N>
1125 class GTY(()) fixed_wide_int_storage
1127 private:
1128 HOST_WIDE_INT val[(N + HOST_BITS_PER_WIDE_INT + 1) / HOST_BITS_PER_WIDE_INT];
1129 unsigned int len;
1131 public:
1132 fixed_wide_int_storage ();
1133 template <typename T>
1134 fixed_wide_int_storage (const T &);
1136 /* The standard generic_wide_int storage methods. */
1137 unsigned int get_precision () const;
1138 const HOST_WIDE_INT *get_val () const;
1139 unsigned int get_len () const;
1140 HOST_WIDE_INT *write_val ();
1141 void set_len (unsigned int, bool = false);
1143 static FIXED_WIDE_INT (N) from (const wide_int_ref &, signop);
1144 static FIXED_WIDE_INT (N) from_array (const HOST_WIDE_INT *, unsigned int,
1145 bool = true);
1148 namespace wi
1150 template <>
1151 template <int N>
1152 struct int_traits < fixed_wide_int_storage <N> >
1154 static const enum precision_type precision_type = CONST_PRECISION;
1155 static const bool host_dependent_precision = false;
1156 static const bool is_sign_extended = true;
1157 static const unsigned int precision = N;
1158 template <typename T1, typename T2>
1159 static FIXED_WIDE_INT (N) get_binary_result (const T1 &, const T2 &);
1163 template <int N>
1164 inline fixed_wide_int_storage <N>::fixed_wide_int_storage () {}
1166 /* Initialize the storage from integer X, in precision N. */
1167 template <int N>
1168 template <typename T>
1169 inline fixed_wide_int_storage <N>::fixed_wide_int_storage (const T &x)
1171 /* Check for type compatibility. We don't want to initialize a
1172 fixed-width integer from something like a wide_int. */
1173 WI_BINARY_RESULT (T, FIXED_WIDE_INT (N)) *assertion ATTRIBUTE_UNUSED;
1174 wi::copy (*this, WIDE_INT_REF_FOR (T) (x, N));
1177 template <int N>
1178 inline unsigned int
1179 fixed_wide_int_storage <N>::get_precision () const
1181 return N;
1184 template <int N>
1185 inline const HOST_WIDE_INT *
1186 fixed_wide_int_storage <N>::get_val () const
1188 return val;
1191 template <int N>
1192 inline unsigned int
1193 fixed_wide_int_storage <N>::get_len () const
1195 return len;
1198 template <int N>
1199 inline HOST_WIDE_INT *
1200 fixed_wide_int_storage <N>::write_val ()
1202 return val;
1205 template <int N>
1206 inline void
1207 fixed_wide_int_storage <N>::set_len (unsigned int l, bool)
1209 len = l;
1210 /* There are no excess bits in val[len - 1]. */
1211 STATIC_ASSERT (N % HOST_BITS_PER_WIDE_INT == 0);
1214 /* Treat X as having signedness SGN and convert it to an N-bit number. */
1215 template <int N>
1216 inline FIXED_WIDE_INT (N)
1217 fixed_wide_int_storage <N>::from (const wide_int_ref &x, signop sgn)
1219 FIXED_WIDE_INT (N) result;
1220 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1221 x.precision, N, sgn));
1222 return result;
1225 /* Create a FIXED_WIDE_INT (N) from the explicit block encoding given by
1226 VAL and LEN. NEED_CANON_P is true if the encoding may have redundant
1227 trailing blocks. */
1228 template <int N>
1229 inline FIXED_WIDE_INT (N)
1230 fixed_wide_int_storage <N>::from_array (const HOST_WIDE_INT *val,
1231 unsigned int len,
1232 bool need_canon_p)
1234 FIXED_WIDE_INT (N) result;
1235 result.set_len (wi::from_array (result.write_val (), val, len,
1236 N, need_canon_p));
1237 return result;
1240 template <int N>
1241 template <typename T1, typename T2>
1242 inline FIXED_WIDE_INT (N)
1243 wi::int_traits < fixed_wide_int_storage <N> >::
1244 get_binary_result (const T1 &, const T2 &)
1246 return FIXED_WIDE_INT (N) ();
1249 /* A reference to one element of a trailing_wide_ints structure. */
1250 class trailing_wide_int_storage
1252 private:
1253 /* The precision of the integer, which is a fixed property of the
1254 parent trailing_wide_ints. */
1255 unsigned int m_precision;
1257 /* A pointer to the length field. */
1258 unsigned char *m_len;
1260 /* A pointer to the HWI array. There are enough elements to hold all
1261 values of precision M_PRECISION. */
1262 HOST_WIDE_INT *m_val;
1264 public:
1265 trailing_wide_int_storage (unsigned int, unsigned char *, HOST_WIDE_INT *);
1267 /* The standard generic_wide_int storage methods. */
1268 unsigned int get_len () const;
1269 unsigned int get_precision () const;
1270 const HOST_WIDE_INT *get_val () const;
1271 HOST_WIDE_INT *write_val ();
1272 void set_len (unsigned int, bool = false);
1274 template <typename T>
1275 trailing_wide_int_storage &operator = (const T &);
1278 typedef generic_wide_int <trailing_wide_int_storage> trailing_wide_int;
1280 /* trailing_wide_int behaves like a wide_int. */
1281 namespace wi
1283 template <>
1284 struct int_traits <trailing_wide_int_storage>
1285 : public int_traits <wide_int_storage> {};
1288 /* An array of N wide_int-like objects that can be put at the end of
1289 a variable-sized structure. Use extra_size to calculate how many
1290 bytes beyond the sizeof need to be allocated. Use set_precision
1291 to initialize the structure. */
1292 template <int N>
1293 class GTY(()) trailing_wide_ints
1295 private:
1296 /* The shared precision of each number. */
1297 unsigned short m_precision;
1299 /* The shared maximum length of each number. */
1300 unsigned char m_max_len;
1302 /* The current length of each number. */
1303 unsigned char m_len[N];
1305 /* The variable-length part of the structure, which always contains
1306 at least one HWI. Element I starts at index I * M_MAX_LEN. */
1307 HOST_WIDE_INT m_val[1];
1309 public:
1310 void set_precision (unsigned int);
1311 trailing_wide_int operator [] (unsigned int);
1312 static size_t extra_size (unsigned int);
1315 inline trailing_wide_int_storage::
1316 trailing_wide_int_storage (unsigned int precision, unsigned char *len,
1317 HOST_WIDE_INT *val)
1318 : m_precision (precision), m_len (len), m_val (val)
1322 inline unsigned int
1323 trailing_wide_int_storage::get_len () const
1325 return *m_len;
1328 inline unsigned int
1329 trailing_wide_int_storage::get_precision () const
1331 return m_precision;
1334 inline const HOST_WIDE_INT *
1335 trailing_wide_int_storage::get_val () const
1337 return m_val;
1340 inline HOST_WIDE_INT *
1341 trailing_wide_int_storage::write_val ()
1343 return m_val;
1346 inline void
1347 trailing_wide_int_storage::set_len (unsigned int len, bool is_sign_extended)
1349 *m_len = len;
1350 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > m_precision)
1351 m_val[len - 1] = sext_hwi (m_val[len - 1],
1352 m_precision % HOST_BITS_PER_WIDE_INT);
1355 template <typename T>
1356 inline trailing_wide_int_storage &
1357 trailing_wide_int_storage::operator = (const T &x)
1359 WIDE_INT_REF_FOR (T) xi (x, m_precision);
1360 wi::copy (*this, xi);
1361 return *this;
1364 /* Initialize the structure and record that all elements have precision
1365 PRECISION. */
1366 template <int N>
1367 inline void
1368 trailing_wide_ints <N>::set_precision (unsigned int precision)
1370 m_precision = precision;
1371 m_max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1372 / HOST_BITS_PER_WIDE_INT);
1375 /* Return a reference to element INDEX. */
1376 template <int N>
1377 inline trailing_wide_int
1378 trailing_wide_ints <N>::operator [] (unsigned int index)
1380 return trailing_wide_int_storage (m_precision, &m_len[index],
1381 &m_val[index * m_max_len]);
1384 /* Return how many extra bytes need to be added to the end of the structure
1385 in order to handle N wide_ints of precision PRECISION. */
1386 template <int N>
1387 inline size_t
1388 trailing_wide_ints <N>::extra_size (unsigned int precision)
1390 unsigned int max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1391 / HOST_BITS_PER_WIDE_INT);
1392 return (N * max_len - 1) * sizeof (HOST_WIDE_INT);
1395 /* This macro is used in structures that end with a trailing_wide_ints field
1396 called FIELD. It declares get_NAME() and set_NAME() methods to access
1397 element I of FIELD. */
1398 #define TRAILING_WIDE_INT_ACCESSOR(NAME, FIELD, I) \
1399 trailing_wide_int get_##NAME () { return FIELD[I]; } \
1400 template <typename T> void set_##NAME (const T &x) { FIELD[I] = x; }
1402 namespace wi
1404 /* Implementation of int_traits for primitive integer types like "int". */
1405 template <typename T, bool signed_p>
1406 struct primitive_int_traits
1408 static const enum precision_type precision_type = FLEXIBLE_PRECISION;
1409 static const bool host_dependent_precision = true;
1410 static const bool is_sign_extended = true;
1411 static unsigned int get_precision (T);
1412 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int, T);
1416 template <typename T, bool signed_p>
1417 inline unsigned int
1418 wi::primitive_int_traits <T, signed_p>::get_precision (T)
1420 return sizeof (T) * CHAR_BIT;
1423 template <typename T, bool signed_p>
1424 inline wi::storage_ref
1425 wi::primitive_int_traits <T, signed_p>::decompose (HOST_WIDE_INT *scratch,
1426 unsigned int precision, T x)
1428 scratch[0] = x;
1429 if (signed_p || scratch[0] >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1430 return wi::storage_ref (scratch, 1, precision);
1431 scratch[1] = 0;
1432 return wi::storage_ref (scratch, 2, precision);
1435 /* Allow primitive C types to be used in wi:: routines. */
1436 namespace wi
1438 template <>
1439 struct int_traits <int>
1440 : public primitive_int_traits <int, true> {};
1442 template <>
1443 struct int_traits <unsigned int>
1444 : public primitive_int_traits <unsigned int, false> {};
1446 template <>
1447 struct int_traits <long>
1448 : public primitive_int_traits <long, true> {};
1450 template <>
1451 struct int_traits <unsigned long>
1452 : public primitive_int_traits <unsigned long, false> {};
1454 #if defined HAVE_LONG_LONG
1455 template <>
1456 struct int_traits <long long>
1457 : public primitive_int_traits <long long, true> {};
1459 template <>
1460 struct int_traits <unsigned long long>
1461 : public primitive_int_traits <unsigned long long, false> {};
1462 #endif
1465 namespace wi
1467 /* Stores HWI-sized integer VAL, treating it as having signedness SGN
1468 and precision PRECISION. */
1469 struct hwi_with_prec
1471 hwi_with_prec (HOST_WIDE_INT, unsigned int, signop);
1472 HOST_WIDE_INT val;
1473 unsigned int precision;
1474 signop sgn;
1477 hwi_with_prec shwi (HOST_WIDE_INT, unsigned int);
1478 hwi_with_prec uhwi (unsigned HOST_WIDE_INT, unsigned int);
1480 hwi_with_prec minus_one (unsigned int);
1481 hwi_with_prec zero (unsigned int);
1482 hwi_with_prec one (unsigned int);
1483 hwi_with_prec two (unsigned int);
1486 inline wi::hwi_with_prec::hwi_with_prec (HOST_WIDE_INT v, unsigned int p,
1487 signop s)
1488 : val (v), precision (p), sgn (s)
1492 /* Return a signed integer that has value VAL and precision PRECISION. */
1493 inline wi::hwi_with_prec
1494 wi::shwi (HOST_WIDE_INT val, unsigned int precision)
1496 return hwi_with_prec (val, precision, SIGNED);
1499 /* Return an unsigned integer that has value VAL and precision PRECISION. */
1500 inline wi::hwi_with_prec
1501 wi::uhwi (unsigned HOST_WIDE_INT val, unsigned int precision)
1503 return hwi_with_prec (val, precision, UNSIGNED);
1506 /* Return a wide int of -1 with precision PRECISION. */
1507 inline wi::hwi_with_prec
1508 wi::minus_one (unsigned int precision)
1510 return wi::shwi (-1, precision);
1513 /* Return a wide int of 0 with precision PRECISION. */
1514 inline wi::hwi_with_prec
1515 wi::zero (unsigned int precision)
1517 return wi::shwi (0, precision);
1520 /* Return a wide int of 1 with precision PRECISION. */
1521 inline wi::hwi_with_prec
1522 wi::one (unsigned int precision)
1524 return wi::shwi (1, precision);
1527 /* Return a wide int of 2 with precision PRECISION. */
1528 inline wi::hwi_with_prec
1529 wi::two (unsigned int precision)
1531 return wi::shwi (2, precision);
1534 namespace wi
1536 template <>
1537 struct int_traits <wi::hwi_with_prec>
1539 static const enum precision_type precision_type = VAR_PRECISION;
1540 /* hwi_with_prec has an explicitly-given precision, rather than the
1541 precision of HOST_WIDE_INT. */
1542 static const bool host_dependent_precision = false;
1543 static const bool is_sign_extended = true;
1544 static unsigned int get_precision (const wi::hwi_with_prec &);
1545 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
1546 const wi::hwi_with_prec &);
1550 inline unsigned int
1551 wi::int_traits <wi::hwi_with_prec>::get_precision (const wi::hwi_with_prec &x)
1553 return x.precision;
1556 inline wi::storage_ref
1557 wi::int_traits <wi::hwi_with_prec>::
1558 decompose (HOST_WIDE_INT *scratch, unsigned int precision,
1559 const wi::hwi_with_prec &x)
1561 gcc_checking_assert (precision == x.precision);
1562 scratch[0] = x.val;
1563 if (x.sgn == SIGNED || x.val >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1564 return wi::storage_ref (scratch, 1, precision);
1565 scratch[1] = 0;
1566 return wi::storage_ref (scratch, 2, precision);
1569 /* Private functions for handling large cases out of line. They take
1570 individual length and array parameters because that is cheaper for
1571 the inline caller than constructing an object on the stack and
1572 passing a reference to it. (Although many callers use wide_int_refs,
1573 we generally want those to be removed by SRA.) */
1574 namespace wi
1576 bool eq_p_large (const HOST_WIDE_INT *, unsigned int,
1577 const HOST_WIDE_INT *, unsigned int, unsigned int);
1578 bool lts_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1579 const HOST_WIDE_INT *, unsigned int);
1580 bool ltu_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1581 const HOST_WIDE_INT *, unsigned int);
1582 int cmps_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1583 const HOST_WIDE_INT *, unsigned int);
1584 int cmpu_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1585 const HOST_WIDE_INT *, unsigned int);
1586 unsigned int sext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1587 unsigned int,
1588 unsigned int, unsigned int);
1589 unsigned int zext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1590 unsigned int,
1591 unsigned int, unsigned int);
1592 unsigned int set_bit_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1593 unsigned int, unsigned int, unsigned int);
1594 unsigned int lshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1595 unsigned int, unsigned int, unsigned int);
1596 unsigned int lrshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1597 unsigned int, unsigned int, unsigned int,
1598 unsigned int);
1599 unsigned int arshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1600 unsigned int, unsigned int, unsigned int,
1601 unsigned int);
1602 unsigned int and_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1603 const HOST_WIDE_INT *, unsigned int, unsigned int);
1604 unsigned int and_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1605 unsigned int, const HOST_WIDE_INT *,
1606 unsigned int, unsigned int);
1607 unsigned int or_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1608 const HOST_WIDE_INT *, unsigned int, unsigned int);
1609 unsigned int or_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1610 unsigned int, const HOST_WIDE_INT *,
1611 unsigned int, unsigned int);
1612 unsigned int xor_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1613 const HOST_WIDE_INT *, unsigned int, unsigned int);
1614 unsigned int add_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1615 const HOST_WIDE_INT *, unsigned int, unsigned int,
1616 signop, bool *);
1617 unsigned int sub_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1618 const HOST_WIDE_INT *, unsigned int, unsigned int,
1619 signop, bool *);
1620 unsigned int mul_internal (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1621 unsigned int, const HOST_WIDE_INT *,
1622 unsigned int, unsigned int, signop, bool *,
1623 bool);
1624 unsigned int divmod_internal (HOST_WIDE_INT *, unsigned int *,
1625 HOST_WIDE_INT *, const HOST_WIDE_INT *,
1626 unsigned int, unsigned int,
1627 const HOST_WIDE_INT *,
1628 unsigned int, unsigned int,
1629 signop, bool *);
1632 /* Return the number of bits that integer X can hold. */
1633 template <typename T>
1634 inline unsigned int
1635 wi::get_precision (const T &x)
1637 return wi::int_traits <T>::get_precision (x);
1640 /* Return the number of bits that the result of a binary operation can
1641 hold when the input operands are X and Y. */
1642 template <typename T1, typename T2>
1643 inline unsigned int
1644 wi::get_binary_precision (const T1 &x, const T2 &y)
1646 return get_precision (wi::int_traits <WI_BINARY_RESULT (T1, T2)>::
1647 get_binary_result (x, y));
1650 /* Copy the contents of Y to X, but keeping X's current precision. */
1651 template <typename T1, typename T2>
1652 inline void
1653 wi::copy (T1 &x, const T2 &y)
1655 HOST_WIDE_INT *xval = x.write_val ();
1656 const HOST_WIDE_INT *yval = y.get_val ();
1657 unsigned int len = y.get_len ();
1658 unsigned int i = 0;
1660 xval[i] = yval[i];
1661 while (++i < len);
1662 x.set_len (len, y.is_sign_extended);
1665 /* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
1666 template <typename T>
1667 inline bool
1668 wi::fits_shwi_p (const T &x)
1670 WIDE_INT_REF_FOR (T) xi (x);
1671 return xi.len == 1;
1674 /* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
1675 precision. */
1676 template <typename T>
1677 inline bool
1678 wi::fits_uhwi_p (const T &x)
1680 WIDE_INT_REF_FOR (T) xi (x);
1681 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
1682 return true;
1683 if (xi.len == 1)
1684 return xi.slow () >= 0;
1685 return xi.len == 2 && xi.uhigh () == 0;
1688 /* Return true if X is negative based on the interpretation of SGN.
1689 For UNSIGNED, this is always false. */
1690 template <typename T>
1691 inline bool
1692 wi::neg_p (const T &x, signop sgn)
1694 WIDE_INT_REF_FOR (T) xi (x);
1695 if (sgn == UNSIGNED)
1696 return false;
1697 return xi.sign_mask () < 0;
1700 /* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
1701 template <typename T>
1702 inline HOST_WIDE_INT
1703 wi::sign_mask (const T &x)
1705 WIDE_INT_REF_FOR (T) xi (x);
1706 return xi.sign_mask ();
1709 /* Return true if X == Y. X and Y must be binary-compatible. */
1710 template <typename T1, typename T2>
1711 inline bool
1712 wi::eq_p (const T1 &x, const T2 &y)
1714 unsigned int precision = get_binary_precision (x, y);
1715 WIDE_INT_REF_FOR (T1) xi (x, precision);
1716 WIDE_INT_REF_FOR (T2) yi (y, precision);
1717 if (xi.is_sign_extended && yi.is_sign_extended)
1719 /* This case reduces to array equality. */
1720 if (xi.len != yi.len)
1721 return false;
1722 unsigned int i = 0;
1724 if (xi.val[i] != yi.val[i])
1725 return false;
1726 while (++i != xi.len);
1727 return true;
1729 if (__builtin_expect (yi.len == 1, true))
1731 /* XI is only equal to YI if it too has a single HWI. */
1732 if (xi.len != 1)
1733 return false;
1734 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
1735 with 0 are simple. */
1736 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1737 return xi.val[0] == 0;
1738 /* Otherwise flush out any excess bits first. */
1739 unsigned HOST_WIDE_INT diff = xi.val[0] ^ yi.val[0];
1740 int excess = HOST_BITS_PER_WIDE_INT - precision;
1741 if (excess > 0)
1742 diff <<= excess;
1743 return diff == 0;
1745 return eq_p_large (xi.val, xi.len, yi.val, yi.len, precision);
1748 /* Return true if X != Y. X and Y must be binary-compatible. */
1749 template <typename T1, typename T2>
1750 inline bool
1751 wi::ne_p (const T1 &x, const T2 &y)
1753 return !eq_p (x, y);
1756 /* Return true if X < Y when both are treated as signed values. */
1757 template <typename T1, typename T2>
1758 inline bool
1759 wi::lts_p (const T1 &x, const T2 &y)
1761 unsigned int precision = get_binary_precision (x, y);
1762 WIDE_INT_REF_FOR (T1) xi (x, precision);
1763 WIDE_INT_REF_FOR (T2) yi (y, precision);
1764 /* We optimize x < y, where y is 64 or fewer bits. */
1765 if (wi::fits_shwi_p (yi))
1767 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
1768 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1769 return neg_p (xi);
1770 /* If x fits directly into a shwi, we can compare directly. */
1771 if (wi::fits_shwi_p (xi))
1772 return xi.to_shwi () < yi.to_shwi ();
1773 /* If x doesn't fit and is negative, then it must be more
1774 negative than any value in y, and hence smaller than y. */
1775 if (neg_p (xi))
1776 return true;
1777 /* If x is positive, then it must be larger than any value in y,
1778 and hence greater than y. */
1779 return false;
1781 /* Optimize the opposite case, if it can be detected at compile time. */
1782 if (STATIC_CONSTANT_P (xi.len == 1))
1783 /* If YI is negative it is lower than the least HWI.
1784 If YI is positive it is greater than the greatest HWI. */
1785 return !neg_p (yi);
1786 return lts_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1789 /* Return true if X < Y when both are treated as unsigned values. */
1790 template <typename T1, typename T2>
1791 inline bool
1792 wi::ltu_p (const T1 &x, const T2 &y)
1794 unsigned int precision = get_binary_precision (x, y);
1795 WIDE_INT_REF_FOR (T1) xi (x, precision);
1796 WIDE_INT_REF_FOR (T2) yi (y, precision);
1797 /* Optimize comparisons with constants. */
1798 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1799 return xi.len == 1 && xi.to_uhwi () < (unsigned HOST_WIDE_INT) yi.val[0];
1800 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1801 return yi.len != 1 || yi.to_uhwi () > (unsigned HOST_WIDE_INT) xi.val[0];
1802 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1803 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1804 values does not change the result. */
1805 if (__builtin_expect (xi.len + yi.len == 2, true))
1807 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1808 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1809 return xl < yl;
1811 return ltu_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1814 /* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
1815 template <typename T1, typename T2>
1816 inline bool
1817 wi::lt_p (const T1 &x, const T2 &y, signop sgn)
1819 if (sgn == SIGNED)
1820 return lts_p (x, y);
1821 else
1822 return ltu_p (x, y);
1825 /* Return true if X <= Y when both are treated as signed values. */
1826 template <typename T1, typename T2>
1827 inline bool
1828 wi::les_p (const T1 &x, const T2 &y)
1830 return !lts_p (y, x);
1833 /* Return true if X <= Y when both are treated as unsigned values. */
1834 template <typename T1, typename T2>
1835 inline bool
1836 wi::leu_p (const T1 &x, const T2 &y)
1838 return !ltu_p (y, x);
1841 /* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
1842 template <typename T1, typename T2>
1843 inline bool
1844 wi::le_p (const T1 &x, const T2 &y, signop sgn)
1846 if (sgn == SIGNED)
1847 return les_p (x, y);
1848 else
1849 return leu_p (x, y);
1852 /* Return true if X > Y when both are treated as signed values. */
1853 template <typename T1, typename T2>
1854 inline bool
1855 wi::gts_p (const T1 &x, const T2 &y)
1857 return lts_p (y, x);
1860 /* Return true if X > Y when both are treated as unsigned values. */
1861 template <typename T1, typename T2>
1862 inline bool
1863 wi::gtu_p (const T1 &x, const T2 &y)
1865 return ltu_p (y, x);
1868 /* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
1869 template <typename T1, typename T2>
1870 inline bool
1871 wi::gt_p (const T1 &x, const T2 &y, signop sgn)
1873 if (sgn == SIGNED)
1874 return gts_p (x, y);
1875 else
1876 return gtu_p (x, y);
1879 /* Return true if X >= Y when both are treated as signed values. */
1880 template <typename T1, typename T2>
1881 inline bool
1882 wi::ges_p (const T1 &x, const T2 &y)
1884 return !lts_p (x, y);
1887 /* Return true if X >= Y when both are treated as unsigned values. */
1888 template <typename T1, typename T2>
1889 inline bool
1890 wi::geu_p (const T1 &x, const T2 &y)
1892 return !ltu_p (x, y);
1895 /* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
1896 template <typename T1, typename T2>
1897 inline bool
1898 wi::ge_p (const T1 &x, const T2 &y, signop sgn)
1900 if (sgn == SIGNED)
1901 return ges_p (x, y);
1902 else
1903 return geu_p (x, y);
1906 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1907 as signed values. */
1908 template <typename T1, typename T2>
1909 inline int
1910 wi::cmps (const T1 &x, const T2 &y)
1912 unsigned int precision = get_binary_precision (x, y);
1913 WIDE_INT_REF_FOR (T1) xi (x, precision);
1914 WIDE_INT_REF_FOR (T2) yi (y, precision);
1915 if (wi::fits_shwi_p (yi))
1917 /* Special case for comparisons with 0. */
1918 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1919 return neg_p (xi) ? -1 : !(xi.len == 1 && xi.val[0] == 0);
1920 /* If x fits into a signed HWI, we can compare directly. */
1921 if (wi::fits_shwi_p (xi))
1923 HOST_WIDE_INT xl = xi.to_shwi ();
1924 HOST_WIDE_INT yl = yi.to_shwi ();
1925 return xl < yl ? -1 : xl > yl;
1927 /* If x doesn't fit and is negative, then it must be more
1928 negative than any signed HWI, and hence smaller than y. */
1929 if (neg_p (xi))
1930 return -1;
1931 /* If x is positive, then it must be larger than any signed HWI,
1932 and hence greater than y. */
1933 return 1;
1935 /* Optimize the opposite case, if it can be detected at compile time. */
1936 if (STATIC_CONSTANT_P (xi.len == 1))
1937 /* If YI is negative it is lower than the least HWI.
1938 If YI is positive it is greater than the greatest HWI. */
1939 return neg_p (yi) ? 1 : -1;
1940 return cmps_large (xi.val, xi.len, precision, yi.val, yi.len);
1943 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1944 as unsigned values. */
1945 template <typename T1, typename T2>
1946 inline int
1947 wi::cmpu (const T1 &x, const T2 &y)
1949 unsigned int precision = get_binary_precision (x, y);
1950 WIDE_INT_REF_FOR (T1) xi (x, precision);
1951 WIDE_INT_REF_FOR (T2) yi (y, precision);
1952 /* Optimize comparisons with constants. */
1953 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1955 /* If XI doesn't fit in a HWI then it must be larger than YI. */
1956 if (xi.len != 1)
1957 return 1;
1958 /* Otherwise compare directly. */
1959 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1960 unsigned HOST_WIDE_INT yl = yi.val[0];
1961 return xl < yl ? -1 : xl > yl;
1963 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1965 /* If YI doesn't fit in a HWI then it must be larger than XI. */
1966 if (yi.len != 1)
1967 return -1;
1968 /* Otherwise compare directly. */
1969 unsigned HOST_WIDE_INT xl = xi.val[0];
1970 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1971 return xl < yl ? -1 : xl > yl;
1973 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1974 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1975 values does not change the result. */
1976 if (__builtin_expect (xi.len + yi.len == 2, true))
1978 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1979 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1980 return xl < yl ? -1 : xl > yl;
1982 return cmpu_large (xi.val, xi.len, precision, yi.val, yi.len);
1985 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
1986 X and Y indicated by SGN. */
1987 template <typename T1, typename T2>
1988 inline int
1989 wi::cmp (const T1 &x, const T2 &y, signop sgn)
1991 if (sgn == SIGNED)
1992 return cmps (x, y);
1993 else
1994 return cmpu (x, y);
1997 /* Return ~x. */
1998 template <typename T>
1999 inline WI_UNARY_RESULT (T)
2000 wi::bit_not (const T &x)
2002 WI_UNARY_RESULT_VAR (result, val, T, x);
2003 WIDE_INT_REF_FOR (T) xi (x, get_precision (result));
2004 for (unsigned int i = 0; i < xi.len; ++i)
2005 val[i] = ~xi.val[i];
2006 result.set_len (xi.len);
2007 return result;
2010 /* Return -x. */
2011 template <typename T>
2012 inline WI_UNARY_RESULT (T)
2013 wi::neg (const T &x)
2015 return sub (0, x);
2018 /* Return -x. Indicate in *OVERFLOW if X is the minimum signed value. */
2019 template <typename T>
2020 inline WI_UNARY_RESULT (T)
2021 wi::neg (const T &x, bool *overflow)
2023 *overflow = only_sign_bit_p (x);
2024 return sub (0, x);
2027 /* Return the absolute value of x. */
2028 template <typename T>
2029 inline WI_UNARY_RESULT (T)
2030 wi::abs (const T &x)
2032 return neg_p (x) ? neg (x) : WI_UNARY_RESULT (T) (x);
2035 /* Return the result of sign-extending the low OFFSET bits of X. */
2036 template <typename T>
2037 inline WI_UNARY_RESULT (T)
2038 wi::sext (const T &x, unsigned int offset)
2040 WI_UNARY_RESULT_VAR (result, val, T, x);
2041 unsigned int precision = get_precision (result);
2042 WIDE_INT_REF_FOR (T) xi (x, precision);
2044 if (offset <= HOST_BITS_PER_WIDE_INT)
2046 val[0] = sext_hwi (xi.ulow (), offset);
2047 result.set_len (1, true);
2049 else
2050 result.set_len (sext_large (val, xi.val, xi.len, precision, offset));
2051 return result;
2054 /* Return the result of zero-extending the low OFFSET bits of X. */
2055 template <typename T>
2056 inline WI_UNARY_RESULT (T)
2057 wi::zext (const T &x, unsigned int offset)
2059 WI_UNARY_RESULT_VAR (result, val, T, x);
2060 unsigned int precision = get_precision (result);
2061 WIDE_INT_REF_FOR (T) xi (x, precision);
2063 /* This is not just an optimization, it is actually required to
2064 maintain canonization. */
2065 if (offset >= precision)
2067 wi::copy (result, xi);
2068 return result;
2071 /* In these cases we know that at least the top bit will be clear,
2072 so no sign extension is necessary. */
2073 if (offset < HOST_BITS_PER_WIDE_INT)
2075 val[0] = zext_hwi (xi.ulow (), offset);
2076 result.set_len (1, true);
2078 else
2079 result.set_len (zext_large (val, xi.val, xi.len, precision, offset), true);
2080 return result;
2083 /* Return the result of extending the low OFFSET bits of X according to
2084 signedness SGN. */
2085 template <typename T>
2086 inline WI_UNARY_RESULT (T)
2087 wi::ext (const T &x, unsigned int offset, signop sgn)
2089 return sgn == SIGNED ? sext (x, offset) : zext (x, offset);
2092 /* Return an integer that represents X | (1 << bit). */
2093 template <typename T>
2094 inline WI_UNARY_RESULT (T)
2095 wi::set_bit (const T &x, unsigned int bit)
2097 WI_UNARY_RESULT_VAR (result, val, T, x);
2098 unsigned int precision = get_precision (result);
2099 WIDE_INT_REF_FOR (T) xi (x, precision);
2100 if (precision <= HOST_BITS_PER_WIDE_INT)
2102 val[0] = xi.ulow () | ((unsigned HOST_WIDE_INT) 1 << bit);
2103 result.set_len (1);
2105 else
2106 result.set_len (set_bit_large (val, xi.val, xi.len, precision, bit));
2107 return result;
2110 /* Return the mininum of X and Y, treating them both as having
2111 signedness SGN. */
2112 template <typename T1, typename T2>
2113 inline WI_BINARY_RESULT (T1, T2)
2114 wi::min (const T1 &x, const T2 &y, signop sgn)
2116 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2117 unsigned int precision = get_precision (result);
2118 if (wi::le_p (x, y, sgn))
2119 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2120 else
2121 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2122 return result;
2125 /* Return the minimum of X and Y, treating both as signed values. */
2126 template <typename T1, typename T2>
2127 inline WI_BINARY_RESULT (T1, T2)
2128 wi::smin (const T1 &x, const T2 &y)
2130 return wi::min (x, y, SIGNED);
2133 /* Return the minimum of X and Y, treating both as unsigned values. */
2134 template <typename T1, typename T2>
2135 inline WI_BINARY_RESULT (T1, T2)
2136 wi::umin (const T1 &x, const T2 &y)
2138 return wi::min (x, y, UNSIGNED);
2141 /* Return the maxinum of X and Y, treating them both as having
2142 signedness SGN. */
2143 template <typename T1, typename T2>
2144 inline WI_BINARY_RESULT (T1, T2)
2145 wi::max (const T1 &x, const T2 &y, signop sgn)
2147 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2148 unsigned int precision = get_precision (result);
2149 if (wi::ge_p (x, y, sgn))
2150 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2151 else
2152 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2153 return result;
2156 /* Return the maximum of X and Y, treating both as signed values. */
2157 template <typename T1, typename T2>
2158 inline WI_BINARY_RESULT (T1, T2)
2159 wi::smax (const T1 &x, const T2 &y)
2161 return wi::max (x, y, SIGNED);
2164 /* Return the maximum of X and Y, treating both as unsigned values. */
2165 template <typename T1, typename T2>
2166 inline WI_BINARY_RESULT (T1, T2)
2167 wi::umax (const T1 &x, const T2 &y)
2169 return wi::max (x, y, UNSIGNED);
2172 /* Return X & Y. */
2173 template <typename T1, typename T2>
2174 inline WI_BINARY_RESULT (T1, T2)
2175 wi::bit_and (const T1 &x, const T2 &y)
2177 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2178 unsigned int precision = get_precision (result);
2179 WIDE_INT_REF_FOR (T1) xi (x, precision);
2180 WIDE_INT_REF_FOR (T2) yi (y, precision);
2181 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2182 if (__builtin_expect (xi.len + yi.len == 2, true))
2184 val[0] = xi.ulow () & yi.ulow ();
2185 result.set_len (1, is_sign_extended);
2187 else
2188 result.set_len (and_large (val, xi.val, xi.len, yi.val, yi.len,
2189 precision), is_sign_extended);
2190 return result;
2193 /* Return X & ~Y. */
2194 template <typename T1, typename T2>
2195 inline WI_BINARY_RESULT (T1, T2)
2196 wi::bit_and_not (const T1 &x, const T2 &y)
2198 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2199 unsigned int precision = get_precision (result);
2200 WIDE_INT_REF_FOR (T1) xi (x, precision);
2201 WIDE_INT_REF_FOR (T2) yi (y, precision);
2202 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2203 if (__builtin_expect (xi.len + yi.len == 2, true))
2205 val[0] = xi.ulow () & ~yi.ulow ();
2206 result.set_len (1, is_sign_extended);
2208 else
2209 result.set_len (and_not_large (val, xi.val, xi.len, yi.val, yi.len,
2210 precision), is_sign_extended);
2211 return result;
2214 /* Return X | Y. */
2215 template <typename T1, typename T2>
2216 inline WI_BINARY_RESULT (T1, T2)
2217 wi::bit_or (const T1 &x, const T2 &y)
2219 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2220 unsigned int precision = get_precision (result);
2221 WIDE_INT_REF_FOR (T1) xi (x, precision);
2222 WIDE_INT_REF_FOR (T2) yi (y, precision);
2223 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2224 if (__builtin_expect (xi.len + yi.len == 2, true))
2226 val[0] = xi.ulow () | yi.ulow ();
2227 result.set_len (1, is_sign_extended);
2229 else
2230 result.set_len (or_large (val, xi.val, xi.len,
2231 yi.val, yi.len, precision), is_sign_extended);
2232 return result;
2235 /* Return X | ~Y. */
2236 template <typename T1, typename T2>
2237 inline WI_BINARY_RESULT (T1, T2)
2238 wi::bit_or_not (const T1 &x, const T2 &y)
2240 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2241 unsigned int precision = get_precision (result);
2242 WIDE_INT_REF_FOR (T1) xi (x, precision);
2243 WIDE_INT_REF_FOR (T2) yi (y, precision);
2244 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2245 if (__builtin_expect (xi.len + yi.len == 2, true))
2247 val[0] = xi.ulow () | ~yi.ulow ();
2248 result.set_len (1, is_sign_extended);
2250 else
2251 result.set_len (or_not_large (val, xi.val, xi.len, yi.val, yi.len,
2252 precision), is_sign_extended);
2253 return result;
2256 /* Return X ^ Y. */
2257 template <typename T1, typename T2>
2258 inline WI_BINARY_RESULT (T1, T2)
2259 wi::bit_xor (const T1 &x, const T2 &y)
2261 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2262 unsigned int precision = get_precision (result);
2263 WIDE_INT_REF_FOR (T1) xi (x, precision);
2264 WIDE_INT_REF_FOR (T2) yi (y, precision);
2265 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2266 if (__builtin_expect (xi.len + yi.len == 2, true))
2268 val[0] = xi.ulow () ^ yi.ulow ();
2269 result.set_len (1, is_sign_extended);
2271 else
2272 result.set_len (xor_large (val, xi.val, xi.len,
2273 yi.val, yi.len, precision), is_sign_extended);
2274 return result;
2277 /* Return X + Y. */
2278 template <typename T1, typename T2>
2279 inline WI_BINARY_RESULT (T1, T2)
2280 wi::add (const T1 &x, const T2 &y)
2282 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2283 unsigned int precision = get_precision (result);
2284 WIDE_INT_REF_FOR (T1) xi (x, precision);
2285 WIDE_INT_REF_FOR (T2) yi (y, precision);
2286 if (precision <= HOST_BITS_PER_WIDE_INT)
2288 val[0] = xi.ulow () + yi.ulow ();
2289 result.set_len (1);
2291 /* If the precision is known at compile time to be greater than
2292 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2293 knowing that (a) all bits in those HWIs are significant and
2294 (b) the result has room for at least two HWIs. This provides
2295 a fast path for things like offset_int and widest_int.
2297 The STATIC_CONSTANT_P test prevents this path from being
2298 used for wide_ints. wide_ints with precisions greater than
2299 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2300 point handling them inline. */
2301 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2302 && __builtin_expect (xi.len + yi.len == 2, true))
2304 unsigned HOST_WIDE_INT xl = xi.ulow ();
2305 unsigned HOST_WIDE_INT yl = yi.ulow ();
2306 unsigned HOST_WIDE_INT resultl = xl + yl;
2307 val[0] = resultl;
2308 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2309 result.set_len (1 + (((resultl ^ xl) & (resultl ^ yl))
2310 >> (HOST_BITS_PER_WIDE_INT - 1)));
2312 else
2313 result.set_len (add_large (val, xi.val, xi.len,
2314 yi.val, yi.len, precision,
2315 UNSIGNED, 0));
2316 return result;
2319 /* Return X + Y. Treat X and Y as having the signednes given by SGN
2320 and indicate in *OVERFLOW whether the operation overflowed. */
2321 template <typename T1, typename T2>
2322 inline WI_BINARY_RESULT (T1, T2)
2323 wi::add (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2325 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2326 unsigned int precision = get_precision (result);
2327 WIDE_INT_REF_FOR (T1) xi (x, precision);
2328 WIDE_INT_REF_FOR (T2) yi (y, precision);
2329 if (precision <= HOST_BITS_PER_WIDE_INT)
2331 unsigned HOST_WIDE_INT xl = xi.ulow ();
2332 unsigned HOST_WIDE_INT yl = yi.ulow ();
2333 unsigned HOST_WIDE_INT resultl = xl + yl;
2334 if (sgn == SIGNED)
2335 *overflow = (((resultl ^ xl) & (resultl ^ yl))
2336 >> (precision - 1)) & 1;
2337 else
2338 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2339 < (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2340 val[0] = resultl;
2341 result.set_len (1);
2343 else
2344 result.set_len (add_large (val, xi.val, xi.len,
2345 yi.val, yi.len, precision,
2346 sgn, overflow));
2347 return result;
2350 /* Return X - Y. */
2351 template <typename T1, typename T2>
2352 inline WI_BINARY_RESULT (T1, T2)
2353 wi::sub (const T1 &x, const T2 &y)
2355 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2356 unsigned int precision = get_precision (result);
2357 WIDE_INT_REF_FOR (T1) xi (x, precision);
2358 WIDE_INT_REF_FOR (T2) yi (y, precision);
2359 if (precision <= HOST_BITS_PER_WIDE_INT)
2361 val[0] = xi.ulow () - yi.ulow ();
2362 result.set_len (1);
2364 /* If the precision is known at compile time to be greater than
2365 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2366 knowing that (a) all bits in those HWIs are significant and
2367 (b) the result has room for at least two HWIs. This provides
2368 a fast path for things like offset_int and widest_int.
2370 The STATIC_CONSTANT_P test prevents this path from being
2371 used for wide_ints. wide_ints with precisions greater than
2372 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2373 point handling them inline. */
2374 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2375 && __builtin_expect (xi.len + yi.len == 2, true))
2377 unsigned HOST_WIDE_INT xl = xi.ulow ();
2378 unsigned HOST_WIDE_INT yl = yi.ulow ();
2379 unsigned HOST_WIDE_INT resultl = xl - yl;
2380 val[0] = resultl;
2381 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2382 result.set_len (1 + (((resultl ^ xl) & (xl ^ yl))
2383 >> (HOST_BITS_PER_WIDE_INT - 1)));
2385 else
2386 result.set_len (sub_large (val, xi.val, xi.len,
2387 yi.val, yi.len, precision,
2388 UNSIGNED, 0));
2389 return result;
2392 /* Return X - Y. Treat X and Y as having the signednes given by SGN
2393 and indicate in *OVERFLOW whether the operation overflowed. */
2394 template <typename T1, typename T2>
2395 inline WI_BINARY_RESULT (T1, T2)
2396 wi::sub (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2398 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2399 unsigned int precision = get_precision (result);
2400 WIDE_INT_REF_FOR (T1) xi (x, precision);
2401 WIDE_INT_REF_FOR (T2) yi (y, precision);
2402 if (precision <= HOST_BITS_PER_WIDE_INT)
2404 unsigned HOST_WIDE_INT xl = xi.ulow ();
2405 unsigned HOST_WIDE_INT yl = yi.ulow ();
2406 unsigned HOST_WIDE_INT resultl = xl - yl;
2407 if (sgn == SIGNED)
2408 *overflow = (((xl ^ yl) & (resultl ^ xl)) >> (precision - 1)) & 1;
2409 else
2410 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2411 > (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2412 val[0] = resultl;
2413 result.set_len (1);
2415 else
2416 result.set_len (sub_large (val, xi.val, xi.len,
2417 yi.val, yi.len, precision,
2418 sgn, overflow));
2419 return result;
2422 /* Return X * Y. */
2423 template <typename T1, typename T2>
2424 inline WI_BINARY_RESULT (T1, T2)
2425 wi::mul (const T1 &x, const T2 &y)
2427 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2428 unsigned int precision = get_precision (result);
2429 WIDE_INT_REF_FOR (T1) xi (x, precision);
2430 WIDE_INT_REF_FOR (T2) yi (y, precision);
2431 if (precision <= HOST_BITS_PER_WIDE_INT)
2433 val[0] = xi.ulow () * yi.ulow ();
2434 result.set_len (1);
2436 else
2437 result.set_len (mul_internal (val, xi.val, xi.len, yi.val, yi.len,
2438 precision, UNSIGNED, 0, false));
2439 return result;
2442 /* Return X * Y. Treat X and Y as having the signednes given by SGN
2443 and indicate in *OVERFLOW whether the operation overflowed. */
2444 template <typename T1, typename T2>
2445 inline WI_BINARY_RESULT (T1, T2)
2446 wi::mul (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2448 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2449 unsigned int precision = get_precision (result);
2450 WIDE_INT_REF_FOR (T1) xi (x, precision);
2451 WIDE_INT_REF_FOR (T2) yi (y, precision);
2452 result.set_len (mul_internal (val, xi.val, xi.len,
2453 yi.val, yi.len, precision,
2454 sgn, overflow, false));
2455 return result;
2458 /* Return X * Y, treating both X and Y as signed values. Indicate in
2459 *OVERFLOW whether the operation overflowed. */
2460 template <typename T1, typename T2>
2461 inline WI_BINARY_RESULT (T1, T2)
2462 wi::smul (const T1 &x, const T2 &y, bool *overflow)
2464 return mul (x, y, SIGNED, overflow);
2467 /* Return X * Y, treating both X and Y as unsigned values. Indicate in
2468 *OVERFLOW whether the operation overflowed. */
2469 template <typename T1, typename T2>
2470 inline WI_BINARY_RESULT (T1, T2)
2471 wi::umul (const T1 &x, const T2 &y, bool *overflow)
2473 return mul (x, y, UNSIGNED, overflow);
2476 /* Perform a widening multiplication of X and Y, extending the values
2477 according to SGN, and return the high part of the result. */
2478 template <typename T1, typename T2>
2479 inline WI_BINARY_RESULT (T1, T2)
2480 wi::mul_high (const T1 &x, const T2 &y, signop sgn)
2482 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2483 unsigned int precision = get_precision (result);
2484 WIDE_INT_REF_FOR (T1) xi (x, precision);
2485 WIDE_INT_REF_FOR (T2) yi (y, precision);
2486 result.set_len (mul_internal (val, xi.val, xi.len,
2487 yi.val, yi.len, precision,
2488 sgn, 0, true));
2489 return result;
2492 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2493 signedness given by SGN. Indicate in *OVERFLOW if the result
2494 overflows. */
2495 template <typename T1, typename T2>
2496 inline WI_BINARY_RESULT (T1, T2)
2497 wi::div_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2499 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2500 unsigned int precision = get_precision (quotient);
2501 WIDE_INT_REF_FOR (T1) xi (x, precision);
2502 WIDE_INT_REF_FOR (T2) yi (y);
2504 quotient.set_len (divmod_internal (quotient_val, 0, 0, xi.val, xi.len,
2505 precision,
2506 yi.val, yi.len, yi.precision,
2507 sgn, overflow));
2508 return quotient;
2511 /* Return X / Y, rouding towards 0. Treat X and Y as signed values. */
2512 template <typename T1, typename T2>
2513 inline WI_BINARY_RESULT (T1, T2)
2514 wi::sdiv_trunc (const T1 &x, const T2 &y)
2516 return div_trunc (x, y, SIGNED);
2519 /* Return X / Y, rouding towards 0. Treat X and Y as unsigned values. */
2520 template <typename T1, typename T2>
2521 inline WI_BINARY_RESULT (T1, T2)
2522 wi::udiv_trunc (const T1 &x, const T2 &y)
2524 return div_trunc (x, y, UNSIGNED);
2527 /* Return X / Y, rouding towards -inf. Treat X and Y as having the
2528 signedness given by SGN. Indicate in *OVERFLOW if the result
2529 overflows. */
2530 template <typename T1, typename T2>
2531 inline WI_BINARY_RESULT (T1, T2)
2532 wi::div_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2534 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2535 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2536 unsigned int precision = get_precision (quotient);
2537 WIDE_INT_REF_FOR (T1) xi (x, precision);
2538 WIDE_INT_REF_FOR (T2) yi (y);
2540 unsigned int remainder_len;
2541 quotient.set_len (divmod_internal (quotient_val,
2542 &remainder_len, remainder_val,
2543 xi.val, xi.len, precision,
2544 yi.val, yi.len, yi.precision, sgn,
2545 overflow));
2546 remainder.set_len (remainder_len);
2547 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2548 return quotient - 1;
2549 return quotient;
2552 /* Return X / Y, rouding towards -inf. Treat X and Y as signed values. */
2553 template <typename T1, typename T2>
2554 inline WI_BINARY_RESULT (T1, T2)
2555 wi::sdiv_floor (const T1 &x, const T2 &y)
2557 return div_floor (x, y, SIGNED);
2560 /* Return X / Y, rouding towards -inf. Treat X and Y as unsigned values. */
2561 /* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
2562 template <typename T1, typename T2>
2563 inline WI_BINARY_RESULT (T1, T2)
2564 wi::udiv_floor (const T1 &x, const T2 &y)
2566 return div_floor (x, y, UNSIGNED);
2569 /* Return X / Y, rouding towards +inf. Treat X and Y as having the
2570 signedness given by SGN. Indicate in *OVERFLOW if the result
2571 overflows. */
2572 template <typename T1, typename T2>
2573 inline WI_BINARY_RESULT (T1, T2)
2574 wi::div_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2576 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2577 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2578 unsigned int precision = get_precision (quotient);
2579 WIDE_INT_REF_FOR (T1) xi (x, precision);
2580 WIDE_INT_REF_FOR (T2) yi (y);
2582 unsigned int remainder_len;
2583 quotient.set_len (divmod_internal (quotient_val,
2584 &remainder_len, remainder_val,
2585 xi.val, xi.len, precision,
2586 yi.val, yi.len, yi.precision, sgn,
2587 overflow));
2588 remainder.set_len (remainder_len);
2589 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2590 return quotient + 1;
2591 return quotient;
2594 /* Return X / Y, rouding towards nearest with ties away from zero.
2595 Treat X and Y as having the signedness given by SGN. Indicate
2596 in *OVERFLOW if the result overflows. */
2597 template <typename T1, typename T2>
2598 inline WI_BINARY_RESULT (T1, T2)
2599 wi::div_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2601 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2602 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2603 unsigned int precision = get_precision (quotient);
2604 WIDE_INT_REF_FOR (T1) xi (x, precision);
2605 WIDE_INT_REF_FOR (T2) yi (y);
2607 unsigned int remainder_len;
2608 quotient.set_len (divmod_internal (quotient_val,
2609 &remainder_len, remainder_val,
2610 xi.val, xi.len, precision,
2611 yi.val, yi.len, yi.precision, sgn,
2612 overflow));
2613 remainder.set_len (remainder_len);
2615 if (remainder != 0)
2617 if (sgn == SIGNED)
2619 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2620 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2622 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2623 return quotient - 1;
2624 else
2625 return quotient + 1;
2628 else
2630 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2631 return quotient + 1;
2634 return quotient;
2637 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2638 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
2639 template <typename T1, typename T2>
2640 inline WI_BINARY_RESULT (T1, T2)
2641 wi::divmod_trunc (const T1 &x, const T2 &y, signop sgn,
2642 WI_BINARY_RESULT (T1, T2) *remainder_ptr)
2644 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2645 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2646 unsigned int precision = get_precision (quotient);
2647 WIDE_INT_REF_FOR (T1) xi (x, precision);
2648 WIDE_INT_REF_FOR (T2) yi (y);
2650 unsigned int remainder_len;
2651 quotient.set_len (divmod_internal (quotient_val,
2652 &remainder_len, remainder_val,
2653 xi.val, xi.len, precision,
2654 yi.val, yi.len, yi.precision, sgn, 0));
2655 remainder.set_len (remainder_len);
2657 *remainder_ptr = remainder;
2658 return quotient;
2661 /* Compute X / Y, rouding towards 0, and return the remainder.
2662 Treat X and Y as having the signedness given by SGN. Indicate
2663 in *OVERFLOW if the division overflows. */
2664 template <typename T1, typename T2>
2665 inline WI_BINARY_RESULT (T1, T2)
2666 wi::mod_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2668 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2669 unsigned int precision = get_precision (remainder);
2670 WIDE_INT_REF_FOR (T1) xi (x, precision);
2671 WIDE_INT_REF_FOR (T2) yi (y);
2673 unsigned int remainder_len;
2674 divmod_internal (0, &remainder_len, remainder_val,
2675 xi.val, xi.len, precision,
2676 yi.val, yi.len, yi.precision, sgn, overflow);
2677 remainder.set_len (remainder_len);
2679 return remainder;
2682 /* Compute X / Y, rouding towards 0, and return the remainder.
2683 Treat X and Y as signed values. */
2684 template <typename T1, typename T2>
2685 inline WI_BINARY_RESULT (T1, T2)
2686 wi::smod_trunc (const T1 &x, const T2 &y)
2688 return mod_trunc (x, y, SIGNED);
2691 /* Compute X / Y, rouding towards 0, and return the remainder.
2692 Treat X and Y as unsigned values. */
2693 template <typename T1, typename T2>
2694 inline WI_BINARY_RESULT (T1, T2)
2695 wi::umod_trunc (const T1 &x, const T2 &y)
2697 return mod_trunc (x, y, UNSIGNED);
2700 /* Compute X / Y, rouding towards -inf, and return the remainder.
2701 Treat X and Y as having the signedness given by SGN. Indicate
2702 in *OVERFLOW if the division overflows. */
2703 template <typename T1, typename T2>
2704 inline WI_BINARY_RESULT (T1, T2)
2705 wi::mod_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2707 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2708 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2709 unsigned int precision = get_precision (quotient);
2710 WIDE_INT_REF_FOR (T1) xi (x, precision);
2711 WIDE_INT_REF_FOR (T2) yi (y);
2713 unsigned int remainder_len;
2714 quotient.set_len (divmod_internal (quotient_val,
2715 &remainder_len, remainder_val,
2716 xi.val, xi.len, precision,
2717 yi.val, yi.len, yi.precision, sgn,
2718 overflow));
2719 remainder.set_len (remainder_len);
2721 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2722 return remainder + y;
2723 return remainder;
2726 /* Compute X / Y, rouding towards -inf, and return the remainder.
2727 Treat X and Y as unsigned values. */
2728 /* ??? Why do we have both this and umod_trunc. Aren't they the same? */
2729 template <typename T1, typename T2>
2730 inline WI_BINARY_RESULT (T1, T2)
2731 wi::umod_floor (const T1 &x, const T2 &y)
2733 return mod_floor (x, y, UNSIGNED);
2736 /* Compute X / Y, rouding towards +inf, and return the remainder.
2737 Treat X and Y as having the signedness given by SGN. Indicate
2738 in *OVERFLOW if the division overflows. */
2739 template <typename T1, typename T2>
2740 inline WI_BINARY_RESULT (T1, T2)
2741 wi::mod_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2743 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2744 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2745 unsigned int precision = get_precision (quotient);
2746 WIDE_INT_REF_FOR (T1) xi (x, precision);
2747 WIDE_INT_REF_FOR (T2) yi (y);
2749 unsigned int remainder_len;
2750 quotient.set_len (divmod_internal (quotient_val,
2751 &remainder_len, remainder_val,
2752 xi.val, xi.len, precision,
2753 yi.val, yi.len, yi.precision, sgn,
2754 overflow));
2755 remainder.set_len (remainder_len);
2757 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2758 return remainder - y;
2759 return remainder;
2762 /* Compute X / Y, rouding towards nearest with ties away from zero,
2763 and return the remainder. Treat X and Y as having the signedness
2764 given by SGN. Indicate in *OVERFLOW if the division overflows. */
2765 template <typename T1, typename T2>
2766 inline WI_BINARY_RESULT (T1, T2)
2767 wi::mod_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2769 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2770 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2771 unsigned int precision = get_precision (quotient);
2772 WIDE_INT_REF_FOR (T1) xi (x, precision);
2773 WIDE_INT_REF_FOR (T2) yi (y);
2775 unsigned int remainder_len;
2776 quotient.set_len (divmod_internal (quotient_val,
2777 &remainder_len, remainder_val,
2778 xi.val, xi.len, precision,
2779 yi.val, yi.len, yi.precision, sgn,
2780 overflow));
2781 remainder.set_len (remainder_len);
2783 if (remainder != 0)
2785 if (sgn == SIGNED)
2787 WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
2788 if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
2790 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2791 return remainder + y;
2792 else
2793 return remainder - y;
2796 else
2798 if (wi::geu_p (remainder, wi::sub (y, remainder)))
2799 return remainder - y;
2802 return remainder;
2805 /* Return true if X is a multiple of Y. Treat X and Y as having the
2806 signedness given by SGN. */
2807 template <typename T1, typename T2>
2808 inline bool
2809 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn)
2811 return wi::mod_trunc (x, y, sgn) == 0;
2814 /* Return true if X is a multiple of Y, storing X / Y in *RES if so.
2815 Treat X and Y as having the signedness given by SGN. */
2816 template <typename T1, typename T2>
2817 inline bool
2818 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn,
2819 WI_BINARY_RESULT (T1, T2) *res)
2821 WI_BINARY_RESULT (T1, T2) remainder;
2822 WI_BINARY_RESULT (T1, T2) quotient
2823 = divmod_trunc (x, y, sgn, &remainder);
2824 if (remainder == 0)
2826 *res = quotient;
2827 return true;
2829 return false;
2832 /* Return X << Y. Return 0 if Y is greater than or equal to
2833 the precision of X. */
2834 template <typename T1, typename T2>
2835 inline WI_UNARY_RESULT (T1)
2836 wi::lshift (const T1 &x, const T2 &y)
2838 WI_UNARY_RESULT_VAR (result, val, T1, x);
2839 unsigned int precision = get_precision (result);
2840 WIDE_INT_REF_FOR (T1) xi (x, precision);
2841 WIDE_INT_REF_FOR (T2) yi (y);
2842 /* Handle the simple cases quickly. */
2843 if (geu_p (yi, precision))
2845 val[0] = 0;
2846 result.set_len (1);
2848 else
2850 unsigned int shift = yi.to_uhwi ();
2851 /* For fixed-precision integers like offset_int and widest_int,
2852 handle the case where the shift value is constant and the
2853 result is a single nonnegative HWI (meaning that we don't
2854 need to worry about val[1]). This is particularly common
2855 for converting a byte count to a bit count.
2857 For variable-precision integers like wide_int, handle HWI
2858 and sub-HWI integers inline. */
2859 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2860 ? (STATIC_CONSTANT_P (shift < HOST_BITS_PER_WIDE_INT - 1)
2861 && xi.len == 1
2862 && xi.val[0] <= (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT)
2863 HOST_WIDE_INT_MAX >> shift))
2864 : precision <= HOST_BITS_PER_WIDE_INT)
2866 val[0] = xi.ulow () << shift;
2867 result.set_len (1);
2869 else
2870 result.set_len (lshift_large (val, xi.val, xi.len,
2871 precision, shift));
2873 return result;
2876 /* Return X >> Y, using a logical shift. Return 0 if Y is greater than
2877 or equal to the precision of X. */
2878 template <typename T1, typename T2>
2879 inline WI_UNARY_RESULT (T1)
2880 wi::lrshift (const T1 &x, const T2 &y)
2882 WI_UNARY_RESULT_VAR (result, val, T1, x);
2883 /* Do things in the precision of the input rather than the output,
2884 since the result can be no larger than that. */
2885 WIDE_INT_REF_FOR (T1) xi (x);
2886 WIDE_INT_REF_FOR (T2) yi (y);
2887 /* Handle the simple cases quickly. */
2888 if (geu_p (yi, xi.precision))
2890 val[0] = 0;
2891 result.set_len (1);
2893 else
2895 unsigned int shift = yi.to_uhwi ();
2896 /* For fixed-precision integers like offset_int and widest_int,
2897 handle the case where the shift value is constant and the
2898 shifted value is a single nonnegative HWI (meaning that all
2899 bits above the HWI are zero). This is particularly common
2900 for converting a bit count to a byte count.
2902 For variable-precision integers like wide_int, handle HWI
2903 and sub-HWI integers inline. */
2904 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2905 ? xi.len == 1 && xi.val[0] >= 0
2906 : xi.precision <= HOST_BITS_PER_WIDE_INT)
2908 val[0] = xi.to_uhwi () >> shift;
2909 result.set_len (1);
2911 else
2912 result.set_len (lrshift_large (val, xi.val, xi.len, xi.precision,
2913 get_precision (result), shift));
2915 return result;
2918 /* Return X >> Y, using an arithmetic shift. Return a sign mask if
2919 Y is greater than or equal to the precision of X. */
2920 template <typename T1, typename T2>
2921 inline WI_UNARY_RESULT (T1)
2922 wi::arshift (const T1 &x, const T2 &y)
2924 WI_UNARY_RESULT_VAR (result, val, T1, x);
2925 /* Do things in the precision of the input rather than the output,
2926 since the result can be no larger than that. */
2927 WIDE_INT_REF_FOR (T1) xi (x);
2928 WIDE_INT_REF_FOR (T2) yi (y);
2929 /* Handle the simple cases quickly. */
2930 if (geu_p (yi, xi.precision))
2932 val[0] = sign_mask (x);
2933 result.set_len (1);
2935 else
2937 unsigned int shift = yi.to_uhwi ();
2938 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
2940 val[0] = sext_hwi (xi.ulow () >> shift, xi.precision - shift);
2941 result.set_len (1, true);
2943 else
2944 result.set_len (arshift_large (val, xi.val, xi.len, xi.precision,
2945 get_precision (result), shift));
2947 return result;
2950 /* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
2951 logical shift otherwise. */
2952 template <typename T1, typename T2>
2953 inline WI_UNARY_RESULT (T1)
2954 wi::rshift (const T1 &x, const T2 &y, signop sgn)
2956 if (sgn == UNSIGNED)
2957 return lrshift (x, y);
2958 else
2959 return arshift (x, y);
2962 /* Return the result of rotating the low WIDTH bits of X left by Y
2963 bits and zero-extending the result. Use a full-width rotate if
2964 WIDTH is zero. */
2965 template <typename T1, typename T2>
2966 WI_UNARY_RESULT (T1)
2967 wi::lrotate (const T1 &x, const T2 &y, unsigned int width)
2969 unsigned int precision = get_binary_precision (x, x);
2970 if (width == 0)
2971 width = precision;
2972 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
2973 WI_UNARY_RESULT (T1) left = wi::lshift (x, ymod);
2974 WI_UNARY_RESULT (T1) right = wi::lrshift (x, wi::sub (width, ymod));
2975 if (width != precision)
2976 return wi::zext (left, width) | wi::zext (right, width);
2977 return left | right;
2980 /* Return the result of rotating the low WIDTH bits of X right by Y
2981 bits and zero-extending the result. Use a full-width rotate if
2982 WIDTH is zero. */
2983 template <typename T1, typename T2>
2984 WI_UNARY_RESULT (T1)
2985 wi::rrotate (const T1 &x, const T2 &y, unsigned int width)
2987 unsigned int precision = get_binary_precision (x, x);
2988 if (width == 0)
2989 width = precision;
2990 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
2991 WI_UNARY_RESULT (T1) right = wi::lrshift (x, ymod);
2992 WI_UNARY_RESULT (T1) left = wi::lshift (x, wi::sub (width, ymod));
2993 if (width != precision)
2994 return wi::zext (left, width) | wi::zext (right, width);
2995 return left | right;
2998 /* Return 0 if the number of 1s in X is even and 1 if the number of 1s
2999 is odd. */
3000 inline int
3001 wi::parity (const wide_int_ref &x)
3003 return popcount (x) & 1;
3006 /* Extract WIDTH bits from X, starting at BITPOS. */
3007 template <typename T>
3008 inline unsigned HOST_WIDE_INT
3009 wi::extract_uhwi (const T &x, unsigned int bitpos, unsigned int width)
3011 unsigned precision = get_precision (x);
3012 if (precision < bitpos + width)
3013 precision = bitpos + width;
3014 WIDE_INT_REF_FOR (T) xi (x, precision);
3016 /* Handle this rare case after the above, so that we assert about
3017 bogus BITPOS values. */
3018 if (width == 0)
3019 return 0;
3021 unsigned int start = bitpos / HOST_BITS_PER_WIDE_INT;
3022 unsigned int shift = bitpos % HOST_BITS_PER_WIDE_INT;
3023 unsigned HOST_WIDE_INT res = xi.elt (start);
3024 res >>= shift;
3025 if (shift + width > HOST_BITS_PER_WIDE_INT)
3027 unsigned HOST_WIDE_INT upper = xi.elt (start + 1);
3028 res |= upper << (-shift % HOST_BITS_PER_WIDE_INT);
3030 return zext_hwi (res, width);
3033 /* Return the minimum precision needed to store X with sign SGN. */
3034 template <typename T>
3035 inline unsigned int
3036 wi::min_precision (const T &x, signop sgn)
3038 if (sgn == SIGNED)
3039 return get_precision (x) - clrsb (x);
3040 else
3041 return get_precision (x) - clz (x);
3044 template<typename T>
3045 void
3046 gt_ggc_mx (generic_wide_int <T> *)
3050 template<typename T>
3051 void
3052 gt_pch_nx (generic_wide_int <T> *)
3056 template<typename T>
3057 void
3058 gt_pch_nx (generic_wide_int <T> *, void (*) (void *, void *), void *)
3062 template<int N>
3063 void
3064 gt_ggc_mx (trailing_wide_ints <N> *)
3068 template<int N>
3069 void
3070 gt_pch_nx (trailing_wide_ints <N> *)
3074 template<int N>
3075 void
3076 gt_pch_nx (trailing_wide_ints <N> *, void (*) (void *, void *), void *)
3080 namespace wi
3082 /* Used for overloaded functions in which the only other acceptable
3083 scalar type is a pointer. It stops a plain 0 from being treated
3084 as a null pointer. */
3085 struct never_used1 {};
3086 struct never_used2 {};
3088 wide_int min_value (unsigned int, signop);
3089 wide_int min_value (never_used1 *);
3090 wide_int min_value (never_used2 *);
3091 wide_int max_value (unsigned int, signop);
3092 wide_int max_value (never_used1 *);
3093 wide_int max_value (never_used2 *);
3095 /* FIXME: this is target dependent, so should be elsewhere.
3096 It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
3097 wide_int from_buffer (const unsigned char *, unsigned int);
3099 #ifndef GENERATOR_FILE
3100 void to_mpz (const wide_int_ref &, mpz_t, signop);
3101 #endif
3103 wide_int mask (unsigned int, bool, unsigned int);
3104 wide_int shifted_mask (unsigned int, unsigned int, bool, unsigned int);
3105 wide_int set_bit_in_zero (unsigned int, unsigned int);
3106 wide_int insert (const wide_int &x, const wide_int &y, unsigned int,
3107 unsigned int);
3109 template <typename T>
3110 T mask (unsigned int, bool);
3112 template <typename T>
3113 T shifted_mask (unsigned int, unsigned int, bool);
3115 template <typename T>
3116 T set_bit_in_zero (unsigned int);
3118 unsigned int mask (HOST_WIDE_INT *, unsigned int, bool, unsigned int);
3119 unsigned int shifted_mask (HOST_WIDE_INT *, unsigned int, unsigned int,
3120 bool, unsigned int);
3121 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
3122 unsigned int, unsigned int, bool);
3125 /* Return a PRECISION-bit integer in which the low WIDTH bits are set
3126 and the other bits are clear, or the inverse if NEGATE_P. */
3127 inline wide_int
3128 wi::mask (unsigned int width, bool negate_p, unsigned int precision)
3130 wide_int result = wide_int::create (precision);
3131 result.set_len (mask (result.write_val (), width, negate_p, precision));
3132 return result;
3135 /* Return a PRECISION-bit integer in which the low START bits are clear,
3136 the next WIDTH bits are set, and the other bits are clear,
3137 or the inverse if NEGATE_P. */
3138 inline wide_int
3139 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p,
3140 unsigned int precision)
3142 wide_int result = wide_int::create (precision);
3143 result.set_len (shifted_mask (result.write_val (), start, width, negate_p,
3144 precision));
3145 return result;
3148 /* Return a PRECISION-bit integer in which bit BIT is set and all the
3149 others are clear. */
3150 inline wide_int
3151 wi::set_bit_in_zero (unsigned int bit, unsigned int precision)
3153 return shifted_mask (bit, 1, false, precision);
3156 /* Return an integer of type T in which the low WIDTH bits are set
3157 and the other bits are clear, or the inverse if NEGATE_P. */
3158 template <typename T>
3159 inline T
3160 wi::mask (unsigned int width, bool negate_p)
3162 STATIC_ASSERT (wi::int_traits<T>::precision);
3163 T result;
3164 result.set_len (mask (result.write_val (), width, negate_p,
3165 wi::int_traits <T>::precision));
3166 return result;
3169 /* Return an integer of type T in which the low START bits are clear,
3170 the next WIDTH bits are set, and the other bits are clear, or the
3171 inverse if NEGATE_P. */
3172 template <typename T>
3173 inline T
3174 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p)
3176 STATIC_ASSERT (wi::int_traits<T>::precision);
3177 T result;
3178 result.set_len (shifted_mask (result.write_val (), start, width,
3179 negate_p,
3180 wi::int_traits <T>::precision));
3181 return result;
3184 /* Return an integer of type T in which bit BIT is set and all the
3185 others are clear. */
3186 template <typename T>
3187 inline T
3188 wi::set_bit_in_zero (unsigned int bit)
3190 return shifted_mask <T> (bit, 1, false);
3193 #endif /* WIDE_INT_H */