* config/msp430/msp430.md (split): Don't allow subregs when
[official-gcc.git] / gcc / wide-int.h
blobd5ab4281869f30196f9583433bf3e3b3dbaf7db4
1 /* Operations with very long integers. -*- C++ -*-
2 Copyright (C) 2012-2013 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. */
220 #include <utility>
221 #include "system.h"
222 #include "hwint.h"
223 #include "signop.h"
224 #include "insn-modes.h"
226 /* The MAX_BITSIZE_MODE_ANY_INT is automatically generated by a very
227 early examination of the target's mode file. The WIDE_INT_MAX_ELTS
228 can accomodate at least 1 more bit so that unsigned numbers of that
229 mode can be represented as a signed value. Note that it is still
230 possible to create fixed_wide_ints that have precisions greater than
231 MAX_BITSIZE_MODE_ANY_INT. This can be useful when representing a
232 double-width multiplication result, for example. */
233 #define WIDE_INT_MAX_ELTS \
234 ((MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT) / HOST_BITS_PER_WIDE_INT)
236 #define WIDE_INT_MAX_PRECISION (WIDE_INT_MAX_ELTS * HOST_BITS_PER_WIDE_INT)
238 /* This is the max size of any pointer on any machine. It does not
239 seem to be as easy to sniff this out of the machine description as
240 it is for MAX_BITSIZE_MODE_ANY_INT since targets may support
241 multiple address sizes and may have different address sizes for
242 different address spaces. However, currently the largest pointer
243 on any platform is 64 bits. When that changes, then it is likely
244 that a target hook should be defined so that targets can make this
245 value larger for those targets. */
246 #define ADDR_MAX_BITSIZE 64
248 /* This is the internal precision used when doing any address
249 arithmetic. The '4' is really 3 + 1. Three of the bits are for
250 the number of extra bits needed to do bit addresses and the other bit
251 is to allow everything to be signed without loosing any precision.
252 Then everything is rounded up to the next HWI for efficiency. */
253 #define ADDR_MAX_PRECISION \
254 ((ADDR_MAX_BITSIZE + 4 + HOST_BITS_PER_WIDE_INT - 1) \
255 & ~(HOST_BITS_PER_WIDE_INT - 1))
257 /* The number of HWIs needed to store an offset_int. */
258 #define OFFSET_INT_ELTS (ADDR_MAX_PRECISION / HOST_BITS_PER_WIDE_INT)
260 /* The type of result produced by a binary operation on types T1 and T2.
261 Defined purely for brevity. */
262 #define WI_BINARY_RESULT(T1, T2) \
263 typename wi::binary_traits <T1, T2>::result_type
265 /* The type of result produced by a unary operation on type T. */
266 #define WI_UNARY_RESULT(T) \
267 typename wi::unary_traits <T>::result_type
269 /* Define a variable RESULT to hold the result of a binary operation on
270 X and Y, which have types T1 and T2 respectively. Define VAL to
271 point to the blocks of RESULT. Once the user of the macro has
272 filled in VAL, it should call RESULT.set_len to set the number
273 of initialized blocks. */
274 #define WI_BINARY_RESULT_VAR(RESULT, VAL, T1, X, T2, Y) \
275 WI_BINARY_RESULT (T1, T2) RESULT = \
276 wi::int_traits <WI_BINARY_RESULT (T1, T2)>::get_binary_result (X, Y); \
277 HOST_WIDE_INT *VAL = RESULT.write_val ()
279 /* Similar for the result of a unary operation on X, which has type T. */
280 #define WI_UNARY_RESULT_VAR(RESULT, VAL, T, X) \
281 WI_UNARY_RESULT (T) RESULT = \
282 wi::int_traits <WI_UNARY_RESULT (T)>::get_binary_result (X, X); \
283 HOST_WIDE_INT *VAL = RESULT.write_val ()
285 template <typename T> struct generic_wide_int;
286 template <int N> struct fixed_wide_int_storage;
287 struct wide_int_storage;
289 /* An N-bit integer. Until we can use typedef templates, use this instead. */
290 #define FIXED_WIDE_INT(N) \
291 generic_wide_int < fixed_wide_int_storage <N> >
293 typedef generic_wide_int <wide_int_storage> wide_int;
294 typedef FIXED_WIDE_INT (ADDR_MAX_PRECISION) offset_int;
295 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION) widest_int;
297 template <bool SE>
298 struct wide_int_ref_storage;
300 typedef generic_wide_int <wide_int_ref_storage <false> > wide_int_ref;
302 /* This can be used instead of wide_int_ref if the referenced value is
303 known to have type T. It carries across properties of T's representation,
304 such as whether excess upper bits in a HWI are defined, and can therefore
305 help avoid redundant work.
307 The macro could be replaced with a template typedef, once we're able
308 to use those. */
309 #define WIDE_INT_REF_FOR(T) \
310 generic_wide_int \
311 <wide_int_ref_storage <wi::int_traits <T>::is_sign_extended> >
313 namespace wi
315 /* Classifies an integer based on its precision. */
316 enum precision_type {
317 /* The integer has both a precision and defined signedness. This allows
318 the integer to be converted to any width, since we know whether to fill
319 any extra bits with zeros or signs. */
320 FLEXIBLE_PRECISION,
322 /* The integer has a variable precision but no defined signedness. */
323 VAR_PRECISION,
325 /* The integer has a constant precision (known at GCC compile time)
326 but no defined signedness. */
327 CONST_PRECISION
330 /* This class, which has no default implementation, is expected to
331 provide the following members:
333 static const enum precision_type precision_type;
334 Classifies the type of T.
336 static const unsigned int precision;
337 Only defined if precision_type == CONST_PRECISION. Specifies the
338 precision of all integers of type T.
340 static const bool host_dependent_precision;
341 True if the precision of T depends (or can depend) on the host.
343 static unsigned int get_precision (const T &x)
344 Return the number of bits in X.
346 static wi::storage_ref *decompose (HOST_WIDE_INT *scratch,
347 unsigned int precision, const T &x)
348 Decompose X as a PRECISION-bit integer, returning the associated
349 wi::storage_ref. SCRATCH is available as scratch space if needed.
350 The routine should assert that PRECISION is acceptable. */
351 template <typename T> struct int_traits;
353 /* This class provides a single type, result_type, which specifies the
354 type of integer produced by a binary operation whose inputs have
355 types T1 and T2. The definition should be symmetric. */
356 template <typename T1, typename T2,
357 enum precision_type P1 = int_traits <T1>::precision_type,
358 enum precision_type P2 = int_traits <T2>::precision_type>
359 struct binary_traits;
361 /* The result of a unary operation on T is the same as the result of
362 a binary operation on two values of type T. */
363 template <typename T>
364 struct unary_traits : public binary_traits <T, T> {};
366 /* Specify the result type for each supported combination of binary
367 inputs. Note that CONST_PRECISION and VAR_PRECISION cannot be
368 mixed, in order to give stronger type checking. When both inputs
369 are CONST_PRECISION, they must have the same precision. */
370 template <>
371 template <typename T1, typename T2>
372 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, FLEXIBLE_PRECISION>
374 typedef widest_int result_type;
377 template <>
378 template <typename T1, typename T2>
379 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, VAR_PRECISION>
381 typedef wide_int result_type;
384 template <>
385 template <typename T1, typename T2>
386 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, CONST_PRECISION>
388 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
389 so as not to confuse gengtype. */
390 typedef generic_wide_int < fixed_wide_int_storage
391 <int_traits <T2>::precision> > result_type;
394 template <>
395 template <typename T1, typename T2>
396 struct binary_traits <T1, T2, VAR_PRECISION, FLEXIBLE_PRECISION>
398 typedef wide_int result_type;
401 template <>
402 template <typename T1, typename T2>
403 struct binary_traits <T1, T2, CONST_PRECISION, FLEXIBLE_PRECISION>
405 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
406 so as not to confuse gengtype. */
407 typedef generic_wide_int < fixed_wide_int_storage
408 <int_traits <T1>::precision> > result_type;
411 template <>
412 template <typename T1, typename T2>
413 struct binary_traits <T1, T2, CONST_PRECISION, CONST_PRECISION>
415 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
416 so as not to confuse gengtype. */
417 STATIC_ASSERT (int_traits <T1>::precision == int_traits <T2>::precision);
418 typedef generic_wide_int < fixed_wide_int_storage
419 <int_traits <T1>::precision> > result_type;
422 template <>
423 template <typename T1, typename T2>
424 struct binary_traits <T1, T2, VAR_PRECISION, VAR_PRECISION>
426 typedef wide_int result_type;
430 /* Public functions for querying and operating on integers. */
431 namespace wi
433 template <typename T>
434 unsigned int get_precision (const T &);
436 template <typename T1, typename T2>
437 unsigned int get_binary_precision (const T1 &, const T2 &);
439 template <typename T1, typename T2>
440 void copy (T1 &, const T2 &);
442 #define UNARY_PREDICATE \
443 template <typename T> bool
444 #define UNARY_FUNCTION \
445 template <typename T> WI_UNARY_RESULT (T)
446 #define BINARY_PREDICATE \
447 template <typename T1, typename T2> bool
448 #define BINARY_FUNCTION \
449 template <typename T1, typename T2> WI_BINARY_RESULT (T1, T2)
450 #define SHIFT_FUNCTION \
451 template <typename T1, typename T2> WI_UNARY_RESULT (T1)
453 UNARY_PREDICATE fits_shwi_p (const T &);
454 UNARY_PREDICATE fits_uhwi_p (const T &);
455 UNARY_PREDICATE neg_p (const T &, signop = SIGNED);
457 template <typename T>
458 HOST_WIDE_INT sign_mask (const T &);
460 BINARY_PREDICATE eq_p (const T1 &, const T2 &);
461 BINARY_PREDICATE ne_p (const T1 &, const T2 &);
462 BINARY_PREDICATE lt_p (const T1 &, const T2 &, signop);
463 BINARY_PREDICATE lts_p (const T1 &, const T2 &);
464 BINARY_PREDICATE ltu_p (const T1 &, const T2 &);
465 BINARY_PREDICATE le_p (const T1 &, const T2 &, signop);
466 BINARY_PREDICATE les_p (const T1 &, const T2 &);
467 BINARY_PREDICATE leu_p (const T1 &, const T2 &);
468 BINARY_PREDICATE gt_p (const T1 &, const T2 &, signop);
469 BINARY_PREDICATE gts_p (const T1 &, const T2 &);
470 BINARY_PREDICATE gtu_p (const T1 &, const T2 &);
471 BINARY_PREDICATE ge_p (const T1 &, const T2 &, signop);
472 BINARY_PREDICATE ges_p (const T1 &, const T2 &);
473 BINARY_PREDICATE geu_p (const T1 &, const T2 &);
475 template <typename T1, typename T2>
476 int cmp (const T1 &, const T2 &, signop);
478 template <typename T1, typename T2>
479 int cmps (const T1 &, const T2 &);
481 template <typename T1, typename T2>
482 int cmpu (const T1 &, const T2 &);
484 UNARY_FUNCTION bit_not (const T &);
485 UNARY_FUNCTION neg (const T &);
486 UNARY_FUNCTION neg (const T &, bool *);
487 UNARY_FUNCTION abs (const T &);
488 UNARY_FUNCTION ext (const T &, unsigned int, signop);
489 UNARY_FUNCTION sext (const T &, unsigned int);
490 UNARY_FUNCTION zext (const T &, unsigned int);
491 UNARY_FUNCTION set_bit (const T &, unsigned int);
493 BINARY_FUNCTION min (const T1 &, const T2 &, signop);
494 BINARY_FUNCTION smin (const T1 &, const T2 &);
495 BINARY_FUNCTION umin (const T1 &, const T2 &);
496 BINARY_FUNCTION max (const T1 &, const T2 &, signop);
497 BINARY_FUNCTION smax (const T1 &, const T2 &);
498 BINARY_FUNCTION umax (const T1 &, const T2 &);
500 BINARY_FUNCTION bit_and (const T1 &, const T2 &);
501 BINARY_FUNCTION bit_and_not (const T1 &, const T2 &);
502 BINARY_FUNCTION bit_or (const T1 &, const T2 &);
503 BINARY_FUNCTION bit_or_not (const T1 &, const T2 &);
504 BINARY_FUNCTION bit_xor (const T1 &, const T2 &);
505 BINARY_FUNCTION add (const T1 &, const T2 &);
506 BINARY_FUNCTION add (const T1 &, const T2 &, signop, bool *);
507 BINARY_FUNCTION sub (const T1 &, const T2 &);
508 BINARY_FUNCTION sub (const T1 &, const T2 &, signop, bool *);
509 BINARY_FUNCTION mul (const T1 &, const T2 &);
510 BINARY_FUNCTION mul (const T1 &, const T2 &, signop, bool *);
511 BINARY_FUNCTION smul (const T1 &, const T2 &, bool *);
512 BINARY_FUNCTION umul (const T1 &, const T2 &, bool *);
513 BINARY_FUNCTION mul_high (const T1 &, const T2 &, signop);
514 BINARY_FUNCTION div_trunc (const T1 &, const T2 &, signop, bool * = 0);
515 BINARY_FUNCTION sdiv_trunc (const T1 &, const T2 &);
516 BINARY_FUNCTION udiv_trunc (const T1 &, const T2 &);
517 BINARY_FUNCTION div_floor (const T1 &, const T2 &, signop, bool * = 0);
518 BINARY_FUNCTION udiv_floor (const T1 &, const T2 &);
519 BINARY_FUNCTION sdiv_floor (const T1 &, const T2 &);
520 BINARY_FUNCTION div_ceil (const T1 &, const T2 &, signop, bool * = 0);
521 BINARY_FUNCTION div_round (const T1 &, const T2 &, signop, bool * = 0);
522 BINARY_FUNCTION divmod_trunc (const T1 &, const T2 &, signop,
523 WI_BINARY_RESULT (T1, T2) *);
524 BINARY_FUNCTION mod_trunc (const T1 &, const T2 &, signop, bool * = 0);
525 BINARY_FUNCTION smod_trunc (const T1 &, const T2 &);
526 BINARY_FUNCTION umod_trunc (const T1 &, const T2 &);
527 BINARY_FUNCTION mod_floor (const T1 &, const T2 &, signop, bool * = 0);
528 BINARY_FUNCTION umod_floor (const T1 &, const T2 &);
529 BINARY_FUNCTION mod_ceil (const T1 &, const T2 &, signop, bool * = 0);
530 BINARY_FUNCTION mod_round (const T1 &, const T2 &, signop, bool * = 0);
532 template <typename T1, typename T2>
533 bool multiple_of_p (const T1 &, const T2 &, signop);
535 template <typename T1, typename T2>
536 bool multiple_of_p (const T1 &, const T2 &, signop,
537 WI_BINARY_RESULT (T1, T2) *);
539 SHIFT_FUNCTION lshift (const T1 &, const T2 &);
540 SHIFT_FUNCTION lrshift (const T1 &, const T2 &);
541 SHIFT_FUNCTION arshift (const T1 &, const T2 &);
542 SHIFT_FUNCTION rshift (const T1 &, const T2 &, signop sgn);
543 SHIFT_FUNCTION lrotate (const T1 &, const T2 &, unsigned int = 0);
544 SHIFT_FUNCTION rrotate (const T1 &, const T2 &, unsigned int = 0);
546 #undef SHIFT_FUNCTION
547 #undef BINARY_PREDICATE
548 #undef BINARY_FUNCTION
549 #undef UNARY_PREDICATE
550 #undef UNARY_FUNCTION
552 bool only_sign_bit_p (const wide_int_ref &, unsigned int);
553 bool only_sign_bit_p (const wide_int_ref &);
554 int clz (const wide_int_ref &);
555 int clrsb (const wide_int_ref &);
556 int ctz (const wide_int_ref &);
557 int exact_log2 (const wide_int_ref &);
558 int floor_log2 (const wide_int_ref &);
559 int ffs (const wide_int_ref &);
560 int popcount (const wide_int_ref &);
561 int parity (const wide_int_ref &);
563 template <typename T>
564 unsigned HOST_WIDE_INT extract_uhwi (const T &, unsigned int, unsigned int);
566 template <typename T>
567 unsigned int min_precision (const T &, signop);
570 namespace wi
572 /* Contains the components of a decomposed integer for easy, direct
573 access. */
574 struct storage_ref
576 storage_ref (const HOST_WIDE_INT *, unsigned int, unsigned int);
578 const HOST_WIDE_INT *val;
579 unsigned int len;
580 unsigned int precision;
582 /* Provide enough trappings for this class to act as storage for
583 generic_wide_int. */
584 unsigned int get_len () const;
585 unsigned int get_precision () const;
586 const HOST_WIDE_INT *get_val () const;
590 inline::wi::storage_ref::storage_ref (const HOST_WIDE_INT *val_in,
591 unsigned int len_in,
592 unsigned int precision_in)
593 : val (val_in), len (len_in), precision (precision_in)
597 inline unsigned int
598 wi::storage_ref::get_len () const
600 return len;
603 inline unsigned int
604 wi::storage_ref::get_precision () const
606 return precision;
609 inline const HOST_WIDE_INT *
610 wi::storage_ref::get_val () const
612 return val;
615 /* This class defines an integer type using the storage provided by the
616 template argument. The storage class must provide the following
617 functions:
619 unsigned int get_precision () const
620 Return the number of bits in the integer.
622 HOST_WIDE_INT *get_val () const
623 Return a pointer to the array of blocks that encodes the integer.
625 unsigned int get_len () const
626 Return the number of blocks in get_val (). If this is smaller
627 than the number of blocks implied by get_precision (), the
628 remaining blocks are sign extensions of block get_len () - 1.
630 Although not required by generic_wide_int itself, writable storage
631 classes can also provide the following functions:
633 HOST_WIDE_INT *write_val ()
634 Get a modifiable version of get_val ()
636 unsigned int set_len (unsigned int len)
637 Set the value returned by get_len () to LEN. */
638 template <typename storage>
639 class GTY(()) generic_wide_int : public storage
641 public:
642 generic_wide_int ();
644 template <typename T>
645 generic_wide_int (const T &);
647 template <typename T>
648 generic_wide_int (const T &, unsigned int);
650 /* Conversions. */
651 HOST_WIDE_INT to_shwi (unsigned int) const;
652 HOST_WIDE_INT to_shwi () const;
653 unsigned HOST_WIDE_INT to_uhwi (unsigned int) const;
654 unsigned HOST_WIDE_INT to_uhwi () const;
655 HOST_WIDE_INT to_short_addr () const;
657 /* Public accessors for the interior of a wide int. */
658 HOST_WIDE_INT sign_mask () const;
659 HOST_WIDE_INT elt (unsigned int) const;
660 unsigned HOST_WIDE_INT ulow () const;
661 unsigned HOST_WIDE_INT uhigh () const;
662 HOST_WIDE_INT slow () const;
663 HOST_WIDE_INT shigh () const;
665 template <typename T>
666 generic_wide_int &operator = (const T &);
668 #define BINARY_PREDICATE(OP, F) \
669 template <typename T> \
670 bool OP (const T &c) const { return wi::F (*this, c); }
672 #define UNARY_OPERATOR(OP, F) \
673 WI_UNARY_RESULT (generic_wide_int) OP () const { return wi::F (*this); }
675 #define BINARY_OPERATOR(OP, F) \
676 template <typename T> \
677 WI_BINARY_RESULT (generic_wide_int, T) \
678 OP (const T &c) const { return wi::F (*this, c); }
680 #define ASSIGNMENT_OPERATOR(OP, F) \
681 template <typename T> \
682 generic_wide_int &OP (const T &c) { return (*this = wi::F (*this, c)); }
684 #define INCDEC_OPERATOR(OP, DELTA) \
685 generic_wide_int &OP () { *this += DELTA; return *this; }
687 UNARY_OPERATOR (operator ~, bit_not)
688 UNARY_OPERATOR (operator -, neg)
689 BINARY_PREDICATE (operator ==, eq_p)
690 BINARY_PREDICATE (operator !=, ne_p)
691 BINARY_OPERATOR (operator &, bit_and)
692 BINARY_OPERATOR (and_not, bit_and_not)
693 BINARY_OPERATOR (operator |, bit_or)
694 BINARY_OPERATOR (or_not, bit_or_not)
695 BINARY_OPERATOR (operator ^, bit_xor)
696 BINARY_OPERATOR (operator +, add)
697 BINARY_OPERATOR (operator -, sub)
698 BINARY_OPERATOR (operator *, mul)
699 ASSIGNMENT_OPERATOR (operator &=, bit_and)
700 ASSIGNMENT_OPERATOR (operator |=, bit_or)
701 ASSIGNMENT_OPERATOR (operator ^=, bit_xor)
702 ASSIGNMENT_OPERATOR (operator +=, add)
703 ASSIGNMENT_OPERATOR (operator -=, sub)
704 ASSIGNMENT_OPERATOR (operator *=, mul)
705 INCDEC_OPERATOR (operator ++, 1)
706 INCDEC_OPERATOR (operator --, -1)
708 #undef BINARY_PREDICATE
709 #undef UNARY_OPERATOR
710 #undef BINARY_OPERATOR
711 #undef ASSIGNMENT_OPERATOR
712 #undef INCDEC_OPERATOR
714 /* Debugging functions. */
715 void dump () const;
717 static const bool is_sign_extended
718 = wi::int_traits <generic_wide_int <storage> >::is_sign_extended;
721 template <typename storage>
722 inline generic_wide_int <storage>::generic_wide_int () {}
724 template <typename storage>
725 template <typename T>
726 inline generic_wide_int <storage>::generic_wide_int (const T &x)
727 : storage (x)
731 template <typename storage>
732 template <typename T>
733 inline generic_wide_int <storage>::generic_wide_int (const T &x,
734 unsigned int precision)
735 : storage (x, precision)
739 /* Return THIS as a signed HOST_WIDE_INT, sign-extending from PRECISION.
740 If THIS does not fit in PRECISION, the information is lost. */
741 template <typename storage>
742 inline HOST_WIDE_INT
743 generic_wide_int <storage>::to_shwi (unsigned int precision) const
745 if (precision < HOST_BITS_PER_WIDE_INT)
746 return sext_hwi (this->get_val ()[0], precision);
747 else
748 return this->get_val ()[0];
751 /* Return THIS as a signed HOST_WIDE_INT, in its natural precision. */
752 template <typename storage>
753 inline HOST_WIDE_INT
754 generic_wide_int <storage>::to_shwi () const
756 if (is_sign_extended)
757 return this->get_val ()[0];
758 else
759 return to_shwi (this->get_precision ());
762 /* Return THIS as an unsigned HOST_WIDE_INT, zero-extending from
763 PRECISION. If THIS does not fit in PRECISION, the information
764 is lost. */
765 template <typename storage>
766 inline unsigned HOST_WIDE_INT
767 generic_wide_int <storage>::to_uhwi (unsigned int precision) const
769 if (precision < HOST_BITS_PER_WIDE_INT)
770 return zext_hwi (this->get_val ()[0], precision);
771 else
772 return this->get_val ()[0];
775 /* Return THIS as an signed HOST_WIDE_INT, in its natural precision. */
776 template <typename storage>
777 inline unsigned HOST_WIDE_INT
778 generic_wide_int <storage>::to_uhwi () const
780 return to_uhwi (this->get_precision ());
783 /* TODO: The compiler is half converted from using HOST_WIDE_INT to
784 represent addresses to using offset_int to represent addresses.
785 We use to_short_addr at the interface from new code to old,
786 unconverted code. */
787 template <typename storage>
788 inline HOST_WIDE_INT
789 generic_wide_int <storage>::to_short_addr () const
791 return this->get_val ()[0];
794 /* Return the implicit value of blocks above get_len (). */
795 template <typename storage>
796 inline HOST_WIDE_INT
797 generic_wide_int <storage>::sign_mask () const
799 unsigned int len = this->get_len ();
800 unsigned HOST_WIDE_INT high = this->get_val ()[len - 1];
801 if (!is_sign_extended)
803 unsigned int precision = this->get_precision ();
804 int excess = len * HOST_BITS_PER_WIDE_INT - precision;
805 if (excess > 0)
806 high <<= excess;
808 return (HOST_WIDE_INT) (high) < 0 ? -1 : 0;
811 /* Return the signed value of the least-significant explicitly-encoded
812 block. */
813 template <typename storage>
814 inline HOST_WIDE_INT
815 generic_wide_int <storage>::slow () const
817 return this->get_val ()[0];
820 /* Return the signed value of the most-significant explicitly-encoded
821 block. */
822 template <typename storage>
823 inline HOST_WIDE_INT
824 generic_wide_int <storage>::shigh () const
826 return this->get_val ()[this->get_len () - 1];
829 /* Return the unsigned value of the least-significant
830 explicitly-encoded block. */
831 template <typename storage>
832 inline unsigned HOST_WIDE_INT
833 generic_wide_int <storage>::ulow () const
835 return this->get_val ()[0];
838 /* Return the unsigned value of the most-significant
839 explicitly-encoded block. */
840 template <typename storage>
841 inline unsigned HOST_WIDE_INT
842 generic_wide_int <storage>::uhigh () const
844 return this->get_val ()[this->get_len () - 1];
847 /* Return block I, which might be implicitly or explicit encoded. */
848 template <typename storage>
849 inline HOST_WIDE_INT
850 generic_wide_int <storage>::elt (unsigned int i) const
852 if (i >= this->get_len ())
853 return sign_mask ();
854 else
855 return this->get_val ()[i];
858 template <typename storage>
859 template <typename T>
860 generic_wide_int <storage> &
861 generic_wide_int <storage>::operator = (const T &x)
863 storage::operator = (x);
864 return *this;
867 /* Dump the contents of the integer to stderr, for debugging. */
868 template <typename storage>
869 void
870 generic_wide_int <storage>::dump () const
872 unsigned int len = this->get_len ();
873 const HOST_WIDE_INT *val = this->get_val ();
874 unsigned int precision = this->get_precision ();
875 fprintf (stderr, "[");
876 if (len * HOST_BITS_PER_WIDE_INT < precision)
877 fprintf (stderr, "...,");
878 for (unsigned int i = 0; i < len - 1; ++i)
879 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX ",", val[len - 1 - i]);
880 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX "], precision = %d\n",
881 val[0], precision);
884 namespace wi
886 template <>
887 template <typename storage>
888 struct int_traits < generic_wide_int <storage> >
889 : public wi::int_traits <storage>
891 static unsigned int get_precision (const generic_wide_int <storage> &);
892 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
893 const generic_wide_int <storage> &);
897 template <typename storage>
898 inline unsigned int
899 wi::int_traits < generic_wide_int <storage> >::
900 get_precision (const generic_wide_int <storage> &x)
902 return x.get_precision ();
905 template <typename storage>
906 inline wi::storage_ref
907 wi::int_traits < generic_wide_int <storage> >::
908 decompose (HOST_WIDE_INT *, unsigned int precision,
909 const generic_wide_int <storage> &x)
911 gcc_checking_assert (precision == x.get_precision ());
912 return wi::storage_ref (x.get_val (), x.get_len (), precision);
915 /* Provide the storage for a wide_int_ref. This acts like a read-only
916 wide_int, with the optimization that VAL is normally a pointer to
917 another integer's storage, so that no array copy is needed. */
918 template <bool SE>
919 struct wide_int_ref_storage : public wi::storage_ref
921 private:
922 /* Scratch space that can be used when decomposing the original integer.
923 It must live as long as this object. */
924 HOST_WIDE_INT scratch[2];
926 public:
927 wide_int_ref_storage (const wi::storage_ref &);
929 template <typename T>
930 wide_int_ref_storage (const T &);
932 template <typename T>
933 wide_int_ref_storage (const T &, unsigned int);
936 /* Create a reference from an existing reference. */
937 template <bool SE>
938 inline wide_int_ref_storage <SE>::
939 wide_int_ref_storage (const wi::storage_ref &x)
940 : storage_ref (x)
943 /* Create a reference to integer X in its natural precision. Note
944 that the natural precision is host-dependent for primitive
945 types. */
946 template <bool SE>
947 template <typename T>
948 inline wide_int_ref_storage <SE>::wide_int_ref_storage (const T &x)
949 : storage_ref (wi::int_traits <T>::decompose (scratch,
950 wi::get_precision (x), x))
954 /* Create a reference to integer X in precision PRECISION. */
955 template <bool SE>
956 template <typename T>
957 inline wide_int_ref_storage <SE>::wide_int_ref_storage (const T &x,
958 unsigned int precision)
959 : storage_ref (wi::int_traits <T>::decompose (scratch, precision, x))
963 namespace wi
965 template <>
966 template <bool SE>
967 struct int_traits <wide_int_ref_storage <SE> >
969 static const enum precision_type precision_type = VAR_PRECISION;
970 /* wi::storage_ref can be a reference to a primitive type,
971 so this is the conservatively-correct setting. */
972 static const bool host_dependent_precision = true;
973 static const bool is_sign_extended = SE;
977 namespace wi
979 unsigned int force_to_size (HOST_WIDE_INT *, const HOST_WIDE_INT *,
980 unsigned int, unsigned int, unsigned int,
981 signop sgn);
982 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
983 unsigned int, unsigned int, bool = true);
986 /* The storage used by wide_int. */
987 class GTY(()) wide_int_storage
989 private:
990 HOST_WIDE_INT val[WIDE_INT_MAX_ELTS];
991 unsigned int len;
992 unsigned int precision;
994 public:
995 wide_int_storage ();
996 template <typename T>
997 wide_int_storage (const T &);
999 /* The standard generic_wide_int storage methods. */
1000 unsigned int get_precision () const;
1001 const HOST_WIDE_INT *get_val () const;
1002 unsigned int get_len () const;
1003 HOST_WIDE_INT *write_val ();
1004 void set_len (unsigned int, bool = false);
1006 static wide_int from (const wide_int_ref &, unsigned int, signop);
1007 static wide_int from_array (const HOST_WIDE_INT *, unsigned int,
1008 unsigned int, bool = true);
1009 static wide_int create (unsigned int);
1011 /* FIXME: target-dependent, so should disappear. */
1012 wide_int bswap () const;
1015 namespace wi
1017 template <>
1018 struct int_traits <wide_int_storage>
1020 static const enum precision_type precision_type = VAR_PRECISION;
1021 /* Guaranteed by a static assert in the wide_int_storage constructor. */
1022 static const bool host_dependent_precision = false;
1023 static const bool is_sign_extended = true;
1024 template <typename T1, typename T2>
1025 static wide_int get_binary_result (const T1 &, const T2 &);
1029 inline wide_int_storage::wide_int_storage () {}
1031 /* Initialize the storage from integer X, in its natural precision.
1032 Note that we do not allow integers with host-dependent precision
1033 to become wide_ints; wide_ints must always be logically independent
1034 of the host. */
1035 template <typename T>
1036 inline wide_int_storage::wide_int_storage (const T &x)
1038 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision); }
1039 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION); }
1040 WIDE_INT_REF_FOR (T) xi (x);
1041 precision = xi.precision;
1042 wi::copy (*this, xi);
1045 inline unsigned int
1046 wide_int_storage::get_precision () const
1048 return precision;
1051 inline const HOST_WIDE_INT *
1052 wide_int_storage::get_val () const
1054 return val;
1057 inline unsigned int
1058 wide_int_storage::get_len () const
1060 return len;
1063 inline HOST_WIDE_INT *
1064 wide_int_storage::write_val ()
1066 return val;
1069 inline void
1070 wide_int_storage::set_len (unsigned int l, bool is_sign_extended)
1072 len = l;
1073 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > precision)
1074 val[len - 1] = sext_hwi (val[len - 1],
1075 precision % HOST_BITS_PER_WIDE_INT);
1078 /* Treat X as having signedness SGN and convert it to a PRECISION-bit
1079 number. */
1080 inline wide_int
1081 wide_int_storage::from (const wide_int_ref &x, unsigned int precision,
1082 signop sgn)
1084 wide_int result = wide_int::create (precision);
1085 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1086 x.precision, precision, sgn));
1087 return result;
1090 /* Create a wide_int from the explicit block encoding given by VAL and
1091 LEN. PRECISION is the precision of the integer. NEED_CANON_P is
1092 true if the encoding may have redundant trailing blocks. */
1093 inline wide_int
1094 wide_int_storage::from_array (const HOST_WIDE_INT *val, unsigned int len,
1095 unsigned int precision, bool need_canon_p)
1097 wide_int result = wide_int::create (precision);
1098 result.set_len (wi::from_array (result.write_val (), val, len, precision,
1099 need_canon_p));
1100 return result;
1103 /* Return an uninitialized wide_int with precision PRECISION. */
1104 inline wide_int
1105 wide_int_storage::create (unsigned int precision)
1107 wide_int x;
1108 x.precision = precision;
1109 return x;
1112 template <typename T1, typename T2>
1113 inline wide_int
1114 wi::int_traits <wide_int_storage>::get_binary_result (const T1 &x, const T2 &y)
1116 /* This shouldn't be used for two flexible-precision inputs. */
1117 STATIC_ASSERT (wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION
1118 || wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION);
1119 if (wi::int_traits <T1>::precision_type == FLEXIBLE_PRECISION)
1120 return wide_int::create (wi::get_precision (y));
1121 else
1122 return wide_int::create (wi::get_precision (x));
1125 /* The storage used by FIXED_WIDE_INT (N). */
1126 template <int N>
1127 class GTY(()) fixed_wide_int_storage
1129 private:
1130 HOST_WIDE_INT val[(N + HOST_BITS_PER_WIDE_INT + 1) / HOST_BITS_PER_WIDE_INT];
1131 unsigned int len;
1133 public:
1134 fixed_wide_int_storage ();
1135 template <typename T>
1136 fixed_wide_int_storage (const T &);
1138 /* The standard generic_wide_int storage methods. */
1139 unsigned int get_precision () const;
1140 const HOST_WIDE_INT *get_val () const;
1141 unsigned int get_len () const;
1142 HOST_WIDE_INT *write_val ();
1143 void set_len (unsigned int, bool = false);
1145 static FIXED_WIDE_INT (N) from (const wide_int_ref &, signop);
1146 static FIXED_WIDE_INT (N) from_array (const HOST_WIDE_INT *, unsigned int,
1147 bool = true);
1150 namespace wi
1152 template <>
1153 template <int N>
1154 struct int_traits < fixed_wide_int_storage <N> >
1156 static const enum precision_type precision_type = CONST_PRECISION;
1157 static const bool host_dependent_precision = false;
1158 static const bool is_sign_extended = true;
1159 static const unsigned int precision = N;
1160 template <typename T1, typename T2>
1161 static FIXED_WIDE_INT (N) get_binary_result (const T1 &, const T2 &);
1165 template <int N>
1166 inline fixed_wide_int_storage <N>::fixed_wide_int_storage () {}
1168 /* Initialize the storage from integer X, in precision N. */
1169 template <int N>
1170 template <typename T>
1171 inline fixed_wide_int_storage <N>::fixed_wide_int_storage (const T &x)
1173 /* Check for type compatibility. We don't want to initialize a
1174 fixed-width integer from something like a wide_int. */
1175 WI_BINARY_RESULT (T, FIXED_WIDE_INT (N)) *assertion ATTRIBUTE_UNUSED;
1176 wi::copy (*this, WIDE_INT_REF_FOR (T) (x, N));
1179 template <int N>
1180 inline unsigned int
1181 fixed_wide_int_storage <N>::get_precision () const
1183 return N;
1186 template <int N>
1187 inline const HOST_WIDE_INT *
1188 fixed_wide_int_storage <N>::get_val () const
1190 return val;
1193 template <int N>
1194 inline unsigned int
1195 fixed_wide_int_storage <N>::get_len () const
1197 return len;
1200 template <int N>
1201 inline HOST_WIDE_INT *
1202 fixed_wide_int_storage <N>::write_val ()
1204 return val;
1207 template <int N>
1208 inline void
1209 fixed_wide_int_storage <N>::set_len (unsigned int l, bool)
1211 len = l;
1212 /* There are no excess bits in val[len - 1]. */
1213 STATIC_ASSERT (N % HOST_BITS_PER_WIDE_INT == 0);
1216 /* Treat X as having signedness SGN and convert it to an N-bit number. */
1217 template <int N>
1218 inline FIXED_WIDE_INT (N)
1219 fixed_wide_int_storage <N>::from (const wide_int_ref &x, signop sgn)
1221 FIXED_WIDE_INT (N) result;
1222 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1223 x.precision, N, sgn));
1224 return result;
1227 /* Create a FIXED_WIDE_INT (N) from the explicit block encoding given by
1228 VAL and LEN. NEED_CANON_P is true if the encoding may have redundant
1229 trailing blocks. */
1230 template <int N>
1231 inline FIXED_WIDE_INT (N)
1232 fixed_wide_int_storage <N>::from_array (const HOST_WIDE_INT *val,
1233 unsigned int len,
1234 bool need_canon_p)
1236 FIXED_WIDE_INT (N) result;
1237 result.set_len (wi::from_array (result.write_val (), val, len,
1238 N, need_canon_p));
1239 return result;
1242 template <int N>
1243 template <typename T1, typename T2>
1244 inline FIXED_WIDE_INT (N)
1245 wi::int_traits < fixed_wide_int_storage <N> >::
1246 get_binary_result (const T1 &, const T2 &)
1248 return FIXED_WIDE_INT (N) ();
1251 /* A reference to one element of a trailing_wide_ints structure. */
1252 class trailing_wide_int_storage
1254 private:
1255 /* The precision of the integer, which is a fixed property of the
1256 parent trailing_wide_ints. */
1257 unsigned int m_precision;
1259 /* A pointer to the length field. */
1260 unsigned char *m_len;
1262 /* A pointer to the HWI array. There are enough elements to hold all
1263 values of precision M_PRECISION. */
1264 HOST_WIDE_INT *m_val;
1266 public:
1267 trailing_wide_int_storage (unsigned int, unsigned char *, HOST_WIDE_INT *);
1269 /* The standard generic_wide_int storage methods. */
1270 unsigned int get_len () const;
1271 unsigned int get_precision () const;
1272 const HOST_WIDE_INT *get_val () const;
1273 HOST_WIDE_INT *write_val ();
1274 void set_len (unsigned int, bool = false);
1276 template <typename T>
1277 trailing_wide_int_storage &operator = (const T &);
1280 typedef generic_wide_int <trailing_wide_int_storage> trailing_wide_int;
1282 /* trailing_wide_int behaves like a wide_int. */
1283 namespace wi
1285 template <>
1286 struct int_traits <trailing_wide_int_storage>
1287 : public int_traits <wide_int_storage> {};
1290 /* An array of N wide_int-like objects that can be put at the end of
1291 a variable-sized structure. Use extra_size to calculate how many
1292 bytes beyond the sizeof need to be allocated. Use set_precision
1293 to initialize the structure. */
1294 template <int N>
1295 class GTY(()) trailing_wide_ints
1297 private:
1298 /* The shared precision of each number. */
1299 unsigned short m_precision;
1301 /* The shared maximum length of each number. */
1302 unsigned char m_max_len;
1304 /* The current length of each number. */
1305 unsigned char m_len[N];
1307 /* The variable-length part of the structure, which always contains
1308 at least one HWI. Element I starts at index I * M_MAX_LEN. */
1309 HOST_WIDE_INT m_val[1];
1311 public:
1312 void set_precision (unsigned int);
1313 trailing_wide_int operator [] (unsigned int);
1314 static size_t extra_size (unsigned int);
1317 inline trailing_wide_int_storage::
1318 trailing_wide_int_storage (unsigned int precision, unsigned char *len,
1319 HOST_WIDE_INT *val)
1320 : m_precision (precision), m_len (len), m_val (val)
1324 inline unsigned int
1325 trailing_wide_int_storage::get_len () const
1327 return *m_len;
1330 inline unsigned int
1331 trailing_wide_int_storage::get_precision () const
1333 return m_precision;
1336 inline const HOST_WIDE_INT *
1337 trailing_wide_int_storage::get_val () const
1339 return m_val;
1342 inline HOST_WIDE_INT *
1343 trailing_wide_int_storage::write_val ()
1345 return m_val;
1348 inline void
1349 trailing_wide_int_storage::set_len (unsigned int len, bool is_sign_extended)
1351 *m_len = len;
1352 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > m_precision)
1353 m_val[len - 1] = sext_hwi (m_val[len - 1],
1354 m_precision % HOST_BITS_PER_WIDE_INT);
1357 template <typename T>
1358 inline trailing_wide_int_storage &
1359 trailing_wide_int_storage::operator = (const T &x)
1361 WIDE_INT_REF_FOR (T) xi (x, m_precision);
1362 wi::copy (*this, xi);
1363 return *this;
1366 /* Initialize the structure and record that all elements have precision
1367 PRECISION. */
1368 template <int N>
1369 inline void
1370 trailing_wide_ints <N>::set_precision (unsigned int precision)
1372 m_precision = precision;
1373 m_max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1374 / HOST_BITS_PER_WIDE_INT);
1377 /* Return a reference to element INDEX. */
1378 template <int N>
1379 inline trailing_wide_int
1380 trailing_wide_ints <N>::operator [] (unsigned int index)
1382 return trailing_wide_int_storage (m_precision, &m_len[index],
1383 &m_val[index * m_max_len]);
1386 /* Return how many extra bytes need to be added to the end of the structure
1387 in order to handle N wide_ints of precision PRECISION. */
1388 template <int N>
1389 inline size_t
1390 trailing_wide_ints <N>::extra_size (unsigned int precision)
1392 unsigned int max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1393 / HOST_BITS_PER_WIDE_INT);
1394 return (N * max_len - 1) * sizeof (HOST_WIDE_INT);
1397 /* This macro is used in structures that end with a trailing_wide_ints field
1398 called FIELD. It declares get_NAME() and set_NAME() methods to access
1399 element I of FIELD. */
1400 #define TRAILING_WIDE_INT_ACCESSOR(NAME, FIELD, I) \
1401 trailing_wide_int get_##NAME () { return FIELD[I]; } \
1402 template <typename T> void set_##NAME (const T &x) { FIELD[I] = x; }
1404 namespace wi
1406 /* Implementation of int_traits for primitive integer types like "int". */
1407 template <typename T, bool signed_p>
1408 struct primitive_int_traits
1410 static const enum precision_type precision_type = FLEXIBLE_PRECISION;
1411 static const bool host_dependent_precision = true;
1412 static const bool is_sign_extended = true;
1413 static unsigned int get_precision (T);
1414 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int, T);
1418 template <typename T, bool signed_p>
1419 inline unsigned int
1420 wi::primitive_int_traits <T, signed_p>::get_precision (T)
1422 return sizeof (T) * CHAR_BIT;
1425 template <typename T, bool signed_p>
1426 inline wi::storage_ref
1427 wi::primitive_int_traits <T, signed_p>::decompose (HOST_WIDE_INT *scratch,
1428 unsigned int precision, T x)
1430 scratch[0] = x;
1431 if (signed_p || scratch[0] >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1432 return wi::storage_ref (scratch, 1, precision);
1433 scratch[1] = 0;
1434 return wi::storage_ref (scratch, 2, precision);
1437 /* Allow primitive C types to be used in wi:: routines. */
1438 namespace wi
1440 template <>
1441 struct int_traits <int>
1442 : public primitive_int_traits <int, true> {};
1444 template <>
1445 struct int_traits <unsigned int>
1446 : public primitive_int_traits <unsigned int, false> {};
1448 template <>
1449 struct int_traits <HOST_WIDE_INT>
1450 : public primitive_int_traits <HOST_WIDE_INT, true> {};
1452 template <>
1453 struct int_traits <unsigned HOST_WIDE_INT>
1454 : public primitive_int_traits <unsigned HOST_WIDE_INT, false> {};
1457 namespace wi
1459 /* Stores HWI-sized integer VAL, treating it as having signedness SGN
1460 and precision PRECISION. */
1461 struct hwi_with_prec
1463 hwi_with_prec (HOST_WIDE_INT, unsigned int, signop);
1464 HOST_WIDE_INT val;
1465 unsigned int precision;
1466 signop sgn;
1469 hwi_with_prec shwi (HOST_WIDE_INT, unsigned int);
1470 hwi_with_prec uhwi (unsigned HOST_WIDE_INT, unsigned int);
1472 hwi_with_prec minus_one (unsigned int);
1473 hwi_with_prec zero (unsigned int);
1474 hwi_with_prec one (unsigned int);
1475 hwi_with_prec two (unsigned int);
1478 inline wi::hwi_with_prec::hwi_with_prec (HOST_WIDE_INT v, unsigned int p,
1479 signop s)
1480 : val (v), precision (p), sgn (s)
1484 /* Return a signed integer that has value VAL and precision PRECISION. */
1485 inline wi::hwi_with_prec
1486 wi::shwi (HOST_WIDE_INT val, unsigned int precision)
1488 return hwi_with_prec (val, precision, SIGNED);
1491 /* Return an unsigned integer that has value VAL and precision PRECISION. */
1492 inline wi::hwi_with_prec
1493 wi::uhwi (unsigned HOST_WIDE_INT val, unsigned int precision)
1495 return hwi_with_prec (val, precision, UNSIGNED);
1498 /* Return a wide int of -1 with precision PRECISION. */
1499 inline wi::hwi_with_prec
1500 wi::minus_one (unsigned int precision)
1502 return wi::shwi (-1, precision);
1505 /* Return a wide int of 0 with precision PRECISION. */
1506 inline wi::hwi_with_prec
1507 wi::zero (unsigned int precision)
1509 return wi::shwi (0, precision);
1512 /* Return a wide int of 1 with precision PRECISION. */
1513 inline wi::hwi_with_prec
1514 wi::one (unsigned int precision)
1516 return wi::shwi (1, precision);
1519 /* Return a wide int of 2 with precision PRECISION. */
1520 inline wi::hwi_with_prec
1521 wi::two (unsigned int precision)
1523 return wi::shwi (2, precision);
1526 namespace wi
1528 template <>
1529 struct int_traits <wi::hwi_with_prec>
1531 static const enum precision_type precision_type = VAR_PRECISION;
1532 /* hwi_with_prec has an explicitly-given precision, rather than the
1533 precision of HOST_WIDE_INT. */
1534 static const bool host_dependent_precision = false;
1535 static const bool is_sign_extended = true;
1536 static unsigned int get_precision (const wi::hwi_with_prec &);
1537 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
1538 const wi::hwi_with_prec &);
1542 inline unsigned int
1543 wi::int_traits <wi::hwi_with_prec>::get_precision (const wi::hwi_with_prec &x)
1545 return x.precision;
1548 inline wi::storage_ref
1549 wi::int_traits <wi::hwi_with_prec>::
1550 decompose (HOST_WIDE_INT *scratch, unsigned int precision,
1551 const wi::hwi_with_prec &x)
1553 gcc_checking_assert (precision == x.precision);
1554 scratch[0] = x.val;
1555 if (x.sgn == SIGNED || x.val >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1556 return wi::storage_ref (scratch, 1, precision);
1557 scratch[1] = 0;
1558 return wi::storage_ref (scratch, 2, precision);
1561 /* Private functions for handling large cases out of line. They take
1562 individual length and array parameters because that is cheaper for
1563 the inline caller than constructing an object on the stack and
1564 passing a reference to it. (Although many callers use wide_int_refs,
1565 we generally want those to be removed by SRA.) */
1566 namespace wi
1568 bool eq_p_large (const HOST_WIDE_INT *, unsigned int,
1569 const HOST_WIDE_INT *, unsigned int, unsigned int);
1570 bool lts_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1571 const HOST_WIDE_INT *, unsigned int);
1572 bool ltu_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1573 const HOST_WIDE_INT *, unsigned int);
1574 int cmps_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1575 const HOST_WIDE_INT *, unsigned int);
1576 int cmpu_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1577 const HOST_WIDE_INT *, unsigned int);
1578 unsigned int sext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1579 unsigned int,
1580 unsigned int, unsigned int);
1581 unsigned int zext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1582 unsigned int,
1583 unsigned int, unsigned int);
1584 unsigned int set_bit_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1585 unsigned int, unsigned int, unsigned int);
1586 unsigned int lshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1587 unsigned int, unsigned int, unsigned int);
1588 unsigned int lrshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1589 unsigned int, unsigned int, unsigned int,
1590 unsigned int);
1591 unsigned int arshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1592 unsigned int, unsigned int, unsigned int,
1593 unsigned int);
1594 unsigned int and_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1595 const HOST_WIDE_INT *, unsigned int, unsigned int);
1596 unsigned int and_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1597 unsigned int, const HOST_WIDE_INT *,
1598 unsigned int, unsigned int);
1599 unsigned int or_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1600 const HOST_WIDE_INT *, unsigned int, unsigned int);
1601 unsigned int or_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1602 unsigned int, const HOST_WIDE_INT *,
1603 unsigned int, unsigned int);
1604 unsigned int xor_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1605 const HOST_WIDE_INT *, unsigned int, unsigned int);
1606 unsigned int add_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1607 const HOST_WIDE_INT *, unsigned int, unsigned int,
1608 signop, bool *);
1609 unsigned int sub_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1610 const HOST_WIDE_INT *, unsigned int, unsigned int,
1611 signop, bool *);
1612 unsigned int mul_internal (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1613 unsigned int, const HOST_WIDE_INT *,
1614 unsigned int, unsigned int, signop, bool *,
1615 bool);
1616 unsigned int divmod_internal (HOST_WIDE_INT *, unsigned int *,
1617 HOST_WIDE_INT *, const HOST_WIDE_INT *,
1618 unsigned int, unsigned int,
1619 const HOST_WIDE_INT *,
1620 unsigned int, unsigned int,
1621 signop, bool *);
1624 /* Return the number of bits that integer X can hold. */
1625 template <typename T>
1626 inline unsigned int
1627 wi::get_precision (const T &x)
1629 return wi::int_traits <T>::get_precision (x);
1632 /* Return the number of bits that the result of a binary operation can
1633 hold when the input operands are X and Y. */
1634 template <typename T1, typename T2>
1635 inline unsigned int
1636 wi::get_binary_precision (const T1 &x, const T2 &y)
1638 return get_precision (wi::int_traits <WI_BINARY_RESULT (T1, T2)>::
1639 get_binary_result (x, y));
1642 /* Copy the contents of Y to X, but keeping X's current precision. */
1643 template <typename T1, typename T2>
1644 inline void
1645 wi::copy (T1 &x, const T2 &y)
1647 HOST_WIDE_INT *xval = x.write_val ();
1648 const HOST_WIDE_INT *yval = y.get_val ();
1649 unsigned int len = y.get_len ();
1650 unsigned int i = 0;
1652 xval[i] = yval[i];
1653 while (++i < len);
1654 x.set_len (len, y.is_sign_extended);
1657 /* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
1658 template <typename T>
1659 inline bool
1660 wi::fits_shwi_p (const T &x)
1662 WIDE_INT_REF_FOR (T) xi (x);
1663 return xi.len == 1;
1666 /* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
1667 precision. */
1668 template <typename T>
1669 inline bool
1670 wi::fits_uhwi_p (const T &x)
1672 WIDE_INT_REF_FOR (T) xi (x);
1673 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
1674 return true;
1675 if (xi.len == 1)
1676 return xi.slow () >= 0;
1677 return xi.len == 2 && xi.uhigh () == 0;
1680 /* Return true if X is negative based on the interpretation of SGN.
1681 For UNSIGNED, this is always false. */
1682 template <typename T>
1683 inline bool
1684 wi::neg_p (const T &x, signop sgn)
1686 WIDE_INT_REF_FOR (T) xi (x);
1687 if (sgn == UNSIGNED)
1688 return false;
1689 return xi.sign_mask () < 0;
1692 /* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
1693 template <typename T>
1694 inline HOST_WIDE_INT
1695 wi::sign_mask (const T &x)
1697 WIDE_INT_REF_FOR (T) xi (x);
1698 return xi.sign_mask ();
1701 /* Return true if X == Y. X and Y must be binary-compatible. */
1702 template <typename T1, typename T2>
1703 inline bool
1704 wi::eq_p (const T1 &x, const T2 &y)
1706 unsigned int precision = get_binary_precision (x, y);
1707 WIDE_INT_REF_FOR (T1) xi (x, precision);
1708 WIDE_INT_REF_FOR (T2) yi (y, precision);
1709 if (xi.is_sign_extended && yi.is_sign_extended)
1711 /* This case reduces to array equality. */
1712 if (xi.len != yi.len)
1713 return false;
1714 unsigned int i = 0;
1716 if (xi.val[i] != yi.val[i])
1717 return false;
1718 while (++i != xi.len);
1719 return true;
1721 if (__builtin_expect (yi.len == 1, true))
1723 /* XI is only equal to YI if it too has a single HWI. */
1724 if (xi.len != 1)
1725 return false;
1726 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
1727 with 0 are simple. */
1728 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1729 return xi.val[0] == 0;
1730 /* Otherwise flush out any excess bits first. */
1731 unsigned HOST_WIDE_INT diff = xi.val[0] ^ yi.val[0];
1732 int excess = HOST_BITS_PER_WIDE_INT - precision;
1733 if (excess > 0)
1734 diff <<= excess;
1735 return diff == 0;
1737 return eq_p_large (xi.val, xi.len, yi.val, yi.len, precision);
1740 /* Return true if X != Y. X and Y must be binary-compatible. */
1741 template <typename T1, typename T2>
1742 inline bool
1743 wi::ne_p (const T1 &x, const T2 &y)
1745 return !eq_p (x, y);
1748 /* Return true if X < Y when both are treated as signed values. */
1749 template <typename T1, typename T2>
1750 inline bool
1751 wi::lts_p (const T1 &x, const T2 &y)
1753 unsigned int precision = get_binary_precision (x, y);
1754 WIDE_INT_REF_FOR (T1) xi (x, precision);
1755 WIDE_INT_REF_FOR (T2) yi (y, precision);
1756 /* We optimize x < y, where y is 64 or fewer bits. */
1757 if (wi::fits_shwi_p (yi))
1759 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
1760 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1761 return neg_p (xi);
1762 /* If x fits directly into a shwi, we can compare directly. */
1763 if (wi::fits_shwi_p (xi))
1764 return xi.to_shwi () < yi.to_shwi ();
1765 /* If x doesn't fit and is negative, then it must be more
1766 negative than any value in y, and hence smaller than y. */
1767 if (neg_p (xi))
1768 return true;
1769 /* If x is positive, then it must be larger than any value in y,
1770 and hence greater than y. */
1771 return false;
1773 /* Optimize the opposite case, if it can be detected at compile time. */
1774 if (STATIC_CONSTANT_P (xi.len == 1))
1775 /* If YI is negative it is lower than the least HWI.
1776 If YI is positive it is greater than the greatest HWI. */
1777 return !neg_p (yi);
1778 return lts_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1781 /* Return true if X < Y when both are treated as unsigned values. */
1782 template <typename T1, typename T2>
1783 inline bool
1784 wi::ltu_p (const T1 &x, const T2 &y)
1786 unsigned int precision = get_binary_precision (x, y);
1787 WIDE_INT_REF_FOR (T1) xi (x, precision);
1788 WIDE_INT_REF_FOR (T2) yi (y, precision);
1789 /* Optimize comparisons with constants. */
1790 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1791 return xi.len == 1 && xi.to_uhwi () < (unsigned HOST_WIDE_INT) yi.val[0];
1792 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1793 return yi.len != 1 || yi.to_uhwi () > (unsigned HOST_WIDE_INT) xi.val[0];
1794 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1795 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1796 values does not change the result. */
1797 if (__builtin_expect (xi.len + yi.len == 2, true))
1799 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1800 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1801 return xl < yl;
1803 return ltu_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1806 /* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
1807 template <typename T1, typename T2>
1808 inline bool
1809 wi::lt_p (const T1 &x, const T2 &y, signop sgn)
1811 if (sgn == SIGNED)
1812 return lts_p (x, y);
1813 else
1814 return ltu_p (x, y);
1817 /* Return true if X <= Y when both are treated as signed values. */
1818 template <typename T1, typename T2>
1819 inline bool
1820 wi::les_p (const T1 &x, const T2 &y)
1822 return !lts_p (y, x);
1825 /* Return true if X <= Y when both are treated as unsigned values. */
1826 template <typename T1, typename T2>
1827 inline bool
1828 wi::leu_p (const T1 &x, const T2 &y)
1830 return !ltu_p (y, x);
1833 /* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
1834 template <typename T1, typename T2>
1835 inline bool
1836 wi::le_p (const T1 &x, const T2 &y, signop sgn)
1838 if (sgn == SIGNED)
1839 return les_p (x, y);
1840 else
1841 return leu_p (x, y);
1844 /* Return true if X > Y when both are treated as signed values. */
1845 template <typename T1, typename T2>
1846 inline bool
1847 wi::gts_p (const T1 &x, const T2 &y)
1849 return lts_p (y, x);
1852 /* Return true if X > Y when both are treated as unsigned values. */
1853 template <typename T1, typename T2>
1854 inline bool
1855 wi::gtu_p (const T1 &x, const T2 &y)
1857 return ltu_p (y, x);
1860 /* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
1861 template <typename T1, typename T2>
1862 inline bool
1863 wi::gt_p (const T1 &x, const T2 &y, signop sgn)
1865 if (sgn == SIGNED)
1866 return gts_p (x, y);
1867 else
1868 return gtu_p (x, y);
1871 /* Return true if X >= Y when both are treated as signed values. */
1872 template <typename T1, typename T2>
1873 inline bool
1874 wi::ges_p (const T1 &x, const T2 &y)
1876 return !lts_p (x, y);
1879 /* Return true if X >= Y when both are treated as unsigned values. */
1880 template <typename T1, typename T2>
1881 inline bool
1882 wi::geu_p (const T1 &x, const T2 &y)
1884 return !ltu_p (x, y);
1887 /* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
1888 template <typename T1, typename T2>
1889 inline bool
1890 wi::ge_p (const T1 &x, const T2 &y, signop sgn)
1892 if (sgn == SIGNED)
1893 return ges_p (x, y);
1894 else
1895 return geu_p (x, y);
1898 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1899 as signed values. */
1900 template <typename T1, typename T2>
1901 inline int
1902 wi::cmps (const T1 &x, const T2 &y)
1904 unsigned int precision = get_binary_precision (x, y);
1905 WIDE_INT_REF_FOR (T1) xi (x, precision);
1906 WIDE_INT_REF_FOR (T2) yi (y, precision);
1907 if (wi::fits_shwi_p (yi))
1909 /* Special case for comparisons with 0. */
1910 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1911 return neg_p (xi) ? -1 : !(xi.len == 1 && xi.val[0] == 0);
1912 /* If x fits into a signed HWI, we can compare directly. */
1913 if (wi::fits_shwi_p (xi))
1915 HOST_WIDE_INT xl = xi.to_shwi ();
1916 HOST_WIDE_INT yl = yi.to_shwi ();
1917 return xl < yl ? -1 : xl > yl;
1919 /* If x doesn't fit and is negative, then it must be more
1920 negative than any signed HWI, and hence smaller than y. */
1921 if (neg_p (xi))
1922 return -1;
1923 /* If x is positive, then it must be larger than any signed HWI,
1924 and hence greater than y. */
1925 return 1;
1927 /* Optimize the opposite case, if it can be detected at compile time. */
1928 if (STATIC_CONSTANT_P (xi.len == 1))
1929 /* If YI is negative it is lower than the least HWI.
1930 If YI is positive it is greater than the greatest HWI. */
1931 return neg_p (yi) ? 1 : -1;
1932 return cmps_large (xi.val, xi.len, precision, yi.val, yi.len);
1935 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1936 as unsigned values. */
1937 template <typename T1, typename T2>
1938 inline int
1939 wi::cmpu (const T1 &x, const T2 &y)
1941 unsigned int precision = get_binary_precision (x, y);
1942 WIDE_INT_REF_FOR (T1) xi (x, precision);
1943 WIDE_INT_REF_FOR (T2) yi (y, precision);
1944 /* Optimize comparisons with constants. */
1945 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1947 /* If XI doesn't fit in a HWI then it must be larger than YI. */
1948 if (xi.len != 1)
1949 return 1;
1950 /* Otherwise compare directly. */
1951 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1952 unsigned HOST_WIDE_INT yl = yi.val[0];
1953 return xl < yl ? -1 : xl > yl;
1955 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1957 /* If YI doesn't fit in a HWI then it must be larger than XI. */
1958 if (yi.len != 1)
1959 return -1;
1960 /* Otherwise compare directly. */
1961 unsigned HOST_WIDE_INT xl = xi.val[0];
1962 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1963 return xl < yl ? -1 : xl > yl;
1965 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1966 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1967 values does not change the result. */
1968 if (__builtin_expect (xi.len + yi.len == 2, true))
1970 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1971 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1972 return xl < yl ? -1 : xl > yl;
1974 return cmpu_large (xi.val, xi.len, precision, yi.val, yi.len);
1977 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
1978 X and Y indicated by SGN. */
1979 template <typename T1, typename T2>
1980 inline int
1981 wi::cmp (const T1 &x, const T2 &y, signop sgn)
1983 if (sgn == SIGNED)
1984 return cmps (x, y);
1985 else
1986 return cmpu (x, y);
1989 /* Return ~x. */
1990 template <typename T>
1991 inline WI_UNARY_RESULT (T)
1992 wi::bit_not (const T &x)
1994 WI_UNARY_RESULT_VAR (result, val, T, x);
1995 WIDE_INT_REF_FOR (T) xi (x, get_precision (result));
1996 for (unsigned int i = 0; i < xi.len; ++i)
1997 val[i] = ~xi.val[i];
1998 result.set_len (xi.len);
1999 return result;
2002 /* Return -x. */
2003 template <typename T>
2004 inline WI_UNARY_RESULT (T)
2005 wi::neg (const T &x)
2007 return sub (0, x);
2010 /* Return -x. Indicate in *OVERFLOW if X is the minimum signed value. */
2011 template <typename T>
2012 inline WI_UNARY_RESULT (T)
2013 wi::neg (const T &x, bool *overflow)
2015 *overflow = only_sign_bit_p (x);
2016 return sub (0, x);
2019 /* Return the absolute value of x. */
2020 template <typename T>
2021 inline WI_UNARY_RESULT (T)
2022 wi::abs (const T &x)
2024 return neg_p (x) ? neg (x) : WI_UNARY_RESULT (T) (x);
2027 /* Return the result of sign-extending the low OFFSET bits of X. */
2028 template <typename T>
2029 inline WI_UNARY_RESULT (T)
2030 wi::sext (const T &x, unsigned int offset)
2032 WI_UNARY_RESULT_VAR (result, val, T, x);
2033 unsigned int precision = get_precision (result);
2034 WIDE_INT_REF_FOR (T) xi (x, precision);
2036 if (offset <= HOST_BITS_PER_WIDE_INT)
2038 val[0] = sext_hwi (xi.ulow (), offset);
2039 result.set_len (1, true);
2041 else
2042 result.set_len (sext_large (val, xi.val, xi.len, precision, offset));
2043 return result;
2046 /* Return the result of zero-extending the low OFFSET bits of X. */
2047 template <typename T>
2048 inline WI_UNARY_RESULT (T)
2049 wi::zext (const T &x, unsigned int offset)
2051 WI_UNARY_RESULT_VAR (result, val, T, x);
2052 unsigned int precision = get_precision (result);
2053 WIDE_INT_REF_FOR (T) xi (x, precision);
2055 /* This is not just an optimization, it is actually required to
2056 maintain canonization. */
2057 if (offset >= precision)
2059 wi::copy (result, xi);
2060 return result;
2063 /* In these cases we know that at least the top bit will be clear,
2064 so no sign extension is necessary. */
2065 if (offset < HOST_BITS_PER_WIDE_INT)
2067 val[0] = zext_hwi (xi.ulow (), offset);
2068 result.set_len (1, true);
2070 else
2071 result.set_len (zext_large (val, xi.val, xi.len, precision, offset), true);
2072 return result;
2075 /* Return the result of extending the low OFFSET bits of X according to
2076 signedness SGN. */
2077 template <typename T>
2078 inline WI_UNARY_RESULT (T)
2079 wi::ext (const T &x, unsigned int offset, signop sgn)
2081 return sgn == SIGNED ? sext (x, offset) : zext (x, offset);
2084 /* Return an integer that represents X | (1 << bit). */
2085 template <typename T>
2086 inline WI_UNARY_RESULT (T)
2087 wi::set_bit (const T &x, unsigned int bit)
2089 WI_UNARY_RESULT_VAR (result, val, T, x);
2090 unsigned int precision = get_precision (result);
2091 WIDE_INT_REF_FOR (T) xi (x, precision);
2092 if (precision <= HOST_BITS_PER_WIDE_INT)
2094 val[0] = xi.ulow () | ((unsigned HOST_WIDE_INT) 1 << bit);
2095 result.set_len (1);
2097 else
2098 result.set_len (set_bit_large (val, xi.val, xi.len, precision, bit));
2099 return result;
2102 /* Return the mininum of X and Y, treating them both as having
2103 signedness SGN. */
2104 template <typename T1, typename T2>
2105 inline WI_BINARY_RESULT (T1, T2)
2106 wi::min (const T1 &x, const T2 &y, signop sgn)
2108 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2109 unsigned int precision = get_precision (result);
2110 if (wi::le_p (x, y, sgn))
2111 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2112 else
2113 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2114 return result;
2117 /* Return the minimum of X and Y, treating both as signed values. */
2118 template <typename T1, typename T2>
2119 inline WI_BINARY_RESULT (T1, T2)
2120 wi::smin (const T1 &x, const T2 &y)
2122 return min (x, y, SIGNED);
2125 /* Return the minimum of X and Y, treating both as unsigned values. */
2126 template <typename T1, typename T2>
2127 inline WI_BINARY_RESULT (T1, T2)
2128 wi::umin (const T1 &x, const T2 &y)
2130 return min (x, y, UNSIGNED);
2133 /* Return the maxinum of X and Y, treating them both as having
2134 signedness SGN. */
2135 template <typename T1, typename T2>
2136 inline WI_BINARY_RESULT (T1, T2)
2137 wi::max (const T1 &x, const T2 &y, signop sgn)
2139 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2140 unsigned int precision = get_precision (result);
2141 if (wi::ge_p (x, y, sgn))
2142 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2143 else
2144 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2145 return result;
2148 /* Return the maximum of X and Y, treating both as signed values. */
2149 template <typename T1, typename T2>
2150 inline WI_BINARY_RESULT (T1, T2)
2151 wi::smax (const T1 &x, const T2 &y)
2153 return max (x, y, SIGNED);
2156 /* Return the maximum of X and Y, treating both as unsigned values. */
2157 template <typename T1, typename T2>
2158 inline WI_BINARY_RESULT (T1, T2)
2159 wi::umax (const T1 &x, const T2 &y)
2161 return max (x, y, UNSIGNED);
2164 /* Return X & Y. */
2165 template <typename T1, typename T2>
2166 inline WI_BINARY_RESULT (T1, T2)
2167 wi::bit_and (const T1 &x, const T2 &y)
2169 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2170 unsigned int precision = get_precision (result);
2171 WIDE_INT_REF_FOR (T1) xi (x, precision);
2172 WIDE_INT_REF_FOR (T2) yi (y, precision);
2173 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2174 if (__builtin_expect (xi.len + yi.len == 2, true))
2176 val[0] = xi.ulow () & yi.ulow ();
2177 result.set_len (1, is_sign_extended);
2179 else
2180 result.set_len (and_large (val, xi.val, xi.len, yi.val, yi.len,
2181 precision), is_sign_extended);
2182 return result;
2185 /* Return X & ~Y. */
2186 template <typename T1, typename T2>
2187 inline WI_BINARY_RESULT (T1, T2)
2188 wi::bit_and_not (const T1 &x, const T2 &y)
2190 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2191 unsigned int precision = get_precision (result);
2192 WIDE_INT_REF_FOR (T1) xi (x, precision);
2193 WIDE_INT_REF_FOR (T2) yi (y, precision);
2194 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2195 if (__builtin_expect (xi.len + yi.len == 2, true))
2197 val[0] = xi.ulow () & ~yi.ulow ();
2198 result.set_len (1, is_sign_extended);
2200 else
2201 result.set_len (and_not_large (val, xi.val, xi.len, yi.val, yi.len,
2202 precision), is_sign_extended);
2203 return result;
2206 /* Return X | Y. */
2207 template <typename T1, typename T2>
2208 inline WI_BINARY_RESULT (T1, T2)
2209 wi::bit_or (const T1 &x, const T2 &y)
2211 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2212 unsigned int precision = get_precision (result);
2213 WIDE_INT_REF_FOR (T1) xi (x, precision);
2214 WIDE_INT_REF_FOR (T2) yi (y, precision);
2215 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2216 if (__builtin_expect (xi.len + yi.len == 2, true))
2218 val[0] = xi.ulow () | yi.ulow ();
2219 result.set_len (1, is_sign_extended);
2221 else
2222 result.set_len (or_large (val, xi.val, xi.len,
2223 yi.val, yi.len, precision), is_sign_extended);
2224 return result;
2227 /* Return X | ~Y. */
2228 template <typename T1, typename T2>
2229 inline WI_BINARY_RESULT (T1, T2)
2230 wi::bit_or_not (const T1 &x, const T2 &y)
2232 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2233 unsigned int precision = get_precision (result);
2234 WIDE_INT_REF_FOR (T1) xi (x, precision);
2235 WIDE_INT_REF_FOR (T2) yi (y, precision);
2236 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2237 if (__builtin_expect (xi.len + yi.len == 2, true))
2239 val[0] = xi.ulow () | ~yi.ulow ();
2240 result.set_len (1, is_sign_extended);
2242 else
2243 result.set_len (or_not_large (val, xi.val, xi.len, yi.val, yi.len,
2244 precision), is_sign_extended);
2245 return result;
2248 /* Return X ^ Y. */
2249 template <typename T1, typename T2>
2250 inline WI_BINARY_RESULT (T1, T2)
2251 wi::bit_xor (const T1 &x, const T2 &y)
2253 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2254 unsigned int precision = get_precision (result);
2255 WIDE_INT_REF_FOR (T1) xi (x, precision);
2256 WIDE_INT_REF_FOR (T2) yi (y, precision);
2257 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2258 if (__builtin_expect (xi.len + yi.len == 2, true))
2260 val[0] = xi.ulow () ^ yi.ulow ();
2261 result.set_len (1, is_sign_extended);
2263 else
2264 result.set_len (xor_large (val, xi.val, xi.len,
2265 yi.val, yi.len, precision), is_sign_extended);
2266 return result;
2269 /* Return X + Y. */
2270 template <typename T1, typename T2>
2271 inline WI_BINARY_RESULT (T1, T2)
2272 wi::add (const T1 &x, const T2 &y)
2274 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2275 unsigned int precision = get_precision (result);
2276 WIDE_INT_REF_FOR (T1) xi (x, precision);
2277 WIDE_INT_REF_FOR (T2) yi (y, precision);
2278 if (precision <= HOST_BITS_PER_WIDE_INT)
2280 val[0] = xi.ulow () + yi.ulow ();
2281 result.set_len (1);
2283 /* If the precision is known at compile time to be greater than
2284 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2285 knowing that (a) all bits in those HWIs are significant and
2286 (b) the result has room for at least two HWIs. This provides
2287 a fast path for things like offset_int and widest_int.
2289 The STATIC_CONSTANT_P test prevents this path from being
2290 used for wide_ints. wide_ints with precisions greater than
2291 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2292 point handling them inline. */
2293 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2294 && __builtin_expect (xi.len + yi.len == 2, true))
2296 unsigned HOST_WIDE_INT xl = xi.ulow ();
2297 unsigned HOST_WIDE_INT yl = yi.ulow ();
2298 unsigned HOST_WIDE_INT resultl = xl + yl;
2299 val[0] = resultl;
2300 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2301 result.set_len (1 + (((resultl ^ xl) & (resultl ^ yl))
2302 >> (HOST_BITS_PER_WIDE_INT - 1)));
2304 else
2305 result.set_len (add_large (val, xi.val, xi.len,
2306 yi.val, yi.len, precision,
2307 UNSIGNED, 0));
2308 return result;
2311 /* Return X + Y. Treat X and Y as having the signednes given by SGN
2312 and indicate in *OVERFLOW whether the operation overflowed. */
2313 template <typename T1, typename T2>
2314 inline WI_BINARY_RESULT (T1, T2)
2315 wi::add (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2317 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2318 unsigned int precision = get_precision (result);
2319 WIDE_INT_REF_FOR (T1) xi (x, precision);
2320 WIDE_INT_REF_FOR (T2) yi (y, precision);
2321 if (precision <= HOST_BITS_PER_WIDE_INT)
2323 unsigned HOST_WIDE_INT xl = xi.ulow ();
2324 unsigned HOST_WIDE_INT yl = yi.ulow ();
2325 unsigned HOST_WIDE_INT resultl = xl + yl;
2326 if (sgn == SIGNED)
2327 *overflow = (((resultl ^ xl) & (resultl ^ yl))
2328 >> (precision - 1)) & 1;
2329 else
2330 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2331 < (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2332 val[0] = resultl;
2333 result.set_len (1);
2335 else
2336 result.set_len (add_large (val, xi.val, xi.len,
2337 yi.val, yi.len, precision,
2338 sgn, overflow));
2339 return result;
2342 /* Return X - Y. */
2343 template <typename T1, typename T2>
2344 inline WI_BINARY_RESULT (T1, T2)
2345 wi::sub (const T1 &x, const T2 &y)
2347 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2348 unsigned int precision = get_precision (result);
2349 WIDE_INT_REF_FOR (T1) xi (x, precision);
2350 WIDE_INT_REF_FOR (T2) yi (y, precision);
2351 if (precision <= HOST_BITS_PER_WIDE_INT)
2353 val[0] = xi.ulow () - yi.ulow ();
2354 result.set_len (1);
2356 /* If the precision is known at compile time to be greater than
2357 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2358 knowing that (a) all bits in those HWIs are significant and
2359 (b) the result has room for at least two HWIs. This provides
2360 a fast path for things like offset_int and widest_int.
2362 The STATIC_CONSTANT_P test prevents this path from being
2363 used for wide_ints. wide_ints with precisions greater than
2364 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2365 point handling them inline. */
2366 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2367 && __builtin_expect (xi.len + yi.len == 2, true))
2369 unsigned HOST_WIDE_INT xl = xi.ulow ();
2370 unsigned HOST_WIDE_INT yl = yi.ulow ();
2371 unsigned HOST_WIDE_INT resultl = xl - yl;
2372 val[0] = resultl;
2373 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2374 result.set_len (1 + (((resultl ^ xl) & (xl ^ yl))
2375 >> (HOST_BITS_PER_WIDE_INT - 1)));
2377 else
2378 result.set_len (sub_large (val, xi.val, xi.len,
2379 yi.val, yi.len, precision,
2380 UNSIGNED, 0));
2381 return result;
2384 /* Return X - Y. Treat X and Y as having the signednes given by SGN
2385 and indicate in *OVERFLOW whether the operation overflowed. */
2386 template <typename T1, typename T2>
2387 inline WI_BINARY_RESULT (T1, T2)
2388 wi::sub (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2390 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2391 unsigned int precision = get_precision (result);
2392 WIDE_INT_REF_FOR (T1) xi (x, precision);
2393 WIDE_INT_REF_FOR (T2) yi (y, precision);
2394 if (precision <= HOST_BITS_PER_WIDE_INT)
2396 unsigned HOST_WIDE_INT xl = xi.ulow ();
2397 unsigned HOST_WIDE_INT yl = yi.ulow ();
2398 unsigned HOST_WIDE_INT resultl = xl - yl;
2399 if (sgn == SIGNED)
2400 *overflow = (((xl ^ yl) & (resultl ^ xl)) >> (precision - 1)) & 1;
2401 else
2402 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2403 > (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2404 val[0] = resultl;
2405 result.set_len (1);
2407 else
2408 result.set_len (sub_large (val, xi.val, xi.len,
2409 yi.val, yi.len, precision,
2410 sgn, overflow));
2411 return result;
2414 /* Return X * Y. */
2415 template <typename T1, typename T2>
2416 inline WI_BINARY_RESULT (T1, T2)
2417 wi::mul (const T1 &x, const T2 &y)
2419 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2420 unsigned int precision = get_precision (result);
2421 WIDE_INT_REF_FOR (T1) xi (x, precision);
2422 WIDE_INT_REF_FOR (T2) yi (y, precision);
2423 if (precision <= HOST_BITS_PER_WIDE_INT)
2425 val[0] = xi.ulow () * yi.ulow ();
2426 result.set_len (1);
2428 else
2429 result.set_len (mul_internal (val, xi.val, xi.len, yi.val, yi.len,
2430 precision, UNSIGNED, 0, false));
2431 return result;
2434 /* Return X * Y. Treat X and Y as having the signednes given by SGN
2435 and indicate in *OVERFLOW whether the operation overflowed. */
2436 template <typename T1, typename T2>
2437 inline WI_BINARY_RESULT (T1, T2)
2438 wi::mul (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2440 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2441 unsigned int precision = get_precision (result);
2442 WIDE_INT_REF_FOR (T1) xi (x, precision);
2443 WIDE_INT_REF_FOR (T2) yi (y, precision);
2444 result.set_len (mul_internal (val, xi.val, xi.len,
2445 yi.val, yi.len, precision,
2446 sgn, overflow, false));
2447 return result;
2450 /* Return X * Y, treating both X and Y as signed values. Indicate in
2451 *OVERFLOW whether the operation overflowed. */
2452 template <typename T1, typename T2>
2453 inline WI_BINARY_RESULT (T1, T2)
2454 wi::smul (const T1 &x, const T2 &y, bool *overflow)
2456 return mul (x, y, SIGNED, overflow);
2459 /* Return X * Y, treating both X and Y as unsigned values. Indicate in
2460 *OVERFLOW whether the operation overflowed. */
2461 template <typename T1, typename T2>
2462 inline WI_BINARY_RESULT (T1, T2)
2463 wi::umul (const T1 &x, const T2 &y, bool *overflow)
2465 return mul (x, y, UNSIGNED, overflow);
2468 /* Perform a widening multiplication of X and Y, extending the values
2469 according to SGN, and return the high part of the result. */
2470 template <typename T1, typename T2>
2471 inline WI_BINARY_RESULT (T1, T2)
2472 wi::mul_high (const T1 &x, const T2 &y, signop sgn)
2474 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2475 unsigned int precision = get_precision (result);
2476 WIDE_INT_REF_FOR (T1) xi (x, precision);
2477 WIDE_INT_REF_FOR (T2) yi (y, precision);
2478 result.set_len (mul_internal (val, xi.val, xi.len,
2479 yi.val, yi.len, precision,
2480 sgn, 0, true));
2481 return result;
2484 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2485 signedness given by SGN. Indicate in *OVERFLOW if the result
2486 overflows. */
2487 template <typename T1, typename T2>
2488 inline WI_BINARY_RESULT (T1, T2)
2489 wi::div_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2491 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2492 unsigned int precision = get_precision (quotient);
2493 WIDE_INT_REF_FOR (T1) xi (x, precision);
2494 WIDE_INT_REF_FOR (T2) yi (y);
2496 quotient.set_len (divmod_internal (quotient_val, 0, 0, xi.val, xi.len,
2497 precision,
2498 yi.val, yi.len, yi.precision,
2499 sgn, overflow));
2500 return quotient;
2503 /* Return X / Y, rouding towards 0. Treat X and Y as signed values. */
2504 template <typename T1, typename T2>
2505 inline WI_BINARY_RESULT (T1, T2)
2506 wi::sdiv_trunc (const T1 &x, const T2 &y)
2508 return div_trunc (x, y, SIGNED);
2511 /* Return X / Y, rouding towards 0. Treat X and Y as unsigned values. */
2512 template <typename T1, typename T2>
2513 inline WI_BINARY_RESULT (T1, T2)
2514 wi::udiv_trunc (const T1 &x, const T2 &y)
2516 return div_trunc (x, y, UNSIGNED);
2519 /* Return X / Y, rouding towards -inf. Treat X and Y as having the
2520 signedness given by SGN. Indicate in *OVERFLOW if the result
2521 overflows. */
2522 template <typename T1, typename T2>
2523 inline WI_BINARY_RESULT (T1, T2)
2524 wi::div_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2526 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2527 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2528 unsigned int precision = get_precision (quotient);
2529 WIDE_INT_REF_FOR (T1) xi (x, precision);
2530 WIDE_INT_REF_FOR (T2) yi (y);
2532 unsigned int remainder_len;
2533 quotient.set_len (divmod_internal (quotient_val,
2534 &remainder_len, remainder_val,
2535 xi.val, xi.len, precision,
2536 yi.val, yi.len, yi.precision, sgn,
2537 overflow));
2538 remainder.set_len (remainder_len);
2539 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2540 return quotient - 1;
2541 return quotient;
2544 /* Return X / Y, rouding towards -inf. Treat X and Y as signed values. */
2545 template <typename T1, typename T2>
2546 inline WI_BINARY_RESULT (T1, T2)
2547 wi::sdiv_floor (const T1 &x, const T2 &y)
2549 return div_floor (x, y, SIGNED);
2552 /* Return X / Y, rouding towards -inf. Treat X and Y as unsigned values. */
2553 /* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
2554 template <typename T1, typename T2>
2555 inline WI_BINARY_RESULT (T1, T2)
2556 wi::udiv_floor (const T1 &x, const T2 &y)
2558 return div_floor (x, y, UNSIGNED);
2561 /* Return X / Y, rouding towards +inf. Treat X and Y as having the
2562 signedness given by SGN. Indicate in *OVERFLOW if the result
2563 overflows. */
2564 template <typename T1, typename T2>
2565 inline WI_BINARY_RESULT (T1, T2)
2566 wi::div_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2568 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2569 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2570 unsigned int precision = get_precision (quotient);
2571 WIDE_INT_REF_FOR (T1) xi (x, precision);
2572 WIDE_INT_REF_FOR (T2) yi (y);
2574 unsigned int remainder_len;
2575 quotient.set_len (divmod_internal (quotient_val,
2576 &remainder_len, remainder_val,
2577 xi.val, xi.len, precision,
2578 yi.val, yi.len, yi.precision, sgn,
2579 overflow));
2580 remainder.set_len (remainder_len);
2581 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2582 return quotient + 1;
2583 return quotient;
2586 /* Return X / Y, rouding towards nearest with ties away from zero.
2587 Treat X and Y as having the signedness given by SGN. Indicate
2588 in *OVERFLOW if the result overflows. */
2589 template <typename T1, typename T2>
2590 inline WI_BINARY_RESULT (T1, T2)
2591 wi::div_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2593 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2594 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2595 unsigned int precision = get_precision (quotient);
2596 WIDE_INT_REF_FOR (T1) xi (x, precision);
2597 WIDE_INT_REF_FOR (T2) yi (y);
2599 unsigned int remainder_len;
2600 quotient.set_len (divmod_internal (quotient_val,
2601 &remainder_len, remainder_val,
2602 xi.val, xi.len, precision,
2603 yi.val, yi.len, yi.precision, sgn,
2604 overflow));
2605 remainder.set_len (remainder_len);
2607 if (remainder != 0)
2609 if (sgn == SIGNED)
2611 if (wi::ges_p (wi::abs (remainder),
2612 wi::lrshift (wi::abs (y), 1)))
2614 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2615 return quotient - 1;
2616 else
2617 return quotient + 1;
2620 else
2622 if (wi::geu_p (remainder, wi::lrshift (y, 1)))
2623 return quotient + 1;
2626 return quotient;
2629 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2630 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
2631 template <typename T1, typename T2>
2632 inline WI_BINARY_RESULT (T1, T2)
2633 wi::divmod_trunc (const T1 &x, const T2 &y, signop sgn,
2634 WI_BINARY_RESULT (T1, T2) *remainder_ptr)
2636 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2637 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2638 unsigned int precision = get_precision (quotient);
2639 WIDE_INT_REF_FOR (T1) xi (x, precision);
2640 WIDE_INT_REF_FOR (T2) yi (y);
2642 unsigned int remainder_len;
2643 quotient.set_len (divmod_internal (quotient_val,
2644 &remainder_len, remainder_val,
2645 xi.val, xi.len, precision,
2646 yi.val, yi.len, yi.precision, sgn, 0));
2647 remainder.set_len (remainder_len);
2649 *remainder_ptr = remainder;
2650 return quotient;
2653 /* Compute X / Y, rouding towards 0, and return the remainder.
2654 Treat X and Y as having the signedness given by SGN. Indicate
2655 in *OVERFLOW if the division overflows. */
2656 template <typename T1, typename T2>
2657 inline WI_BINARY_RESULT (T1, T2)
2658 wi::mod_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2660 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2661 unsigned int precision = get_precision (remainder);
2662 WIDE_INT_REF_FOR (T1) xi (x, precision);
2663 WIDE_INT_REF_FOR (T2) yi (y);
2665 unsigned int remainder_len;
2666 divmod_internal (0, &remainder_len, remainder_val,
2667 xi.val, xi.len, precision,
2668 yi.val, yi.len, yi.precision, sgn, overflow);
2669 remainder.set_len (remainder_len);
2671 return remainder;
2674 /* Compute X / Y, rouding towards 0, and return the remainder.
2675 Treat X and Y as signed values. */
2676 template <typename T1, typename T2>
2677 inline WI_BINARY_RESULT (T1, T2)
2678 wi::smod_trunc (const T1 &x, const T2 &y)
2680 return mod_trunc (x, y, SIGNED);
2683 /* Compute X / Y, rouding towards 0, and return the remainder.
2684 Treat X and Y as unsigned values. */
2685 template <typename T1, typename T2>
2686 inline WI_BINARY_RESULT (T1, T2)
2687 wi::umod_trunc (const T1 &x, const T2 &y)
2689 return mod_trunc (x, y, UNSIGNED);
2692 /* Compute X / Y, rouding towards -inf, and return the remainder.
2693 Treat X and Y as having the signedness given by SGN. Indicate
2694 in *OVERFLOW if the division overflows. */
2695 template <typename T1, typename T2>
2696 inline WI_BINARY_RESULT (T1, T2)
2697 wi::mod_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2699 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2700 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2701 unsigned int precision = get_precision (quotient);
2702 WIDE_INT_REF_FOR (T1) xi (x, precision);
2703 WIDE_INT_REF_FOR (T2) yi (y);
2705 unsigned int remainder_len;
2706 quotient.set_len (divmod_internal (quotient_val,
2707 &remainder_len, remainder_val,
2708 xi.val, xi.len, precision,
2709 yi.val, yi.len, yi.precision, sgn,
2710 overflow));
2711 remainder.set_len (remainder_len);
2713 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2714 return remainder + y;
2715 return remainder;
2718 /* Compute X / Y, rouding towards -inf, and return the remainder.
2719 Treat X and Y as unsigned values. */
2720 /* ??? Why do we have both this and umod_trunc. Aren't they the same? */
2721 template <typename T1, typename T2>
2722 inline WI_BINARY_RESULT (T1, T2)
2723 wi::umod_floor (const T1 &x, const T2 &y)
2725 return mod_floor (x, y, UNSIGNED);
2728 /* Compute X / Y, rouding towards +inf, and return the remainder.
2729 Treat X and Y as having the signedness given by SGN. Indicate
2730 in *OVERFLOW if the division overflows. */
2731 template <typename T1, typename T2>
2732 inline WI_BINARY_RESULT (T1, T2)
2733 wi::mod_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2735 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2736 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2737 unsigned int precision = get_precision (quotient);
2738 WIDE_INT_REF_FOR (T1) xi (x, precision);
2739 WIDE_INT_REF_FOR (T2) yi (y);
2741 unsigned int remainder_len;
2742 quotient.set_len (divmod_internal (quotient_val,
2743 &remainder_len, remainder_val,
2744 xi.val, xi.len, precision,
2745 yi.val, yi.len, yi.precision, sgn,
2746 overflow));
2747 remainder.set_len (remainder_len);
2749 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2750 return remainder - y;
2751 return remainder;
2754 /* Compute X / Y, rouding towards nearest with ties away from zero,
2755 and return the remainder. Treat X and Y as having the signedness
2756 given by SGN. Indicate in *OVERFLOW if the division overflows. */
2757 template <typename T1, typename T2>
2758 inline WI_BINARY_RESULT (T1, T2)
2759 wi::mod_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2761 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2762 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2763 unsigned int precision = get_precision (quotient);
2764 WIDE_INT_REF_FOR (T1) xi (x, precision);
2765 WIDE_INT_REF_FOR (T2) yi (y);
2767 unsigned int remainder_len;
2768 quotient.set_len (divmod_internal (quotient_val,
2769 &remainder_len, remainder_val,
2770 xi.val, xi.len, precision,
2771 yi.val, yi.len, yi.precision, sgn,
2772 overflow));
2773 remainder.set_len (remainder_len);
2775 if (remainder != 0)
2777 if (sgn == SIGNED)
2779 if (wi::ges_p (wi::abs (remainder),
2780 wi::lrshift (wi::abs (y), 1)))
2782 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2783 return remainder + y;
2784 else
2785 return remainder - y;
2788 else
2790 if (wi::geu_p (remainder, wi::lrshift (y, 1)))
2791 return remainder - y;
2794 return remainder;
2797 /* Return true if X is a multiple of Y. Treat X and Y as having the
2798 signedness given by SGN. */
2799 template <typename T1, typename T2>
2800 inline bool
2801 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn)
2803 return wi::mod_trunc (x, y, sgn) == 0;
2806 /* Return true if X is a multiple of Y, storing X / Y in *RES if so.
2807 Treat X and Y as having the signedness given by SGN. */
2808 template <typename T1, typename T2>
2809 inline bool
2810 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn,
2811 WI_BINARY_RESULT (T1, T2) *res)
2813 WI_BINARY_RESULT (T1, T2) remainder;
2814 WI_BINARY_RESULT (T1, T2) quotient
2815 = divmod_trunc (x, y, sgn, &remainder);
2816 if (remainder == 0)
2818 *res = quotient;
2819 return true;
2821 return false;
2824 /* Return X << Y. Return 0 if Y is greater than or equal to
2825 the precision of X. */
2826 template <typename T1, typename T2>
2827 inline WI_UNARY_RESULT (T1)
2828 wi::lshift (const T1 &x, const T2 &y)
2830 WI_UNARY_RESULT_VAR (result, val, T1, x);
2831 unsigned int precision = get_precision (result);
2832 WIDE_INT_REF_FOR (T1) xi (x, precision);
2833 WIDE_INT_REF_FOR (T2) yi (y);
2834 /* Handle the simple cases quickly. */
2835 if (geu_p (yi, precision))
2837 val[0] = 0;
2838 result.set_len (1);
2840 else
2842 unsigned int shift = yi.to_uhwi ();
2843 /* For fixed-precision integers like offset_int and widest_int,
2844 handle the case where the shift value is constant and the
2845 result is a single nonnegative HWI (meaning that we don't
2846 need to worry about val[1]). This is particularly common
2847 for converting a byte count to a bit count.
2849 For variable-precision integers like wide_int, handle HWI
2850 and sub-HWI integers inline. */
2851 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2852 ? (STATIC_CONSTANT_P (shift < HOST_BITS_PER_WIDE_INT - 1)
2853 && xi.len == 1
2854 && xi.val[0] <= (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT)
2855 HOST_WIDE_INT_MAX >> shift))
2856 : precision <= HOST_BITS_PER_WIDE_INT)
2858 val[0] = xi.ulow () << shift;
2859 result.set_len (1);
2861 else
2862 result.set_len (lshift_large (val, xi.val, xi.len,
2863 precision, shift));
2865 return result;
2868 /* Return X >> Y, using a logical shift. Return 0 if Y is greater than
2869 or equal to the precision of X. */
2870 template <typename T1, typename T2>
2871 inline WI_UNARY_RESULT (T1)
2872 wi::lrshift (const T1 &x, const T2 &y)
2874 WI_UNARY_RESULT_VAR (result, val, T1, x);
2875 /* Do things in the precision of the input rather than the output,
2876 since the result can be no larger than that. */
2877 WIDE_INT_REF_FOR (T1) xi (x);
2878 WIDE_INT_REF_FOR (T2) yi (y);
2879 /* Handle the simple cases quickly. */
2880 if (geu_p (yi, xi.precision))
2882 val[0] = 0;
2883 result.set_len (1);
2885 else
2887 unsigned int shift = yi.to_uhwi ();
2888 /* For fixed-precision integers like offset_int and widest_int,
2889 handle the case where the shift value is constant and the
2890 shifted value is a single nonnegative HWI (meaning that all
2891 bits above the HWI are zero). This is particularly common
2892 for converting a bit count to a byte count.
2894 For variable-precision integers like wide_int, handle HWI
2895 and sub-HWI integers inline. */
2896 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2897 ? xi.len == 1 && xi.val[0] >= 0
2898 : xi.precision <= HOST_BITS_PER_WIDE_INT)
2900 val[0] = xi.to_uhwi () >> shift;
2901 result.set_len (1);
2903 else
2904 result.set_len (lrshift_large (val, xi.val, xi.len, xi.precision,
2905 get_precision (result), shift));
2907 return result;
2910 /* Return X >> Y, using an arithmetic shift. Return a sign mask if
2911 Y is greater than or equal to the precision of X. */
2912 template <typename T1, typename T2>
2913 inline WI_UNARY_RESULT (T1)
2914 wi::arshift (const T1 &x, const T2 &y)
2916 WI_UNARY_RESULT_VAR (result, val, T1, x);
2917 /* Do things in the precision of the input rather than the output,
2918 since the result can be no larger than that. */
2919 WIDE_INT_REF_FOR (T1) xi (x);
2920 WIDE_INT_REF_FOR (T2) yi (y);
2921 /* Handle the simple cases quickly. */
2922 if (geu_p (yi, xi.precision))
2924 val[0] = sign_mask (x);
2925 result.set_len (1);
2927 else
2929 unsigned int shift = yi.to_uhwi ();
2930 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
2932 val[0] = sext_hwi (xi.ulow () >> shift, xi.precision - shift);
2933 result.set_len (1, true);
2935 else
2936 result.set_len (arshift_large (val, xi.val, xi.len, xi.precision,
2937 get_precision (result), shift));
2939 return result;
2942 /* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
2943 logical shift otherwise. */
2944 template <typename T1, typename T2>
2945 inline WI_UNARY_RESULT (T1)
2946 wi::rshift (const T1 &x, const T2 &y, signop sgn)
2948 if (sgn == UNSIGNED)
2949 return lrshift (x, y);
2950 else
2951 return arshift (x, y);
2954 /* Return the result of rotating the low WIDTH bits of X left by Y
2955 bits and zero-extending the result. Use a full-width rotate if
2956 WIDTH is zero. */
2957 template <typename T1, typename T2>
2958 WI_UNARY_RESULT (T1)
2959 wi::lrotate (const T1 &x, const T2 &y, unsigned int width)
2961 unsigned int precision = get_binary_precision (x, x);
2962 if (width == 0)
2963 width = precision;
2964 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
2965 WI_UNARY_RESULT (T1) left = wi::lshift (x, ymod);
2966 WI_UNARY_RESULT (T1) right = wi::lrshift (x, wi::sub (width, ymod));
2967 if (width != precision)
2968 return wi::zext (left, width) | wi::zext (right, width);
2969 return left | right;
2972 /* Return the result of rotating the low WIDTH bits of X right by Y
2973 bits and zero-extending the result. Use a full-width rotate if
2974 WIDTH is zero. */
2975 template <typename T1, typename T2>
2976 WI_UNARY_RESULT (T1)
2977 wi::rrotate (const T1 &x, const T2 &y, unsigned int width)
2979 unsigned int precision = get_binary_precision (x, x);
2980 if (width == 0)
2981 width = precision;
2982 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
2983 WI_UNARY_RESULT (T1) right = wi::lrshift (x, ymod);
2984 WI_UNARY_RESULT (T1) left = wi::lshift (x, wi::sub (width, ymod));
2985 if (width != precision)
2986 return wi::zext (left, width) | wi::zext (right, width);
2987 return left | right;
2990 /* Return 0 if the number of 1s in X is even and 1 if the number of 1s
2991 is odd. */
2992 inline int
2993 wi::parity (const wide_int_ref &x)
2995 return popcount (x) & 1;
2998 /* Extract WIDTH bits from X, starting at BITPOS. */
2999 template <typename T>
3000 inline unsigned HOST_WIDE_INT
3001 wi::extract_uhwi (const T &x, unsigned int bitpos, unsigned int width)
3003 unsigned precision = get_precision (x);
3004 if (precision < bitpos + width)
3005 precision = bitpos + width;
3006 WIDE_INT_REF_FOR (T) xi (x, precision);
3008 /* Handle this rare case after the above, so that we assert about
3009 bogus BITPOS values. */
3010 if (width == 0)
3011 return 0;
3013 unsigned int start = bitpos / HOST_BITS_PER_WIDE_INT;
3014 unsigned int shift = bitpos % HOST_BITS_PER_WIDE_INT;
3015 unsigned HOST_WIDE_INT res = xi.elt (start);
3016 res >>= shift;
3017 if (shift + width > HOST_BITS_PER_WIDE_INT)
3019 unsigned HOST_WIDE_INT upper = xi.elt (start + 1);
3020 res |= upper << (-shift % HOST_BITS_PER_WIDE_INT);
3022 return zext_hwi (res, width);
3025 /* Return the minimum precision needed to store X with sign SGN. */
3026 template <typename T>
3027 inline unsigned int
3028 wi::min_precision (const T &x, signop sgn)
3030 if (sgn == SIGNED)
3031 return get_precision (x) - clrsb (x);
3032 else
3033 return get_precision (x) - clz (x);
3036 template<typename T>
3037 void
3038 gt_ggc_mx (generic_wide_int <T> *)
3042 template<typename T>
3043 void
3044 gt_pch_nx (generic_wide_int <T> *)
3048 template<typename T>
3049 void
3050 gt_pch_nx (generic_wide_int <T> *, void (*) (void *, void *), void *)
3054 template<int N>
3055 void
3056 gt_ggc_mx (trailing_wide_ints <N> *)
3060 template<int N>
3061 void
3062 gt_pch_nx (trailing_wide_ints <N> *)
3066 template<int N>
3067 void
3068 gt_pch_nx (trailing_wide_ints <N> *, void (*) (void *, void *), void *)
3072 namespace wi
3074 /* Used for overloaded functions in which the only other acceptable
3075 scalar type is a pointer. It stops a plain 0 from being treated
3076 as a null pointer. */
3077 struct never_used1 {};
3078 struct never_used2 {};
3080 wide_int min_value (unsigned int, signop);
3081 wide_int min_value (never_used1 *);
3082 wide_int min_value (never_used2 *);
3083 wide_int max_value (unsigned int, signop);
3084 wide_int max_value (never_used1 *);
3085 wide_int max_value (never_used2 *);
3087 /* FIXME: this is target dependent, so should be elsewhere.
3088 It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
3089 wide_int from_buffer (const unsigned char *, unsigned int);
3091 #ifndef GENERATOR_FILE
3092 void to_mpz (const wide_int_ref &, mpz_t, signop);
3093 #endif
3095 wide_int mask (unsigned int, bool, unsigned int);
3096 wide_int shifted_mask (unsigned int, unsigned int, bool, unsigned int);
3097 wide_int set_bit_in_zero (unsigned int, unsigned int);
3098 wide_int insert (const wide_int &x, const wide_int &y, unsigned int,
3099 unsigned int);
3101 template <typename T>
3102 T mask (unsigned int, bool);
3104 template <typename T>
3105 T shifted_mask (unsigned int, unsigned int, bool);
3107 template <typename T>
3108 T set_bit_in_zero (unsigned int);
3110 unsigned int mask (HOST_WIDE_INT *, unsigned int, bool, unsigned int);
3111 unsigned int shifted_mask (HOST_WIDE_INT *, unsigned int, unsigned int,
3112 bool, unsigned int);
3113 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
3114 unsigned int, unsigned int, bool);
3117 /* Return a PRECISION-bit integer in which the low WIDTH bits are set
3118 and the other bits are clear, or the inverse if NEGATE_P. */
3119 inline wide_int
3120 wi::mask (unsigned int width, bool negate_p, unsigned int precision)
3122 wide_int result = wide_int::create (precision);
3123 result.set_len (mask (result.write_val (), width, negate_p, precision));
3124 return result;
3127 /* Return a PRECISION-bit integer in which the low START bits are clear,
3128 the next WIDTH bits are set, and the other bits are clear,
3129 or the inverse if NEGATE_P. */
3130 inline wide_int
3131 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p,
3132 unsigned int precision)
3134 wide_int result = wide_int::create (precision);
3135 result.set_len (shifted_mask (result.write_val (), start, width, negate_p,
3136 precision));
3137 return result;
3140 /* Return a PRECISION-bit integer in which bit BIT is set and all the
3141 others are clear. */
3142 inline wide_int
3143 wi::set_bit_in_zero (unsigned int bit, unsigned int precision)
3145 return shifted_mask (bit, 1, false, precision);
3148 /* Return an integer of type T in which the low WIDTH bits are set
3149 and the other bits are clear, or the inverse if NEGATE_P. */
3150 template <typename T>
3151 inline T
3152 wi::mask (unsigned int width, bool negate_p)
3154 STATIC_ASSERT (wi::int_traits<T>::precision);
3155 T result;
3156 result.set_len (mask (result.write_val (), width, negate_p,
3157 wi::int_traits <T>::precision));
3158 return result;
3161 /* Return an integer of type T in which the low START bits are clear,
3162 the next WIDTH bits are set, and the other bits are clear, or the
3163 inverse if NEGATE_P. */
3164 template <typename T>
3165 inline T
3166 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p)
3168 STATIC_ASSERT (wi::int_traits<T>::precision);
3169 T result;
3170 result.set_len (shifted_mask (result.write_val (), start, width,
3171 negate_p,
3172 wi::int_traits <T>::precision));
3173 return result;
3176 /* Return an integer of type T in which bit BIT is set and all the
3177 others are clear. */
3178 template <typename T>
3179 inline T
3180 wi::set_bit_in_zero (unsigned int bit)
3182 return shifted_mask <T> (bit, 1, false);
3185 #endif /* WIDE_INT_H */