2014-07-29 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / wide-int.h
blobf1081162436c418e74a92ffd1a5ec9ac3dd62467
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 class 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 <long>
1450 : public primitive_int_traits <long, true> {};
1452 template <>
1453 struct int_traits <unsigned long>
1454 : public primitive_int_traits <unsigned long, false> {};
1456 #if defined HAVE_LONG_LONG
1457 template <>
1458 struct int_traits <long long>
1459 : public primitive_int_traits <long long, true> {};
1461 template <>
1462 struct int_traits <unsigned long long>
1463 : public primitive_int_traits <unsigned long long, false> {};
1464 #endif
1467 namespace wi
1469 /* Stores HWI-sized integer VAL, treating it as having signedness SGN
1470 and precision PRECISION. */
1471 struct hwi_with_prec
1473 hwi_with_prec (HOST_WIDE_INT, unsigned int, signop);
1474 HOST_WIDE_INT val;
1475 unsigned int precision;
1476 signop sgn;
1479 hwi_with_prec shwi (HOST_WIDE_INT, unsigned int);
1480 hwi_with_prec uhwi (unsigned HOST_WIDE_INT, unsigned int);
1482 hwi_with_prec minus_one (unsigned int);
1483 hwi_with_prec zero (unsigned int);
1484 hwi_with_prec one (unsigned int);
1485 hwi_with_prec two (unsigned int);
1488 inline wi::hwi_with_prec::hwi_with_prec (HOST_WIDE_INT v, unsigned int p,
1489 signop s)
1490 : val (v), precision (p), sgn (s)
1494 /* Return a signed integer that has value VAL and precision PRECISION. */
1495 inline wi::hwi_with_prec
1496 wi::shwi (HOST_WIDE_INT val, unsigned int precision)
1498 return hwi_with_prec (val, precision, SIGNED);
1501 /* Return an unsigned integer that has value VAL and precision PRECISION. */
1502 inline wi::hwi_with_prec
1503 wi::uhwi (unsigned HOST_WIDE_INT val, unsigned int precision)
1505 return hwi_with_prec (val, precision, UNSIGNED);
1508 /* Return a wide int of -1 with precision PRECISION. */
1509 inline wi::hwi_with_prec
1510 wi::minus_one (unsigned int precision)
1512 return wi::shwi (-1, precision);
1515 /* Return a wide int of 0 with precision PRECISION. */
1516 inline wi::hwi_with_prec
1517 wi::zero (unsigned int precision)
1519 return wi::shwi (0, precision);
1522 /* Return a wide int of 1 with precision PRECISION. */
1523 inline wi::hwi_with_prec
1524 wi::one (unsigned int precision)
1526 return wi::shwi (1, precision);
1529 /* Return a wide int of 2 with precision PRECISION. */
1530 inline wi::hwi_with_prec
1531 wi::two (unsigned int precision)
1533 return wi::shwi (2, precision);
1536 namespace wi
1538 template <>
1539 struct int_traits <wi::hwi_with_prec>
1541 static const enum precision_type precision_type = VAR_PRECISION;
1542 /* hwi_with_prec has an explicitly-given precision, rather than the
1543 precision of HOST_WIDE_INT. */
1544 static const bool host_dependent_precision = false;
1545 static const bool is_sign_extended = true;
1546 static unsigned int get_precision (const wi::hwi_with_prec &);
1547 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
1548 const wi::hwi_with_prec &);
1552 inline unsigned int
1553 wi::int_traits <wi::hwi_with_prec>::get_precision (const wi::hwi_with_prec &x)
1555 return x.precision;
1558 inline wi::storage_ref
1559 wi::int_traits <wi::hwi_with_prec>::
1560 decompose (HOST_WIDE_INT *scratch, unsigned int precision,
1561 const wi::hwi_with_prec &x)
1563 gcc_checking_assert (precision == x.precision);
1564 scratch[0] = x.val;
1565 if (x.sgn == SIGNED || x.val >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1566 return wi::storage_ref (scratch, 1, precision);
1567 scratch[1] = 0;
1568 return wi::storage_ref (scratch, 2, precision);
1571 /* Private functions for handling large cases out of line. They take
1572 individual length and array parameters because that is cheaper for
1573 the inline caller than constructing an object on the stack and
1574 passing a reference to it. (Although many callers use wide_int_refs,
1575 we generally want those to be removed by SRA.) */
1576 namespace wi
1578 bool eq_p_large (const HOST_WIDE_INT *, unsigned int,
1579 const HOST_WIDE_INT *, unsigned int, unsigned int);
1580 bool lts_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1581 const HOST_WIDE_INT *, unsigned int);
1582 bool ltu_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1583 const HOST_WIDE_INT *, unsigned int);
1584 int cmps_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1585 const HOST_WIDE_INT *, unsigned int);
1586 int cmpu_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1587 const HOST_WIDE_INT *, unsigned int);
1588 unsigned int sext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1589 unsigned int,
1590 unsigned int, unsigned int);
1591 unsigned int zext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1592 unsigned int,
1593 unsigned int, unsigned int);
1594 unsigned int set_bit_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1595 unsigned int, unsigned int, unsigned int);
1596 unsigned int lshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1597 unsigned int, unsigned int, unsigned int);
1598 unsigned int lrshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1599 unsigned int, unsigned int, unsigned int,
1600 unsigned int);
1601 unsigned int arshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1602 unsigned int, unsigned int, unsigned int,
1603 unsigned int);
1604 unsigned int and_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1605 const HOST_WIDE_INT *, unsigned int, unsigned int);
1606 unsigned int and_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1607 unsigned int, const HOST_WIDE_INT *,
1608 unsigned int, unsigned int);
1609 unsigned int or_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1610 const HOST_WIDE_INT *, unsigned int, unsigned int);
1611 unsigned int or_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1612 unsigned int, const HOST_WIDE_INT *,
1613 unsigned int, unsigned int);
1614 unsigned int xor_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1615 const HOST_WIDE_INT *, unsigned int, unsigned int);
1616 unsigned int add_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1617 const HOST_WIDE_INT *, unsigned int, unsigned int,
1618 signop, bool *);
1619 unsigned int sub_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1620 const HOST_WIDE_INT *, unsigned int, unsigned int,
1621 signop, bool *);
1622 unsigned int mul_internal (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1623 unsigned int, const HOST_WIDE_INT *,
1624 unsigned int, unsigned int, signop, bool *,
1625 bool);
1626 unsigned int divmod_internal (HOST_WIDE_INT *, unsigned int *,
1627 HOST_WIDE_INT *, const HOST_WIDE_INT *,
1628 unsigned int, unsigned int,
1629 const HOST_WIDE_INT *,
1630 unsigned int, unsigned int,
1631 signop, bool *);
1634 /* Return the number of bits that integer X can hold. */
1635 template <typename T>
1636 inline unsigned int
1637 wi::get_precision (const T &x)
1639 return wi::int_traits <T>::get_precision (x);
1642 /* Return the number of bits that the result of a binary operation can
1643 hold when the input operands are X and Y. */
1644 template <typename T1, typename T2>
1645 inline unsigned int
1646 wi::get_binary_precision (const T1 &x, const T2 &y)
1648 return get_precision (wi::int_traits <WI_BINARY_RESULT (T1, T2)>::
1649 get_binary_result (x, y));
1652 /* Copy the contents of Y to X, but keeping X's current precision. */
1653 template <typename T1, typename T2>
1654 inline void
1655 wi::copy (T1 &x, const T2 &y)
1657 HOST_WIDE_INT *xval = x.write_val ();
1658 const HOST_WIDE_INT *yval = y.get_val ();
1659 unsigned int len = y.get_len ();
1660 unsigned int i = 0;
1662 xval[i] = yval[i];
1663 while (++i < len);
1664 x.set_len (len, y.is_sign_extended);
1667 /* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
1668 template <typename T>
1669 inline bool
1670 wi::fits_shwi_p (const T &x)
1672 WIDE_INT_REF_FOR (T) xi (x);
1673 return xi.len == 1;
1676 /* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
1677 precision. */
1678 template <typename T>
1679 inline bool
1680 wi::fits_uhwi_p (const T &x)
1682 WIDE_INT_REF_FOR (T) xi (x);
1683 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
1684 return true;
1685 if (xi.len == 1)
1686 return xi.slow () >= 0;
1687 return xi.len == 2 && xi.uhigh () == 0;
1690 /* Return true if X is negative based on the interpretation of SGN.
1691 For UNSIGNED, this is always false. */
1692 template <typename T>
1693 inline bool
1694 wi::neg_p (const T &x, signop sgn)
1696 WIDE_INT_REF_FOR (T) xi (x);
1697 if (sgn == UNSIGNED)
1698 return false;
1699 return xi.sign_mask () < 0;
1702 /* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
1703 template <typename T>
1704 inline HOST_WIDE_INT
1705 wi::sign_mask (const T &x)
1707 WIDE_INT_REF_FOR (T) xi (x);
1708 return xi.sign_mask ();
1711 /* Return true if X == Y. X and Y must be binary-compatible. */
1712 template <typename T1, typename T2>
1713 inline bool
1714 wi::eq_p (const T1 &x, const T2 &y)
1716 unsigned int precision = get_binary_precision (x, y);
1717 WIDE_INT_REF_FOR (T1) xi (x, precision);
1718 WIDE_INT_REF_FOR (T2) yi (y, precision);
1719 if (xi.is_sign_extended && yi.is_sign_extended)
1721 /* This case reduces to array equality. */
1722 if (xi.len != yi.len)
1723 return false;
1724 unsigned int i = 0;
1726 if (xi.val[i] != yi.val[i])
1727 return false;
1728 while (++i != xi.len);
1729 return true;
1731 if (__builtin_expect (yi.len == 1, true))
1733 /* XI is only equal to YI if it too has a single HWI. */
1734 if (xi.len != 1)
1735 return false;
1736 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
1737 with 0 are simple. */
1738 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1739 return xi.val[0] == 0;
1740 /* Otherwise flush out any excess bits first. */
1741 unsigned HOST_WIDE_INT diff = xi.val[0] ^ yi.val[0];
1742 int excess = HOST_BITS_PER_WIDE_INT - precision;
1743 if (excess > 0)
1744 diff <<= excess;
1745 return diff == 0;
1747 return eq_p_large (xi.val, xi.len, yi.val, yi.len, precision);
1750 /* Return true if X != Y. X and Y must be binary-compatible. */
1751 template <typename T1, typename T2>
1752 inline bool
1753 wi::ne_p (const T1 &x, const T2 &y)
1755 return !eq_p (x, y);
1758 /* Return true if X < Y when both are treated as signed values. */
1759 template <typename T1, typename T2>
1760 inline bool
1761 wi::lts_p (const T1 &x, const T2 &y)
1763 unsigned int precision = get_binary_precision (x, y);
1764 WIDE_INT_REF_FOR (T1) xi (x, precision);
1765 WIDE_INT_REF_FOR (T2) yi (y, precision);
1766 /* We optimize x < y, where y is 64 or fewer bits. */
1767 if (wi::fits_shwi_p (yi))
1769 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
1770 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1771 return neg_p (xi);
1772 /* If x fits directly into a shwi, we can compare directly. */
1773 if (wi::fits_shwi_p (xi))
1774 return xi.to_shwi () < yi.to_shwi ();
1775 /* If x doesn't fit and is negative, then it must be more
1776 negative than any value in y, and hence smaller than y. */
1777 if (neg_p (xi))
1778 return true;
1779 /* If x is positive, then it must be larger than any value in y,
1780 and hence greater than y. */
1781 return false;
1783 /* Optimize the opposite case, if it can be detected at compile time. */
1784 if (STATIC_CONSTANT_P (xi.len == 1))
1785 /* If YI is negative it is lower than the least HWI.
1786 If YI is positive it is greater than the greatest HWI. */
1787 return !neg_p (yi);
1788 return lts_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1791 /* Return true if X < Y when both are treated as unsigned values. */
1792 template <typename T1, typename T2>
1793 inline bool
1794 wi::ltu_p (const T1 &x, const T2 &y)
1796 unsigned int precision = get_binary_precision (x, y);
1797 WIDE_INT_REF_FOR (T1) xi (x, precision);
1798 WIDE_INT_REF_FOR (T2) yi (y, precision);
1799 /* Optimize comparisons with constants. */
1800 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1801 return xi.len == 1 && xi.to_uhwi () < (unsigned HOST_WIDE_INT) yi.val[0];
1802 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1803 return yi.len != 1 || yi.to_uhwi () > (unsigned HOST_WIDE_INT) xi.val[0];
1804 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1805 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1806 values does not change the result. */
1807 if (__builtin_expect (xi.len + yi.len == 2, true))
1809 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1810 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1811 return xl < yl;
1813 return ltu_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1816 /* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
1817 template <typename T1, typename T2>
1818 inline bool
1819 wi::lt_p (const T1 &x, const T2 &y, signop sgn)
1821 if (sgn == SIGNED)
1822 return lts_p (x, y);
1823 else
1824 return ltu_p (x, y);
1827 /* Return true if X <= Y when both are treated as signed values. */
1828 template <typename T1, typename T2>
1829 inline bool
1830 wi::les_p (const T1 &x, const T2 &y)
1832 return !lts_p (y, x);
1835 /* Return true if X <= Y when both are treated as unsigned values. */
1836 template <typename T1, typename T2>
1837 inline bool
1838 wi::leu_p (const T1 &x, const T2 &y)
1840 return !ltu_p (y, x);
1843 /* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
1844 template <typename T1, typename T2>
1845 inline bool
1846 wi::le_p (const T1 &x, const T2 &y, signop sgn)
1848 if (sgn == SIGNED)
1849 return les_p (x, y);
1850 else
1851 return leu_p (x, y);
1854 /* Return true if X > Y when both are treated as signed values. */
1855 template <typename T1, typename T2>
1856 inline bool
1857 wi::gts_p (const T1 &x, const T2 &y)
1859 return lts_p (y, x);
1862 /* Return true if X > Y when both are treated as unsigned values. */
1863 template <typename T1, typename T2>
1864 inline bool
1865 wi::gtu_p (const T1 &x, const T2 &y)
1867 return ltu_p (y, x);
1870 /* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
1871 template <typename T1, typename T2>
1872 inline bool
1873 wi::gt_p (const T1 &x, const T2 &y, signop sgn)
1875 if (sgn == SIGNED)
1876 return gts_p (x, y);
1877 else
1878 return gtu_p (x, y);
1881 /* Return true if X >= Y when both are treated as signed values. */
1882 template <typename T1, typename T2>
1883 inline bool
1884 wi::ges_p (const T1 &x, const T2 &y)
1886 return !lts_p (x, y);
1889 /* Return true if X >= Y when both are treated as unsigned values. */
1890 template <typename T1, typename T2>
1891 inline bool
1892 wi::geu_p (const T1 &x, const T2 &y)
1894 return !ltu_p (x, y);
1897 /* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
1898 template <typename T1, typename T2>
1899 inline bool
1900 wi::ge_p (const T1 &x, const T2 &y, signop sgn)
1902 if (sgn == SIGNED)
1903 return ges_p (x, y);
1904 else
1905 return geu_p (x, y);
1908 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1909 as signed values. */
1910 template <typename T1, typename T2>
1911 inline int
1912 wi::cmps (const T1 &x, const T2 &y)
1914 unsigned int precision = get_binary_precision (x, y);
1915 WIDE_INT_REF_FOR (T1) xi (x, precision);
1916 WIDE_INT_REF_FOR (T2) yi (y, precision);
1917 if (wi::fits_shwi_p (yi))
1919 /* Special case for comparisons with 0. */
1920 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1921 return neg_p (xi) ? -1 : !(xi.len == 1 && xi.val[0] == 0);
1922 /* If x fits into a signed HWI, we can compare directly. */
1923 if (wi::fits_shwi_p (xi))
1925 HOST_WIDE_INT xl = xi.to_shwi ();
1926 HOST_WIDE_INT yl = yi.to_shwi ();
1927 return xl < yl ? -1 : xl > yl;
1929 /* If x doesn't fit and is negative, then it must be more
1930 negative than any signed HWI, and hence smaller than y. */
1931 if (neg_p (xi))
1932 return -1;
1933 /* If x is positive, then it must be larger than any signed HWI,
1934 and hence greater than y. */
1935 return 1;
1937 /* Optimize the opposite case, if it can be detected at compile time. */
1938 if (STATIC_CONSTANT_P (xi.len == 1))
1939 /* If YI is negative it is lower than the least HWI.
1940 If YI is positive it is greater than the greatest HWI. */
1941 return neg_p (yi) ? 1 : -1;
1942 return cmps_large (xi.val, xi.len, precision, yi.val, yi.len);
1945 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1946 as unsigned values. */
1947 template <typename T1, typename T2>
1948 inline int
1949 wi::cmpu (const T1 &x, const T2 &y)
1951 unsigned int precision = get_binary_precision (x, y);
1952 WIDE_INT_REF_FOR (T1) xi (x, precision);
1953 WIDE_INT_REF_FOR (T2) yi (y, precision);
1954 /* Optimize comparisons with constants. */
1955 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1957 /* If XI doesn't fit in a HWI then it must be larger than YI. */
1958 if (xi.len != 1)
1959 return 1;
1960 /* Otherwise compare directly. */
1961 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1962 unsigned HOST_WIDE_INT yl = yi.val[0];
1963 return xl < yl ? -1 : xl > yl;
1965 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1967 /* If YI doesn't fit in a HWI then it must be larger than XI. */
1968 if (yi.len != 1)
1969 return -1;
1970 /* Otherwise compare directly. */
1971 unsigned HOST_WIDE_INT xl = xi.val[0];
1972 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1973 return xl < yl ? -1 : xl > yl;
1975 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1976 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1977 values does not change the result. */
1978 if (__builtin_expect (xi.len + yi.len == 2, true))
1980 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1981 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1982 return xl < yl ? -1 : xl > yl;
1984 return cmpu_large (xi.val, xi.len, precision, yi.val, yi.len);
1987 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
1988 X and Y indicated by SGN. */
1989 template <typename T1, typename T2>
1990 inline int
1991 wi::cmp (const T1 &x, const T2 &y, signop sgn)
1993 if (sgn == SIGNED)
1994 return cmps (x, y);
1995 else
1996 return cmpu (x, y);
1999 /* Return ~x. */
2000 template <typename T>
2001 inline WI_UNARY_RESULT (T)
2002 wi::bit_not (const T &x)
2004 WI_UNARY_RESULT_VAR (result, val, T, x);
2005 WIDE_INT_REF_FOR (T) xi (x, get_precision (result));
2006 for (unsigned int i = 0; i < xi.len; ++i)
2007 val[i] = ~xi.val[i];
2008 result.set_len (xi.len);
2009 return result;
2012 /* Return -x. */
2013 template <typename T>
2014 inline WI_UNARY_RESULT (T)
2015 wi::neg (const T &x)
2017 return sub (0, x);
2020 /* Return -x. Indicate in *OVERFLOW if X is the minimum signed value. */
2021 template <typename T>
2022 inline WI_UNARY_RESULT (T)
2023 wi::neg (const T &x, bool *overflow)
2025 *overflow = only_sign_bit_p (x);
2026 return sub (0, x);
2029 /* Return the absolute value of x. */
2030 template <typename T>
2031 inline WI_UNARY_RESULT (T)
2032 wi::abs (const T &x)
2034 return neg_p (x) ? neg (x) : WI_UNARY_RESULT (T) (x);
2037 /* Return the result of sign-extending the low OFFSET bits of X. */
2038 template <typename T>
2039 inline WI_UNARY_RESULT (T)
2040 wi::sext (const T &x, unsigned int offset)
2042 WI_UNARY_RESULT_VAR (result, val, T, x);
2043 unsigned int precision = get_precision (result);
2044 WIDE_INT_REF_FOR (T) xi (x, precision);
2046 if (offset <= HOST_BITS_PER_WIDE_INT)
2048 val[0] = sext_hwi (xi.ulow (), offset);
2049 result.set_len (1, true);
2051 else
2052 result.set_len (sext_large (val, xi.val, xi.len, precision, offset));
2053 return result;
2056 /* Return the result of zero-extending the low OFFSET bits of X. */
2057 template <typename T>
2058 inline WI_UNARY_RESULT (T)
2059 wi::zext (const T &x, unsigned int offset)
2061 WI_UNARY_RESULT_VAR (result, val, T, x);
2062 unsigned int precision = get_precision (result);
2063 WIDE_INT_REF_FOR (T) xi (x, precision);
2065 /* This is not just an optimization, it is actually required to
2066 maintain canonization. */
2067 if (offset >= precision)
2069 wi::copy (result, xi);
2070 return result;
2073 /* In these cases we know that at least the top bit will be clear,
2074 so no sign extension is necessary. */
2075 if (offset < HOST_BITS_PER_WIDE_INT)
2077 val[0] = zext_hwi (xi.ulow (), offset);
2078 result.set_len (1, true);
2080 else
2081 result.set_len (zext_large (val, xi.val, xi.len, precision, offset), true);
2082 return result;
2085 /* Return the result of extending the low OFFSET bits of X according to
2086 signedness SGN. */
2087 template <typename T>
2088 inline WI_UNARY_RESULT (T)
2089 wi::ext (const T &x, unsigned int offset, signop sgn)
2091 return sgn == SIGNED ? sext (x, offset) : zext (x, offset);
2094 /* Return an integer that represents X | (1 << bit). */
2095 template <typename T>
2096 inline WI_UNARY_RESULT (T)
2097 wi::set_bit (const T &x, unsigned int bit)
2099 WI_UNARY_RESULT_VAR (result, val, T, x);
2100 unsigned int precision = get_precision (result);
2101 WIDE_INT_REF_FOR (T) xi (x, precision);
2102 if (precision <= HOST_BITS_PER_WIDE_INT)
2104 val[0] = xi.ulow () | ((unsigned HOST_WIDE_INT) 1 << bit);
2105 result.set_len (1);
2107 else
2108 result.set_len (set_bit_large (val, xi.val, xi.len, precision, bit));
2109 return result;
2112 /* Return the mininum of X and Y, treating them both as having
2113 signedness SGN. */
2114 template <typename T1, typename T2>
2115 inline WI_BINARY_RESULT (T1, T2)
2116 wi::min (const T1 &x, const T2 &y, signop sgn)
2118 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2119 unsigned int precision = get_precision (result);
2120 if (wi::le_p (x, y, sgn))
2121 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2122 else
2123 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2124 return result;
2127 /* Return the minimum of X and Y, treating both as signed values. */
2128 template <typename T1, typename T2>
2129 inline WI_BINARY_RESULT (T1, T2)
2130 wi::smin (const T1 &x, const T2 &y)
2132 return min (x, y, SIGNED);
2135 /* Return the minimum of X and Y, treating both as unsigned values. */
2136 template <typename T1, typename T2>
2137 inline WI_BINARY_RESULT (T1, T2)
2138 wi::umin (const T1 &x, const T2 &y)
2140 return min (x, y, UNSIGNED);
2143 /* Return the maxinum of X and Y, treating them both as having
2144 signedness SGN. */
2145 template <typename T1, typename T2>
2146 inline WI_BINARY_RESULT (T1, T2)
2147 wi::max (const T1 &x, const T2 &y, signop sgn)
2149 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2150 unsigned int precision = get_precision (result);
2151 if (wi::ge_p (x, y, sgn))
2152 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2153 else
2154 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2155 return result;
2158 /* Return the maximum of X and Y, treating both as signed values. */
2159 template <typename T1, typename T2>
2160 inline WI_BINARY_RESULT (T1, T2)
2161 wi::smax (const T1 &x, const T2 &y)
2163 return max (x, y, SIGNED);
2166 /* Return the maximum of X and Y, treating both as unsigned values. */
2167 template <typename T1, typename T2>
2168 inline WI_BINARY_RESULT (T1, T2)
2169 wi::umax (const T1 &x, const T2 &y)
2171 return max (x, y, UNSIGNED);
2174 /* Return X & Y. */
2175 template <typename T1, typename T2>
2176 inline WI_BINARY_RESULT (T1, T2)
2177 wi::bit_and (const T1 &x, const T2 &y)
2179 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2180 unsigned int precision = get_precision (result);
2181 WIDE_INT_REF_FOR (T1) xi (x, precision);
2182 WIDE_INT_REF_FOR (T2) yi (y, precision);
2183 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2184 if (__builtin_expect (xi.len + yi.len == 2, true))
2186 val[0] = xi.ulow () & yi.ulow ();
2187 result.set_len (1, is_sign_extended);
2189 else
2190 result.set_len (and_large (val, xi.val, xi.len, yi.val, yi.len,
2191 precision), is_sign_extended);
2192 return result;
2195 /* Return X & ~Y. */
2196 template <typename T1, typename T2>
2197 inline WI_BINARY_RESULT (T1, T2)
2198 wi::bit_and_not (const T1 &x, const T2 &y)
2200 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2201 unsigned int precision = get_precision (result);
2202 WIDE_INT_REF_FOR (T1) xi (x, precision);
2203 WIDE_INT_REF_FOR (T2) yi (y, precision);
2204 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2205 if (__builtin_expect (xi.len + yi.len == 2, true))
2207 val[0] = xi.ulow () & ~yi.ulow ();
2208 result.set_len (1, is_sign_extended);
2210 else
2211 result.set_len (and_not_large (val, xi.val, xi.len, yi.val, yi.len,
2212 precision), is_sign_extended);
2213 return result;
2216 /* Return X | Y. */
2217 template <typename T1, typename T2>
2218 inline WI_BINARY_RESULT (T1, T2)
2219 wi::bit_or (const T1 &x, const T2 &y)
2221 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2222 unsigned int precision = get_precision (result);
2223 WIDE_INT_REF_FOR (T1) xi (x, precision);
2224 WIDE_INT_REF_FOR (T2) yi (y, precision);
2225 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2226 if (__builtin_expect (xi.len + yi.len == 2, true))
2228 val[0] = xi.ulow () | yi.ulow ();
2229 result.set_len (1, is_sign_extended);
2231 else
2232 result.set_len (or_large (val, xi.val, xi.len,
2233 yi.val, yi.len, precision), is_sign_extended);
2234 return result;
2237 /* Return X | ~Y. */
2238 template <typename T1, typename T2>
2239 inline WI_BINARY_RESULT (T1, T2)
2240 wi::bit_or_not (const T1 &x, const T2 &y)
2242 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2243 unsigned int precision = get_precision (result);
2244 WIDE_INT_REF_FOR (T1) xi (x, precision);
2245 WIDE_INT_REF_FOR (T2) yi (y, precision);
2246 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2247 if (__builtin_expect (xi.len + yi.len == 2, true))
2249 val[0] = xi.ulow () | ~yi.ulow ();
2250 result.set_len (1, is_sign_extended);
2252 else
2253 result.set_len (or_not_large (val, xi.val, xi.len, yi.val, yi.len,
2254 precision), is_sign_extended);
2255 return result;
2258 /* Return X ^ Y. */
2259 template <typename T1, typename T2>
2260 inline WI_BINARY_RESULT (T1, T2)
2261 wi::bit_xor (const T1 &x, const T2 &y)
2263 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2264 unsigned int precision = get_precision (result);
2265 WIDE_INT_REF_FOR (T1) xi (x, precision);
2266 WIDE_INT_REF_FOR (T2) yi (y, precision);
2267 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2268 if (__builtin_expect (xi.len + yi.len == 2, true))
2270 val[0] = xi.ulow () ^ yi.ulow ();
2271 result.set_len (1, is_sign_extended);
2273 else
2274 result.set_len (xor_large (val, xi.val, xi.len,
2275 yi.val, yi.len, precision), is_sign_extended);
2276 return result;
2279 /* Return X + Y. */
2280 template <typename T1, typename T2>
2281 inline WI_BINARY_RESULT (T1, T2)
2282 wi::add (const T1 &x, const T2 &y)
2284 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2285 unsigned int precision = get_precision (result);
2286 WIDE_INT_REF_FOR (T1) xi (x, precision);
2287 WIDE_INT_REF_FOR (T2) yi (y, precision);
2288 if (precision <= HOST_BITS_PER_WIDE_INT)
2290 val[0] = xi.ulow () + yi.ulow ();
2291 result.set_len (1);
2293 /* If the precision is known at compile time to be greater than
2294 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2295 knowing that (a) all bits in those HWIs are significant and
2296 (b) the result has room for at least two HWIs. This provides
2297 a fast path for things like offset_int and widest_int.
2299 The STATIC_CONSTANT_P test prevents this path from being
2300 used for wide_ints. wide_ints with precisions greater than
2301 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2302 point handling them inline. */
2303 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2304 && __builtin_expect (xi.len + yi.len == 2, true))
2306 unsigned HOST_WIDE_INT xl = xi.ulow ();
2307 unsigned HOST_WIDE_INT yl = yi.ulow ();
2308 unsigned HOST_WIDE_INT resultl = xl + yl;
2309 val[0] = resultl;
2310 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2311 result.set_len (1 + (((resultl ^ xl) & (resultl ^ yl))
2312 >> (HOST_BITS_PER_WIDE_INT - 1)));
2314 else
2315 result.set_len (add_large (val, xi.val, xi.len,
2316 yi.val, yi.len, precision,
2317 UNSIGNED, 0));
2318 return result;
2321 /* Return X + Y. Treat X and Y as having the signednes given by SGN
2322 and indicate in *OVERFLOW whether the operation overflowed. */
2323 template <typename T1, typename T2>
2324 inline WI_BINARY_RESULT (T1, T2)
2325 wi::add (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2327 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2328 unsigned int precision = get_precision (result);
2329 WIDE_INT_REF_FOR (T1) xi (x, precision);
2330 WIDE_INT_REF_FOR (T2) yi (y, precision);
2331 if (precision <= HOST_BITS_PER_WIDE_INT)
2333 unsigned HOST_WIDE_INT xl = xi.ulow ();
2334 unsigned HOST_WIDE_INT yl = yi.ulow ();
2335 unsigned HOST_WIDE_INT resultl = xl + yl;
2336 if (sgn == SIGNED)
2337 *overflow = (((resultl ^ xl) & (resultl ^ yl))
2338 >> (precision - 1)) & 1;
2339 else
2340 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2341 < (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2342 val[0] = resultl;
2343 result.set_len (1);
2345 else
2346 result.set_len (add_large (val, xi.val, xi.len,
2347 yi.val, yi.len, precision,
2348 sgn, overflow));
2349 return result;
2352 /* Return X - Y. */
2353 template <typename T1, typename T2>
2354 inline WI_BINARY_RESULT (T1, T2)
2355 wi::sub (const T1 &x, const T2 &y)
2357 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2358 unsigned int precision = get_precision (result);
2359 WIDE_INT_REF_FOR (T1) xi (x, precision);
2360 WIDE_INT_REF_FOR (T2) yi (y, precision);
2361 if (precision <= HOST_BITS_PER_WIDE_INT)
2363 val[0] = xi.ulow () - yi.ulow ();
2364 result.set_len (1);
2366 /* If the precision is known at compile time to be greater than
2367 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2368 knowing that (a) all bits in those HWIs are significant and
2369 (b) the result has room for at least two HWIs. This provides
2370 a fast path for things like offset_int and widest_int.
2372 The STATIC_CONSTANT_P test prevents this path from being
2373 used for wide_ints. wide_ints with precisions greater than
2374 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2375 point handling them inline. */
2376 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2377 && __builtin_expect (xi.len + yi.len == 2, true))
2379 unsigned HOST_WIDE_INT xl = xi.ulow ();
2380 unsigned HOST_WIDE_INT yl = yi.ulow ();
2381 unsigned HOST_WIDE_INT resultl = xl - yl;
2382 val[0] = resultl;
2383 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2384 result.set_len (1 + (((resultl ^ xl) & (xl ^ yl))
2385 >> (HOST_BITS_PER_WIDE_INT - 1)));
2387 else
2388 result.set_len (sub_large (val, xi.val, xi.len,
2389 yi.val, yi.len, precision,
2390 UNSIGNED, 0));
2391 return result;
2394 /* Return X - Y. Treat X and Y as having the signednes given by SGN
2395 and indicate in *OVERFLOW whether the operation overflowed. */
2396 template <typename T1, typename T2>
2397 inline WI_BINARY_RESULT (T1, T2)
2398 wi::sub (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2400 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2401 unsigned int precision = get_precision (result);
2402 WIDE_INT_REF_FOR (T1) xi (x, precision);
2403 WIDE_INT_REF_FOR (T2) yi (y, precision);
2404 if (precision <= HOST_BITS_PER_WIDE_INT)
2406 unsigned HOST_WIDE_INT xl = xi.ulow ();
2407 unsigned HOST_WIDE_INT yl = yi.ulow ();
2408 unsigned HOST_WIDE_INT resultl = xl - yl;
2409 if (sgn == SIGNED)
2410 *overflow = (((xl ^ yl) & (resultl ^ xl)) >> (precision - 1)) & 1;
2411 else
2412 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2413 > (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2414 val[0] = resultl;
2415 result.set_len (1);
2417 else
2418 result.set_len (sub_large (val, xi.val, xi.len,
2419 yi.val, yi.len, precision,
2420 sgn, overflow));
2421 return result;
2424 /* Return X * Y. */
2425 template <typename T1, typename T2>
2426 inline WI_BINARY_RESULT (T1, T2)
2427 wi::mul (const T1 &x, const T2 &y)
2429 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2430 unsigned int precision = get_precision (result);
2431 WIDE_INT_REF_FOR (T1) xi (x, precision);
2432 WIDE_INT_REF_FOR (T2) yi (y, precision);
2433 if (precision <= HOST_BITS_PER_WIDE_INT)
2435 val[0] = xi.ulow () * yi.ulow ();
2436 result.set_len (1);
2438 else
2439 result.set_len (mul_internal (val, xi.val, xi.len, yi.val, yi.len,
2440 precision, UNSIGNED, 0, false));
2441 return result;
2444 /* Return X * Y. Treat X and Y as having the signednes given by SGN
2445 and indicate in *OVERFLOW whether the operation overflowed. */
2446 template <typename T1, typename T2>
2447 inline WI_BINARY_RESULT (T1, T2)
2448 wi::mul (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2450 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2451 unsigned int precision = get_precision (result);
2452 WIDE_INT_REF_FOR (T1) xi (x, precision);
2453 WIDE_INT_REF_FOR (T2) yi (y, precision);
2454 result.set_len (mul_internal (val, xi.val, xi.len,
2455 yi.val, yi.len, precision,
2456 sgn, overflow, false));
2457 return result;
2460 /* Return X * Y, treating both X and Y as signed values. Indicate in
2461 *OVERFLOW whether the operation overflowed. */
2462 template <typename T1, typename T2>
2463 inline WI_BINARY_RESULT (T1, T2)
2464 wi::smul (const T1 &x, const T2 &y, bool *overflow)
2466 return mul (x, y, SIGNED, overflow);
2469 /* Return X * Y, treating both X and Y as unsigned values. Indicate in
2470 *OVERFLOW whether the operation overflowed. */
2471 template <typename T1, typename T2>
2472 inline WI_BINARY_RESULT (T1, T2)
2473 wi::umul (const T1 &x, const T2 &y, bool *overflow)
2475 return mul (x, y, UNSIGNED, overflow);
2478 /* Perform a widening multiplication of X and Y, extending the values
2479 according to SGN, and return the high part of the result. */
2480 template <typename T1, typename T2>
2481 inline WI_BINARY_RESULT (T1, T2)
2482 wi::mul_high (const T1 &x, const T2 &y, signop sgn)
2484 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2485 unsigned int precision = get_precision (result);
2486 WIDE_INT_REF_FOR (T1) xi (x, precision);
2487 WIDE_INT_REF_FOR (T2) yi (y, precision);
2488 result.set_len (mul_internal (val, xi.val, xi.len,
2489 yi.val, yi.len, precision,
2490 sgn, 0, true));
2491 return result;
2494 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2495 signedness given by SGN. Indicate in *OVERFLOW if the result
2496 overflows. */
2497 template <typename T1, typename T2>
2498 inline WI_BINARY_RESULT (T1, T2)
2499 wi::div_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2501 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2502 unsigned int precision = get_precision (quotient);
2503 WIDE_INT_REF_FOR (T1) xi (x, precision);
2504 WIDE_INT_REF_FOR (T2) yi (y);
2506 quotient.set_len (divmod_internal (quotient_val, 0, 0, xi.val, xi.len,
2507 precision,
2508 yi.val, yi.len, yi.precision,
2509 sgn, overflow));
2510 return quotient;
2513 /* Return X / Y, rouding towards 0. Treat X and Y as signed values. */
2514 template <typename T1, typename T2>
2515 inline WI_BINARY_RESULT (T1, T2)
2516 wi::sdiv_trunc (const T1 &x, const T2 &y)
2518 return div_trunc (x, y, SIGNED);
2521 /* Return X / Y, rouding towards 0. Treat X and Y as unsigned values. */
2522 template <typename T1, typename T2>
2523 inline WI_BINARY_RESULT (T1, T2)
2524 wi::udiv_trunc (const T1 &x, const T2 &y)
2526 return div_trunc (x, y, UNSIGNED);
2529 /* Return X / Y, rouding towards -inf. Treat X and Y as having the
2530 signedness given by SGN. Indicate in *OVERFLOW if the result
2531 overflows. */
2532 template <typename T1, typename T2>
2533 inline WI_BINARY_RESULT (T1, T2)
2534 wi::div_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2536 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2537 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2538 unsigned int precision = get_precision (quotient);
2539 WIDE_INT_REF_FOR (T1) xi (x, precision);
2540 WIDE_INT_REF_FOR (T2) yi (y);
2542 unsigned int remainder_len;
2543 quotient.set_len (divmod_internal (quotient_val,
2544 &remainder_len, remainder_val,
2545 xi.val, xi.len, precision,
2546 yi.val, yi.len, yi.precision, sgn,
2547 overflow));
2548 remainder.set_len (remainder_len);
2549 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2550 return quotient - 1;
2551 return quotient;
2554 /* Return X / Y, rouding towards -inf. Treat X and Y as signed values. */
2555 template <typename T1, typename T2>
2556 inline WI_BINARY_RESULT (T1, T2)
2557 wi::sdiv_floor (const T1 &x, const T2 &y)
2559 return div_floor (x, y, SIGNED);
2562 /* Return X / Y, rouding towards -inf. Treat X and Y as unsigned values. */
2563 /* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
2564 template <typename T1, typename T2>
2565 inline WI_BINARY_RESULT (T1, T2)
2566 wi::udiv_floor (const T1 &x, const T2 &y)
2568 return div_floor (x, y, UNSIGNED);
2571 /* Return X / Y, rouding towards +inf. Treat X and Y as having the
2572 signedness given by SGN. Indicate in *OVERFLOW if the result
2573 overflows. */
2574 template <typename T1, typename T2>
2575 inline WI_BINARY_RESULT (T1, T2)
2576 wi::div_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2578 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2579 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2580 unsigned int precision = get_precision (quotient);
2581 WIDE_INT_REF_FOR (T1) xi (x, precision);
2582 WIDE_INT_REF_FOR (T2) yi (y);
2584 unsigned int remainder_len;
2585 quotient.set_len (divmod_internal (quotient_val,
2586 &remainder_len, remainder_val,
2587 xi.val, xi.len, precision,
2588 yi.val, yi.len, yi.precision, sgn,
2589 overflow));
2590 remainder.set_len (remainder_len);
2591 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2592 return quotient + 1;
2593 return quotient;
2596 /* Return X / Y, rouding towards nearest with ties away from zero.
2597 Treat X and Y as having the signedness given by SGN. Indicate
2598 in *OVERFLOW if the result overflows. */
2599 template <typename T1, typename T2>
2600 inline WI_BINARY_RESULT (T1, T2)
2601 wi::div_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2603 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2604 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2605 unsigned int precision = get_precision (quotient);
2606 WIDE_INT_REF_FOR (T1) xi (x, precision);
2607 WIDE_INT_REF_FOR (T2) yi (y);
2609 unsigned int remainder_len;
2610 quotient.set_len (divmod_internal (quotient_val,
2611 &remainder_len, remainder_val,
2612 xi.val, xi.len, precision,
2613 yi.val, yi.len, yi.precision, sgn,
2614 overflow));
2615 remainder.set_len (remainder_len);
2617 if (remainder != 0)
2619 if (sgn == SIGNED)
2621 if (wi::ges_p (wi::abs (remainder),
2622 wi::lrshift (wi::abs (y), 1)))
2624 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2625 return quotient - 1;
2626 else
2627 return quotient + 1;
2630 else
2632 if (wi::geu_p (remainder, wi::lrshift (y, 1)))
2633 return quotient + 1;
2636 return quotient;
2639 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2640 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
2641 template <typename T1, typename T2>
2642 inline WI_BINARY_RESULT (T1, T2)
2643 wi::divmod_trunc (const T1 &x, const T2 &y, signop sgn,
2644 WI_BINARY_RESULT (T1, T2) *remainder_ptr)
2646 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2647 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2648 unsigned int precision = get_precision (quotient);
2649 WIDE_INT_REF_FOR (T1) xi (x, precision);
2650 WIDE_INT_REF_FOR (T2) yi (y);
2652 unsigned int remainder_len;
2653 quotient.set_len (divmod_internal (quotient_val,
2654 &remainder_len, remainder_val,
2655 xi.val, xi.len, precision,
2656 yi.val, yi.len, yi.precision, sgn, 0));
2657 remainder.set_len (remainder_len);
2659 *remainder_ptr = remainder;
2660 return quotient;
2663 /* Compute X / Y, rouding towards 0, and return the remainder.
2664 Treat X and Y as having the signedness given by SGN. Indicate
2665 in *OVERFLOW if the division overflows. */
2666 template <typename T1, typename T2>
2667 inline WI_BINARY_RESULT (T1, T2)
2668 wi::mod_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2670 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2671 unsigned int precision = get_precision (remainder);
2672 WIDE_INT_REF_FOR (T1) xi (x, precision);
2673 WIDE_INT_REF_FOR (T2) yi (y);
2675 unsigned int remainder_len;
2676 divmod_internal (0, &remainder_len, remainder_val,
2677 xi.val, xi.len, precision,
2678 yi.val, yi.len, yi.precision, sgn, overflow);
2679 remainder.set_len (remainder_len);
2681 return remainder;
2684 /* Compute X / Y, rouding towards 0, and return the remainder.
2685 Treat X and Y as signed values. */
2686 template <typename T1, typename T2>
2687 inline WI_BINARY_RESULT (T1, T2)
2688 wi::smod_trunc (const T1 &x, const T2 &y)
2690 return mod_trunc (x, y, SIGNED);
2693 /* Compute X / Y, rouding towards 0, and return the remainder.
2694 Treat X and Y as unsigned values. */
2695 template <typename T1, typename T2>
2696 inline WI_BINARY_RESULT (T1, T2)
2697 wi::umod_trunc (const T1 &x, const T2 &y)
2699 return mod_trunc (x, y, UNSIGNED);
2702 /* Compute X / Y, rouding towards -inf, and return the remainder.
2703 Treat X and Y as having the signedness given by SGN. Indicate
2704 in *OVERFLOW if the division overflows. */
2705 template <typename T1, typename T2>
2706 inline WI_BINARY_RESULT (T1, T2)
2707 wi::mod_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2709 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2710 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2711 unsigned int precision = get_precision (quotient);
2712 WIDE_INT_REF_FOR (T1) xi (x, precision);
2713 WIDE_INT_REF_FOR (T2) yi (y);
2715 unsigned int remainder_len;
2716 quotient.set_len (divmod_internal (quotient_val,
2717 &remainder_len, remainder_val,
2718 xi.val, xi.len, precision,
2719 yi.val, yi.len, yi.precision, sgn,
2720 overflow));
2721 remainder.set_len (remainder_len);
2723 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2724 return remainder + y;
2725 return remainder;
2728 /* Compute X / Y, rouding towards -inf, and return the remainder.
2729 Treat X and Y as unsigned values. */
2730 /* ??? Why do we have both this and umod_trunc. Aren't they the same? */
2731 template <typename T1, typename T2>
2732 inline WI_BINARY_RESULT (T1, T2)
2733 wi::umod_floor (const T1 &x, const T2 &y)
2735 return mod_floor (x, y, UNSIGNED);
2738 /* Compute X / Y, rouding towards +inf, and return the remainder.
2739 Treat X and Y as having the signedness given by SGN. Indicate
2740 in *OVERFLOW if the division overflows. */
2741 template <typename T1, typename T2>
2742 inline WI_BINARY_RESULT (T1, T2)
2743 wi::mod_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2745 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2746 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2747 unsigned int precision = get_precision (quotient);
2748 WIDE_INT_REF_FOR (T1) xi (x, precision);
2749 WIDE_INT_REF_FOR (T2) yi (y);
2751 unsigned int remainder_len;
2752 quotient.set_len (divmod_internal (quotient_val,
2753 &remainder_len, remainder_val,
2754 xi.val, xi.len, precision,
2755 yi.val, yi.len, yi.precision, sgn,
2756 overflow));
2757 remainder.set_len (remainder_len);
2759 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2760 return remainder - y;
2761 return remainder;
2764 /* Compute X / Y, rouding towards nearest with ties away from zero,
2765 and return the remainder. Treat X and Y as having the signedness
2766 given by SGN. Indicate in *OVERFLOW if the division overflows. */
2767 template <typename T1, typename T2>
2768 inline WI_BINARY_RESULT (T1, T2)
2769 wi::mod_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2771 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2772 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2773 unsigned int precision = get_precision (quotient);
2774 WIDE_INT_REF_FOR (T1) xi (x, precision);
2775 WIDE_INT_REF_FOR (T2) yi (y);
2777 unsigned int remainder_len;
2778 quotient.set_len (divmod_internal (quotient_val,
2779 &remainder_len, remainder_val,
2780 xi.val, xi.len, precision,
2781 yi.val, yi.len, yi.precision, sgn,
2782 overflow));
2783 remainder.set_len (remainder_len);
2785 if (remainder != 0)
2787 if (sgn == SIGNED)
2789 if (wi::ges_p (wi::abs (remainder),
2790 wi::lrshift (wi::abs (y), 1)))
2792 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2793 return remainder + y;
2794 else
2795 return remainder - y;
2798 else
2800 if (wi::geu_p (remainder, wi::lrshift (y, 1)))
2801 return remainder - y;
2804 return remainder;
2807 /* Return true if X is a multiple of Y. Treat X and Y as having the
2808 signedness given by SGN. */
2809 template <typename T1, typename T2>
2810 inline bool
2811 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn)
2813 return wi::mod_trunc (x, y, sgn) == 0;
2816 /* Return true if X is a multiple of Y, storing X / Y in *RES if so.
2817 Treat X and Y as having the signedness given by SGN. */
2818 template <typename T1, typename T2>
2819 inline bool
2820 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn,
2821 WI_BINARY_RESULT (T1, T2) *res)
2823 WI_BINARY_RESULT (T1, T2) remainder;
2824 WI_BINARY_RESULT (T1, T2) quotient
2825 = divmod_trunc (x, y, sgn, &remainder);
2826 if (remainder == 0)
2828 *res = quotient;
2829 return true;
2831 return false;
2834 /* Return X << Y. Return 0 if Y is greater than or equal to
2835 the precision of X. */
2836 template <typename T1, typename T2>
2837 inline WI_UNARY_RESULT (T1)
2838 wi::lshift (const T1 &x, const T2 &y)
2840 WI_UNARY_RESULT_VAR (result, val, T1, x);
2841 unsigned int precision = get_precision (result);
2842 WIDE_INT_REF_FOR (T1) xi (x, precision);
2843 WIDE_INT_REF_FOR (T2) yi (y);
2844 /* Handle the simple cases quickly. */
2845 if (geu_p (yi, precision))
2847 val[0] = 0;
2848 result.set_len (1);
2850 else
2852 unsigned int shift = yi.to_uhwi ();
2853 /* For fixed-precision integers like offset_int and widest_int,
2854 handle the case where the shift value is constant and the
2855 result is a single nonnegative HWI (meaning that we don't
2856 need to worry about val[1]). This is particularly common
2857 for converting a byte count to a bit count.
2859 For variable-precision integers like wide_int, handle HWI
2860 and sub-HWI integers inline. */
2861 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2862 ? (STATIC_CONSTANT_P (shift < HOST_BITS_PER_WIDE_INT - 1)
2863 && xi.len == 1
2864 && xi.val[0] <= (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT)
2865 HOST_WIDE_INT_MAX >> shift))
2866 : precision <= HOST_BITS_PER_WIDE_INT)
2868 val[0] = xi.ulow () << shift;
2869 result.set_len (1);
2871 else
2872 result.set_len (lshift_large (val, xi.val, xi.len,
2873 precision, shift));
2875 return result;
2878 /* Return X >> Y, using a logical shift. Return 0 if Y is greater than
2879 or equal to the precision of X. */
2880 template <typename T1, typename T2>
2881 inline WI_UNARY_RESULT (T1)
2882 wi::lrshift (const T1 &x, const T2 &y)
2884 WI_UNARY_RESULT_VAR (result, val, T1, x);
2885 /* Do things in the precision of the input rather than the output,
2886 since the result can be no larger than that. */
2887 WIDE_INT_REF_FOR (T1) xi (x);
2888 WIDE_INT_REF_FOR (T2) yi (y);
2889 /* Handle the simple cases quickly. */
2890 if (geu_p (yi, xi.precision))
2892 val[0] = 0;
2893 result.set_len (1);
2895 else
2897 unsigned int shift = yi.to_uhwi ();
2898 /* For fixed-precision integers like offset_int and widest_int,
2899 handle the case where the shift value is constant and the
2900 shifted value is a single nonnegative HWI (meaning that all
2901 bits above the HWI are zero). This is particularly common
2902 for converting a bit count to a byte count.
2904 For variable-precision integers like wide_int, handle HWI
2905 and sub-HWI integers inline. */
2906 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2907 ? xi.len == 1 && xi.val[0] >= 0
2908 : xi.precision <= HOST_BITS_PER_WIDE_INT)
2910 val[0] = xi.to_uhwi () >> shift;
2911 result.set_len (1);
2913 else
2914 result.set_len (lrshift_large (val, xi.val, xi.len, xi.precision,
2915 get_precision (result), shift));
2917 return result;
2920 /* Return X >> Y, using an arithmetic shift. Return a sign mask if
2921 Y is greater than or equal to the precision of X. */
2922 template <typename T1, typename T2>
2923 inline WI_UNARY_RESULT (T1)
2924 wi::arshift (const T1 &x, const T2 &y)
2926 WI_UNARY_RESULT_VAR (result, val, T1, x);
2927 /* Do things in the precision of the input rather than the output,
2928 since the result can be no larger than that. */
2929 WIDE_INT_REF_FOR (T1) xi (x);
2930 WIDE_INT_REF_FOR (T2) yi (y);
2931 /* Handle the simple cases quickly. */
2932 if (geu_p (yi, xi.precision))
2934 val[0] = sign_mask (x);
2935 result.set_len (1);
2937 else
2939 unsigned int shift = yi.to_uhwi ();
2940 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
2942 val[0] = sext_hwi (xi.ulow () >> shift, xi.precision - shift);
2943 result.set_len (1, true);
2945 else
2946 result.set_len (arshift_large (val, xi.val, xi.len, xi.precision,
2947 get_precision (result), shift));
2949 return result;
2952 /* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
2953 logical shift otherwise. */
2954 template <typename T1, typename T2>
2955 inline WI_UNARY_RESULT (T1)
2956 wi::rshift (const T1 &x, const T2 &y, signop sgn)
2958 if (sgn == UNSIGNED)
2959 return lrshift (x, y);
2960 else
2961 return arshift (x, y);
2964 /* Return the result of rotating the low WIDTH bits of X left by Y
2965 bits and zero-extending the result. Use a full-width rotate if
2966 WIDTH is zero. */
2967 template <typename T1, typename T2>
2968 WI_UNARY_RESULT (T1)
2969 wi::lrotate (const T1 &x, const T2 &y, unsigned int width)
2971 unsigned int precision = get_binary_precision (x, x);
2972 if (width == 0)
2973 width = precision;
2974 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
2975 WI_UNARY_RESULT (T1) left = wi::lshift (x, ymod);
2976 WI_UNARY_RESULT (T1) right = wi::lrshift (x, wi::sub (width, ymod));
2977 if (width != precision)
2978 return wi::zext (left, width) | wi::zext (right, width);
2979 return left | right;
2982 /* Return the result of rotating the low WIDTH bits of X right by Y
2983 bits and zero-extending the result. Use a full-width rotate if
2984 WIDTH is zero. */
2985 template <typename T1, typename T2>
2986 WI_UNARY_RESULT (T1)
2987 wi::rrotate (const T1 &x, const T2 &y, unsigned int width)
2989 unsigned int precision = get_binary_precision (x, x);
2990 if (width == 0)
2991 width = precision;
2992 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
2993 WI_UNARY_RESULT (T1) right = wi::lrshift (x, ymod);
2994 WI_UNARY_RESULT (T1) left = wi::lshift (x, wi::sub (width, ymod));
2995 if (width != precision)
2996 return wi::zext (left, width) | wi::zext (right, width);
2997 return left | right;
3000 /* Return 0 if the number of 1s in X is even and 1 if the number of 1s
3001 is odd. */
3002 inline int
3003 wi::parity (const wide_int_ref &x)
3005 return popcount (x) & 1;
3008 /* Extract WIDTH bits from X, starting at BITPOS. */
3009 template <typename T>
3010 inline unsigned HOST_WIDE_INT
3011 wi::extract_uhwi (const T &x, unsigned int bitpos, unsigned int width)
3013 unsigned precision = get_precision (x);
3014 if (precision < bitpos + width)
3015 precision = bitpos + width;
3016 WIDE_INT_REF_FOR (T) xi (x, precision);
3018 /* Handle this rare case after the above, so that we assert about
3019 bogus BITPOS values. */
3020 if (width == 0)
3021 return 0;
3023 unsigned int start = bitpos / HOST_BITS_PER_WIDE_INT;
3024 unsigned int shift = bitpos % HOST_BITS_PER_WIDE_INT;
3025 unsigned HOST_WIDE_INT res = xi.elt (start);
3026 res >>= shift;
3027 if (shift + width > HOST_BITS_PER_WIDE_INT)
3029 unsigned HOST_WIDE_INT upper = xi.elt (start + 1);
3030 res |= upper << (-shift % HOST_BITS_PER_WIDE_INT);
3032 return zext_hwi (res, width);
3035 /* Return the minimum precision needed to store X with sign SGN. */
3036 template <typename T>
3037 inline unsigned int
3038 wi::min_precision (const T &x, signop sgn)
3040 if (sgn == SIGNED)
3041 return get_precision (x) - clrsb (x);
3042 else
3043 return get_precision (x) - clz (x);
3046 template<typename T>
3047 void
3048 gt_ggc_mx (generic_wide_int <T> *)
3052 template<typename T>
3053 void
3054 gt_pch_nx (generic_wide_int <T> *)
3058 template<typename T>
3059 void
3060 gt_pch_nx (generic_wide_int <T> *, void (*) (void *, void *), void *)
3064 template<int N>
3065 void
3066 gt_ggc_mx (trailing_wide_ints <N> *)
3070 template<int N>
3071 void
3072 gt_pch_nx (trailing_wide_ints <N> *)
3076 template<int N>
3077 void
3078 gt_pch_nx (trailing_wide_ints <N> *, void (*) (void *, void *), void *)
3082 namespace wi
3084 /* Used for overloaded functions in which the only other acceptable
3085 scalar type is a pointer. It stops a plain 0 from being treated
3086 as a null pointer. */
3087 struct never_used1 {};
3088 struct never_used2 {};
3090 wide_int min_value (unsigned int, signop);
3091 wide_int min_value (never_used1 *);
3092 wide_int min_value (never_used2 *);
3093 wide_int max_value (unsigned int, signop);
3094 wide_int max_value (never_used1 *);
3095 wide_int max_value (never_used2 *);
3097 /* FIXME: this is target dependent, so should be elsewhere.
3098 It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
3099 wide_int from_buffer (const unsigned char *, unsigned int);
3101 #ifndef GENERATOR_FILE
3102 void to_mpz (const wide_int_ref &, mpz_t, signop);
3103 #endif
3105 wide_int mask (unsigned int, bool, unsigned int);
3106 wide_int shifted_mask (unsigned int, unsigned int, bool, unsigned int);
3107 wide_int set_bit_in_zero (unsigned int, unsigned int);
3108 wide_int insert (const wide_int &x, const wide_int &y, unsigned int,
3109 unsigned int);
3111 template <typename T>
3112 T mask (unsigned int, bool);
3114 template <typename T>
3115 T shifted_mask (unsigned int, unsigned int, bool);
3117 template <typename T>
3118 T set_bit_in_zero (unsigned int);
3120 unsigned int mask (HOST_WIDE_INT *, unsigned int, bool, unsigned int);
3121 unsigned int shifted_mask (HOST_WIDE_INT *, unsigned int, unsigned int,
3122 bool, unsigned int);
3123 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
3124 unsigned int, unsigned int, bool);
3127 /* Return a PRECISION-bit integer in which the low WIDTH bits are set
3128 and the other bits are clear, or the inverse if NEGATE_P. */
3129 inline wide_int
3130 wi::mask (unsigned int width, bool negate_p, unsigned int precision)
3132 wide_int result = wide_int::create (precision);
3133 result.set_len (mask (result.write_val (), width, negate_p, precision));
3134 return result;
3137 /* Return a PRECISION-bit integer in which the low START bits are clear,
3138 the next WIDTH bits are set, and the other bits are clear,
3139 or the inverse if NEGATE_P. */
3140 inline wide_int
3141 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p,
3142 unsigned int precision)
3144 wide_int result = wide_int::create (precision);
3145 result.set_len (shifted_mask (result.write_val (), start, width, negate_p,
3146 precision));
3147 return result;
3150 /* Return a PRECISION-bit integer in which bit BIT is set and all the
3151 others are clear. */
3152 inline wide_int
3153 wi::set_bit_in_zero (unsigned int bit, unsigned int precision)
3155 return shifted_mask (bit, 1, false, precision);
3158 /* Return an integer of type T in which the low WIDTH bits are set
3159 and the other bits are clear, or the inverse if NEGATE_P. */
3160 template <typename T>
3161 inline T
3162 wi::mask (unsigned int width, bool negate_p)
3164 STATIC_ASSERT (wi::int_traits<T>::precision);
3165 T result;
3166 result.set_len (mask (result.write_val (), width, negate_p,
3167 wi::int_traits <T>::precision));
3168 return result;
3171 /* Return an integer of type T in which the low START bits are clear,
3172 the next WIDTH bits are set, and the other bits are clear, or the
3173 inverse if NEGATE_P. */
3174 template <typename T>
3175 inline T
3176 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p)
3178 STATIC_ASSERT (wi::int_traits<T>::precision);
3179 T result;
3180 result.set_len (shifted_mask (result.write_val (), start, width,
3181 negate_p,
3182 wi::int_traits <T>::precision));
3183 return result;
3186 /* Return an integer of type T in which bit BIT is set and all the
3187 others are clear. */
3188 template <typename T>
3189 inline T
3190 wi::set_bit_in_zero (unsigned int bit)
3192 return shifted_mask <T> (bit, 1, false);
3195 #endif /* WIDE_INT_H */