014-05-09 David Wohlferd <LimeGreenSocks@yahoo.com> Andrew Haley...
[official-gcc.git] / gcc / wide-int.h
blob46a1c22998fa7b5c6970725f8db7faf35f2354a9
1 /* Operations with very long integers. -*- C++ -*-
2 Copyright (C) 2012-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef WIDE_INT_H
21 #define WIDE_INT_H
23 /* wide-int.[cc|h] implements a class that efficiently performs
24 mathematical operations on finite precision integers. wide_ints
25 are designed to be transient - they are not for long term storage
26 of values. There is tight integration between wide_ints and the
27 other longer storage GCC representations (rtl and tree).
29 The actual precision of a wide_int depends on the flavor. There
30 are three predefined flavors:
32 1) wide_int (the default). This flavor does the math in the
33 precision of its input arguments. It is assumed (and checked)
34 that the precisions of the operands and results are consistent.
35 This is the most efficient flavor. It is not possible to examine
36 bits above the precision that has been specified. Because of
37 this, the default flavor has semantics that are simple to
38 understand and in general model the underlying hardware that the
39 compiler is targetted for.
41 This flavor must be used at the RTL level of gcc because there
42 is, in general, not enough information in the RTL representation
43 to extend a value beyond the precision specified in the mode.
45 This flavor should also be used at the TREE and GIMPLE levels of
46 the compiler except for the circumstances described in the
47 descriptions of the other two flavors.
49 The default wide_int representation does not contain any
50 information inherent about signedness of the represented value,
51 so it can be used to represent both signed and unsigned numbers.
52 For operations where the results depend on signedness (full width
53 multiply, division, shifts, comparisons, and operations that need
54 overflow detected), the signedness must be specified separately.
56 2) offset_int. This is a fixed size representation that is
57 guaranteed to be large enough to compute any bit or byte sized
58 address calculation on the target. Currently the value is 64 + 4
59 bits rounded up to the next number even multiple of
60 HOST_BITS_PER_WIDE_INT (but this can be changed when the first
61 port needs more than 64 bits for the size of a pointer).
63 This flavor can be used for all address math on the target. In
64 this representation, the values are sign or zero extended based
65 on their input types to the internal precision. All math is done
66 in this precision and then the values are truncated to fit in the
67 result type. Unlike most gimple or rtl intermediate code, it is
68 not useful to perform the address arithmetic at the same
69 precision in which the operands are represented because there has
70 been no effort by the front ends to convert most addressing
71 arithmetic to canonical types.
73 3) widest_int. This representation is an approximation of
74 infinite precision math. However, it is not really infinite
75 precision math as in the GMP library. It is really finite
76 precision math where the precision is 4 times the size of the
77 largest integer that the target port can represent.
79 widest_int is supposed to be wider than any number that it needs to
80 store, meaning that there is always at least one leading sign bit.
81 All widest_int values are therefore signed.
83 There are several places in the GCC where this should/must be used:
85 * Code that does induction variable optimizations. This code
86 works with induction variables of many different types at the
87 same time. Because of this, it ends up doing many different
88 calculations where the operands are not compatible types. The
89 widest_int makes this easy, because it provides a field where
90 nothing is lost when converting from any variable,
92 * There are a small number of passes that currently use the
93 widest_int that should use the default. These should be
94 changed.
96 There are surprising features of offset_int and widest_int
97 that the users should be careful about:
99 1) Shifts and rotations are just weird. You have to specify a
100 precision in which the shift or rotate is to happen in. The bits
101 above this precision are zeroed. While this is what you
102 want, it is clearly non obvious.
104 2) Larger precision math sometimes does not produce the same
105 answer as would be expected for doing the math at the proper
106 precision. In particular, a multiply followed by a divide will
107 produce a different answer if the first product is larger than
108 what can be represented in the input precision.
110 The offset_int and the widest_int flavors are more expensive
111 than the default wide int, so in addition to the caveats with these
112 two, the default is the prefered representation.
114 All three flavors of wide_int are represented as a vector of
115 HOST_WIDE_INTs. The default and widest_int vectors contain enough elements
116 to hold a value of MAX_BITSIZE_MODE_ANY_INT bits. offset_int contains only
117 enough elements to hold ADDR_MAX_PRECISION bits. The values are stored
118 in the vector with the least significant HOST_BITS_PER_WIDE_INT bits
119 in element 0.
121 The default wide_int contains three fields: the vector (VAL),
122 the precision and a length (LEN). The length is the number of HWIs
123 needed to represent the value. widest_int and offset_int have a
124 constant precision that cannot be changed, so they only store the
125 VAL and LEN fields.
127 Since most integers used in a compiler are small values, it is
128 generally profitable to use a representation of the value that is
129 as small as possible. LEN is used to indicate the number of
130 elements of the vector that are in use. The numbers are stored as
131 sign extended numbers as a means of compression. Leading
132 HOST_WIDE_INTs that contain strings of either -1 or 0 are removed
133 as long as they can be reconstructed from the top bit that is being
134 represented.
136 The precision and length of a wide_int are always greater than 0.
137 Any bits in a wide_int above the precision are sign-extended from the
138 most significant bit. For example, a 4-bit value 0x8 is represented as
139 VAL = { 0xf...fff8 }. However, as an optimization, we allow other integer
140 constants to be represented with undefined bits above the precision.
141 This allows INTEGER_CSTs to be pre-extended according to TYPE_SIGN,
142 so that the INTEGER_CST representation can be used both in TYPE_PRECISION
143 and in wider precisions.
145 There are constructors to create the various forms of wide_int from
146 trees, rtl and constants. For trees you can simply say:
148 tree t = ...;
149 wide_int x = t;
151 However, a little more syntax is required for rtl constants since
152 they do not have an explicit precision. To make an rtl into a
153 wide_int, you have to pair it with a mode. The canonical way to do
154 this is with std::make_pair as in:
156 rtx r = ...
157 wide_int x = std::make_pair (r, mode);
159 Similarly, a wide_int can only be constructed from a host value if
160 the target precision is given explicitly, such as in:
162 wide_int x = wi::shwi (c, prec); // sign-extend C if necessary
163 wide_int y = wi::uhwi (c, prec); // zero-extend C if necessary
165 However, offset_int and widest_int have an inherent precision and so
166 can be initialized directly from a host value:
168 offset_int x = (int) c; // sign-extend C
169 widest_int x = (unsigned int) c; // zero-extend C
171 It is also possible to do arithmetic directly on trees, rtxes and
172 constants. For example:
174 wi::add (t1, t2); // add equal-sized INTEGER_CSTs t1 and t2
175 wi::add (t1, 1); // add 1 to INTEGER_CST t1
176 wi::add (r1, r2); // add equal-sized rtx constants r1 and r2
177 wi::lshift (1, 100); // 1 << 100 as a widest_int
179 Many binary operations place restrictions on the combinations of inputs,
180 using the following rules:
182 - {tree, rtx, wide_int} op {tree, rtx, wide_int} -> wide_int
183 The inputs must be the same precision. The result is a wide_int
184 of the same precision
186 - {tree, rtx, wide_int} op (un)signed HOST_WIDE_INT -> wide_int
187 (un)signed HOST_WIDE_INT op {tree, rtx, wide_int} -> wide_int
188 The HOST_WIDE_INT is extended or truncated to the precision of
189 the other input. The result is a wide_int of the same precision
190 as that input.
192 - (un)signed HOST_WIDE_INT op (un)signed HOST_WIDE_INT -> widest_int
193 The inputs are extended to widest_int precision and produce a
194 widest_int result.
196 - offset_int op offset_int -> offset_int
197 offset_int op (un)signed HOST_WIDE_INT -> offset_int
198 (un)signed HOST_WIDE_INT op offset_int -> offset_int
200 - widest_int op widest_int -> widest_int
201 widest_int op (un)signed HOST_WIDE_INT -> widest_int
202 (un)signed HOST_WIDE_INT op widest_int -> widest_int
204 Other combinations like:
206 - widest_int op offset_int and
207 - wide_int op offset_int
209 are not allowed. The inputs should instead be extended or truncated
210 so that they match.
212 The inputs to comparison functions like wi::eq_p and wi::lts_p
213 follow the same compatibility rules, although their return types
214 are different. Unary functions on X produce the same result as
215 a binary operation X + X. Shift functions X op Y also produce
216 the same result as X + X; the precision of the shift amount Y
217 can be arbitrarily different from X. */
220 #include <utility>
221 #include "system.h"
222 #include "hwint.h"
223 #include "signop.h"
224 #include "insn-modes.h"
226 /* The MAX_BITSIZE_MODE_ANY_INT is automatically generated by a very
227 early examination of the target's mode file. The WIDE_INT_MAX_ELTS
228 can accomodate at least 1 more bit so that unsigned numbers of that
229 mode can be represented as a signed value. Note that it is still
230 possible to create fixed_wide_ints that have precisions greater than
231 MAX_BITSIZE_MODE_ANY_INT. This can be useful when representing a
232 double-width multiplication result, for example. */
233 #define WIDE_INT_MAX_ELTS \
234 ((MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT) / HOST_BITS_PER_WIDE_INT)
236 #define WIDE_INT_MAX_PRECISION (WIDE_INT_MAX_ELTS * HOST_BITS_PER_WIDE_INT)
238 /* This is the max size of any pointer on any machine. It does not
239 seem to be as easy to sniff this out of the machine description as
240 it is for MAX_BITSIZE_MODE_ANY_INT since targets may support
241 multiple address sizes and may have different address sizes for
242 different address spaces. However, currently the largest pointer
243 on any platform is 64 bits. When that changes, then it is likely
244 that a target hook should be defined so that targets can make this
245 value larger for those targets. */
246 #define ADDR_MAX_BITSIZE 64
248 /* This is the internal precision used when doing any address
249 arithmetic. The '4' is really 3 + 1. Three of the bits are for
250 the number of extra bits needed to do bit addresses and the other bit
251 is to allow everything to be signed without loosing any precision.
252 Then everything is rounded up to the next HWI for efficiency. */
253 #define ADDR_MAX_PRECISION \
254 ((ADDR_MAX_BITSIZE + 4 + HOST_BITS_PER_WIDE_INT - 1) \
255 & ~(HOST_BITS_PER_WIDE_INT - 1))
257 /* The number of HWIs needed to store an offset_int. */
258 #define OFFSET_INT_ELTS (ADDR_MAX_PRECISION / HOST_BITS_PER_WIDE_INT)
260 /* The type of result produced by a binary operation on types T1 and T2.
261 Defined purely for brevity. */
262 #define WI_BINARY_RESULT(T1, T2) \
263 typename wi::binary_traits <T1, T2>::result_type
265 /* The type of result produced by a unary operation on type T. */
266 #define WI_UNARY_RESULT(T) \
267 typename wi::unary_traits <T>::result_type
269 /* Define a variable RESULT to hold the result of a binary operation on
270 X and Y, which have types T1 and T2 respectively. Define VAL to
271 point to the blocks of RESULT. Once the user of the macro has
272 filled in VAL, it should call RESULT.set_len to set the number
273 of initialized blocks. */
274 #define WI_BINARY_RESULT_VAR(RESULT, VAL, T1, X, T2, Y) \
275 WI_BINARY_RESULT (T1, T2) RESULT = \
276 wi::int_traits <WI_BINARY_RESULT (T1, T2)>::get_binary_result (X, Y); \
277 HOST_WIDE_INT *VAL = RESULT.write_val ()
279 /* Similar for the result of a unary operation on X, which has type T. */
280 #define WI_UNARY_RESULT_VAR(RESULT, VAL, T, X) \
281 WI_UNARY_RESULT (T) RESULT = \
282 wi::int_traits <WI_UNARY_RESULT (T)>::get_binary_result (X, X); \
283 HOST_WIDE_INT *VAL = RESULT.write_val ()
285 template <typename T> struct generic_wide_int;
286 template <int N> struct fixed_wide_int_storage;
287 struct wide_int_storage;
289 /* An N-bit integer. Until we can use typedef templates, use this instead. */
290 #define FIXED_WIDE_INT(N) \
291 generic_wide_int < fixed_wide_int_storage <N> >
293 typedef generic_wide_int <wide_int_storage> wide_int;
294 typedef FIXED_WIDE_INT (ADDR_MAX_PRECISION) offset_int;
295 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION) widest_int;
297 template <bool SE>
298 struct wide_int_ref_storage;
300 typedef generic_wide_int <wide_int_ref_storage <false> > wide_int_ref;
302 /* This can be used instead of wide_int_ref if the referenced value is
303 known to have type T. It carries across properties of T's representation,
304 such as whether excess upper bits in a HWI are defined, and can therefore
305 help avoid redundant work.
307 The macro could be replaced with a template typedef, once we're able
308 to use those. */
309 #define WIDE_INT_REF_FOR(T) \
310 generic_wide_int \
311 <wide_int_ref_storage <wi::int_traits <T>::is_sign_extended> >
313 namespace wi
315 /* Classifies an integer based on its precision. */
316 enum precision_type {
317 /* The integer has both a precision and defined signedness. This allows
318 the integer to be converted to any width, since we know whether to fill
319 any extra bits with zeros or signs. */
320 FLEXIBLE_PRECISION,
322 /* The integer has a variable precision but no defined signedness. */
323 VAR_PRECISION,
325 /* The integer has a constant precision (known at GCC compile time)
326 but no defined signedness. */
327 CONST_PRECISION
330 /* This class, which has no default implementation, is expected to
331 provide the following members:
333 static const enum precision_type precision_type;
334 Classifies the type of T.
336 static const unsigned int precision;
337 Only defined if precision_type == CONST_PRECISION. Specifies the
338 precision of all integers of type T.
340 static const bool host_dependent_precision;
341 True if the precision of T depends (or can depend) on the host.
343 static unsigned int get_precision (const T &x)
344 Return the number of bits in X.
346 static wi::storage_ref *decompose (HOST_WIDE_INT *scratch,
347 unsigned int precision, const T &x)
348 Decompose X as a PRECISION-bit integer, returning the associated
349 wi::storage_ref. SCRATCH is available as scratch space if needed.
350 The routine should assert that PRECISION is acceptable. */
351 template <typename T> struct int_traits;
353 /* This class provides a single type, result_type, which specifies the
354 type of integer produced by a binary operation whose inputs have
355 types T1 and T2. The definition should be symmetric. */
356 template <typename T1, typename T2,
357 enum precision_type P1 = int_traits <T1>::precision_type,
358 enum precision_type P2 = int_traits <T2>::precision_type>
359 struct binary_traits;
361 /* The result of a unary operation on T is the same as the result of
362 a binary operation on two values of type T. */
363 template <typename T>
364 struct unary_traits : public binary_traits <T, T> {};
366 /* Specify the result type for each supported combination of binary
367 inputs. Note that CONST_PRECISION and VAR_PRECISION cannot be
368 mixed, in order to give stronger type checking. When both inputs
369 are CONST_PRECISION, they must have the same precision. */
370 template <>
371 template <typename T1, typename T2>
372 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, FLEXIBLE_PRECISION>
374 typedef widest_int result_type;
377 template <>
378 template <typename T1, typename T2>
379 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, VAR_PRECISION>
381 typedef wide_int result_type;
384 template <>
385 template <typename T1, typename T2>
386 struct binary_traits <T1, T2, FLEXIBLE_PRECISION, CONST_PRECISION>
388 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
389 so as not to confuse gengtype. */
390 typedef generic_wide_int < fixed_wide_int_storage
391 <int_traits <T2>::precision> > result_type;
394 template <>
395 template <typename T1, typename T2>
396 struct binary_traits <T1, T2, VAR_PRECISION, FLEXIBLE_PRECISION>
398 typedef wide_int result_type;
401 template <>
402 template <typename T1, typename T2>
403 struct binary_traits <T1, T2, CONST_PRECISION, FLEXIBLE_PRECISION>
405 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
406 so as not to confuse gengtype. */
407 typedef generic_wide_int < fixed_wide_int_storage
408 <int_traits <T1>::precision> > result_type;
411 template <>
412 template <typename T1, typename T2>
413 struct binary_traits <T1, T2, CONST_PRECISION, CONST_PRECISION>
415 /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
416 so as not to confuse gengtype. */
417 STATIC_ASSERT (int_traits <T1>::precision == int_traits <T2>::precision);
418 typedef generic_wide_int < fixed_wide_int_storage
419 <int_traits <T1>::precision> > result_type;
422 template <>
423 template <typename T1, typename T2>
424 struct binary_traits <T1, T2, VAR_PRECISION, VAR_PRECISION>
426 typedef wide_int result_type;
430 /* Public functions for querying and operating on integers. */
431 namespace wi
433 template <typename T>
434 unsigned int get_precision (const T &);
436 template <typename T1, typename T2>
437 unsigned int get_binary_precision (const T1 &, const T2 &);
439 template <typename T1, typename T2>
440 void copy (T1 &, const T2 &);
442 #define UNARY_PREDICATE \
443 template <typename T> bool
444 #define UNARY_FUNCTION \
445 template <typename T> WI_UNARY_RESULT (T)
446 #define BINARY_PREDICATE \
447 template <typename T1, typename T2> bool
448 #define BINARY_FUNCTION \
449 template <typename T1, typename T2> WI_BINARY_RESULT (T1, T2)
450 #define SHIFT_FUNCTION \
451 template <typename T1, typename T2> WI_UNARY_RESULT (T1)
453 UNARY_PREDICATE fits_shwi_p (const T &);
454 UNARY_PREDICATE fits_uhwi_p (const T &);
455 UNARY_PREDICATE neg_p (const T &, signop = SIGNED);
457 template <typename T>
458 HOST_WIDE_INT sign_mask (const T &);
460 BINARY_PREDICATE eq_p (const T1 &, const T2 &);
461 BINARY_PREDICATE ne_p (const T1 &, const T2 &);
462 BINARY_PREDICATE lt_p (const T1 &, const T2 &, signop);
463 BINARY_PREDICATE lts_p (const T1 &, const T2 &);
464 BINARY_PREDICATE ltu_p (const T1 &, const T2 &);
465 BINARY_PREDICATE le_p (const T1 &, const T2 &, signop);
466 BINARY_PREDICATE les_p (const T1 &, const T2 &);
467 BINARY_PREDICATE leu_p (const T1 &, const T2 &);
468 BINARY_PREDICATE gt_p (const T1 &, const T2 &, signop);
469 BINARY_PREDICATE gts_p (const T1 &, const T2 &);
470 BINARY_PREDICATE gtu_p (const T1 &, const T2 &);
471 BINARY_PREDICATE ge_p (const T1 &, const T2 &, signop);
472 BINARY_PREDICATE ges_p (const T1 &, const T2 &);
473 BINARY_PREDICATE geu_p (const T1 &, const T2 &);
475 template <typename T1, typename T2>
476 int cmp (const T1 &, const T2 &, signop);
478 template <typename T1, typename T2>
479 int cmps (const T1 &, const T2 &);
481 template <typename T1, typename T2>
482 int cmpu (const T1 &, const T2 &);
484 UNARY_FUNCTION bit_not (const T &);
485 UNARY_FUNCTION neg (const T &);
486 UNARY_FUNCTION neg (const T &, bool *);
487 UNARY_FUNCTION abs (const T &);
488 UNARY_FUNCTION ext (const T &, unsigned int, signop);
489 UNARY_FUNCTION sext (const T &, unsigned int);
490 UNARY_FUNCTION zext (const T &, unsigned int);
491 UNARY_FUNCTION set_bit (const T &, unsigned int);
493 BINARY_FUNCTION min (const T1 &, const T2 &, signop);
494 BINARY_FUNCTION smin (const T1 &, const T2 &);
495 BINARY_FUNCTION umin (const T1 &, const T2 &);
496 BINARY_FUNCTION max (const T1 &, const T2 &, signop);
497 BINARY_FUNCTION smax (const T1 &, const T2 &);
498 BINARY_FUNCTION umax (const T1 &, const T2 &);
500 BINARY_FUNCTION bit_and (const T1 &, const T2 &);
501 BINARY_FUNCTION bit_and_not (const T1 &, const T2 &);
502 BINARY_FUNCTION bit_or (const T1 &, const T2 &);
503 BINARY_FUNCTION bit_or_not (const T1 &, const T2 &);
504 BINARY_FUNCTION bit_xor (const T1 &, const T2 &);
505 BINARY_FUNCTION add (const T1 &, const T2 &);
506 BINARY_FUNCTION add (const T1 &, const T2 &, signop, bool *);
507 BINARY_FUNCTION sub (const T1 &, const T2 &);
508 BINARY_FUNCTION sub (const T1 &, const T2 &, signop, bool *);
509 BINARY_FUNCTION mul (const T1 &, const T2 &);
510 BINARY_FUNCTION mul (const T1 &, const T2 &, signop, bool *);
511 BINARY_FUNCTION smul (const T1 &, const T2 &, bool *);
512 BINARY_FUNCTION umul (const T1 &, const T2 &, bool *);
513 BINARY_FUNCTION mul_high (const T1 &, const T2 &, signop);
514 BINARY_FUNCTION div_trunc (const T1 &, const T2 &, signop, bool * = 0);
515 BINARY_FUNCTION sdiv_trunc (const T1 &, const T2 &);
516 BINARY_FUNCTION udiv_trunc (const T1 &, const T2 &);
517 BINARY_FUNCTION div_floor (const T1 &, const T2 &, signop, bool * = 0);
518 BINARY_FUNCTION udiv_floor (const T1 &, const T2 &);
519 BINARY_FUNCTION sdiv_floor (const T1 &, const T2 &);
520 BINARY_FUNCTION div_ceil (const T1 &, const T2 &, signop, bool * = 0);
521 BINARY_FUNCTION div_round (const T1 &, const T2 &, signop, bool * = 0);
522 BINARY_FUNCTION divmod_trunc (const T1 &, const T2 &, signop,
523 WI_BINARY_RESULT (T1, T2) *);
524 BINARY_FUNCTION mod_trunc (const T1 &, const T2 &, signop, bool * = 0);
525 BINARY_FUNCTION smod_trunc (const T1 &, const T2 &);
526 BINARY_FUNCTION umod_trunc (const T1 &, const T2 &);
527 BINARY_FUNCTION mod_floor (const T1 &, const T2 &, signop, bool * = 0);
528 BINARY_FUNCTION umod_floor (const T1 &, const T2 &);
529 BINARY_FUNCTION mod_ceil (const T1 &, const T2 &, signop, bool * = 0);
530 BINARY_FUNCTION mod_round (const T1 &, const T2 &, signop, bool * = 0);
532 template <typename T1, typename T2>
533 bool multiple_of_p (const T1 &, const T2 &, signop,
534 WI_BINARY_RESULT (T1, T2) *);
536 SHIFT_FUNCTION lshift (const T1 &, const T2 &);
537 SHIFT_FUNCTION lrshift (const T1 &, const T2 &);
538 SHIFT_FUNCTION arshift (const T1 &, const T2 &);
539 SHIFT_FUNCTION rshift (const T1 &, const T2 &, signop sgn);
540 SHIFT_FUNCTION lrotate (const T1 &, const T2 &, unsigned int = 0);
541 SHIFT_FUNCTION rrotate (const T1 &, const T2 &, unsigned int = 0);
543 #undef SHIFT_FUNCTION
544 #undef BINARY_PREDICATE
545 #undef BINARY_FUNCTION
546 #undef UNARY_PREDICATE
547 #undef UNARY_FUNCTION
549 bool only_sign_bit_p (const wide_int_ref &, unsigned int);
550 bool only_sign_bit_p (const wide_int_ref &);
551 int clz (const wide_int_ref &);
552 int clrsb (const wide_int_ref &);
553 int ctz (const wide_int_ref &);
554 int exact_log2 (const wide_int_ref &);
555 int floor_log2 (const wide_int_ref &);
556 int ffs (const wide_int_ref &);
557 int popcount (const wide_int_ref &);
558 int parity (const wide_int_ref &);
560 template <typename T>
561 unsigned HOST_WIDE_INT extract_uhwi (const T &, unsigned int, unsigned int);
563 template <typename T>
564 unsigned int min_precision (const T &, signop);
567 namespace wi
569 /* Contains the components of a decomposed integer for easy, direct
570 access. */
571 struct storage_ref
573 storage_ref (const HOST_WIDE_INT *, unsigned int, unsigned int);
575 const HOST_WIDE_INT *val;
576 unsigned int len;
577 unsigned int precision;
579 /* Provide enough trappings for this class to act as storage for
580 generic_wide_int. */
581 unsigned int get_len () const;
582 unsigned int get_precision () const;
583 const HOST_WIDE_INT *get_val () const;
587 inline::wi::storage_ref::storage_ref (const HOST_WIDE_INT *val_in,
588 unsigned int len_in,
589 unsigned int precision_in)
590 : val (val_in), len (len_in), precision (precision_in)
594 inline unsigned int
595 wi::storage_ref::get_len () const
597 return len;
600 inline unsigned int
601 wi::storage_ref::get_precision () const
603 return precision;
606 inline const HOST_WIDE_INT *
607 wi::storage_ref::get_val () const
609 return val;
612 /* This class defines an integer type using the storage provided by the
613 template argument. The storage class must provide the following
614 functions:
616 unsigned int get_precision () const
617 Return the number of bits in the integer.
619 HOST_WIDE_INT *get_val () const
620 Return a pointer to the array of blocks that encodes the integer.
622 unsigned int get_len () const
623 Return the number of blocks in get_val (). If this is smaller
624 than the number of blocks implied by get_precision (), the
625 remaining blocks are sign extensions of block get_len () - 1.
627 Although not required by generic_wide_int itself, writable storage
628 classes can also provide the following functions:
630 HOST_WIDE_INT *write_val ()
631 Get a modifiable version of get_val ()
633 unsigned int set_len (unsigned int len)
634 Set the value returned by get_len () to LEN. */
635 template <typename storage>
636 class GTY(()) generic_wide_int : public storage
638 public:
639 generic_wide_int ();
641 template <typename T>
642 generic_wide_int (const T &);
644 template <typename T>
645 generic_wide_int (const T &, unsigned int);
647 /* Conversions. */
648 HOST_WIDE_INT to_shwi (unsigned int) const;
649 HOST_WIDE_INT to_shwi () const;
650 unsigned HOST_WIDE_INT to_uhwi (unsigned int) const;
651 unsigned HOST_WIDE_INT to_uhwi () const;
652 HOST_WIDE_INT to_short_addr () const;
654 /* Public accessors for the interior of a wide int. */
655 HOST_WIDE_INT sign_mask () const;
656 HOST_WIDE_INT elt (unsigned int) const;
657 unsigned HOST_WIDE_INT ulow () const;
658 unsigned HOST_WIDE_INT uhigh () const;
659 HOST_WIDE_INT slow () const;
660 HOST_WIDE_INT shigh () const;
662 template <typename T>
663 generic_wide_int &operator = (const T &);
665 #define BINARY_PREDICATE(OP, F) \
666 template <typename T> \
667 bool OP (const T &c) const { return wi::F (*this, c); }
669 #define UNARY_OPERATOR(OP, F) \
670 WI_UNARY_RESULT (generic_wide_int) OP () const { return wi::F (*this); }
672 #define BINARY_OPERATOR(OP, F) \
673 template <typename T> \
674 WI_BINARY_RESULT (generic_wide_int, T) \
675 OP (const T &c) const { return wi::F (*this, c); }
677 #define ASSIGNMENT_OPERATOR(OP, F) \
678 template <typename T> \
679 generic_wide_int &OP (const T &c) { return (*this = wi::F (*this, c)); }
681 #define INCDEC_OPERATOR(OP, DELTA) \
682 generic_wide_int &OP () { *this += DELTA; return *this; }
684 UNARY_OPERATOR (operator ~, bit_not)
685 UNARY_OPERATOR (operator -, neg)
686 BINARY_PREDICATE (operator ==, eq_p)
687 BINARY_PREDICATE (operator !=, ne_p)
688 BINARY_OPERATOR (operator &, bit_and)
689 BINARY_OPERATOR (and_not, bit_and_not)
690 BINARY_OPERATOR (operator |, bit_or)
691 BINARY_OPERATOR (or_not, bit_or_not)
692 BINARY_OPERATOR (operator ^, bit_xor)
693 BINARY_OPERATOR (operator +, add)
694 BINARY_OPERATOR (operator -, sub)
695 BINARY_OPERATOR (operator *, mul)
696 ASSIGNMENT_OPERATOR (operator &=, bit_and)
697 ASSIGNMENT_OPERATOR (operator |=, bit_or)
698 ASSIGNMENT_OPERATOR (operator ^=, bit_xor)
699 ASSIGNMENT_OPERATOR (operator +=, add)
700 ASSIGNMENT_OPERATOR (operator -=, sub)
701 ASSIGNMENT_OPERATOR (operator *=, mul)
702 INCDEC_OPERATOR (operator ++, 1)
703 INCDEC_OPERATOR (operator --, -1)
705 #undef BINARY_PREDICATE
706 #undef UNARY_OPERATOR
707 #undef BINARY_OPERATOR
708 #undef ASSIGNMENT_OPERATOR
709 #undef INCDEC_OPERATOR
711 /* Debugging functions. */
712 void dump () const;
714 static const bool is_sign_extended
715 = wi::int_traits <generic_wide_int <storage> >::is_sign_extended;
718 template <typename storage>
719 inline generic_wide_int <storage>::generic_wide_int () {}
721 template <typename storage>
722 template <typename T>
723 inline generic_wide_int <storage>::generic_wide_int (const T &x)
724 : storage (x)
728 template <typename storage>
729 template <typename T>
730 inline generic_wide_int <storage>::generic_wide_int (const T &x,
731 unsigned int precision)
732 : storage (x, precision)
736 /* Return THIS as a signed HOST_WIDE_INT, sign-extending from PRECISION.
737 If THIS does not fit in PRECISION, the information is lost. */
738 template <typename storage>
739 inline HOST_WIDE_INT
740 generic_wide_int <storage>::to_shwi (unsigned int precision) const
742 if (precision < HOST_BITS_PER_WIDE_INT)
743 return sext_hwi (this->get_val ()[0], precision);
744 else
745 return this->get_val ()[0];
748 /* Return THIS as a signed HOST_WIDE_INT, in its natural precision. */
749 template <typename storage>
750 inline HOST_WIDE_INT
751 generic_wide_int <storage>::to_shwi () const
753 if (is_sign_extended)
754 return this->get_val ()[0];
755 else
756 return to_shwi (this->get_precision ());
759 /* Return THIS as an unsigned HOST_WIDE_INT, zero-extending from
760 PRECISION. If THIS does not fit in PRECISION, the information
761 is lost. */
762 template <typename storage>
763 inline unsigned HOST_WIDE_INT
764 generic_wide_int <storage>::to_uhwi (unsigned int precision) const
766 if (precision < HOST_BITS_PER_WIDE_INT)
767 return zext_hwi (this->get_val ()[0], precision);
768 else
769 return this->get_val ()[0];
772 /* Return THIS as an signed HOST_WIDE_INT, in its natural precision. */
773 template <typename storage>
774 inline unsigned HOST_WIDE_INT
775 generic_wide_int <storage>::to_uhwi () const
777 return to_uhwi (this->get_precision ());
780 /* TODO: The compiler is half converted from using HOST_WIDE_INT to
781 represent addresses to using offset_int to represent addresses.
782 We use to_short_addr at the interface from new code to old,
783 unconverted code. */
784 template <typename storage>
785 inline HOST_WIDE_INT
786 generic_wide_int <storage>::to_short_addr () const
788 return this->get_val ()[0];
791 /* Return the implicit value of blocks above get_len (). */
792 template <typename storage>
793 inline HOST_WIDE_INT
794 generic_wide_int <storage>::sign_mask () const
796 unsigned int len = this->get_len ();
797 unsigned HOST_WIDE_INT high = this->get_val ()[len - 1];
798 if (!is_sign_extended)
800 unsigned int precision = this->get_precision ();
801 int excess = len * HOST_BITS_PER_WIDE_INT - precision;
802 if (excess > 0)
803 high <<= excess;
805 return (HOST_WIDE_INT) (high) < 0 ? -1 : 0;
808 /* Return the signed value of the least-significant explicitly-encoded
809 block. */
810 template <typename storage>
811 inline HOST_WIDE_INT
812 generic_wide_int <storage>::slow () const
814 return this->get_val ()[0];
817 /* Return the signed value of the most-significant explicitly-encoded
818 block. */
819 template <typename storage>
820 inline HOST_WIDE_INT
821 generic_wide_int <storage>::shigh () const
823 return this->get_val ()[this->get_len () - 1];
826 /* Return the unsigned value of the least-significant
827 explicitly-encoded block. */
828 template <typename storage>
829 inline unsigned HOST_WIDE_INT
830 generic_wide_int <storage>::ulow () const
832 return this->get_val ()[0];
835 /* Return the unsigned value of the most-significant
836 explicitly-encoded block. */
837 template <typename storage>
838 inline unsigned HOST_WIDE_INT
839 generic_wide_int <storage>::uhigh () const
841 return this->get_val ()[this->get_len () - 1];
844 /* Return block I, which might be implicitly or explicit encoded. */
845 template <typename storage>
846 inline HOST_WIDE_INT
847 generic_wide_int <storage>::elt (unsigned int i) const
849 if (i >= this->get_len ())
850 return sign_mask ();
851 else
852 return this->get_val ()[i];
855 template <typename storage>
856 template <typename T>
857 generic_wide_int <storage> &
858 generic_wide_int <storage>::operator = (const T &x)
860 storage::operator = (x);
861 return *this;
864 /* Dump the contents of the integer to stderr, for debugging. */
865 template <typename storage>
866 void
867 generic_wide_int <storage>::dump () const
869 unsigned int len = this->get_len ();
870 const HOST_WIDE_INT *val = this->get_val ();
871 unsigned int precision = this->get_precision ();
872 fprintf (stderr, "[");
873 if (len * HOST_BITS_PER_WIDE_INT < precision)
874 fprintf (stderr, "...,");
875 for (unsigned int i = 0; i < len - 1; ++i)
876 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX ",", val[len - 1 - i]);
877 fprintf (stderr, HOST_WIDE_INT_PRINT_HEX "], precision = %d\n",
878 val[0], precision);
881 namespace wi
883 template <>
884 template <typename storage>
885 struct int_traits < generic_wide_int <storage> >
886 : public wi::int_traits <storage>
888 static unsigned int get_precision (const generic_wide_int <storage> &);
889 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
890 const generic_wide_int <storage> &);
894 template <typename storage>
895 inline unsigned int
896 wi::int_traits < generic_wide_int <storage> >::
897 get_precision (const generic_wide_int <storage> &x)
899 return x.get_precision ();
902 template <typename storage>
903 inline wi::storage_ref
904 wi::int_traits < generic_wide_int <storage> >::
905 decompose (HOST_WIDE_INT *, unsigned int precision,
906 const generic_wide_int <storage> &x)
908 gcc_checking_assert (precision == x.get_precision ());
909 return wi::storage_ref (x.get_val (), x.get_len (), precision);
912 /* Provide the storage for a wide_int_ref. This acts like a read-only
913 wide_int, with the optimization that VAL is normally a pointer to
914 another integer's storage, so that no array copy is needed. */
915 template <bool SE>
916 struct wide_int_ref_storage : public wi::storage_ref
918 private:
919 /* Scratch space that can be used when decomposing the original integer.
920 It must live as long as this object. */
921 HOST_WIDE_INT scratch[2];
923 public:
924 wide_int_ref_storage (const wi::storage_ref &);
926 template <typename T>
927 wide_int_ref_storage (const T &);
929 template <typename T>
930 wide_int_ref_storage (const T &, unsigned int);
933 /* Create a reference from an existing reference. */
934 template <bool SE>
935 inline wide_int_ref_storage <SE>::
936 wide_int_ref_storage (const wi::storage_ref &x)
937 : storage_ref (x)
940 /* Create a reference to integer X in its natural precision. Note
941 that the natural precision is host-dependent for primitive
942 types. */
943 template <bool SE>
944 template <typename T>
945 inline wide_int_ref_storage <SE>::wide_int_ref_storage (const T &x)
946 : storage_ref (wi::int_traits <T>::decompose (scratch,
947 wi::get_precision (x), x))
951 /* Create a reference to integer X in precision PRECISION. */
952 template <bool SE>
953 template <typename T>
954 inline wide_int_ref_storage <SE>::wide_int_ref_storage (const T &x,
955 unsigned int precision)
956 : storage_ref (wi::int_traits <T>::decompose (scratch, precision, x))
960 namespace wi
962 template <>
963 template <bool SE>
964 struct int_traits <wide_int_ref_storage <SE> >
966 static const enum precision_type precision_type = VAR_PRECISION;
967 /* wi::storage_ref can be a reference to a primitive type,
968 so this is the conservatively-correct setting. */
969 static const bool host_dependent_precision = true;
970 static const bool is_sign_extended = SE;
974 namespace wi
976 unsigned int force_to_size (HOST_WIDE_INT *, const HOST_WIDE_INT *,
977 unsigned int, unsigned int, unsigned int,
978 signop sgn);
979 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
980 unsigned int, unsigned int, bool = true);
983 /* The storage used by wide_int. */
984 class GTY(()) wide_int_storage
986 private:
987 HOST_WIDE_INT val[WIDE_INT_MAX_ELTS];
988 unsigned int len;
989 unsigned int precision;
991 public:
992 wide_int_storage ();
993 template <typename T>
994 wide_int_storage (const T &);
996 /* The standard generic_wide_int storage methods. */
997 unsigned int get_precision () const;
998 const HOST_WIDE_INT *get_val () const;
999 unsigned int get_len () const;
1000 HOST_WIDE_INT *write_val ();
1001 void set_len (unsigned int, bool = false);
1003 static wide_int from (const wide_int_ref &, unsigned int, signop);
1004 static wide_int from_array (const HOST_WIDE_INT *, unsigned int,
1005 unsigned int, bool = true);
1006 static wide_int create (unsigned int);
1008 /* FIXME: target-dependent, so should disappear. */
1009 wide_int bswap () const;
1012 namespace wi
1014 template <>
1015 struct int_traits <wide_int_storage>
1017 static const enum precision_type precision_type = VAR_PRECISION;
1018 /* Guaranteed by a static assert in the wide_int_storage constructor. */
1019 static const bool host_dependent_precision = false;
1020 static const bool is_sign_extended = true;
1021 template <typename T1, typename T2>
1022 static wide_int get_binary_result (const T1 &, const T2 &);
1026 inline wide_int_storage::wide_int_storage () {}
1028 /* Initialize the storage from integer X, in its natural precision.
1029 Note that we do not allow integers with host-dependent precision
1030 to become wide_ints; wide_ints must always be logically independent
1031 of the host. */
1032 template <typename T>
1033 inline wide_int_storage::wide_int_storage (const T &x)
1035 { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision); }
1036 { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION); }
1037 WIDE_INT_REF_FOR (T) xi (x);
1038 precision = xi.precision;
1039 wi::copy (*this, xi);
1042 inline unsigned int
1043 wide_int_storage::get_precision () const
1045 return precision;
1048 inline const HOST_WIDE_INT *
1049 wide_int_storage::get_val () const
1051 return val;
1054 inline unsigned int
1055 wide_int_storage::get_len () const
1057 return len;
1060 inline HOST_WIDE_INT *
1061 wide_int_storage::write_val ()
1063 return val;
1066 inline void
1067 wide_int_storage::set_len (unsigned int l, bool is_sign_extended)
1069 len = l;
1070 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > precision)
1071 val[len - 1] = sext_hwi (val[len - 1],
1072 precision % HOST_BITS_PER_WIDE_INT);
1075 /* Treat X as having signedness SGN and convert it to a PRECISION-bit
1076 number. */
1077 inline wide_int
1078 wide_int_storage::from (const wide_int_ref &x, unsigned int precision,
1079 signop sgn)
1081 wide_int result = wide_int::create (precision);
1082 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1083 x.precision, precision, sgn));
1084 return result;
1087 /* Create a wide_int from the explicit block encoding given by VAL and
1088 LEN. PRECISION is the precision of the integer. NEED_CANON_P is
1089 true if the encoding may have redundant trailing blocks. */
1090 inline wide_int
1091 wide_int_storage::from_array (const HOST_WIDE_INT *val, unsigned int len,
1092 unsigned int precision, bool need_canon_p)
1094 wide_int result = wide_int::create (precision);
1095 result.set_len (wi::from_array (result.write_val (), val, len, precision,
1096 need_canon_p));
1097 return result;
1100 /* Return an uninitialized wide_int with precision PRECISION. */
1101 inline wide_int
1102 wide_int_storage::create (unsigned int precision)
1104 wide_int x;
1105 x.precision = precision;
1106 return x;
1109 template <typename T1, typename T2>
1110 inline wide_int
1111 wi::int_traits <wide_int_storage>::get_binary_result (const T1 &x, const T2 &y)
1113 /* This shouldn't be used for two flexible-precision inputs. */
1114 STATIC_ASSERT (wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION
1115 || wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION);
1116 if (wi::int_traits <T1>::precision_type == FLEXIBLE_PRECISION)
1117 return wide_int::create (wi::get_precision (y));
1118 else
1119 return wide_int::create (wi::get_precision (x));
1122 /* The storage used by FIXED_WIDE_INT (N). */
1123 template <int N>
1124 class GTY(()) fixed_wide_int_storage
1126 private:
1127 HOST_WIDE_INT val[(N + HOST_BITS_PER_WIDE_INT + 1) / HOST_BITS_PER_WIDE_INT];
1128 unsigned int len;
1130 public:
1131 fixed_wide_int_storage ();
1132 template <typename T>
1133 fixed_wide_int_storage (const T &);
1135 /* The standard generic_wide_int storage methods. */
1136 unsigned int get_precision () const;
1137 const HOST_WIDE_INT *get_val () const;
1138 unsigned int get_len () const;
1139 HOST_WIDE_INT *write_val ();
1140 void set_len (unsigned int, bool = false);
1142 static FIXED_WIDE_INT (N) from (const wide_int_ref &, signop);
1143 static FIXED_WIDE_INT (N) from_array (const HOST_WIDE_INT *, unsigned int,
1144 bool = true);
1147 namespace wi
1149 template <>
1150 template <int N>
1151 struct int_traits < fixed_wide_int_storage <N> >
1153 static const enum precision_type precision_type = CONST_PRECISION;
1154 static const bool host_dependent_precision = false;
1155 static const bool is_sign_extended = true;
1156 static const unsigned int precision = N;
1157 template <typename T1, typename T2>
1158 static FIXED_WIDE_INT (N) get_binary_result (const T1 &, const T2 &);
1162 template <int N>
1163 inline fixed_wide_int_storage <N>::fixed_wide_int_storage () {}
1165 /* Initialize the storage from integer X, in precision N. */
1166 template <int N>
1167 template <typename T>
1168 inline fixed_wide_int_storage <N>::fixed_wide_int_storage (const T &x)
1170 /* Check for type compatibility. We don't want to initialize a
1171 fixed-width integer from something like a wide_int. */
1172 WI_BINARY_RESULT (T, FIXED_WIDE_INT (N)) *assertion ATTRIBUTE_UNUSED;
1173 wi::copy (*this, WIDE_INT_REF_FOR (T) (x, N));
1176 template <int N>
1177 inline unsigned int
1178 fixed_wide_int_storage <N>::get_precision () const
1180 return N;
1183 template <int N>
1184 inline const HOST_WIDE_INT *
1185 fixed_wide_int_storage <N>::get_val () const
1187 return val;
1190 template <int N>
1191 inline unsigned int
1192 fixed_wide_int_storage <N>::get_len () const
1194 return len;
1197 template <int N>
1198 inline HOST_WIDE_INT *
1199 fixed_wide_int_storage <N>::write_val ()
1201 return val;
1204 template <int N>
1205 inline void
1206 fixed_wide_int_storage <N>::set_len (unsigned int l, bool)
1208 len = l;
1209 /* There are no excess bits in val[len - 1]. */
1210 STATIC_ASSERT (N % HOST_BITS_PER_WIDE_INT == 0);
1213 /* Treat X as having signedness SGN and convert it to an N-bit number. */
1214 template <int N>
1215 inline FIXED_WIDE_INT (N)
1216 fixed_wide_int_storage <N>::from (const wide_int_ref &x, signop sgn)
1218 FIXED_WIDE_INT (N) result;
1219 result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
1220 x.precision, N, sgn));
1221 return result;
1224 /* Create a FIXED_WIDE_INT (N) from the explicit block encoding given by
1225 VAL and LEN. NEED_CANON_P is true if the encoding may have redundant
1226 trailing blocks. */
1227 template <int N>
1228 inline FIXED_WIDE_INT (N)
1229 fixed_wide_int_storage <N>::from_array (const HOST_WIDE_INT *val,
1230 unsigned int len,
1231 bool need_canon_p)
1233 FIXED_WIDE_INT (N) result;
1234 result.set_len (wi::from_array (result.write_val (), val, len,
1235 N, need_canon_p));
1236 return result;
1239 template <int N>
1240 template <typename T1, typename T2>
1241 inline FIXED_WIDE_INT (N)
1242 wi::int_traits < fixed_wide_int_storage <N> >::
1243 get_binary_result (const T1 &, const T2 &)
1245 return FIXED_WIDE_INT (N) ();
1248 /* A reference to one element of a trailing_wide_ints structure. */
1249 class trailing_wide_int_storage
1251 private:
1252 /* The precision of the integer, which is a fixed property of the
1253 parent trailing_wide_ints. */
1254 unsigned int m_precision;
1256 /* A pointer to the length field. */
1257 unsigned char *m_len;
1259 /* A pointer to the HWI array. There are enough elements to hold all
1260 values of precision M_PRECISION. */
1261 HOST_WIDE_INT *m_val;
1263 public:
1264 trailing_wide_int_storage (unsigned int, unsigned char *, HOST_WIDE_INT *);
1266 /* The standard generic_wide_int storage methods. */
1267 unsigned int get_len () const;
1268 unsigned int get_precision () const;
1269 const HOST_WIDE_INT *get_val () const;
1270 HOST_WIDE_INT *write_val ();
1271 void set_len (unsigned int, bool = false);
1273 template <typename T>
1274 trailing_wide_int_storage &operator = (const T &);
1277 typedef generic_wide_int <trailing_wide_int_storage> trailing_wide_int;
1279 /* trailing_wide_int behaves like a wide_int. */
1280 namespace wi
1282 template <>
1283 struct int_traits <trailing_wide_int_storage>
1284 : public int_traits <wide_int_storage> {};
1287 /* An array of N wide_int-like objects that can be put at the end of
1288 a variable-sized structure. Use extra_size to calculate how many
1289 bytes beyond the sizeof need to be allocated. Use set_precision
1290 to initialize the structure. */
1291 template <int N>
1292 class GTY(()) trailing_wide_ints
1294 private:
1295 /* The shared precision of each number. */
1296 unsigned short m_precision;
1298 /* The shared maximum length of each number. */
1299 unsigned char m_max_len;
1301 /* The current length of each number. */
1302 unsigned char m_len[N];
1304 /* The variable-length part of the structure, which always contains
1305 at least one HWI. Element I starts at index I * M_MAX_LEN. */
1306 HOST_WIDE_INT m_val[1];
1308 public:
1309 void set_precision (unsigned int);
1310 trailing_wide_int operator [] (unsigned int);
1311 static size_t extra_size (unsigned int);
1314 inline trailing_wide_int_storage::
1315 trailing_wide_int_storage (unsigned int precision, unsigned char *len,
1316 HOST_WIDE_INT *val)
1317 : m_precision (precision), m_len (len), m_val (val)
1321 inline unsigned int
1322 trailing_wide_int_storage::get_len () const
1324 return *m_len;
1327 inline unsigned int
1328 trailing_wide_int_storage::get_precision () const
1330 return m_precision;
1333 inline const HOST_WIDE_INT *
1334 trailing_wide_int_storage::get_val () const
1336 return m_val;
1339 inline HOST_WIDE_INT *
1340 trailing_wide_int_storage::write_val ()
1342 return m_val;
1345 inline void
1346 trailing_wide_int_storage::set_len (unsigned int len, bool is_sign_extended)
1348 *m_len = len;
1349 if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > m_precision)
1350 m_val[len - 1] = sext_hwi (m_val[len - 1],
1351 m_precision % HOST_BITS_PER_WIDE_INT);
1354 template <typename T>
1355 inline trailing_wide_int_storage &
1356 trailing_wide_int_storage::operator = (const T &x)
1358 WIDE_INT_REF_FOR (T) xi (x, m_precision);
1359 wi::copy (*this, xi);
1360 return *this;
1363 /* Initialize the structure and record that all elements have precision
1364 PRECISION. */
1365 template <int N>
1366 inline void
1367 trailing_wide_ints <N>::set_precision (unsigned int precision)
1369 m_precision = precision;
1370 m_max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1371 / HOST_BITS_PER_WIDE_INT);
1374 /* Return a reference to element INDEX. */
1375 template <int N>
1376 inline trailing_wide_int
1377 trailing_wide_ints <N>::operator [] (unsigned int index)
1379 return trailing_wide_int_storage (m_precision, &m_len[index],
1380 &m_val[index * m_max_len]);
1383 /* Return how many extra bytes need to be added to the end of the structure
1384 in order to handle N wide_ints of precision PRECISION. */
1385 template <int N>
1386 inline size_t
1387 trailing_wide_ints <N>::extra_size (unsigned int precision)
1389 unsigned int max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
1390 / HOST_BITS_PER_WIDE_INT);
1391 return (N * max_len - 1) * sizeof (HOST_WIDE_INT);
1394 /* This macro is used in structures that end with a trailing_wide_ints field
1395 called FIELD. It declares get_NAME() and set_NAME() methods to access
1396 element I of FIELD. */
1397 #define TRAILING_WIDE_INT_ACCESSOR(NAME, FIELD, I) \
1398 trailing_wide_int get_##NAME () { return FIELD[I]; } \
1399 template <typename T> void set_##NAME (const T &x) { FIELD[I] = x; }
1401 namespace wi
1403 /* Implementation of int_traits for primitive integer types like "int". */
1404 template <typename T, bool signed_p>
1405 struct primitive_int_traits
1407 static const enum precision_type precision_type = FLEXIBLE_PRECISION;
1408 static const bool host_dependent_precision = true;
1409 static const bool is_sign_extended = true;
1410 static unsigned int get_precision (T);
1411 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int, T);
1415 template <typename T, bool signed_p>
1416 inline unsigned int
1417 wi::primitive_int_traits <T, signed_p>::get_precision (T)
1419 return sizeof (T) * CHAR_BIT;
1422 template <typename T, bool signed_p>
1423 inline wi::storage_ref
1424 wi::primitive_int_traits <T, signed_p>::decompose (HOST_WIDE_INT *scratch,
1425 unsigned int precision, T x)
1427 scratch[0] = x;
1428 if (signed_p || scratch[0] >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1429 return wi::storage_ref (scratch, 1, precision);
1430 scratch[1] = 0;
1431 return wi::storage_ref (scratch, 2, precision);
1434 /* Allow primitive C types to be used in wi:: routines. */
1435 namespace wi
1437 template <>
1438 struct int_traits <int>
1439 : public primitive_int_traits <int, true> {};
1441 template <>
1442 struct int_traits <unsigned int>
1443 : public primitive_int_traits <unsigned int, false> {};
1445 template <>
1446 struct int_traits <HOST_WIDE_INT>
1447 : public primitive_int_traits <HOST_WIDE_INT, true> {};
1449 template <>
1450 struct int_traits <unsigned HOST_WIDE_INT>
1451 : public primitive_int_traits <unsigned HOST_WIDE_INT, false> {};
1454 namespace wi
1456 /* Stores HWI-sized integer VAL, treating it as having signedness SGN
1457 and precision PRECISION. */
1458 struct hwi_with_prec
1460 hwi_with_prec (HOST_WIDE_INT, unsigned int, signop);
1461 HOST_WIDE_INT val;
1462 unsigned int precision;
1463 signop sgn;
1466 hwi_with_prec shwi (HOST_WIDE_INT, unsigned int);
1467 hwi_with_prec uhwi (unsigned HOST_WIDE_INT, unsigned int);
1469 hwi_with_prec minus_one (unsigned int);
1470 hwi_with_prec zero (unsigned int);
1471 hwi_with_prec one (unsigned int);
1472 hwi_with_prec two (unsigned int);
1475 inline wi::hwi_with_prec::hwi_with_prec (HOST_WIDE_INT v, unsigned int p,
1476 signop s)
1477 : val (v), precision (p), sgn (s)
1481 /* Return a signed integer that has value VAL and precision PRECISION. */
1482 inline wi::hwi_with_prec
1483 wi::shwi (HOST_WIDE_INT val, unsigned int precision)
1485 return hwi_with_prec (val, precision, SIGNED);
1488 /* Return an unsigned integer that has value VAL and precision PRECISION. */
1489 inline wi::hwi_with_prec
1490 wi::uhwi (unsigned HOST_WIDE_INT val, unsigned int precision)
1492 return hwi_with_prec (val, precision, UNSIGNED);
1495 /* Return a wide int of -1 with precision PRECISION. */
1496 inline wi::hwi_with_prec
1497 wi::minus_one (unsigned int precision)
1499 return wi::shwi (-1, precision);
1502 /* Return a wide int of 0 with precision PRECISION. */
1503 inline wi::hwi_with_prec
1504 wi::zero (unsigned int precision)
1506 return wi::shwi (0, precision);
1509 /* Return a wide int of 1 with precision PRECISION. */
1510 inline wi::hwi_with_prec
1511 wi::one (unsigned int precision)
1513 return wi::shwi (1, precision);
1516 /* Return a wide int of 2 with precision PRECISION. */
1517 inline wi::hwi_with_prec
1518 wi::two (unsigned int precision)
1520 return wi::shwi (2, precision);
1523 namespace wi
1525 template <>
1526 struct int_traits <wi::hwi_with_prec>
1528 static const enum precision_type precision_type = VAR_PRECISION;
1529 /* hwi_with_prec has an explicitly-given precision, rather than the
1530 precision of HOST_WIDE_INT. */
1531 static const bool host_dependent_precision = false;
1532 static const bool is_sign_extended = true;
1533 static unsigned int get_precision (const wi::hwi_with_prec &);
1534 static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
1535 const wi::hwi_with_prec &);
1539 inline unsigned int
1540 wi::int_traits <wi::hwi_with_prec>::get_precision (const wi::hwi_with_prec &x)
1542 return x.precision;
1545 inline wi::storage_ref
1546 wi::int_traits <wi::hwi_with_prec>::
1547 decompose (HOST_WIDE_INT *scratch, unsigned int precision,
1548 const wi::hwi_with_prec &x)
1550 gcc_checking_assert (precision == x.precision);
1551 scratch[0] = x.val;
1552 if (x.sgn == SIGNED || x.val >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
1553 return wi::storage_ref (scratch, 1, precision);
1554 scratch[1] = 0;
1555 return wi::storage_ref (scratch, 2, precision);
1558 /* Private functions for handling large cases out of line. They take
1559 individual length and array parameters because that is cheaper for
1560 the inline caller than constructing an object on the stack and
1561 passing a reference to it. (Although many callers use wide_int_refs,
1562 we generally want those to be removed by SRA.) */
1563 namespace wi
1565 bool eq_p_large (const HOST_WIDE_INT *, unsigned int,
1566 const HOST_WIDE_INT *, unsigned int, unsigned int);
1567 bool lts_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1568 const HOST_WIDE_INT *, unsigned int);
1569 bool ltu_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1570 const HOST_WIDE_INT *, unsigned int);
1571 int cmps_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1572 const HOST_WIDE_INT *, unsigned int);
1573 int cmpu_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
1574 const HOST_WIDE_INT *, unsigned int);
1575 unsigned int sext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1576 unsigned int,
1577 unsigned int, unsigned int);
1578 unsigned int zext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1579 unsigned int,
1580 unsigned int, unsigned int);
1581 unsigned int set_bit_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1582 unsigned int, unsigned int, unsigned int);
1583 unsigned int lshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1584 unsigned int, unsigned int, unsigned int);
1585 unsigned int lrshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1586 unsigned int, unsigned int, unsigned int,
1587 unsigned int);
1588 unsigned int arshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1589 unsigned int, unsigned int, unsigned int,
1590 unsigned int);
1591 unsigned int and_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1592 const HOST_WIDE_INT *, unsigned int, unsigned int);
1593 unsigned int and_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1594 unsigned int, const HOST_WIDE_INT *,
1595 unsigned int, unsigned int);
1596 unsigned int or_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1597 const HOST_WIDE_INT *, unsigned int, unsigned int);
1598 unsigned int or_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1599 unsigned int, const HOST_WIDE_INT *,
1600 unsigned int, unsigned int);
1601 unsigned int xor_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1602 const HOST_WIDE_INT *, unsigned int, unsigned int);
1603 unsigned int add_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1604 const HOST_WIDE_INT *, unsigned int, unsigned int,
1605 signop, bool *);
1606 unsigned int sub_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
1607 const HOST_WIDE_INT *, unsigned int, unsigned int,
1608 signop, bool *);
1609 unsigned int mul_internal (HOST_WIDE_INT *, const HOST_WIDE_INT *,
1610 unsigned int, const HOST_WIDE_INT *,
1611 unsigned int, unsigned int, signop, bool *,
1612 bool);
1613 unsigned int divmod_internal (HOST_WIDE_INT *, unsigned int *,
1614 HOST_WIDE_INT *, const HOST_WIDE_INT *,
1615 unsigned int, unsigned int,
1616 const HOST_WIDE_INT *,
1617 unsigned int, unsigned int,
1618 signop, bool *);
1621 /* Return the number of bits that integer X can hold. */
1622 template <typename T>
1623 inline unsigned int
1624 wi::get_precision (const T &x)
1626 return wi::int_traits <T>::get_precision (x);
1629 /* Return the number of bits that the result of a binary operation can
1630 hold when the input operands are X and Y. */
1631 template <typename T1, typename T2>
1632 inline unsigned int
1633 wi::get_binary_precision (const T1 &x, const T2 &y)
1635 return get_precision (wi::int_traits <WI_BINARY_RESULT (T1, T2)>::
1636 get_binary_result (x, y));
1639 /* Copy the contents of Y to X, but keeping X's current precision. */
1640 template <typename T1, typename T2>
1641 inline void
1642 wi::copy (T1 &x, const T2 &y)
1644 HOST_WIDE_INT *xval = x.write_val ();
1645 const HOST_WIDE_INT *yval = y.get_val ();
1646 unsigned int len = y.get_len ();
1647 unsigned int i = 0;
1649 xval[i] = yval[i];
1650 while (++i < len);
1651 x.set_len (len, y.is_sign_extended);
1654 /* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
1655 template <typename T>
1656 inline bool
1657 wi::fits_shwi_p (const T &x)
1659 WIDE_INT_REF_FOR (T) xi (x);
1660 return xi.len == 1;
1663 /* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
1664 precision. */
1665 template <typename T>
1666 inline bool
1667 wi::fits_uhwi_p (const T &x)
1669 WIDE_INT_REF_FOR (T) xi (x);
1670 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
1671 return true;
1672 if (xi.len == 1)
1673 return xi.slow () >= 0;
1674 return xi.len == 2 && xi.uhigh () == 0;
1677 /* Return true if X is negative based on the interpretation of SGN.
1678 For UNSIGNED, this is always false. */
1679 template <typename T>
1680 inline bool
1681 wi::neg_p (const T &x, signop sgn)
1683 WIDE_INT_REF_FOR (T) xi (x);
1684 if (sgn == UNSIGNED)
1685 return false;
1686 return xi.sign_mask () < 0;
1689 /* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
1690 template <typename T>
1691 inline HOST_WIDE_INT
1692 wi::sign_mask (const T &x)
1694 WIDE_INT_REF_FOR (T) xi (x);
1695 return xi.sign_mask ();
1698 /* Return true if X == Y. X and Y must be binary-compatible. */
1699 template <typename T1, typename T2>
1700 inline bool
1701 wi::eq_p (const T1 &x, const T2 &y)
1703 unsigned int precision = get_binary_precision (x, y);
1704 WIDE_INT_REF_FOR (T1) xi (x, precision);
1705 WIDE_INT_REF_FOR (T2) yi (y, precision);
1706 if (xi.is_sign_extended && yi.is_sign_extended)
1708 /* This case reduces to array equality. */
1709 if (xi.len != yi.len)
1710 return false;
1711 unsigned int i = 0;
1713 if (xi.val[i] != yi.val[i])
1714 return false;
1715 while (++i != xi.len);
1716 return true;
1718 if (__builtin_expect (yi.len == 1, true))
1720 /* XI is only equal to YI if it too has a single HWI. */
1721 if (xi.len != 1)
1722 return false;
1723 /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
1724 with 0 are simple. */
1725 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1726 return xi.val[0] == 0;
1727 /* Otherwise flush out any excess bits first. */
1728 unsigned HOST_WIDE_INT diff = xi.val[0] ^ yi.val[0];
1729 int excess = HOST_BITS_PER_WIDE_INT - precision;
1730 if (excess > 0)
1731 diff <<= excess;
1732 return diff == 0;
1734 return eq_p_large (xi.val, xi.len, yi.val, yi.len, precision);
1737 /* Return true if X != Y. X and Y must be binary-compatible. */
1738 template <typename T1, typename T2>
1739 inline bool
1740 wi::ne_p (const T1 &x, const T2 &y)
1742 return !eq_p (x, y);
1745 /* Return true if X < Y when both are treated as signed values. */
1746 template <typename T1, typename T2>
1747 inline bool
1748 wi::lts_p (const T1 &x, const T2 &y)
1750 unsigned int precision = get_binary_precision (x, y);
1751 WIDE_INT_REF_FOR (T1) xi (x, precision);
1752 WIDE_INT_REF_FOR (T2) yi (y, precision);
1753 /* We optimize x < y, where y is 64 or fewer bits. */
1754 if (wi::fits_shwi_p (yi))
1756 /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
1757 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1758 return neg_p (xi);
1759 /* If x fits directly into a shwi, we can compare directly. */
1760 if (wi::fits_shwi_p (xi))
1761 return xi.to_shwi () < yi.to_shwi ();
1762 /* If x doesn't fit and is negative, then it must be more
1763 negative than any value in y, and hence smaller than y. */
1764 if (neg_p (xi))
1765 return true;
1766 /* If x is positive, then it must be larger than any value in y,
1767 and hence greater than y. */
1768 return false;
1770 /* Optimize the opposite case, if it can be detected at compile time. */
1771 if (STATIC_CONSTANT_P (xi.len == 1))
1772 /* If YI is negative it is lower than the least HWI.
1773 If YI is positive it is greater than the greatest HWI. */
1774 return !neg_p (yi);
1775 return lts_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1778 /* Return true if X < Y when both are treated as unsigned values. */
1779 template <typename T1, typename T2>
1780 inline bool
1781 wi::ltu_p (const T1 &x, const T2 &y)
1783 unsigned int precision = get_binary_precision (x, y);
1784 WIDE_INT_REF_FOR (T1) xi (x, precision);
1785 WIDE_INT_REF_FOR (T2) yi (y, precision);
1786 /* Optimize comparisons with constants. */
1787 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1788 return xi.len == 1 && xi.to_uhwi () < (unsigned HOST_WIDE_INT) yi.val[0];
1789 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1790 return yi.len != 1 || yi.to_uhwi () > (unsigned HOST_WIDE_INT) xi.val[0];
1791 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1792 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1793 values does not change the result. */
1794 if (__builtin_expect (xi.len + yi.len == 2, true))
1796 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1797 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1798 return xl < yl;
1800 return ltu_p_large (xi.val, xi.len, precision, yi.val, yi.len);
1803 /* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
1804 template <typename T1, typename T2>
1805 inline bool
1806 wi::lt_p (const T1 &x, const T2 &y, signop sgn)
1808 if (sgn == SIGNED)
1809 return lts_p (x, y);
1810 else
1811 return ltu_p (x, y);
1814 /* Return true if X <= Y when both are treated as signed values. */
1815 template <typename T1, typename T2>
1816 inline bool
1817 wi::les_p (const T1 &x, const T2 &y)
1819 return !lts_p (y, x);
1822 /* Return true if X <= Y when both are treated as unsigned values. */
1823 template <typename T1, typename T2>
1824 inline bool
1825 wi::leu_p (const T1 &x, const T2 &y)
1827 return !ltu_p (y, x);
1830 /* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
1831 template <typename T1, typename T2>
1832 inline bool
1833 wi::le_p (const T1 &x, const T2 &y, signop sgn)
1835 if (sgn == SIGNED)
1836 return les_p (x, y);
1837 else
1838 return leu_p (x, y);
1841 /* Return true if X > Y when both are treated as signed values. */
1842 template <typename T1, typename T2>
1843 inline bool
1844 wi::gts_p (const T1 &x, const T2 &y)
1846 return lts_p (y, x);
1849 /* Return true if X > Y when both are treated as unsigned values. */
1850 template <typename T1, typename T2>
1851 inline bool
1852 wi::gtu_p (const T1 &x, const T2 &y)
1854 return ltu_p (y, x);
1857 /* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
1858 template <typename T1, typename T2>
1859 inline bool
1860 wi::gt_p (const T1 &x, const T2 &y, signop sgn)
1862 if (sgn == SIGNED)
1863 return gts_p (x, y);
1864 else
1865 return gtu_p (x, y);
1868 /* Return true if X >= Y when both are treated as signed values. */
1869 template <typename T1, typename T2>
1870 inline bool
1871 wi::ges_p (const T1 &x, const T2 &y)
1873 return !lts_p (x, y);
1876 /* Return true if X >= Y when both are treated as unsigned values. */
1877 template <typename T1, typename T2>
1878 inline bool
1879 wi::geu_p (const T1 &x, const T2 &y)
1881 return !ltu_p (x, y);
1884 /* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
1885 template <typename T1, typename T2>
1886 inline bool
1887 wi::ge_p (const T1 &x, const T2 &y, signop sgn)
1889 if (sgn == SIGNED)
1890 return ges_p (x, y);
1891 else
1892 return geu_p (x, y);
1895 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1896 as signed values. */
1897 template <typename T1, typename T2>
1898 inline int
1899 wi::cmps (const T1 &x, const T2 &y)
1901 unsigned int precision = get_binary_precision (x, y);
1902 WIDE_INT_REF_FOR (T1) xi (x, precision);
1903 WIDE_INT_REF_FOR (T2) yi (y, precision);
1904 if (wi::fits_shwi_p (yi))
1906 /* Special case for comparisons with 0. */
1907 if (STATIC_CONSTANT_P (yi.val[0] == 0))
1908 return neg_p (xi) ? -1 : !(xi.len == 1 && xi.val[0] == 0);
1909 /* If x fits into a signed HWI, we can compare directly. */
1910 if (wi::fits_shwi_p (xi))
1912 HOST_WIDE_INT xl = xi.to_shwi ();
1913 HOST_WIDE_INT yl = yi.to_shwi ();
1914 return xl < yl ? -1 : xl > yl;
1916 /* If x doesn't fit and is negative, then it must be more
1917 negative than any signed HWI, and hence smaller than y. */
1918 if (neg_p (xi))
1919 return -1;
1920 /* If x is positive, then it must be larger than any signed HWI,
1921 and hence greater than y. */
1922 return 1;
1924 /* Optimize the opposite case, if it can be detected at compile time. */
1925 if (STATIC_CONSTANT_P (xi.len == 1))
1926 /* If YI is negative it is lower than the least HWI.
1927 If YI is positive it is greater than the greatest HWI. */
1928 return neg_p (yi) ? 1 : -1;
1929 return cmps_large (xi.val, xi.len, precision, yi.val, yi.len);
1932 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
1933 as unsigned values. */
1934 template <typename T1, typename T2>
1935 inline int
1936 wi::cmpu (const T1 &x, const T2 &y)
1938 unsigned int precision = get_binary_precision (x, y);
1939 WIDE_INT_REF_FOR (T1) xi (x, precision);
1940 WIDE_INT_REF_FOR (T2) yi (y, precision);
1941 /* Optimize comparisons with constants. */
1942 if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
1944 /* If XI doesn't fit in a HWI then it must be larger than YI. */
1945 if (xi.len != 1)
1946 return 1;
1947 /* Otherwise compare directly. */
1948 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1949 unsigned HOST_WIDE_INT yl = yi.val[0];
1950 return xl < yl ? -1 : xl > yl;
1952 if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
1954 /* If YI doesn't fit in a HWI then it must be larger than XI. */
1955 if (yi.len != 1)
1956 return -1;
1957 /* Otherwise compare directly. */
1958 unsigned HOST_WIDE_INT xl = xi.val[0];
1959 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1960 return xl < yl ? -1 : xl > yl;
1962 /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
1963 for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
1964 values does not change the result. */
1965 if (__builtin_expect (xi.len + yi.len == 2, true))
1967 unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
1968 unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
1969 return xl < yl ? -1 : xl > yl;
1971 return cmpu_large (xi.val, xi.len, precision, yi.val, yi.len);
1974 /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
1975 X and Y indicated by SGN. */
1976 template <typename T1, typename T2>
1977 inline int
1978 wi::cmp (const T1 &x, const T2 &y, signop sgn)
1980 if (sgn == SIGNED)
1981 return cmps (x, y);
1982 else
1983 return cmpu (x, y);
1986 /* Return ~x. */
1987 template <typename T>
1988 inline WI_UNARY_RESULT (T)
1989 wi::bit_not (const T &x)
1991 WI_UNARY_RESULT_VAR (result, val, T, x);
1992 WIDE_INT_REF_FOR (T) xi (x, get_precision (result));
1993 for (unsigned int i = 0; i < xi.len; ++i)
1994 val[i] = ~xi.val[i];
1995 result.set_len (xi.len);
1996 return result;
1999 /* Return -x. */
2000 template <typename T>
2001 inline WI_UNARY_RESULT (T)
2002 wi::neg (const T &x)
2004 return sub (0, x);
2007 /* Return -x. Indicate in *OVERFLOW if X is the minimum signed value. */
2008 template <typename T>
2009 inline WI_UNARY_RESULT (T)
2010 wi::neg (const T &x, bool *overflow)
2012 *overflow = only_sign_bit_p (x);
2013 return sub (0, x);
2016 /* Return the absolute value of x. */
2017 template <typename T>
2018 inline WI_UNARY_RESULT (T)
2019 wi::abs (const T &x)
2021 return neg_p (x) ? neg (x) : WI_UNARY_RESULT (T) (x);
2024 /* Return the result of sign-extending the low OFFSET bits of X. */
2025 template <typename T>
2026 inline WI_UNARY_RESULT (T)
2027 wi::sext (const T &x, unsigned int offset)
2029 WI_UNARY_RESULT_VAR (result, val, T, x);
2030 unsigned int precision = get_precision (result);
2031 WIDE_INT_REF_FOR (T) xi (x, precision);
2033 if (offset <= HOST_BITS_PER_WIDE_INT)
2035 val[0] = sext_hwi (xi.ulow (), offset);
2036 result.set_len (1, true);
2038 else
2039 result.set_len (sext_large (val, xi.val, xi.len, precision, offset));
2040 return result;
2043 /* Return the result of zero-extending the low OFFSET bits of X. */
2044 template <typename T>
2045 inline WI_UNARY_RESULT (T)
2046 wi::zext (const T &x, unsigned int offset)
2048 WI_UNARY_RESULT_VAR (result, val, T, x);
2049 unsigned int precision = get_precision (result);
2050 WIDE_INT_REF_FOR (T) xi (x, precision);
2052 /* This is not just an optimization, it is actually required to
2053 maintain canonization. */
2054 if (offset >= precision)
2056 wi::copy (result, xi);
2057 return result;
2060 /* In these cases we know that at least the top bit will be clear,
2061 so no sign extension is necessary. */
2062 if (offset < HOST_BITS_PER_WIDE_INT)
2064 val[0] = zext_hwi (xi.ulow (), offset);
2065 result.set_len (1, true);
2067 else
2068 result.set_len (zext_large (val, xi.val, xi.len, precision, offset), true);
2069 return result;
2072 /* Return the result of extending the low OFFSET bits of X according to
2073 signedness SGN. */
2074 template <typename T>
2075 inline WI_UNARY_RESULT (T)
2076 wi::ext (const T &x, unsigned int offset, signop sgn)
2078 return sgn == SIGNED ? sext (x, offset) : zext (x, offset);
2081 /* Return an integer that represents X | (1 << bit). */
2082 template <typename T>
2083 inline WI_UNARY_RESULT (T)
2084 wi::set_bit (const T &x, unsigned int bit)
2086 WI_UNARY_RESULT_VAR (result, val, T, x);
2087 unsigned int precision = get_precision (result);
2088 WIDE_INT_REF_FOR (T) xi (x, precision);
2089 if (precision <= HOST_BITS_PER_WIDE_INT)
2091 val[0] = xi.ulow () | ((unsigned HOST_WIDE_INT) 1 << bit);
2092 result.set_len (1);
2094 else
2095 result.set_len (set_bit_large (val, xi.val, xi.len, precision, bit));
2096 return result;
2099 /* Return the mininum of X and Y, treating them both as having
2100 signedness SGN. */
2101 template <typename T1, typename T2>
2102 inline WI_BINARY_RESULT (T1, T2)
2103 wi::min (const T1 &x, const T2 &y, signop sgn)
2105 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2106 unsigned int precision = get_precision (result);
2107 if (wi::le_p (x, y, sgn))
2108 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2109 else
2110 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2111 return result;
2114 /* Return the minimum of X and Y, treating both as signed values. */
2115 template <typename T1, typename T2>
2116 inline WI_BINARY_RESULT (T1, T2)
2117 wi::smin (const T1 &x, const T2 &y)
2119 return min (x, y, SIGNED);
2122 /* Return the minimum of X and Y, treating both as unsigned values. */
2123 template <typename T1, typename T2>
2124 inline WI_BINARY_RESULT (T1, T2)
2125 wi::umin (const T1 &x, const T2 &y)
2127 return min (x, y, UNSIGNED);
2130 /* Return the maxinum of X and Y, treating them both as having
2131 signedness SGN. */
2132 template <typename T1, typename T2>
2133 inline WI_BINARY_RESULT (T1, T2)
2134 wi::max (const T1 &x, const T2 &y, signop sgn)
2136 WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
2137 unsigned int precision = get_precision (result);
2138 if (wi::ge_p (x, y, sgn))
2139 wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
2140 else
2141 wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
2142 return result;
2145 /* Return the maximum of X and Y, treating both as signed values. */
2146 template <typename T1, typename T2>
2147 inline WI_BINARY_RESULT (T1, T2)
2148 wi::smax (const T1 &x, const T2 &y)
2150 return max (x, y, SIGNED);
2153 /* Return the maximum of X and Y, treating both as unsigned values. */
2154 template <typename T1, typename T2>
2155 inline WI_BINARY_RESULT (T1, T2)
2156 wi::umax (const T1 &x, const T2 &y)
2158 return max (x, y, UNSIGNED);
2161 /* Return X & Y. */
2162 template <typename T1, typename T2>
2163 inline WI_BINARY_RESULT (T1, T2)
2164 wi::bit_and (const T1 &x, const T2 &y)
2166 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2167 unsigned int precision = get_precision (result);
2168 WIDE_INT_REF_FOR (T1) xi (x, precision);
2169 WIDE_INT_REF_FOR (T2) yi (y, precision);
2170 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2171 if (__builtin_expect (xi.len + yi.len == 2, true))
2173 val[0] = xi.ulow () & yi.ulow ();
2174 result.set_len (1, is_sign_extended);
2176 else
2177 result.set_len (and_large (val, xi.val, xi.len, yi.val, yi.len,
2178 precision), is_sign_extended);
2179 return result;
2182 /* Return X & ~Y. */
2183 template <typename T1, typename T2>
2184 inline WI_BINARY_RESULT (T1, T2)
2185 wi::bit_and_not (const T1 &x, const T2 &y)
2187 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2188 unsigned int precision = get_precision (result);
2189 WIDE_INT_REF_FOR (T1) xi (x, precision);
2190 WIDE_INT_REF_FOR (T2) yi (y, precision);
2191 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2192 if (__builtin_expect (xi.len + yi.len == 2, true))
2194 val[0] = xi.ulow () & ~yi.ulow ();
2195 result.set_len (1, is_sign_extended);
2197 else
2198 result.set_len (and_not_large (val, xi.val, xi.len, yi.val, yi.len,
2199 precision), is_sign_extended);
2200 return result;
2203 /* Return X | Y. */
2204 template <typename T1, typename T2>
2205 inline WI_BINARY_RESULT (T1, T2)
2206 wi::bit_or (const T1 &x, const T2 &y)
2208 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2209 unsigned int precision = get_precision (result);
2210 WIDE_INT_REF_FOR (T1) xi (x, precision);
2211 WIDE_INT_REF_FOR (T2) yi (y, precision);
2212 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2213 if (__builtin_expect (xi.len + yi.len == 2, true))
2215 val[0] = xi.ulow () | yi.ulow ();
2216 result.set_len (1, is_sign_extended);
2218 else
2219 result.set_len (or_large (val, xi.val, xi.len,
2220 yi.val, yi.len, precision), is_sign_extended);
2221 return result;
2224 /* Return X | ~Y. */
2225 template <typename T1, typename T2>
2226 inline WI_BINARY_RESULT (T1, T2)
2227 wi::bit_or_not (const T1 &x, const T2 &y)
2229 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2230 unsigned int precision = get_precision (result);
2231 WIDE_INT_REF_FOR (T1) xi (x, precision);
2232 WIDE_INT_REF_FOR (T2) yi (y, precision);
2233 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2234 if (__builtin_expect (xi.len + yi.len == 2, true))
2236 val[0] = xi.ulow () | ~yi.ulow ();
2237 result.set_len (1, is_sign_extended);
2239 else
2240 result.set_len (or_not_large (val, xi.val, xi.len, yi.val, yi.len,
2241 precision), is_sign_extended);
2242 return result;
2245 /* Return X ^ Y. */
2246 template <typename T1, typename T2>
2247 inline WI_BINARY_RESULT (T1, T2)
2248 wi::bit_xor (const T1 &x, const T2 &y)
2250 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2251 unsigned int precision = get_precision (result);
2252 WIDE_INT_REF_FOR (T1) xi (x, precision);
2253 WIDE_INT_REF_FOR (T2) yi (y, precision);
2254 bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
2255 if (__builtin_expect (xi.len + yi.len == 2, true))
2257 val[0] = xi.ulow () ^ yi.ulow ();
2258 result.set_len (1, is_sign_extended);
2260 else
2261 result.set_len (xor_large (val, xi.val, xi.len,
2262 yi.val, yi.len, precision), is_sign_extended);
2263 return result;
2266 /* Return X + Y. */
2267 template <typename T1, typename T2>
2268 inline WI_BINARY_RESULT (T1, T2)
2269 wi::add (const T1 &x, const T2 &y)
2271 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2272 unsigned int precision = get_precision (result);
2273 WIDE_INT_REF_FOR (T1) xi (x, precision);
2274 WIDE_INT_REF_FOR (T2) yi (y, precision);
2275 if (precision <= HOST_BITS_PER_WIDE_INT)
2277 val[0] = xi.ulow () + yi.ulow ();
2278 result.set_len (1);
2280 /* If the precision is known at compile time to be greater than
2281 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2282 knowing that (a) all bits in those HWIs are significant and
2283 (b) the result has room for at least two HWIs. This provides
2284 a fast path for things like offset_int and widest_int.
2286 The STATIC_CONSTANT_P test prevents this path from being
2287 used for wide_ints. wide_ints with precisions greater than
2288 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2289 point handling them inline. */
2290 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2291 && __builtin_expect (xi.len + yi.len == 2, true))
2293 unsigned HOST_WIDE_INT xl = xi.ulow ();
2294 unsigned HOST_WIDE_INT yl = yi.ulow ();
2295 unsigned HOST_WIDE_INT resultl = xl + yl;
2296 val[0] = resultl;
2297 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2298 result.set_len (1 + (((resultl ^ xl) & (resultl ^ yl))
2299 >> (HOST_BITS_PER_WIDE_INT - 1)));
2301 else
2302 result.set_len (add_large (val, xi.val, xi.len,
2303 yi.val, yi.len, precision,
2304 UNSIGNED, 0));
2305 return result;
2308 /* Return X + Y. Treat X and Y as having the signednes given by SGN
2309 and indicate in *OVERFLOW whether the operation overflowed. */
2310 template <typename T1, typename T2>
2311 inline WI_BINARY_RESULT (T1, T2)
2312 wi::add (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2314 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2315 unsigned int precision = get_precision (result);
2316 WIDE_INT_REF_FOR (T1) xi (x, precision);
2317 WIDE_INT_REF_FOR (T2) yi (y, precision);
2318 if (precision <= HOST_BITS_PER_WIDE_INT)
2320 unsigned HOST_WIDE_INT xl = xi.ulow ();
2321 unsigned HOST_WIDE_INT yl = yi.ulow ();
2322 unsigned HOST_WIDE_INT resultl = xl + yl;
2323 if (sgn == SIGNED)
2324 *overflow = (((resultl ^ xl) & (resultl ^ yl))
2325 >> (precision - 1)) & 1;
2326 else
2327 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2328 < (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2329 val[0] = resultl;
2330 result.set_len (1);
2332 else
2333 result.set_len (add_large (val, xi.val, xi.len,
2334 yi.val, yi.len, precision,
2335 sgn, overflow));
2336 return result;
2339 /* Return X - Y. */
2340 template <typename T1, typename T2>
2341 inline WI_BINARY_RESULT (T1, T2)
2342 wi::sub (const T1 &x, const T2 &y)
2344 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2345 unsigned int precision = get_precision (result);
2346 WIDE_INT_REF_FOR (T1) xi (x, precision);
2347 WIDE_INT_REF_FOR (T2) yi (y, precision);
2348 if (precision <= HOST_BITS_PER_WIDE_INT)
2350 val[0] = xi.ulow () - yi.ulow ();
2351 result.set_len (1);
2353 /* If the precision is known at compile time to be greater than
2354 HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
2355 knowing that (a) all bits in those HWIs are significant and
2356 (b) the result has room for at least two HWIs. This provides
2357 a fast path for things like offset_int and widest_int.
2359 The STATIC_CONSTANT_P test prevents this path from being
2360 used for wide_ints. wide_ints with precisions greater than
2361 HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
2362 point handling them inline. */
2363 else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
2364 && __builtin_expect (xi.len + yi.len == 2, true))
2366 unsigned HOST_WIDE_INT xl = xi.ulow ();
2367 unsigned HOST_WIDE_INT yl = yi.ulow ();
2368 unsigned HOST_WIDE_INT resultl = xl - yl;
2369 val[0] = resultl;
2370 val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
2371 result.set_len (1 + (((resultl ^ xl) & (xl ^ yl))
2372 >> (HOST_BITS_PER_WIDE_INT - 1)));
2374 else
2375 result.set_len (sub_large (val, xi.val, xi.len,
2376 yi.val, yi.len, precision,
2377 UNSIGNED, 0));
2378 return result;
2381 /* Return X - Y. Treat X and Y as having the signednes given by SGN
2382 and indicate in *OVERFLOW whether the operation overflowed. */
2383 template <typename T1, typename T2>
2384 inline WI_BINARY_RESULT (T1, T2)
2385 wi::sub (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2387 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2388 unsigned int precision = get_precision (result);
2389 WIDE_INT_REF_FOR (T1) xi (x, precision);
2390 WIDE_INT_REF_FOR (T2) yi (y, precision);
2391 if (precision <= HOST_BITS_PER_WIDE_INT)
2393 unsigned HOST_WIDE_INT xl = xi.ulow ();
2394 unsigned HOST_WIDE_INT yl = yi.ulow ();
2395 unsigned HOST_WIDE_INT resultl = xl - yl;
2396 if (sgn == SIGNED)
2397 *overflow = (((xl ^ yl) & (resultl ^ xl)) >> (precision - 1)) & 1;
2398 else
2399 *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
2400 > (xl << (HOST_BITS_PER_WIDE_INT - precision)));
2401 val[0] = resultl;
2402 result.set_len (1);
2404 else
2405 result.set_len (sub_large (val, xi.val, xi.len,
2406 yi.val, yi.len, precision,
2407 sgn, overflow));
2408 return result;
2411 /* Return X * Y. */
2412 template <typename T1, typename T2>
2413 inline WI_BINARY_RESULT (T1, T2)
2414 wi::mul (const T1 &x, const T2 &y)
2416 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2417 unsigned int precision = get_precision (result);
2418 WIDE_INT_REF_FOR (T1) xi (x, precision);
2419 WIDE_INT_REF_FOR (T2) yi (y, precision);
2420 if (precision <= HOST_BITS_PER_WIDE_INT)
2422 val[0] = xi.ulow () * yi.ulow ();
2423 result.set_len (1);
2425 else
2426 result.set_len (mul_internal (val, xi.val, xi.len, yi.val, yi.len,
2427 precision, UNSIGNED, 0, false));
2428 return result;
2431 /* Return X * Y. Treat X and Y as having the signednes given by SGN
2432 and indicate in *OVERFLOW whether the operation overflowed. */
2433 template <typename T1, typename T2>
2434 inline WI_BINARY_RESULT (T1, T2)
2435 wi::mul (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2437 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2438 unsigned int precision = get_precision (result);
2439 WIDE_INT_REF_FOR (T1) xi (x, precision);
2440 WIDE_INT_REF_FOR (T2) yi (y, precision);
2441 result.set_len (mul_internal (val, xi.val, xi.len,
2442 yi.val, yi.len, precision,
2443 sgn, overflow, false));
2444 return result;
2447 /* Return X * Y, treating both X and Y as signed values. Indicate in
2448 *OVERFLOW whether the operation overflowed. */
2449 template <typename T1, typename T2>
2450 inline WI_BINARY_RESULT (T1, T2)
2451 wi::smul (const T1 &x, const T2 &y, bool *overflow)
2453 return mul (x, y, SIGNED, overflow);
2456 /* Return X * Y, treating both X and Y as unsigned values. Indicate in
2457 *OVERFLOW whether the operation overflowed. */
2458 template <typename T1, typename T2>
2459 inline WI_BINARY_RESULT (T1, T2)
2460 wi::umul (const T1 &x, const T2 &y, bool *overflow)
2462 return mul (x, y, UNSIGNED, overflow);
2465 /* Perform a widening multiplication of X and Y, extending the values
2466 according to SGN, and return the high part of the result. */
2467 template <typename T1, typename T2>
2468 inline WI_BINARY_RESULT (T1, T2)
2469 wi::mul_high (const T1 &x, const T2 &y, signop sgn)
2471 WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
2472 unsigned int precision = get_precision (result);
2473 WIDE_INT_REF_FOR (T1) xi (x, precision);
2474 WIDE_INT_REF_FOR (T2) yi (y, precision);
2475 result.set_len (mul_internal (val, xi.val, xi.len,
2476 yi.val, yi.len, precision,
2477 sgn, 0, true));
2478 return result;
2481 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2482 signedness given by SGN. Indicate in *OVERFLOW if the result
2483 overflows. */
2484 template <typename T1, typename T2>
2485 inline WI_BINARY_RESULT (T1, T2)
2486 wi::div_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2488 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2489 unsigned int precision = get_precision (quotient);
2490 WIDE_INT_REF_FOR (T1) xi (x, precision);
2491 WIDE_INT_REF_FOR (T2) yi (y);
2493 quotient.set_len (divmod_internal (quotient_val, 0, 0, xi.val, xi.len,
2494 precision,
2495 yi.val, yi.len, yi.precision,
2496 sgn, overflow));
2497 return quotient;
2500 /* Return X / Y, rouding towards 0. Treat X and Y as signed values. */
2501 template <typename T1, typename T2>
2502 inline WI_BINARY_RESULT (T1, T2)
2503 wi::sdiv_trunc (const T1 &x, const T2 &y)
2505 return div_trunc (x, y, SIGNED);
2508 /* Return X / Y, rouding towards 0. Treat X and Y as unsigned values. */
2509 template <typename T1, typename T2>
2510 inline WI_BINARY_RESULT (T1, T2)
2511 wi::udiv_trunc (const T1 &x, const T2 &y)
2513 return div_trunc (x, y, UNSIGNED);
2516 /* Return X / Y, rouding towards -inf. Treat X and Y as having the
2517 signedness given by SGN. Indicate in *OVERFLOW if the result
2518 overflows. */
2519 template <typename T1, typename T2>
2520 inline WI_BINARY_RESULT (T1, T2)
2521 wi::div_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2523 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2524 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2525 unsigned int precision = get_precision (quotient);
2526 WIDE_INT_REF_FOR (T1) xi (x, precision);
2527 WIDE_INT_REF_FOR (T2) yi (y);
2529 unsigned int remainder_len;
2530 quotient.set_len (divmod_internal (quotient_val,
2531 &remainder_len, remainder_val,
2532 xi.val, xi.len, precision,
2533 yi.val, yi.len, yi.precision, sgn,
2534 overflow));
2535 remainder.set_len (remainder_len);
2536 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2537 return quotient - 1;
2538 return quotient;
2541 /* Return X / Y, rouding towards -inf. Treat X and Y as signed values. */
2542 template <typename T1, typename T2>
2543 inline WI_BINARY_RESULT (T1, T2)
2544 wi::sdiv_floor (const T1 &x, const T2 &y)
2546 return div_floor (x, y, SIGNED);
2549 /* Return X / Y, rouding towards -inf. Treat X and Y as unsigned values. */
2550 /* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
2551 template <typename T1, typename T2>
2552 inline WI_BINARY_RESULT (T1, T2)
2553 wi::udiv_floor (const T1 &x, const T2 &y)
2555 return div_floor (x, y, UNSIGNED);
2558 /* Return X / Y, rouding towards +inf. Treat X and Y as having the
2559 signedness given by SGN. Indicate in *OVERFLOW if the result
2560 overflows. */
2561 template <typename T1, typename T2>
2562 inline WI_BINARY_RESULT (T1, T2)
2563 wi::div_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2565 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2566 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2567 unsigned int precision = get_precision (quotient);
2568 WIDE_INT_REF_FOR (T1) xi (x, precision);
2569 WIDE_INT_REF_FOR (T2) yi (y);
2571 unsigned int remainder_len;
2572 quotient.set_len (divmod_internal (quotient_val,
2573 &remainder_len, remainder_val,
2574 xi.val, xi.len, precision,
2575 yi.val, yi.len, yi.precision, sgn,
2576 overflow));
2577 remainder.set_len (remainder_len);
2578 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2579 return quotient + 1;
2580 return quotient;
2583 /* Return X / Y, rouding towards nearest with ties away from zero.
2584 Treat X and Y as having the signedness given by SGN. Indicate
2585 in *OVERFLOW if the result overflows. */
2586 template <typename T1, typename T2>
2587 inline WI_BINARY_RESULT (T1, T2)
2588 wi::div_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2590 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2591 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2592 unsigned int precision = get_precision (quotient);
2593 WIDE_INT_REF_FOR (T1) xi (x, precision);
2594 WIDE_INT_REF_FOR (T2) yi (y);
2596 unsigned int remainder_len;
2597 quotient.set_len (divmod_internal (quotient_val,
2598 &remainder_len, remainder_val,
2599 xi.val, xi.len, precision,
2600 yi.val, yi.len, yi.precision, sgn,
2601 overflow));
2602 remainder.set_len (remainder_len);
2604 if (remainder != 0)
2606 if (sgn == SIGNED)
2608 if (wi::ges_p (wi::abs (remainder),
2609 wi::lrshift (wi::abs (y), 1)))
2611 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2612 return quotient - 1;
2613 else
2614 return quotient + 1;
2617 else
2619 if (wi::geu_p (remainder, wi::lrshift (y, 1)))
2620 return quotient + 1;
2623 return quotient;
2626 /* Return X / Y, rouding towards 0. Treat X and Y as having the
2627 signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
2628 template <typename T1, typename T2>
2629 inline WI_BINARY_RESULT (T1, T2)
2630 wi::divmod_trunc (const T1 &x, const T2 &y, signop sgn,
2631 WI_BINARY_RESULT (T1, T2) *remainder_ptr)
2633 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2634 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2635 unsigned int precision = get_precision (quotient);
2636 WIDE_INT_REF_FOR (T1) xi (x, precision);
2637 WIDE_INT_REF_FOR (T2) yi (y);
2639 unsigned int remainder_len;
2640 quotient.set_len (divmod_internal (quotient_val,
2641 &remainder_len, remainder_val,
2642 xi.val, xi.len, precision,
2643 yi.val, yi.len, yi.precision, sgn, 0));
2644 remainder.set_len (remainder_len);
2646 *remainder_ptr = remainder;
2647 return quotient;
2650 /* Compute X / Y, rouding towards 0, and return the remainder.
2651 Treat X and Y as having the signedness given by SGN. Indicate
2652 in *OVERFLOW if the division overflows. */
2653 template <typename T1, typename T2>
2654 inline WI_BINARY_RESULT (T1, T2)
2655 wi::mod_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2657 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2658 unsigned int precision = get_precision (remainder);
2659 WIDE_INT_REF_FOR (T1) xi (x, precision);
2660 WIDE_INT_REF_FOR (T2) yi (y);
2662 unsigned int remainder_len;
2663 divmod_internal (0, &remainder_len, remainder_val,
2664 xi.val, xi.len, precision,
2665 yi.val, yi.len, yi.precision, sgn, overflow);
2666 remainder.set_len (remainder_len);
2668 return remainder;
2671 /* Compute X / Y, rouding towards 0, and return the remainder.
2672 Treat X and Y as signed values. */
2673 template <typename T1, typename T2>
2674 inline WI_BINARY_RESULT (T1, T2)
2675 wi::smod_trunc (const T1 &x, const T2 &y)
2677 return mod_trunc (x, y, SIGNED);
2680 /* Compute X / Y, rouding towards 0, and return the remainder.
2681 Treat X and Y as unsigned values. */
2682 template <typename T1, typename T2>
2683 inline WI_BINARY_RESULT (T1, T2)
2684 wi::umod_trunc (const T1 &x, const T2 &y)
2686 return mod_trunc (x, y, UNSIGNED);
2689 /* Compute X / Y, rouding towards -inf, and return the remainder.
2690 Treat X and Y as having the signedness given by SGN. Indicate
2691 in *OVERFLOW if the division overflows. */
2692 template <typename T1, typename T2>
2693 inline WI_BINARY_RESULT (T1, T2)
2694 wi::mod_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2696 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2697 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2698 unsigned int precision = get_precision (quotient);
2699 WIDE_INT_REF_FOR (T1) xi (x, precision);
2700 WIDE_INT_REF_FOR (T2) yi (y);
2702 unsigned int remainder_len;
2703 quotient.set_len (divmod_internal (quotient_val,
2704 &remainder_len, remainder_val,
2705 xi.val, xi.len, precision,
2706 yi.val, yi.len, yi.precision, sgn,
2707 overflow));
2708 remainder.set_len (remainder_len);
2710 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
2711 return remainder + y;
2712 return remainder;
2715 /* Compute X / Y, rouding towards -inf, and return the remainder.
2716 Treat X and Y as unsigned values. */
2717 /* ??? Why do we have both this and umod_trunc. Aren't they the same? */
2718 template <typename T1, typename T2>
2719 inline WI_BINARY_RESULT (T1, T2)
2720 wi::umod_floor (const T1 &x, const T2 &y)
2722 return mod_floor (x, y, UNSIGNED);
2725 /* Compute X / Y, rouding towards +inf, and return the remainder.
2726 Treat X and Y as having the signedness given by SGN. Indicate
2727 in *OVERFLOW if the division overflows. */
2728 template <typename T1, typename T2>
2729 inline WI_BINARY_RESULT (T1, T2)
2730 wi::mod_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2732 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2733 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2734 unsigned int precision = get_precision (quotient);
2735 WIDE_INT_REF_FOR (T1) xi (x, precision);
2736 WIDE_INT_REF_FOR (T2) yi (y);
2738 unsigned int remainder_len;
2739 quotient.set_len (divmod_internal (quotient_val,
2740 &remainder_len, remainder_val,
2741 xi.val, xi.len, precision,
2742 yi.val, yi.len, yi.precision, sgn,
2743 overflow));
2744 remainder.set_len (remainder_len);
2746 if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
2747 return remainder - y;
2748 return remainder;
2751 /* Compute X / Y, rouding towards nearest with ties away from zero,
2752 and return the remainder. Treat X and Y as having the signedness
2753 given by SGN. Indicate in *OVERFLOW if the division overflows. */
2754 template <typename T1, typename T2>
2755 inline WI_BINARY_RESULT (T1, T2)
2756 wi::mod_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
2758 WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
2759 WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
2760 unsigned int precision = get_precision (quotient);
2761 WIDE_INT_REF_FOR (T1) xi (x, precision);
2762 WIDE_INT_REF_FOR (T2) yi (y);
2764 unsigned int remainder_len;
2765 quotient.set_len (divmod_internal (quotient_val,
2766 &remainder_len, remainder_val,
2767 xi.val, xi.len, precision,
2768 yi.val, yi.len, yi.precision, sgn,
2769 overflow));
2770 remainder.set_len (remainder_len);
2772 if (remainder != 0)
2774 if (sgn == SIGNED)
2776 if (wi::ges_p (wi::abs (remainder),
2777 wi::lrshift (wi::abs (y), 1)))
2779 if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
2780 return remainder + y;
2781 else
2782 return remainder - y;
2785 else
2787 if (wi::geu_p (remainder, wi::lrshift (y, 1)))
2788 return remainder - y;
2791 return remainder;
2794 /* Return true if X is a multiple of Y, storing X / Y in *RES if so.
2795 Treat X and Y as having the signedness given by SGN. */
2796 template <typename T1, typename T2>
2797 inline bool
2798 wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn,
2799 WI_BINARY_RESULT (T1, T2) *res)
2801 WI_BINARY_RESULT (T1, T2) remainder;
2802 WI_BINARY_RESULT (T1, T2) quotient
2803 = divmod_trunc (x, y, sgn, &remainder);
2804 if (remainder == 0)
2806 *res = quotient;
2807 return true;
2809 return false;
2812 /* Return X << Y. Return 0 if Y is greater than or equal to
2813 the precision of X. */
2814 template <typename T1, typename T2>
2815 inline WI_UNARY_RESULT (T1)
2816 wi::lshift (const T1 &x, const T2 &y)
2818 WI_UNARY_RESULT_VAR (result, val, T1, x);
2819 unsigned int precision = get_precision (result);
2820 WIDE_INT_REF_FOR (T1) xi (x, precision);
2821 WIDE_INT_REF_FOR (T2) yi (y);
2822 /* Handle the simple cases quickly. */
2823 if (geu_p (yi, precision))
2825 val[0] = 0;
2826 result.set_len (1);
2828 else
2830 unsigned int shift = yi.to_uhwi ();
2831 /* For fixed-precision integers like offset_int and widest_int,
2832 handle the case where the shift value is constant and the
2833 result is a single nonnegative HWI (meaning that we don't
2834 need to worry about val[1]). This is particularly common
2835 for converting a byte count to a bit count.
2837 For variable-precision integers like wide_int, handle HWI
2838 and sub-HWI integers inline. */
2839 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2840 ? (STATIC_CONSTANT_P (shift < HOST_BITS_PER_WIDE_INT - 1)
2841 && xi.len == 1
2842 && xi.val[0] <= (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT)
2843 HOST_WIDE_INT_MAX >> shift))
2844 : precision <= HOST_BITS_PER_WIDE_INT)
2846 val[0] = xi.ulow () << shift;
2847 result.set_len (1);
2849 else
2850 result.set_len (lshift_large (val, xi.val, xi.len,
2851 precision, shift));
2853 return result;
2856 /* Return X >> Y, using a logical shift. Return 0 if Y is greater than
2857 or equal to the precision of X. */
2858 template <typename T1, typename T2>
2859 inline WI_UNARY_RESULT (T1)
2860 wi::lrshift (const T1 &x, const T2 &y)
2862 WI_UNARY_RESULT_VAR (result, val, T1, x);
2863 /* Do things in the precision of the input rather than the output,
2864 since the result can be no larger than that. */
2865 WIDE_INT_REF_FOR (T1) xi (x);
2866 WIDE_INT_REF_FOR (T2) yi (y);
2867 /* Handle the simple cases quickly. */
2868 if (geu_p (yi, xi.precision))
2870 val[0] = 0;
2871 result.set_len (1);
2873 else
2875 unsigned int shift = yi.to_uhwi ();
2876 /* For fixed-precision integers like offset_int and widest_int,
2877 handle the case where the shift value is constant and the
2878 shifted value is a single nonnegative HWI (meaning that all
2879 bits above the HWI are zero). This is particularly common
2880 for converting a bit count to a byte count.
2882 For variable-precision integers like wide_int, handle HWI
2883 and sub-HWI integers inline. */
2884 if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
2885 ? xi.len == 1 && xi.val[0] >= 0
2886 : xi.precision <= HOST_BITS_PER_WIDE_INT)
2888 val[0] = xi.to_uhwi () >> shift;
2889 result.set_len (1);
2891 else
2892 result.set_len (lrshift_large (val, xi.val, xi.len, xi.precision,
2893 get_precision (result), shift));
2895 return result;
2898 /* Return X >> Y, using an arithmetic shift. Return a sign mask if
2899 Y is greater than or equal to the precision of X. */
2900 template <typename T1, typename T2>
2901 inline WI_UNARY_RESULT (T1)
2902 wi::arshift (const T1 &x, const T2 &y)
2904 WI_UNARY_RESULT_VAR (result, val, T1, x);
2905 /* Do things in the precision of the input rather than the output,
2906 since the result can be no larger than that. */
2907 WIDE_INT_REF_FOR (T1) xi (x);
2908 WIDE_INT_REF_FOR (T2) yi (y);
2909 /* Handle the simple cases quickly. */
2910 if (geu_p (yi, xi.precision))
2912 val[0] = sign_mask (x);
2913 result.set_len (1);
2915 else
2917 unsigned int shift = yi.to_uhwi ();
2918 if (xi.precision <= HOST_BITS_PER_WIDE_INT)
2920 val[0] = sext_hwi (xi.ulow () >> shift, xi.precision - shift);
2921 result.set_len (1, true);
2923 else
2924 result.set_len (arshift_large (val, xi.val, xi.len, xi.precision,
2925 get_precision (result), shift));
2927 return result;
2930 /* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
2931 logical shift otherwise. */
2932 template <typename T1, typename T2>
2933 inline WI_UNARY_RESULT (T1)
2934 wi::rshift (const T1 &x, const T2 &y, signop sgn)
2936 if (sgn == UNSIGNED)
2937 return lrshift (x, y);
2938 else
2939 return arshift (x, y);
2942 /* Return the result of rotating the low WIDTH bits of X left by Y
2943 bits and zero-extending the result. Use a full-width rotate if
2944 WIDTH is zero. */
2945 template <typename T1, typename T2>
2946 WI_UNARY_RESULT (T1)
2947 wi::lrotate (const T1 &x, const T2 &y, unsigned int width)
2949 unsigned int precision = get_binary_precision (x, x);
2950 if (width == 0)
2951 width = precision;
2952 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
2953 WI_UNARY_RESULT (T1) left = wi::lshift (x, ymod);
2954 WI_UNARY_RESULT (T1) right = wi::lrshift (x, wi::sub (width, ymod));
2955 if (width != precision)
2956 return wi::zext (left, width) | wi::zext (right, width);
2957 return left | right;
2960 /* Return the result of rotating the low WIDTH bits of X right by Y
2961 bits and zero-extending the result. Use a full-width rotate if
2962 WIDTH is zero. */
2963 template <typename T1, typename T2>
2964 WI_UNARY_RESULT (T1)
2965 wi::rrotate (const T1 &x, const T2 &y, unsigned int width)
2967 unsigned int precision = get_binary_precision (x, x);
2968 if (width == 0)
2969 width = precision;
2970 WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
2971 WI_UNARY_RESULT (T1) right = wi::lrshift (x, ymod);
2972 WI_UNARY_RESULT (T1) left = wi::lshift (x, wi::sub (width, ymod));
2973 if (width != precision)
2974 return wi::zext (left, width) | wi::zext (right, width);
2975 return left | right;
2978 /* Return 0 if the number of 1s in X is even and 1 if the number of 1s
2979 is odd. */
2980 inline int
2981 wi::parity (const wide_int_ref &x)
2983 return popcount (x) & 1;
2986 /* Extract WIDTH bits from X, starting at BITPOS. */
2987 template <typename T>
2988 inline unsigned HOST_WIDE_INT
2989 wi::extract_uhwi (const T &x, unsigned int bitpos, unsigned int width)
2991 unsigned precision = get_precision (x);
2992 if (precision < bitpos + width)
2993 precision = bitpos + width;
2994 WIDE_INT_REF_FOR (T) xi (x, precision);
2996 /* Handle this rare case after the above, so that we assert about
2997 bogus BITPOS values. */
2998 if (width == 0)
2999 return 0;
3001 unsigned int start = bitpos / HOST_BITS_PER_WIDE_INT;
3002 unsigned int shift = bitpos % HOST_BITS_PER_WIDE_INT;
3003 unsigned HOST_WIDE_INT res = xi.elt (start);
3004 res >>= shift;
3005 if (shift + width > HOST_BITS_PER_WIDE_INT)
3007 unsigned HOST_WIDE_INT upper = xi.elt (start + 1);
3008 res |= upper << (-shift % HOST_BITS_PER_WIDE_INT);
3010 return zext_hwi (res, width);
3013 /* Return the minimum precision needed to store X with sign SGN. */
3014 template <typename T>
3015 inline unsigned int
3016 wi::min_precision (const T &x, signop sgn)
3018 if (sgn == SIGNED)
3019 return get_precision (x) - clrsb (x);
3020 else
3021 return get_precision (x) - clz (x);
3024 template<typename T>
3025 void
3026 gt_ggc_mx (generic_wide_int <T> *)
3030 template<typename T>
3031 void
3032 gt_pch_nx (generic_wide_int <T> *)
3036 template<typename T>
3037 void
3038 gt_pch_nx (generic_wide_int <T> *, void (*) (void *, void *), void *)
3042 template<int N>
3043 void
3044 gt_ggc_mx (trailing_wide_ints <N> *)
3048 template<int N>
3049 void
3050 gt_pch_nx (trailing_wide_ints <N> *)
3054 template<int N>
3055 void
3056 gt_pch_nx (trailing_wide_ints <N> *, void (*) (void *, void *), void *)
3060 namespace wi
3062 /* Used for overloaded functions in which the only other acceptable
3063 scalar type is a pointer. It stops a plain 0 from being treated
3064 as a null pointer. */
3065 struct never_used1 {};
3066 struct never_used2 {};
3068 wide_int min_value (unsigned int, signop);
3069 wide_int min_value (never_used1 *);
3070 wide_int min_value (never_used2 *);
3071 wide_int max_value (unsigned int, signop);
3072 wide_int max_value (never_used1 *);
3073 wide_int max_value (never_used2 *);
3075 /* FIXME: this is target dependent, so should be elsewhere.
3076 It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
3077 wide_int from_buffer (const unsigned char *, unsigned int);
3079 #ifndef GENERATOR_FILE
3080 void to_mpz (const wide_int_ref &, mpz_t, signop);
3081 #endif
3083 wide_int mask (unsigned int, bool, unsigned int);
3084 wide_int shifted_mask (unsigned int, unsigned int, bool, unsigned int);
3085 wide_int set_bit_in_zero (unsigned int, unsigned int);
3086 wide_int insert (const wide_int &x, const wide_int &y, unsigned int,
3087 unsigned int);
3089 template <typename T>
3090 T mask (unsigned int, bool);
3092 template <typename T>
3093 T shifted_mask (unsigned int, unsigned int, bool);
3095 template <typename T>
3096 T set_bit_in_zero (unsigned int);
3098 unsigned int mask (HOST_WIDE_INT *, unsigned int, bool, unsigned int);
3099 unsigned int shifted_mask (HOST_WIDE_INT *, unsigned int, unsigned int,
3100 bool, unsigned int);
3101 unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
3102 unsigned int, unsigned int, bool);
3105 /* Return a PRECISION-bit integer in which the low WIDTH bits are set
3106 and the other bits are clear, or the inverse if NEGATE_P. */
3107 inline wide_int
3108 wi::mask (unsigned int width, bool negate_p, unsigned int precision)
3110 wide_int result = wide_int::create (precision);
3111 result.set_len (mask (result.write_val (), width, negate_p, precision));
3112 return result;
3115 /* Return a PRECISION-bit integer in which the low START bits are clear,
3116 the next WIDTH bits are set, and the other bits are clear,
3117 or the inverse if NEGATE_P. */
3118 inline wide_int
3119 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p,
3120 unsigned int precision)
3122 wide_int result = wide_int::create (precision);
3123 result.set_len (shifted_mask (result.write_val (), start, width, negate_p,
3124 precision));
3125 return result;
3128 /* Return a PRECISION-bit integer in which bit BIT is set and all the
3129 others are clear. */
3130 inline wide_int
3131 wi::set_bit_in_zero (unsigned int bit, unsigned int precision)
3133 return shifted_mask (bit, 1, false, precision);
3136 /* Return an integer of type T in which the low WIDTH bits are set
3137 and the other bits are clear, or the inverse if NEGATE_P. */
3138 template <typename T>
3139 inline T
3140 wi::mask (unsigned int width, bool negate_p)
3142 STATIC_ASSERT (wi::int_traits<T>::precision);
3143 T result;
3144 result.set_len (mask (result.write_val (), width, negate_p,
3145 wi::int_traits <T>::precision));
3146 return result;
3149 /* Return an integer of type T in which the low START bits are clear,
3150 the next WIDTH bits are set, and the other bits are clear, or the
3151 inverse if NEGATE_P. */
3152 template <typename T>
3153 inline T
3154 wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p)
3156 STATIC_ASSERT (wi::int_traits<T>::precision);
3157 T result;
3158 result.set_len (shifted_mask (result.write_val (), start, width,
3159 negate_p,
3160 wi::int_traits <T>::precision));
3161 return result;
3164 /* Return an integer of type T in which bit BIT is set and all the
3165 others are clear. */
3166 template <typename T>
3167 inline T
3168 wi::set_bit_in_zero (unsigned int bit)
3170 return shifted_mask <T> (bit, 1, false);
3173 #endif /* WIDE_INT_H */